-
-
Notifications
You must be signed in to change notification settings - Fork 23
evalpoly for matrix polynomials #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevengj
wants to merge
9
commits into
master
Choose a base branch
from
sgj/evalpoly
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c6eacfd
evalpoly for matrix polynomials
stevengj d5be83b
tweak
stevengj dbe1787
fix load-order issues by moving evalpoly methods to a separate file
stevengj b43e5e6
fix heterogeneous and abstract cases
stevengj 1b43a2e
Update src/evalpoly.jl
stevengj e051033
Merge branch 'master' into sgj/evalpoly
ViralBShah 437b35d
Merge branch 'master' into sgj/evalpoly
ViralBShah 374d8fe
Merge branch 'master' into sgj/evalpoly
ViralBShah 68b3f0f
Merge branch 'master' into sgj/evalpoly
LilithHafner 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 hidden or 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 hidden or 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,85 @@ | ||
# matrix methods for evalpoly(X, p) = ∑ₖ Xᵏ⁻¹ p[k] | ||
|
||
# non-inplace fallback for evalpoly(X, p) | ||
function _evalpoly(X::AbstractMatrix, p) | ||
Base.require_one_based_indexing(p) | ||
p0 = isempty(p) ? Base.reduce_empty_iter(+, p) : p[end] | ||
Xone = one(X) | ||
S = Base.promote_op(*, typeof(Xone), typeof(Xone))(Xone) * p0 | ||
for i = length(p)-1:-1:1 | ||
S = X * S + @inbounds(p[i] isa AbstractMatrix ? p[i] : p[i] * I) | ||
end | ||
return S | ||
end | ||
|
||
_scalarval(x::Number) = x | ||
_scalarval(x::UniformScaling) = x.λ | ||
|
||
""" | ||
evalpoly!(Y::AbstractMatrix, X::AbstractMatrix, p) | ||
|
||
Evaluate the matrix polynomial ``Y = \\sum_k X^{k-1} p[k]``, storing the result | ||
in-place in `Y`, for the coefficients `p[k]` (a vector or tuple). The coefficients | ||
can be scalars, matrices, or [`UniformScaling`](@ref). | ||
|
||
Similar to `evalpoly`, but may be more efficient by working more in-place. (Some | ||
allocations may still be required, however.) | ||
""" | ||
function evalpoly!(Y::AbstractMatrix, X::AbstractMatrix, p::Union{AbstractVector,Tuple}) | ||
@boundscheck axes(Y,1) == axes(Y,2) == axes(X,1) == axes(X,2) | ||
Base.require_one_based_indexing(p) | ||
|
||
N = length(p) | ||
pN = iszero(N) ? Base.reduce_empty_iter(+, p) : p[N] | ||
if pN isa AbstractMatrix | ||
Y .= pN | ||
elseif N > 1 && p[N-1] isa Union{Number,UniformScaling} | ||
# initialize Y to p[N-1] I + X p[N], in-place | ||
Y .= X .* _scalarval(pN) | ||
for i in axes(Y,1) | ||
@inbounds Y[i,i] += p[N-1] * I | ||
end | ||
N -= 1 | ||
else | ||
# initialize Y to one(Y) * pN in-place | ||
for i in axes(Y,1) | ||
for j in axes(Y,2) | ||
@inbounds Y[i,j] = zero(Y[i,j]) | ||
end | ||
@inbounds Y[i,i] += one(Y[i,i]) * pN | ||
end | ||
end | ||
if N > 1 | ||
Z = similar(Y) # workspace for mul! | ||
for i = N-1:-1:1 | ||
mul!(Z, X, Y) | ||
if p[i] isa AbstractMatrix | ||
Y .= p[i] .+ Z | ||
else | ||
# Y = p[i] * I + Z, in-place | ||
Y .= Z | ||
for j in axes(Y,1) | ||
@inbounds Y[j,j] += p[i] * I | ||
end | ||
end | ||
end | ||
end | ||
return Y | ||
end | ||
|
||
# fallback cases: call out-of-place _evalpoly | ||
Base.evalpoly(X::AbstractMatrix, p::Tuple) = _evalpoly(X, p) | ||
Base.evalpoly(X::AbstractMatrix, ::Tuple{}) = zero(one(X)) # dimensionless zero, i.e. 0 * X^0 | ||
Base.evalpoly(X::AbstractMatrix, p::AbstractVector) = _evalpoly(X, p) | ||
|
||
# optimized in-place cases, limited to types like homogeneous tuples with length > 1 | ||
# where we can reliably deduce the output type (= type of X * p[2]), | ||
# and restricted to StridedMatrix (for now) so that we can be more confident that this is a performance win: | ||
Base.evalpoly(X::StridedMatrix{<:Number}, p::Tuple{T, T, Vararg{T}}) where {T<:Union{Number, UniformScaling}} = | ||
evalpoly!(similar(X, Base.promote_op(*, eltype(X), typeof(_scalarval(p[2])))), X, p) | ||
Base.evalpoly(X::StridedMatrix{<:Number}, p::Tuple{AbstractMatrix{T}, AbstractMatrix{T}, Vararg{AbstractMatrix{T}}}) where {T<:Number} = | ||
evalpoly!(similar(X, Base.promote_op(*, eltype(X), T)), X, p) | ||
Base.evalpoly(X::StridedMatrix{<:Number}, p::AbstractVector{<:Union{Number, UniformScaling}}) = | ||
length(p) < 2 ? _evalpoly(X, p) : evalpoly!(similar(X, Base.promote_op(*, eltype(X), typeof(_scalarval(p[begin+1])))), X, p) | ||
Base.evalpoly(X::StridedMatrix{<:Number}, p::AbstractVector{<:AbstractMatrix{<:Number}}) = | ||
length(p) < 2 ? _evalpoly(X, p) : evalpoly!(similar(X, Base.promote_op(*, eltype(X), eltype(p[begin+1]))), X, p) |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.