Skip to content

Add SVD algorithm kwarg to nullspace#1576

Open
trevorgloe wants to merge 5 commits into
JuliaLang:masterfrom
trevorgloe:tgl/add_svd_alg_to_nullsp
Open

Add SVD algorithm kwarg to nullspace#1576
trevorgloe wants to merge 5 commits into
JuliaLang:masterfrom
trevorgloe:tgl/add_svd_alg_to_nullsp

Conversation

@trevorgloe

Copy link
Copy Markdown
Contributor

Fixes #1571

Now you can call nullspace with an alg argument, which is passed through to svd.

A = [1 1 1 0; 1 1 1 0; 0 0 1 1; 0 1 0 1]
qr_null = nullspace(A, alg=LinearAlgebra.QRIteration())
div_null = nullspace(A, alg=LinearAlgebra.DivideAndConquer())

@codecov

codecov Bot commented Apr 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.38%. Comparing base (beff324) to head (a693cff).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1576   +/-   ##
=======================================
  Coverage   94.37%   94.38%           
=======================================
  Files          35       35           
  Lines       16064    16069    +5     
=======================================
+ Hits        15161    15166    +5     
  Misses        903      903           

☔ View full report in Codecov by Harness.
📢 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.

Comment thread src/dense.jl

The desired algorithm, `alg` is passed through to `svd`. The available algorithms will
be the same as that of ['svd'](@ref).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a compat entry here, saying something like "the keyword argument alg requires Julia version 1.14" or something like that. There are examples in this or other files.

@stevengj

stevengj commented Apr 21, 2026

Copy link
Copy Markdown
Member

I can imagine an alg argument where it doesn't use SVD at all, but instead uses a pivoted QR factorization. (See also #855 and #1052).

@stevengj

stevengj commented Apr 27, 2026

Copy link
Copy Markdown
Member

Wouldn't a cleaner and more powerful API be to just talk an existing SVD object as an argument to nullspace, instead of the matrix?

That way, the caller could construct the SVD however they wanted, and it would also enable an existing SVD to be re-used.

Code-wise, it would just require a bit of refactoring of the existing method.

@trevorgloe

Copy link
Copy Markdown
Contributor Author

Wouldn't a cleaner and more powerful API be to just talk an existing SVD object as an argument to nullspace, instead of the matrix?

That way, the caller could construct the SVD however they wanted, and it would also enable an existing SVD to be re-used.

Code-wise, it would just require a bit of refactoring of the existing method.

I was actually thinking the same thing. It seems a bit inefficient to compute the whole SVD here if the user may already have one. I will try my hand at making two methods: one for taking in an actual matrix (at way something like a pivoted QR can be used rather than an SVD), and one taking in an SVD object.
This would also keep the change from breaking existing uses of the function.

@trevorgloe

Copy link
Copy Markdown
Contributor Author

Ok, I've fixed the test that seemed to be giving a problem. The test was to find the nullspace of

A1 = [1.0 1.0; 2.0 0.0; -1.0 1.0] * [1.0 1.0 1.0; 1.0 -1.0 1.0]

and compare it to [-1/sqrt(2); 0; 1/sqrt(2)], which should be the nullspace (at least theoretically). For some reason this test would only fail on the macos-aarch64 test suite. Anybody know why this would happen?

@stevengj

stevengj commented May 24, 2026

Copy link
Copy Markdown
Member

For some reason this test would only fail on the macos-aarch64 test suite. Anybody know why this would happen?

The nullspace basis is only defined up to an arbitrary rotation. In this case, for a 1d nullspace, the sign is arbitrary, and on MacOS it flips the sign, and returns a vector ≈ [1/√2, 0, -1/√2]

@stevengj stevengj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing through an alg parameter, just refactor to add a method where you can pass an SVD object directly.

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.

Add SVD-algorithm kwarg to nullspace

4 participants