operatortheory

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 7 Imported by: 0

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:

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

Examples

Constants

This section is empty.

Variables

View Source
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

func FromReal(rows, cols int, data []float64) (*Matrix, error)

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 Identity

func Identity(n int) *Matrix

Identity returns the n-by-n identity operator.

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

func NewMatrix(rows, cols int) *Matrix

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

func OrthogonalProjector(vectors []Vector) (*Matrix, error)

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

func RandomHermitian(n int, seed int64) *Matrix

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

func RandomMatrix(rows, cols int, seed int64) *Matrix

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

func RandomUnitary(n int, seed int64) *Matrix

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

func RealDiagonal(d []float64) *Matrix

RealDiagonal returns the square diagonal matrix with the given real diagonal entries.

func Reflection

func Reflection(v Vector) (*Matrix, error)

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 Zero

func Zero(rows, cols int) *Matrix

Zero returns the rows-by-cols zero matrix. It is a synonym for NewMatrix.

func (*Matrix) Add

func (m *Matrix) Add(b *Matrix) (*Matrix, error)

Add returns m + b. It returns ErrDimensionMismatch on a shape mismatch.

func (*Matrix) Adjoint

func (m *Matrix) Adjoint() *Matrix

Adjoint returns the conjugate transpose (Hermitian adjoint) m^H.

func (*Matrix) AntiCommutator

func (m *Matrix) AntiCommutator(b *Matrix) (*Matrix, error)

AntiCommutator returns the anticommutator {m,b} = m*b + b*m.

func (*Matrix) Apply

func (m *Matrix) Apply(v Vector) Vector

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

func (m *Matrix) CayleyTransform() (*Matrix, error)

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) Clone

func (m *Matrix) Clone() *Matrix

Clone returns an independent copy of the matrix.

func (*Matrix) Col

func (m *Matrix) Col(j int) Vector

Col returns a copy of column j as a Vector.

func (*Matrix) Cols

func (m *Matrix) Cols() int

Cols returns the number of columns.

func (*Matrix) Commutator

func (m *Matrix) Commutator(b *Matrix) (*Matrix, error)

Commutator returns the commutator [m,b] = m*b - b*m. It returns an error if the products are not defined.

func (*Matrix) ConditionNumber

func (m *Matrix) ConditionNumber() float64

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) Conjugate

func (m *Matrix) Conjugate() *Matrix

Conjugate returns the entrywise complex conjugate of m.

func (*Matrix) Cos

func (m *Matrix) Cos() (*Matrix, error)

Cos returns the matrix cosine cos(m) via the functional calculus.

func (*Matrix) Cosh

func (m *Matrix) Cosh() (*Matrix, error)

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

func (m *Matrix) Diag() Vector

Diag returns the main diagonal of the matrix as a Vector of length min(rows,cols).

func (*Matrix) DiagonalizeHermitian

func (m *Matrix) DiagonalizeHermitian() (u, d *Matrix, err error)

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) Dims

func (m *Matrix) Dims() (int, int)

Dims returns the number of rows and columns.

func (*Matrix) DirectSum

func (m *Matrix) DirectSum(b *Matrix) *Matrix

DirectSum returns the block-diagonal matrix diag(m, b).

func (*Matrix) DistanceFrobenius

func (m *Matrix) DistanceFrobenius(b *Matrix) (float64, error)

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

func (m *Matrix) DistanceToSingularity() float64

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

func (m *Matrix) Eigen() ([]Eigenpair, error)

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

func (m *Matrix) Equal(other *Matrix, tol float64) bool

Equal reports whether m and other have the same shape and agree entrywise to within tol.

func (*Matrix) Exp

func (m *Matrix) Exp() (*Matrix, error)

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

func (m *Matrix) FrobeniusNorm() float64

FrobeniusNorm returns the Frobenius norm, the square root of the sum of the squared moduli of the entries.

func (*Matrix) HadamardProduct

func (m *Matrix) HadamardProduct(b *Matrix) (*Matrix, error)

HadamardProduct returns the entrywise (Schur) product of m and b.

func (*Matrix) HermitianEigen

func (m *Matrix) HermitianEigen() (values []float64, vectors *Matrix, err error)

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

func (m *Matrix) HermitianPart() *Matrix

HermitianPart returns (m + m^H)/2, the Hermitian part of a square matrix.

func (*Matrix) Hessenberg

func (m *Matrix) Hessenberg() (h, q *Matrix, err error)

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

func (m *Matrix) ImagPart() *Matrix

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

