This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathcomposite_operators.jl
212 lines (195 loc) · 8.83 KB
/
composite_operators.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# The composite operators are built using basic operators (scalar, array, and
# derivative) using arithmetic or other operator compositions. The composite
# operator types are lazy and maintain the structure used to build them.
# Define a helper function `sparse1` that handles
# `DiffEqArrayOperator` and `DiffEqScaledOperator`.
# We should define `sparse` for these types in `SciMLBase` instead,
# but that package doesn't know anything about sparse arrays yet, so
# we'll introduce a temporary work-around here.
sparse1(A) = sparse(A)
sparse1(A::DiffEqArrayOperator) = sparse1(A.A)
sparse1(A::DiffEqScaledOperator) = A.coeff * sparse1(A.op)
# Linear Combination
struct DiffEqOperatorCombination{T, O <: Tuple{Vararg{AbstractDiffEqLinearOperator{T}}},
C <: AbstractVector{T}} <:
AbstractDiffEqCompositeOperator{T}
ops::O
cache::C
function DiffEqOperatorCombination(ops; cache = nothing)
T = eltype(ops[1])
for i in 2:length(ops)
@assert size(ops[i])==size(ops[1]) "Operators must be of the same size to be combined! Mismatch between $(ops[i]) and $(ops[i-1]), which are operators $i and $(i-1) respectively"
end
if cache === nothing
cache = zeros(T, size(ops[1], 1))
end
new{T, typeof(ops), typeof(cache)}(ops, cache)
end
end
Base.:+(ops::AbstractDiffEqLinearOperator...) = DiffEqOperatorCombination(ops)
Base.:+(op::AbstractDiffEqLinearOperator, A::AbstractMatrix) = op + DiffEqArrayOperator(A)
function Base.:+(op::AbstractDiffEqLinearOperator{T}, α::UniformScaling) where {T}
op + UniformScaling(T(α.λ))(size(op, 1))
end
Base.:+(A::AbstractMatrix, op::AbstractDiffEqLinearOperator) = op + A
Base.:+(α::UniformScaling, op::AbstractDiffEqLinearOperator) = op + α
function Base.:+(L1::DiffEqOperatorCombination, L2::AbstractDiffEqLinearOperator)
DiffEqOperatorCombination((L1.ops..., L2))
end
function Base.:+(L1::AbstractDiffEqLinearOperator, L2::DiffEqOperatorCombination)
DiffEqOperatorCombination((L1, L2.ops...))
end
function Base.:+(L1::DiffEqOperatorCombination, L2::DiffEqOperatorCombination)
DiffEqOperatorCombination((L1.ops..., L2.ops...))
end
Base.:-(L1::AbstractDiffEqLinearOperator, L2::AbstractDiffEqLinearOperator) = L1 + (-L2)
Base.:-(L::AbstractDiffEqLinearOperator, A::AbstractMatrix) = L + (-A)
Base.:-(A::AbstractMatrix, L::AbstractDiffEqLinearOperator) = A + (-L)
Base.:-(L::AbstractDiffEqLinearOperator, α::UniformScaling) = L + (-α)
Base.:-(α::UniformScaling, L::AbstractDiffEqLinearOperator) = α + (-L)
getops(L::DiffEqOperatorCombination) = L.ops
Matrix(L::DiffEqOperatorCombination) = sum(Matrix, L.ops)
function convert(::Type{AbstractMatrix}, L::DiffEqOperatorCombination)
sum(op -> convert(AbstractMatrix, op), L.ops)
end
SparseArrays.sparse(L::DiffEqOperatorCombination) = sum(sparse1, L.ops)
size(L::DiffEqOperatorCombination, args...) = size(L.ops[1], args...)
getindex(L::DiffEqOperatorCombination, i::Int) = sum(op -> op[i], L.ops)
function getindex(L::DiffEqOperatorCombination, I::Vararg{Int, N}) where {N}
sum(op -> op[I...], L.ops)
end
Base.:*(L::DiffEqOperatorCombination, x::AbstractVecOrMat) = sum(op -> op * x, L.ops)
Base.:*(L::DiffEqOperatorCombination, x::AbstractArray) = sum(op -> op * x, L.ops)
Base.:*(x::AbstractVecOrMat, L::DiffEqOperatorCombination) = sum(op -> x * op, L.ops)
Base.:*(x::AbstractArray, L::DiffEqOperatorCombination) = sum(op -> x * op, L.ops)
/(L::DiffEqOperatorCombination, x::AbstractVecOrMat) = sum(op -> op / x, L.ops)
/(L::DiffEqOperatorCombination, x::AbstractArray) = sum(op -> op / x, L.ops)
\(x::AbstractVecOrMat, L::DiffEqOperatorCombination) = sum(op -> x \ op, L.ops)
\(x::AbstractArray, L::DiffEqOperatorCombination) = sum(op -> x \ op, L.ops)
function mul!(y::AbstractVector, L::DiffEqOperatorCombination, b::AbstractVector)
mul!(y, L.ops[1], b)
for op in L.ops[2:end]
mul!(L.cache, op, b)
y .+= L.cache
end
return y
end
# Composition (A ∘ B)
struct DiffEqOperatorComposition{T, O <: Tuple{Vararg{AbstractDiffEqLinearOperator{T}}},
C <: Tuple{Vararg{AbstractVector{T}}}} <:
AbstractDiffEqCompositeOperator{T}
ops::O # stored in the order of application
caches::C
function DiffEqOperatorComposition(ops; caches = nothing)
T = eltype(ops[1])
for i in 2:length(ops)
@assert size(ops[i - 1], 1)==size(ops[i], 2) "Operations do not have compatible sizes! Mismatch between $(ops[i]) and $(ops[i-1]), which are operators $i and $(i-1) respectively."
end
if caches === nothing
# Construct a list of caches to be used by mul! and ldiv!
caches = []
for op in ops[1:(end - 1)]
tmp = Vector{T}(undef, size(op, 1))
fill!(tmp, 0)
push!(caches, tmp)
end
caches = tuple(caches...)
end
new{T, typeof(ops), typeof(caches)}(ops, caches)
end
end
# this is needed to not break dispatch in MethodOfLines
function Base.:*(ops::AbstractDiffEqLinearOperator...)
try
return DiffEqOperatorComposition(reverse(ops))
catch e
return 1
end
end
function ∘(L1::AbstractDiffEqLinearOperator, L2::AbstractDiffEqLinearOperator)
DiffEqOperatorComposition((L2, L1))
end
function Base.:*(L1::DiffEqOperatorComposition, L2::AbstractDiffEqLinearOperator)
DiffEqOperatorComposition((L2, L1.ops...))
end
function ∘(L1::DiffEqOperatorComposition, L2::AbstractDiffEqLinearOperator)
DiffEqOperatorComposition((L2, L1.ops...))
end
function Base.:*(L1::AbstractDiffEqLinearOperator, L2::DiffEqOperatorComposition)
DiffEqOperatorComposition((L2.ops..., L1))
end
function ∘(L1::AbstractDiffEqLinearOperator, L2::DiffEqOperatorComposition)
DiffEqOperatorComposition((L2.ops..., L1))
end
function Base.:*(L1::DiffEqOperatorComposition, L2::DiffEqOperatorComposition)
DiffEqOperatorComposition((L2.ops..., L1.ops...))
end
function ∘(L1::DiffEqOperatorComposition, L2::DiffEqOperatorComposition)
DiffEqOperatorComposition((L2.ops..., L1.ops...))
end
getops(L::DiffEqOperatorComposition) = L.ops
Matrix(L::DiffEqOperatorComposition) = prod(Matrix, reverse(L.ops))
function convert(::Type{AbstractMatrix}, L::DiffEqOperatorComposition)
prod(op -> convert(AbstractMatrix, op), reverse(L.ops))
end
SparseArrays.sparse(L::DiffEqOperatorComposition) = prod(sparse1, reverse(L.ops))
size(L::DiffEqOperatorComposition) = (size(L.ops[end], 1), size(L.ops[1], 2))
size(L::DiffEqOperatorComposition, m::Integer) = size(L)[m]
opnorm(L::DiffEqOperatorComposition) = prod(opnorm, L.ops)
function Base.:*(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
foldl((acc, op) -> op * acc, L.ops; init = x)
end
function Base.:*(L::DiffEqOperatorComposition, x::AbstractArray)
foldl((acc, op) -> op * acc, L.ops; init = x)
end
function Base.:*(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc * op, reverse(L.ops); init = x)
end
function Base.:*(x::AbstractArray, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc * op, reverse(L.ops); init = x)
end
function /(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
foldl((acc, op) -> op / acc, L.ops; init = x)
end
function /(L::DiffEqOperatorComposition, x::AbstractArray)
foldl((acc, op) -> op / acc, L.ops; init = x)
end
function /(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc / op, L.ops; init = x)
end
function /(x::AbstractArray, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc / op, L.ops; init = x)
end
function \(L::DiffEqOperatorComposition, x::AbstractVecOrMat)
foldl((acc, op) -> op \ acc, reverse(L.ops); init = x)
end
function \(L::DiffEqOperatorComposition, x::AbstractArray)
foldl((acc, op) -> op \ acc, reverse(L.ops); init = x)
end
function \(x::AbstractVecOrMat, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc \ op, reverse(L.ops); init = x)
end
function \(x::AbstractArray, L::DiffEqOperatorComposition)
foldl((acc, op) -> acc \ op, reverse(L.ops); init = x)
end
function mul!(y::AbstractVector, L::DiffEqOperatorComposition, b::AbstractVector)
mul!(L.caches[1], L.ops[1], b)
for i in 2:(length(L.ops) - 1)
mul!(L.caches[i], L.ops[i], L.caches[i - 1])
end
mul!(y, L.ops[end], L.caches[end])
end
function ldiv!(y::AbstractVector, L::DiffEqOperatorComposition, b::AbstractVector)
ldiv!(L.caches[end], L.ops[end], b)
for i in (length(L.ops) - 1):-1:2
ldiv!(L.caches[i - 1], L.ops[i], L.caches[i])
end
ldiv!(y, L.ops[1], L.caches[1])
end
factorize(L::DiffEqOperatorComposition) = prod(factorize, reverse(L.ops))
for fact in (:lu, :lu!, :qr, :qr!, :cholesky, :cholesky!, :ldlt, :ldlt!,
:bunchkaufman, :bunchkaufman!, :lq, :lq!, :svd, :svd!)
@eval function LinearAlgebra.$fact(L::DiffEqOperatorComposition, args...)
prod(op -> $fact(op, args...), reverse(L.ops))
end
end