Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ struct AddedOperator{T,
function AddedOperator(ops)
@assert !isempty(ops)
_check_AddedOperator_sizes(ops)
T = promote_type(eltype.(ops)...)
T = mapreduce(eltype, promote_type, ops)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this doesn't allocate? Usually mapreduce is expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change it because the broadcasting was actually making type instabilities.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh it's on a tuple, that works.

new{T, typeof(ops)}(ops)
end
end
Expand Down Expand Up @@ -476,9 +476,13 @@ function Base.:+(Z::NullOperator, A::AddedOperator)
A
end

Base.:-(A::AddedOperator) = AddedOperator(map(-, A.ops))
Base.:-(A::AbstractSciMLOperator, B::AbstractSciMLOperator) = AddedOperator(A, -B)
Base.:-(A::AbstractSciMLOperator, B::AbstractMatrix) = A - MatrixOperator(B)
Base.:-(A::AbstractMatrix, B::AbstractSciMLOperator) = MatrixOperator(A) - B
Base.:-(A::AddedOperator, B::AbstractSciMLOperator) = AddedOperator(A.ops..., -B)
Base.:-(A::AbstractSciMLOperator, B::AddedOperator) = AddedOperator(A, (-B).ops...)
Base.:-(A::AddedOperator, B::AddedOperator) = AddedOperator(A.ops..., (-B).ops...)

for op in (:+, :-)
for T in SCALINGNUMBERTYPES
Expand Down
43 changes: 18 additions & 25 deletions test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,33 +278,26 @@ end
end

## Time-Dependent Coefficients

for T in (Float32, Float64, ComplexF32, ComplexF64)
N = 100
A1_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
A2_sparse = MatrixOperator(sprand(T, N, N, 5 / N))
A3_sparse = MatrixOperator(sprand(T, N, N, 5 / N))

A1_dense = MatrixOperator(rand(T, N, N))
A2_dense = MatrixOperator(rand(T, N, N))
A3_dense = MatrixOperator(rand(T, N, N))

coeff1(a, u, p, t) = sin(p.ω * t)
coeff2(a, u, p, t) = cos(p.ω * t)
coeff3(a, u, p, t) = sin(p.ω * t) * cos(p.ω * t)

c1 = ScalarOperator(rand(T), coeff1)
c2 = ScalarOperator(rand(T), coeff2)
c3 = ScalarOperator(rand(T), coeff3)

H_sparse = c1 * A1_sparse + c2 * A2_sparse + c3 * A3_sparse
H_dense = c1 * A1_dense + c2 * A2_dense + c3 * A3_dense

u = rand(T, N)
v = rand(T, N)
du = similar(u)
p = (ω = 0.1,)
t = 0.1
A = sprand(T, N, N, 2 / N)

func1(a, u, p, t) = t
func2(a, u, p, t) = t^2
func3(a, u, p, t) = t^3
func4(a, u, p, t) = t^4
func5(a, u, p, t) = t^5

O1 = MatrixOperator(A) + ScalarOperator(0.0, func1) * MatrixOperator(A) + ScalarOperator(0.0, func2) * MatrixOperator(A)

O2 = MatrixOperator(A) + ScalarOperator(0.0, func3) * MatrixOperator(A) + ScalarOperator(0.0, func4) * MatrixOperator(A)

O3 = MatrixOperator(A) + ScalarOperator(0.0, func5) * MatrixOperator(A)

Op = -1im * (O1 - O2)

@test length(Op.ops) == length(O1.ops) + length(O2.ops)
@inferred Op + O3
end
end

Expand Down
Loading