Code Packages  1
Add-on code processing modules
Public Member Functions | Static Public Member Functions | Properties | List of all members
dsp.Matrix< T > Class Template Reference

Provides a generic matrix data type and common matrices operations. More...

Inheritance diagram for dsp.Matrix< T >:

Public Member Functions

 Matrix (int rows, int columns)
 Initialize a new instance of a 2D Matrix class More...
 
void Add (params T[] args)
 Used internally for initialization upon declaration. More...
 
void Clear ()
 Clears all elements of the matrix More...
 
Matrix< T > Copy ()
 Copy the current matrix to a new matrix More...
 
override bool Equals (object obj)
 
T [] GetColumn (int index)
 Gets all matrix elements in the specified column More...
 
T [] GetDiagonal ()
 Gets diagonal elements of the matrix More...
 
IEnumerator GetEnumerator ()
 Returns an enumerator that iterates through a collection. More...
 
override int GetHashCode ()
 
T [] GetRow (int index)
 Gets all matrix elements in the specified row More...
 
bool IsDiagonal ()
 Check if current matrix is a diagonal matrix More...
 
bool IsIdentity ()
 Check if current matrix is an Identity matrix More...
 
bool IsOne ()
 Check if current matrix consists of all 1s More...
 
bool IsScalar ()
 Check if current matrix is a scalar matrix More...
 
bool IsSingular ()
 Check if current matrix is singular More...
 
bool IsSquare ()
 Check if current matrix is a square matrix More...
 
bool IsSymmetric ()
 Check if current matrix is a symmetric matrix More...
 
bool IsZero ()
 Check if current matrix is a zero matrix More...
 
void SetColumn (int index, params T[] values)
 Sets matrix elements in the specified column to the given values More...
 
void SetRow (int index, params T[] values)
 Sets matrix elements in the specified row to the given values More...
 
override string ToString ()
 Converts the Matrix to its string representation More...
 

Static Public Member Functions

static Matrix< T > Adjugate< T > (Matrix< T > A)
 Calculates the Adjugate of a square Matrix (classical adjoint) More...
 
static Matrix< T > Cofactor< T > (Matrix< T > A)
 Calculates the cofactor of a square Matrix More...
 
static Matrix< T > Compare< T > (Matrix< T > A, Matrix< T > B)
 Compares two matrices element by element More...
 
static T Determinant< T > (Matrix< T > A)
 Calculates the determinant of a square matrix More...
 
static Matrix< T > Diagonal< T > (params T[] values)
 Creates an Instance of a diagonal matrix with specified diagonal elements More...
 
static Matrix< T > ElementF< T > (Matrix< T > A, Func< T, T > f)
 Function to perform calculations on each element of a matrix More...
 
static Matrix< T > Fill< T > (int rows, int columns, T value)
 Creates an Instance of an empty matrix and fills all elements with the specified value More...
 
static Matrix< T > Identity< T > (int size)
 Creates an Instance of an Identity matrix More...
 
static Matrix< T > Inverse< T > (Matrix< T > A)
 Calculates the inverse (reciprocal matrix) of a square matrix More...
 
static T Minor< T > (Matrix< T > A, int row, int column)
 Calculates the Minor at specified index of a Matrix More...
 
static bool operator!= (Matrix< T > A, Matrix< T > B)
 Inequality check of two matrices More...
 
static Matrix< T > operator* (Matrix< T > A, Matrix< T > B)
 Performs Matrix multiplication More...
 
static Matrix< T > operator* (Matrix< T > A, T value)
 Performs scalar multiplication More...
 
static Matrix< T > operator+ (Matrix< T > A, Matrix< T > B)
 Performs Matrix addition More...
 
static Matrix< T > operator+ (Matrix< T > A, T value)
 Performs scalar addition More...
 
static Matrix< T > operator- (Matrix< T > A, Matrix< T > B)
 Performs Matrix subtraction More...
 
static Matrix< T > operator- (Matrix< T > A, T value)
 Performs scalar subtraction More...
 
