Table of Contents

Class MatrixExtensions

Namespace
AiDotNet.Extensions
Assembly
AiDotNet.dll

Provides extension methods for matrix operations, making it easier to work with matrices in AI applications.

public static class MatrixExtensions
Inheritance
MatrixExtensions
Inherited Members

Remarks

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

AddColumn<T>(Matrix<T>, Vector<T>)

Adds a new column to the right side of a matrix.

public static Matrix<T> AddColumn<T>(this Matrix<T> matrix, Vector<T> column)

Parameters

matrix Matrix<T>

The matrix to add a column to.

column Vector<T>

The vector to add as a new column.

Returns

Matrix<T>

A new matrix with the additional column.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method creates a new matrix that includes all the data from your original matrix, plus an additional column at the right side. The new column's values come from the vector you provide. The vector must have the same number of elements as the matrix has rows.

This is useful when you need to augment your data with additional features or when constructing special matrices for certain algorithms.

Exceptions

ArgumentException

Thrown when the column length doesn't match the matrix row count.

AddConstantColumn<T>(Matrix<T>, T)

Adds a constant value as the first column of a matrix.

public static Matrix<T> AddConstantColumn<T>(this Matrix<T> matrix, T value)

Parameters

matrix Matrix<T>

The original matrix to modify.

value T

The constant value to add as the first column.

Returns

Matrix<T>

A new matrix with the constant column added as the first column.

Type Parameters

T

The numeric data type of the matrix elements.

Remarks

For Beginners: This method is often used in linear regression to add a "bias" term. It creates a new matrix with an extra column at the beginning, where every value in that column is the same (usually 1). This helps the AI model learn an offset or baseline value.

AddVectorToEachRow<T>(Matrix<T>, Vector<T>)

Adds a vector to each row of a matrix.

public static Matrix<T> AddVectorToEachRow<T>(this Matrix<T> matrix, Vector<T> vector)

Parameters

matrix Matrix<T>

The matrix to which the vector will be added.

vector Vector<T>

The vector to add to each row of the matrix.

Returns

Matrix<T>

A new matrix where each row is the sum of the original row and the vector.

Type Parameters

T

The numeric data type of the matrix and vector elements.

Remarks

For Beginners: This is like adding the same set of values to every row in your matrix. For example, if you have a matrix of features for different data points, and you want to adjust all features by the same amount, you would use this method.

Exceptions

ArgumentException

Thrown when the vector length doesn't match the matrix column count.

BackwardSubstitution<T>(Matrix<T>, Vector<T>)

Performs backward substitution to solve a system of linear equations represented by an upper triangular matrix.

public static Vector<T> BackwardSubstitution<T>(this Matrix<T> aMatrix, Vector<T> bVector)

Parameters

aMatrix Matrix<T>

The upper triangular coefficient matrix.

bVector Vector<T>

The right-hand side vector of the equation system.

Returns

Vector<T>

The solution vector x where Ax = b.

Type Parameters

T

The numeric data type of the matrix and vector elements.

Remarks

For Beginners: Backward substitution is a method to solve equations when your matrix is in a special form called "upper triangular" (where all values below the diagonal are zero).

This is often used as the final step in solving systems of linear equations, which is a common task in many AI algorithms like linear regression. The method starts from the bottom row and works upward, solving for one variable at a time.

ConjugateTranspose<T>(Matrix<Complex<T>>)

Computes the conjugate transpose (also known as Hermitian transpose) of a complex matrix.

public static Matrix<Complex<T>> ConjugateTranspose<T>(this Matrix<Complex<T>> matrix)

Parameters

matrix Matrix<Complex<T>>

The complex matrix to transpose and conjugate.

Returns

Matrix<Complex<T>>

A new matrix that is the conjugate transpose of the input matrix.

Type Parameters

T

The numeric type of the real and imaginary parts of the complex numbers.

Remarks

For Beginners: The conjugate transpose of a complex matrix is created by: 1. Flipping the matrix over its diagonal (exchanging rows and columns) 2. Taking the complex conjugate of each element (reversing the sign of the imaginary part)

For example, if you have a matrix: [a+bi c+di] [e+fi g+hi]

The conjugate transpose would be: [a-bi e-fi] [c-di g-hi]

This operation is important in quantum computing and signal processing applications.

CreateComplexMatrix<T>(Matrix<Complex<T>>, int, int)

Creates a new complex matrix with the specified dimensions.

public static Matrix<Complex<T>> CreateComplexMatrix<T>(this Matrix<Complex<T>> matrix, int rows, int cols)

Parameters

matrix Matrix<Complex<T>>

The source matrix (used for type inference).

rows int

The number of rows for the new matrix.

cols int

The number of columns for the new matrix.

Returns

Matrix<Complex<T>>

A new complex matrix with the specified dimensions.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This helper method creates a new empty complex matrix with the dimensions you specify. It's useful when you need to create a result matrix for operations on complex matrices.

Determinant<T>(Matrix<T>)

Calculates the determinant of a square matrix.

public static T Determinant<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to calculate the determinant for.

Returns

T

The determinant value.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The determinant is a special number calculated from a square matrix that tells you important information about the matrix. If the determinant is zero, the matrix is "singular" (has no inverse). The determinant also tells you how much the matrix scales areas or volumes when used as a transformation. This method uses a recursive approach to calculate the determinant.

Exceptions

ArgumentNullException

Thrown when the matrix is null.

ArgumentException

Thrown when the matrix is not square or has zero rows/columns.

Eigenvalues<T>(Matrix<T>)

Calculates the eigenvalues of a matrix using the QR algorithm.

public static Vector<T> Eigenvalues<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix for which to calculate eigenvalues.

Returns

Vector<T>

A vector containing the eigenvalues of the matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Eigenvalues are special scalars associated with a matrix. When the matrix is multiplied by certain vectors (called eigenvectors), the result is the same as multiplying those vectors by the eigenvalue. Eigenvalues are crucial in many applications including principal component analysis, vibration analysis, and quantum mechanics. This method uses the QR algorithm, which is an iterative approach to find eigenvalues of a matrix.

Extract<T>(Matrix<T>, int, int)

Extracts a submatrix of specified dimensions from the top-left corner of the original matrix.

public static Matrix<T> Extract<T>(this Matrix<T> matrix, int rows, int columns)

Parameters

matrix Matrix<T>

The source matrix.

rows int

The number of rows to extract.

columns int

The number of columns to extract.

Returns

Matrix<T>

A new matrix containing the extracted elements.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method creates a smaller matrix from a larger one by taking only the specified number of rows and columns from the top-left corner. Think of it like cropping a photo to focus on just one part of the image.

Flatten<T>(Matrix<T>)

Converts a two-dimensional matrix into a one-dimensional vector by placing all elements in a single row.

public static Vector<T> Flatten<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to flatten.

Returns

Vector<T>

A vector containing all elements of the matrix in row-major order.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Flattening a matrix means converting it from a 2D structure (rows and columns) into a 1D structure (a single line of values). This method takes all the values from your matrix and puts them into a vector (a one-dimensional array), reading from left to right, top to bottom.

For example, if you have a 2×3 matrix: [1, 2, 3] [4, 5, 6] The flattened vector would be: [1, 2, 3, 4, 5, 6]

This is commonly used in machine learning when you need to feed a 2D structure (like an image) into an algorithm that only accepts 1D inputs.

