Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure MatElem subtypes T have T(R::Ring, r::Int, c::Int) constructor #1791

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions src/arb/ArbTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,9 @@
rows::Ptr{Nothing}
#base_ring::ArbField

# MatElem interface
RealMatrix(::RealField, r::Int, c::Int) = RealMatrix(r, c)

Check warning on line 800 in src/arb/ArbTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/ArbTypes.jl#L800

Added line #L800 was not covered by tests

function RealMatrix(r::Int, c::Int)
z = new()
ccall((:arb_mat_init, libflint), Nothing, (Ref{RealMatrix}, Int, Int), z, r, c)
Expand Down Expand Up @@ -884,6 +887,13 @@
rows::Ptr{Nothing}
base_ring::ArbField

# MatElem interface
function ArbMatrix(R::ArbField, r::Int, c::Int)
z = ArbMatrix(r, c)
z.base_ring = R
return z
end

function ArbMatrix(r::Int, c::Int)
z = new()
ccall((:arb_mat_init, libflint), Nothing, (Ref{ArbMatrix}, Int, Int), z, r, c)
Expand Down Expand Up @@ -976,6 +986,9 @@
rows::Ptr{Nothing}
#base_ring::AcbField

# MatElem interface
ComplexMatrix(::ComplexField, r::Int, c::Int) = ComplexMatrix(r, c)

Check warning on line 990 in src/arb/ArbTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/ArbTypes.jl#L990

Added line #L990 was not covered by tests

function ComplexMatrix(r::Int, c::Int)
z = new()
ccall((:acb_mat_init, libflint), Nothing, (Ref{ComplexMatrix}, Int, Int), z, r, c)
Expand Down Expand Up @@ -1166,6 +1179,13 @@
rows::Ptr{Nothing}
base_ring::AcbField

# MatElem interface
function AcbMatrix(R::AcbField, r::Int, c::Int)
z = AcbMatrix(r, c)
z.base_ring = R
return z
end

function AcbMatrix(r::Int, c::Int)
z = new()
ccall((:acb_mat_init, libflint), Nothing, (Ref{AcbMatrix}, Int, Int), z, r, c)
Expand Down
12 changes: 4 additions & 8 deletions src/arb/acb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
###############################################################################

function similar(::AcbMatrix, R::AcbField, r::Int, c::Int)
Copy link
Member Author

Choose a reason for hiding this comment

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

In the future this and its many siblings could be replaced by a single default method

similar(::T, R::AcbField, r::Int, c::Int) where T <: MatElem = T(R, r, c)

z = AcbMatrix(r, c)
z.base_ring = R
z = AcbMatrix(R, r, c)
return z
end

Expand Down Expand Up @@ -744,8 +743,7 @@ end
###############################################################################

function (x::AcbMatrixSpace)()
z = AcbMatrix(nrows(x), ncols(x))
z.base_ring = x.base_ring
z = AcbMatrix(base_ring(x), nrows(x), ncols(x))
return z
end

Expand Down Expand Up @@ -890,8 +888,7 @@ function zero_matrix(R::AcbField, r::Int, c::Int)
if r < 0 || c < 0
error("dimensions must not be negative")
end
z = AcbMatrix(r, c)
z.base_ring = R
z = AcbMatrix(R, r, c)
return z
end

Expand All @@ -905,9 +902,8 @@ function identity_matrix(R::AcbField, n::Int)
if n < 0
error("dimension must not be negative")
end
z = AcbMatrix(n, n)
z = AcbMatrix(R, n, n)
ccall((:acb_mat_one, libflint), Nothing, (Ref{AcbMatrix}, ), z)
z.base_ring = R
return z
end

Expand Down
15 changes: 5 additions & 10 deletions src/arb/arb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
###############################################################################

function similar(::ArbMatrix, R::ArbField, r::Int, c::Int)
z = ArbMatrix(r, c)
z.base_ring = R
z = ArbMatrix(R, r, c)
return z
end

Expand Down Expand Up @@ -81,9 +80,8 @@ number_of_rows(a::ArbMatrix) = a.r
number_of_columns(a::ArbMatrix) = a.c

function deepcopy_internal(x::ArbMatrix, dict::IdDict)
z = ArbMatrix(nrows(x), ncols(x))
z = ArbMatrix(base_ring(x), nrows(x), ncols(x))
ccall((:arb_mat_set, libflint), Nothing, (Ref{ArbMatrix}, Ref{ArbMatrix}), z, x)
z.base_ring = x.base_ring
return z
end

Expand Down Expand Up @@ -711,8 +709,7 @@ end
###############################################################################

function (x::ArbMatrixSpace)()
z = ArbMatrix(nrows(x), ncols(x))
z.base_ring = x.base_ring
z = ArbMatrix(base_ring(x), nrows(x), ncols(x))
return z
end