static Matrix< T > operator- (Matrix< T > A)
 Negates (opposite) all elements in the matrix More...
 
static Matrix< T > operator/ (Matrix< T > A, T value)
 Performs scalar division More...
 
static bool operator== (Matrix< T > A, Matrix< T > B)
 Equality check of two matrices More...
 
static Matrix< T > Pow< T > (Matrix< T > A, int exponent)
 Calculates the matrix raised to specified power More...
 
static Matrix< T > Remove< T > (Matrix< T > A, int row, int column)
 Removes the specified row and column More...
 
static Matrix< T > RemoveColumn< T > (Matrix< T > A, int index)
 Removes the specified column More...
 
static Matrix< T > RemoveRow< T > (Matrix< T > A, int index)
 Removes the specified row More...
 
static Matrix< T > Round< T > (Matrix< T > A, int decimals)
 Rounds a matrix to the specified number of fractional digits More...
 
static T Trace< T > (Matrix< T > A)
 Calculates the Trace of a Matrix More...
 
static Matrix< T > Transpose< T > (Matrix< T > A)
 Matrix Transpose More...
 

Properties

int Columns [get]
 Gets the number of columns in the Matrix More...
 
int Rows [get]
 Gets the number of rows in the Matrix More...
 
int Size [get]
 Gets the size of the Matrix More...
 
this[int row, int column] [get, set]
 Provides indexing in Matrix More...
 

Detailed Description

Provides a generic matrix data type and common matrices operations.

Provides basic matrix calculations.

Matrix supports many data types such as Matrix<int>, Matrix<double>, Matrix<Complex>, etc.
Note: A class instance is a reference type, not a value type.

Template Parameters
Tspecified elements data type

Constructor & Destructor Documentation

◆ Matrix()

dsp.Matrix< T >.Matrix ( int  rows,
int  columns 
)

Initialize a new instance of a 2D Matrix class


Parameters
rowsmatrix rows
columnsmatrix columns


Example 1
The following example creates an empty Matrix with 2 rows and 3 columns.
Each element of a matrix is double data type.

Matrix<double> m = new Matrix<double>(2,3);
Console.WriteLine(m);
0 0 0
0 0 0

Example 2
The following example instantiates a Matrix (2 rows by 3 columns) with default values.
Element type is double

Matrix<double> m = new Matrix<double>(2,3){{1,2,3},{4,5,6}};
Console.WriteLine(m);
1 2 3
4 5 6

Example 3
The following example instantiates a column vector (single dimension matrix) with default values.
Element type is double

Matrix<double> m = new Matrix<double>(3,1){1,2,3};
Console.WriteLine(m);
1
2
3

Example 4
The following example instantiates a row vector (single dimension matrix) with default values.
Element type is double

Matrix<double> m = new Matrix<double>(1,3){{1,2,3}};
Console.WriteLine(m);
1 2 3

Example 5
The following example instantiates a complex square Matrix with default values.
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
0+0i 0+1i
1+0i 1+1i

Member Function Documentation

◆ Add()

void dsp.Matrix< T >.Add ( params T []  args)

Used internally for initialization upon declaration.

Do not use this method!

◆ Adjugate< T >()

static Matrix<T> dsp.Matrix< T >.Adjugate< T > ( Matrix< T >  A)
static

Calculates the Adjugate of a square Matrix (classical adjoint)

The adjugate of matrix A is the transpose of the cofactor matrix of A

Parameters
Ainput matrix
Returns
$ \displaystyle adj(A) = C^T \hspace{0.5cm},\hspace{0.5cm} A adj(A)=det(A)I$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{0,4,5},{1,0,6}};
Console.WriteLine(m);
Console.WriteLine(Matrix.Adjugate(m));
1 2 3
0 4 5
1 0 6
24 -12 -2
5 3 -5
-4 2 4

◆ Clear()

void dsp.Matrix< T >.Clear ( )

Clears all elements of the matrix

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m.Clear();
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 0

◆ Cofactor< T >()

