subspace_svd.bend checks
raw source on the hub · import bend-quant-risk@0.1.0.0/subspace_svd.bend as Subspace_svd
Fast flat-Array<F32> deterministic subspace-iteration top-K SVD. Exploits K << min(m, n) via QR-stabilized power iterations in O(n_iter * m * n * (k + p)), delegating directly to exact linalg.svd_thin when min(m, n) is small or 2*k >= min(m, n). Internal kernel to risk; public entry points live in risk.bend.
3 imports
import Base import bend-kit-f32array@0.2.1.0/f32array.bend as A import bend-quant-linalg@0.1.0.0/linalg.bend as L
Types
type CMRow source · line 40 · raw
Type
Extract column-major m x q array into row-major List<&2, List<&2, F32>>.
CMRow@a:Array<F32> -> @row:List<&2, F32> -> CMRow
type CMMat source · line 55 · raw
Type
CMMat@a:Array<F32> -> @rows:List<&2, List<&2, F32>> -> CMMat
type DotRMCM source · line 124 · raw
Type
DotRMCM@a:Array<F32> -> @x:Array<F32> -> @acc:F32 -> DotRMCM
type MulSt source · line 143 · raw
Type
MulSt@a:Array<F32> -> @x:Array<F32> -> @y:Array<F32> -> MulSt
type AtQRow source · line 179 · raw
Type
Computes Z = A^T * Q where A is m x n (row-major), Q is m x q (col-major), Z is n x q (col-major). Accumulates row-wise over i = 0..m-1: Z[col*n + j] += A[i*n + j] * Q[col*m + i].
AtQRow@a:Array<F32> -> @z:Array<F32> -> AtQRow
Definitions
def arr_depth.go source · line 11 · raw
@rem:Nat -> @+d:Nat -> @+cap:U32 -> @+n:U32 -> Nat
def arr_depth source · line 19 · raw
@+n:U32 -> Nat
def fill_row_rm source · line 22 · raw
@r:List<&2, F32> -> @a:Array<F32> -> @+idx:U32 -> Array<F32>
def fill_mat_rm source · line 29 · raw
@rows:List<&2, List<&2, F32>> -> @a:Array<F32> -> @+off:U32 -> @+nc:U32 -> Array<F32>
def mat_to_rm source · line 36 · raw
@rows:List<&2, List<&2, F32>> -> @+m:U32 -> @+n:U32 -> Array<F32>
def cm_row.push source · line 43 · raw
@p:Pair(Array<F32>, F32) -> @row:List<&2, F32> -> CMRow
def cm_row.go source · line 47 · raw
@rem:Nat -> @st:CMRow -> @+i:U32 -> @+j:U32 -> @+m:U32 -> CMRow
def cm_mat.push source · line 58 · raw
@st:CMRow -> @rows:List<&2, List<&2, F32>> -> CMMat
def cm_mat.go source · line 62 · raw
@rem:Nat -> @st:CMMat -> @+i:U32 -> @+m:U32 -> @+q:U32 -> @+qn:Nat -> List<&2, List<&2, F32>>
def cm_to_mat source · line 72 · raw
@a:Array<F32> -> @+m:U32 -> @+q:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat
def rm_row.go source · line 76 · raw
@rem:Nat -> @st:CMRow -> @+off:U32 -> CMRow
Extract row-major q x n array into L.Mat.
def rm_mat.go source · line 84 · raw
@rem:Nat -> @st:CMMat -> @+i:U32 -> @+n:U32 -> @+nn:Nat -> List<&2, List<&2, F32>>
def rm_to_mat source · line 95 · raw
@a:Array<F32> -> @+q:U32 -> @+n:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat
def lcg_next source · line 101 · raw
@+s:U32 -> U32
Deterministic LCG in [-1, 1] (pure, zero IO).
def lcg_f32 source · line 104 · raw
@+s:U32 -> F32
def probe.fill source · line 109 · raw
@rem:Nat -> @a:Array<F32> -> @+idx:U32 -> @+seed:U32 -> Array<F32>
def make_probe source · line 117 · raw
@+n:U32 -> @+q:U32 -> Array<F32>
def dot_rmcm.step2 source · line 127 · raw
@px:Pair(Array<F32>, F32) -> @a:Array<F32> -> @+av:F32 -> @+acc:F32 -> DotRMCM
def dot_rmcm.step1 source · line 131 · raw
@pa:Pair(Array<F32>, F32) -> @x:Array<F32> -> @+xi:U32 -> @+acc:F32 -> DotRMCM
def dot_rmcm source · line 135 · raw
@rem:Nat -> @st:DotRMCM -> @+ai:U32 -> @+xi:U32 -> DotRMCM
def mul_ax.set source · line 146 · raw
@st:DotRMCM -> @y:Array<F32> -> @+yi:U32 -> MulSt
def mul_ax.i source · line 151 · raw
@rem:Nat -> @st:MulSt -> @+i:U32 -> @+col:U32 -> @+m:U32 -> @+n:U32 -> @+nn:Nat -> MulSt
Y[i, col] = sum_j A[i*n + j] * X[col*n + j]
def mul_ax.col source · line 160 · raw
@rem:Nat -> @st:MulSt -> @+col:U32 -> @+m:U32 -> @+mm:Nat -> @+n:U32 -> @+nn:Nat -> MulSt
def mul_ax.fin source · line 169 · raw
@st:MulSt -> Pair(Array<F32>, Array<F32>)
Computes Y = A * X where A is m x n (row-major), X is n x q (col-major), Y is m x q (col-major). Returns (A, Y) (consumes X).
def mul_ax source · line 173 · raw
@a:Array<F32> -> @x:Array<F32> -> @+m:U32 -> @+n:U32 -> @+q:U32 -> Pair(Array<F32>, Array<F32>)
def atq.upd source · line 182 · raw
@pz:Pair(Array<F32>, F32) -> @a:Array<F32> -> @+av:F32 -> @+qv:F32 -> @+zi:U32 -> AtQRow
def atq.step_a source · line 186 · raw
@pa:Pair(Array<F32>, F32) -> @z:Array<F32> -> @+qv:F32 -> @+zi:U32 -> AtQRow
def atq.j source · line 190 · raw
@rem:Nat -> @st:AtQRow -> @+ai:U32 -> @+zi:U32 -> @+qv:F32 -> AtQRow
def atq.wrap_col source · line 198 · raw
@st:AtQRow -> @q:Array<F32> -> MulSt
def atq.get_q source · line 202 · raw
@pq:Pair(Array<F32>, F32) -> @a:Array<F32> -> @z:Array<F32> -> @+ai0:U32 -> @+zi0:U32 -> @+nn:Nat -> MulSt
def atq.col source · line 206 · raw
@rem:Nat -> @st:MulSt -> @+i:U32 -> @+col:U32 -> @+m:U32 -> @+n:U32 -> @+nn:Nat -> MulSt
def atq.i source · line 215 · raw
@rem:Nat -> @st:MulSt -> @+i:U32 -> @+m:U32 -> @+n:U32 -> @+nn:Nat -> @+qn:Nat -> MulSt
def mul_atq.fin source · line 222 · raw
@st:MulSt -> Pair(Array<F32>, Array<F32>)
def mul_atq source · line 226 · raw
@a:Array<F32> -> @q:Array<F32> -> @+m:U32 -> @+n:U32 -> @+qcols:U32 -> Pair(Array<F32>, Array<F32>)
def mul_qta.fin source · line 233 · raw
@st:MulSt -> Pair(Array<F32>, Array<F32>)
Computes B = Q^T * A (row-major q x n) while keeping Q intact! Here Z = A^T * Q in col-major n x q IS ALREADY B = Q^T * A in row-major q x n! Because Z[col * n + j] = (A^T Q)[j, col] = (Q^T A)[col, j] = B[col * n + j]!
def mul_qta source · line 237 · raw
@a:Array<F32> -> @q:Array<F32> -> @+m:U32 -> @+n:U32 -> @+qcols:U32 -> Pair(Array<F32>, Array<F32>)
def cm_col_ss.step source · line 245 · raw
@p:Pair(Array<F32>, F32) -> @+acc:F32 -> Pair(Array<F32>, F32)
def cm_col_ss source · line 249 · raw
@rem:Nat -> @st:Pair(Array<F32>, F32) -> @+idx:U32 -> Pair(Array<F32>, F32)
def cm_col_dot.step2 source · line 257 · raw
@p:Pair(Array<F32>, F32) -> @+v1:F32 -> @+acc:F32 -> Pair(Array<F32>, F32)
def cm_col_dot.step1 source · line 261 · raw
@p:Pair(Array<F32>, F32) -> @+i2:U32 -> @+acc:F32 -> Pair(Array<F32>, F32)
def cm_col_dot source · line 265 · raw
@rem:Nat -> @st:Pair(Array<F32>, F32) -> @+i1:U32 -> @+i2:U32 -> Pair(Array<F32>, F32)
def cm_col_axpy.step2 source · line 274 · raw
@p:Pair(Array<F32>, F32) -> @+vj:F32 -> @+d:F32 -> @+ik:U32 -> Array<F32>
col_k -= d * col_j
def cm_col_axpy.step1 source · line 278 · raw
@p:Pair(Array<F32>, F32) -> @+ik:U32 -> @+d:F32 -> Array<F32>
def cm_col_axpy source · line 282 · raw
@rem:Nat -> @a:Array<F32> -> @+ij:U32 -> @+ik:U32 -> @+d:F32 -> Array<F32>
def cm_col_scale.step source · line 290 · raw
@p:Pair(Array<F32>, F32) -> @+ik:U32 -> @+s:F32 -> Array<F32>
col_k *= s
def cm_col_scale source · line 294 · raw
@rem:Nat -> @a:Array<F32> -> @+ik:U32 -> @+s:F32 -> Array<F32>
def mgs.orth_one source · line 302 · raw
@p:Pair(Array<F32>, F32) -> @+joff:U32 -> @+koff:U32 -> @+mm:Nat -> Array<F32>
Single pass orthogonalizing col k against cols 0..k-1.
def mgs.orth_loop source · line 306 · raw
@rem:Nat -> @a:Array<F32> -> @+j:U32 -> @+koff:U32 -> @+m:U32 -> @+mm:Nat -> Array<F32>
def mgs.norm_col source · line 315 · raw
@p:Pair(Array<F32>, F32) -> @+koff:U32 -> @+mm:Nat -> Array<F32>
def mgs.cols source · line 322 · raw
@rem:Nat -> @a:Array<F32> -> @+k:U32 -> @+m:U32 -> @+mm:Nat -> Array<F32>
def orthonormalize_cm source · line 334 · raw
@a:Array<F32> -> @+m:U32 -> @+q:U32 -> Array<F32>
def sub_iter.step2 source · line 339 · raw
@p:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+q:U32 -> Pair(Array<F32>, Array<F32>)
def sub_iter.step1 source · line 343 · raw
@p:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+n:U32 -> @+q:U32 -> Pair(Array<F32>, Array<F32>)
def sub_iter.go source · line 348 · raw
@rem:Nat -> @st:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+n:U32 -> @+q:U32 -> Pair(Array<F32>, Array<F32>)
def take_f32 source · line 358 · raw
@k:Nat -> @xs:List<&2, F32> -> List<&2, F32>
def slice_svd source · line 367 · raw
@svd:0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.pick_u source · line 371 · raw
@mu:Maybe<&2, 0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat> -> @fallback:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @sb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Vec -> @vtb:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.combine1 source · line 378 · raw
@svd:0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD -> @+q_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.combine source · line 382 · raw
@q_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @b_mat:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.finish2 source · line 385 · raw
@p:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+n:U32 -> @+q:U32 -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.finish source · line 389 · raw
@p:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+n:U32 -> @+q:U32 -> @+k:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.tall1 source · line 393 · raw
@p:Pair(Array<F32>, Array<F32>) -> @+m:U32 -> @+n:U32 -> @+q:U32 -> @+k:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.tall source · line 398 · raw
@rows:List<&2, List<&2, F32>> -> @+m:U32 -> @+n:U32 -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.wide_fin source · line 404 · raw
@svd:0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.wide1 source · line 408 · raw
@at:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.wide source · line 412 · raw
@a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_subspace.orient source · line 415 · raw
@is_tall:Bool -> @a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_top_k.branch source · line 423 · raw
@exact:Bool -> @+a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_top_k.zero source · line 431 · raw
@is_zero:Bool -> @+a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
def svd_top_k source · line 447 · raw
@+a:0xa0e1b674ac5289412017a9dd17825c31/linalg.Mat -> @+k:U32 -> @+oversample:U32 -> @+n_iter:U32 -> 0xa0e1b674ac5289412017a9dd17825c31/linalg.SVD
Fast deterministic top-k SVD of a (m x n): a ~= U * diag(s) * Vt, U is m x k, s is length k (descending), Vt is k x n. Uses exact linalg.svd_thin when min(m, n) <= max(k + oversample, 40) or 2*k >= min(m, n), otherwise runs QR/MGS-stabilized subspace iteration in flat Array<F32>.