ForwardSubstitution<T>(Matrix<T>, Vector<T>)

Solves a system of linear equations Ax = b using forward substitution, where A is a lower triangular matrix.

public static Vector<T> ForwardSubstitution<T>(this Matrix<T> aMatrix, Vector<T> bVector)

Parameters

aMatrix Matrix<T>

The lower triangular coefficient matrix A.

bVector Vector<T>

The right-hand side vector b.

Returns

Vector<T>

The solution vector x.

Type Parameters

T

The numeric type of the matrix and vector elements.

Remarks

For Beginners: Forward substitution is a method for solving a system of linear equations where the coefficient matrix is lower triangular (all elements above the main diagonal are zero). The method works by solving for each variable in sequence, starting from the first equation and using previously computed values.

FrobeniusNorm<T>(Matrix<T>)

Calculates the Frobenius norm of a matrix.

public static T FrobeniusNorm<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to calculate the norm for.

Returns

T

The Frobenius norm value.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The Frobenius norm is a way to measure the "size" of a matrix. It's calculated by squaring each element, adding them all up, and then taking the square root of that sum. Think of it like finding the length of a vector, but for a matrix.

GetBlock<T>(Matrix<T>, int, int, int, int)

Extracts a block (sub-matrix) from a matrix starting at the specified position.

public static Matrix<T> GetBlock<T>(this Matrix<T> matrix, int startRow, int startCol, int blockRows, int blockCols)

Parameters

matrix Matrix<T>

The source matrix.

startRow int

The starting row index (0-based).

startCol int

The starting column index (0-based).

blockRows int

The number of rows in the block.

blockCols int

The number of columns in the block.

Returns

Matrix<T>

A new matrix containing the extracted block.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method lets you take a smaller piece (or "block") out of a larger matrix. It's like cutting out a rectangular section from a grid. You specify where to start cutting (startRow, startCol) and how big a piece to cut (blockRows, blockCols).

GetColumnVectors<T>(Matrix<T>, int[])

Gets specific column vectors from a matrix based on the specified indices.

public static Vector<T>[] GetColumnVectors<T>(this Matrix<T> matrix, int[] indices)

Parameters

matrix Matrix<T>

The matrix from which to extract columns.

indices int[]

The indices of the columns to extract.

Returns

Vector<T>[]

An array of vectors representing the selected columns.

Type Parameters

T

Remarks

This extension method allows for efficient extraction of specific columns from a matrix. It creates a new array of vectors where each vector represents one of the requested columns.

For Beginners: This method lets you pull out specific columns from your data matrix.

Imagine your data as a grid of numbers:

  • Each column might represent a feature like age, height, or temperature
  • This method lets you select only the columns you're interested in
  • The result is a collection of vectors, where each vector is one column

For example, if you only want columns 0, 3, and 5 from a matrix with 10 columns, you would pass [0, 3, 5] as the indices.

GetColumn<T>(Matrix<T>, int)

Extracts a specific column from the matrix as a vector.

public static Vector<T> GetColumn<T>(this Matrix<T> matrix, int columnIndex)

Parameters

matrix Matrix<T>

The matrix from which to extract the column.

columnIndex int

The index of the column to extract.

Returns

Vector<T>

A vector containing the values from the specified column.

Type Parameters

T

The numeric data type of the matrix elements.

Remarks

For Beginners: This method pulls out a single column from your matrix and returns it as a vector. This is useful when you need to work with just one feature or dimension from your dataset.

GetColumns<T>(Matrix<T>, IEnumerable<int>)

Creates a new matrix containing only the specified columns from the original matrix.

public static Matrix<T> GetColumns<T>(this Matrix<T> matrix, IEnumerable<int> columnIndices)

Parameters

matrix Matrix<T>

The source matrix to extract columns from.

columnIndices IEnumerable<int>

A collection of zero-based indices of columns to extract.

Returns

Matrix<T>

A new matrix containing only the specified columns.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method allows you to create a new matrix that includes only specific columns from your original matrix. For example, if you have a dataset where each column represents a different feature, you can use this method to select only the features you're interested in.

The order of columns in the result will match the order of indices in the columnIndices collection.

GetDeterminant<T>(Matrix<T>)

Calculates the determinant of a matrix.

public static T GetDeterminant<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix for which to calculate the determinant.

Returns

T

The determinant of the matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The determinant is a special number calculated from a square matrix. It tells you important information about the matrix, such as whether it has an inverse (when determinant is not zero). Geometrically, the determinant represents how much the matrix scales volumes. This method uses a recursive approach called cofactor expansion to calculate the determinant.

GetMatrixTypes<T>(Matrix<T>, IMatrixDecomposition<T>, T?, int, int, T?, T?, int, int)

Identifies the types of a matrix based on its properties.

public static IEnumerable<MatrixType> GetMatrixTypes<T>(this Matrix<T> matrix, IMatrixDecomposition<T> matrixDecomposition, T? tolerance = default, int subDiagonalThreshold = 1, int superDiagonalThreshold = 1, T? sparsityThreshold = default, T? denseThreshold = default, int blockRows = 2, int blockCols = 2)

Parameters

matrix Matrix<T>

The matrix to analyze.

matrixDecomposition IMatrixDecomposition<T>

The decomposition method to use for certain matrix type checks.

tolerance T

The numerical tolerance for floating-point comparisons (default: 1e-10).

subDiagonalThreshold int

The threshold for sub-diagonal elements (default: 1).

superDiagonalThreshold int

The threshold for super-diagonal elements (default: 1).

sparsityThreshold T

The threshold ratio for determining if a matrix is sparse (default: 0.5).

denseThreshold T

The threshold ratio for determining if a matrix is dense (default: 0.5).

blockRows int

The number of rows in each block for block matrix detection (default: 2).

blockCols int

The number of columns in each block for block matrix detection (default: 2).

Returns

IEnumerable<MatrixType>

An enumeration of matrix types that apply to the given matrix.

Type Parameters

T

The numeric data type of the matrix elements.

Remarks

For Beginners: This method examines a matrix and tells you what special properties it has.

Matrices can have many different characteristics (like being symmetric, triangular, etc.) that make them behave in special ways. Knowing these properties can help you choose the right algorithms to work with your data efficiently.

For example, if your matrix is "sparse" (mostly zeros), you can use special techniques that save memory and computation time.

GetNullity<T>(Matrix<T>, T?)

Calculates the nullity of a matrix, which is the dimension of its null space.

public static int GetNullity<T>(this Matrix<T> matrix, T? threshold = default)

Parameters

matrix Matrix<T>

The matrix to analyze.

threshold T

Optional threshold value for determining when a value is considered zero. If not provided, a default threshold is calculated.

Returns

int

The nullity of the matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The nullity of a matrix tells you how many independent ways there are to get zero when multiplying the matrix by a vector. In practical terms, it helps identify how much "redundant" information is in your data. A higher nullity means more redundancy or dependency in your data.

GetRange<T>(Matrix<T>, T)

Computes the range (column space) of a matrix.

public static Matrix<T> GetRange<T>(this Matrix<T> matrix, T threshold)

Parameters

matrix Matrix<T>

The matrix to analyze.

threshold T

The threshold value for determining when a value is considered zero.

Returns

Matrix<T>