static Matrix<T> dsp.Matrix< T >.Cofactor< T > ( Matrix< T >  A)
static

Calculates the cofactor of a square Matrix

Returns a matrix with elements that are the cofactors, term-by-term, of a given square matrix.

Parameters
Ainput matrix
Returns
$ \displaystyle C_{ij}=(-1)^{i+j} M_{ij} \hspace{0.5cm},\hspace{0.5cm} M_{ij}=Minor(A_{ij})$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m = Matrix.Cofactor(m);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
-3 6 -3
6 -12 6
-3 6 -3

◆ Compare< T >()

static Matrix<T> dsp.Matrix< T >.Compare< T > ( Matrix< T >  A,
Matrix< T >  B 
)
static

Compares two matrices element by element

Provides a way to check for equality of two matrices element by element
Matrixes A and B must be of equal size

Parameters
Afirst input matrix
Bsecond input matrix

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Matrix<double> c = m.Copy();
Console.WriteLine(Matrix.Compare(m,c));
1 1 1
1 1 1
1 1 1

◆ Copy()

Matrix<T> dsp.Matrix< T >.Copy ( )

Copy the current matrix to a new matrix

Copy method provides direct copy of all elements of the current matrix to a new instance of the matrix class.

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Matrix<double> c = m.Copy();
Console.WriteLine(m==c);
True

◆ Determinant< T >()

static T dsp.Matrix< T >.Determinant< T > ( Matrix< T >  A)
static

Calculates the determinant of a square matrix

A single number obtained from a matrix that reveals a variety of the matrix's properties.
Input must be a square matrix.

Parameters
Ainput matrix
Returns
$ \displaystyle det(A)=\sum\limits_{i=1}^n a_{ij} C_{ij} \hspace{0.5cm},\hspace{0.5cm} C = cofactor(A)$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(Matrix.Determinant(m));
1 2 3
4 5 6
7 8 9
0

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(Matrix.Determinant(m));
0+0i 0+1i
1+0i 1+1i
0-1i

◆ Diagonal< T >()

static Matrix<T> dsp.Matrix< T >.Diagonal< T > ( params T []  values)
static

Creates an Instance of a diagonal matrix with specified diagonal elements

A diagonal matrix is a square matrix in which the entries outside the main diagonal (↘) are all zero.
The diagonal entries themselves may or may not be zero.


Parameters
valuesdiagonal elements

Example 1
Element type is double

Matrix<double> m = Matrix.Diagonal<double>(1,2,3);
Console.WriteLine(m);
1 0 0
0 2 0
0 0 3

Example 2
Element type is Complex

Matrix<Complex> m = Matrix.Diagonal<Complex>(1,2,3);
Console.WriteLine(m);
1+0i 0+0i 0+0i
0+0i 2+0i 0+0i
0+0i 0+0i 3+0i

◆ ElementF< T >()

static Matrix<T> dsp.Matrix< T >.ElementF< T > ( Matrix< T >  A,
Func< T, T >  f 
)
static

Function to perform calculations on each element of a matrix

ElementF can handle input functions that have more than one parameter by using the lamda operator =>

Parameters
Ainput matrix
finput function which returns a supported type value


Example 1
The following example calculates the complex conjugate transpose on a complex matrix

Matrix<Complex> m = new Matrix<Complex>(2,2){{1,new Complex(-2,-1)},{new Complex(1,1), new Complex(0,-1)}};
Console.WriteLine(m);
m = Matrix.Transpose(m);
m = Matrix.ElementF(m, Complex.Conjugate);
Console.WriteLine(m);
1+0i -2-1i
1+1i 0-1i
1+0i 1-1i
-2+1i 0+1i

Example 2
The following example calculates the cube on each element of a matrix, using the lamda operator

Matrix<double> m = new Matrix<double>(2,2){{0,1},{2, 3}};
Console.WriteLine(m);
m = Matrix.ElementF(m, d=> Math.Pow(d,3));
Console.WriteLine(m);
0 1
2 3
0 1
8 27

