Skip to content

Commit

Permalink
Refactor MatrixSpace implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed May 10, 2024
1 parent fd32962 commit 1ef145b
Show file tree
Hide file tree
Showing 16 changed files with 19 additions and 706 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RandomExtensions = "fb686558-2515-59ef-acaa-46db3789a887"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[compat]
AbstractAlgebra = "0.41.0"
AbstractAlgebra = "0.41.4"
FLINT_jll = "^300.100.100"
Libdl = "1.6"
LinearAlgebra = "1.6"
Expand Down
51 changes: 4 additions & 47 deletions src/arb/ArbTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,17 +867,7 @@ base_ring(a::AcbPolyRing) = a.base_ring
#
################################################################################

struct RealMatSpace <: MatSpace{RealFieldElem}
nrows::Int
ncols::Int

function RealMatSpace(R::RealField, r::Int, c::Int)
(r < 0 || c < 0) && throw(_err_dim_negative)
return new(r, c)
end
end

const RealMatSpaceID = CacheDictType{Tuple{Int, Int}, RealMatSpace}()
const RealMatSpace = AbstractAlgebra.Generic.MatSpace{RealFieldElem}

mutable struct RealMat <: MatElem{RealFieldElem}
entries::Ptr{Nothing}
Expand Down Expand Up @@ -990,18 +980,7 @@ end

# fixed precision

struct ArbMatSpace <: MatSpace{ArbFieldElem}
nrows::Int
ncols::Int
base_ring::ArbField

function ArbMatSpace(R::ArbField, r::Int, c::Int)
(r < 0 || c < 0) && throw(_err_dim_negative)
return new(r, c, R)
end
end

const ArbMatSpaceID = CacheDictType{Tuple{ArbField, Int, Int}, ArbMatSpace}()
const ArbMatSpace = AbstractAlgebra.Generic.MatSpace{ArbFieldElem}

mutable struct ArbMatrix <: MatElem{ArbFieldElem}
entries::Ptr{Nothing}
Expand Down Expand Up @@ -1118,18 +1097,7 @@ end
#
################################################################################

struct ComplexMatSpace <: MatSpace{ComplexFieldElem}
nrows::Int
ncols::Int
#base_ring::AcbField

function ComplexMatSpace(R::ComplexField, r::Int, c::Int)
(r < 0 || c < 0) && throw(_err_dim_negative)
return new(r, c)
end
end

const ComplexMatSpaceID = CacheDictType{Tuple{Int, Int}, ComplexMatSpace}()
const ComplexMatSpace = AbstractAlgebra.Generic.MatSpace{ComplexFieldElem}

mutable struct ComplexMat <: MatElem{ComplexFieldElem}
entries::Ptr{Nothing}
Expand Down Expand Up @@ -1386,18 +1354,7 @@ end

# fixed precision

struct AcbMatSpace <: MatSpace{AcbFieldElem}
nrows::Int
ncols::Int
base_ring::AcbField

function AcbMatSpace(R::AcbField, r::Int, c::Int)
(r < 0 || c < 0) && throw(_err_dim_negative)
return new(r, c, R)
end
end

const AcbMatSpaceID = CacheDictType{Tuple{AcbField, Int, Int}, AcbMatSpace}()
const AcbMatSpace = AbstractAlgebra.Generic.MatSpace{AcbFieldElem}

mutable struct AcbMatrix <: MatElem{AcbFieldElem}
entries::Ptr{Nothing}
Expand Down
27 changes: 1 addition & 26 deletions src/arb/ComplexMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,10 @@ zero(m::ComplexMat, R::ComplexField, r::Int, c::Int) = similar(m, R, r, c)
#
###############################################################################

parent_type(::Type{ComplexMat}) = ComplexMatSpace

elem_type(::Type{ComplexMatSpace}) = ComplexMat

parent(x::ComplexMat) = matrix_space(base_ring(x), nrows(x), ncols(x))
base_ring(a::ComplexMat) = ComplexField()

dense_matrix_type(::Type{ComplexFieldElem}) = ComplexMat

base_ring(a::ComplexMatSpace) = ComplexField()

base_ring(a::ComplexMat) = ComplexField()

function check_parent(x::ComplexMat, y::ComplexMat, throw::Bool = true)
fl = (nrows(x) != nrows(y) || ncols(x) != ncols(y) || base_ring(x) != base_ring(y))
fl && throw && error("Incompatible matrices")
Expand Down Expand Up @@ -97,8 +89,6 @@ end
setindex!(x::ComplexMat, y::Tuple{Rational{T}, Rational{T}}, r::Int, c::Int) where {T <: Integer} =
setindex!(x, map(QQFieldElem, y), r, c)

