Skip to content

Commit

Permalink
Simplify and fix similar for our matrix types
Browse files Browse the repository at this point in the history
Remove the internal helper `_similar` which untangles the `MatElem` and
`MatRingElem` code further and makes it easier to see what's going on.

Also change `similar` for `MatElem` to default to calling `zero_matrix`.
With this `similar(::ZZMatrix)` in Nemo returns a new `ZZMatrix` instead
of a `AbstractAlgebra.Generic.MatSpaceElem{ZZRingElem}`.
  • Loading branch information
fingolfin committed Oct 31, 2024
1 parent f90e000 commit d4044b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/MatRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ is_finite(R::MatRing) = iszero(nrows(a)) || is_finite(base_ring(R))
similar(x::Generic.MatrixElem, R::NCRing=base_ring(x))
similar(x::Generic.MatrixElem, R::NCRing, r::Int, c::Int)
similar(x::Generic.MatrixElem, r::Int, c::Int)
similar(x::MatRingElem, R::NCRing=base_ring(x))
similar(x::MatRingElem, R::NCRing, n::Int)
similar(x::MatRingElem, n::Int)
Create an uninitialized matrix over the given ring and dimensions,
with defaults based upon the given source matrix `x`.
"""
similar(x::MatRingElem, R::NCRing, n::Int) = _similar(x, R, n, n)
function similar(x::MatRingElem, R::NCRing, n::Int)
TT = elem_type(R)
M = Matrix{TT}(undef, (n, n))
return Generic.MatRingElem{TT}(R, M)
end

similar(x::MatRingElem, R::NCRing=base_ring(x)) = similar(x, R, degree(x))

Expand Down
9 changes: 1 addition & 8 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,7 @@ end
#
###############################################################################

function _similar(x::MatrixElem{T}, R::NCRing, r::Int, c::Int) where T <: NCRingElement
TT = elem_type(R)
M = Matrix{TT}(undef, (r, c))
z = x isa MatElem ? Generic.MatSpaceElem{TT}(R, M) : Generic.MatRingElem{TT}(R, M)
return z
end

similar(x::MatElem, R::NCRing, r::Int, c::Int) = _similar(x, R, r, c)
similar(x::MatElem, R::NCRing, r::Int, c::Int) = zero_matrix(R, r, c)

similar(x::MatElem, R::NCRing=base_ring(x)) = similar(x, R, nrows(x), ncols(x))

Expand Down
19 changes: 18 additions & 1 deletion test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)
R = base_ring(S)
T = elem_type(R)

@test base_ring_type(S) == typeof(R)
@test parent_type(ST) == typeof(S)
@test dense_matrix_type(R) == ST

@testset "MatSpace interface for $(S) of type $(typeof(S))" begin

@testset "Constructors" begin
Expand All @@ -715,6 +719,11 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)
@test a == matrix(R, T[a[i, j] for i in 1:nrows(a), j in 1:ncols(a)])
@test a == matrix(R, nrows(S), ncols(S),
T[a[i, j] for i in 1:nrows(a) for j in 1:ncols(a)])

b = similar(a)
@test b isa ST
@test nrows(b) == nrows(S)
@test ncols(b) == ncols(S)
end
@test iszero(zero_matrix(R, nrows(S), ncols(S)))
end
Expand All @@ -730,12 +739,20 @@ function test_MatSpace_interface(S::MatSpace; reps = 20)
for k in 1:reps
a = test_elem(S)::ST
A = deepcopy(a)
@test A isa ST

b = zero_matrix(R, nrows(a), ncols(a))
@test b isa ST
for i in 1:nrows(a), j in 1:ncols(a)
b[i, j] = a[i, j]
end
@test b == a
@test transpose(transpose(a)) == a

t = transpose(a)
@test t isa ST
@test nrows(t) == ncols(S)
@test ncols(t) == nrows(S)
@test transpose(t) == a
@test a == A
end
end
Expand Down

0 comments on commit d4044b8

Please sign in to comment.