Compute and interpret a spectrum
Choose a dense route when the matrix fits in memory and all or many spectral components are needed. Choose a partial operator route when only a few values are needed and matrix application is cheaper than materialization.
import gale.linalg.*import gale.spectral.*Solve a dense symmetric eigenproblem
Section titled “Solve a dense symmetric eigenproblem”The dense symmetric facade reads the lower triangle and returns eigenvalues in ascending algebraic order:
val symmetric = Matrix(3, 3)( 2.0, -1.0, 0.0, -1.0, 2.0, -1.0, 0.0, -1.0, 2.0)
val eigen = Eigen .eigSymmetric( symmetric, EigenSelection.All, EigenVectors.Right ) .orThrow// eigen: EigenDecomposition = EigenDecomposition(// eigenvalues = gale.linalg.DVec@11d9d541,// eigenvectors = gale.linalg.DMat@68020441,// diagnostics = SpectralDiagnostics(3,3,gale.linalg.DVec@3ce46aed,<deferred>,0,None,true,None)// )
( eigen.eigenvalues.toSeq, eigen.diagnostics.converged, eigen.diagnostics.worstResidual)// res0: Tuple3[Seq[Double], Int, Double] = (// Vector(0.5857864376269051, 2.0, 3.414213562373095),// 3,// 5.926969055564841E-16// )Signs of individual eigenvectors and bases inside repeated eigenspaces are not identity guarantees. Check residuals, orthogonality, and invariant subspaces rather than comparing raw vector columns.
Compute an economy SVD
Section titled “Compute an economy SVD”For an m x n dense matrix, the full facade returns economy factors with
k = min(m, n): U is m x k and V.t is k x n.
val rectangular = Matrix(4, 2)( 1.0, 0.0, 1.0, 1.0, 1.0, 2.0, 1.0, 3.0)val svd = rectangular.svd.orThrow// svd: SVD = SVD(// singularValues = gale.linalg.DVec@48ff314f,// u = gale.linalg.DMat@4150357,// vt = gale.linalg.DMat@5321206a,// rank = 2,// diagnostics = SpectralDiagnostics(2,2,<deferred>,<deferred>,0,Some(2),true,None)// )
( svd.singularValues.toSeq, (svd.u.rows, svd.u.cols), (svd.vt.rows, svd.vt.cols))// res1: Tuple3[Seq[Double], Tuple2[Int, Int], Tuple2[Int, Int]] = (// Vector(4.100030448168241, 1.0907567666961069),// (4, 2),// (2, 2)// )Svds.svd supplies partial singular values and an operator overload when a
transpose action is available. Singular values are returned in descending
order.
Request a few eigenpairs and check the result
Section titled “Request a few eigenpairs and check the result”Passing an operator and its dimension selects the iterative overload. Here only the largest eigenvalue of the earlier symmetric matrix is requested:
val partial = Eigen.eigSymmetric( symmetric, symmetric.rows, EigenSelection.Count(1, EigenOrder.LargestAlgebraic)).orThrow// partial: EigenDecomposition = EigenDecomposition(// eigenvalues = gale.linalg.DVec@43ed4fd6,// eigenvectors = gale.linalg.DMat@54cbf841,// diagnostics = SpectralDiagnostics(1,1,gale.linalg.DVec@6ea7fcf,4.440892098500626E-16,1,None,true,None)// )
val complete = partial.requireConverged.orThrow// complete: EigenDecomposition = EigenDecomposition(// eigenvalues = gale.linalg.DVec@43ed4fd6,// eigenvectors = gale.linalg.DMat@54cbf841,// diagnostics = SpectralDiagnostics(1,1,gale.linalg.DVec@6ea7fcf,4.440892098500626E-16,1,None,true,None)// )assert(complete.eigenvalues.length == 1)assert(math.abs(complete.eigenvalues(0) - (2.0 + math.sqrt(2.0))) < 1e-10)(partial.diagnostics.converged, partial.diagnostics.worstResidual)// res4: Tuple2[Int, Double] = (1, 7.953907621355592E-16)An iterative run can return Right with fewer converged pairs than requested.
requireConverged checks completeness and returns a typed failure when pairs
are missing. Keep that Either in application code when partial output is
unacceptable; .orThrow above makes a failed tutorial expectation explicit.
Completeness does not always prove that the solver found the requested end of
the spectrum. A vector can satisfy A * v = lambda * v accurately while its
lambda is not the largest eigenvalue. An iterative method may explore a
subspace that misses that direction. Such an approximate eigenpair is called
a Ritz pair.
If the application also needs a certificate that the result belongs to the requested spectral end, use the stricter check:
val globallyCertified = partial.requireExtremeCertified// globallyCertified: Either[LinAlgError, EigenDecomposition] = Right(// EigenDecomposition(// eigenvalues = gale.linalg.DVec@43ed4fd6,// eigenvectors = gale.linalg.DMat@54cbf841,// diagnostics = SpectralDiagnostics(1,1,gale.linalg.DVec@6ea7fcf,4.440892098500626E-16,1,None,true,None)// )// )globallyCertified.isRight// res5: Boolean = trueThis check may reject residual-converged results. It does not rerun the solver or improve the approximation; it checks the evidence already in the result. Do not discard that failure if global extremality is required by your algorithm.
Choose the generalized route
Section titled “Choose the generalized route”For a dense symmetric-definite problem A x = lambda B x, use the dense
Eigen.eigSymmetricGeneralized facade when the matrices fit and a full dense
decomposition is acceptable.
For a few eigenpairs from operators, use LOBPCG when you can supply operator
applications and an optional preconditioner. Use generalized block Lanczos
when you can supply a genuine metric solve for B. The complete typed
selection, metric, convergence, and work-accounting contract is in
Matrix-free generalized symmetric eigensolving.
Read the Numerical contract before relying on ordering, tolerance, backend equivalence, or deterministic layout choices.