func (m *Matrix) Inertia(tol float64) (neg, zero, pos int)

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) InfNorm

func (m *Matrix) InfNorm() float64

InfNorm returns the induced infinity-norm, the maximum absolute row sum.

func (*Matrix) Inverse

func (m *Matrix) Inverse() (*Matrix, error)

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

func (m *Matrix) IsContraction(tol float64) bool

IsContraction reports whether the operator norm of m is at most 1 + tol.

func (*Matrix) IsDiagonal

func (m *Matrix) IsDiagonal(tol float64) bool

IsDiagonal reports whether all off-diagonal entries vanish to within tol.

func (*Matrix) IsHermitian

func (m *Matrix) IsHermitian(tol float64) bool

IsHermitian reports whether the matrix equals its conjugate transpose to within tol. A non-positive tol selects a default tolerance.

func (*Matrix) IsIdempotent

func (m *Matrix) IsIdempotent(tol float64) bool

IsIdempotent is a synonym for IsProjection.

func (*Matrix) IsInvolution

func (m *Matrix) IsInvolution(tol float64) bool

IsInvolution reports whether m^2 = I to within tol.

func (*Matrix) IsIsometry

func (m *Matrix) IsIsometry(tol float64) bool

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

func (m *Matrix) IsLowerTriangular(tol float64) bool

IsLowerTriangular reports whether all entries above the main diagonal vanish to within tol.

func (*Matrix) IsNilpotent

func (m *Matrix) IsNilpotent(tol float64) bool

IsNilpotent reports whether some power m^k (k <= n) is the zero matrix to within tol.

func (*Matrix) IsNormal

func (m *Matrix) IsNormal(tol float64) bool

IsNormal reports whether m commutes with its adjoint, m^H m = m m^H, to within tol.

func (*Matrix) IsOrthogonal

func (m *Matrix) IsOrthogonal(tol float64) bool

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

func (m *Matrix) IsOrthogonalProjection(tol float64) bool

IsOrthogonalProjection reports whether m is both Hermitian and idempotent, so that it projects orthogonally onto its range.

func (*Matrix) IsPartialIsometry

func (m *Matrix) IsPartialIsometry(tol float64) bool

IsPartialIsometry reports whether m^H m is an orthogonal projection, the defining property of a partial isometry.

func (*Matrix) IsPositiveDefinite

func (m *Matrix) IsPositiveDefinite(tol float64) bool

IsPositiveDefinite reports whether m is Hermitian with strictly positive eigenvalues, all exceeding tol.

func (*Matrix) IsPositiveSemidefinite

func (m *Matrix) IsPositiveSemidefinite(tol float64) bool

IsPositiveSemidefinite reports whether m is Hermitian with eigenvalues no smaller than -tol.

func (*Matrix) IsProjection

func (m *Matrix) IsProjection(tol float64) bool

IsProjection reports whether m is idempotent, m^2 = m, to within tol.

func (*Matrix) IsSchurStable

func (m *Matrix) IsSchurStable(tol float64) bool

IsSchurStable reports whether every eigenvalue of m lies strictly inside the unit disc (a discrete-time stable operator).

func (*Matrix) IsSelfAdjoint

func (m *Matrix) IsSelfAdjoint(tol float64) bool

IsSelfAdjoint is a synonym for IsHermitian.

func (*Matrix) IsSkewHermitian

func (m *Matrix) IsSkewHermitian(tol float64) bool

IsSkewHermitian reports whether m^H = -m to within tol.

func (*Matrix) IsSquare

func (m *Matrix) IsSquare() bool

IsSquare reports whether the matrix has equal numbers of rows and columns.

func (*Matrix) IsStable

func (m *Matrix) IsStable(tol float64) bool

IsStable reports whether every eigenvalue of m has strictly negative real part (a continuous-time stable, or Hurwitz, operator).

func (*Matrix) IsStrictContraction

func (m *Matrix) IsStrictContraction(tol float64) bool

IsStrictContraction reports whether the operator norm of m is strictly less than 1 - tol.

func (*Matrix) IsSymmetric

func (m *Matrix) IsSymmetric(tol float64) bool

IsSymmetric reports whether m equals its transpose (no conjugation) to within tol.

func (*Matrix) IsUnitary

func (m *Matrix) IsUnitary(tol float64) bool

IsUnitary reports whether m^H m equals the identity to within tol.

func (*Matrix) IsUpperTriangular

func (m *Matrix) IsUpperTriangular(tol float64) bool

IsUpperTriangular reports whether all entries below the main diagonal vanish to within tol.