◆ Equals()

override bool dsp.Matrix< T >.Equals ( object  obj)

◆ Fill< T >()

static Matrix<T> dsp.Matrix< T >.Fill< T > ( int  rows,
int  columns,
value 
)
static

Creates an Instance of an empty matrix and fills all elements with the specified value

Parameters
rowsmatrix rows
columnsmatrix columns
valueelements value

Example 1
Element type is double

Matrix<double> m = Matrix.Fill<double>(3,3,1);
Console.WriteLine(m);
1 1 1
1 1 1
1 1 1

Example 2
Element type is Complex

Matrix<Complex> m = Matrix.Fill<Complex>(3,3,1);
Console.WriteLine(m);
1+0i 1+0i 1+0i
1+0i 1+0i 1+0i
1+0i 1+0i 1+0i

◆ GetColumn()

T [] dsp.Matrix< T >.GetColumn ( int  index)

Gets all matrix elements in the specified column

Parameters
indexcolumn index (zero based)


Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
double[] r = m.GetColumn(1);
Console.WriteLine(NArray.ToString(r));
2 5 8

◆ GetDiagonal()

T [] dsp.Matrix< T >.GetDiagonal ( )

Gets diagonal elements of the matrix

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
double[] r = m.GetDiagonal();
Console.WriteLine(NArray.ToString(r));
1 5 9

◆ GetEnumerator()

IEnumerator dsp.Matrix< T >.GetEnumerator ( )

Returns an enumerator that iterates through a collection.

◆ GetHashCode()

override int dsp.Matrix< T >.GetHashCode ( )

◆ GetRow()

T [] dsp.Matrix< T >.GetRow ( int  index)

Gets all matrix elements in the specified row

Parameters
indexrow index (zero based)


Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
double[] r = m.GetRow(1);
Console.WriteLine(NArray.ToString(r));
4 5 6

◆ Identity< T >()

static Matrix<T> dsp.Matrix< T >.Identity< T > ( int  size)
static

Creates an Instance of an Identity matrix

An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere.


Parameters
sizesize of one side of the square matrix

Example 1
Element type is double

Matrix<double> m = Matrix.Identity<double>(3);
Console.WriteLine(m);
1 0 0
0 1 0
0 0 1

Example 2
Element type is Complex

Matrix<Complex> m = Matrix.Identity<Complex>(3);
Console.WriteLine(m);
1+0i 0+0i 0+0i
0+0i 1+0i 0+0i
0+0i 0+0i 1+0i

◆ Inverse< T >()

static Matrix<T> dsp.Matrix< T >.Inverse< T > ( Matrix< T >  A)
static

Calculates the inverse (reciprocal matrix) of a square matrix

A square matrix A has an inverse iff the determinant is not zero (nonsingular matrix). Non-square matrices do not have inverses.

Parameters
Ainput matrix
Returns
$ \displaystyle A A^{-1} = A^{-1} A = I$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(2,2){{4,7},{2,6}};
Console.WriteLine(m);
Console.WriteLine(m.IsSingular());
Console.WriteLine(Matrix.Inverse(m));
4 7
2 6
false
0.6 -0.7
-0.2 0.4

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(m.IsSingular());
Console.WriteLine(Matrix.Inverse(m));
0+0i 0+1i
1+0i 1+1i
false
-1+1i 1+0i
0-1i 0+0i

◆ IsDiagonal()

bool dsp.Matrix< T >.IsDiagonal ( )

Check if current matrix is a diagonal matrix

A diagonal matrix is a square matrix in which the entries outside the main diagonal (↘) are all zero.
The diagonal entries themselves may or may not be zero.


Example

Matrix<double> m = Matrix.Diagonal<double>(1,2,3);
Console.WriteLine(m);
Console.WriteLine(m.IsDiagonal());
1 0 0
0 2 0
0 0 3
True

◆ IsIdentity()

bool dsp.Matrix< T >.IsIdentity ( )

Check if current matrix is an Identity matrix

