Skip to content

Commit

Permalink
Merge redundant MatrixSpace dispatches (#1978)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Dec 18, 2024
1 parent deb2b97 commit a66bf0d
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 305 deletions.
34 changes: 6 additions & 28 deletions src/arb/ComplexMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -706,54 +706,32 @@ end

for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, RealFieldElem, ComplexFieldElem, String]
@eval begin
function (x::ComplexMatrixSpace)(y::AbstractMatrix{$T})
_check_dim(nrows(x), ncols(x), y)
z = ComplexMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
end

function (x::ComplexMatrixSpace)(y::AbstractVector{$T})
function (x::ComplexMatrixSpace)(y::AbstractVecOrMat{$T})
_check_dim(nrows(x), ncols(x), y)
z = ComplexMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
end
end
end

(x::ComplexMatrixSpace)(y::AbstractMatrix{T}) where {T <: Integer} = x(map(ZZRingElem, y))

(x::ComplexMatrixSpace)(y::AbstractVector{T}) where {T <: Integer} = x(map(ZZRingElem, y))
(x::ComplexMatrixSpace)(y::AbstractVecOrMat{T}) where {T <: Integer} = x(map(ZZRingElem, y))

(x::ComplexMatrixSpace)(y::AbstractMatrix{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))

(x::ComplexMatrixSpace)(y::AbstractVector{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))
(x::ComplexMatrixSpace)(y::AbstractVecOrMat{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))

for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, RealFieldElem, String]
@eval begin
function (x::ComplexMatrixSpace)(y::AbstractMatrix{Tuple{$T, $T}})
_check_dim(nrows(x), ncols(x), y)
z = ComplexMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
end

function (x::ComplexMatrixSpace)(y::AbstractVector{Tuple{$T, $T}})
function (x::ComplexMatrixSpace)(y::AbstractVecOrMat{Tuple{$T, $T}})
_check_dim(nrows(x), ncols(x), y)
z = ComplexMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
end
end
end

(x::ComplexMatrixSpace)(y::AbstractMatrix{Tuple{T, T}}) where {T <: Integer} =
(x::ComplexMatrixSpace)(y::AbstractVecOrMat{Tuple{T, T}}) where {T <: Integer} =
x(map(z -> (ZZRingElem(z[1]), ZZRingElem(z[2])), y))

(x::ComplexMatrixSpace)(y::AbstractVector{Tuple{T, T}}) where {T <: Integer} =
x(map(z -> (ZZRingElem(z[1]), ZZRingElem(z[2])), y))

