Skip to content

Commit

Permalink
Negating a OneElement produces a OneElement (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Aug 27, 2024
1 parent 6bab762 commit 7b2bb11
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit 7b2bb11

Please sign in to comment.