An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere.


Example

Matrix<double> m = Matrix.Identity<double>(3);
Console.WriteLine(m.IsIdentity());
True

◆ IsOne()

bool dsp.Matrix< T >.IsOne ( )

Check if current matrix consists of all 1s

Example

Matrix<double> m = Matrix.Fill<double>(3, 3, 1)
Console.WriteLine(m);
Console.WriteLine(m.IsOne());
1 1 1
1 1 1
1 1 1
True

◆ IsScalar()

bool dsp.Matrix< T >.IsScalar ( )

Check if current matrix is a scalar matrix

A diagonal matrix with all its main diagonal entries equal is a scalar matrix.

Example

Matrix<double> m = Matrix.Diagonal<double>(1,1,1);
Console.WriteLine(m);
Console.WriteLine(m.IsScalar());
1 0 0
0 1 0
0 0 1
True

◆ IsSingular()

bool dsp.Matrix< T >.IsSingular ( )

Check if current matrix is singular

Singular matrix is a square matrix that does not have a matrix inverse.
A matrix is singular iff its determinant is 0.

Returns
$true \iff |m|=0 $

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m.IsSingular());
Console.WriteLine("det=" + Matrix.Determinant(m));
Console.WriteLine(Matrix.Inverse(m));
1 2 3
4 5 6
7 8 9
True
det=0
-Infinity Infinity -Infinity
Infinity -Infinity Infinity
-Infinity Infinity -Infinity

◆ IsSquare()

bool dsp.Matrix< T >.IsSquare ( )

Check if current matrix is a square matrix

Example

Matrix<double> m = new Matrix<double>(3,3);
Console.WriteLine(m.IsSquare());
True

◆ IsSymmetric()

bool dsp.Matrix< T >.IsSymmetric ( )

Check if current matrix is a symmetric matrix

A symmetric matrix is a square matrix that is equal to its transpose. The entries of a symmetric matrix are symmetric with respect to the main diagonal.


Example

Matrix<double> m = Matrix.Identity<double>(3);
Console.WriteLine(m.IsSymmetric());
True

◆ IsZero()

bool dsp.Matrix< T >.IsZero ( )

Check if current matrix is a zero matrix

Example

Matrix<double> m = new Matrix<double>(3,3);
Console.WriteLine(m);
Console.WriteLine(m.IsZero());
0 0 0
0 0 0
0 0 0
True

◆ Minor< T >()

static T dsp.Matrix< T >.Minor< T > ( Matrix< T >  A,
int  row,
int  column 
)
static

Calculates the Minor at specified index of a Matrix

A minor is the reduced determinant of a determinant expansion that is formed by omitting the ith row and jth column of a square matrix A

Parameters
Ainput matrix
rowrow index (zero based)
columncolumn index (zero based)
Returns
$ \displaystyle M_{ij}=det(A_{pq}) \hspace{0.5cm},\hspace{0.5cm} p\ne i, q\ne j$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,4,7},{3,0,5},{-1,9,11}};
Console.WriteLine(m);
Console.WriteLine(Matrix.Minor(m,1,2));
1 4 7
3 0 5
-1 9 11
13

◆ operator!=()

static bool dsp.Matrix< T >.operator!= ( Matrix< T >  A,
Matrix< T >  B 
)
static

Inequality check of two matrices

Parameters
Afirst input matrix
Bsecond input matrix
Returns
$true \iff A \ne B $

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Matrix<double> c = m.Copy();
Console.WriteLine(m!=c);
False

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Matrix<Complex> c = m.Copy();
Console.WriteLine(m!=c);
False

◆ operator*() [1/2]

static Matrix<T> dsp.Matrix< T >.operator* ( Matrix< T >  A,
Matrix< T >  B 
)
static

Performs Matrix multiplication

If A is an n×m matrix and B is an m×p matrix,the result is an n×p matrix.

Parameters
Amatrix
Bmatrix
Returns
$\displaystyle C_{ij}=\sum\limits_{k=1}^{m}A_{ik}B_{kj}\hspace{1cm} for\ i=1, ...,n \ and \ j=1, ..., p$