(x::ComplexMatrixSpace)(y::AbstractMatrix{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
x(map(z -> (QQFieldElem(z[1]), QQFieldElem(z[2])), y))

(x::ComplexMatrixSpace)(y::AbstractVector{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
(x::ComplexMatrixSpace)(y::AbstractVecOrMat{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
x(map(z -> (QQFieldElem(z[1]), QQFieldElem(z[2])), y))

for T in [Integer, ZZRingElem, QQFieldElem, Float64, BigFloat, RealFieldElem, ComplexFieldElem, String]
Expand Down
8 changes: 1 addition & 7 deletions src/arb/RealMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,13 +643,7 @@ function (x::RealMatrixSpace)(y::ZZMatrix)
return z
end

function (x::RealMatrixSpace)(y::AbstractMatrix{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, RealFieldElem, AbstractString}}
_check_dim(nrows(x), ncols(x), y)
z = RealMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
end

function (x::RealMatrixSpace)(y::AbstractVector{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, RealFieldElem, AbstractString}}
function (x::RealMatrixSpace)(y::AbstractVecOrMat{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, RealFieldElem, AbstractString}}
_check_dim(nrows(x), ncols(x), y)
z = RealMatrix(nrows(x), ncols(x), y, precision(Balls))
return z
Expand Down
36 changes: 6 additions & 30 deletions src/arb/acb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,7 @@ end

for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, ArbFieldElem, AcbFieldElem, String]
@eval begin
function (x::AcbMatrixSpace)(y::AbstractMatrix{$T})
_check_dim(nrows(x), ncols(x), y)
z = AcbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
return z
end

function (x::AcbMatrixSpace)(y::AbstractVector{$T})
function (x::AcbMatrixSpace)(y::AbstractVecOrMat{$T})
_check_dim(nrows(x), ncols(x), y)
z = AcbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
Expand All @@ -725,24 +718,13 @@ for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, ArbFieldElem, AcbFieldElem
end
end

(x::AcbMatrixSpace)(y::AbstractMatrix{T}) where {T <: Integer} = x(map(ZZRingElem, y))

(x::AcbMatrixSpace)(y::AbstractVector{T}) where {T <: Integer} = x(map(ZZRingElem, y))
(x::AcbMatrixSpace)(y::AbstractVecOrMat{T}) where {T <: Integer} = x(map(ZZRingElem, y))

(x::AcbMatrixSpace)(y::AbstractMatrix{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))

(x::AcbMatrixSpace)(y::AbstractVector{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))
(x::AcbMatrixSpace)(y::AbstractVecOrMat{Rational{T}}) where {T <: Integer} = x(map(QQFieldElem, y))

for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, ArbFieldElem, String]
@eval begin
function (x::AcbMatrixSpace)(y::AbstractMatrix{Tuple{$T, $T}})
_check_dim(nrows(x), ncols(x), y)
z = AcbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
return z
end

function (x::AcbMatrixSpace)(y::AbstractVector{Tuple{$T, $T}})
function (x::AcbMatrixSpace)(y::AbstractVecOrMat{Tuple{$T, $T}})
_check_dim(nrows(x), ncols(x), y)
z = AcbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
Expand All @@ -751,16 +733,10 @@ for T in [Float64, ZZRingElem, QQFieldElem, BigFloat, ArbFieldElem, String]
end
end

(x::AcbMatrixSpace)(y::AbstractMatrix{Tuple{T, T}}) where {T <: Integer} =
(x::AcbMatrixSpace)(y::AbstractVecOrMat{Tuple{T, T}}) where {T <: Integer} =
x(map(z -> (ZZRingElem(z[1]), ZZRingElem(z[2])), y))

(x::AcbMatrixSpace)(y::AbstractVector{Tuple{T, T}}) where {T <: Integer} =
x(map(z -> (ZZRingElem(z[1]), ZZRingElem(z[2])), y))

(x::AcbMatrixSpace)(y::AbstractMatrix{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
x(map(z -> (QQFieldElem(z[1]), QQFieldElem(z[2])), y))

(x::AcbMatrixSpace)(y::AbstractVector{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
(x::AcbMatrixSpace)(y::AbstractVecOrMat{Tuple{Rational{T}, Rational{T}}}) where {T <: Integer} =
x(map(z -> (QQFieldElem(z[1]), QQFieldElem(z[2])), y))

for T in [Integer, ZZRingElem, QQFieldElem, Float64, BigFloat, ArbFieldElem, AcbFieldElem, String]
Expand Down
9 changes: 1 addition & 8 deletions src/arb/arb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -667,14 +667,7 @@ function (x::ArbMatrixSpace)(y::ZZMatrix)
return z
end

function (x::ArbMatrixSpace)(y::AbstractMatrix{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, ArbFieldElem, AbstractString}}
_check_dim(nrows(x), ncols(x), y)
z = ArbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
return z
end

function (x::ArbMatrixSpace)(y::AbstractVector{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, ArbFieldElem, AbstractString}}
function (x::ArbMatrixSpace)(y::AbstractVecOrMat{T}) where {T <: Union{Int, UInt, ZZRingElem, QQFieldElem, Float64, BigFloat, ArbFieldElem, AbstractString}}
_check_dim(nrows(x), ncols(x), y)
z = ArbMatrix(nrows(x), ncols(x), y, precision(x))
z.base_ring = x.base_ring
Expand Down
49 changes: 4 additions & 45 deletions src/flint/fmpq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -727,50 +727,19 @@ function (a::QQMatrixSpace)()
return z
end

function (a::QQMatrixSpace)(arr::AbstractMatrix{QQFieldElem})
function (a::QQMatrixSpace)(arr::AbstractVecOrMat{QQFieldElem})
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::QQMatrixSpace)(arr::AbstractMatrix{ZZRingElem})
function (a::QQMatrixSpace)(arr::AbstractVecOrMat{T}) where {T <: IntegerUnion}
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end


function (a::QQMatrixSpace)(arr::AbstractMatrix{T}) where {T <: Integer}
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::QQMatrixSpace)(arr::AbstractMatrix{Rational{T}}) where {T <: Integer}
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), map(QQFieldElem, arr))
return z
end

function (a::QQMatrixSpace)(arr::AbstractVector{QQFieldElem})
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::QQMatrixSpace)(arr::AbstractVector{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::QQMatrixSpace)(arr::AbstractVector{T}) where {T <: Integer}
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::QQMatrixSpace)(arr::AbstractVector{Rational{T}}) where {T <: Integer}
function (a::QQMatrixSpace)(arr::AbstractVecOrMat{<:Rational})
_check_dim(nrows(a), ncols(a), arr)
z = QQMatrix(nrows(a), ncols(a), map(QQFieldElem, arr))
return z
Expand All @@ -781,17 +750,7 @@ function (a::QQMatrixSpace)(d::QQFieldElem)
return z
end

function (a::QQMatrixSpace)(d::ZZRingElem)
z = QQMatrix(nrows(a), ncols(a), QQFieldElem(d))
return z
end

function (a::QQMatrixSpace)(d::Integer)
z = QQMatrix(nrows(a), ncols(a), QQFieldElem(d))
return z
end

(a::QQMatrixSpace)(d::Rational) = a(QQFieldElem(d))
(a::QQMatrixSpace)(d::RationalUnion) = a(QQFieldElem(d))

function (a::QQMatrixSpace)(M::ZZMatrix)
(ncols(a) == ncols(M) && nrows(a) == nrows(M)) || error("wrong matrix dimension")
Expand Down
8 changes: 1 addition & 7 deletions src/flint/fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1908,13 +1908,7 @@ function (a::ZZMatrixSpace)()
return z
end

function (a::ZZMatrixSpace)(arr::AbstractMatrix{T}) where {T <: IntegerUnion}
_check_dim(nrows(a), ncols(a), arr)
z = ZZMatrix(nrows(a), ncols(a), arr)
return z
end

function (a::ZZMatrixSpace)(arr::AbstractVector{T}) where {T <: IntegerUnion}
function (a::ZZMatrixSpace)(arr::AbstractVecOrMat{T}) where {T <: IntegerUnion}
_check_dim(nrows(a), ncols(a), arr)
z = ZZMatrix(nrows(a), ncols(a), arr)
return z
Expand Down
47 changes: 2 additions & 45 deletions src/flint/fmpz_mod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,57 +638,14 @@ function (a::ZZModMatrixSpace)()
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractMatrix{BigInt})
function (a::ZZModMatrixSpace)(arr::AbstractVecOrMat{<:IntegerUnion})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractVector{BigInt})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractMatrix{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractVector{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractMatrix{Int})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractVector{Int})
_check_dim(nrows(a), ncols(a), arr)
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractMatrix{ZZModRingElem})
_check_dim(nrows(a), ncols(a), arr)
(length(arr) > 0 && (base_ring(a) != parent(arr[1]))) && error("Elements must have same base ring")
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::ZZModMatrixSpace)(arr::AbstractVector{ZZModRingElem})
function (a::ZZModMatrixSpace)(arr::AbstractVecOrMat{ZZModRingElem})
_check_dim(nrows(a), ncols(a), arr)
(length(arr) > 0 && (base_ring(a) != parent(arr[1]))) && error("Elements must have same base ring")
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
Expand Down
47 changes: 2 additions & 45 deletions src/flint/gfp_fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,57 +224,14 @@ function (a::FpMatrixSpace)(b::FpFieldElem)
return M
end

function (a::FpMatrixSpace)(arr::AbstractMatrix{BigInt})
function (a::FpMatrixSpace)(arr::AbstractVecOrMat{T}) where {T <: IntegerUnion}
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractVector{BigInt})
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractMatrix{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractVector{ZZRingElem})
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractMatrix{Int})
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractVector{Int})
_check_dim(nrows(a), ncols(a), arr)
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractMatrix{FpFieldElem})
_check_dim(nrows(a), ncols(a), arr)
(length(arr) > 0 && (base_ring(a) != parent(arr[1]))) && error("Elements must have same base ring")
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
z.base_ring = a.base_ring
return z
end

function (a::FpMatrixSpace)(arr::AbstractVector{FpFieldElem})
function (a::FpMatrixSpace)(arr::AbstractVecOrMat{FpFieldElem})
_check_dim(nrows(a), ncols(a), arr)
(length(arr) > 0 && (base_ring(a) != parent(arr[1]))) && error("Elements must have same base ring")
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv, arr)
Expand Down
Loading

0 comments on commit a66bf0d

Please sign in to comment.