Skip to content

Add SimpleArray::pow() for matrix power#844

Merged
yungyuc merged 1 commit into
solvcon:masterfrom
yungyuc:feat/pow
May 31, 2026
Merged

Add SimpleArray::pow() for matrix power#844
yungyuc merged 1 commit into
solvcon:masterfrom
yungyuc:feat/pow

Conversation

@yungyuc
Copy link
Copy Markdown
Member

@yungyuc yungyuc commented May 30, 2026

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 #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 #821.

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 yungyuc self-assigned this May 30, 2026
@yungyuc yungyuc added the array Multi-dimensional array implementation label May 30, 2026

// Exponentiation by squaring. n is non-negative here, so scanning its
// bits is well-defined.
A base = *athis;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raise exceptions.

Comment thread tests/test_matrix.py
def test_matrix_dim_to_5(self):
"""A^n matches numpy across several square matrices and exponents"""
fixtures = [
[[-3]],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test with 1x1, 2x2, 3x3, and 5x5 matrices.

Comment thread tests/test_matrix.py

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]],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Power requires square matrices.

Comment thread tests/test_matrix.py
"""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)]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matrices must be 2D arrays.

@yungyuc yungyuc changed the title Implement matrix power SimpleArray::pow() Add SimpleArray::pow() for matrix power May 30, 2026
@yungyuc yungyuc marked this pull request as ready for review May 31, 2026 01:46
@yungyuc
Copy link
Copy Markdown
Member Author

yungyuc commented May 31, 2026

@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.

@yungyuc yungyuc moved this from Todo to In Progress in tensor operations May 31, 2026
@yungyuc yungyuc merged commit 0a5fb6a into solvcon:master May 31, 2026
21 of 32 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in tensor operations May 31, 2026
@yungyuc yungyuc deleted the feat/pow branch May 31, 2026 01:48
@yungyuc yungyuc linked an issue May 31, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array Multi-dimensional array implementation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Implement matrix power SimpleArray::pow()

1 participant