Class MatrixExtensions
A matrix extensions.
Inheritance
Inherited Members
Namespace:numl.Math.LinearAlgebra
Assembly:numl.dll
Syntax
public static class MatrixExtensions
Methods
View SourceCholesky(Matrix)
A Matrix extension method that choleskies the given m.
Declaration
public static Matrix Cholesky(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Correlation(Matrix, VectorType)
A Matrix extension method that correlations.
Declaration
public static Matrix Correlation(this Matrix source, VectorType t = VectorType.Col)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
VectorType | t | (Optional) Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Covariance(Matrix, VectorType)
A Matrix extension method that covariances.
Declaration
public static Matrix Covariance(this Matrix source, VectorType t = VectorType.Col)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
VectorType | t | (Optional) Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
CovarianceDiag(Matrix, VectorType)
A Matrix extension method that covariance diagram.
Declaration
public static Vector CovarianceDiag(this Matrix source, VectorType t = VectorType.Col)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
VectorType | t | (Optional) Row or Column sum. |
Returns
Type | Description |
---|---|
Vector | A Vector. |
Det(Matrix)
computes matrix determinant NOTE: currently using cholesky factorization to save time so non symmetric positive semi-definite matrices will cause problems...
Declaration
public static double Det(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Double | Determinant. |
Diag(Matrix)
A Matrix extension method that diagrams the given m.
Declaration
public static Vector Diag(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
Vector | A Vector. |
Each(Matrix, Matrix, Func<Double, Double, Double>)
Performs an element-wise operation on the input Matrices.
Declaration
public static Matrix Each(this Matrix m1, Matrix m2, Func<double, double, double> fnElementWiseOp)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m1 | First Matrix. |
Matrix | m2 | Second Matrix. |
System.Func<System.Double, System.Double, System.Double> | fnElementWiseOp | Operation to perform on the value from the first and second matrices. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Each(Matrix, Func<Double, Double>)
Performs an element-wise operation on the input Matrix.
Declaration
public static Matrix Each(this Matrix m, Func<double, double> fnElementWiseOp)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Func<System.Double, System.Double> | fnElementWiseOp | Function to apply. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Each(Matrix, Func<Double, Int32, Int32, Double>)
Performs an element wise operation on the input Matrix.
Declaration
public static Matrix Each(this Matrix m, Func<double, int, int, double> fnElementWiseOp)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Func<System.Double, System.Int32, System.Int32, System.Double> | fnElementWiseOp | Function to apply to each cell specified by the value and cell coordinates. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Eigs(Matrix)
A Matrix extension method that eigs the given m.
Declaration
public static Tuple<Vector, Matrix> Eigs(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Tuple<Vector, Matrix> | A Tuple<Vector,Matrix> |
Extract(Matrix, Int32, Int32, Int32, Int32, Boolean)
A Matrix extension method that extracts this object.
Declaration
public static Matrix Extract(this Matrix m, int x, int y, int width, int height, bool safe = true)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Int32 | x | The x coordinate. |
System.Int32 | y | The y coordinate. |
System.Int32 | width | The width. |
System.Int32 | height | The height. |
System.Boolean | safe | (Optional) true to safe. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Indices(Matrix, Func<Vector, Boolean>)
Enumerates indices in this collection.
Declaration
public static IEnumerable<int> Indices(this Matrix source, Func<Vector, bool> f)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
System.Func<Vector, System.Boolean> | f | The Func<Vector,bool> to process. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Int32> | An enumerator that allows foreach to be used to process indices in this collection. |
Indices(Matrix, Func<Vector, Boolean>, VectorType)
Enumerates indices in this collection.
Declaration
public static IEnumerable<int> Indices(this Matrix source, Func<Vector, bool> f, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
System.Func<Vector, System.Boolean> | f | The Func<Vector,bool> to process. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Int32> | An enumerator that allows foreach to be used to process indices in this collection. |
Max(Matrix)
A Matrix extension method that determines the maximum of the given parameters.
Declaration
public static double Max(this Matrix source)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
Returns
Type | Description |
---|---|
System.Double | The maximum value. |
Max(Matrix, VectorType)
Returns a vector of the maximum values for each row/column.
Declaration
public static Vector Max(this Matrix source, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | |
VectorType | t |
Returns
Type | Description |
---|---|
Vector |
Mean(Matrix, VectorType)
A Matrix extension method that determines the mean of the given parameters.
Declaration
public static Vector Mean(this Matrix source, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Vector | The mean value. |
Median(Matrix, VectorType)
Returns a vector of the median values for each row or column.
Declaration
public static Vector Median(this Matrix source, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | |
VectorType | t |
Returns
Type | Description |
---|---|
Vector |
Min(Matrix)
A Matrix extension method that determines the minimum of the given parameters.
Declaration
public static double Min(this Matrix source)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
Returns
Type | Description |
---|---|
System.Double | The minimum value. |
Min(Matrix, VectorType)
Returns a vector of the minimum values for each row/column.
Declaration
public static Vector Min(this Matrix source, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | |
VectorType | t |
Returns
Type | Description |
---|---|
Vector |
Norm(Matrix)
A Matrix extension method that normals.
Declaration
public static double Norm(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Double | A double. |
Norm(Matrix, Double)
A Matrix extension method that normals.
Declaration
public static double Norm(this Matrix m, double p)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Double | p | The double to process. |
Returns
Type | Description |
---|---|
System.Double | A double. |
Reverse(Matrix, VectorType)
Enumerates reverse in this collection.
Declaration
public static IEnumerable<Vector> Reverse(this Matrix source, VectorType t = VectorType.Row)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | The source to act on. |
VectorType | t | (Optional) Row or Column sum. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<Vector> | An enumerator that allows foreach to be used to process reverse in this collection. |
Round(Matrix, Int32)
A Matrix extension method that rounds.
Declaration
public static Matrix Round(this Matrix m, int decimals = 0)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Int32 | decimals | (Optional) the decimals. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Slice(Matrix, IEnumerable<IEnumerable<Int32>>, VectorType)
Slices the Matrix returning only the cells specified by their location.
Declaration
public static Matrix Slice(this Matrix m, IEnumerable<IEnumerable<int>> indices, VectorType t = VectorType.Row)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<System.Int32>> | indices | An 2-D location array of indexes to include. |
VectorType | t | Type of the first dimension in the location array. |
Returns
Type | Description |
---|---|
Matrix |
Slice(Matrix, IEnumerable<Int32>)
A Matrix extension method that slices.
Declaration
public static Matrix Slice(this Matrix m, IEnumerable<int> indices)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Collections.Generic.IEnumerable<System.Int32> | indices | The indices. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Slice(Matrix, IEnumerable<Int32>, VectorType)
A Matrix extension method that slices.
Declaration
public static Matrix Slice(this Matrix m, IEnumerable<int> indices, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Collections.Generic.IEnumerable<System.Int32> | indices | The indices. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Slice(Matrix, Int32, Int32, VectorType)
Slices the input matrix using starting and stopping positions.
Declaration
public static Matrix Slice(this Matrix m, int minIndex, int maxIndex, VectorType t = VectorType.Row)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Source matrix. |
System.Int32 | minIndex | Minimum index to slice from. |
System.Int32 | maxIndex | Maximum index to slice. |
VectorType | t |
Returns
Type | Description |
---|---|
Matrix |
Sort(Matrix, Func<Vector, Double>, VectorType, Boolean)
Sorts the given Matrix by the specified row or column and returns the new Matrix.
Declaration
public static Matrix Sort(this Matrix m, Func<Vector, double> keySelector, VectorType t, bool isAscending = true)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | The Matrix |
System.Func<Vector, System.Double> | keySelector | Property selector to sort by. |
VectorType | t | Specifies whether to sort horizontally (Col) or vertically (Row). |
System.Boolean | isAscending | Determines whether to sort ascending or descending (Default: True) |
Returns
Type | Description |
---|---|
Matrix | New Matrix and Vector of original indices. |
Sort(Matrix, Func<Vector, Double>, VectorType, Boolean, out Vector)
Sorts the given Matrix by the specified row or column index and returns the new Matrix along with the original indices.
Declaration
public static Matrix Sort(this Matrix m, Func<Vector, double> keySelector, VectorType t, bool isAscending, out Vector indices)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | The Matrix |
System.Func<Vector, System.Double> | keySelector | Property selector to sort by. |
VectorType | t | Specifies whether to sort horizontally (Col) or vertically (Row). |
System.Boolean | isAscending | Determines whether to sort ascending or descending (Default: True) |
Vector | indices | Vector of the original (t) indices before the sort operation. |
Returns
Type | Description |
---|---|
Matrix | New Matrix and Vector of original indices. |
Stack(Matrix, Matrix)
A Matrix extension method that stacks.
Declaration
public static Matrix Stack(this Matrix m, Matrix t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Matrix | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |
Stats(Matrix)
A Matrix extension method that statistics.
Declaration
public static Matrix[] Stats(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
Matrix[] | A Matrix[]. |
Stats(Matrix, VectorType)
A Matrix extension method that statistics.
Declaration
public static Matrix[] Stats(this Matrix m, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix[] | A Matrix[]. |
StdDev(Matrix, VectorType)
Computes the standard deviation of the given matrix
Declaration
public static Vector StdDev(this Matrix source, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | source | |
VectorType | t | Return a Row or Column vector |
Returns
Type | Description |
---|---|
Vector |
Sum(Matrix)
Computes the entire sum of every element in the Matrix.
Declaration
public static double Sum(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Double | Double. |
Sum(Matrix, VectorType)
Computes the sum of either the rows or columns of a matrix and returns a vector.
Declaration
public static Vector Sum(this Matrix m, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Vector | Vector Sum. |
Sum(Matrix, Int32, VectorType)
Computes the sum of either the rows or columns of a matrix and returns a vector.
Declaration
public static double Sum(Matrix m, int i, VectorType t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Int32 | i | Zero-based index of the. |
VectorType | t | Row or Column sum. |
Returns
Type | Description |
---|---|
System.Double | Vector Sum. |
SVD(Matrix)
A Matrix extension method that svds the given m.
Declaration
public static Tuple<Matrix, Vector, Matrix> SVD(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Tuple<Matrix, Vector, Matrix> | A Tuple<Matrix,Vector,Matrix> |
ToBinary(Matrix, Func<Double, Boolean>, Double, Double)
Converts a matrix to a binary indicator matrix based on the specified predicate.
Declaration
public static Matrix ToBinary(this Matrix m, Func<double, bool> fnPredicate, double trueValue = 1, double falseValue = 0)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
System.Func<System.Double, System.Boolean> | fnPredicate | Function to test each Value. |
System.Double | trueValue | (Optional) Value to set when condition (fnPredicate) returns True |
System.Double | falseValue | (Optional) Value to set when condition (fnPredicate) returns False |
Returns
Type | Description |
---|---|
Matrix |
Trace(Matrix)
Computes the trace of a matrix.
Declaration
public static double Trace(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Returns
Type | Description |
---|---|
System.Double | trace. |
Unshape(Matrix)
Unrolls the matrix into an n x 1 vector.
Declaration
public static Vector Unshape(this Matrix m)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m |
Returns
Type | Description |
---|---|
Vector |
VStack(Matrix, Matrix)
A Matrix extension method that stacks.
Declaration
public static Matrix VStack(this Matrix m, Matrix t)
Parameters
Type | Name | Description |
---|---|---|
Matrix | m | Matrix. |
Matrix | t | Row or Column sum. |
Returns
Type | Description |
---|---|
Matrix | A Matrix. |