Add SimpleArray::pow() for matrix power#844
Merged
Merged
Conversation
Add SimpleArray::pow(n) computing A^n for a square matrix and a non-negative integer exponent via exponentiation by squaring (O(log n) matmuls). A^0 returns the identity; negative exponents are rejected since they require matrix inversion (tracked in issue solvcon#719). Include Float32/Float64 tests covering exponent values, identity invariance, a numpy cross-check over fixed fixtures, and the non-square and non-2D (1D/3D/4D) error cases. For issue solvcon#821.
yungyuc
commented
May 30, 2026
|
|
||
| // Exponentiation by squaring. n is non-negative here, so scanning its | ||
| // bits is well-defined. | ||
| A base = *athis; |
Member
Author
There was a problem hiding this comment.
Squaring (O(ln n) complexity) needs two arrays.
| A SimpleArrayMixinMatrix<A, T>::pow(ssize_t n) const | ||
| { | ||
| auto const * athis = static_cast<A const *>(this); | ||
| validate_square("pow"); |
| def test_matrix_dim_to_5(self): | ||
| """A^n matches numpy across several square matrices and exponents""" | ||
| fixtures = [ | ||
| [[-3]], |
Member
Author
There was a problem hiding this comment.
Test with 1x1, 2x2, 3x3, and 5x5 matrices.
|
|
||
| def test_non_square_error(self): | ||
| """A non-square matrix cannot be raised to a power""" | ||
| mat_data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], |
Member
Author
There was a problem hiding this comment.
Power requires square matrices.
| """A non-2D SimpleArray cannot be raised to a power""" | ||
| # 1D, 3D, and 4D arrays must all be rejected, and the error must | ||
| # report the offending dimensionality. | ||
| shapes = [(3,), (2, 2, 2), (2, 2, 2, 2)] |
Member
Author
There was a problem hiding this comment.
Matrices must be 2D arrays.
SimpleArray::pow() for matrix power
9 tasks
Member
Author
|
@ThreeMonth03 @tigercosmos @KHLee529 please help take a look. All PRs have CI false alarms, so does this one. We have to inspect and then ignore them before coming up with a solution. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add$$A^n$$ for a square matrix and a non-negative integer exponent via exponentiation by squaring (O(log n) matmuls). $$A^0$$ returns the identity; negative exponents are rejected since they require matrix inversion (tracked in issue #719).
SimpleArray::pow(n)computingInclude Float32/Float64 tests covering exponent values, identity invariance, a numpy cross-check over fixed fixtures, and the non-square and non-2D (1D/3D/4D) error cases.
For issue #821.