zero(x::ComplexMatSpace) = x()

function one(x::ComplexMatSpace)
z = x()
ccall((:acb_mat_one, libflint), Nothing, (Ref{ComplexMat}, ), z)
Expand All @@ -109,10 +99,6 @@ number_of_rows(a::ComplexMat) = a.r

number_of_columns(a::ComplexMat) = a.c

number_of_rows(a::ComplexMatSpace) = a.nrows

number_of_columns(a::ComplexMatSpace) = a.ncols

function deepcopy_internal(x::ComplexMat, dict::IdDict)
z = similar(x)
ccall((:acb_mat_set, libflint), Nothing, (Ref{ComplexMat}, Ref{ComplexMat}), z, x)
Expand Down Expand Up @@ -1084,14 +1070,3 @@ function eigenvalues(A::ComplexMat)
e, _ = _eig_multiple(A)
return [ x[1] for x in e ]
end

###############################################################################
#
# matrix_space constructor
#
###############################################################################

function matrix_space(R::ComplexField, r::Int, c::Int; cached = true)
# TODO/FIXME: `cached` is ignored and only exists for backwards compatibility
return ComplexMatSpace(R, r, c)
end
25 changes: 0 additions & 25 deletions src/arb/RealMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ zero(m::RealMat, R::RealField, r::Int, c::Int) = similar(m, R, r, c)
#
###############################################################################

parent_type(::Type{RealMat}) = RealMatSpace

elem_type(::Type{RealMatSpace}) = RealMat

base_ring(a::RealMatSpace) = RealField()

base_ring(a::RealMat) = RealField()

parent(x::RealMat) = matrix_space(base_ring(x), nrows(x), ncols(x))

dense_matrix_type(::Type{RealFieldElem}) = RealMat

function check_parent(x::RealMat, y::RealMat, throw::Bool = true)
Expand Down Expand Up @@ -84,8 +76,6 @@ Base.@propagate_inbounds setindex!(x::RealMat, y::Rational{T},
r::Int, c::Int) where {T <: Integer} =
setindex!(x, ZZRingElem(y), r, c)

zero(a::RealMatSpace) = a()

function one(x::RealMatSpace)
z = x()
ccall((:arb_mat_one, libflint), Nothing, (Ref{RealMat}, ), z)
Expand All @@ -96,10 +86,6 @@ number_of_rows(a::RealMat) = a.r

number_of_columns(a::RealMat) = a.c

number_of_rows(a::RealMatSpace) = a.nrows

number_of_columns(a::RealMatSpace) = a.ncols

function deepcopy_internal(x::RealMat, dict::IdDict)
z = RealMat(nrows(x), ncols(x))
ccall((:arb_mat_set, libflint), Nothing, (Ref{RealMat}, Ref{RealMat}), z, x)
Expand Down Expand Up @@ -835,14 +821,3 @@ promote_rule(::Type{RealMat}, ::Type{BigFloat}) = RealMat
promote_rule(::Type{RealMat}, ::Type{ZZMatrix}) = RealMat

promote_rule(::Type{RealMat}, ::Type{QQMatrix}) = RealMat

###############################################################################
#
# matrix_space constructor
#
###############################################################################

function matrix_space(R::RealField, r::Int, c::Int; cached = true)
# TODO/FIXME: `cached` is ignored and only exists for backwards compatibility
return RealMatSpace(R, r, c)
end
29 changes: 2 additions & 27 deletions src/arb/acb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,11 @@ zero(m::AcbMatrix, R::AcbField, r::Int, c::Int) = similar(m, R, r, c)
#
###############################################################################

parent_type(::Type{AcbMatrix}) = AcbMatSpace

elem_type(::Type{AcbMatSpace}) = AcbMatrix

parent(x::AcbMatrix) = matrix_space(base_ring(x), nrows(x), ncols(x))
base_ring(a::AcbMatrix) = a.base_ring

dense_matrix_type(::Type{AcbFieldElem}) = AcbMatrix

precision(x::AcbMatSpace) = precision(x.base_ring)

base_ring(a::AcbMatSpace) = a.base_ring

base_ring(a::AcbMatrix) = a.base_ring
precision(x::AcbMatSpace) = precision(base_ring(x))

