Skip to content

Euclidean norm crashfix sparse matrices #40493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

JosePisco
Copy link
Contributor

Fixes #40492.

The call to .norm() or norm() with default parameter (p = 2 for euclidean norm) crashes for sparse matrices.
I go around it by checking if the matrix is sparse and if so, copy it into a dense matrix internally to compute the norm. This must be done since the euclidean norm uses the .SVD() method which is not exposed for sparse matrices.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

A = self.dense_matrix().change_ring(CDF)
else:
A = self.change_ring(CDF)

A = A.conjugate().transpose() * A
Copy link
Contributor

Choose a reason for hiding this comment

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

while we're at it, maybe you can change this to .conjugate_transpose().

if self.is_sparse():
A = self.dense_matrix().change_ring(CDF)
else:
A = self.change_ring(CDF)
Copy link
Contributor

Choose a reason for hiding this comment

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

overcomplicated, you could call .dense_matrix() unconditionally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, the cost is null if the matrix is already dense.

@user202729
Copy link
Contributor

user202729 commented Jul 28, 2025

actually there's #39831 that adds singular_values for generic matrix, I guess computing SVD isn't too difficult either.

Either way, at least for sparse matrix over CDF, I think it's cleaner to implement .SVD() instead. Maybe with a if base_ring != ...: raise NotImplementedError.

although even with that, it may still be better for performance to call .dense_matrix() anyway because the multiplication likely leads to fill-in.

@JosePisco
Copy link
Contributor Author

actually there's #39831 that adds singular_values for generic matrix, I guess computing SVD isn't too difficult either.

Either way, at least for sparse matrix over CDF, I think it's cleaner to implement .SVD() instead. Maybe with a if base_ring != ...: raise NotImplementedError.

although even with that, it may still be better for performance to call .dense_matrix() anyway because the multiplication likely leads to fill-in.

I see your point. I am unfamiliar with SVD and just wanted to patch the crash.

@vbraun vbraun merged commit f580145 into sagemath:develop Aug 2, 2025
18 of 23 checks passed
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.

Euclidean norm for sparse matrices crashes when calling .norm() or norm(...)
3 participants