Skip to content
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 @@ -76,7 +76,8 @@ scalar_matrix(R::Ring, n::Int, a::RingElement)
diagonal_matrix(::RingElement, ::Int, ::Int)
zero(::MatElem{T}, ::Ring) where T <: RingElement
one(::MatElem{T}) where T <: RingElement
transpose(::MatrixElem{T}) where T <: RingElement
transpose(::MatElem)
transpose!(::MatElem)
tr(::MatElem{T}) where T <: RingElement
det{T <: RingElem}(::MatElem{T})
rank{T <: RingElem}(::MatElem{T})
Expand Down
16 changes: 16 additions & 0 deletions ext/TestExt/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,26 @@ 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

if nrows(S) == ncols(S)
Copy link
Member Author

Choose a reason for hiding this comment

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

let's make sure we test at least one square matrix space...

# in-place transpose! only supported for square matrices
t = transpose!(deepcopy(a))
@test t isa ST
@test base_ring(t) == base_ring(a)
@test t == transpose(a)
end

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
48 changes: 39 additions & 9 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1457,21 +1457,18 @@ end


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

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

# Examples

```jldoctest
julia> R, t = polynomial_ring(QQ, :t)
(Univariate polynomial ring in t over rationals, t)

julia> S = matrix_space(R, 3, 3)
Matrix space of 3 rows and 3 columns
over univariate polynomial ring in t over rationals

julia> A = S([t + 1 t R(1); t^2 t t; R(-2) t + 2 t^2 + t + 1])
julia> A = matrix(R, [t + 1 t R(1); t^2 t t; R(-2) t + 2 t^2 + t + 1])
[t + 1 t 1]
[ t^2 t t]
[ -2 t + 2 t^2 + t + 1]
Expand All @@ -1483,9 +1480,42 @@ julia> B = transpose(A)

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

transpose(x::MatRingElem) = transpose(matrix(x))

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

Return the transpose of `x`, possibly modifying the object `z` in the process.
Aliasing is permitted.

The unary version only is supported if `x` is a square matrix.
"""
function transpose!(x::MatElem)
@req is_square(x) "Matrix must be a square matrix"
return transpose!(x, x)
end

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

###############################################################################
#
Expand Down
2 changes: 2 additions & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,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
4 changes: 3 additions & 1 deletion src/generic/GenericTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1169,12 +1169,14 @@ end
struct MatRingElem{T <: NCRingElement} <: AbstractAlgebra.MatRingElem{T}
data::MatElem{T}

function MatRingElem(A::MatElem{TT}) where TT <: NCRingElement
function MatRingElem{TT}(A::MatElem{TT}) where TT <: NCRingElement
nrows(A) == ncols(A) || error("Matrix must be square")
return new{TT}(A)
end
end

MatRingElem(A::MatElem{TT}) where TT <: NCRingElement = MatRingElem{TT}(A)

function MatRingElem(R::NCRing, n::Int, A::Vector{T}) where T <: NCRingElement
t = matrix(R, n, n, A)
return MatRingElem(t)
Expand Down
6 changes: 3 additions & 3 deletions src/generic/MatRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ Base.isassigned(a::MatRingElem, i, j) = isassigned(matrix(a), i, j)
#
###############################################################################

function transpose(x::MatRingElem)
return MatRingElem(transpose(matrix(x)))
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?


###############################################################################
#
Expand Down
11 changes: 10 additions & 1 deletion test/generic/Matrix-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4454,6 +4454,15 @@ end
c = mul!(c, a, QQ(2))
@test c == a * QQ(2)

# test 2-arg transpose! with destination != source
b = transpose(a)
@test AbstractAlgebra.transpose!(a) == b
c = zero_matrix(QQ, 3, 2)
@test AbstractAlgebra.transpose!(c, a) == b

# test 2-arg transpose! with destination == source
x = QQ[1 2; 3 4]
@test AbstractAlgebra.transpose!(x, x) == QQ[1 3; 2 4]

# in-place transpose of non-square matrix does not work
@test_throws ArgumentError AbstractAlgebra.transpose!(a)
end
Loading