Skip to content

Commit 6454d33

Browse files
Remove allocating methods on MatrixOperators
1 parent 13014f9 commit 6454d33

File tree

2 files changed

+8
-33
lines changed

2 files changed

+8
-33
lines changed

src/basic.jl

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,11 @@ end
243243

244244
# constructors
245245
for T in SCALINGNUMBERTYPES[2:end]
246-
@eval ScaledOperator::$T, L::AbstractSciMLOperator) = ScaledOperator(
247-
ScalarOperator(λ),
248-
L)
246+
@eval function ScaledOperator::$T, L::AbstractSciMLOperator)
247+
T2 = Base.promote_eltype(λ, L)
248+
Λ = λ isa UniformScaling ? UniformScaling(T2.λ)) : T2(λ)
249+
ScaledOperator(ScalarOperator(Λ), L)
250+
end
249251
end
250252

251253
for T in SCALINGNUMBERTYPES
@@ -276,28 +278,16 @@ for T in SCALINGNUMBERTYPES[2:end]
276278
isconstant(L.λ) && return ScaledOperator* L.λ, L.L)
277279
return ScaledOperator(L.λ, α * L.L) # Try to propagate the rule
278280
end
279-
@eval function Base.:*::$T, L::MatrixOperator)
280-
isconstant(L) && return MatrixOperator* L.A)
281-
return ScaledOperator(α, L) # Going back to the generic case
282-
end
283-
@eval function Base.:*(L::MatrixOperator, α::$T)
284-
isconstant(L) && return MatrixOperator* L.A)
285-
return ScaledOperator(α, L) # Going back to the generic case
286-
end
287281
end
288282

289283
Base.:+(L::AbstractSciMLOperator) = L
290-
Base.:-(L::AbstractSciMLOperator) = ScaledOperator(-true, L)
284+
Base.:-(L::AbstractSciMLOperator{T}) where T = ScaledOperator(-one(T), L)
291285

292286
# Special cases for constant scalars. These simplify the structure when applicable
293287
function Base.:-(L::ScaledOperator)
294288
isconstant(L.λ) && return ScaledOperator(-L.λ, L.L)
295289
return ScaledOperator(L.λ, -L.L) # Try to propagate the rule
296290
end
297-
function Base.:-(L::MatrixOperator)
298-
isconstant(L) && return MatrixOperator(-L.A)
299-
return ScaledOperator(-true, L) # Going back to the generic case
300-
end
301291

302292
function Base.convert(::Type{AbstractMatrix}, L::ScaledOperator)
303293
convert(Number, L.λ) * convert(AbstractMatrix, L.L)

test/basic.jl

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,9 @@ end
150150

151151
# Test unary - on constant MatrixOperator (simplified to MatrixOperator)
152152
minusA = -A
153-
@test minusA isa MatrixOperator
153+
@test minusA isa ScaledOperator
154154
@test minusA * v -A.A * v
155-
156-
# Test unary - on non-constant MatrixOperator (falls back to ScaledOperator)
157-
func(A, u, p, t) = t
158-
timeDepA = MatrixOperator(A.A; update_func = func)
159-
minusTimeDepA = -timeDepA
160-
@test minusTimeDepA isa ScaledOperator # Can't simplify, uses generic fallback
161-
162-
# Test unary - on non-constant ScaledOperator (propagates to inner operator)
163-
timeDepScaled = ScalarOperator(0.0, func) * A
164-
@test !isconstant(timeDepScaled)
165-
minusTimeDepScaled = -timeDepScaled
166-
@test minusTimeDepScaled.λ isa ScalarOperator && minusTimeDepScaled.L isa MatrixOperator # Propagates negation to inner MatrixOperator
167-
168-
# Test double negation
169-
@test -(-A) isa MatrixOperator
170-
@test (-(-A)) * v A * v
155+
@test eltype(minusA.λ) == eltype(A.A)
171156
end
172157

173158
@testset "ScaledOperator" begin

0 commit comments

Comments
 (0)