Expand Down Expand Up @@ -787,8 +784,7 @@ function zero_matrix(R::ArbField, r::Int, c::Int)
if r < 0 || c < 0
error("dimensions must not be negative")
end
z = ArbMatrix(r, c)
z.base_ring = R
z = ArbMatrix(R, r, c)
return z
end

Expand All @@ -802,9 +798,8 @@ function identity_matrix(R::ArbField, n::Int)
if n < 0
error("dimension must not be negative")
end
z = ArbMatrix(n, n)
z = ArbMatrix(R, n, n)
ccall((:arb_mat_one, libflint), Nothing, (Ref{ArbMatrix}, ), z)
z.base_ring = R
return z
end

Expand Down
69 changes: 57 additions & 12 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4697,7 +4697,10 @@
rows::Ptr{Ptr{QQFieldElem}}
view_parent

# used by windows, not finalised!!
# MatElem interface
QQMatrix(::QQField, r::Int, c::Int) = QQMatrix(r, c)

Check warning on line 4701 in src/flint/FlintTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/FlintTypes.jl#L4701

Added line #L4701 was not covered by tests

# Used by view, not finalised!!
function QQMatrix()
return new()
end
Expand Down Expand Up @@ -4740,6 +4743,9 @@
rows::Ptr{Ptr{ZZRingElem}}
view_parent

# MatElem interface
ZZMatrix(::ZZRing, r::Int, c::Int) = ZZMatrix(r, c)

Check warning on line 4747 in src/flint/FlintTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/FlintTypes.jl#L4747

Added line #L4747 was not covered by tests

# Used by view, not finalised!!
function ZZMatrix()
return new()
Expand Down Expand Up @@ -4785,10 +4791,16 @@
base_ring::zzModRing
view_parent

# MatElem interface
function zzModMatrix(R::zzModRing, r::Int, c::Int)
z = zzModMatrix(r, c, R.n)
z.base_ring = R
return z
end

# Used by view, not finalised!!
function zzModMatrix()
z = new()
return z
return new()
end

function zzModMatrix(r::Int, c::Int, n::UInt)
Expand Down Expand Up @@ -4929,10 +4941,16 @@
base_ring::ZZModRing
view_parent

# MatElem interface
function ZZModMatrix(R::ZZModRing, r::Int, c::Int)
z = ZZModMatrix(r, c, R.ninv)
z.base_ring = R
return z
end

# Used by view, not finalised!!
function ZZModMatrix()
z = new()
return z
return new()
end

function ZZModMatrix(r::Int, c::Int, ctx::fmpz_mod_ctx_struct)
Expand Down Expand Up @@ -5087,10 +5105,16 @@
base_ring::FpField
view_parent

# MatElem interface
function FpMatrix(R::FpField, r::Int, c::Int)
z = FpMatrix(r, c, R.ninv)
z.base_ring = R
return z
end

# Used by view, not finalised!!
function FpMatrix()
z = new()
return z
return new()
end

function FpMatrix(r::Int, c::Int, ctx::fmpz_mod_ctx_struct)
Expand Down Expand Up @@ -5197,10 +5221,16 @@
base_ring::fpField
view_parent

# MatElem interface
function fpMatrix(R::fpField, r::Int, c::Int)
z = fpMatrix(r, c, R.n)
z.base_ring = R
return z
end

# Used by view, not finalised!!
function fpMatrix()
z = new()
return z
return new()
end

function fpMatrix(r::Int, c::Int, n::UInt)
Expand Down Expand Up @@ -5775,7 +5805,12 @@
base_ring::FqField
view_parent

# used by windows, not finalised!!
# MatElem interface
function FqMatrix(R::FqField, r::Int, c::Int)
return FqMatrix(r, c, R)

Check warning on line 5810 in src/flint/FlintTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/FlintTypes.jl#L5809-L5810

Added lines #L5809 - L5810 were not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

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

For FqMatrix we already have FqMatrix(r, c, R) but I don't see a downside with having both that and FqMatrix(R, r, c). Down the road we could think about abolishing the former in a breaking release, but there is no strong need either.

