Skip to content
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

Add generic methods for transpose and transpose! #1937

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/src/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ julia> c = M[1, 1]
### Transpose

```@docs
transpose(::MatrixElem{T}) where T <: RingElement
transpose(::MatrixElem)
transpose!(::MatrixElem)
```

### Submatrices
Expand Down
42 changes: 38 additions & 4 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1482,9 +1482,9 @@ end


@doc raw"""
transpose(x::MatrixElem{T}) where T <: NCRingElement
transpose(x::MatrixElem)

Return the transpose of the given matrix.
Return the transpose of `x`.

# Examples

Expand All @@ -1508,9 +1508,43 @@ julia> B = transpose(A)

```
"""
transpose(x::MatrixElem{T}) where T <: NCRingElement
function transpose(x::MatrixElem)
z = similar(base_ring(x), ncols(x), nrows(x))
return transpose!(z, x)
end

@doc raw"""
transpose!(x::MatrixElem)
transpose!(z::T, x::T) where T <: MatrixElem

Return the transpose of `x`, possibly modifying the object `z` in the process.
Aliasing is permitted.
The unary version may modify `x` in-place (but this is not always possible).
"""
function transpose!(x::MatrixElem)
if is_square(x)
# square matrix, so we can transpose it in-place
n = nrows(x)
for i in 1:n
for j in i+1:n
x[i, j], x[j, i] = x[j, i], x[i, j]
end
end
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....

z = similar(base_ring(x), ncols(x), nrows(x))
return transpose!(z, x)
end

function transpose!(z::T, x::T) where T <: MatrixElem
for i in 1:nrows(x), j in 1:ncols(x)
z[j, i] = x[i, j]
end
return z
end

transpose!(A::MatrixElem) = transpose(A)

###############################################################################
#
Expand Down
2 changes: 2 additions & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ export total_degree
export total_ring_of_fractions
export tr
export trailing_coefficient
export transpose
export transpose!
export truncate
export typed_hcat
export typed_hvcat
Expand Down
13 changes: 13 additions & 0 deletions test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,23 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)

t = transpose(a)
@test t isa ST
@test base_ring(t) == base_ring(a)
@test nrows(t) == ncols(S)
@test ncols(t) == nrows(S)
@test transpose(t) == a
@test a == A

t = transpose!(deepcopy(a))
@test t isa ST
@test base_ring(t) == base_ring(a)
@test t == transpose(a)

z = zero_matrix(R, ncols(a), nrows(a))
t = transpose!(z, a)
@test t isa ST
@test base_ring(t) == base_ring(a)
@test t == transpose(a)
@test a == A
end
end

Expand Down
Loading