function check_parent(x::AcbMatrix, y::AcbMatrix, throw::Bool = true)
fl = (nrows(x) != nrows(y) || ncols(x) != ncols(y) || base_ring(x) != base_ring(y))
Expand Down Expand Up @@ -100,8 +92,6 @@ end
setindex!(x::AcbMatrix, y::Tuple{Rational{T}, Rational{T}}, r::Int, c::Int) where {T <: Integer} =
setindex!(x, map(QQFieldElem, y), r, c)

zero(x::AcbMatSpace) = x()

function one(x::AcbMatSpace)
z = x()
ccall((:acb_mat_one, libflint), Nothing, (Ref{AcbMatrix}, ), z)
Expand All @@ -112,10 +102,6 @@ number_of_rows(a::AcbMatrix) = a.r

number_of_columns(a::AcbMatrix) = a.c

number_of_rows(a::AcbMatSpace) = a.nrows

number_of_columns(a::AcbMatSpace) = a.ncols

function deepcopy_internal(x::AcbMatrix, dict::IdDict)
z = similar(x)
ccall((:acb_mat_set, libflint), Nothing, (Ref{AcbMatrix}, Ref{AcbMatrix}), z, x)
Expand Down Expand Up @@ -1098,14 +1084,3 @@ function eigenvalues(A::AcbMatrix)
e, _ = _eig_multiple(A)
return [ x[1] for x in e ]
end

###############################################################################
#
# matrix_space constructor
#
###############################################################################

function matrix_space(R::AcbField, r::Int, c::Int; cached = true)
# TODO/FIXME: `cached` is ignored and only exists for backwards compatibility
return AcbMatSpace(R, r, c)
end
40 changes: 0 additions & 40 deletions src/arb/arb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ zero(m::ArbMatrix, R::ArbField, r::Int, c::Int) = similar(m, R, r, c)
#
###############################################################################

parent_type(::Type{ArbMatrix}) = ArbMatSpace

elem_type(::Type{ArbMatSpace}) = ArbMatrix

base_ring(a::ArbMatSpace) = a.base_ring

base_ring(a::ArbMatrix) = a.base_ring

parent(x::ArbMatrix) = matrix_space(base_ring(x), nrows(x), ncols(x))

dense_matrix_type(::Type{ArbFieldElem}) = ArbMatrix

precision(x::ArbMatSpace) = precision(x.base_ring)
Expand Down Expand Up @@ -87,8 +79,6 @@ Base.@propagate_inbounds setindex!(x::ArbMatrix, y::Rational{T},
r::Int, c::Int) where {T <: Integer} =
setindex!(x, ZZRingElem(y), r, c)

zero(a::ArbMatSpace) = a()

function one(x::ArbMatSpace)
z = x()
ccall((:arb_mat_one, libflint), Nothing, (Ref{ArbMatrix}, ), z)
Expand All @@ -99,10 +89,6 @@ number_of_rows(a::ArbMatrix) = a.r

number_of_columns(a::ArbMatrix) = a.c

number_of_rows(a::ArbMatSpace) = a.nrows

number_of_columns(a::ArbMatSpace) = a.ncols

function deepcopy_internal(x::ArbMatrix, dict::IdDict)
z = ArbMatrix(nrows(x), ncols(x))
ccall((:arb_mat_set, libflint), Nothing, (Ref{ArbMatrix}, Ref{ArbMatrix}), z, x)
Expand Down Expand Up @@ -762,21 +748,6 @@ function (x::ArbMatSpace)(y::AbstractVector{T}) where {T <: Union{Int, UInt, ZZR
return z
end

function (x::ArbMatSpace)(y::Union{Int, UInt, ZZRingElem, QQFieldElem, Float64,
BigFloat, ArbFieldElem, AbstractString})
z = x()
for i in 1:nrows(z)
for j = 1:ncols(z)
if i != j
z[i, j] = zero(base_ring(x))
else
z[i, j] = y
end
end
end
return z
end

###############################################################################
#
# Matrix constructor
Expand Down Expand Up @@ -870,14 +841,3 @@ promote_rule(::Type{ArbMatrix}, ::Type{BigFloat}) = ArbMatrix
promote_rule(::Type{ArbMatrix}, ::Type{ZZMatrix}) = ArbMatrix

promote_rule(::Type{ArbMatrix}, ::Type{QQMatrix}) = ArbMatrix

###############################################################################
#
# matrix_space constructor
#
###############################################################################

function matrix_space(R::ArbField, r::Int, c::Int; cached = true)
# TODO/FIXME: `cached` is ignored and only exists for backwards compatibility
return ArbMatSpace(R, r, c)
end
Loading

0 comments on commit 1ef145b

Please sign in to comment.