A matrix whose columns form a basis for the range of the input matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The range of a matrix represents all possible outputs you can get when multiplying the matrix by any vector. It shows what kinds of transformations or changes the matrix can produce. Understanding the range helps identify what patterns or variations your data can represent.

GetRank<T>(Matrix<T>, T)

Calculates the rank of a matrix based on a given threshold.

public static int GetRank<T>(this Matrix<T> matrix, T threshold)

Parameters

matrix Matrix<T>

The matrix to calculate the rank for.

threshold T

The threshold value for determining linearly independent rows/columns.

Returns

int

The rank of the matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The rank of a matrix tells you how many linearly independent rows or columns it has. Think of it as the number of dimensions the matrix can represent. The threshold parameter helps determine when values are considered significant enough to contribute to the rank.

GetRowRange<T>(Matrix<T>, int, int)

Extracts a range of consecutive rows from the matrix.

public static Matrix<T> GetRowRange<T>(this Matrix<T> matrix, int startRow, int rowCount)

Parameters

matrix Matrix<T>

The source matrix to extract rows from.

startRow int

The zero-based index of the first row to include.

rowCount int

The number of consecutive rows to extract.

Returns

Matrix<T>

A new matrix containing the specified rows.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method lets you extract a sequence of rows from your matrix. For example, if you have a dataset where each row represents a different observation, you can use this method to select a specific range of observations (like rows 10-20).

The resulting matrix will have the same number of columns as the original matrix.

GetRow<T>(Matrix<T>, int)

Retrieves a specific row from the matrix as an array.

public static T[] GetRow<T>(this Matrix<T> matrix, int rowIndex)

Parameters

matrix Matrix<T>

The matrix to extract the row from.

rowIndex int

The zero-based index of the row to retrieve.

Returns

T[]

An array containing all elements in the specified row.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method extracts a single horizontal row of values from your matrix. Think of it like selecting an entire row from a spreadsheet. The rowIndex parameter specifies which row you want (starting from 0 for the first row).

GetSubColumn<T>(Matrix<T>, int, int, int)

Extracts a portion of a column from the matrix as a vector.

public static Vector<T> GetSubColumn<T>(this Matrix<T> matrix, int columnIndex, int startRow, int length)

Parameters

matrix Matrix<T>

The matrix to extract the column portion from.

columnIndex int

The zero-based index of the column to extract from.

startRow int

The zero-based index of the first row to include.

length int

The number of elements to extract from the column.

Returns

Vector<T>

A vector containing the specified portion of the column.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method extracts a section of a vertical column from your matrix. You specify which column you want with columnIndex, where to start in that column with startRow, and how many values to extract with length. The result is a vector (a one-dimensional array) containing just those values.

Exceptions

ArgumentOutOfRangeException

Thrown when columnIndex is outside the matrix bounds, startRow is outside the matrix bounds, or the requested length would extend beyond the matrix bounds.

InverseGaussianJordanElimination<T>(Matrix<T>)

Inverts a matrix using the Gaussian-Jordan elimination method.

public static Matrix<T> InverseGaussianJordanElimination<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to invert.

Returns

Matrix<T>

The inverted matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Matrix inversion is like finding the reciprocal of a number. For example, the reciprocal of 2 is 1/2. Similarly, the inverse of a matrix A is another matrix that, when multiplied with A, gives the identity matrix (similar to how 2 × 1/2 = 1). The Gaussian-Jordan elimination is a step-by-step process to find this inverse by transforming the original matrix into the identity matrix.

Exceptions

InvalidOperationException

Thrown when the matrix is singular and cannot be inverted.

InverseNewton<T>(Matrix<T>, int, T?)

Inverts a matrix using Newton's iterative method.

public static Matrix<T> InverseNewton<T>(this Matrix<T> A, int maxIterations = 100, T? tolerance = default)

Parameters

A Matrix<T>

The matrix to invert.

maxIterations int

The maximum number of iterations to perform.

tolerance T

The convergence tolerance. If null, a default value is used.

Returns

Matrix<T>

The inverted matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Matrix inversion is like finding the reciprocal of a number (e.g., 1/x). Newton's method is an iterative approach that gradually improves an initial guess for the inverse. This method keeps refining the approximation until it's accurate enough (determined by the tolerance) or until it reaches the maximum number of iterations.

Exceptions

ArgumentException

Thrown when the matrix is not square.

InvalidOperationException

Thrown when Newton's method does not converge.

InverseStrassen<T>(Matrix<T>)

Inverts a matrix using Strassen's algorithm.

public static Matrix<T> InverseStrassen<T>(this Matrix<T> A)

Parameters

A Matrix<T>

The matrix to invert.

Returns

Matrix<T>

The inverted matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Strassen's algorithm is a divide-and-conquer approach to matrix operations. It works by breaking down the matrix into smaller submatrices, solving the smaller problems, and then combining the results. This method requires that the matrix size be a power of 2 (like 2, 4, 8, 16, etc.). It's often more efficient for large matrices than standard methods.

Exceptions

ArgumentException

Thrown when the matrix is not square or its size is not a power of 2.

Inverse<T>(Matrix<T>, InverseType, int, T?)

Inverts a matrix using the specified algorithm.

public static Matrix<T> Inverse<T>(this Matrix<T> matrix, InverseType inverseType = InverseType.GaussianJordan, int maxIterations = 100, T? tolerance = default)

Parameters

matrix Matrix<T>

The matrix to invert.

inverseType InverseType

The algorithm to use for matrix inversion.

maxIterations int

The maximum number of iterations for iterative methods.

tolerance T

The convergence tolerance for iterative methods.

Returns

Matrix<T>

The inverted matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method is a convenient way to invert a matrix using different algorithms. You can choose between: - GaussianJordan: A direct method that works for any square matrix. - Newton: An iterative method that gradually improves the approximation. - Strassen: A divide-and-conquer method for matrices with dimensions that are powers of 2. Each method has its advantages in different situations.

Exceptions

ArgumentException

Thrown when an invalid inverse type is specified.

InvertDiagonalMatrix<T>(Matrix<T>)

Inverts a diagonal matrix.

public static Matrix<T> InvertDiagonalMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The diagonal matrix to invert.

Returns

Matrix<T>

The inverted diagonal matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A diagonal matrix has non-zero values only along its main diagonal (top-left to bottom-right). Inverting a diagonal matrix is simple - just replace each diagonal element with its reciprocal (1/value). This method assumes the input is already a diagonal matrix.

InvertLowerTriangularMatrix<T>(Matrix<T>)

Inverts a lower triangular matrix.

public static Matrix<T> InvertLowerTriangularMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The lower triangular matrix to invert.

Returns

Matrix<T>

The inverted lower triangular matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A lower triangular matrix is a square matrix where all elements above the main diagonal are zero. Inverting a matrix means finding another matrix that, when multiplied with the original matrix, gives the identity matrix. This method provides a specialized, efficient way to invert lower triangular matrices.

InvertUnitaryMatrix<T>(Matrix<Complex<T>>)

Inverts a unitary matrix by taking its transpose.

public static Matrix<Complex<T>> InvertUnitaryMatrix<T>(this Matrix<Complex<T>> matrix)

Parameters

matrix Matrix<Complex<T>>

The unitary matrix to invert.

Returns

Matrix<Complex<T>>

