diff --git a/src/oneelement.jl b/src/oneelement.jl index 7cffbee4..7fc63594 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -115,6 +115,17 @@ Base.@propagate_inbounds function Base.setindex(A::AbstractZeros{T,N}, v, kj::Va OneElement(convert(T, v), kj, axes(A)) end +zero(A::OneElement) = OneElement(zero(A.val), A.ind, A.axes) + +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) /(x::OneElement, b::Number) = OneElement(x.val / b, x.ind, x.axes) diff --git a/test/runtests.jl b/test/runtests.jl index 1e066665..08bee6ac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2501,6 +2501,28 @@ end @test A / (2 + 3.0im) === OneElement(val / (2 + 3.0im), (2,2), (3,4)) == B / (2 + 3.0im) end + @testset "zero/iszero" begin + v = OneElement(10, 3, 4) + @test v + zero(v) == 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 + @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 @testset "tril/triu" begin for A in (OneElement(3, (4,2), (4,5)), OneElement(3, (2,3), (4,5)), OneElement(3, (3,3), (4,5)))