From 93e4b6f19a8ec4df35605b3da064372a0814312f Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 22 Dec 2023 17:25:17 +0530 Subject: [PATCH 1/4] zero for OneElement --- src/oneelement.jl | 2 ++ test/runtests.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/oneelement.jl b/src/oneelement.jl index 869f56c8..ab4bed19 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -62,6 +62,8 @@ Base.@propagate_inbounds function Base.setindex(A::AbstractZeros{T,N}, v, kj::Va OneElement(convert(T, v), kj, axes(A)) end +Base.zero(A::OneElement) = Zeros{eltype(A)}(axes(A)) + *(x::OneElement, b::Number) = OneElement(x.val * b, x.ind, x.axes) *(b::Number, x::OneElement) = OneElement(b * x.val, x.ind, x.axes) /(x::OneElement, b::Number) = OneElement(x.val / b, x.ind, x.axes) diff --git a/test/runtests.jl b/test/runtests.jl index 0de16a98..816b1c53 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2218,6 +2218,12 @@ end @test A * (2 + 3.0im) === OneElement(val * (2 + 3.0im), (2,2), (3,4)) == B * (2 + 3.0im) @test A / (2 + 3.0im) === OneElement(val / (2 + 3.0im), (2,2), (3,4)) == B / (2 + 3.0im) end + + @testset "zero" begin + v = OneElement(10, 3, 4) + @test v + zero(v) == v + @test v + zero(v) isa typeof(v) + end end @testset "repeat" begin From 9b47c4a41160d416b296f3c402aa051d4bab941a Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 22 Dec 2023 18:04:56 +0530 Subject: [PATCH 2/4] Specialize iszero --- src/oneelement.jl | 2 ++ test/runtests.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/oneelement.jl b/src/oneelement.jl index ab4bed19..d136d0c5 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -64,6 +64,8 @@ end Base.zero(A::OneElement) = Zeros{eltype(A)}(axes(A)) +Base.iszero(A::OneElement) = iszero(getindex_value(A)) + *(x::OneElement, b::Number) = OneElement(x.val * b, x.ind, x.axes) *(b::Number, x::OneElement) = OneElement(b * x.val, x.ind, x.axes) /(x::OneElement, b::Number) = OneElement(x.val / b, x.ind, x.axes) diff --git a/test/runtests.jl b/test/runtests.jl index 816b1c53..b69658ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2223,6 +2223,10 @@ end v = OneElement(10, 3, 4) @test v + zero(v) == v @test v + zero(v) isa typeof(v) + + @test !iszero(v) + @test iszero(OneElement(0, 3, 4)) + @test iszero(OneElement(0, 5, 4)) end end From e51f01b363b10c6785c9c7261c00d8577962c940 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 22 Dec 2023 18:21:49 +0530 Subject: [PATCH 3/4] Specialize isone --- src/oneelement.jl | 11 +++++++++-- test/runtests.jl | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/oneelement.jl b/src/oneelement.jl index d136d0c5..c4404789 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -62,9 +62,16 @@ Base.@propagate_inbounds function Base.setindex(A::AbstractZeros{T,N}, v, kj::Va OneElement(convert(T, v), kj, axes(A)) end -Base.zero(A::OneElement) = Zeros{eltype(A)}(axes(A)) +zero(A::OneElement) = Zeros{eltype(A)}(axes(A)) -Base.iszero(A::OneElement) = iszero(getindex_value(A)) +iszero(A::OneElement) = iszero(getindex_value(A)) + +function isone(A::OneElementMatrix) + lenA = length(A) + lenA == 0 && return true + lenA > 1 && return false + isone(getindex_value(A)) +end *(x::OneElement, b::Number) = OneElement(x.val * b, x.ind, x.axes) *(b::Number, x::OneElement) = OneElement(b * x.val, x.ind, x.axes) diff --git a/test/runtests.jl b/test/runtests.jl index b69658ad..ec48990f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2219,7 +2219,7 @@ end @test A / (2 + 3.0im) === OneElement(val / (2 + 3.0im), (2,2), (3,4)) == B / (2 + 3.0im) end - @testset "zero" begin + @testset "zero/iszero" begin v = OneElement(10, 3, 4) @test v + zero(v) == v @test v + zero(v) isa typeof(v) @@ -2227,6 +2227,15 @@ end @test !iszero(v) @test iszero(OneElement(0, 3, 4)) @test iszero(OneElement(0, 5, 4)) + @test iszero(OneElement(3, (2,2), (0,0))) + @test iszero(OneElement(3, (2,2), (1,2))) + end + + @testset "isone" begin + @test isone(OneElement(3, (0,0), (0,0))) + @test isone(OneElement(1, (1,1), (1,1))) + @test !isone(OneElement(2, (1,1), (1,1))) + @test !isone(OneElement(1, (2,2), (2,2))) end end From 2e96a5551e7fc040a9fe4aa63f4b73ddb93e5c3a Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 7 Jun 2024 12:46:59 +0530 Subject: [PATCH 4/4] Return a OneElement instead of a Zero --- src/oneelement.jl | 2 +- test/runtests.jl | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/oneelement.jl b/src/oneelement.jl index c4404789..b2f85d2b 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -62,7 +62,7 @@ Base.@propagate_inbounds function Base.setindex(A::AbstractZeros{T,N}, v, kj::Va OneElement(convert(T, v), kj, axes(A)) end -zero(A::OneElement) = Zeros{eltype(A)}(axes(A)) +zero(A::OneElement) = OneElement(zero(A.val), A.ind, A.axes) iszero(A::OneElement) = iszero(getindex_value(A)) diff --git a/test/runtests.jl b/test/runtests.jl index ec48990f..e770041c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2222,13 +2222,17 @@ end @testset "zero/iszero" begin v = OneElement(10, 3, 4) @test v + zero(v) == v - @test v + zero(v) isa typeof(v) + @test typeof(zero(v)) == typeof(v) @test !iszero(v) @test iszero(OneElement(0, 3, 4)) @test iszero(OneElement(0, 5, 4)) @test iszero(OneElement(3, (2,2), (0,0))) @test iszero(OneElement(3, (2,2), (1,2))) + + v = OneElement(SMatrix{2,2}(1:4), 3, 4) + @test v + zero(v) == v + @test typeof(zero(v)) == typeof(v) end @testset "isone" begin