pinned_leading_columns ====================== .. py:function:: t3toolbox.backend.linalg.pinned_leading_columns(pinned, A, k) .. code-block:: python def pinned_leading_columns( pinned: NDArray, # C+(n, s): orthonormal columns, held fixed A: NDArray, # C+(n, m): the content whose leading directions are wanted k: int, # s + k <= n (structural) ) -> NDArray: # C+(n, k): the leading k left singular directions of (I - P P^T) A, completed against P The pinned truncated SVD: the leading ``k`` left singular directions of ``A`` projected off ``span(pinned)``, completed by :py:func:`pinned_completion` when the projection has rank ``< k``. The count ``k`` is the caller's (structural); nothing is compared with a tolerance. The steps: ``Aperp = A - P (P^T A)``; its thin SVD's left factor (descending); then ``pinned_completion(pinned, U, k)``, whose QR of the orthonormal ``[P | U_k]`` returns ``U_k`` up to column signs. When ``Aperp`` has at least ``k`` nonzero singular values the result spans its leading-``k`` left singular subspace. Otherwise the columns beyond that rank come from the SVD's trailing singular vectors (zero or roundoff-level ``sigma``), orthogonalized against ``P`` and the leading columns by the QR: always a valid orthonormal completion orthogonal to ``P``, deterministic for a given input and backend, but **not determined by** ``A`` -- roundoff-level changes to ``A`` (e.g. its content inside ``span(pinned)``) can rotate those columns by O(1), and backends may differ there. A tie at the cut (``sigma_k = sigma_{k+1}``) returns some basis of the tied subspace, as any truncated SVD does. ``pinned`` is assumed orthonormal (a caveat, not checked). The stack ``C`` is vectorized; ``k = 0`` returns ``C+(n, 0)``; structural errors as :py:func:`pinned_completion`. .. rubric:: Examples A planted leading direction outside ``span(pinned)`` is recovered, whatever ``A`` holds inside it: >>> import numpy as np >>> import t3toolbox.backend.linalg as linalg >>> np.random.seed(0) >>> P, _ = np.linalg.qr(np.random.randn(6, 2)) # pinned: n = 6, s = 2 >>> B = np.linalg.qr(np.concatenate([P, np.random.randn(6, 2)], axis=1))[0] >>> u, w = B[:, 2], B[:, 3] # orthonormal, orthogonal to P >>> A = 100.0 * P @ np.random.randn(2, 4) + 10.0 * np.outer(u, [1, 0, 0, 0]) + np.outer(w, [0, 1, 0, 0]) >>> U = linalg.pinned_leading_columns(P, A, 1) # off span(P): sigma = 10 (u), 1 (w) >>> print(U.shape, bool(abs(U[:, 0] @ u) > 1 - 1e-12)) (6, 1) True When the projected content has rank below ``k`` the rest is completed -- still orthonormal and orthogonal to ``pinned``: >>> A1 = P @ np.random.randn(2, 3) + np.outer(u, np.random.randn(3)) # rank 1 off span(P), k = 3 >>> PU = np.concatenate([P, linalg.pinned_leading_columns(P, A1, 3)], axis=1) >>> print(bool(np.linalg.norm(PU.T @ PU - np.eye(5)) < 1e-14)) True