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

Matrix binary power #63

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

Conversation

WhiteBlackGoose
Copy link
Contributor

A^13 = A^6 * A^6 * A...

@@ -1134,68 +1135,120 @@ public Matrix<T> Divide(T b)

#region Power (Matrix ^ Scalar)

private class SquareMatrixFactory
{
private List<Matrix<T>> cache = new List<Matrix<T>>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is probably better to replace List with your type to keep the "ecosystem"

Multiply(mp2, dest, ref tmp);
mp2 = tmp;
}
destination = mp2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oof that was supposed to be .Clone I guess

{
if (!(c is null) && c._matrix.Length == a._matrix.Length)
if (!(destination is null) && destination.IsSquare)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better use IsSquare to keep the code readable, the power operation is extremely pricy, 1-2 ns isn't worth saving

if (power == 1)
return;
if (squareMatrixFactory is null || squareMatrixFactory.DiagonalLength != a._rows)
squareMatrixFactory = new SquareMatrixFactory(a._rows);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you already have a square matrix factory, this should be replaced with your type. Also my factory won't work if we change matrix' size after each operation, e. g.

Power(A, 3);
Power(A, 4);
Power(A, -13);

Is fast because we pull square matrices from the factory, while

Power(A, 3);
Power(B, 4);
Power(A, -13);

Because we re-create the factory in the second and third lines

@WhiteBlackGoose
Copy link
Contributor Author

oof not to merge now, I discovered a few bugs

@WhiteBlackGoose
Copy link
Contributor Author

Now it's ready

@ZacharyPatten ZacharyPatten added the On Hold issues that are on hold for any reason label Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
On Hold issues that are on hold for any reason
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants