Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small tweaks to rules #713

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/interface/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@
scale::S
end

@inline square(x) = Square(sign(x)^2, norm(x))
@inline square(x) = Square(sign(x)^2 / one(x), norm(x))

@inline root(x::Square) = sqrt(x.arg) * x.scale

@inline Base.zero(::Type{Square{T,S}}) where {T,S} = Square{T,S}(zero(T), zero(S))
@inline Base.zero(::Square{T,S}) where {T,S} = Square{T,S}(zero(T), zero(S))
@inline Base.isone(x::Square) = isone(root(x))

Check warning on line 405 in src/interface/lazy.jl

View check run for this annotation

Codecov / codecov/patch

src/interface/lazy.jl#L405

Added line #L405 was not covered by tests

@inline Base.isinf(x::Finch.Square) = isinf(x.arg) || isinf(x.scale)

Expand Down Expand Up @@ -458,6 +459,7 @@
Power{T,S,E}(zero(T), zero(S), one(E))
@inline Base.zero(x::Power) = Power(zero(x.arg), zero(x.scale), x.exponent)
@inline Base.isinf(x::Finch.Power) = isinf(x.arg) || isinf(x.scale) || isinf(x.exponent)
@inline Base.isone(x::Power) = isone(root(x))

function Base.promote_rule(
::Type{Power{T1,S1,E1}}, ::Type{Power{T2,S2,E2}}
Expand Down
3 changes: 3 additions & 0 deletions src/symbolic/simplify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
(@rule call(norm, ~x::isliteral, ~y) => if iszero(x.val)
x
end),
(@rule call(^, ~x, ~p::isliteral) => if isone(p.val)
x

Check warning on line 123 in src/symbolic/simplify.jl

View check run for this annotation

Codecov / codecov/patch

src/symbolic/simplify.jl#L123

Added line #L123 was not covered by tests
end),
(@rule block(~a1..., sieve(~c, ~b1), sieve(~c, ~b2), ~a2...) =>
block(a1..., sieve(~c, block(b1, b2)), a2...)
),
Expand Down
22 changes: 16 additions & 6 deletions src/symbolic/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@
isidentity(::AbstractAlgebra, ::typeof(&), x) = !ismissing(x) && x == ~(zero(x))
isidentity(::AbstractAlgebra, ::typeof(min), x) = !ismissing(x) && isinf(x) && x > 0
isidentity(::AbstractAlgebra, ::typeof(max), x) = !ismissing(x) && isinf(x) && x < 0
isidentity(::Finch.AbstractAlgebra, ::InitMax{D}, x) where {D} = isequal(x, D)
isidentity(::Finch.AbstractAlgebra, ::InitMin{D}, x) where {D} = isequal(x, D)
function isidentity(::Finch.AbstractAlgebra, ::InitMax{D}, x) where {D}
isequal(x, D) || isequal(x == D, true)

Check warning on line 97 in src/symbolic/symbolic.jl

View check run for this annotation

Codecov / codecov/patch

src/symbolic/symbolic.jl#L96-L97

Added lines #L96 - L97 were not covered by tests
end
function isidentity(::Finch.AbstractAlgebra, ::InitMin{D}, x) where {D}
isequal(x, D) || isequal(x == D, true)

Check warning on line 100 in src/symbolic/symbolic.jl

View check run for this annotation

Codecov / codecov/patch

src/symbolic/symbolic.jl#L99-L100

Added lines #L99 - L100 were not covered by tests
end

function isidentity_by_fn(alg::AbstractAlgebra, ::typeof(minby), x::FinchNode)
if @capture x call(tuple, ~a::isliteral, ~b)
Expand All @@ -112,8 +116,12 @@
end
return false
end
isidentity(::AbstractAlgebra, ::Chooser{Vf}, x) where {Vf} = isequal(x, Vf)
isidentity(::AbstractAlgebra, ::InitWriter{Vf}, x) where {Vf} = isequal(x, Vf)
function isidentity(::AbstractAlgebra, ::Chooser{Vf}, x) where {Vf}
isequal(x, Vf) || isequal(x == Vf, true)
end
function isidentity(::AbstractAlgebra, ::InitWriter{Vf}, x) where {Vf}
isequal(x, Vf) || isequal(x == Vf, true)
end

isannihilator(alg) = (f, x) -> isannihilator(alg, f, x)
function isannihilator(alg, f::FinchNode, x::FinchNode)
Expand Down Expand Up @@ -150,8 +158,10 @@
end
return false
end
isannihilator(::AbstractAlgebra, ::Chooser{Vf}, x) where {Vf} = !isequal(x, Vf)
#isannihilator(::AbstractAlgebra, ::InitWriter{Vf}, x) where {Vf} = !isequal(x, Vf)
function isannihilator(::AbstractAlgebra, ::Chooser{Vf}, x) where {Vf}
!(isequal(x, Vf) || isequal(x == Vf, true))

Check warning on line 162 in src/symbolic/symbolic.jl

View check run for this annotation

Codecov / codecov/patch

src/symbolic/symbolic.jl#L161-L162

Added lines #L161 - L162 were not covered by tests
end
#isannihilator(::AbstractAlgebra, ::InitWriter{Vf}, x) where {Vf} = !(isequal(x, Vf) || isequal(x == Vf, true))

isinverse(alg) = (f, g) -> isinverse(alg, f, g)
function isinverse(alg, f::FinchNode, g::FinchNode)
Expand Down
31 changes: 31 additions & 0 deletions test/suites/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,37 @@ end
@test A * x == C * x
@test A * x == compute(lazy(C) * x)
end

# https://github.com/finch-tensor/Finch.jl/issues/708
let
for p in [-Inf, -3, -2, -1, 0, 1, 2, 3, Inf]
@testset "$p-norm" begin
A_ref = rand(4, 3)
A = Tensor(A_ref)
@test norm(A_ref, p) ≈ norm(A, p)

A_ref = sprand(4, 3, 1.0)
A = Tensor(A_ref)
@test norm(A_ref, p) ≈ norm(A, p)
end
end
end

# https://github.com/finch-tensor/Finch.jl/pull/709
let
A_ref = [1 2 0 4 0; 0 -2 1 0 1]
A = swizzle(Tensor(Dense(SparseList(Element(0))), A_ref), 1, 2)

@test norm(A, 1) == norm(A_ref, 1)
@test norm(A, 2) == norm(A_ref, 2)
@test norm(A, 3) == norm(A_ref, 3)
end

# https://github.com/finch-tensor/Finch.jl/issues/686
let
A = fsprand(5, 5, 3)
@test countstored(A - A) == 3 skip = (key != "default")
end
end
end
end
Expand Down
Loading