Example 1
Element type is double

Matrix<double> a = new Matrix<double>(2,3){{1,2,3},{4,5,6}};
Matrix<double> b = new Matrix<double>(3,2){{7,8},{9,10},{11,12}};
Console.WriteLine("a=\n" + a);
Console.WriteLine("b=\n" + b);
Console.WriteLine("c=\n" + a*b);
a=
1 2 3
4 5 6
b=
7 8
9 10
11 12
c=
58 64
139 154

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(m*m);
0+0i 0+1i
1+0i 1+1i
0+1i -1+1i
1+1i 0+3i

◆ operator*() [2/2]

static Matrix<T> dsp.Matrix< T >.operator* ( Matrix< T >  A,
value 
)
static

Performs scalar multiplication

Parameters
Ainput matrix
valuescalar value
Returns
$\displaystyle C_{ij}=A_{ij} * value$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m*1.5);
1 2 3
4 5 6
7 8 9
1.5 3 4.5
6 7.5 9
10.5 12 13.5

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(m*1.5);
0+0i 0+1i
1+0i 1+1i
0+0i 0+1.5i
1.5+0i 1.5+1.5i

◆ operator+() [1/2]

static Matrix<T> dsp.Matrix< T >.operator+ ( Matrix< T >  A,
Matrix< T >  B 
)
static

Performs Matrix addition

A and B should be of same dimension

Parameters
Afirst input matrix
Bsecond input matrix
Returns
$\displaystyle C_{ij}=A_{ij} + B_{ij}$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m+m);
1 2 3
4 5 6
7 8 9
2 4 6
8 10 12
14 16 18

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(m+m);
0+0i 0+1i
1+0i 1+1i
0+0i 0+2i
2+0i 2+2i

◆ operator+() [2/2]

static Matrix<T> dsp.Matrix< T >.operator+ ( Matrix< T >  A,
value 
)
static

Performs scalar addition

Parameters
Ainput matrix
valuescalar value
Returns
$\displaystyle C_{ij}=A_{ij} + value$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m+1);
1 2 3
4 5 6
7 8 9
2 3 4
5 6 7
8 9 10

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(m+1);
0+0i 0+1i
1+0i 1+1i
1+0i 1+1i
2+0i 2+1i

◆ operator-() [1/3]

static Matrix<T> dsp.Matrix< T >.operator- ( Matrix< T >  A,
Matrix< T >  B 
)
static

Performs Matrix subtraction

A and B should be of same dimension

Parameters
Afirst input matrix
Bsecond input matrix
Returns
$\displaystyle C_{ij}=A_{ij} - B_{ij}$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m-m);
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 0

◆ operator-() [2/3]

static Matrix<T> dsp.Matrix< T >.operator- ( Matrix< T >  A,
value 
)
static

Performs scalar subtraction

Parameters
Ainput matrix
valuescalar value
Returns
$\displaystyle C_{ij}=A_{ij} - value$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m-1);
1 2 3
4 5 6
7 8 9
0 1 2
3 4 5
6 7 8

◆ operator-() [3/3]

static Matrix<T> dsp.Matrix< T >.operator- ( Matrix< T >  A)
static

Negates (opposite) all elements in the matrix

Parameters
Ainput matrix
Returns
$\displaystyle C_{ij}=-A_{ij}$

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{-1,2,3},{4,-5,6},{7,8,-9}};
Console.WriteLine(m);
Console.WriteLine(-m);
1 2 3
4 5 6
7 8 9
1 -2 -3
-4 5 -6
-7 -8 9

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
Console.WriteLine(-m);
0+0i 0+1i
1+0i 1+1i
0+0i 0-1i
-1+0i -1-1i

◆ operator/()

static Matrix<T> dsp.Matrix< T >.operator/ ( Matrix< T >  A,
value 
)
static

Performs scalar division

