Skip to content

feat: add dispatch rules for pinv and svd - #123

Open
alessandrogentili001 wants to merge 4 commits into
wilson-labs:mainfrom
alessandrogentili001:feature/dispatch-rules-pinv-svd
Open

feat: add dispatch rules for pinv and svd#123
alessandrogentili001 wants to merge 4 commits into
wilson-labs:mainfrom
alessandrogentili001:feature/dispatch-rules-pinv-svd

Conversation

@alessandrogentili001

Copy link
Copy Markdown

Add Dispatch Rules for pinv and svd

Related to #49

Summary

This PR adds specialized dispatch rules for pinv (pseudoinverse) and svd (singular value decomposition) for structured linear operators. These additions follow the existing patterns used by inv, logdet, and trace to improve performance and numerical stability by exploiting operator structure.


Changes

1. pinv Dispatch Rules (cola/linalg/inverse/pinv.py)

  • BlockDiag: $pinv(\text{BlockDiag}(A, B, \dots)) = \text{BlockDiag}(pinv(A), pinv(B), \dots)$
  • Kronecker: $pinv(\text{Kronecker}(A, B)) = \text{Kronecker}(pinv(A), pinv(B))$
  • Diagonal (Fix): Updated to handle zero diagonal entries correctly using a mask to avoid division by zero.

2. svd Dispatch Rules (cola/linalg/svd/svd.py)

  • ScalarMul: Decomposes into phase, magnitude, and identity components.
  • Permutation: Recognizes permutations as unitary (all singular values equal to 1).
  • BlockDiag: $svd(\text{BlockDiag}(A, B)) \to \text{BlockDiag}(U_A, U_B), \text{Diag}(\text{concat}(\sigma_A, \sigma_B)), \text{BlockDiag}(V_A, V_B)$
  • Kronecker: $svd(\text{Kronecker}(A, B)) \to \text{Kronecker}(U_A, U_B), \text{Kronecker}(S_A, S_B), \text{Kronecker}(V_A, V_B)$

3. Backend Additions

  • Added pinv to np_fns.py (np.linalg.pinv) and jax_fns.py (jnp.linalg.pinv) to ensure consistency across all supported backends.

Tests

I have added two new test files containing 18 tests covering all dispatch rules across NumPy, PyTorch, and JAX backends.

Test File Cases Covered Status
tests/linalg/inverse/test_pinv_dispatch.py BlockDiag, Kronecker (3 backends) ✅ 6 passed
tests/linalg/svd/test_svd_dispatch.py ScalarMul, Permutation, BlockDiag, Kronecker (3 backends) ✅ 12 passed

Verification Criteria:

  • Reconstruction Accuracy: Verified that $U \Sigma V^H \approx A$ with a relative error $< 10^{-5}$.
  • Analytical Correctness: Singular values were verified against known analytical solutions where applicable.

@codecov

codecov Bot commented Feb 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.64%. Comparing base (2ddcb3d) to head (b501909).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #123      +/-   ##
==========================================
+ Coverage   83.09%   83.64%   +0.54%     
==========================================
  Files          45       45              
  Lines        3827     3870      +43     
==========================================
+ Hits         3180     3237      +57     
+ Misses        647      633      -14     
Flag Coverage Δ
?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant