Class Matrix
Two-dimensional container of doubles.
Implements
Inherited Members
Namespace: Microsoft.ML.Probabilistic.Math
Assembly: Microsoft.ML.Probabilistic.dll
Syntax
[Serializable]
[DataContract]
public class Matrix : IList<double>, ICollection<double>, IReadOnlyList<double>, IReadOnlyCollection<double>, IEnumerable<double>, IEnumerable, ICloneable, SettableTo<Matrix>, CanSetAllElementsTo<double>
Constructors
Matrix()
Default constructor just used for serialization.
Declaration
protected Matrix()
Matrix(Matrix)
Copy constructor.
Declaration
public Matrix(Matrix that)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that |
Matrix(Double[,])
Construct a matrix from data in a 2D array.
Declaration
[Construction(new string[]{"ToArray"})]
public Matrix(double[, ] data)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[,] | data | 2D array of elements. |
Remarks
The 2D array is copied into new storage. The size of the matrix is taken from the array.
Matrix(Int32, Int32)
Construct a zero matrix of the given dimensions.
Declaration
public Matrix(int rows, int cols)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | rows | Number of rows >= 0. |
| Int32 | cols | Number of columns >= 0. |
Matrix(Int32, Int32, Double[])
Construct a matrix by referencing an array.
Declaration
public Matrix(int nRows, int nCols, double[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | nRows | Number of rows. |
| Int32 | nCols | Number of columns. |
| Double[] | data | Storage for the matrix elements. |
Fields
cols
Declaration
[DataMember]
protected readonly int cols
Field Value
| Type | Description |
|---|---|
| Int32 |
data
Declaration
[DataMember]
protected double[] data
Field Value
| Type | Description |
|---|---|
| Double[] |
rows
Declaration
[DataMember]
protected readonly int rows
Field Value
| Type | Description |
|---|---|
| Int32 |
Properties
Cols
The number of columns of the matrix.
Declaration
public int Cols { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Count
Number of elements in the matrix
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Item[Int32]
Gets and sets an element.
Declaration
public double this[int index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index |
Property Value
| Type | Description |
|---|---|
| Double |
Item[Int32, Int32]
Gets and sets an element.
Declaration
public double this[int row, int col] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | row | |
| Int32 | col |
Property Value
| Type | Description |
|---|---|
| Double |
Rows
The number of rows of the matrix.
Declaration
public int Rows { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
SourceArray
Gets/sets the matrix's source array
Declaration
[IgnoreDataMember]
public double[] SourceArray { get; set; }
Property Value
| Type | Description |
|---|---|
| Double[] |
Methods
CheckCompatible(Matrix, String)
Checks that a given matrix is the same size as this matrix. Throws an exception if not with the given string
Declaration
protected void CheckCompatible(Matrix that, string paramName)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | The matrix to check |
| String | paramName |
CheckSymmetry(String)
Checks this matrix for symmetry
Declaration
[Conditional("DEBUG")]
public void CheckSymmetry(string paramName)
Parameters
| Type | Name | Description |
|---|---|---|
| String | paramName |
Clone()
Fully clones this matrix
Declaration
public virtual object Clone()
Returns
| Type | Description |
|---|---|
| Object |
Commutation(Int32, Int32)
Magnus and Neudecker's commutation matrix for a given size matrix
Declaration
public static Matrix Commutation(int rows, int cols)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | rows | |
| Int32 | cols |
Returns
| Type | Description |
|---|---|
| Matrix | The commutation matrix of size rowscols by rowscols |
Contains(Double)
Returns true if this matrix contains the given value
Declaration
public bool Contains(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item |
Returns
| Type | Description |
|---|---|
| Boolean |
CopyTo(Double[], Int32)
Copies the values in this matrix to an array starting at a given index in the destination array
Declaration
public void CopyTo(double[] array, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | array | |
| Int32 | index |
CopyTo(Double[,])
Copies matrix values to a two-dimensional array.
Declaration
public void CopyTo(double[, ] values)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[,] | values | An array whose |
Determinant()
Determinant of a square matrix
Declaration
public double Determinant()
Returns
| Type | Description |
|---|---|
| Double | The determinant |
Diagonal()
Creates a vector from the diagonal values in the matrix
Declaration
public Vector Diagonal()
Returns
| Type | Description |
|---|---|
| Vector |
DiagonalOfProduct(Matrix, Matrix)
The diagonal elements of a matrix product.
Declaration
public static Vector DiagonalOfProduct(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | A matrix of size n by m. |
| Matrix | b | A matrix of size m by n. |
Returns
| Type | Description |
|---|---|
| Vector |
|
EigenvaluesInPlace(Double[], Double[])
Compute the eigenvalues of a square matrix, destroying the contents of the matrix.
Declaration
public void EigenvaluesInPlace(double[] eigenvaluesReal, double[] eigenvaluesImag)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | eigenvaluesReal | On output, the real part of the eigenvalues. |
| Double[] | eigenvaluesImag | On output, the imaginary part of the eigenvalues. |
EnumerateDiagonal()
Enumerator which yields the diagonal elements of the matrix
Declaration
public IEnumerable<double> EnumerateDiagonal()
Returns
| Type | Description |
|---|---|
| IEnumerable<Double> |
Equals(Object)
Object comparator.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | obj | An object - must be an IntMatrix. |
Returns
| Type | Description |
|---|---|
| Boolean | True if objects are equal. |
Overrides
EqualsAll(Double)
Tests if all elements are equal to a given value.
Declaration
public bool EqualsAll(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value | The value to test against. |
Returns
| Type | Description |
|---|---|
| Boolean | True if all elements are equal to |
FromArray(Double[,])
Construct a matrix from data in a 2D array.
Declaration
public static Matrix FromArray(double[, ] data)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[,] | data | 2D array of elements. |
Returns
| Type | Description |
|---|---|
| Matrix |
Remarks
The 2D array is copied into new storage. The size of the matrix is taken from the array.
FromDiagonal(Vector)
Creates a new diagonal matrix with diagonal values specified in the given vector
Declaration
public static Matrix FromDiagonal(Vector diag)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | diag |
Returns
| Type | Description |
|---|---|
| Matrix |
GetColumn(Int32)
Returns the specified column as an array of doubles
Declaration
public double[] GetColumn(int col)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | col |
Returns
| Type | Description |
|---|---|
| Double[] |
GetEnumerator()
Gets a typed enumerator for this matrix
Declaration
public IEnumerator<double> GetEnumerator()
Returns
| Type | Description |
|---|---|
| IEnumerator<Double> |
GetHashCode()
Hash code generator.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 | The hash code for the instance. |
Overrides
GetRow(Int32)
Returns the specified row as an array of doubles
Declaration
public double[] GetRow(int row)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | row |
Returns
| Type | Description |
|---|---|
| Double[] |
Identity(Int32)
Creates an identity matrix of the specified size
Declaration
public static Matrix Identity(int dimension)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dimension |
Returns
| Type | Description |
|---|---|
| Matrix |
IdentityScaledBy(Int32, Double)
Creates an identity matrix of the specified size, scaled by the specified value
Declaration
public static Matrix IdentityScaledBy(int dimension, double scale)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dimension | |
| Double | scale |
Returns
| Type | Description |
|---|---|
| Matrix |
IndexOf(Double)
Returns the first index of the given item if it exists in the matrix, otherwise returns -1
Declaration
public int IndexOf(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item |
Returns
| Type | Description |
|---|---|
| Int32 |
Inner(Matrix)
Inner product of matrices.
Declaration
public double Inner(Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | A matrix with the same size as this. |
Returns
| Type | Description |
|---|---|
| Double | sum_ij A[i,j]*this[i,j]. |
Inverse()
Return the inverse of this matrix - not implemented yet
Declaration
public Matrix Inverse()
Returns
| Type | Description |
|---|---|
| Matrix |
IsPositiveDefinite()
Tests for positive-definiteness.
Declaration
public bool IsPositiveDefinite()
Returns
| Type | Description |
|---|---|
| Boolean | True if the matrix is positive-definite, i.e. all eigenvalues > 0. |
Kronecker(Matrix)
Kronecker product with another matrix.
Declaration
public Matrix Kronecker(Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A |
Returns
| Type | Description |
|---|---|
| Matrix | The Kronecker product |
MaxDiff(Matrix)
Returns the maximum absolute difference between matrix elements.
Declaration
public double MaxDiff(Matrix that)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | The second matrix. |
Returns
| Type | Description |
|---|---|
| Double | max(abs(this[i,j] - that[i,j])). |
Remarks
This routine is typically used instead of Equals, since Equals is susceptible to roundoff errors.
MaxDiff(Matrix, Double)
Returns the maximum relative difference between matrix elements.
Declaration
public double MaxDiff(Matrix that, double rel)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | The second matrix. |
| Double | rel | An offset to avoid division by zero. |
Returns
| Type | Description |
|---|---|
| Double | max(abs(this[i,j] - that[i,j])/(abs(this[i,j]) + rel)). |
Remarks
This routine is typically used instead of Equals, since Equals is susceptible to roundoff errors.
Median()
Returns the median of the values in the matrix
Declaration
public double Median()
Returns
| Type | Description |
|---|---|
| Double |
Outer()
Returns the product of this matrix and its transpose
Declaration
public PositiveDefiniteMatrix Outer()
Returns
| Type | Description |
|---|---|
| PositiveDefiniteMatrix |
OuterTranspose()
Returns the transpose of this matrix times this matrix
Declaration
public PositiveDefiniteMatrix OuterTranspose()
Returns
| Type | Description |
|---|---|
| PositiveDefiniteMatrix |
Parse(String)
Parse a string (in the format produced by ToString) to recover a Matrix
Declaration
public static Matrix Parse(string s)
Parameters
| Type | Name | Description |
|---|---|---|
| String | s | The string to parse |
Returns
| Type | Description |
|---|---|
| Matrix | A matrix |
PredivideBy(LowerTriangularMatrix)
Gets the solution to AX=B, where A is a lower triangular matrix. Equivalent to the left-division X = A\B.
Declaration
public Matrix PredivideBy(LowerTriangularMatrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| LowerTriangularMatrix | A | A lower triangular matrix with A.Rows == this.Rows. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this is used as the right-hand side matrix B, and it also
receives the solution.
Throws an exception if A is singular.
PredivideBy(PositiveDefiniteMatrix)
Premultiply this matrix by the inverse of the given positive definite matrix
Declaration
public Matrix PredivideBy(PositiveDefiniteMatrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| PositiveDefiniteMatrix | A |
Returns
| Type | Description |
|---|---|
| Matrix |
PredivideBy(UpperTriangularMatrix)
Gets the solution to AX=B, where A is an upper triangular matrix and B is a matrix of right-hand sides. It is equivalent to the left-division X = A\B.
Declaration
public Matrix PredivideBy(UpperTriangularMatrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| UpperTriangularMatrix | A | An upper triangular matrix with A.Rows == this.Rows. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this is used as the right-hand side matrix B, and it also
receives the solution.
Throws an exception if A is singular.
PredivideByTranspose(LowerTriangularMatrix)
Gets the solution to A'X=B, where A is a lower triangular matrix. Equivalent to the left-division X = A'\B.
Declaration
public Matrix PredivideByTranspose(LowerTriangularMatrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| LowerTriangularMatrix | A | A lower triangular matrix with A.Cols == this.Rows. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this is used as the right-hand side matrix B, and it also
receives the solution.
Throws an exception if A is singular.
QuadraticForm(Vector)
Evaluates the product x'Ax (where ' is transposition).
Declaration
public double QuadraticForm(Vector x)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | x | A vector whose length equals Rows. |
Returns
| Type | Description |
|---|---|
| Double | The above product. |
Remarks
this must be a square matrix with Rows == x.Count.
QuadraticForm(Vector, Vector)
Evaluates the product x'Ay (where ' is transposition).
Declaration
public double QuadraticForm(Vector x, Vector y)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | x | A vector whose length equals Rows. |
| Vector | y | A vector whose length equals Cows. |
Returns
| Type | Description |
|---|---|
| Double | The above product. |
RowVector(Int32)
A row of the matrix.
Declaration
public Vector RowVector(int row)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | row | An integer in [0,Rows-1]. |
Returns
| Type | Description |
|---|---|
| Vector | If colStride == 1, then a Vector object pointing at one row of this matrix. Otherwise a copy of the row. |
Scale(Double)
Multiplies this matrix by a scalar.
Declaration
public Matrix Scale(double scale)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | scale | The scalar. |
Returns
| Type | Description |
|---|---|
| Matrix | this |
Remarks
this receives the product. This method is a synonym for SetToProduct(this, scale)
ScaleCols(Vector)
Multiplies each column of this matrix by a different scalar.
Declaration
public Matrix ScaleCols(Vector colScale)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | colScale | The ith element scales column i. |
Returns
| Type | Description |
|---|---|
| Matrix | this |
Remarks
this receives the product.
ScaleRows(Vector)
Multiplies each row of this matrix by a different scalar.
Declaration
public Matrix ScaleRows(Vector rowScale)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | rowScale | The ith element scales row i. |
Returns
| Type | Description |
|---|---|
| Matrix | this |
Remarks
this receives the product.
SetAllElementsTo(Double)
Sets all elements to a given value.
Declaration
public void SetAllElementsTo(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | value | The new value. |
SetDiagonal(Vector)
Sets the diagonal elements to the values specified in the given vector
Declaration
public Matrix SetDiagonal(Vector diag)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | diag |
Returns
| Type | Description |
|---|---|
| Matrix |
SetSubmatrix(Int32, Int32, Matrix)
Set a submatrix of this matrix to match another matrix.
Declaration
public void SetSubmatrix(int firstRow, int firstColumn, Matrix that)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | firstRow | Index of the first row in |
| Int32 | firstColumn | Index of the first column in |
| Matrix | that | Size is at most |
SetSubmatrix(Int32, Int32, Matrix, Int32, Int32, Int32, Int32)
Sets a submatrix of this matrix to match another matrix.
Declaration
public void SetSubmatrix(int thisRow, int thisColumn, Matrix that, int thatRow, int thatColumn, int numRows, int numColumns)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | thisRow | Index of the first row in |
| Int32 | thisColumn | Index of the first column in |
| Matrix | that | Size is at most |
| Int32 | thatRow | Index of the first row in |
| Int32 | thatColumn | Index of the first column in |
| Int32 | numRows | The number of rows to copy |
| Int32 | numColumns | The number of columns to copy |
SetTo(Matrix)
Copies values from another matrix.
Declaration
public void SetTo(Matrix that)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | The second matrix, which must have the same size as this. |
SetTo(Double[])
Copies values from an array.
Declaration
public void SetTo(double[] values)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | values | An array whose length is at least |
SetTo(Double[], Int32)
Copies values from an array.
Declaration
public void SetTo(double[] values, int startIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | values | An array whose length is at least |
| Int32 | startIndex | The index of the first value in |
SetTo(Double[,])
Copies values from a two-dimensional array.
Declaration
public void SetTo(double[, ] values)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[,] | values | An array whose |
SetToDiagonal(Vector)
Sets this matrix to a diagonal matrix with diagonal values specified in the given vector
Declaration
public Matrix SetToDiagonal(Vector diag)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | diag |
Returns
| Type | Description |
|---|---|
| Matrix |
SetToDifference(Matrix, Matrix)
Sets this matrix to the difference of two matrices. Assumes compatible matrices.
Declaration
public Matrix SetToDifference(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix, which must have the same size as |
| Matrix | b | Second matrix, which must have the same size as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this receives the difference, and must already be the correct size.
a and/or b may be the same object as this.
If this and a/b occupy distinct yet overlapping portions of the same source array, the results are undefined.
SetToDifference(Matrix, Double, Matrix)
Sets this matrix to a - bScale*b
Declaration
public Matrix SetToDifference(Matrix a, double bScale, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | |
| Double | bScale | |
| Matrix | b |
Returns
| Type | Description |
|---|---|
| Matrix |
SetToEigenvectorsOfSymmetric(Matrix)
Set the columns to the eigenvectors of a symmetric matrix, modifying the input matrix to contain its eigenvalues on the diagonal. The input matrix = this'modifiedthis.
Declaration
public void SetToEigenvectorsOfSymmetric(Matrix symmetricMatrix)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | symmetricMatrix | A symmetric matrix, modified on return to contain its eigenvalues on the diagonal |
SetToElementwiseProduct(Matrix, Matrix)
The element-wise product of two matrices.
Declaration
public Matrix SetToElementwiseProduct(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix, which must have the same size as |
| Matrix | b | Second matrix, which must have the same size as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this[i,j] = a[i,j] * b[i,j]. this receives the product, and must already be the correct size.
a and/or b may be the same object as this.
If this and a/b occupy distinct yet overlapping portions of the same source array, the results are undefined.
SetToElementwiseRatio(Matrix, Matrix)
The element-wise ratio of two matrices.
Declaration
public Matrix SetToElementwiseRatio(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix, which must have the same size as |
| Matrix | b | Second matrix, which must have the same size as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this[i,j] = a[i,j] / b[i,j]. this receives the ratio, and must already be the correct size.
a and/or b may be the same object as this.
If this and a/b occupy distinct yet overlapping portions of the same source array, the results are undefined.
SetToIdentity()
Sets the matrix to the identity.
Declaration
public Matrix SetToIdentity()
Returns
| Type | Description |
|---|---|
| Matrix | The mutated matrix. |
Remarks
The matrix must be square.
SetToIdentityScaledBy(Double)
Sets the matrix to the identity times a number.
Declaration
public Matrix SetToIdentityScaledBy(double scale)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | scale |
Returns
| Type | Description |
|---|---|
| Matrix | The mutated matrix. |
Remarks
The matrix must be square.
SetToKronecker(Matrix, Matrix)
Set this matrix to the Kronecker product of two matrices
Declaration
public void SetToKronecker(Matrix A, Matrix B)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | |
| Matrix | B |
SetToLeastSquares(Matrix, Matrix)
Solve Y = X*A
Declaration
public double SetToLeastSquares(Matrix Y, Matrix X)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | Y | Portrait matrix |
| Matrix | X | Portrait matrix |
Returns
| Type | Description |
|---|---|
| Double | The smallest squared singular value of X. This is useful for detecting an ill-conditioned problem. |
SetToOuter(Matrix)
Modify this to be the product of a matrix and its transpose (A*A').
Declaration
public Matrix SetToOuter(Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | Matrix. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this receives A*A' and must already be the correct size.
If this and A occupy overlapping portions of the same source array, the results are undefined.
SetToOuter(Vector, Vector)
Returns the outer product of two vectors.
Declaration
public Matrix SetToOuter(Vector a, Vector b)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | a | First vector, of length |
| Vector | b | Second vector, of length |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this receives the product, and must already be the correct size.
If this and a/b occupy overlapping portions of the same source array, the results are undefined.
SetToOuterTranspose(Matrix)
Modify this to be the product A'*A.
Declaration
public Matrix SetToOuterTranspose(Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | Matrix. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this receives A'*A and must already be the correct size.
If this and A occupy overlapping portions of the same source array, the results are undefined.
SetToProduct(Matrix, Matrix)
Modify this to be the product of two matrices.
Declaration
public Matrix SetToProduct(Matrix A, Matrix B)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | First matrix. Cannot be |
| Matrix | B | Second matrix. Cannot be |
Returns
| Type | Description |
|---|---|
| Matrix |
Remarks
A and B must have compatible dimensions.
this receives the product, and must already be the correct size.
If this and A/B occupy overlapping portions of the same source array, the results are undefined.
SetToProduct(Matrix, Double)
Gets a matrix times a scalar.
Declaration
public Matrix SetToProduct(Matrix m, double s)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | m | A matrix, which must have the same size as |
| Double | s | A scalar. |
Returns
| Type | Description |
|---|---|
| Matrix |
Remarks
this receives the product, and must already be the correct size.
If this and m occupy distinct yet overlapping portions of the same source array, the results are undefined.
SetToRightSingularVectors(Matrix)
Set the columns to the right singular vectors of a matrix, modifying the input matrix to contain its left singular vectors scaled by the singular values.
Declaration
public void SetToRightSingularVectors(Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | A portrait matrix with the same number of columns as |
Remarks
this must be a square matrix.
SetToSubmatrix(Matrix, Int32, Int32)
Sets the matrix to a submatrix of another.
Declaration
public Matrix SetToSubmatrix(Matrix that, int firstRow, int firstColumn)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | Size must be at least |
| Int32 | firstRow | Index of the first row in |
| Int32 | firstColumn | Index of the first column in |
Returns
| Type | Description |
|---|---|
| Matrix |
|
SetToSum(Matrix, Matrix)
Sets this matrix to sum of two other matrices. Assumes compatible matrices.
Declaration
public Matrix SetToSum(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix, which must have the same size as |
| Matrix | b | Second matrix, which must have the same size as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this[i,j] = a[i,j] + b[i,j]. this receives the sum, and must already be the correct size.
a and/or b may be the same object as this.
If this and a/b occupy distinct yet overlapping portions of the same source array, the results are undefined.
SetToSum(Double, Matrix, Double, Matrix)
The sum of two matrices with scale factors.
Declaration
public Matrix SetToSum(double aScale, Matrix a, double bScale, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | aScale | A scale factor. |
| Matrix | a | First matrix, which must have the same size as |
| Double | bScale | A scale factor. |
| Matrix | b | Second matrix, which must have the same size as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this[i,j] = aScalea[i,j] + bScaleb[i,j].
SetToSumWithOuter(Matrix, Double, Vector, Vector)
Returns a matrix plus the scaled outer product of two vectors.
Declaration
public Matrix SetToSumWithOuter(Matrix m, double scale, Vector a, Vector b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | m | A matrix with the same size as |
| Double | scale | |
| Vector | a | A vector with |
| Vector | b | A vector with |
Returns
| Type | Description |
|---|---|
| Matrix | this[i,j] = m[i,j] + scale*a[i]*b[j] |
SetToTranspose(Matrix)
Sets the matrix to the transpose of another.
Declaration
public Matrix SetToTranspose(Matrix that)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | that | The matrix to transpose. Can be the same object as |
Returns
| Type | Description |
|---|---|
| Matrix |
|
Remarks
this receives the transposed matrix, and must already
be the correct size.
If this and that are different objects but occupy overlapping portions of the same source array, the results are undefined.
Symmetrize()
Set a[i,j] to the average (a[i,j]+a[j,i])/2
Declaration
public void Symmetrize()
SymmetryError()
max | this[i,j] - this[j,i] |
Declaration
public double SymmetryError()
Returns
| Type | Description |
|---|---|
| Double |
ToArray()
Creates a 2-D arrays from this matrix
Declaration
public double[, ] ToArray()
Returns
| Type | Description |
|---|---|
| Double[,] |
ToString()
ToString override
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| String |
Overrides
ToString(String)
Convert the matrix to a string, using a specified number format
Declaration
public string ToString(string format)
Parameters
| Type | Name | Description |
|---|---|---|
| String | format | The format string for each matrix entry |
Returns
| Type | Description |
|---|---|
| String | A string |
Trace()
Trace of a square matrix.
Declaration
public double Trace()
Returns
| Type | Description |
|---|---|
| Double | The trace. |
TraceOfProduct(Matrix, Matrix)
The sum of diagonal elements of a matrix product.
Declaration
public static double TraceOfProduct(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | A matrix of size n by m. |
| Matrix | b | A matrix of size m by n. |
Returns
| Type | Description |
|---|---|
| Double |
|
Transpose()
Transposes the matrix.
Declaration
public Matrix Transpose()
Returns
| Type | Description |
|---|---|
| Matrix | The transposed matrix. |
Operators
Addition(Matrix, Matrix)
Returns the sum of two matrices. Assumes compatible matrices.
Declaration
public static Matrix operator +(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix. |
| Matrix | b | Second matrix. |
Returns
| Type | Description |
|---|---|
| Matrix | Their sum. |
Division(Matrix, Double)
Divides every element of a matrix by a scalar.
Declaration
public static Matrix operator /(Matrix m, double s)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | m | A matrix. |
| Double | s | A scalar. |
Returns
| Type | Description |
|---|---|
| Matrix | A new matrix with the ratio. |
Equality(Matrix, Matrix)
Equality operator.
Declaration
public static bool operator ==(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix. |
| Matrix | b | Second Matrix. |
Returns
| Type | Description |
|---|---|
| Boolean | True if the matrices have the same size and element values. |
Inequality(Matrix, Matrix)
Inequality operator.
Declaration
public static bool operator !=(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix. |
| Matrix | b | Second matrix. |
Returns
| Type | Description |
|---|---|
| Boolean | True if matrices are not equal. |
Multiply(Matrix, Matrix)
Matrix product.
Declaration
public static Matrix operator *(Matrix A, Matrix B)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | First matrix. |
| Matrix | B | Second matrix. |
Returns
| Type | Description |
|---|---|
| Matrix | A new matrix with their product. |
Multiply(Matrix, Vector)
Premultiplies a vector by a matrix (i.e. A*x).
Declaration
public static Vector operator *(Matrix A, Vector x)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | A | A matrix. |
| Vector | x | A vector. |
Returns
| Type | Description |
|---|---|
| Vector | A new vector with the product. |
Multiply(Matrix, Double)
Multiplies every element of a matrix by a scalar.
Declaration
public static Matrix operator *(Matrix m, double s)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | m | A matrix. |
| Double | s | A scalar. |
Returns
| Type | Description |
|---|---|
| Matrix | A new matrix with the product. |
Multiply(Vector, Matrix)
Postmultiplies a vector by a matrix (i.e. x*A).
Declaration
public static Vector operator *(Vector x, Matrix A)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | x | A vector. |
| Matrix | A | A matrix. |
Returns
| Type | Description |
|---|---|
| Vector | The new vector with the product. |
Subtraction(Matrix, Matrix)
Returns the difference of two matrices. Assumes compatible matrices.
Declaration
public static Matrix operator -(Matrix a, Matrix b)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | First matrix. |
| Matrix | b | Second matrix. |
Returns
| Type | Description |
|---|---|
| Matrix | Their difference. |
UnaryNegation(Matrix)
Returns the negation of a matrix.
Declaration
public static Matrix operator -(Matrix a)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix | a | The matrix to negate. |
Returns
| Type | Description |
|---|---|
| Matrix | The negation -a. |
Explicit Interface Implementations
ICollection<Double>.Add(Double)
Declaration
void ICollection<double>.Add(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item |
ICollection<Double>.Clear()
Declaration
void ICollection<double>.Clear()
ICollection<Double>.IsReadOnly
Declaration
bool ICollection<double>.IsReadOnly { get; }
Returns
| Type | Description |
|---|---|
| Boolean |
ICollection<Double>.Remove(Double)
Declaration
bool ICollection<double>.Remove(double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | item |
Returns
| Type | Description |
|---|---|
| Boolean |
IList<Double>.Insert(Int32, Double)
Declaration
void IList<double>.Insert(int index, double item)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index | |
| Double | item |
IList<Double>.RemoveAt(Int32)
Declaration
void IList<double>.RemoveAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index |
IEnumerable.GetEnumerator()
Gets an enumerator for this matrix
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
| Type | Description |
|---|---|
| IEnumerator |