Skip to content

Direct L2 norm computation with vector inner product - #514

Open
anushkasinghh wants to merge 16 commits into
develfrom
new-refactor-to-array
Open

anushkasinghh wants to merge 16 commits into
develfrom
new-refactor-to-array

Conversation

@anushkasinghh

@anushkasinghh anushkasinghh commented Jul 22, 2025 •

Copy link
Copy Markdown
Contributor

Compute the 2-norm of PSYDAC vectors in the unit tests with Vector.inner, instead of converting the vectors to NumPy arrays with toarray (fixes #492). No np.linalg.norm(... .toarray()) is left in the library.

Changes:

  • Add the helper norm2_sqr(v) to psydac/linalg/tests/utilities.py. It returns v.inner(v).real, which is also correct for complex vectors. The tests compare it with the squared tolerance, which avoids a square root.
  • Use norm2_sqr(x) instead of np.linalg.norm(x.toarray()) in test_linalg.py, test_matrix_free.py, test_kron_direct_solver.py, and `test_solvers.py.
  • In test_operator_evaluation, check the results of S_ILO and S_ILO**2 (and of B_ILO and B_ILO**2). Previously the test checked NumPy's np.linalg.solve instead. The error bound for the double inverse is (1 + ||S||) * tol <= 4 * tol, where ||S|| < 3 follows from the Gershgorin theorem.
  • Remove the print statements and the unused imports in test_matrix_free.py.

Outside test_operator_evaluation, the element-wise comparisons with np.allclose and np.array_equal are not changed.

@anushkasinghh anushkasinghh changed the title migrate changes from pr502 Refactor to array (migrate changes from pr502) Jul 23, 2025
@anushkasinghh
anushkasinghh marked this pull request as ready for review July 23, 2025 12:18
@yguclu

yguclu commented Aug 5, 2025 •

Copy link
Copy Markdown
Member

@anushkasinghh If this PR is ready, you should request potential reviewers. In general you can choose psydac-dev and all developers in this team will be notified.

@anushkasinghh
anushkasinghh requested review from a team, campospinto and yguclu August 12, 2025 13:32

@campospinto campospinto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The changes look good to me, but I don't find the PR title very clear. Instead of describing the changes as a refactor of to array, you should mention the direct computation of array l2 norms using psydac inner products avoiding conversions to numpy arrays. I guess that the point is to save time, did you observe some gain ?

@anushkasinghh

Copy link
Copy Markdown
Contributor Author

I ran the tests before and after the change, and the times are almost the same — around 170 seconds in both cases. The small differences I saw (a few tenths of a second) are just normal fluctuations, so there’s no real speedup.

@anushkasinghh anushkasinghh changed the title Refactor to array (migrate changes from pr502) Direct L2 norm computation with Psydac inner products (no NumPy conversion)(migrate changes from pr502) Aug 20, 2025
@anushkasinghh anushkasinghh changed the title Direct L2 norm computation with Psydac inner products (no NumPy conversion)(migrate changes from pr502) Direct L2 norm computation with Psydac inner products (no NumPy conversion) Aug 20, 2025
Comment thread psydac/linalg/tests/test_linalg.py Resolved
Comment thread psydac/linalg/tests/test_linalg.py Resolved
Comment thread psydac/linalg/tests/test_linalg.py Resolved
Comment thread psydac/linalg/tests/test_linalg.py Resolved
Comment thread psydac/linalg/tests/test_linalg.py Resolved
Comment thread psydac/linalg/tests/test_linalg.py Resolved
@jowezarek

Copy link
Copy Markdown
Contributor

Hey @anushkasinghh, I added a few suggestions. Most of them are just cosmetics, but the PowerLinearOperator and InverseLinearOperator interaction test needed a proper change.
Other than that, this PR looks good to me. It will probably not decrease the test run time, but it's good practice to avoid unnecessary toarray calls.

@yguclu yguclu added the Next Release Must be in next release label Jan 20, 2026
@yguclu yguclu self-assigned this Mar 26, 2026
@codacy-production

codacy-production Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

yguclu
yguclu previously requested changes Nov 9, 2026

@yguclu yguclu 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.

I have a couple of further suggestions

Co-authored-by: Julian Owezarek <j.owezarek@googlemail.com>
Co-authored-by: Yaman Güçlü <yaman.guclu@ipp.mpg.de>
@yguclu
yguclu marked this pull request as draft November 9, 2026 15:54
@yguclu
yguclu marked this pull request as ready for review November 9, 2026 15:54
@yguclu yguclu changed the title Direct L2 norm computation with Psydac inner products (no NumPy conversion) Direct L2 norm computation with vector inner products Nov 12, 2026
@yguclu yguclu changed the title Direct L2 norm computation with vector inner products Direct L2 norm computation with vector inner product Nov 12, 2026
@yguclu
yguclu marked this pull request as draft November 12, 2026 18:10
@yguclu
yguclu marked this pull request as ready for review November 12, 2026 18:10
@yguclu
yguclu dismissed their stale review November 12, 2026 18:12

As I am now working directly on this PR, I am dismissing my own review

@yguclu
yguclu requested a review from jowezarek November 12, 2026 18:12

This branch has not been deployed

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

Labels

Next Release Must be in next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid excessive use of toarray in unit tests

4 participants