The inverted unitary matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A unitary matrix is a special type of matrix where its inverse equals its conjugate transpose. This property makes unitary matrices very useful in many AI and mathematical applications because they preserve the length of vectors and the angles between them. For real-valued matrices, unitary matrices are called orthogonal matrices.

InvertUpperTriangularMatrix<T>(Matrix<T>)

Inverts an upper triangular matrix.

public static Matrix<T> InvertUpperTriangularMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The upper triangular matrix to invert.

Returns

Matrix<T>

The inverted upper triangular matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An upper triangular matrix is a square matrix where all elements below the main diagonal are zero. Inverting a matrix means finding another matrix that, when multiplied with the original matrix, gives the identity matrix. This method provides a specialized, efficient way to invert upper triangular matrices.

IsAdjacencyMatrix<T>(Matrix<T>)

Determines if a matrix is an adjacency matrix representing a graph.

public static bool IsAdjacencyMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is an adjacency matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An adjacency matrix represents connections between nodes in a graph: - The matrix must be square (same number of rows and columns) - Each element must be either 0 or 1 - A value of 1 at position [i,j] means there is a connection from node i to node j - A value of 0 means there is no connection

This is a fundamental way to represent relationships between objects in computer science.

IsBandMatrix<T>(Matrix<T>, int, int)

Determines if a matrix is a band matrix with specified sub-diagonal and super-diagonal thresholds.

public static bool IsBandMatrix<T>(this Matrix<T> matrix, int subDiagonalThreshold = 1, int superDiagonalThreshold = 1)

Parameters

matrix Matrix<T>

The matrix to check.

subDiagonalThreshold int

The number of sub-diagonals to consider (default is 1).

superDiagonalThreshold int

The number of super-diagonals to consider (default is 1).

Returns

bool

True if the matrix is a band matrix with the specified thresholds; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A band matrix is a square matrix where all elements are zero except for those on the main diagonal and a specific number of diagonals above and below it. The parameters let you specify how many diagonals above and below the main diagonal can have non-zero values.

IsBlockMatrix<T>(Matrix<T>, int, int)

Determines if a matrix can be divided into consistent blocks of a specified size.

public static bool IsBlockMatrix<T>(this Matrix<T> matrix, int blockRows, int blockCols)

Parameters

matrix Matrix<T>

The matrix to check.

blockRows int

The number of rows in each block.

blockCols int

The number of columns in each block.

Returns

bool

True if the matrix can be divided into consistent blocks, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A block matrix is a matrix that can be divided into smaller matrices (blocks) where each block has the same properties. Think of it like dividing a large grid into smaller, identical sub-grids. This method checks if your matrix can be neatly divided this way.

IsCauchyMatrix<T>(Matrix<T>)

Determines if a matrix is a Cauchy matrix.

public static bool IsCauchyMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Cauchy matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Cauchy matrix is formed from two sequences of numbers (x1, x2, ...) and (y1, y2, ...). Each element at position [i,j] equals 1/(x_i - y_j).

This method verifies the Cauchy property by checking that for any 2x2 submatrix, the difference of reciprocals is consistent across rows: 1/C[i,j] - 1/C[i,l] = 1/C[k,j] - 1/C[k,l] for all i,k,j,l

Cauchy matrices have applications in interpolation problems and numerical analysis.

IsCirculantMatrix<T>(Matrix<T>)

Determines if a matrix is a circulant matrix.

public static bool IsCirculantMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is circulant; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A circulant matrix is a special square matrix where: 1. Each row is a circular shift of the row above it 2. The first row defines the entire matrix 3. Each subsequent row shifts the elements one position to the right

For example, if the first row is [a, b, c], the circulant matrix would be: [a, b, c] [c, a, b] [b, c, a]

These matrices have special properties that make them useful in signal processing and solving certain types of equations.

IsCompanionMatrix<T>(Matrix<T>)

Determines if a matrix is a companion matrix.

public static bool IsCompanionMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a companion matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A companion matrix is a special square matrix that helps solve polynomial equations. It has a specific structure where: 1. The first row contains coefficients of a polynomial 2. The subdiagonal (the diagonal just below the main diagonal) contains all 1's 3. All other elements are 0

Companion matrices are useful for finding roots of polynomials and in control theory.

IsConsistentBlock<T>(Matrix<T>)

Checks if all elements in a matrix block are identical.

public static bool IsConsistentBlock<T>(this Matrix<T> block)

Parameters

block Matrix<T>

The matrix block to check.

Returns

bool

True if all elements in the block are identical, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method checks if all numbers in a matrix are exactly the same. For example, if every position in your grid contains the number 5, this would return true. If there's even one different number, it returns false.

IsDenseMatrix<T>(Matrix<T>, T?)

Determines if a matrix is dense (contains mostly non-zero elements).

public static bool IsDenseMatrix<T>(this Matrix<T> matrix, T? denseThreshold = default)

Parameters

matrix Matrix<T>

The matrix to check.

denseThreshold T

Optional threshold that determines how many non-zeros make a matrix dense. Default is 0.5 (50%).

Returns

bool

True if the matrix is dense, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A dense matrix is the opposite of a sparse matrix - it has mostly non-zero values. Dense matrices typically require different processing techniques than sparse ones. By default, this method considers a matrix dense if at least 50% of its elements are non-zero, but you can adjust this threshold.

IsDiagonalMatrix<T>(Matrix<T>)

Determines if a matrix is diagonal (all non-diagonal elements are zero).

public static bool IsDiagonalMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is diagonal; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A diagonal matrix has values only along its main diagonal (from top-left to bottom-right), and zeros everywhere else. For example: [5 0 0] [0 2 0] [0 0 9] This is a diagonal matrix because only the diagonal positions have non-zero values.

IsDoublyStochasticMatrix<T>(Matrix<T>)

Determines if a matrix is a doubly stochastic matrix.

public static bool IsDoublyStochasticMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is doubly stochastic; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A doubly stochastic matrix is a square matrix where: 1. All elements are non-negative (greater than or equal to zero) 2. The sum of elements in each row equals 1 3. The sum of elements in each column equals 1

These matrices are commonly used in probability theory and Markov chains to represent transition probabilities between states.

IsHankelMatrix<T>(Matrix<T>)

Determines if a matrix is a Hankel matrix.

public static bool IsHankelMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Hankel matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Hankel matrix is a matrix where each skew-diagonal (running from bottom-left to top-right) contains the same value. In other words, the value at position (i,j) depends only on the sum i+j.

Hankel matrices appear in signal processing, control theory, and when solving certain types of differential equations.

IsHermitianMatrix<T>(Matrix<Complex<T>>)

Determines if a complex matrix is Hermitian (equal to its conjugate transpose).

public static bool IsHermitianMatrix<T>(this Matrix<Complex<T>> matrix)

Parameters

matrix Matrix<Complex<T>>

The complex matrix to check.

Returns

bool

True if the matrix is Hermitian; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Hermitian matrix is a complex square matrix that equals its own conjugate transpose. The conjugate transpose means you flip the matrix along its main diagonal and take the complex conjugate of each element (change the sign of the imaginary part). Hermitian matrices are the complex equivalent of symmetric matrices in real numbers.

IsHilbertMatrix<T>(Matrix<T>)

Determines if a matrix is a Hilbert matrix.

