A HermitianMatrixT
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
Constructing Hermitian matrices is analogous to triangular matrices, by calling the
MatrixCreateHermitian
method. The fact that Hermitian matrices are always square means only one dimension
needs to be passed to the method. The method has several overloads.
The simplest overload has just one argument. It constructs a Hermitian matrix
with the specified number of rows and columns. All elements are 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);
Dim h1 = Matrix.CreateHermitian(Of Complex(Of Double))(5)
No code example is currently available or this language may not be supported.
let h1 = Matrix.CreateHermitian<Complex<float>>(5)
The two remaining overloads allow you to initialize the elements of the matrix.
The first argument once again specifies the number of rows and columns.
The second argument is an array containing the elements of the new 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.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor);
var h3 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Lower, MatrixElementOrder.RowMajor);
Dim elements = {
New Complex(Of Double)(1), Complex(Of Double).Zero, Complex(Of Double).Zero,
New Complex(Of Double)(2, 2), New Complex(Of Double)(3), Complex(Of Double).Zero,
New Complex(Of Double)(4, 3), New Complex(Of Double)(5, 4), New Complex(Of Double)(6)}
Dim h2 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor)
Dim h3 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Lower, MatrixElementOrder.RowMajor)
No code example is currently available or this language may not be supported.
let elements = [|
Complex(1.) ; Complex.Zero ; Complex.Zero ;
Complex(2., 2.) ; Complex(3.) ; Complex.Zero ;
Complex(4., 3.) ; Complex(5., 4.) ; Complex(6.) |]
let h2 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor)
let h3 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Lower, MatrixElementOrder.RowMajor)
The last overload adds one Boolean argument that lets you specify whether
the element array should be reused or copied. If the element array is reused,
any changes to its elements will result in changes to the corresponding
elements of the matrix.
var h4 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor, true);
Dim h4 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor, True)
No code example is currently available or this language may not be supported.
let h4 = Matrix.CreateHermitian(3, elements,
MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor, true)
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.Create(3, 2, new[] {
new Complex<double>(1, 6), new Complex<double>(2, 5), new Complex<double>(3, 4),
new Complex<double>(4, 3), new Complex<double>(5, 2), new Complex<double>(6, 1) },
MatrixElementOrder.ColumnMajor);
var h5 = Extreme.Mathematics.LinearAlgebra.HermitianMatrix<Complex<double>>.FromOuterProduct(a);
Dim A = Matrix.Create(3, 2, {
New Complex(Of Double)(1, 6), New Complex(Of Double)(2, 5), New Complex(Of Double)(3, 4),
New Complex(Of Double)(4, 3), New Complex(Of Double)(5, 2), New Complex(Of Double)(6, 1)},
MatrixElementOrder.ColumnMajor)
Dim h5 = HermitianMatrix(Of Complex(Of Double)).FromOuterProduct(A)
No code example is currently available or this language may not be supported.
let a = Matrix.Create(3, 2,
[|
Complex(1., 6.) ; Complex(2., 5.) ; Complex(3., 4.) ;
Complex(4., 3.) ; Complex(5., 2.) ; Complex(6., 1.)
|], MatrixElementOrder.ColumnMajor)
let h5 = Extreme.Mathematics.LinearAlgebra.HermitianMatrix<Complex<float>>.FromOuterProduct(a)
The HermitianMatrixT
class has two specific instance methods. The
AddOuterProduct
method adds the outer product of a vector or a matrix with its transpose to a
HermitianMatrixT.
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.Create(new Complex<double>(1, 2),
new Complex<double>(3, 4), new Complex<double>(5, 6));
var w = Vector.Create(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);
Dim h6 = Matrix.CreateHermitian(Of Complex(Of Double))(3)
Dim v = Vector.Create(New Complex(Of Double)(1, 2),
New Complex(Of Double)(3, 4), New Complex(Of Double)(5, 6))
Dim w = Vector.Create(New Complex(Of Double)(6, 5),
New Complex(Of Double)(4, 3), New Complex(Of Double)(2, 1))
h6.AddOuterProduct(2, v)
h6.AddHermitianOuterProduct(1.0, v, w)
No code example is currently available or this language may not be supported.
let A = Matrix.CreateHermitian<Complex<float>>(3)
let v = Vector.Create(Complex(1., 2.), Complex(3., 4.), Complex(5., 6.))
let w = Vector.Create(Complex(6., 5.), Complex(4., 3.), Complex(2., 1.))
A.AddOuterProduct(Complex(2.,-1.), v) |> ignore
A.AddHermitianOuterProduct(Complex(1.0), v, w) |> ignore
Complementary to this method is the
SubtractOuterProduct method,
which subtracts an outer product.
The GetEigenvalues method
returns a DenseVectorT that contains the
eigenvalues of the matrix. If you also need the eigenvectors, it is more efficient to create a
EigenvalueDecompositionT object
from the matrix and use its
Eigenvalues
and Eigenvectors
properties.
The ApplyMatrixFunction
calculates a matrix function. Its only argument is a FuncT, 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.CreateHermitian(2, new[] {
new Complex<double>(1, 2), Complex<double>.Zero,
new Complex<double>(3, 4), new Complex<double>(5, 6) },
MatrixTriangle.Lower, MatrixElementOrder.RowMajor);
var expS = H.ApplyMatrixFunction(Complex<double>.Exp);
Dim H = Matrix.CreateHermitian(2, {
New Complex(Of Double)(1, 2), Complex(Of Double).Zero,
New Complex(Of Double)(3, 4), New Complex(Of Double)(5, 6)},
MatrixTriangle.Lower, MatrixElementOrder.RowMajor)
Dim expS = H.ApplyMatrixFunction(AddressOf Complex(Of Double).Exp)
No code example is currently available or this language may not be supported.
let H = Matrix.CreateHermitian(2,
[|
Complex(1., 2.) ; Complex.Zero ;
Complex(3., 4.) ; Complex(5., 6.)
|], MatrixTriangle.Lower, MatrixElementOrder.RowMajor)
let expS = H.ApplyMatrixFunction(Func<_,_>(Complex<float>.Exp))