Skip to content

Commit ca700e0

Browse files
committed
Add generic methods for transpose and transpose!
1 parent 747df7d commit ca700e0

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

docs/src/matrix.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ julia> c = M[1, 1]
566566
### Transpose
567567

568568
```@docs
569-
transpose(::MatrixElem{T}) where T <: RingElement
569+
transpose(::MatrixElem)
570+
transpose!(::MatrixElem)
570571
```
571572

572573
### Submatrices

src/Matrix.jl

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,9 @@ end
14821482

14831483

14841484
@doc raw"""
1485-
transpose(x::MatrixElem{T}) where T <: NCRingElement
1485+
transpose(x::MatrixElem)
14861486
1487-
Return the transpose of the given matrix.
1487+
Return the transpose of `x`.
14881488
14891489
# Examples
14901490
@@ -1508,9 +1508,43 @@ julia> B = transpose(A)
15081508
15091509
```
15101510
"""
1511-
transpose(x::MatrixElem{T}) where T <: NCRingElement
1511+
function transpose(x::MatrixElem)
1512+
z = similar(base_ring(x), ncols(x), nrows(x))
1513+
return transpose!(z, x)
1514+
end
1515+
1516+
@doc raw"""
1517+
transpose!(x::MatrixElem)
1518+
transpose!(z::T, x::T) where T <: MatrixElem
1519+
1520+
Return the transpose of `x`, possibly modifying the object `z` in the process.
1521+
Aliasing is permitted.
1522+
The unary version may modify `x` in-place (but this is not always possible).
1523+
"""
1524+
function transpose!(x::MatrixElem)
1525+
if is_square(x)
1526+
# square matrix, so we can transpose it in-place
1527+
n = nrows(x)
1528+
for i in 1:n
1529+
for j in i+1:n
1530+
x[i, j], x[j, i] = x[j, i], x[i, j]
1531+
end
1532+
end
1533+
return x
1534+
end
1535+
# we have no generic way to change the dimensions of x, so instead we
1536+
# delegate to the two argument version of transpose!
1537+
z = similar(base_ring(x), ncols(x), nrows(x))
1538+
return transpose!(z, x)
1539+
end
1540+
1541+
function transpose!(z::T, x::T) where T <: MatrixElem
1542+
for i in 1:nrows(x), j in 1:ncols(x)
1543+
z[j, i] = x[i, j]
1544+
end
1545+
return z
1546+
end
15121547

1513-
transpose!(A::MatrixElem) = transpose(A)
15141548

15151549
###############################################################################
15161550
#

src/exports.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ export total_degree
554554
export total_ring_of_fractions
555555
export tr
556556
export trailing_coefficient
557+
export transpose
558+
export transpose!
557559
export truncate
558560
export typed_hcat
559561
export typed_hvcat

test/Rings-conformance-tests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,23 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)
805805

806806
t = transpose(a)
807807
@test t isa ST
808+
@test base_ring(t) == base_ring(a)
808809
@test nrows(t) == ncols(S)
809810
@test ncols(t) == nrows(S)
810811
@test transpose(t) == a
811812
@test a == A
813+
814+
t = transpose!(deepcopy(a))
815+
@test t isa ST
816+
@test base_ring(t) == base_ring(a)
817+
@test t == transpose(a)
818+
819+
z = zero_matrix(R, ncols(a), nrows(a))
820+
t = transpose!(z, a)
821+
@test t isa ST
822+
@test base_ring(t) == base_ring(a)
823+
@test t == transpose(a)
824+
@test a == A
812825
end
813826
end
814827

0 commit comments

Comments
 (0)