func (*Matrix) KernelBasis

func (m *Matrix) KernelBasis(tol float64) []Vector

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) Kron

func (m *Matrix) Kron(b *Matrix) *Matrix

Kron returns the Kronecker product m (x) b.

func (*Matrix) KyFanNorm

func (m *Matrix) KyFanNorm(k int) float64

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

func (m *Matrix) Log() (*Matrix, error)

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) MaxAbs

func (m *Matrix) MaxAbs() float64

MaxAbs returns the largest modulus among the entries of m.

func (*Matrix) MaxEigenvalue

func (m *Matrix) MaxEigenvalue() float64

MaxEigenvalue returns the largest eigenvalue of a Hermitian matrix.

func (*Matrix) MaxNorm

func (m *Matrix) MaxNorm() float64

MaxNorm returns the largest modulus among the entries (the entrywise max-norm).

func (*Matrix) MinEigenvalue

func (m *Matrix) MinEigenvalue() float64

MinEigenvalue returns the smallest eigenvalue of a Hermitian matrix.

func (*Matrix) Mul

func (m *Matrix) Mul(b *Matrix) (*Matrix, error)

Mul returns the matrix product m*b. It returns ErrDimensionMismatch when the inner dimensions do not agree.

func (*Matrix) MulVec

func (m *Matrix) MulVec(v Vector) (Vector, error)

MulVec returns the matrix-vector product m*v. It returns ErrDimensionMismatch when len(v) != cols.

func (*Matrix) Neg

func (m *Matrix) Neg() *Matrix

Neg returns -m.

func (*Matrix) NuclearNorm

func (m *Matrix) NuclearNorm() float64

NuclearNorm returns the nuclear (trace) norm, the sum of the singular values.

func (*Matrix) NumericalAbscissa

func (m *Matrix) NumericalAbscissa() float64

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

func (m *Matrix) NumericalRadius(samples int) float64

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) OneNorm

func (m *Matrix) OneNorm() float64

OneNorm returns the induced 1-norm, the maximum absolute column sum.

func (*Matrix) OperatorNorm

func (m *Matrix) OperatorNorm() float64

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

func (m *Matrix) PolarDecomposition() (unitary, positive *Matrix, err error)

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

func (m *Matrix) Power(k int) *Matrix

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

func (m *Matrix) PowerReal(p float64) (*Matrix, error)

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

func (m *Matrix) PseudospectralAbscissa(eps float64, samples int) (float64, error)

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

func (m *Matrix) PseudospectralRadius(eps float64, samples int) (float64, error)

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

func (m *Matrix) QR() (q, r *Matrix)

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

func (m *Matrix) RangeProjector() *Matrix

RangeProjector returns the orthogonal projection onto the column space (range) of m.

func (*Matrix) Rank

func (m *Matrix) Rank(tol float64) int

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

func (m *Matrix) RealPart() *Matrix

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) Row

func (m *Matrix) Row(i int) Vector

Row returns a copy of row i as a Vector.

func (*Matrix) Rows

func (m *Matrix) Rows() int

Rows returns the number of rows.

func (*Matrix) SVD

func (m *Matrix) SVD() (u *Matrix, s []float64, v *Matrix)

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) Scale

func (m *Matrix) Scale(s complex128) *Matrix

Scale returns the matrix s*m.

func (*Matrix) ScaleReal

func (m *Matrix) ScaleReal(s float64) *Matrix

ScaleReal returns the matrix s*m for a real scalar s.

func (*Matrix) SchattenNorm

func (m *Matrix) SchattenNorm(p float64) float64

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

func (m *Matrix) Semigroup(t float64) (*Matrix, error)

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

func (m *Matrix) SetCol(j int, v Vector)

SetCol overwrites column j with the entries of v. It panics on a length mismatch.

func (*Matrix) SetRow

func (m *Matrix) SetRow(i int, v Vector)

SetRow overwrites row i with the entries of v. It panics on a length mismatch.

func (*Matrix) Sign

func (m *Matrix) Sign() (*Matrix, error)

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) Sin

func (m *Matrix) Sin() (*Matrix, error)

Sin returns the matrix sine sin(m) via the functional calculus.

func (*Matrix) SingularValues

func (m *Matrix) SingularValues() []float64

SingularValues returns the singular values of the matrix in descending order.

func (*Matrix) Sinh

func (m *Matrix) Sinh() (*Matrix, error)

Sinh returns the matrix hyperbolic sine sinh(m) via the functional calculus.

func (*Matrix) SkewHermitianPart

