-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
base: main
Are you sure you want to change the base?
Matrix binary power #63
Conversation
@@ -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>>(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
oof not to merge now, I discovered a few bugs |
Now it's ready |
A^13 = A^6 * A^6 * A...