Parameters
Ainput matrix
valuescalar value
Returns
$\displaystyle C_{ij}=A_{ij} / value$

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
Console.WriteLine(m/2);
1 2 3
4 5 6
7 8 9
0.5 1 1.5
2 2.5 3
3.5 4 4.5

◆ operator==()

static bool dsp.Matrix< T >.operator== ( Matrix< T >  A,
Matrix< T >  B 
)
static

Equality check of two matrices

Parameters
Afirst input matrixr
Bsecond input matrix
Returns
$true \iff A=B $

Example 1
Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Matrix<double> c = m.Copy();
Console.WriteLine(m==c);
True

Example 2
Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Matrix<Complex> c = m.Copy();
Console.WriteLine(m==c);
True

◆ Pow< T >()

static Matrix<T> dsp.Matrix< T >.Pow< T > ( Matrix< T >  A,
int  exponent 
)
static

Calculates the matrix raised to specified power

Square matrices can be multiplied by themselves repeatedly in the same way as ordinary numbers, because they always have the same number of rows and columns.
This repeated multiplication can be described as a power of the matrix, a special case of the ordinary matrix product.

Parameters
Ainput matrix
exponentexponent value


Example 1

Element type is double

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m = Matrix.Pow(m,2);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
30 36 42
66 81 96
102 126 150

Example 2

Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2){{0,new Complex(0,1)},{1,new Complex(1,1)}};
Console.WriteLine(m);
m = Matrix.Pow(m,3);
Console.WriteLine(m);
0+0i 0+1i
1+0i 1+1i
-1+1i -3+0i
0+3i -4+4i

◆ Remove< T >()

static Matrix<T> dsp.Matrix< T >.Remove< T > ( Matrix< T >  A,
int  row,
int  column 
)
static

Removes the specified row and column

Returns a new matrix instance with row and column removed

Parameters
Ainput matrix
rowrow to be removed (zero index)
columncolumn to be removed (zero index)

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m = Matrix.Remove(m,0,1);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
4 6
7 9

◆ RemoveColumn< T >()

static Matrix<T> dsp.Matrix< T >.RemoveColumn< T > ( Matrix< T >  A,
int  index 
)
static

Removes the specified column

Returns a new matrix instance with column removed

Parameters
Ainput matrix
indexcolumn to be removed (zero index)

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m = Matrix.RemoveColumn(m,1);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
1 3
4 6
7 9

◆ RemoveRow< T >()

static Matrix<T> dsp.Matrix< T >.RemoveRow< T > ( Matrix< T >  A,
int  index 
)
static

Removes the specified row

Returns a new matrix instance with row removed

Parameters
Ainput matrix
indexrow to be removed (zero index)

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m = Matrix.RemoveRow(m,1);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
1 2 3
7 8 9

◆ Round< T >()

static Matrix<T> dsp.Matrix< T >.Round< T > ( Matrix< T >  A,
int  decimals 
)
static

Rounds a matrix to the specified number of fractional digits

Rounding applies on all matrix elements using an away from zero mode.

Parameters
Ainput matrix
decimalsnumber of fractional digits


Example 1

Element type is double

Matrix<double> m = new Matrix<double>(3,3){{0,2,3},{4,0,6},{7,8,0}};
m = Matrix.Inverse(m);
Console.WriteLine(m);
Console.WriteLine(Matrix.Round(m,3));
-0.266666666666667 0.133333333333333 0.0666666666666667
0.233333333333333 -0.116666666666667 0.0666666666666667
0.177777777777778 0.0777777777777778 -0.0444444444444444
-0.267 0.133 0.067
0.233 -0.117 0.067
0.178 0.078 -0.044

Example 2

Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(3,3){{0,2,3},{4,0,6},{7,8,0}};
m = Matrix.Inverse(m);
Console.WriteLine(m);
Console.WriteLine(Matrix.Round(m,3));
-0.266666666666667+0i 0.133333333333333+0i 0.0666666666666667+0i
0.233333333333333+0i -0.116666666666667+0i 0.0666666666666667+0i
0.177777777777778+0i 0.0777777777777778+0i -0.0444444444444444+0i
-0.267+0i 0.133+0i 0.067+0i
0.233+0i -0.117+0i 0.067+0i
0.178+0i 0.078+0i -0.044+0i

