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
Linear Combinations of Operators #55
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
673bacc
Fix error in norm calculation for `DiffEqArrayOperator`
MSeeker1340 aeda250
Implement `full` and `expm` for operator combinations
MSeeker1340 11835e8
Implement `norm` and `normbound` for operator combinations
MSeeker1340 67c81cd
`normbound` for `AbstractArray`
MSeeker1340 8018256
Fallback methods for division of operators
MSeeker1340 dc74ec0
`normbound` for numbers
MSeeker1340 79897f4
Functor definition for `LinearCombination`
MSeeker1340 ea7e198
.* the `full` method for `DiffEqArrayOperator`
MSeeker1340 db2838b
Re-define `\` in terms of `/`
MSeeker1340 590acb6
Define `/` and `\` separately using `full`
MSeeker1340 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#= | ||
Most of the functionality for linear combination of operators is already | ||
covered in LinearMaps.jl. | ||
=# | ||
|
||
(L::LinearCombination)(u,p,t) = L*u | ||
(L::LinearCombination)(du,u,p,t) = A_mul_B!(du,L,u) | ||
|
||
#= | ||
The fallback implementation in LinearMaps.jl effectively computes A*eye(N), | ||
which is very inefficient. | ||
|
||
Instead, build up the full matrix for each operator iteratively. | ||
=# | ||
# TODO: Type dispatch for this is incorrect at the moment | ||
# function Base.full(A::LinearCombination{T,Tuple{Vararg{O}},Ts}) where {T,O<:Union{AbstractDiffEqLinearOperator,IdentityMap},Ts} | ||
# out = zeros(T,size(A)) | ||
# for i = 1:length(A.maps) | ||
# c = A.coeffs[i] | ||
# op = A.maps[i] | ||
# if isa(op, IdentityMap) | ||
# @. out += c * eye(size(A,1)) | ||
# else | ||
# @. out += c * full(op) | ||
# end | ||
# end | ||
# return out | ||
# end | ||
|
||
#= | ||
Fallback methods that use the full representation | ||
=# | ||
Base.expm(A::LinearCombination) = expm(full(A)) | ||
Base.:\(A::AbstractVecOrMat, B::LinearCombination) = A \ full(B) | ||
Base.:\(A::LinearCombination, B::AbstractVecOrMat) = full(A) \ B | ||
Base.:/(A::AbstractVecOrMat, B::LinearCombination) = (B' \ A')' | ||
Base.:/(A::LinearCombination, B::AbstractVecOrMat) = (B' \ A')' | ||
|
||
Base.norm(A::IdentityMap{T}, p::Real=2) where T = real(one(T)) | ||
Base.norm(A::LinearCombination, p::Real=2) = norm(full(A), p) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a lazy way to do |
||
#= | ||
The norm of A+B is difficult to calculate, but in many applications we only | ||
need an estimate of the norm (e.g. for error analysis) so it makes sense to | ||
compute the upper bound given by the triangle inequality | ||
|
||
|A + B| <= |A| + |B| | ||
|
||
For derivative operators A and B, their Inf norm can be calculated easily | ||
and thus so is the Inf norm bound of A + B. | ||
=# | ||
normbound(a::Number, p::Real=2) = abs(a) | ||
normbound(A::AbstractArray, p::Real=2) = norm(A, p) | ||
normbound(A::Union{AbstractDiffEqLinearOperator,IdentityMap}, p::Real=2) = norm(A, p) | ||
normbound(A::LinearCombination, p::Real=2) = sum(abs.(A.coeffs) .* normbound.(A.maps, p)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using DiffEqOperators | ||
using Base.Test | ||
|
||
N = 5 | ||
srand(0); A = rand(N,N); u = rand(N) | ||
L = DiffEqArrayOperator(A) | ||
a = 3.5 | ||
La = L * a | ||
|
||
@test La * u ≈ (a*A) * u | ||
@test lufact(La) \ u ≈ (a*A) \ u | ||
@test norm(La) ≈ norm(a*A) | ||
@test expm(La) ≈ expm(a*A) | ||
@test La[2,3] ≈ A[2,3] # should this be La[2,3] == a*A[2,3]? | ||
|
||
update_func = (_A,t,u) -> _A .= t * A | ||
t = 3.0 | ||
Atmp = zeros(N,N) | ||
Lt = DiffEqArrayOperator(Atmp, a, update_func) | ||
@test Lt(u,nothing,t) ≈ (a*t*A) * u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These transposes might be incorrect because they are just implemented as a no-op. Sorry if I was unclear before. I think you should just
full
and/
, or does the generic version butfull
before transpose.