Lightweight fast matrix class in C# (Strassen algorithm, LU decomposition)
This is lightweight (19 kB) matirx class written in C#, that includes basic matrix operations.
All this is written in C#. Parse() and ToString() methods included. Matrix class can throw exceptions (MException).
Implemented matrix operations
- exponentiation by integer
- LU decomposition
- solve system of linear equations
- transpose, …
Overridden infix operators
- array-like access (a[2,4])
- addition (a + b)
- subtraction (a – b)
- unary minus (-a)
- multiplication (a * b)
- constant multiplication (5.4 * a)
Strassen algorithm (fast multiplication of large matrices)
It contains matrix multiplication with Strassen algorithm, which is memory efficient and cahce-oblivious, it works with still the same array in each level of recursion.
It is about 3 times faster in multiplication of two 500×500 matrices than the standard algorithm (tested). Used only when multiplying matrices larger than 32.
DocumentationShow documentation – Hide documentation
// solving system of linear equations ... A * x = b Matrix A = Matrix.Parse("1 2\r\n-3 4.5"); // \r\n is newline in textbox Matrix b = Matrix.Parse("-1\r\n-3); Matrix x = A.SolveWith(b); // LU decomposition (P is permutation matrix) Matrix PLU = A.GetP() * A.L * A.U; // -> PLU == A double d = A.Det(); Matrix I = A.Invert(); Matrix Powered = Matrix.Power(A, 10); Matrix S = A.Duplicate(); Matrix Z = Matrix.ZeroMatrix(4,3); Matrix I = Matrix.IdentityMatrix(6,7); Matrix R = Matrix.RandomMatrix(4, 4, 80); // rows, cols, dispersion