◆ SetColumn()

void dsp.Matrix< T >.SetColumn ( int  index,
params T []  values 
)

Sets matrix elements in the specified column to the given values

It replace values from up to down. Maximum row count is based on the number of supplied values

Parameters
indexcolumn index (zero based)
valuescolumn elements

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m.SetColumn(1,0,0,0);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
1 0 3
4 0 6
7 0 9

◆ SetRow()

void dsp.Matrix< T >.SetRow ( int  index,
params T []  values 
)

Sets matrix elements in the specified row to the given values

It replace values from left to right. Maximum column count is based on the number of supplied values

Parameters
indexrow index (zero based)
valuesrow elements

Example

Matrix<double> m = new Matrix<double>(3,3){{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(m);
m.SetRow(1,0,0,0);
Console.WriteLine(m);
1 2 3
4 5 6
7 8 9
1 2 3
0 0 0
7 8 9

◆ ToString()

override string dsp.Matrix< T >.ToString ( )

Converts the Matrix to its string representation

Converted string is in clear and printable form.
The function Matrix.Round can be used to aid clarity

Example

Matrix<double> m = new Matrix<double>(2,2){{1,222},{33,44}};
Console.WriteLine(m);
1 222
33 44

◆ Trace< T >()

static T dsp.Matrix< T >.Trace< T > ( Matrix< T >  A)
static

Calculates the Trace of a Matrix

The trace of a square matrix A is defined to be the sum of the elements on the main diagonal

Returns
$ \displaystyle tr(A)=\sum\limits_{i=1}^n A_{ii}=A_{11}+A_{22}+A_{nn}$

Example

Matrix<double> m = new Matrix<double>(3,3){{-1,0,3},{11,5,2},{6,12,-5}};
Console.WriteLine(m);
Console.WriteLine(Matrix.Trace(m));
-1 0 3
11 5 2
6 12 -5
-1

◆ Transpose< T >()

static Matrix<T> dsp.Matrix< T >.Transpose< T > ( Matrix< T >  A)
static

Matrix Transpose

A matrix which is formed by turning all the rows of a given matrix into columns and vice-versa.

Parameters
Ainput matrix
Returns
$ A^T$

Example

Matrix<double> m = new Matrix<double>(3,2){{1,2},{3,4},{5,6}};
Console.WriteLine(m);
m = Matrix.Transpose(m);
Console.WriteLine(m);
1 2
3 4
5 6
1 3 5
2 4 6

Property Documentation

◆ Columns

int dsp.Matrix< T >.Columns
get

Gets the number of columns in the Matrix

Example

Matrix<double> m = new Matrix<double>(2,3);
Console.WriteLine(m.Columns);
3

◆ Rows

int dsp.Matrix< T >.Rows
get

Gets the number of rows in the Matrix

Example

Matrix<double> m = new Matrix<double>(2,3);
Console.WriteLine(m.Rows);
2

◆ Size

int dsp.Matrix< T >.Size
get

Gets the size of the Matrix

Example

Matrix<double> m = new Matrix<double>(2,3);
Console.WriteLine(m.Size);
6

◆ this[int row, int column]

T dsp.Matrix< T >.this[int row, int column]
getset

Provides indexing in Matrix

Can be used to get and set an element


Parameters
rowelement row index (zero based)
columnelement column index (zero based)


Example 1 Element type is double

Matrix<double> m = new Matrix<double>(2,2);
m[0,1] = 1;
Console.WriteLine(m);
0 1
0 0

Example 2 Element type is Complex

Matrix<Complex> m = new Matrix<Complex>(2,2);
m[0,1] = 1;
m[1,0] = new Complex(0,1);
Console.WriteLine(m);
0+0i 1+0i
0+1i 0+0i