func (m *Matrix) SkewHermitianPart() *Matrix

SkewHermitianPart returns (m - m^H)/2, the skew-Hermitian part of a square matrix.

func (*Matrix) SmallestSingularValue

func (m *Matrix) SmallestSingularValue() float64

SmallestSingularValue returns the smallest singular value of the matrix.

func (*Matrix) Solve

func (m *Matrix) Solve(b *Matrix) (*Matrix, error)

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

func (m *Matrix) SolveVec(b Vector) (Vector, error)

SolveVec returns the solution x of the linear system m*x = b for a vector right-hand side.

func (*Matrix) SpectralAbscissa

func (m *Matrix) SpectralAbscissa() float64

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

func (m *Matrix) SpectralGap() float64

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

func (m *Matrix) SpectralNorm() float64

SpectralNorm is an alias for OperatorNorm.

func (*Matrix) SpectralProjector

func (m *Matrix) SpectralProjector(target, tol float64) *Matrix

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

func (m *Matrix) SpectralRadius() float64

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

func (m *Matrix) Sqrt() (*Matrix, error)

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

func (m *Matrix) String() string

String renders the matrix with each entry formatted to three decimal places.

func (*Matrix) Sub

func (m *Matrix) Sub(b *Matrix) (*Matrix, error)

Sub returns m - b. It returns ErrDimensionMismatch on a shape mismatch.

func (*Matrix) Submatrix

func (m *Matrix) Submatrix(r1, r2, c1, c2 int) (*Matrix, error)

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.

func (*Matrix) TraceNorm

func (m *Matrix) TraceNorm() float64

TraceNorm is an alias for NuclearNorm.

func (*Matrix) Transpose

func (m *Matrix) Transpose() *Matrix

Transpose returns the transpose of m (no conjugation).

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

func BasisVector(n, i int) Vector

BasisVector returns the i-th standard basis vector of C^n. It panics if i is out of range.

func GramSchmidt

func GramSchmidt(vectors []Vector, tol float64) []Vector

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 NewVector

func NewVector(n int) Vector

NewVector returns a zero vector of length n. It panics if n is negative.

func VectorFromReal

func VectorFromReal(data []float64) Vector

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) Add

func (v Vector) Add(w Vector) Vector

Add returns v + w. It panics if the lengths differ.

func (Vector) Angle

func (v Vector) Angle(w Vector) float64

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) Clone

func (v Vector) Clone() Vector

Clone returns an independent copy of v.

func (Vector) Conjugate

func (v Vector) Conjugate() Vector

Conjugate returns the entrywise complex conjugate of v.

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

func (v Vector) Equal(w Vector, tol float64) bool

Equal reports whether v and w have the same length and agree entrywise to within tol.

func (Vector) InfNorm

func (v Vector) InfNorm() float64

InfNorm returns the l-infinity norm, the largest modulus among the entries.

func (Vector) IsOrthogonal

func (v Vector) IsOrthogonal(w Vector, tol float64) bool

IsOrthogonal reports whether v and w are orthogonal to within tol, i.e. |<v,w>| <= tol.

func (Vector) IsZero

func (v Vector) IsZero(tol float64) bool

IsZero reports whether every entry of v has modulus at most tol.

func (Vector) Len

func (v Vector) Len() int

Len returns the dimension of the vector.

func (Vector) Neg

func (v Vector) Neg() Vector

Neg returns -v.

func (Vector) Norm

func (v Vector) Norm() float64

Norm returns the Euclidean (l2) norm of v.

func (Vector) NormSquared

func (v Vector) NormSquared() float64

NormSquared returns the squared Euclidean norm <v,v>.

func (Vector) Normalize

func (v Vector) Normalize() (Vector, float64)

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) OneNorm

func (v Vector) OneNorm() float64

OneNorm returns the l1 norm, the sum of the moduli of the entries.

func (Vector) OuterProduct

func (v Vector) OuterProduct(w Vector) *Matrix

OuterProduct returns the rank-one operator v w^H, whose (i,j) entry is v_i conj(w_j).

func (Vector) ProjectOnto

func (v Vector) ProjectOnto(u Vector) Vector

ProjectOnto returns the orthogonal projection of v onto the line spanned by the nonzero vector u, namely (<u,v>/<u,u>) u.

func (Vector) Scale

func (v Vector) Scale(s complex128) Vector

Scale returns the vector s*v.

func (Vector) Sub

func (v Vector) Sub(w Vector) Vector

Sub returns v - w. It panics if the lengths differ.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL