Skip to content

Commit 6c331f7

Browse files
Merge pull request #323 from albertomercurio/master
Improve ComposedScalarOperator update and concretization
2 parents 9ed6a54 + 9cdd305 commit 6c331f7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/scalar.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ struct ComposedScalarOperator{T, O} <: AbstractSciMLScalarOperator{T}
337337

338338
function ComposedScalarOperator(ops::NTuple{N, AbstractSciMLScalarOperator}) where {N}
339339
@assert !isempty(ops)
340-
T = promote_type(eltype.(ops)...)
340+
T = reduce(Base.promote_eltype, ops)
341341
new{T, typeof(ops)}(ops)
342342
end
343343
end
@@ -395,7 +395,7 @@ end
395395

396396
function Base.convert(T::Type{<:Number}, α::ComposedScalarOperator)
397397
iszero(α) && return zero(T)
398-
prod(convert.(T, α.ops))
398+
prod(concretize, α.ops)
399399
end
400400

401401
function Base.show(io::IO, α::ComposedScalarOperator)
@@ -410,20 +410,22 @@ end
410410
Base.conj(L::ComposedScalarOperator) = ComposedScalarOperator(conj.(L.ops))
411411
Base.:-::AbstractSciMLScalarOperator{T}) where {T} = (-one(T)) * α
412412

413-
function update_coefficients(L::ComposedScalarOperator, u, p, t; kwargs...)
414-
ops = ()
415-
for op in L.ops
416-
ops = (ops..., update_coefficients(op, u, p, t; kwargs...))
413+
@generated function update_coefficients(L::ComposedScalarOperator, u, p, t; kwargs...)
414+
N = length(L.parameters[2].parameters)
415+
quote
416+
ops = Base.@ntuple $N i -> update_coefficients(L.ops[i], u, p, t; kwargs...)
417+
ComposedScalarOperator(ops)
417418
end
418-
419-
ComposedScalarOperator(ops)
420419
end
421-
function update_coefficients!(L::ComposedScalarOperator, u, p, t; kwargs...)
422-
for op in L.ops
423-
update_coefficients!(op, u, p, t; kwargs...)
420+
421+
@generated function update_coefficients!(L::ComposedScalarOperator, u, p, t; kwargs...)
422+
N = length(L.parameters[2].parameters)
423+
quote
424+
Base.@nexprs $N i -> update_coefficients!(L.ops[i], u, p, t; kwargs...)
425+
nothing
424426
end
425-
nothing
426427
end
428+
427429
function::ComposedScalarOperator)(v::AbstractArray, u, p, t; kwargs...)
428430
α = update_coefficients(α, u, p, t; kwargs...)
429431
convert(Number, α) * v

0 commit comments

Comments
 (0)