MatrixExtensions
Helpers & Utilities · AiDotNet.Extensions
Provides extension methods for matrix operations, making it easier to work with matrices in AI applications.
How It Works
For Beginners: A matrix is a rectangular array of numbers arranged in rows and columns. These extension methods add useful functionality to matrices, like adding columns or performing mathematical operations that are commonly needed in AI and machine learning algorithms.
Methods
| Method | Summary |
|---|---|
IEnumerable<MatrixType> GetMatrixTypes(Matrix<T> matrix, IMatrixDecomposition<T> matrixDecomposition, T tolerance, int subDiagonalThreshold, int superDiagonalThreshold, T sparsityThreshold, T denseThreshold, int blockRows, int blockCols) |
Identifies the types of a matrix based on its properties. |
Matrix<Complex<T>> ConjugateTranspose(Matrix<Complex<T>> matrix) |
Computes the conjugate transpose (also known as Hermitian transpose) of a complex matrix. |
Matrix<Complex<T>> CreateComplexMatrix(Matrix<Complex<T>> matrix, int rows, int cols) |
Creates a new complex matrix with the specified dimensions. |
Matrix<Complex<T>> InvertUnitaryMatrix(Matrix<Complex<T>> matrix) |
Inverts a unitary matrix by taking its transpose. |
Matrix<Complex<T>> ToComplexMatrix(Matrix<T> matrix) |
Converts a real-valued matrix to a complex-valued matrix. |
Matrix<T> AddColumn(Matrix<T> matrix, Vector<T> column) |
Adds a new column to the right side of a matrix. |
Matrix<T> AddConstantColumn(Matrix<T> matrix, T value) |
Adds a constant value as the first column of a matrix. |
Matrix<T> AddVectorToEachRow(Matrix<T> matrix, Vector<T> vector) |
Adds a vector to each row of a matrix. |
Matrix<T> Extract(Matrix<T> matrix, int rows, int columns) |
Extracts a submatrix of specified dimensions from the top-left corner of the original matrix. |
Matrix<T> GetBlock(Matrix<T> matrix, int startRow, int startCol, int blockRows, int blockCols) |
Extracts a block (sub-matrix) from a matrix starting at the specified position. |
Matrix<T> GetColumns(Matrix<T> matrix, IEnumerable<int> columnIndices) |
Creates a new matrix containing only the specified columns from the original matrix. |
Matrix<T> GetRange(Matrix<T> matrix, T threshold) |
Computes the range (column space) of a matrix. |
Matrix<T> GetRowRange(Matrix<T> matrix, int startRow, int rowCount) |
Extracts a range of consecutive rows from the matrix. |
Matrix<T> Inverse(Matrix<T> matrix, InverseType inverseType, int maxIterations, T tolerance) |
Inverts a matrix using the specified algorithm. |
Matrix<T> InverseGaussianJordanElimination(Matrix<T> matrix) |
Inverts a matrix using the Gaussian-Jordan elimination method. |
Matrix<T> InverseNewton(Matrix<T> A, int maxIterations, T tolerance) |
Inverts a matrix using Newton’s iterative method. |
Matrix<T> InverseStrassen(Matrix<T> A) |
Inverts a matrix using Strassen’s algorithm. |
Matrix<T> InvertDiagonalMatrix(Matrix<T> matrix) |
Inverts a diagonal matrix. |
Matrix<T> InvertLowerTriangularMatrix(Matrix<T> matrix) |
Inverts a lower triangular matrix. |
Matrix<T> InvertUpperTriangularMatrix(Matrix<T> matrix) |
Inverts an upper triangular matrix. |
Matrix<T> KroneckerProduct(Matrix<T> a, Matrix<T> b) |
Computes the Kronecker product of two matrices. |
Matrix<T> MatrixExponential(Matrix<T> matrix, int order) |
Computes the matrix exponential e^A using a truncated Taylor series. |
Matrix<T> MatrixPower(Matrix<T> matrix, int power) |
Computes the matrix power A^k by repeated multiplication. |
Matrix<T> Negate(Matrix<T> matrix) |
Creates a new matrix with all elements negated. |
Matrix<T> Nullspace(Matrix<T> matrix, T threshold) |
Computes the null space (kernel) of a matrix. |
Matrix<T> PointwiseMultiply(Matrix<T> matrix, Matrix<T> other) |
Multiplies each row of a matrix by the corresponding element in a vector. |
Matrix<T> PointwiseMultiply(Matrix<T> matrix, Vector<T> vector) |
Multiplies each row of a matrix by the corresponding element in a vector. |
Matrix<T> Reshape(Matrix<T> matrix, int newRows, int newColumns) |
Reorganizes the elements of a matrix into a new matrix with different dimensions while preserving all data. |
Matrix<T> Submatrix(Matrix<T> matrix, int startRow, int startCol, int numRows, int numCols) |
Extracts a submatrix from the original matrix. |
Matrix<T> Submatrix(Matrix<T> matrix, int[] indices) |
Creates a submatrix from the given matrix using the specified indices. |
Matrix<T> ToRealMatrix(Matrix<Complex<T>> matrix) |
Extracts the real part of a complex-valued matrix to create a real-valued matrix. |
Matrix<T> Transpose(Matrix<T> matrix) |
Transposes a matrix by swapping its rows and columns. |
T Determinant(Matrix<T> matrix) |
Calculates the determinant of a square matrix. |
T FrobeniusNorm(Matrix<T> matrix) |
Calculates the Frobenius norm of a matrix. |
T GetDeterminant(Matrix<T> matrix) |
Calculates the determinant of a matrix. |
T LogDeterminant(Matrix<T> matrix) |
Calculates the logarithm of the determinant of a matrix. |
T Max(Matrix<T> matrix, Func<T, T> selector) |
Finds the maximum value in the matrix after applying a transformation function to each element. |
T Trace(Matrix<T> matrix) |
Computes the trace of a square matrix (sum of diagonal elements). |
T[] GetRow(Matrix<T> matrix, int rowIndex) |
Retrieves a specific row from the matrix as an array. |
Vector<Complex<T>> ToComplexVector(Vector<T> vector) |
Converts a real-valued vector to a complex-valued vector. |
Vector<T> BackwardSubstitution(Matrix<T> aMatrix, Vector<T> bVector) |
Performs backward substitution to solve a system of linear equations represented by an upper triangular matrix. |
Vector<T> Eigenvalues(Matrix<T> matrix) |
Calculates the eigenvalues of a symmetric matrix with the symmetric QR algorithm. |
Vector<T> Flatten(Matrix<T> matrix) |
Converts a two-dimensional matrix into a one-dimensional vector by placing all elements in a single row. |
Vector<T> ForwardSubstitution(Matrix<T> aMatrix, Vector<T> bVector) |
Solves a system of linear equations Ax = b using forward substitution, where A is a lower triangular matrix. |
Vector<T> GetColumn(Matrix<T> matrix, int columnIndex) |
Extracts a specific column from the matrix as a vector. |
Vector<T> GetSubColumn(Matrix<T> matrix, int columnIndex, int startRow, int length) |
Extracts a portion of a column from the matrix as a vector. |
Vector<T> RowWiseArgmax(Matrix<T> matrix) |
For each row in the matrix, finds the index of the column containing the maximum value. |
Vector<T> SumColumns(Matrix<T> matrix) |
Calculates the sum of each column in the matrix. |
Vector<T> ToVector(Matrix<T> matrix) |
Converts a matrix to a vector by flattening its elements in row-major order. |
Vector<T>[] GetColumnVectors(Matrix<T> matrix, int[] indices) |
Gets specific column vectors from a matrix based on the specified indices. |
bool IsAdjacencyMatrix(Matrix<T> matrix) |
Determines if a matrix is an adjacency matrix representing a graph. |
bool IsBandMatrix(Matrix<T> matrix, int subDiagonalThreshold, int superDiagonalThreshold) |
Determines if a matrix is a band matrix with specified sub-diagonal and super-diagonal thresholds. |
bool IsBlockMatrix(Matrix<T> matrix, int blockRows, int blockCols) |
Determines if a matrix can be divided into consistent blocks of a specified size. |
bool IsCauchyMatrix(Matrix<T> matrix) |
Determines if a matrix is a Cauchy matrix. |
bool IsCirculantMatrix(Matrix<T> matrix) |
Determines if a matrix is a circulant matrix. |
bool IsCompanionMatrix(Matrix<T> matrix) |
Determines if a matrix is a companion matrix. |
bool IsConsistentBlock(Matrix<T> block) |
Checks if all elements in a matrix block are identical. |
bool IsDenseMatrix(Matrix<T> matrix, T denseThreshold) |
Determines if a matrix is dense (contains mostly non-zero elements). |
bool IsDiagonalMatrix(Matrix<T> matrix) |
Determines if a matrix is diagonal (all non-diagonal elements are zero). |
bool IsDoublyStochasticMatrix(Matrix<T> matrix) |
Determines if a matrix is a doubly stochastic matrix. |
bool IsHankelMatrix(Matrix<T> matrix) |
Determines if a matrix is a Hankel matrix. |
bool IsHermitianMatrix(Matrix<Complex<T>> matrix) |
Determines if a complex matrix is Hermitian (equal to its conjugate transpose). |
bool IsHilbertMatrix(Matrix<T> matrix) |
Determines if a matrix is a Hilbert matrix. |
bool IsIdempotentMatrix(Matrix<T> matrix) |
Determines if a matrix is idempotent (equal to its own square). |
bool IsIdentityMatrix(Matrix<T> matrix) |
Determines if a matrix is an identity matrix (diagonal elements are 1, all others are 0). |
bool IsIncidenceMatrix(Matrix<T> matrix) |
Determines if a matrix is an incidence matrix for a graph. |
bool IsInvertible(Matrix<T> matrix) |
Determines whether a matrix is invertible. |
bool IsInvolutoryMatrix(Matrix<T> matrix) |
Determines if a matrix is an involutory matrix. |
bool IsLaplacianMatrix(Matrix<T> matrix) |
Determines if a matrix is a Laplacian matrix. |
bool IsLowerBidiagonalMatrix(Matrix<T> matrix) |
Determines if a matrix is a lower bidiagonal matrix. |
bool IsLowerTriangularMatrix(Matrix<T> matrix, T tolerance) |
Determines if a matrix is lower triangular (all elements above the main diagonal are zero). |
bool IsNonSingularMatrix(Matrix<T> matrix) |
Determines if a matrix is non-singular (invertible). |
bool IsOrthogonalMatrix(Matrix<T> matrix, IMatrixDecomposition<T> matrixDecomposition) |
Determines if a matrix is orthogonal (its transpose equals its inverse). |
bool IsOrthogonalProjectionMatrix(Matrix<T> matrix) |
Determines if a matrix is an orthogonal projection matrix. |
bool IsPartitionedMatrix(Matrix<T> matrix) |
Determines if a matrix can be considered a partitioned matrix. |
bool IsPermutationMatrix(Matrix<T> matrix) |
Determines if a matrix is a permutation matrix. |
bool IsPositiveDefiniteMatrix(Matrix<T> matrix, T tolerance) |
Determines if a matrix is positive definite. |
bool IsPositiveSemiDefiniteMatrix(Matrix<T> matrix) |
Determines if a matrix is positive semi-definite. |
bool IsRectangularMatrix(Matrix<T> matrix) |
Determines if a matrix is rectangular (has a different number of rows and columns). |
bool IsScalarMatrix(Matrix<T> matrix) |
Determines if a matrix is a scalar matrix (diagonal elements are equal, all others are 0). |
bool IsSingularMatrix(Matrix<T> matrix) |
Determines if a matrix is singular (non-invertible). |
bool IsSkewHermitianMatrix(Matrix<Complex<T>> matrix) |
Determines if a complex matrix is skew-Hermitian (equal to the negative of its conjugate transpose). |
bool IsSkewSymmetricMatrix(Matrix<T> matrix) |
Determines if a matrix is skew-symmetric (equal to the negative of its transpose). |
bool IsSparseMatrix(Matrix<T> matrix, T sparsityThreshold) |
Determines if a matrix is sparse (contains mostly zero elements). |
bool IsSquareMatrix(Matrix<T> matrix) |
Determines if a matrix is square (has the same number of rows and columns). |
bool IsStochasticMatrix(Matrix<T> matrix) |
Determines if a matrix is stochastic (each row sums to 1 and all elements are non-negative). |
bool IsSymmetricMatrix(Matrix<T> matrix) |
Determines if a matrix is symmetric (equal to its transpose). |
bool IsToeplitzMatrix(Matrix<T> matrix) |
Determines if a matrix is a Toeplitz matrix. |
bool IsTridiagonalMatrix(Matrix<T> matrix) |
Determines if a matrix is a tridiagonal matrix. |
bool IsUnitaryMatrix(Matrix<Complex<T>> matrix, IMatrixDecomposition<Complex<T>> matrixDecomposition) |
Determines if a matrix is unitary by checking if its conjugate transpose equals its inverse. |
bool IsUpperBidiagonalMatrix(Matrix<T> matrix) |
Determines if a matrix is upper bidiagonal (non-zero elements only on main diagonal and first superdiagonal). |
bool IsUpperTriangularMatrix(Matrix<T> matrix, T tolerance) |
Determines if a matrix is upper triangular (all elements below the main diagonal are zero). |
bool IsVandermondeMatrix(Matrix<T> matrix) |
Determines if a matrix is a Vandermonde matrix. |
bool IsZeroMatrix(Matrix<T> matrix) |
Determines if a matrix contains only zero values. |
int GetNullity(Matrix<T> matrix, T threshold) |
Calculates the nullity of a matrix, which is the dimension of its null space. |
int GetRank(Matrix<T> matrix, T threshold) |
Calculates the rank of a matrix based on a given threshold. |
void SetSubmatrix(Matrix<T> matrix, int startRow, int startCol, Matrix<T> submatrix) |
Sets a submatrix within a larger matrix. |
void SwapRows(Matrix<T> matrix, int row1Index, int row2Index) |
Swaps two rows in a matrix. |