public static bool IsHilbertMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Hilbert matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Hilbert matrix is a special square matrix where each element at position (i,j) is defined as 1/(i+j+1). These matrices are important in numerical analysis but are known to be difficult to work with because they become increasingly ill-conditioned as their size grows.

IsIdempotentMatrix<T>(Matrix<T>)

Determines if a matrix is idempotent (equal to its own square).

public static bool IsIdempotentMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is idempotent; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An idempotent matrix is a matrix that, when multiplied by itself, gives the same matrix: A² = A.

This property is important in:

  • Projection matrices in linear algebra
  • Statistical operations like hat matrices in regression
  • Machine learning algorithms that involve projections onto subspaces

Examples of idempotent matrices include identity matrices and projection matrices.

IsIdentityMatrix<T>(Matrix<T>)

Determines if a matrix is an identity matrix (diagonal elements are 1, all others are 0).

public static bool IsIdentityMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is an identity matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An identity matrix is a special diagonal matrix where all the diagonal values are 1. It's like the number "1" in multiplication - when you multiply any matrix by the identity matrix, you get the original matrix back unchanged. For example: [1 0 0] [0 1 0] [0 0 1]

IsIncidenceMatrix<T>(Matrix<T>)

Determines if a matrix is an incidence matrix for a graph.

public static bool IsIncidenceMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is an incidence matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An incidence matrix represents the relationship between vertices (rows) and edges (columns) in a graph. This implementation supports both undirected and directed graphs:

For an undirected graph:

  • Each element is either 0 or 1
  • Each column has exactly two 1's (representing the two vertices connected by that edge)

For a directed graph:

  • Each element is -1, 0, or +1
  • Each column has exactly one +1 (edge source) and one -1 (edge destination)
  • The sum of each column must be 0

Incidence matrices are used in graph theory and network analysis to represent connections between nodes.

IsInvertible<T>(Matrix<T>)

Determines whether a matrix is invertible.

public static bool IsInvertible<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is invertible; otherwise, false.

Type Parameters

T

The numeric type used for matrix elements.

Remarks

This method checks if a matrix is invertible by verifying that it is square and calculating its determinant. If the determinant is zero (or very close to zero for floating-point types), the matrix is not invertible. A matrix is invertible if and only if its determinant is non-zero.

For Beginners: This checks if you can find the inverse of a matrix.

A matrix is invertible when:

  • It is square (same number of rows and columns)
  • Its determinant is not zero

The inverse of a matrix is like the reciprocal of a number (1/x). Not all matrices have inverses, just like division by zero is not allowed.

This method is important for solving systems of linear equations and is used in many machine learning algorithms.

IsInvolutoryMatrix<T>(Matrix<T>)

Determines if a matrix is an involutory matrix.

public static bool IsInvolutoryMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is an involutory matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An involutory matrix is a matrix that, when multiplied by itself, gives the identity matrix. In other words, it's its own inverse (A² = I). These matrices are useful in various applications including cryptography and computer graphics.

IsLaplacianMatrix<T>(Matrix<T>)

Determines if a matrix is a Laplacian matrix.

public static bool IsLaplacianMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Laplacian matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Laplacian matrix represents a graph and has special properties: 1. It's symmetric (mirror image across the diagonal) 2. Off-diagonal elements are non-positive (zero or negative) 3. Each row and column sums to zero 4. Each diagonal element equals the sum of the absolute values of the off-diagonal elements in its row

Laplacian matrices are used in graph theory, network analysis, and spectral clustering algorithms.

IsLowerBidiagonalMatrix<T>(Matrix<T>)

Determines if a matrix is a lower bidiagonal matrix.

public static bool IsLowerBidiagonalMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is lower bidiagonal; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A lower bidiagonal matrix is a square matrix where all elements are zero except for the main diagonal and the diagonal immediately below it (the first subdiagonal). For example: [a 0 0] [b c 0] [0 d e]

IsLowerTriangularMatrix<T>(Matrix<T>, T?)

Determines if a matrix is lower triangular (all elements above the main diagonal are zero).

public static bool IsLowerTriangularMatrix<T>(this Matrix<T> matrix, T? tolerance = default)

Parameters

matrix Matrix<T>

The matrix to check.

tolerance T

Optional tolerance value for floating-point comparisons. Default is 1e-10.

Returns

bool

True if the matrix is lower triangular, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A lower triangular matrix has all its non-zero values either on or below the main diagonal (the diagonal line from top-left to bottom-right). Everything above this diagonal is zero. Like upper triangular matrices, lower triangular matrices simplify many mathematical operations.

The tolerance parameter helps when working with decimal numbers that might have tiny rounding errors.

IsNonSingularMatrix<T>(Matrix<T>)

Determines if a matrix is non-singular (invertible).

public static bool IsNonSingularMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is non-singular; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A non-singular matrix is a square matrix that has an inverse. This happens when the determinant of the matrix is not zero. Non-singular matrices represent transformations that can be reversed, like rotating or scaling in ways that preserve all dimensions.

Having a non-singular matrix is often a requirement for solving systems of equations and many other mathematical operations in AI and machine learning.

IsOrthogonalMatrix<T>(Matrix<T>, IMatrixDecomposition<T>)

Determines if a matrix is orthogonal (its transpose equals its inverse).

public static bool IsOrthogonalMatrix<T>(this Matrix<T> matrix, IMatrixDecomposition<T> matrixDecomposition)

Parameters

matrix Matrix<T>

The matrix to check.

matrixDecomposition IMatrixDecomposition<T>

The matrix decomposition to use for calculating the inverse.

Returns

bool

True if the matrix is orthogonal; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An orthogonal matrix is a square matrix whose transpose equals its inverse. This means that when you multiply an orthogonal matrix by its transpose, you get the identity matrix. Orthogonal matrices preserve lengths and angles when used for transformations, making them useful in computer graphics, physics, and data analysis.

IsOrthogonalProjectionMatrix<T>(Matrix<T>)

Determines if a matrix is an orthogonal projection matrix.

public static bool IsOrthogonalProjectionMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is an orthogonal projection matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An orthogonal projection matrix is a matrix that projects vectors onto a subspace. It has two key properties: it's symmetric (equal to its transpose) and idempotent (multiplying it by itself gives the same matrix). In simpler terms, it's used to "flatten" data onto a lower-dimensional space while preserving as much information as possible.

IsPartitionedMatrix<T>(Matrix<T>)

Determines if a matrix can be considered a partitioned matrix.

public static bool IsPartitionedMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix can be partitioned; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A partitioned matrix is a matrix that can be divided into smaller submatrices. This method checks if the number of rows is divisible by the square root of the number of columns, which is one way to determine if a matrix can be neatly partitioned into equal-sized blocks.

Partitioned matrices are useful in block matrix operations and can simplify complex matrix calculations.

IsPermutationMatrix<T>(Matrix<T>)

Determines if a matrix is a permutation matrix.

public static bool IsPermutationMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a permutation matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A permutation matrix is a special square matrix that has exactly one entry of 1 in each row and each column, with all other entries being 0. These matrices are used to represent rearrangements (permutations) of elements in a vector.

IsPositiveDefiniteMatrix<T>(Matrix<T>, T?)

Determines if a matrix is positive definite.

public static bool IsPositiveDefiniteMatrix<T>(this Matrix<T> matrix, T? tolerance = default)

