From 7b2bb11e70547ee02a9c8099420aeb606167e806 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 27 Aug 2024 12:48:38 +0530 Subject: [PATCH] Negating a OneElement produces a OneElement (#381) --- src/oneelement.jl | 2 ++ test/runtests.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/oneelement.jl b/src/oneelement.jl index ed375f98..a46f53a4 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -141,6 +141,8 @@ function isone(A::OneElementMatrix) isone(getindex_value(A)) end +-(O::OneElement) = OneElement(-O.val, O.ind, O.axes) + *(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 ad5f50a7..c0f36e7f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2171,6 +2171,7 @@ end @test FillArrays.nzind(e₁) == CartesianIndex(2) @test e₁[2] === e₁[2,1] === e₁[2,1,1] === 1 @test_throws BoundsError e₁[6] + @test -e₁ === OneElement(-1, 2, 5) f₁ = AbstractArray{Float64}(e₁) @test f₁ isa OneElement{Float64,1} @@ -2190,6 +2191,7 @@ end V = OneElement(2, (2,3), (3,4)) @test V == [0 0 0 0; 0 0 2 0; 0 0 0 0] @test FillArrays.nzind(V) == CartesianIndex(2,3) + @test -V == OneElement(-2, (2,3), (3,4)) Vf = AbstractArray{Float64}(V) @test Vf isa OneElement{Float64,2}