|
| 1 | +#= |
| 2 | + Most of the functionality for linear combination of operators is already |
| 3 | + covered in LinearMaps.jl. |
| 4 | +=# |
| 5 | + |
| 6 | +(L::LinearCombination)(u,p,t) = L*u |
| 7 | +(L::LinearCombination)(du,u,p,t) = A_mul_B!(du,L,u) |
| 8 | + |
| 9 | +#= |
| 10 | + The fallback implementation in LinearMaps.jl effectively computes A*eye(N), |
| 11 | + which is very inefficient. |
| 12 | +
|
| 13 | + Instead, build up the full matrix for each operator iteratively. |
| 14 | +=# |
| 15 | +# TODO: Type dispatch for this is incorrect at the moment |
| 16 | +# function Base.full(A::LinearCombination{T,Tuple{Vararg{O}},Ts}) where {T,O<:Union{AbstractDiffEqLinearOperator,IdentityMap},Ts} |
| 17 | +# out = zeros(T,size(A)) |
| 18 | +# for i = 1:length(A.maps) |
| 19 | +# c = A.coeffs[i] |
| 20 | +# op = A.maps[i] |
| 21 | +# if isa(op, IdentityMap) |
| 22 | +# @. out += c * eye(size(A,1)) |
| 23 | +# else |
| 24 | +# @. out += c * full(op) |
| 25 | +# end |
| 26 | +# end |
| 27 | +# return out |
| 28 | +# end |
| 29 | + |
| 30 | +#= |
| 31 | + Fallback methods that use the full representation |
| 32 | +=# |
| 33 | +Base.expm(A::LinearCombination) = expm(full(A)) |
| 34 | +Base.:\(A::AbstractVecOrMat, B::LinearCombination) = A \ full(B) |
| 35 | +Base.:\(A::LinearCombination, B::AbstractVecOrMat) = full(A) \ B |
| 36 | +Base.:/(A::AbstractVecOrMat, B::LinearCombination) = A / full(B) |
| 37 | +Base.:/(A::LinearCombination, B::AbstractVecOrMat) = full(A) / B |
| 38 | + |
| 39 | +Base.norm(A::IdentityMap{T}, p::Real=2) where T = real(one(T)) |
| 40 | +Base.norm(A::LinearCombination, p::Real=2) = norm(full(A), p) |
| 41 | +#= |
| 42 | + The norm of A+B is difficult to calculate, but in many applications we only |
| 43 | + need an estimate of the norm (e.g. for error analysis) so it makes sense to |
| 44 | + compute the upper bound given by the triangle inequality |
| 45 | +
|
| 46 | + |A + B| <= |A| + |B| |
| 47 | +
|
| 48 | + For derivative operators A and B, their Inf norm can be calculated easily |
| 49 | + and thus so is the Inf norm bound of A + B. |
| 50 | +=# |
| 51 | +normbound(a::Number, p::Real=2) = abs(a) |
| 52 | +normbound(A::AbstractArray, p::Real=2) = norm(A, p) |
| 53 | +normbound(A::Union{AbstractDiffEqLinearOperator,IdentityMap}, p::Real=2) = norm(A, p) |
| 54 | +normbound(A::LinearCombination, p::Real=2) = sum(abs.(A.coeffs) .* normbound.(A.maps, p)) |
0 commit comments