Class MatrixOperations
- Namespace
- SignalSharp.Utilities
- Assembly
- SignalSharp.dll
Provides operations for matrix manipulation and arithmetic.
The class includes methods to transpose matrices and perform matrix multiplication.
public static class MatrixOperations
- Inheritance
-
MatrixOperations
- Inherited Members
Methods
Add<T>(T[,], T[,])
Adds two matrices of the same dimensions.
public static T[,] Add<T>(T[,] matrixA, T[,] matrixB) where T : IFloatingPointIeee754<T>
Parameters
matrixA
T[,]The first matrix.
matrixB
T[,]The second matrix.
Returns
- T[,]
A new matrix representing the sum of
matrixA
andmatrixB
.
Type Parameters
T
The numeric type of the matrix elements, implementing IFloatingPointIeee754<TSelf>.
Exceptions
- ArgumentNullException
Thrown if
matrixA
ormatrixB
is null.- ArgumentException
Thrown if the dimensions of
matrixA
andmatrixB
do not match.
Combinations<T>(int, int)
Calculates combinations C(n, k).
public static T Combinations<T>(int n, int k) where T : IFloatingPointIeee754<T>
Parameters
Returns
- T
Type Parameters
T
Inverse(double[,])
Inverts the given matrix.
The method uses the Gauss-Jordan elimination method to compute the inverse of a matrix.
public static double[,] Inverse(double[,] matrix)
Parameters
matrix
double[,]The matrix to invert.
Returns
- double[,]
The inverse of the matrix.
Examples
double[,] matrix = { {1, 2}, {3, 4} };
double[,] result = MatrixOperations.Inverse(matrix);
// result is { {-2, 1}, {1.5, -0.5} }
Exceptions
- ArgumentException
Thrown when the matrix is not square or is singular (non-invertible).
Multiply<T>(T[,], T[,])
public static T[,] Multiply<T>(T[,] matrixA, T[,] matrixB) where T : IFloatingPointIeee754<T>
Parameters
matrixA
T[,]matrixB
T[,]
Returns
- T[,]
Type Parameters
T
Multiply<T>(T[,], T[])
public static T[] Multiply<T>(T[,] matrixA, T[] vectorB) where T : IFloatingPointIeee754<T>
Parameters
matrixA
T[,]vectorB
T[]
Returns
- T[]
Type Parameters
T
ScalarMultiply<T>(T, T[,])
Multiplies a matrix by a scalar value.
public static T[,] ScalarMultiply<T>(T scalar, T[,] matrix) where T : IFloatingPointIeee754<T>
Parameters
scalar
TThe scalar value to multiply by.
matrix
T[,]The matrix to be multiplied.
Returns
- T[,]
A new matrix representing the product of the
scalar
andmatrix
.
Type Parameters
T
The numeric type of the matrix elements and scalar, implementing IFloatingPointIeee754<TSelf>.
Exceptions
- ArgumentNullException
Thrown if
matrix
is null.
SolveLinearSystem<T>(T[,], T[])
Solves a system of linear equations Ax = b. For square systems (A is n x n), it uses Gaussian elimination. For overdetermined systems (A is m x n, m > n), it solves the normal equations A^T A x = A^T b using Gaussian elimination.
public static T[] SolveLinearSystem<T>(T[,] A, T[] b) where T : IFloatingPointIeee754<T>
Parameters
A
T[,]The matrix representing the system of linear equations. If A is square, it will be modified in place. If A is overdetermined, it is not modified.
b
T[]The vector representing the right-hand side of the equations. If A is square, b will be modified in place. If A is overdetermined, b is not modified.
Returns
- T[]
An array of solutions for the system of linear equations.
Type Parameters
T
The numeric type of the elements, implementing IFloatingPointIeee754<TSelf>.
Remarks
For square systems, this method modifies the input matrix A
and vector b
in place for efficiency.
For overdetermined systems, intermediate matrices derived from A
and b
are created and modified,
leaving the original A
and b
unchanged by the Gaussian elimination step.
Exceptions
- ArgumentNullException
Thrown if
A
orb
is null.- ArgumentException
Thrown if dimensions are incompatible, if the matrix is singular or ill-conditioned, or if the system is underdetermined.
Transpose<T>(T[,])
public static T[,] Transpose<T>(T[,] matrix) where T : IFloatingPointIeee754<T>
Parameters
matrix
T[,]
Returns
- T[,]
Type Parameters
T
TrySolveLinearSystem<T>(T[,], T[], out T[]?, out string?)
Attempts to solve a system of linear equations Ax = b. For square systems (A is n x n), it uses Gaussian elimination. For overdetermined systems (A is m x n, m > n), it solves the normal equations A^T A x = A^T b using Gaussian elimination.
public static bool TrySolveLinearSystem<T>(T[,] A, T[] b, out T[]? solution, out string? errorMessage) where T : IFloatingPointIeee754<T>
Parameters
A
T[,]The matrix representing the system of linear equations. If A is square, it will be modified in place. If A is overdetermined, it is not modified.
b
T[]The vector representing the right-hand side of the equations. If A is square, b will be modified in place. If A is overdetermined, b is not modified.
solution
T[]When this method returns, contains the solution vector if the solution was found; otherwise, null.
errorMessage
stringWhen this method returns, contains an error message if the solution failed; otherwise, null.
Returns
- bool
True if the system was solved successfully; otherwise, false.
Type Parameters
T
The numeric type of the elements, implementing IFloatingPointIeee754<TSelf>.
Remarks
For square systems, this method modifies the input matrix A
and vector b
in place for efficiency.
For overdetermined systems, intermediate matrices derived from A
and b
are created and modified,
leaving the original A
and b
unchanged by the Gaussian elimination step.
Underdetermined systems (fewer rows than columns in A) are not supported.