Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit f8989c7

Browse files
committed
fix method ambiguities arisising from scimloperators integration
1 parent 92aa842 commit f8989c7

4 files changed

Lines changed: 75 additions & 9 deletions

File tree

src/composite_operators.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@ getindex(L::DiffEqOperatorCombination, i::Int) = sum(op -> op[i], L.ops)
6161
function getindex(L::DiffEqOperatorCombination, I::Vararg{Int, N}) where {N}
6262
sum(op -> op[I...], L.ops)
6363
end
64+
Base.:*(L::DiffEqOperatorCombination, x::AbstractVecOrMat) = sum(op -> op * x, L.ops)
6465
Base.:*(L::DiffEqOperatorCombination, x::AbstractArray) = sum(op -> op * x, L.ops)
66+
67+
Base.:*(x::AbstractVecOrMat, L::DiffEqOperatorCombination) = sum(op -> x * op, L.ops)
6568
Base.:*(x::AbstractArray, L::DiffEqOperatorCombination) = sum(op -> x * op, L.ops)
69+
70+
/(L::DiffEqOperatorCombination, x::AbstractVecOrMat) = sum(op -> op / x, L.ops)
6671
/(L::DiffEqOperatorCombination, x::AbstractArray) = sum(op -> op / x, L.ops)
72+
73+
\(x::AbstractVecOrMat, L::DiffEqOperatorCombination) = sum(op -> x \ op, L.ops)
6774
\(x::AbstractArray, L::DiffEqOperatorCombination) = sum(op -> x \ op, L.ops)
75+
6876
function mul!(y::AbstractVector, L::DiffEqOperatorCombination, b::AbstractVector)
6977
mul!(y, L.ops[1], b)
7078
for op in L.ops[2:end]
@@ -138,24 +146,49 @@ SparseArrays.sparse(L::DiffEqOperatorComposition) = prod(sparse1, reverse(L.ops)
138146
size(L::DiffEqOperatorComposition) = (size(L.ops[end], 1), size(L.ops[1], 2))
139147
size(L::DiffEqOperatorComposition, m::Integer) = size(L)[m]
140148
opnorm(L::DiffEqOperatorComposition) = prod(opnorm, L.ops)
149+
150+
function Base.:*(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
151+
foldl((acc, op) -> op * acc, L.ops; init = x)
152+
end
141153
function Base.:*(L::DiffEqOperatorComposition, x::AbstractArray)
142154
foldl((acc, op) -> op * acc, L.ops; init = x)
143155
end
156+
157+
function Base.:*(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
158+
foldl((acc, op) -> acc * op, reverse(L.ops); init = x)
159+
end
144160
function Base.:*(x::AbstractArray, L::DiffEqOperatorComposition)
145161
foldl((acc, op) -> acc * op, reverse(L.ops); init = x)
146162
end
163+
164+
function /(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
165+
foldl((acc, op) -> op / acc, L.ops; init = x)
166+
end
147167
function /(L::DiffEqOperatorComposition, x::AbstractArray)
148168
foldl((acc, op) -> op / acc, L.ops; init = x)
149169
end
170+
171+
function /(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
172+
foldl((acc, op) -> acc / op, L.ops; init = x)
173+
end
150174
function /(x::AbstractArray, L::DiffEqOperatorComposition)
151175
foldl((acc, op) -> acc / op, L.ops; init = x)
152176
end
177+
178+
function \(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
179+
foldl((acc, op) -> op \ acc, reverse(L.ops); init = x)
180+
end
153181
function \(L::DiffEqOperatorComposition, x::AbstractArray)
154182
foldl((acc, op) -> op \ acc, reverse(L.ops); init = x)
155183
end
184+
185+
function \(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
186+
foldl((acc, op) -> acc \ op, reverse(L.ops); init = x)
187+
end
156188
function \(x::AbstractArray, L::DiffEqOperatorComposition)
157189
foldl((acc, op) -> acc \ op, reverse(L.ops); init = x)
158190
end
191+
159192
function mul!(y::AbstractVector, L::DiffEqOperatorComposition, b::AbstractVector)
160193
mul!(L.caches[1], L.ops[1], b)
161194
for i in 2:(length(L.ops) - 1)

src/derivative_operators/derivative_operator_functions.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515
#
1616

1717
# Fallback mul! implementation for a single DerivativeOperator operating on an AbstractArray
18+
function LinearAlgebra.mul!(x_temp::AbstractVecOrMat{T}, A::DerivativeOperator{T, N},
19+
M::AbstractVecOrMat{T}; overwrite = true) where {T, N}
20+
_mul!(x_temp, A, M; overwrite = overwrite)
21+
end
1822
function LinearAlgebra.mul!(x_temp::AbstractArray{T}, A::DerivativeOperator{T, N},
1923
M::AbstractArray{T}; overwrite = true) where {T, N}
24+
_mul!(x_temp, A, M; overwrite = overwrite)
25+
end
26+
27+
function _mul!(x_temp::AbstractArray{T}, A::DerivativeOperator{T, N},
28+
M::AbstractArray{T}; overwrite = true) where {T, N}
2029

2130
# Check that x_temp has valid dimensions, allowing unnecessary padding in M
2231
v = zeros(ndims(x_temp))
@@ -138,7 +147,14 @@ end
138147

139148
###########################################
140149

141-
function *(A::DerivativeOperator{T, N}, M::AbstractArray{T}) where {T <: Real, N}
150+
function Base.:*(A::DerivativeOperator{T, N}, M::AbstractVecOrMat{T}) where {T <: Real, N}
151+
_mul(A, M)
152+
end
153+
function Base.:*(A::DerivativeOperator{T, N}, M::AbstractArray{T}) where {T <: Real, N}
154+
_mul(A, M)
155+
end
156+
157+
function _mul(A::DerivativeOperator{T, N}, M::AbstractArray{T}) where {T <: Real, N}
142158
size_x_temp = [size(M)...]
143159
size_x_temp[N] -= 2
144160
x_temp = zeros(promote_type(eltype(A), eltype(M)), size_x_temp...)

src/derivative_operators/ghost_derivative_operator.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,49 @@ function *(L::AbstractDiffEqCompositeOperator{T}, Q::AbstractBC{T}) where {T}
1212
return sum(map(op -> op * Q, L.ops))
1313
end
1414

15+
function LinearAlgebra.mul!(out::AbstractVecOrMat, A::GhostDerivativeOperator,
16+
u::AbstractVecOrMat)
17+
padded = A.Q * u # assume: creates boundary padded array w/o realloc
18+
LinearAlgebra.mul!(out, A.L, padded)
19+
end
1520
function LinearAlgebra.mul!(out::AbstractArray, A::GhostDerivativeOperator,
1621
u::AbstractArray)
1722
padded = A.Q * u # assume: creates boundary padded array w/o realloc
1823
LinearAlgebra.mul!(out, A.L, padded)
1924
end
2025

26+
function *(A::GhostDerivativeOperator{T1}, u::AbstractVecOrMat{T2}) where {T1, T2}
27+
#TODO Implement a function domaincheck(L::AbstractDiffEqLinearOperator, u) to see if components of L along each dimension match the size of u
28+
x = zeros(promote_type(T1, T2), unpadded_size(u))
29+
LinearAlgebra.mul!(x, A, u)
30+
return x
31+
end
2132
function *(A::GhostDerivativeOperator{T1}, u::AbstractArray{T2}) where {T1, T2}
2233
#TODO Implement a function domaincheck(L::AbstractDiffEqLinearOperator, u) to see if components of L along each dimension match the size of u
2334
x = zeros(promote_type(T1, T2), unpadded_size(u))
2435
LinearAlgebra.mul!(x, A, u)
2536
return x
2637
end
2738

28-
function \(A::GhostDerivativeOperator, u::AbstractArray) # FIXME should have T1,T2 and promote result
39+
function \(A::GhostDerivativeOperator, u::AbstractVector) # FIXME as above, should promote_type(T1,T2)
40+
# TODO: is this specialization to u::AbstractVector really any faster?
41+
@assert length(u) == size(A.L, 1)
42+
(A_l, A_b) = sparse(A, length(u))
43+
A_l \ Vector(u .- A_b)
44+
end
45+
function \(A::GhostDerivativeOperator, u::AbstractMatrix) # FIXME should have T1,T2 and promote result
2946
#TODO implement check that A has compatible size with u
3047
s = size(u)
3148
(A_l, A_b) = sparse(A, s)
3249
x = A_l \ Vector(reshape(u, length(u)) .- A_b) #Has to be converted to vector to work, A_b being sparse was causing a conversion to sparse.
3350
return reshape(x, s)
3451
end
35-
36-
function \(A::GhostDerivativeOperator, u::AbstractVector) # FIXME as above, should promote_type(T1,T2)
37-
# TODO: is this specialization to u::AbstractVector really any faster?
38-
@assert length(u) == size(A.L, 1)
39-
(A_l, A_b) = sparse(A, length(u))
40-
A_l \ Vector(u .- A_b)
52+
function \(A::GhostDerivativeOperator, u::AbstractArray) # FIXME should have T1,T2 and promote result
53+
#TODO implement check that A has compatible size with u
54+
s = size(u)
55+
(A_l, A_b) = sparse(A, s)
56+
x = A_l \ Vector(reshape(u, length(u)) .- A_b) #Has to be converted to vector to work, A_b being sparse was causing a conversion to sparse.
57+
return reshape(x, s)
4158
end
4259

4360
# update coefficients

src/matrixfree_operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Base.size(M::MatrixFreeOperator)
2020
error("M.size is nothing, please define size as a tuple of integers")
2121
return M.size
2222
end
23-
@inline function Base.size(M::MatrixFreeOperator, n)
23+
@inline function Base.size(M::MatrixFreeOperator, n::Integer)
2424
M.size === nothing &&
2525
error("M.size is nothing, please define size as a tuple of integers")
2626
n <= 0 && error("dimension out of range")

0 commit comments

Comments
 (0)