Skip to content
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

evalpoly for matrix polynomials #1163

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

evalpoly for matrix polynomials #1163

wants to merge 5 commits into from

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Jan 3, 2025

Closes #1161.

Includes a generic out-of-place fallback implementation as well as an in-place evalpoly! method; the latter is called by default only for X::StridedMatrix{<:Number} for now, to be conservative. (e.g. it's much trickier to allocate the in-place buffers correctly for matrices of matrices, which we don't support very well, and I'm not sure this would work well for sparse matrices.) More optimized cases can always be added later.

@stevengj stevengj requested a review from LilithHafner January 3, 2025 22:38
@mcabbott
Copy link
Collaborator

mcabbott commented Jan 3, 2025

In addition to X::StridedMatrix{<:Number}, doesn't the in-place version need to widen based on the coefficients' types? Some failure cases:

julia> evalpoly([1 2; 3 4], (5, 6))  # error without this PR
2×2 Matrix{Int64}:
 11  12
 18  29

julia> evalpoly([1 2; 3 4], (5, 6.7))
ERROR: InexactError: Int64(6.7)
Stacktrace:
...
 [11] evalpoly!(Y::Matrix{Int64}, X::Matrix{Int64}, p::Tuple{Int64, Float64})
    @ Main ./REPL[4]:19

julia> evalpoly([1.0 2.0; 3.0 4.0], (2, 3+im, 4))
ERROR: InexactError: Float64(7.0 + 1.0im)

@stevengj
Copy link
Member Author

stevengj commented Jan 3, 2025

Argh, this is a PITA to get right for heterogeneous tuples of coefficients, especially for a tuple of dimensionful coefficients. (e.g. suppose that X is in meters, and the coefficients p are in (s, s/m, s/m^2) — this is dimensionfully correct with an evalpoly result in seconds, but you can't simply promote the elements of p).

What is a good way to compute the correct element type of the result matrix?

For p a tuple of numbers, I'm tempted to do typeof(evalpoly(zero(eltype(X)), p)), but that will fail if eltype(X) is abstract.

One option would be to just punt on evalpoly! for now. Another option would be to only support homogeneous tuples.

@mcabbott
Copy link
Collaborator

mcabbott commented Jan 3, 2025

Ah now I see some logic for p[begin] which my examples happened to avoid.

Only accepting p::NTuple (and concretely typed vectors) seems fine really. And perhaps even StridedMatrix{<:AbstractFloat} with real coefficients (to avoid my integer example), sim. complex with real-or-complex?

Easy to add methods to send your own fancy types to evalpoly! if you want something not covered by this.

@stevengj
Copy link
Member Author

stevengj commented Jan 4, 2025

I tightened up the type signatures for dispatch to evalpoly! and it fixes your examples, along with abstractly typed arrays. (Also added tests.)

@mcabbott
Copy link
Collaborator

mcabbott commented Jan 4, 2025

This is a little harder to fool, but:

julia> evalpoly([1 2; 3 4], Number[5.5, 6])
ERROR: InexactError: Int64(11.5)

julia> evalpoly([1.0 2.0; 3.0 4.0], Number[2, 3, 4+im])
ERROR: InexactError: Float64(4.0 + 1.0im)

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

Attention: Patch coverage is 87.75510% with 6 lines in your changes missing coverage. Please review.

Project coverage is 91.88%. Comparing base (9bc292d) to head (b43e5e6).

Files with missing lines Patch % Lines
src/evalpoly.jl 87.75% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1163      +/-   ##
==========================================
- Coverage   91.94%   91.88%   -0.07%     
==========================================
  Files          34       35       +1     
  Lines       15354    15403      +49     
==========================================
+ Hits        14118    14153      +35     
- Misses       1236     1250      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@LilithHafner
Copy link
Member

I think it makes sense to dispatch to _evalpoly in the case of vectors with non-concrete eltype. We could do a reducion with promotion but I don't think it's worth trying particularly hard to optimize that case.

src/evalpoly.jl Outdated Show resolved Hide resolved
Co-authored-by: Martin Holters <martin.holters@hsu-hh.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

evalpoly is broken for matrices
5 participants