Parameters

matrix Matrix<T>

The matrix to check.

tolerance T

Optional tolerance value for numerical stability. Default is 1e-10.

Returns

bool

True if the matrix is positive definite; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A positive definite matrix is a special type of symmetric matrix where: 1. The matrix must be symmetric (equal to its transpose) 2. All eigenvalues of the matrix are positive

In simpler terms, when you multiply this matrix by any non-zero vector, the result points in a direction that makes a positive angle with the original vector.

Positive definite matrices are important in machine learning for:

  • Covariance matrices in statistics
  • Kernel methods like Support Vector Machines
  • Optimization problems where we need to ensure a unique minimum exists

This method uses Cholesky decomposition to check for positive definiteness, which is more efficient than calculating eigenvalues directly.

IsPositiveSemiDefiniteMatrix<T>(Matrix<T>)

Determines if a matrix is positive semi-definite.

public static bool IsPositiveSemiDefiniteMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is positive semi-definite; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A positive semi-definite matrix is a symmetric matrix where all eigenvalues are non-negative. These matrices are important in machine learning, statistics, and optimization problems. They represent covariance matrices, kernel matrices in kernel methods, and Hessian matrices in certain optimization problems. A key property is that for any vector x, x^T*A*x = 0, which means these matrices preserve or increase vector lengths in certain directions.

IsRectangularMatrix<T>(Matrix<T>)

Determines if a matrix is rectangular (has a different number of rows and columns).

public static bool IsRectangularMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix has a different number of rows and columns; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A rectangular matrix has a different number of rows and columns. For example, a 2×3 matrix (2 rows, 3 columns) is rectangular.

IsScalarMatrix<T>(Matrix<T>)

Determines if a matrix is a scalar matrix (diagonal elements are equal, all others are 0).

public static bool IsScalarMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a scalar matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A scalar matrix is a special type of diagonal matrix where all the diagonal values are the same. For example: [3 0 0] [0 3 0] [0 0 3] This is a scalar matrix because all diagonal elements have the same value (3) and all other elements are zero.

IsSingularMatrix<T>(Matrix<T>)

Determines if a matrix is singular (non-invertible).

public static bool IsSingularMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is singular; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A singular matrix is a square matrix that doesn't have an inverse. This happens when the determinant of the matrix is zero. In practical terms, singular matrices represent transformations that collapse dimensions (like projecting 3D onto a plane), making it impossible to reverse the transformation.

Singular matrices cause problems in many algorithms because they can't be inverted, which is why it's important to check for this condition.

IsSkewHermitianMatrix<T>(Matrix<Complex<T>>)

Determines if a complex matrix is skew-Hermitian (equal to the negative of its conjugate transpose).

public static bool IsSkewHermitianMatrix<T>(this Matrix<Complex<T>> matrix)

Parameters

matrix Matrix<Complex<T>>

The complex matrix to check.

Returns

bool

True if the matrix is skew-Hermitian; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A skew-Hermitian matrix is a complex square matrix that equals the negative of its conjugate transpose. This means when you flip the matrix along its main diagonal, take the complex conjugate of each element, and then negate all values, you get back the original matrix. The diagonal elements of a skew-Hermitian matrix must be purely imaginary or zero.

IsSkewSymmetricMatrix<T>(Matrix<T>)

Determines if a matrix is skew-symmetric (equal to the negative of its transpose).

