Documentation
¶
Overview ¶
Package operatortheory implements finite-dimensional linear operator and spectral theory using only the Go standard library.
The central type is Matrix, a dense complex matrix that is interpreted as a bounded linear operator on the finite-dimensional Hilbert space C^n endowed with the standard inner product <x,y> = sum conj(x_i) y_i. On top of the usual matrix arithmetic (addition, multiplication, adjoint, Kronecker product, powers) the package provides the machinery of operator theory:
- Classification predicates: Matrix.IsHermitian, Matrix.IsSkewHermitian, Matrix.IsUnitary, Matrix.IsNormal, Matrix.IsProjection, Matrix.IsPositiveDefinite, Matrix.IsContraction, Matrix.IsIsometry and many more.
- Norms: the Frobenius, induced 1-, 2- (spectral) and infinity norms, the nuclear (trace) norm and the numerical radius.
- Spectral theory: Matrix.Eigenvalues, Matrix.HermitianEigen, Matrix.Spectrum, Matrix.SpectralRadius, the Matrix.Resolvent, the Matrix.NumericalRange and Matrix.NumericalAbscissa.
- Decompositions: Matrix.QR, Matrix.Hessenberg, Matrix.SVD, Matrix.PolarDecomposition and the Hermitian spectral decomposition Matrix.SpectralDecomposition.
- Functional calculus on normal operators: Matrix.ApplyFunction together with the ready-made Matrix.Exp, Matrix.Log, Matrix.Sqrt, Matrix.Sign and trigonometric functions.
- Pseudospectra estimates: Matrix.ResolventNorm, Matrix.PseudospectralAbscissa and Matrix.PseudospectrumGrid.
Numerical methods. Symmetric/Hermitian eigenproblems are solved with the cyclic Jacobi method applied to the real 2n-by-2n symmetric embedding of a Hermitian matrix, which is backward stable and returns a full orthonormal set of eigenvectors. General (non-normal) spectra are computed with the explicitly shifted QR algorithm on the upper-Hessenberg form. The singular value decomposition is obtained from the Hermitian eigendecomposition of the Gram matrix. Every routine depends only on math, math/cmplx and sort.
Randomised constructors (RandomMatrix, RandomHermitian, RandomUnitary) take a caller-supplied seed and use math/rand so that results are fully reproducible.
Index ¶
- Variables
- type Eigenpair
- type Matrix
- func Companion(coeffs []complex128) (*Matrix, error)
- func Diagonal(d []complex128) *Matrix
- func FromReal(rows, cols int, data []float64) (*Matrix, error)
- func FromRows(rows [][]complex128) (*Matrix, error)
- func Identity(n int) *Matrix
- func JordanBlock(lambda complex128, n int) *Matrix
- func NewMatrix(rows, cols int) *Matrix
- func NewMatrixFromData(rows, cols int, data []complex128) (*Matrix, error)
- func OrthogonalProjector(vectors []Vector) (*Matrix, error)
- func RandomHermitian(n int, seed int64) *Matrix
- func RandomMatrix(rows, cols int, seed int64) *Matrix
- func RandomUnitary(n int, seed int64) *Matrix
- func RealDiagonal(d []float64) *Matrix
- func Reflection(v Vector) (*Matrix, error)
- func Zero(rows, cols int) *Matrix
- func (m *Matrix) Add(b *Matrix) (*Matrix, error)
- func (m *Matrix) Adjoint() *Matrix
- func (m *Matrix) AntiCommutator(b *Matrix) (*Matrix, error)
- func (m *Matrix) Apply(v Vector) Vector
- func (m *Matrix) ApplyFunction(f ScalarFunction) (*Matrix, error)
- func (m *Matrix) At(i, j int) complex128
- func (m *Matrix) CayleyTransform() (*Matrix, error)
- func (m *Matrix) CharacteristicPolynomial() ([]complex128, error)
- func (m *Matrix) Clone() *Matrix
- func (m *Matrix) Col(j int) Vector
- func (m *Matrix) Cols() int
- func (m *Matrix) Commutator(b *Matrix) (*Matrix, error)
- func (m *Matrix) ConditionNumber() float64
- func (m *Matrix) Conjugate() *Matrix
- func (m *Matrix) Cos() (*Matrix, error)
- func (m *Matrix) Cosh() (*Matrix, error)
- func (m *Matrix) Determinant() (complex128, error)
- func (m *Matrix) Diag() Vector
- func (m *Matrix) DiagonalizeHermitian() (u, d *Matrix, err error)
- func (m *Matrix) Dims() (int, int)
- func (m *Matrix) DirectSum(b *Matrix) *Matrix
- func (m *Matrix) DistanceFrobenius(b *Matrix) (float64, error)
- func (m *Matrix) DistanceToSingularity() float64
- func (m *Matrix) Eigen() ([]Eigenpair, error)
- func (m *Matrix) EigenvalueMultiplicities(tol float64) (values []complex128, mult []int, err error)
- func (m *Matrix) Eigenvalues() ([]complex128, error)
- func (m *Matrix) Equal(other *Matrix, tol float64) bool
- func (m *Matrix) Exp() (*Matrix, error)
- func (m *Matrix) FrobeniusNorm() float64
- func (m *Matrix) HadamardProduct(b *Matrix) (*Matrix, error)
- func (m *Matrix) HermitianEigen() (values []float64, vectors *Matrix, err error)
- func (m *Matrix) HermitianPart() *Matrix
- func (m *Matrix) Hessenberg() (h, q *Matrix, err error)
- func (m *Matrix) ImagPart() *Matrix
- func (m *Matrix) InPseudospectrum(z complex128, eps float64) bool
- func (m *Matrix) Inertia(tol float64) (neg, zero, pos int)
- func (m *Matrix) InfNorm() float64
- func (m *Matrix) Inverse() (*Matrix, error)
- func (m *Matrix) IsContraction(tol float64) bool
- func (m *Matrix) IsDiagonal(tol float64) bool
- func (m *Matrix) IsHermitian(tol float64) bool
- func (m *Matrix) IsIdempotent(tol float64) bool
- func (m *Matrix) IsInvolution(tol float64) bool
- func (m *Matrix) IsIsometry(tol float64) bool
- func (m *Matrix) IsLowerTriangular(tol float64) bool
- func (m *Matrix) IsNilpotent(tol float64) bool
- func (m *Matrix) IsNormal(tol float64) bool
- func (m *Matrix) IsOrthogonal(tol float64) bool
- func (m *Matrix) IsOrthogonalProjection(tol float64) bool
- func (m *Matrix) IsPartialIsometry(tol float64) bool
- func (m *Matrix) IsPositiveDefinite(tol float64) bool
- func (m *Matrix) IsPositiveSemidefinite(tol float64) bool
- func (m *Matrix) IsProjection(tol float64) bool
- func (m *Matrix) IsSchurStable(tol float64) bool
- func (m *Matrix) IsSelfAdjoint(tol float64) bool
- func (m *Matrix) IsSkewHermitian(tol float64) bool
- func (m *Matrix) IsSquare() bool
- func (m *Matrix) IsStable(tol float64) bool
- func (m *Matrix) IsStrictContraction(tol float64) bool
- func (m *Matrix) IsSymmetric(tol float64) bool
- func (m *Matrix) IsUnitary(tol float64) bool
- func (m *Matrix) IsUpperTriangular(tol float64) bool
- func (m *Matrix) KernelBasis(tol float64) []Vector
- func (m *Matrix) Kron(b *Matrix) *Matrix
- func (m *Matrix) KyFanNorm(k int) float64
- func (m *Matrix) Log() (*Matrix, error)
- func (m *Matrix) MaxAbs() float64
- func (m *Matrix) MaxEigenvalue() float64
- func (m *Matrix) MaxNorm() float64
- func (m *Matrix) MinEigenvalue() float64
- func (m *Matrix) Mul(b *Matrix) (*Matrix, error)
- func (m *Matrix) MulVec(v Vector) (Vector, error)
- func (m *Matrix) Neg() *Matrix
- func (m *Matrix) NuclearNorm() float64
- func (m *Matrix) NumericalAbscissa() float64
- func (m *Matrix) NumericalRadius(samples int) float64
- func (m *Matrix) NumericalRange(samples int) []complex128
- func (m *Matrix) OneNorm() float64
- func (m *Matrix) OperatorNorm() float64
- func (m *Matrix) PolarDecomposition() (unitary, positive *Matrix, err error)
- func (m *Matrix) Power(k int) *Matrix
- func (m *Matrix) PowerReal(p float64) (*Matrix, error)
- func (m *Matrix) PseudospectralAbscissa(eps float64, samples int) (float64, error)
- func (m *Matrix) PseudospectralRadius(eps float64, samples int) (float64, error)
- func (m *Matrix) PseudospectrumGrid(reMin, reMax, imMin, imMax float64, nx, ny int) ([][]float64, error)
- func (m *Matrix) QR() (q, r *Matrix)
- func (m *Matrix) QuadraticForm(x, y Vector) complex128
- func (m *Matrix) RangeProjector() *Matrix
- func (m *Matrix) Rank(tol float64) int
- func (m *Matrix) RayleighQuotient(v Vector) complex128
- func (m *Matrix) RealPart() *Matrix
- func (m *Matrix) Resolvent(z complex128) (*Matrix, error)
- func (m *Matrix) ResolventNorm(z complex128) float64
- func (m *Matrix) Row(i int) Vector
- func (m *Matrix) Rows() int
- func (m *Matrix) SVD() (u *Matrix, s []float64, v *Matrix)
- func (m *Matrix) Scale(s complex128) *Matrix
- func (m *Matrix) ScaleReal(s float64) *Matrix
- func (m *Matrix) SchattenNorm(p float64) float64
- func (m *Matrix) Semigroup(t float64) (*Matrix, error)
- func (m *Matrix) Set(i, j int, v complex128)
- func (m *Matrix) SetCol(j int, v Vector)
- func (m *Matrix) SetRow(i int, v Vector)
- func (m *Matrix) Sign() (*Matrix, error)
- func (m *Matrix) Sin() (*Matrix, error)
- func (m *Matrix) SingularValues() []float64
- func (m *Matrix) Sinh() (*Matrix, error)
- func (m *Matrix) SkewHermitianPart() *Matrix
- func (m *Matrix) SmallestSingularValue() float64
- func (m *Matrix) Solve(b *Matrix) (*Matrix, error)
- func (m *Matrix) SolveVec(b Vector) (Vector, error)
- func (m *Matrix) SpectralAbscissa() float64
- func (m *Matrix) SpectralDecomposition(tol float64) ([]SpectralComponent, error)
- func (m *Matrix) SpectralGap() float64
- func (m *Matrix) SpectralNorm() float64
- func (m *Matrix) SpectralProjector(target, tol float64) *Matrix
- func (m *Matrix) SpectralRadius() float64
- func (m *Matrix) Spectrum() ([]complex128, error)
- func (m *Matrix) Sqrt() (*Matrix, error)
- func (m *Matrix) String() string
- func (m *Matrix) Sub(b *Matrix) (*Matrix, error)
- func (m *Matrix) Submatrix(r1, r2, c1, c2 int) (*Matrix, error)
- func (m *Matrix) Trace() complex128
- func (m *Matrix) TraceNorm() float64
- func (m *Matrix) Transpose() *Matrix
- type ScalarFunction
- type SpectralComponent
- type Vector
- func (v Vector) Add(w Vector) Vector
- func (v Vector) Angle(w Vector) float64
- func (v Vector) Clone() Vector
- func (v Vector) Conjugate() Vector
- func (v Vector) Dot(w Vector) complex128
- func (v Vector) Equal(w Vector, tol float64) bool
- func (v Vector) InfNorm() float64
- func (v Vector) IsOrthogonal(w Vector, tol float64) bool
- func (v Vector) IsZero(tol float64) bool
- func (v Vector) Len() int
- func (v Vector) Neg() Vector
- func (v Vector) Norm() float64
- func (v Vector) NormSquared() float64
- func (v Vector) Normalize() (Vector, float64)
- func (v Vector) OneNorm() float64
- func (v Vector) OuterProduct(w Vector) *Matrix
- func (v Vector) ProjectOnto(u Vector) Vector
- func (v Vector) Scale(s complex128) Vector
- func (v Vector) Sub(w Vector) Vector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDimensionMismatch reports that two operands have incompatible shapes. ErrDimensionMismatch = errors.New("operatortheory: dimension mismatch") // ErrNotSquare reports that a square matrix was required but not provided. ErrNotSquare = errors.New("operatortheory: matrix is not square") // ErrNotHermitian reports that a Hermitian matrix was required but not // provided. ErrNotHermitian = errors.New("operatortheory: matrix is not Hermitian") // ErrNotNormal reports that a normal matrix was required but not provided. ErrNotNormal = errors.New("operatortheory: matrix is not normal") // ErrSingular reports that a matrix is singular (or numerically so). ErrSingular = errors.New("operatortheory: matrix is singular") // ErrEmpty reports that an empty matrix or vector was supplied. ErrEmpty = errors.New("operatortheory: empty input") // ErrOutOfRange reports an index outside the valid range. ErrOutOfRange = errors.New("operatortheory: index out of range") // ErrInvalidArgument reports a value outside its permitted domain. ErrInvalidArgument = errors.New("operatortheory: invalid argument") // ErrNoConvergence reports that an iterative method failed to converge // within its iteration budget. ErrNoConvergence = errors.New("operatortheory: iteration did not converge") )
Sentinel errors returned throughout the package. Callers may test for these with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Eigenpair ¶
type Eigenpair struct {
// Value is the eigenvalue.
Value complex128
// Vector is a unit eigenvector for Value.
Vector Vector
}
Eigenpair bundles an eigenvalue with a corresponding unit eigenvector.
type Matrix ¶
type Matrix struct {
// contains filtered or unexported fields
}
Matrix is a dense complex matrix stored in row-major order. It is interpreted throughout the package as a bounded linear operator on C^n (when square).
func Companion ¶
func Companion(coeffs []complex128) (*Matrix, error)
Companion returns the companion matrix of the monic polynomial whose coefficients (excluding the leading 1) are given from the constant term up to the degree-(n-1) term: p(x) = x^n + c[n-1] x^(n-1) + ... + c[1] x + c[0]. Its eigenvalues are the roots of p. It returns ErrEmpty for no coefficients.
func Diagonal ¶
func Diagonal(d []complex128) *Matrix
Diagonal returns the square diagonal matrix with the given diagonal entries.
func FromReal ¶
FromReal returns a complex matrix whose entries are the given real numbers (in row-major order) with zero imaginary part.
func FromRows ¶
func FromRows(rows [][]complex128) (*Matrix, error)
FromRows builds a matrix from a slice of rows. All rows must have equal length. It returns ErrDimensionMismatch on ragged input and ErrEmpty when no rows are given.
func JordanBlock ¶
func JordanBlock(lambda complex128, n int) *Matrix
JordanBlock returns the n-by-n Jordan block with eigenvalue lambda: lambda on the diagonal and 1 on the first superdiagonal.
func NewMatrix ¶
NewMatrix returns a rows-by-cols zero matrix. It panics if either dimension is negative.
func NewMatrixFromData ¶
func NewMatrixFromData(rows, cols int, data []complex128) (*Matrix, error)
NewMatrixFromData returns a rows-by-cols matrix filled from data in row-major order. It returns ErrDimensionMismatch if len(data) != rows*cols.
func OrthogonalProjector ¶
OrthogonalProjector returns the orthogonal projection operator onto the subspace spanned by the given vectors of common length n. Linearly dependent vectors are handled correctly. It returns ErrEmpty when no vectors are given and ErrDimensionMismatch on unequal lengths.
func RandomHermitian ¶
RandomHermitian returns a random n-by-n Hermitian matrix built as (A + A^H)/2 from a random matrix A seeded by seed.
func RandomMatrix ¶
RandomMatrix returns an m-by-n matrix whose entries have real and imaginary parts drawn independently from the standard normal distribution, using a deterministic generator seeded by seed.
func RandomUnitary ¶
RandomUnitary returns a random n-by-n unitary matrix obtained from the QR factorisation of a random complex matrix seeded by seed. The construction follows the standard recipe that yields a Haar-like distribution.
func RealDiagonal ¶
RealDiagonal returns the square diagonal matrix with the given real diagonal entries.
func Reflection ¶
Reflection returns the Householder reflection I - 2 P, where P is the orthogonal projector onto the line spanned by the nonzero unit-normalisable vector v. The result is a unitary involution.
func (*Matrix) AntiCommutator ¶
AntiCommutator returns the anticommutator {m,b} = m*b + b*m.
func (*Matrix) Apply ¶
Apply returns the image m*v of the vector v under the operator, panicking on a dimension mismatch. It is a convenience wrapper around MulVec.
func (*Matrix) ApplyFunction ¶
func (m *Matrix) ApplyFunction(f ScalarFunction) (*Matrix, error)
ApplyFunction returns f(m) defined by the holomorphic functional calculus for a diagonalisable (in particular, normal) operator: if m = V diag(lambda) V^{-1} then f(m) = V diag(f(lambda)) V^{-1}. Hermitian matrices use the exact unitary eigendecomposition; other matrices use the eigenvectors from inverse iteration. It returns ErrNotSquare for a non-square matrix and ErrNotNormal when the eigenvectors are too ill-conditioned to invert (a defective matrix).
func (*Matrix) At ¶
func (m *Matrix) At(i, j int) complex128
At returns the entry in row i and column j. It panics if the indices are out of range.
func (*Matrix) CayleyTransform ¶
CayleyTransform returns (m - i*I)(m + i*I)^{-1}, which maps a Hermitian operator to a unitary one. It returns ErrSingular if m + i*I is singular.
func (*Matrix) CharacteristicPolynomial ¶
func (m *Matrix) CharacteristicPolynomial() ([]complex128, error)
CharacteristicPolynomial returns the coefficients of the characteristic polynomial det(x*I - m) in ascending order, so the result has length n+1 with a leading coefficient of 1 at index n. It uses the Faddeev-LeVerrier algorithm and returns ErrNotSquare for a non-square matrix.
func (*Matrix) Commutator ¶
Commutator returns the commutator [m,b] = m*b - b*m. It returns an error if the products are not defined.
func (*Matrix) ConditionNumber ¶
ConditionNumber returns the 2-norm condition number, the ratio of the largest to the smallest singular value. It returns +Inf for a singular matrix.
func (*Matrix) Cosh ¶
Cosh returns the matrix hyperbolic cosine cosh(m) via the functional calculus.
func (*Matrix) Determinant ¶
func (m *Matrix) Determinant() (complex128, error)
Determinant returns the determinant of a square matrix. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) Diag ¶
Diag returns the main diagonal of the matrix as a Vector of length min(rows,cols).
func (*Matrix) DiagonalizeHermitian ¶
DiagonalizeHermitian returns a unitary U and a real diagonal matrix D such that m = U*D*U^H, for a Hermitian matrix. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) DistanceFrobenius ¶
DistanceFrobenius returns the Frobenius norm of m - b, a metric on the space of matrices of equal shape. It returns ErrDimensionMismatch on a shape mismatch.
func (*Matrix) DistanceToSingularity ¶
DistanceToSingularity returns the 2-norm distance from a square matrix to the nearest singular matrix, which equals the smallest singular value. It is the backward-error interpretation of the resolvent norm evaluated at 0.
func (*Matrix) Eigen ¶
Eigen returns eigenvalue/eigenvector pairs for a square matrix. Eigenvalues are found with the QR algorithm and eigenvectors by inverse iteration. The eigenvectors are reliable for matrices with well-separated eigenvalues; for Hermitian matrices prefer HermitianEigen. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) EigenvalueMultiplicities ¶
func (m *Matrix) EigenvalueMultiplicities(tol float64) (values []complex128, mult []int, err error)
EigenvalueMultiplicities groups the eigenvalues of a square matrix into clusters that agree to within tol and returns the distinct representatives together with their algebraic multiplicities. Representatives are sorted lexicographically.
func (*Matrix) Eigenvalues ¶
func (m *Matrix) Eigenvalues() ([]complex128, error)
Eigenvalues returns all eigenvalues of a square matrix, counted with multiplicity, computed with the shifted QR algorithm. The order is unspecified. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) Equal ¶
Equal reports whether m and other have the same shape and agree entrywise to within tol.
func (*Matrix) Exp ¶
Exp returns the matrix exponential exp(m) computed with the scaling and squaring method combined with a truncated Taylor series. It is valid for any square matrix. It returns ErrNotSquare for a non-square matrix.
Example ¶
// exp of a 90-degree generator is a quarter-turn rotation.
g, _ := FromReal(2, 2, []float64{0, -math.Pi / 2, math.Pi / 2, 0})
e, _ := g.Exp()
fmt.Printf("%.3f %.3f\n%.3f %.3f\n",
real(e.At(0, 0)), real(e.At(0, 1)),
real(e.At(1, 0)), real(e.At(1, 1)))
Output: 0.000 -1.000 1.000 0.000
func (*Matrix) FrobeniusNorm ¶
FrobeniusNorm returns the Frobenius norm, the square root of the sum of the squared moduli of the entries.
func (*Matrix) HadamardProduct ¶
HadamardProduct returns the entrywise (Schur) product of m and b.
func (*Matrix) HermitianEigen ¶
HermitianEigen returns the real eigenvalues (in ascending order) and the orthonormal eigenvectors (as the columns of the returned matrix) of a Hermitian matrix. The matrix is not required to be exactly Hermitian; its Hermitian part is used, so callers should ensure the input truly is Hermitian for meaningful results. It returns ErrNotSquare for a non-square matrix.
Example ¶
// The Hermitian matrix [[2, i], [-i, 2]] has eigenvalues 1 and 3.
m, _ := NewMatrixFromData(2, 2, []complex128{
2, complex(0, 1),
complex(0, -1), 2,
})
vals, _, _ := m.HermitianEigen()
fmt.Printf("%.4f %.4f\n", vals[0], vals[1])
Output: 1.0000 3.0000
func (*Matrix) HermitianPart ¶
HermitianPart returns (m + m^H)/2, the Hermitian part of a square matrix.
func (*Matrix) Hessenberg ¶
Hessenberg returns the upper-Hessenberg reduction H together with the unitary Q such that m = Q*H*Q^H. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) ImagPart ¶
ImagPart returns the entrywise imaginary part of m as a real-valued complex matrix.
func (*Matrix) InPseudospectrum ¶
func (m *Matrix) InPseudospectrum(z complex128, eps float64) bool
InPseudospectrum reports whether z lies in the eps-pseudospectrum of m, i.e. whether ResolventNorm(z) >= 1/eps. It requires eps > 0.
func (*Matrix) Inertia ¶
Inertia returns the numbers of negative, zero and positive eigenvalues of a Hermitian matrix, using tol to decide which eigenvalues count as zero.
func (*Matrix) Inverse ¶
Inverse returns the inverse of a square matrix. It returns ErrNotSquare for a non-square matrix and ErrSingular when the matrix is (numerically) singular.
func (*Matrix) IsContraction ¶
IsContraction reports whether the operator norm of m is at most 1 + tol.
func (*Matrix) IsDiagonal ¶
IsDiagonal reports whether all off-diagonal entries vanish to within tol.
func (*Matrix) IsHermitian ¶
IsHermitian reports whether the matrix equals its conjugate transpose to within tol. A non-positive tol selects a default tolerance.
func (*Matrix) IsIdempotent ¶
IsIdempotent is a synonym for IsProjection.
func (*Matrix) IsInvolution ¶
IsInvolution reports whether m^2 = I to within tol.
func (*Matrix) IsIsometry ¶
IsIsometry reports whether m preserves norms, i.e. m^H m = I. For square matrices this coincides with unitarity; for tall matrices it means the columns are orthonormal.
func (*Matrix) IsLowerTriangular ¶
IsLowerTriangular reports whether all entries above the main diagonal vanish to within tol.
func (*Matrix) IsNilpotent ¶
IsNilpotent reports whether some power m^k (k <= n) is the zero matrix to within tol.
func (*Matrix) IsNormal ¶
IsNormal reports whether m commutes with its adjoint, m^H m = m m^H, to within tol.
func (*Matrix) IsOrthogonal ¶
IsOrthogonal reports whether a real matrix satisfies m^T m = I to within tol. If the matrix has non-negligible imaginary part it is not orthogonal in this sense.
func (*Matrix) IsOrthogonalProjection ¶
IsOrthogonalProjection reports whether m is both Hermitian and idempotent, so that it projects orthogonally onto its range.
func (*Matrix) IsPartialIsometry ¶
IsPartialIsometry reports whether m^H m is an orthogonal projection, the defining property of a partial isometry.
func (*Matrix) IsPositiveDefinite ¶
IsPositiveDefinite reports whether m is Hermitian with strictly positive eigenvalues, all exceeding tol.
func (*Matrix) IsPositiveSemidefinite ¶
IsPositiveSemidefinite reports whether m is Hermitian with eigenvalues no smaller than -tol.
func (*Matrix) IsProjection ¶
IsProjection reports whether m is idempotent, m^2 = m, to within tol.
func (*Matrix) IsSchurStable ¶
IsSchurStable reports whether every eigenvalue of m lies strictly inside the unit disc (a discrete-time stable operator).
func (*Matrix) IsSelfAdjoint ¶
IsSelfAdjoint is a synonym for IsHermitian.
func (*Matrix) IsSkewHermitian ¶
IsSkewHermitian reports whether m^H = -m to within tol.
func (*Matrix) IsSquare ¶
IsSquare reports whether the matrix has equal numbers of rows and columns.
func (*Matrix) IsStable ¶
IsStable reports whether every eigenvalue of m has strictly negative real part (a continuous-time stable, or Hurwitz, operator).
func (*Matrix) IsStrictContraction ¶
IsStrictContraction reports whether the operator norm of m is strictly less than 1 - tol.
func (*Matrix) IsSymmetric ¶
IsSymmetric reports whether m equals its transpose (no conjugation) to within tol.
func (*Matrix) IsUpperTriangular ¶
IsUpperTriangular reports whether all entries below the main diagonal vanish to within tol.
func (*Matrix) KernelBasis ¶
KernelBasis returns an orthonormal basis for the null space (kernel) of a square matrix, obtained from the right singular vectors with negligible singular values. tol sets the relative threshold below which a singular value is treated as zero.
func (*Matrix) KyFanNorm ¶
KyFanNorm returns the Ky Fan k-norm, the sum of the k largest singular values. If k exceeds the number of singular values it uses all of them.
func (*Matrix) Log ¶
Log returns a matrix logarithm of m via the functional calculus, so that exp(Log(m)) = m for a diagonalisable m with eigenvalues off the negative real axis. It returns an error for a non-square or defective matrix.
func (*Matrix) MaxEigenvalue ¶
MaxEigenvalue returns the largest eigenvalue of a Hermitian matrix.
func (*Matrix) MaxNorm ¶
MaxNorm returns the largest modulus among the entries (the entrywise max-norm).
func (*Matrix) MinEigenvalue ¶
MinEigenvalue returns the smallest eigenvalue of a Hermitian matrix.
func (*Matrix) Mul ¶
Mul returns the matrix product m*b. It returns ErrDimensionMismatch when the inner dimensions do not agree.
func (*Matrix) MulVec ¶
MulVec returns the matrix-vector product m*v. It returns ErrDimensionMismatch when len(v) != cols.
func (*Matrix) NuclearNorm ¶
NuclearNorm returns the nuclear (trace) norm, the sum of the singular values.
func (*Matrix) NumericalAbscissa ¶
NumericalAbscissa returns the maximum real part of the numerical range, equal to the largest eigenvalue of the Hermitian part (m + m^H)/2. It bounds the initial growth rate of exp(tA).
func (*Matrix) NumericalRadius ¶
NumericalRadius returns the numerical radius w(m) = max { |z| : z in W(m) }, estimated by sampling the field of values at the given number of angles.
func (*Matrix) NumericalRange ¶
func (m *Matrix) NumericalRange(samples int) []complex128
NumericalRange returns a convex polygon approximating the boundary of the numerical range (field of values) W(m) = { <x, m x> : ||x|| = 1 }, sampled at the given number of angles (a minimum of 8 is used). Consecutive points trace the boundary counter-clockwise.
func (*Matrix) OperatorNorm ¶
OperatorNorm returns the induced 2-norm (spectral norm), the largest singular value. It is computed by power iteration on m^H m for efficiency.
func (*Matrix) PolarDecomposition ¶
PolarDecomposition returns the polar factors of a square matrix, U unitary (or a partial isometry when singular) and P Hermitian positive semidefinite, such that m = U*P. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) Power ¶
Power returns m raised to the non-negative integer power k using binary exponentiation. Power(0) is the identity. It panics if m is not square or k is negative.
func (*Matrix) PowerReal ¶
PowerReal returns m raised to a real power p via the functional calculus, exp(p*Log(m)). For a Hermitian positive definite matrix and integer p it agrees with the ordinary matrix power.
func (*Matrix) PseudospectralAbscissa ¶
PseudospectralAbscissa estimates the eps-pseudospectral abscissa, the maximum real part of any point in the eps-pseudospectrum. It scans a grid around the spectrum enlarged by a margin proportional to eps and refines the rightmost crossing. It requires eps > 0 and returns ErrInvalidArgument otherwise.
func (*Matrix) PseudospectralRadius ¶
PseudospectralRadius estimates the eps-pseudospectral radius, the largest modulus of any point in the eps-pseudospectrum, by scanning a grid enclosing the spectrum enlarged by a margin proportional to eps.
func (*Matrix) PseudospectrumGrid ¶
func (m *Matrix) PseudospectrumGrid(reMin, reMax, imMin, imMax float64, nx, ny int) ([][]float64, error)
PseudospectrumGrid evaluates the resolvent norm on a rectangular grid in the complex plane spanning [reMin,reMax] x [imMin,imMax] with nx columns and ny rows. The result is indexed as grid[row][col], where row 0 corresponds to imMin. It returns ErrInvalidArgument for non-positive grid sizes.
func (*Matrix) QR ¶
QR returns a reduced QR factorisation m = Q*R computed with complex Householder reflections. Q has orthonormal columns and R is upper triangular.
func (*Matrix) QuadraticForm ¶
func (m *Matrix) QuadraticForm(x, y Vector) complex128
QuadraticForm returns the sesquilinear form <x, m y>.
func (*Matrix) RangeProjector ¶
RangeProjector returns the orthogonal projection onto the column space (range) of m.
func (*Matrix) Rank ¶
Rank returns the numerical rank of the matrix, the number of singular values exceeding tol * (largest singular value). If tol <= 0 a default relative tolerance is used.
func (*Matrix) RayleighQuotient ¶
func (m *Matrix) RayleighQuotient(v Vector) complex128
RayleighQuotient returns <v, m v> / <v, v> for a nonzero vector v. For a Hermitian operator this is real and lies between the smallest and largest eigenvalues.
func (*Matrix) RealPart ¶
RealPart returns the entrywise real part of m as a complex matrix with zero imaginary part.
func (*Matrix) Resolvent ¶
func (m *Matrix) Resolvent(z complex128) (*Matrix, error)
Resolvent returns the resolvent operator (z*I - m)^{-1} at the complex point z. It returns ErrSingular when z is (numerically) an eigenvalue.
func (*Matrix) ResolventNorm ¶
func (m *Matrix) ResolventNorm(z complex128) float64
ResolventNorm returns the spectral norm of the resolvent at z, ||(z*I - m)^{-1}||_2 = 1 / sigma_min(z*I - m). It returns +Inf when z is (very close to) an eigenvalue. This quantity defines the pseudospectra: z belongs to the eps-pseudospectrum precisely when ResolventNorm(z) >= 1/eps.
func (*Matrix) SVD ¶
SVD returns a reduced singular value decomposition m = U * diag(s) * V^H, where U has orthonormal columns (m-by-k), s holds the k = min(rows,cols) singular values in descending order, and V has orthonormal columns (cols-by-k).
func (*Matrix) SchattenNorm ¶
SchattenNorm returns the Schatten p-norm, the l^p norm of the vector of singular values, for p >= 1. SchattenNorm(1) is the nuclear norm, SchattenNorm(2) the Frobenius norm and the limit p -> infinity the operator norm.
func (*Matrix) Semigroup ¶
Semigroup returns exp(t*m), the value at time t of the one-parameter operator semigroup generated by m.
func (*Matrix) Set ¶
func (m *Matrix) Set(i, j int, v complex128)
Set stores v in row i and column j. It panics if the indices are out of range.
func (*Matrix) SetCol ¶
SetCol overwrites column j with the entries of v. It panics on a length mismatch.
func (*Matrix) SetRow ¶
SetRow overwrites row i with the entries of v. It panics on a length mismatch.
func (*Matrix) Sign ¶
Sign returns the matrix sign function, mapping each eigenvalue lambda to sign(Re lambda) (and 0 when Re lambda is 0). For a Hermitian matrix this is the difference of the projectors onto the positive and negative eigenspaces.
func (*Matrix) SingularValues ¶
SingularValues returns the singular values of the matrix in descending order.
func (*Matrix) SkewHermitianPart ¶
SkewHermitianPart returns (m - m^H)/2, the skew-Hermitian part of a square matrix.
func (*Matrix) SmallestSingularValue ¶
SmallestSingularValue returns the smallest singular value of the matrix.
func (*Matrix) Solve ¶
Solve returns the solution X of the linear system m*X = b for a matrix right-hand side. It returns ErrNotSquare, ErrDimensionMismatch or ErrSingular as appropriate.
func (*Matrix) SolveVec ¶
SolveVec returns the solution x of the linear system m*x = b for a vector right-hand side.
func (*Matrix) SpectralAbscissa ¶
SpectralAbscissa returns the maximum real part among the eigenvalues, which governs the growth rate of the operator semigroup exp(tA).
func (*Matrix) SpectralDecomposition ¶
func (m *Matrix) SpectralDecomposition(tol float64) ([]SpectralComponent, error)
SpectralDecomposition returns the spectral resolution of a Hermitian matrix: one SpectralComponent per distinct eigenvalue (eigenvalues within tol are merged), sorted in ascending order. The projectors sum to the identity and m = sum lambda_k P_k. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) SpectralGap ¶
SpectralGap returns the difference between the two smallest distinct eigenvalues of a Hermitian matrix (the gap above the ground state). If there are fewer than two distinct eigenvalues it returns 0.
func (*Matrix) SpectralNorm ¶
SpectralNorm is an alias for OperatorNorm.
func (*Matrix) SpectralProjector ¶
SpectralProjector returns the orthogonal projector onto the eigenspace of a Hermitian matrix associated with the eigenvalue closest to target, provided it lies within tol; otherwise it returns the zero matrix. It is the operator obtained from functional calculus with the indicator of {target}.
func (*Matrix) SpectralRadius ¶
SpectralRadius returns the largest modulus among the eigenvalues. It returns 0 for a non-square or empty matrix.
Example ¶
// The rotation-by-scaling matrix has spectral radius equal to the scale.
m, _ := FromReal(2, 2, []float64{0, -2, 2, 0})
fmt.Printf("%.1f\n", m.SpectralRadius())
Output: 2.0
func (*Matrix) Spectrum ¶
func (m *Matrix) Spectrum() ([]complex128, error)
Spectrum returns the eigenvalues of a square matrix sorted lexicographically by real part and then imaginary part. It returns ErrNotSquare for a non-square matrix.
func (*Matrix) Sqrt ¶
Sqrt returns the principal square root of m via the functional calculus, so that Sqrt(m)^2 = m for a diagonalisable m. For a Hermitian positive semidefinite matrix this is the unique positive semidefinite square root.
func (*Matrix) String ¶
String renders the matrix with each entry formatted to three decimal places.
func (*Matrix) Submatrix ¶
Submatrix returns the r1..r2-1 by c1..c2-1 block of m. It returns ErrOutOfRange for an invalid range.
func (*Matrix) Trace ¶
func (m *Matrix) Trace() complex128
Trace returns the sum of the diagonal entries. It panics if the matrix is not square.
type ScalarFunction ¶
type ScalarFunction func(complex128) complex128
ScalarFunction is a complex-valued function of a complex variable, the kind of analytic function to which the functional calculus applies.
type SpectralComponent ¶
type SpectralComponent struct {
// Eigenvalue is the (real) eigenvalue.
Eigenvalue float64
// Multiplicity is the dimension of the eigenspace.
Multiplicity int
// Projector is the orthogonal projection onto the eigenspace.
Projector *Matrix
}
SpectralComponent bundles a (real) eigenvalue of a Hermitian operator with the orthogonal projector onto its eigenspace.
type Vector ¶
type Vector []complex128
Vector is a finite-dimensional complex vector, an element of the Hilbert space C^n with inner product <x,y> = sum conj(x_i) y_i.
func BasisVector ¶
BasisVector returns the i-th standard basis vector of C^n. It panics if i is out of range.
func GramSchmidt ¶
GramSchmidt orthonormalises the supplied vectors using the modified Gram-Schmidt process with the Hermitian inner product. Vectors that are (numerically) linearly dependent on the earlier ones are dropped, so the result is an orthonormal basis for the span of the inputs.
func VectorFromReal ¶
VectorFromReal returns a complex vector whose entries are the given real numbers with zero imaginary part.
func VectorOf ¶
func VectorOf(entries ...complex128) Vector
VectorOf returns a Vector containing the supplied entries.
func (Vector) Angle ¶
Angle returns the angle in radians between the real directions of v and w, computed from |<v,w>| / (||v|| ||w||). It lies in [0, pi/2].
func (Vector) Dot ¶
func (v Vector) Dot(w Vector) complex128
Dot returns the Hermitian inner product <v,w> = sum conj(v_i) w_i. It is conjugate-linear in its first argument and linear in the second. It panics if the lengths differ.
func (Vector) Equal ¶
Equal reports whether v and w have the same length and agree entrywise to within tol.
func (Vector) IsOrthogonal ¶
IsOrthogonal reports whether v and w are orthogonal to within tol, i.e. |<v,w>| <= tol.
func (Vector) NormSquared ¶
NormSquared returns the squared Euclidean norm <v,v>.
func (Vector) Normalize ¶
Normalize returns a unit vector in the direction of v together with the original norm. If v is the zero vector it returns a copy of v and a zero norm.
func (Vector) OuterProduct ¶
OuterProduct returns the rank-one operator v w^H, whose (i,j) entry is v_i conj(w_j).
func (Vector) ProjectOnto ¶
ProjectOnto returns the orthogonal projection of v onto the line spanned by the nonzero vector u, namely (<u,v>/<u,u>) u.