Skip to content

Commit fc93acb

Browse files
committed
provide iszero for Operation to help sparse matrix addition and multiplication
1 parent c3d4cbe commit fc93acb

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/operations.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ Base.isequal(::Variable , ::Operation) = false
5454
Base.isequal(::Operation, ::Constant ) = false
5555
Base.isequal(::Constant , ::Operation) = false
5656

57+
# provide iszero for Operations to help sparse addition and multiplication
58+
Base.iszero(O::Operation) = ((O.op == identity) && iszero(O.args[1])) ||
59+
((O.op == +) && all(iszero(arg) for arg in O.args)) ||
60+
((O.op == *) && any(iszero(arg) for arg in O.args))
61+
5762
Base.show(io::IO, O::Operation) = print(io, convert(Expr, O))
5863

5964
# For inv
@@ -73,12 +78,4 @@ Base.convert(::Type{Expr},x::Operation) = Expr(x)
7378
Base.promote_rule(::Type{<:Constant}, ::Type{<:Operation}) = Operation
7479
Base.promote_rule(::Type{<:Operation}, ::Type{<:Constant}) = Operation
7580

76-
# Fix Sparse MatMul
77-
Base.:*(A::SparseMatrixCSC{Operation,S}, x::StridedVector{Operation}) where {S} =
78-
(T = Operation; mul!(similar(x, T, A.m), A, x, true, false))
79-
Base.:*(A::SparseMatrixCSC{Tx,S}, x::StridedVector{Operation}) where {Tx,S} =
80-
(T = LinearAlgebra.promote_op(LinearAlgebra.matprod, Operation, Tx); mul!(similar(x, T, A.m), A, x, true, false))
81-
Base.:*(A::SparseMatrixCSC{Operation,S}, x::StridedVector{Tx}) where {Tx,S} =
82-
(T = LinearAlgebra.promote_op(LinearAlgebra.matprod, Operation, Tx); mul!(similar(x, T, A.m), A, x, true, false))
83-
8481
LinearAlgebra.lu(O::AbstractMatrix{<:Operation};kwargs...) = lu(O,Val(false);kwargs...)

src/variables.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ Get the value of a [`ModelingToolkit.Constant`](@ref).
8989
"""
9090
Base.get(c::Constant) = c.value
9191

92-
Base.iszero(ex::Expression) = isa(ex, Constant) && iszero(ex.value)
92+
Base.iszero(c::Constant) = iszero(c.value)
93+
9394
Base.isone(ex::Expression) = isa(ex, Constant) && isone(ex.value)
9495

9596
# Variables use isequal for equality since == is an Operation

0 commit comments

Comments
 (0)