public static bool IsSkewSymmetricMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is skew-symmetric; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A skew-symmetric matrix is one where the value at position (i,j) is the negative of the value at position (j,i). For example, if the value at row 2, column 3 is 5, then the value at row 3, column 2 must be -5. Also, all diagonal elements must be zero because a number cannot be the negative of itself (unless it's zero).

IsSparseMatrix<T>(Matrix<T>, T?)

Determines if a matrix is sparse (contains mostly zero elements).

public static bool IsSparseMatrix<T>(this Matrix<T> matrix, T? sparsityThreshold = default)

Parameters

matrix Matrix<T>

The matrix to check.

sparsityThreshold T

Optional threshold that determines how many zeros make a matrix sparse. Default is 0.5 (50%).

Returns

bool

True if the matrix is sparse, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A sparse matrix is one that has mostly zeros. This is important because sparse matrices can be stored and processed more efficiently using special techniques. By default, this method considers a matrix sparse if at least 50% of its elements are zero, but you can adjust this threshold.

IsSquareMatrix<T>(Matrix<T>)

Determines if a matrix is square (has the same number of rows and columns).

public static bool IsSquareMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix has the same number of rows and columns; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A square matrix is simply a matrix with the same number of rows and columns. For example, a 3×3 matrix is square, while a 2×3 matrix is not.

IsStochasticMatrix<T>(Matrix<T>)

Determines if a matrix is stochastic (each row sums to 1 and all elements are non-negative).

public static bool IsStochasticMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is stochastic; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A stochastic matrix (also called a probability matrix or Markov matrix) has two key properties: 1. All elements are non-negative (= 0) 2. The sum of each row equals 1

These matrices are used to represent transition probabilities in Markov chains, where:

  • Each row represents a current state
  • Each column represents a possible next state
  • Each element represents the probability of transitioning from one state to another

Stochastic matrices are fundamental in:

  • Markov processes
  • PageRank algorithm (used by Google)
  • Natural language processing
  • Reinforcement learning

IsSymmetricMatrix<T>(Matrix<T>)

Determines if a matrix is symmetric (equal to its transpose).

public static bool IsSymmetricMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is symmetric; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A symmetric matrix is like a mirror image across its diagonal. For any position (i,j) in the matrix, the value is the same as at position (j,i). For example, if the value at row 2, column 3 is 5, then the value at row 3, column 2 must also be 5.

IsToeplitzMatrix<T>(Matrix<T>)

Determines if a matrix is a Toeplitz matrix.

public static bool IsToeplitzMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Toeplitz matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Toeplitz matrix is a matrix where each descending diagonal from left to right contains the same value. In other words, the value at position (i,j) depends only on the difference i-j.

Toeplitz matrices are common in signal processing and solving differential equations. They have special properties that make certain calculations more efficient.

IsTridiagonalMatrix<T>(Matrix<T>)

Determines if a matrix is a tridiagonal matrix.

public static bool IsTridiagonalMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is tridiagonal; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A tridiagonal matrix is a square matrix where all elements are zero except for the main diagonal and the diagonals immediately above and below it. For example: [a b 0] [c d e] [0 f g]

IsUnitaryMatrix<T>(Matrix<Complex<T>>, IMatrixDecomposition<Complex<T>>)

Determines if a matrix is unitary by checking if its conjugate transpose equals its inverse.

public static bool IsUnitaryMatrix<T>(this Matrix<Complex<T>> matrix, IMatrixDecomposition<Complex<T>> matrixDecomposition)

Parameters

matrix Matrix<Complex<T>>

The matrix to check.

matrixDecomposition IMatrixDecomposition<Complex<T>>

The decomposition method used to calculate the inverse.

Returns

bool

True if the matrix is unitary; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A unitary matrix has the special property that its inverse equals its conjugate transpose. This method checks if a matrix is unitary by comparing these two values. Unitary matrices are important in quantum computing and many AI algorithms because they preserve the "length" of vectors they operate on.

IsUpperBidiagonalMatrix<T>(Matrix<T>)

Determines if a matrix is upper bidiagonal (non-zero elements only on main diagonal and first superdiagonal).

public static bool IsUpperBidiagonalMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is upper bidiagonal; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An upper bidiagonal matrix has non-zero values only on the main diagonal and the diagonal immediately above it (called the superdiagonal). All other elements must be zero. For example: [4 7 0] [0 2 5] [0 0 9] This is an upper bidiagonal matrix because values appear only on the main diagonal and the diagonal above it.

IsUpperTriangularMatrix<T>(Matrix<T>, T?)

Determines if a matrix is upper triangular (all elements below the main diagonal are zero).

public static bool IsUpperTriangularMatrix<T>(this Matrix<T> matrix, T? tolerance = default)

Parameters

matrix Matrix<T>

The matrix to check.

tolerance T

Optional tolerance value for floating-point comparisons. Default is 1e-10.

Returns

bool

True if the matrix is upper triangular, otherwise false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: An upper triangular matrix has all its non-zero values either on or above the main diagonal (the diagonal line from top-left to bottom-right). Everything below this diagonal is zero. This is useful in many mathematical operations because it simplifies calculations.

The tolerance parameter helps when working with decimal numbers that might have tiny rounding errors.

IsVandermondeMatrix<T>(Matrix<T>)

Determines if a matrix is a Vandermonde matrix.

public static bool IsVandermondeMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if the matrix is a Vandermonde matrix; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A Vandermonde matrix is a special matrix where: 1. The first column can contain any values 2. Each subsequent column is formed by raising the corresponding element in the first column to a power

For example, if the first column is [x1, x2, x3], the Vandermonde matrix would be: [x1°, x1¹, x1², ...] [x2°, x2¹, x2², ...] [x3°, x3¹, x3², ...]

These matrices are important in polynomial interpolation and solving systems of linear equations.

IsZeroMatrix<T>(Matrix<T>)

Determines if a matrix contains only zero values.

public static bool IsZeroMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to check.

Returns

bool

True if all elements in the matrix are zero; otherwise, false.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method checks if every single value in the matrix is zero. It's like checking if a grid of numbers contains only zeros.

KroneckerProduct<T>(Matrix<T>, Matrix<T>)

Computes the Kronecker product of two matrices.

public static Matrix<T> KroneckerProduct<T>(this Matrix<T> a, Matrix<T> b)

Parameters

a Matrix<T>

The first matrix.

b Matrix<T>

The second matrix.

Returns

Matrix<T>

The Kronecker product of the two matrices.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The Kronecker product is a special way of combining two matrices that results in a much larger matrix. If matrix A is m×n and matrix B is p×q, their Kronecker product will be a matrix of size (m×p)×(n×q).

Think of it as replacing each element of matrix A with a scaled copy of matrix B, where the scaling factor is the value of the element in A. This operation is useful in various fields including quantum computing, image processing, and when working with certain types of mathematical models.

LogDeterminant<T>(Matrix<T>)

Calculates the logarithm of the determinant of a matrix.

public static T LogDeterminant<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to calculate the log determinant for.

Returns

T

The logarithm of the determinant value.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The determinant is a special number calculated from a square matrix. For very large or very small determinant values, calculating the logarithm of the determinant helps avoid numerical overflow or underflow issues. This method uses LU decomposition (a way of factoring matrices) to calculate the log determinant more efficiently and accurately.

This is particularly useful in statistical applications like calculating multivariate normal distribution probabilities.

Exceptions

ArgumentException

Thrown when the matrix is not square.

Max<T>(Matrix<T>, Func<T, T>)

Finds the maximum value in the matrix after applying a transformation function to each element.

public static T Max<T>(this Matrix<T> matrix, Func<T, T> selector)

Parameters

matrix Matrix<T>

The matrix to search for the maximum value.

selector Func<T, T>

A function to transform each element before comparison.

Returns

T

The maximum value after applying the transformation function.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method helps you find the largest value in your matrix, but with a twist. Before comparing values, it applies a function (the selector) to each element. For example, if you want to find the element with the largest absolute value, you could use a selector that calculates the absolute value of each element.

The selector function takes an element of type T and returns a transformed value of type T.

Negate<T>(Matrix<T>)

Creates a new matrix with all elements negated.

public static Matrix<T> Negate<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to negate.

Returns

Matrix<T>

A new matrix with all elements negated.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method creates a new matrix where each value is the negative of the original. For example, if an element in the original matrix is 5, it will be -5 in the new matrix.

Nullspace<T>(Matrix<T>, T)

Computes the null space (kernel) of a matrix.

public static Matrix<T> Nullspace<T>(this Matrix<T> matrix, T threshold)

Parameters

matrix Matrix<T>

The matrix to analyze.

threshold T

The threshold value for determining when a value is considered zero.

Returns

Matrix<T>

A matrix whose columns form a basis for the null space of the input matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: The null space of a matrix is the set of all vectors that, when multiplied by the matrix, give zero. These vectors represent "hidden patterns" or "invisible dimensions" in your data. Finding the null space helps identify what information your data cannot capture or distinguish between.

PointwiseMultiply<T>(Matrix<T>, Matrix<T>)

Performs element-by-element multiplication of two matrices of the same dimensions.

public static Matrix<T> PointwiseMultiply<T>(this Matrix<T> matrix, Matrix<T> other)

Parameters

matrix Matrix<T>

The first matrix for multiplication.

other Matrix<T>

The second matrix for multiplication.

Returns

Matrix<T>

A new matrix where each element is the product of the corresponding elements in the input matrices.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This is different from regular matrix multiplication. In pointwise multiplication (also called Hadamard product), each element in the result is calculated by multiplying the corresponding elements at the same position in both input matrices. Both matrices must have exactly the same number of rows and columns.

For example, if matrix[1,2] = 5 and other[1,2] = 3, then the result[1,2] will be 15.

Exceptions

ArgumentException

Thrown when the matrices have different dimensions.

PointwiseMultiply<T>(Matrix<T>, Vector<T>)

Multiplies each row of a matrix by the corresponding element in a vector.

public static Matrix<T> PointwiseMultiply<T>(this Matrix<T> matrix, Vector<T> vector)

Parameters

matrix Matrix<T>

The matrix whose rows will be scaled.

vector Vector<T>

The vector containing scaling factors for each row.

Returns

Matrix<T>

A new matrix with each row scaled by the corresponding vector element.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method scales each row of your matrix by a corresponding value from the vector. For example, if your vector is [2, 3, 4] and your matrix has 3 rows, then the first row will be multiplied by 2, the second row by 3, and the third row by 4. This is useful for applying different weights to each row of data.

Exceptions

ArgumentException

Thrown when the number of rows in the matrix doesn't match the vector length.

Reshape<T>(Matrix<T>, int, int)

Reorganizes the elements of a matrix into a new matrix with different dimensions while preserving all data.

public static Matrix<T> Reshape<T>(this Matrix<T> matrix, int newRows, int newColumns)

Parameters

matrix Matrix<T>

The matrix to reshape.

newRows int

The number of rows in the reshaped matrix.

newColumns int

The number of columns in the reshaped matrix.

Returns

Matrix<T>

A new matrix with the specified dimensions containing all elements from the original matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Reshaping a matrix means changing its dimensions (rows and columns) while keeping all the same values. It's like rearranging the same set of numbers into a different grid pattern.

For example, if you have a 2×3 matrix (2 rows, 3 columns): [1, 2, 3] [4, 5, 6]

You could reshape it to a 3×2 matrix (3 rows, 2 columns): [1, 2] [3, 4] [5, 6]

The total number of elements must stay the same (in this example, 6 elements). The method reads the original matrix row by row and fills the new matrix in the same way.

This is useful in data processing and machine learning when you need to transform data between different formats, such as converting between image representations or preparing data for specific algorithms.

Exceptions

ArgumentException

Thrown when the total number of elements in the new shape doesn't match the original matrix.

RowWiseArgmax<T>(Matrix<T>)

For each row in the matrix, finds the index of the column containing the maximum value.

public static Vector<T> RowWiseArgmax<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to analyze.

Returns

Vector<T>

A vector where each element is the index of the maximum value in the corresponding row.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method examines each row of your matrix and identifies which column contains the largest value. It returns a vector where each element corresponds to a row in your original matrix, and the value is the column index (position) of the maximum value in that row.

This is particularly useful in machine learning for finding the predicted class in classification problems, where each row might represent probabilities for different classes.

SetSubmatrix<T>(Matrix<T>, int, int, Matrix<T>)

Sets a submatrix within a larger matrix.

public static void SetSubmatrix<T>(this Matrix<T> matrix, int startRow, int startCol, Matrix<T> submatrix)

Parameters

matrix Matrix<T>

The target matrix to modify.

startRow int

The starting row index where the submatrix will be placed.

startCol int

The starting column index where the submatrix will be placed.

submatrix Matrix<T>

The smaller matrix to insert into the target matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Think of this like pasting a small picture into a specific location of a larger picture. The startRow and startCol parameters tell the method where to begin placing the smaller matrix.

Submatrix<T>(Matrix<T>, int, int, int, int)

Extracts a submatrix from the original matrix.

public static Matrix<T> Submatrix<T>(this Matrix<T> matrix, int startRow, int startCol, int numRows, int numCols)

Parameters

matrix Matrix<T>

The source matrix to extract from.

startRow int

The zero-based index of the first row to include.

startCol int

The zero-based index of the first column to include.

numRows int

The number of rows to extract.

numCols int

The number of columns to extract.

Returns

Matrix<T>

A new matrix containing the specified portion of the original matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method lets you extract a smaller matrix from within a larger one. Think of it like cropping a rectangular section from a spreadsheet. You specify where to start (startRow, startCol) and how many rows and columns to include (numRows, numCols).

For example, if you have a 5x5 matrix and call Submatrix(1, 2, 2, 2), you'll get a 2x2 matrix containing the elements from rows 1-2 and columns 2-3 of the original matrix.

Exceptions

ArgumentOutOfRangeException

Thrown when the specified submatrix dimensions extend beyond the bounds of the original matrix.

Submatrix<T>(Matrix<T>, int[])

Creates a submatrix from the given matrix using the specified indices.

public static Matrix<T> Submatrix<T>(this Matrix<T> matrix, int[] indices)

Parameters

matrix Matrix<T>

The source matrix.

indices int[]

The indices of rows to include in the submatrix.

Returns

Matrix<T>

A new matrix containing only the specified rows.

Type Parameters

T

The type of elements in the matrix.

SumColumns<T>(Matrix<T>)

Calculates the sum of each column in the matrix.

public static Vector<T> SumColumns<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix whose columns will be summed.

Returns

Vector<T>

A vector containing the sum of each column.

Type Parameters

T

The numeric data type of the matrix elements.

Remarks

For Beginners: This method adds up all the values in each column of your matrix. For example, if your matrix represents multiple data points with features in columns, this would give you the total of each feature across all data points.

SwapRows<T>(Matrix<T>, int, int)

Swaps two rows in a matrix.

public static void SwapRows<T>(this Matrix<T> matrix, int row1Index, int row2Index)

Parameters

matrix Matrix<T>

The matrix to modify.

row1Index int

The index of the first row to swap.

row2Index int

The index of the second row to swap.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method exchanges the positions of two rows in a matrix. It's like swapping two rows in a spreadsheet - all values in those rows change places with each other.

ToComplexMatrix<T>(Matrix<T>)

Converts a real-valued matrix to a complex-valued matrix.

public static Matrix<Complex<T>> ToComplexMatrix<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The real-valued matrix to convert.

Returns

Matrix<Complex<T>>

A complex-valued matrix where each element has the original value as its real part and zero as its imaginary part.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: A complex number has two parts: a real part and an imaginary part. This method takes a matrix of real numbers and creates a new matrix where each element is a complex number with the original value as the real part and zero as the imaginary part. This is useful when you need to perform operations that require complex numbers.

ToComplexVector<T>(Vector<T>)

Converts a real-valued vector to a complex-valued vector.

public static Vector<Complex<T>> ToComplexVector<T>(this Vector<T> vector)

Parameters

vector Vector<T>

The real-valued vector to convert.

Returns

Vector<Complex<T>>

A complex-valued vector where each element has the original value as its real part and zero as its imaginary part.

Type Parameters

T

The numeric type of the vector elements.

Remarks

For Beginners: Similar to converting a matrix to complex form, this method takes a vector of real numbers and creates a new vector where each element is a complex number with the original value as the real part and zero as the imaginary part. This is useful when you need to perform operations that require complex numbers.

ToRealMatrix<T>(Matrix<Complex<T>>)

Extracts the real part of a complex-valued matrix to create a real-valued matrix.

public static Matrix<T> ToRealMatrix<T>(this Matrix<Complex<T>> matrix)

Parameters

matrix Matrix<Complex<T>>

The complex-valued matrix.

Returns

Matrix<T>

A real-valued matrix containing only the real parts of the complex matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: This method takes a matrix of complex numbers and creates a new matrix containing only the real parts of those complex numbers. The imaginary parts are discarded. This is useful when you've performed calculations with complex numbers but only need the real results.

ToVector<T>(Matrix<T>)

Converts a matrix to a vector by flattening its elements in row-major order.

public static Vector<T> ToVector<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to convert to a vector.

Returns

Vector<T>

A vector containing all elements of the matrix in row-major order.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

This method flattens a matrix into a vector by concatenating all rows in order. The resulting vector has a length equal to rows * columns of the original matrix.

For Beginners: This is like taking a grid of numbers and writing them out in a single line, going from left to right, top to bottom. For example, a 2x3 matrix: [1, 2, 3] [4, 5, 6] becomes the vector: [1, 2, 3, 4, 5, 6]

Transpose<T>(Matrix<T>)

Transposes a matrix by swapping its rows and columns.

public static Matrix<T> Transpose<T>(this Matrix<T> matrix)

Parameters

matrix Matrix<T>

The matrix to transpose.

Returns

Matrix<T>

A new matrix that is the transpose of the original matrix.

Type Parameters

T

The numeric type of the matrix elements.

Remarks

For Beginners: Transposing a matrix means converting its rows into columns and columns into rows. For example, if you have a matrix with 3 rows and 2 columns, the transposed matrix will have 2 rows and 3 columns. It's like flipping the matrix along its diagonal.

Exceptions

ArgumentNullException

Thrown when the matrix is null.

ArgumentException

Thrown when the matrix has zero rows or columns.