Linear-algebra utilities

The truncated symmetric eigensolvers shared by the spectral clustering and kernel methods. truncated_eigh returns the leading eigenpairs of a symmetric matrix, dispatching between a dense solver and ARPACK’s Lanczos iteration — with eigen_solver="auto" following the same heuristic as scikit-learn’s KernelPCA, since Lanczos only pays off when few eigenpairs of a large matrix are needed.

Truncated symmetric eigensolvers shared by the spectral and kernel methods.

polyview.utils.linalg.resolve_smoother_solver(eigen_solver: str, n: int, total_features: int, k: int) → str

Choose the eigensolver for a sum of ridge smoothers.

‘auto’ selects Lanczos (‘arpack’) when the views have fewer features in total than there are samples: the operator then has rank at most total_features, each product costs O(n * total_features) and the leading eigenvalues are well separated. Otherwise each smoother is close to the identity, the leading eigenvalues cluster and Lanczos needs many products, so forming the (n, n) matrix and calling LAPACK (‘dense’) is faster.

polyview.utils.linalg.smoother_sum_operator(views: Sequence[numpy.ndarray], regs: Sequence[float], scale: float = 1.0) → Tuple[scipy.sparse.linalg.LinearOperator, List[tuple]]

Matrix-free sum of ridge smoothers.

Represents \(M = \sum_v X_v (X_v^T X_v / s + r_v I)^{-1} X_v^T / s\) without forming any (n, n) matrix: each product costs \(O(n \sum_v d_v)\) instead of the \(O(n^2 \sum_v d_v)\) time and \(O(n^2)\) memory needed to build M explicitly.

Parameters:
  • views (sequence of ndarray of shape (n, d_v))

  • regs (sequence of float) – Ridge term \(r_v\) for each view.

  • scale (float, default=1.0) – Covariance normalisation \(s\).

Returns:

  • op (LinearOperator of shape (n, n))

  • factors (list of Cholesky factorisations) – One per view, of \(X_v^T X_v / s + r_v I\), for use with scipy.linalg.cho_solve.

polyview.utils.linalg.truncated_eigh(A: numpy.ndarray | scipy.sparse.linalg.LinearOperator, k: int, *, largest: bool = True, eigen_solver: Literal['auto', 'dense', 'arpack'] = 'auto', random_state=None, v0: numpy.ndarray | None = None) → Tuple[numpy.ndarray, numpy.ndarray]

Compute the k largest or smallest eigenpairs of a symmetric matrix.

Parameters:
  • A (ndarray or LinearOperator of shape (n, n)) – Symmetric matrix. A LinearOperator avoids materialising A when eigen_solver='arpack'.

  • k (int) – Number of eigenpairs to return, 1 <= k <= n.

  • largest (bool, default=True) – Return the largest (True) or smallest (False) algebraic eigenvalues.

  • eigen_solver ({'auto', 'dense', 'arpack'}, default='auto') –

    • ‘dense’ : LAPACK eigh restricted to the requested index range.

    • ’arpack’ : implicitly restarted Lanczos (scipy.sparse.linalg.eigsh), which only needs matrix-vector products. Requires k < n.

    • ’auto’ : ‘arpack’ if n > 200 and k < 10, else ‘dense’.

  • random_state (int, RandomState instance or None, default=None) – Seeds the ARPACK starting vector when v0 is None.

  • v0 (ndarray of shape (n,) or None, default=None) – ARPACK starting vector, e.g. to warm-start from a previous solution.

Returns:

  • eigenvalues (ndarray of shape (k,)) – Sorted from most to least extreme (descending if largest, else ascending).

  • eigenvectors (ndarray of shape (n, k)) – Orthonormal eigenvectors, with signs fixed so that the entry of largest magnitude in each column is positive. This makes the output independent of the solver used.