(One could also argue that we should just use the (r, c, R) order everywhere. Would be also OK to me. I used the former because (a) the old PR used it and (b) it matches the order in matrix(R, r, c, ...), zero_matrix, and some other places

I think @thofma added the FqMatrix constructor here, so perhaps he had a deeper reason for the different order that we should heed?

Copy link
Member

Choose a reason for hiding this comment

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

The reason is that I copy & pasted it from some other matrix constructor in Nemo. There is no deeper reason. It is not part of the API, so can be changed/augmented/removed.

end

# Used by view, not finalised!!
function FqMatrix()
return new()
end
Expand Down Expand Up @@ -5941,7 +5976,12 @@
base_ring::FqPolyRepField
view_parent

# used by windows, not finalised!!
# MatElem interface
function FqPolyRepMatrix(R::FqPolyRepField, r::Int, c::Int)
return FqPolyRepMatrix(r, c, R)

Check warning on line 5981 in src/flint/FlintTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/FlintTypes.jl#L5980-L5981

Added lines #L5980 - L5981 were not covered by tests
end

# Used by view, not finalised!!
function FqPolyRepMatrix()
return new()
end
Expand Down Expand Up @@ -6074,7 +6114,12 @@
base_ring::fqPolyRepField
view_parent

# used by windows, not finalised!!
# MatElem interface
function fqPolyRepMatrix(R::fqPolyRepField, r::Int, c::Int)
return fqPolyRepMatrix(r, c, R)

Check warning on line 6119 in src/flint/FlintTypes.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/FlintTypes.jl#L6118-L6119

Added lines #L6118 - L6119 were not covered by tests
end

# Used by view, not finalised!!
function fqPolyRepMatrix()
return new()
end
Expand Down
12 changes: 4 additions & 8 deletions src/flint/fmpz_mod_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ dense_matrix_type(::Type{ZZModRingElem}) = ZZModMatrix
###############################################################################

function similar(::MatElem, R::ZZModRing, r::Int, c::Int)
z = ZZModMatrix(r, c, R.ninv)
z.base_ring = R
z = ZZModMatrix(R, r, c)
return z
end

Expand Down Expand Up @@ -753,8 +752,7 @@ promote_rule(::Type{ZZModMatrix}, ::Type{ZZRingElem}) = ZZModMatrix
################################################################################

function (a::ZZModMatrixSpace)()
z = ZZModMatrix(nrows(a), ncols(a), base_ring(a).ninv)
z.base_ring = a.base_ring
z = ZZModMatrix(base_ring(a), nrows(a), ncols(a))
return z
end

Expand Down Expand Up @@ -818,11 +816,10 @@ end

function (a::ZZModMatrixSpace)(b::ZZMatrix)
(ncols(a) != b.c || nrows(a) != b.r) && error("Dimensions do not fit")
z = ZZModMatrix(b.r, b.c, base_ring(a).ninv)
z = ZZModMatrix(base_ring(a), b.r, b.c)
ccall((:fmpz_mod_mat_set_fmpz_mat, libflint), Nothing,
(Ref{ZZModMatrix}, Ref{ZZMatrix}, Ref{fmpz_mod_ctx_struct}),
z, b, base_ring(a).ninv)
z.base_ring = a.base_ring
return z
end

Expand Down Expand Up @@ -855,8 +852,7 @@ function zero_matrix(R::ZZModRing, r::Int, c::Int)
if r < 0 || c < 0
error("dimensions must not be negative")
end
z = ZZModMatrix(r, c, R.ninv)
z.base_ring = R
z = ZZModMatrix(R, r, c)
return z
end

Expand Down
9 changes: 3 additions & 6 deletions src/flint/gfp_fmpz_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ dense_matrix_type(::Type{FpFieldElem}) = FpMatrix
###############################################################################

function similar(::MatElem, R::FpField, r::Int, c::Int)
z = FpMatrix(r, c, R.ninv)
z.base_ring = R
z = FpMatrix(R, r, c)
return z
end

Expand Down Expand Up @@ -227,8 +226,7 @@ end
################################################################################

function (a::FpMatrixSpace)()
z = FpMatrix(nrows(a), ncols(a), base_ring(a).ninv)
z.base_ring = a.base_ring
z = FpMatrix(base_ring(a), nrows(a), ncols(a))
return z
end

Expand Down Expand Up @@ -328,8 +326,7 @@ function zero_matrix(R::FpField, r::Int, c::Int)
if r < 0 || c < 0
error("dimensions must not be negative")
end
z = FpMatrix(r, c, R.ninv)
z.base_ring = R
z = FpMatrix(R, r, c)
return z
end

Expand Down
9 changes: 3 additions & 6 deletions src/flint/gfp_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ dense_matrix_type(::Type{fpFieldElem}) = fpMatrix
###############################################################################

function similar(::fpMatrix, R::fpField, r::Int, c::Int)
z = fpMatrix(r, c, R.n)
z.base_ring = R
z = fpMatrix(R, r, c)
return z
end

Expand Down Expand Up @@ -295,8 +294,7 @@ end
################################################################################

function (a::fpMatrixSpace)()
z = fpMatrix(nrows(a), ncols(a), modulus(base_ring(a)))
z.base_ring = a.base_ring
z = fpMatrix(base_ring(a), nrows(a), ncols(a))
return z
end

Expand Down Expand Up @@ -394,8 +392,7 @@ function zero_matrix(R::fpField, r::Int, c::Int)
if r < 0 || c < 0
error("dimensions must not be negative")
end
z = fpMatrix(r, c, R.n)
z.base_ring = R
z = fpMatrix(R, r, c)
return z
end

Expand Down
Loading
Loading