Hermitian Matrices
A HermitianMatrix<T> represents a complex matrix that is equal to its complex conjugate transpose.
How Hermitian matrices are stored
Hermitian matrices are stored in the upper or lower triangular part of a rectangular array. The MatrixTriangle property indicates how elements are stored. It is of type MatrixTriangle and can have values Upper and Lower. The element order is always column major order. Row major storage in the upper triangle is exactly equivalent to column major storage in the lower triangle.
Constructing Hermitian matrices
Hermitian matrices are constructed using factory methods in the Matrix class. Since Hermitian matrices are always square, only one dimension needs to be specified.
The simplest way to create a Hermitian matrix is with the Matrix.CreateHermitian method, which constructs a Hermitian matrix with all elements initially set to zero. The element type must be specified as a generic type argument. For example, for a 5x5 Hermitian matrix, we have:
var h1 = Matrix.CreateHermitian<Complex<double>>(5);To create a Hermitian matrix from existing data, use the CopyFromHermitian<T> method. This method copies the data, so changes to the original array will not affect the matrix. The first argument specifies the dimension (number of rows and columns). The second argument is an array containing the elements of the matrix. The third argument is of type MatrixTriangle, and indicates whether the elements are taken from the upper or lower triangular part of the element array. The fourth argument is of type MatrixElementOrder, and indicates whether the elements in the array are stored in column-major or row-major order.
Complex<double>[] elements = {
new Complex<double>(1), Complex<double>.Zero, Complex<double>.Zero,
new Complex<double>(2, 2), new Complex<double>(3), Complex<double>.Zero,
new Complex<double>(4, 3), new Complex<double>(5, 4), new Complex<double>(6) };
var h2 = Matrix.CopyFromHermitian(elements, 3,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor);
var h3 = Matrix.CopyFromHermitian(elements, 3,
MatrixTriangle.Lower, MatrixElementOrder.RowMajor);To create a Hermitian matrix that wraps an existing array without copying (creating a "view"), use the WrapHermitian method. Any changes to the wrapped array will affect the matrix, and vice versa.
var h4 = Matrix.WrapHermitian(elements, 3,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor);Methods specific to Hermitian matrices
The static FromOuterProduct methods return a Hermitian matrix that is the product of a matrix with its transpose. An optional second argument of type MatrixOperationSide specifies which of the two operands should be transposed. The default is MatrixOperationSide.Left The following example creates a 2x2 Hermitian matrix that is the product of a 2x3 matrix with its transpose.
var a = Matrix.CopyFrom(new Complex<double>[] {
(1, 6), (2, 5), (3, 4),
(4, 3), (5, 2), (6, 1) },
3, 2, MatrixElementOrder.ColumnMajor);
var h5 = Numerics.NET.LinearAlgebra.HermitianMatrix<Complex<double>>.FromOuterProduct(a);The HermitianMatrix<T> class has two specific instance methods. The AddOuterProduct method adds the outer product of a vector or a matrix with its transpose to a HermitianMatrix<T>. You may supply a scale factor for the outer product, as in the following example:
var A = Matrix.CreateHermitian<Complex<double>>(3);
var v = Vector.CopyFrom(new Complex<double>(1, 2),
new Complex<double>(3, 4), new Complex<double>(5, 6));
var w = Vector.CopyFrom(new Complex<double>(6, 5),
new Complex<double>(4, 3), new Complex<double>(2, 1));
A.AddOuterProduct(2, v);
A.AddHermitianOuterProduct(1.0, v, w);Complementary to this method is the SubtractOuterProduct method, which subtracts an outer product.
The GetEigenvalues method returns a DenseVector<T> that contains the eigenvalues of the matrix. If you also need the eigenvectors, it is more efficient to create a EigenvalueDecomposition<T> object from the matrix and use its Eigenvalues and Eigenvectors properties.
The ApplyMatrixFunction calculates a matrix function. Its only argument is a Func<T, TResult> delegate that specifies the function to calculate. The matrix function is computed by computing the eigenvalue decomposition and applying the function to each of the eigenvalues, and finally computing the matrix with the same eigenvectors but the transformed eigenvalues. The example below calculates the exponential of a 2x2 Hermitian matrix:
var H = Matrix.CopyFromHermitian(new[] {
new Complex<double>(1, 2), Complex<double>.Zero,
new Complex<double>(3, 4), new Complex<double>(5, 6) }, 2, MatrixTriangle.Lower, MatrixElementOrder.RowMajor);
var expS = H.ApplyMatrixFunction(Complex<double>.Exp);