Skip to content

Conversation

@fingolfin
Copy link
Member

@fingolfin fingolfin commented Dec 19, 2024

Also forbid in-place transpose! for non-square matrices.

@codecov
Copy link

codecov bot commented Dec 19, 2024

Codecov Report

❌ Patch coverage is 87.50000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.33%. Comparing base (3306a9a) to head (2029fc6).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
src/Matrix.jl 80.00% 3 Missing ⚠️
src/generic/MatRing.jl 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1937      +/-   ##
==========================================
+ Coverage   88.06%   88.33%   +0.27%     
==========================================
  Files         126      126              
  Lines       31736    31797      +61     
==========================================
+ Hits        27948    28088     +140     
+ Misses       3788     3709      -79     

☔ 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.

src/Matrix.jl Outdated
return x
end
# we have no generic way to change the dimensions of x, so instead we
# delegate to the two argument version of transpose!
Copy link
Member Author

Choose a reason for hiding this comment

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

Tests fails because in Nemo some transpose! methods with just one argument only work on square matrices.

Perhaps that the right decision, and I should match it here, and just throw an error in that case, too? Perhaps a bit more helpful one, but an error nevertheless.

Or perhaps the Nemo methods should be made more general.

Any thoughts on this, anyone?

Copy link
Member

Choose a reason for hiding this comment

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

I never missed it for non-square matrices, but maybe @fieker has opinion/wishes?

Copy link
Contributor

Choose a reason for hiding this comment

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

if it was missing - how can there be a test for it? I never used inplace transpose on non-square matrices. i do understand that this is possible (in flint) and an interesting research problem....

Copy link
Member Author

Choose a reason for hiding this comment

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

Reviving this PR from 1 year ago:

The existing method for transpose! simply delegates to transpose:

src/Matrix.jl:1488:transpose!(A::MatrixElem) = transpose(A)

And there is an existing test which invokes transpose! on a non-square matrix:

  a = QQ[1 2 3; 4 5 6]
...
  b = transpose(a)
  @test AbstractAlgebra.transpose!(a) == b

So I have two choices here:

  1. adjust the test to only invoke transpose! on a square matrix, and enforce that in the implementation
  2. allow non-square matrices (as the implementation here currently does).

Personally I'd be perfectly happy with 1, if we can agree on it (the comments from a year ago suggest that might be the case)

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR also adds a docstring for transpose! which of course should be adjusted depending on what we decide here.

It also inserts that docstring into the manual. Of course we also should agree on whether we want that (I don't see why not, but...)

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that Nemo also explicitly supports non-square matrices, e.g. it has

transpose!(A::Union{ZZMatrix,QQMatrix}) = is_square(A) ? transpose!(A, A) : transpose(A)

On the other hand, it also errors out in a few cases, e.g.:

function transpose!(a::FqMatrix)
  !is_square(a) && error("Matrix must be a square matrix")
  @ccall libflint.fq_default_mat_transpose(a::Ref{FqMatrix}, a::Ref{FqMatrix}, base_ring(a)::Ref{FqField})::Nothing
end

@JohnAAbbott
Copy link
Collaborator

As a "naive" user I would wonder what transpose! might do to a non-square matrix if it doesn't give error. Are there other in-place operations which can change the shape of a matrix?
Presumably changing the code so that an error is triggered if the input is non-square would count as a breaking change (assuming the code currently doesn't give an error). That said, one could argue that changing the code to give an error where previously it did not (but should have done?) is making the code safer rather than a true breaking change: anyway any existing code using the "old" behaviour would get a helpful error message rather than mysteriously give a different result.
Since two important users/developers say that they have never needed the suspect behaviour, we could argue that this further justifies making the change.

@fingolfin fingolfin added this to the 0.48 milestone Dec 15, 2025
@fingolfin fingolfin mentioned this pull request Dec 15, 2025
@fingolfin fingolfin changed the title Add generic methods for transpose and transpose! Enhance generic methods for transpose and transpose! Dec 15, 2025
@fingolfin
Copy link
Member Author

I've modified the PR to prevent in-place transpose for non-square matrices. Let's see if it breaks anything in Nemo, Hecke or Oscar.

end
transpose(x::MatRingElem) = MatRingElem(transpose(matrix(x)))
transpose!(x::MatRingElem) = MatRingElem(transpose!(matrix(x)))
transpose!(z::T, x::T) where T <: MatRingElem = MatRingElem(transpose!(matrix(z), matrix(x)))
Copy link
Member Author

Choose a reason for hiding this comment

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

Are these being tested right now?

@fingolfin fingolfin removed the triage label Dec 23, 2025
@fingolfin fingolfin changed the title Enhance generic methods for transpose and transpose! Enhance generic methods for transpose and transpose!, restrict in-place transpose! to square matrices Dec 23, 2025
@fingolfin fingolfin changed the title Enhance generic methods for transpose and transpose!, restrict in-place transpose! to square matrices Enhance performance of generic transpose and transpose! methods, restrict in-place transpose! to square matrices Dec 23, 2025
@fingolfin fingolfin added enhancement New feature or request optimization Performance improvements or improved testing Previously, performance: must go faster release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes labels Dec 23, 2025
@fingolfin
Copy link
Member Author

I think this is good to merge now, if someone would like to approve it ...

@fingolfin fingolfin enabled auto-merge (squash) December 23, 2025 00:34
@thofma
Copy link
Member

thofma commented Dec 23, 2025

I think you have to merge ignoring the checks.

@lgoettgens lgoettgens closed this Dec 23, 2025
auto-merge was automatically disabled December 23, 2025 10:56

Pull request was closed

@lgoettgens lgoettgens reopened this Dec 23, 2025
@thofma
Copy link
Member

thofma commented Dec 23, 2025

It was all green. Why the restart?

@lgoettgens
Copy link
Member

It was all green. Why the restart?

I thought #2259 would resolve the CI issue. But I now noticed it is due to matching branches...
Let me just add my suggestion to not run CI another unnecessary time

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

Labels

enhancement New feature or request optimization Performance improvements or improved testing Previously, performance: must go faster release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants