From a622e98c747b10242a4dd5a1eb19567787768e04 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 27 Sep 2024 14:58:42 +0200 Subject: [PATCH 1/3] Add and use zero!, one!, neg! methods --- src/HeckeMoreStuff.jl | 7 ----- src/antic/nf_elem.jl | 34 ++++++++++------------ src/arb/Complex.jl | 5 ++++ src/arb/ComplexPoly.jl | 5 ++++ src/arb/Real.jl | 5 ++++ src/arb/RealPoly.jl | 6 ++++ src/arb/acb.jl | 5 ++++ src/arb/acb_poly.jl | 5 ++++ src/arb/arb.jl | 5 ++++ src/arb/arb_poly.jl | 6 ++++ src/calcium/ca.jl | 23 +++++++++------ src/calcium/qqbar.jl | 16 +++++++---- src/flint/fmpq.jl | 10 +++---- src/flint/fmpq_abs_series.jl | 7 +++++ src/flint/fmpq_mat.jl | 31 ++++++++++++-------- src/flint/fmpq_mpoly.jl | 2 -- src/flint/fmpq_poly.jl | 19 ++++++++---- src/flint/fmpq_rel_series.jl | 8 ++++++ src/flint/fmpz.jl | 16 +++++------ src/flint/fmpz_abs_series.jl | 7 +++++ src/flint/fmpz_mat.jl | 34 +++++++++++----------- src/flint/fmpz_mod.jl | 14 +++++++++ src/flint/fmpz_mod_abs_series.jl | 8 ++++++ src/flint/fmpz_mod_mat.jl | 36 ++++++++++++----------- src/flint/fmpz_mod_mpoly.jl | 46 ++++++++++++------------------ src/flint/fmpz_mod_poly.jl | 22 +++++++++----- src/flint/fmpz_mod_rel_series.jl | 9 ++++++ src/flint/fmpz_mpoly.jl | 26 +++++++++-------- src/flint/fmpz_poly.jl | 21 +++++++++----- src/flint/fmpz_rel_series.jl | 8 ++++++ src/flint/fq.jl | 31 ++++++++++---------- src/flint/fq_abs_series.jl | 7 +++++ src/flint/fq_default.jl | 32 ++++++++++----------- src/flint/fq_default_abs_series.jl | 24 ++++++++++------ src/flint/fq_default_mat.jl | 31 ++++++++++++-------- src/flint/fq_default_mpoly.jl | 10 +++++++ src/flint/fq_default_poly.jl | 22 +++++++++----- src/flint/fq_default_rel_series.jl | 30 +++++++++++-------- src/flint/fq_mat.jl | 31 ++++++++++++-------- src/flint/fq_nmod.jl | 45 ++++++++++++++--------------- src/flint/fq_nmod_mat.jl | 31 ++++++++++++-------- src/flint/fq_nmod_mpoly.jl | 38 +++++++++++------------- src/flint/fq_nmod_poly.jl | 22 +++++++++----- src/flint/fq_poly.jl | 22 +++++++++----- src/flint/fq_rel_series.jl | 26 +++++++++++------ src/flint/gfp_elem.jl | 9 ++++++ src/flint/gfp_fmpz_elem.jl | 10 +++++++ src/flint/nmod.jl | 9 ++++++ src/flint/nmod_abs_series.jl | 22 +++++++++----- src/flint/nmod_mat.jl | 31 +++++++++++--------- src/flint/nmod_mpoly.jl | 38 +++++++++++------------- src/flint/nmod_poly.jl | 12 ++++---- src/flint/nmod_rel_series.jl | 27 ++++++++++++------ 53 files changed, 626 insertions(+), 380 deletions(-) diff --git a/src/HeckeMoreStuff.jl b/src/HeckeMoreStuff.jl index 5e3ee4c25..26449b852 100644 --- a/src/HeckeMoreStuff.jl +++ b/src/HeckeMoreStuff.jl @@ -333,13 +333,6 @@ function numerator(a::AbsSimpleNumFieldElem) return z end -function one!(r::AbsSimpleNumFieldElem) - a = parent(r) - ccall((:nf_elem_one, libflint), Nothing, - (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), r, a) - return r -end - function divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::ZZRingElem) ccall((:nf_elem_scalar_div_fmpz, libflint), Nothing, (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{ZZRingElem}, Ref{AbsSimpleNumField}), diff --git a/src/antic/nf_elem.jl b/src/antic/nf_elem.jl index 65e67d1b2..0196639d4 100644 --- a/src/antic/nf_elem.jl +++ b/src/antic/nf_elem.jl @@ -111,19 +111,9 @@ function gen(a::AbsSimpleNumField) return r end -function one(a::AbsSimpleNumField) - r = AbsSimpleNumFieldElem(a) - ccall((:nf_elem_one, libflint), Nothing, - (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), r, a) - return r -end +one(a::AbsSimpleNumField) = one!(AbsSimpleNumFieldElem(a)) -function zero(a::AbsSimpleNumField) - r = AbsSimpleNumFieldElem(a) - ccall((:nf_elem_zero, libflint), Nothing, - (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), r, a) - return r -end +zero(a::AbsSimpleNumField) = zero!(AbsSimpleNumFieldElem(a)) @doc raw""" is_gen(a::AbsSimpleNumFieldElem) @@ -274,13 +264,7 @@ canonical_unit(x::AbsSimpleNumFieldElem) = x # ############################################################################### -function -(a::AbsSimpleNumFieldElem) - r = a.parent() - ccall((:nf_elem_neg, libflint), Nothing, - (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), - r, a, a.parent) - return r -end +-(a::AbsSimpleNumFieldElem) = neg!(a.parent(), a) ############################################################################### # @@ -743,6 +727,18 @@ function zero!(a::AbsSimpleNumFieldElem) return a end +function one!(a::AbsSimpleNumFieldElem) + ccall((:nf_elem_one, libflint), Nothing, + (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), a, parent(a)) + return a +end + +function neg!(z::AbsSimpleNumFieldElem, a::AbsSimpleNumFieldElem) + ccall((:nf_elem_neg, libflint), Nothing, + (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), z, a, parent(a)) + return z +end + function mul!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::AbsSimpleNumFieldElem) ccall((:nf_elem_mul, libflint), Nothing, (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), diff --git a/src/arb/Complex.jl b/src/arb/Complex.jl index d97996e35..0920cb2f6 100644 --- a/src/arb/Complex.jl +++ b/src/arb/Complex.jl @@ -1586,6 +1586,11 @@ function zero!(z::ComplexFieldElem) return z end +function one!(z::ComplexFieldElem) + ccall((:acb_one, libflint), Nothing, (Ref{ComplexFieldElem},), z) + return z +end + function add!(z::ComplexFieldElem, x::ComplexFieldElem, y::ComplexFieldElem, prec::Int = precision(Balls)) ccall((:acb_add, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, Int), z, x, y, prec) diff --git a/src/arb/ComplexPoly.jl b/src/arb/ComplexPoly.jl index a7969e284..777042f78 100644 --- a/src/arb/ComplexPoly.jl +++ b/src/arb/ComplexPoly.jl @@ -707,6 +707,11 @@ function zero!(z::ComplexPolyRingElem) return z end +function one!(z::ComplexPolyRingElem) + ccall((:acb_poly_one, libflint), Nothing, (Ref{ComplexPolyRingElem},), z) + return z +end + function fit!(z::ComplexPolyRingElem, n::Int) ccall((:acb_poly_fit_length, libflint), Nothing, (Ref{ComplexPolyRingElem}, Int), z, n) diff --git a/src/arb/Real.jl b/src/arb/Real.jl index f36fb8add..6e72c3eb6 100644 --- a/src/arb/Real.jl +++ b/src/arb/Real.jl @@ -1934,6 +1934,11 @@ function zero!(z::RealFieldElem) return z end +function one!(z::RealFieldElem) + ccall((:arb_one, libflint), Nothing, (Ref{RealFieldElem},), z) + return z +end + for (s,f) in (("add!","arb_add"), ("mul!","arb_mul"), ("div!", "arb_div"), ("sub!","arb_sub")) @eval begin diff --git a/src/arb/RealPoly.jl b/src/arb/RealPoly.jl index de86cdb0f..f67c48c5a 100644 --- a/src/arb/RealPoly.jl +++ b/src/arb/RealPoly.jl @@ -607,6 +607,12 @@ function zero!(z::RealPolyRingElem) return z end +function one!(z::RealPolyRingElem) + ccall((:arb_poly_one, libflint), Nothing, + (Ref{RealPolyRingElem}, ), z) + return z +end + function fit!(z::RealPolyRingElem, n::Int) ccall((:arb_poly_fit_length, libflint), Nothing, (Ref{RealPolyRingElem}, Int), z, n) diff --git a/src/arb/acb.jl b/src/arb/acb.jl index fe180006c..5e8632c6d 100644 --- a/src/arb/acb.jl +++ b/src/arb/acb.jl @@ -1574,6 +1574,11 @@ function zero!(z::AcbFieldElem) return z end +function one!(z::AcbFieldElem) + ccall((:acb_one, libflint), Nothing, (Ref{AcbFieldElem},), z) + return z +end + function add!(z::AcbFieldElem, x::AcbFieldElem, y::AcbFieldElem) ccall((:acb_add, libflint), Nothing, (Ref{AcbFieldElem}, Ref{AcbFieldElem}, Ref{AcbFieldElem}, Int), z, x, y, parent(z).prec) diff --git a/src/arb/acb_poly.jl b/src/arb/acb_poly.jl index 62e2a2a9a..265dcffe8 100644 --- a/src/arb/acb_poly.jl +++ b/src/arb/acb_poly.jl @@ -702,6 +702,11 @@ function zero!(z::AcbPolyRingElem) return z end +function one!(z::AcbPolyRingElem) + ccall((:acb_poly_one, libflint), Nothing, (Ref{AcbPolyRingElem},), z) + return z +end + function fit!(z::AcbPolyRingElem, n::Int) ccall((:acb_poly_fit_length, libflint), Nothing, (Ref{AcbPolyRingElem}, Int), z, n) diff --git a/src/arb/arb.jl b/src/arb/arb.jl index d53c2272a..a494dcd0a 100644 --- a/src/arb/arb.jl +++ b/src/arb/arb.jl @@ -1936,6 +1936,11 @@ function zero!(z::ArbFieldElem) return z end +function one!(z::ArbFieldElem) + ccall((:arb_one, libflint), Nothing, (Ref{ArbFieldElem},), z) + return z +end + for (s,f) in (("add!","arb_add"), ("mul!","arb_mul"), ("div!", "arb_div"), ("sub!","arb_sub")) @eval begin diff --git a/src/arb/arb_poly.jl b/src/arb/arb_poly.jl index 8d6cab030..75f0d1f8f 100644 --- a/src/arb/arb_poly.jl +++ b/src/arb/arb_poly.jl @@ -607,6 +607,12 @@ function zero!(z::ArbPolyRingElem) return z end +function one!(z::ArbPolyRingElem) + ccall((:arb_poly_one, libflint), Nothing, + (Ref{ArbPolyRingElem}, ), z) + return z +end + function fit!(z::ArbPolyRingElem, n::Int) ccall((:arb_poly_fit_length, libflint), Nothing, (Ref{ArbPolyRingElem}, Int), z, n) diff --git a/src/calcium/ca.jl b/src/calcium/ca.jl index a4862c332..bc0d9d52f 100644 --- a/src/calcium/ca.jl +++ b/src/calcium/ca.jl @@ -113,11 +113,7 @@ end zero(C::CalciumField) = C() -function one(C::CalciumField) - z = CalciumFieldElem(C) - ccall((:ca_one, libflint), Nothing, (Ref{CalciumFieldElem}, Ref{CalciumField}), z, C) - return z -end +one(C::CalciumField) = one!(CalciumFieldElem(C)) ############################################################################### # @@ -350,10 +346,7 @@ end ############################################################################### function -(a::CalciumFieldElem) - C = a.parent - r = C() - ccall((:ca_neg, libflint), Nothing, - (Ref{CalciumFieldElem}, Ref{CalciumFieldElem}, Ref{CalciumField}), r, a, C) + r = neg!(a.parent(), a) check_special(r) return r end @@ -1391,6 +1384,18 @@ function zero!(z::CalciumFieldElem) return z end +function one!(z::CalciumFieldElem) + C = z.parent + ccall((:ca_one, libflint), Nothing, (Ref{CalciumFieldElem}, Ref{CalciumField}), z, C) + return z +end + +function neg!(z::CalciumFieldElem, a::CalciumFieldElem) + C = z.parent + ccall((:ca_neg, libflint), Nothing, (Ref{CalciumFieldElem}, Ref{CalciumFieldElem}, Ref{CalciumField}), z, a, C) + return z +end + function mul!(z::CalciumFieldElem, x::CalciumFieldElem, y::CalciumFieldElem) if z.parent != x.parent || x.parent != y.parent error("different parents in in-place operation") diff --git a/src/calcium/qqbar.jl b/src/calcium/qqbar.jl index 7ff6efc1f..87e86b8eb 100644 --- a/src/calcium/qqbar.jl +++ b/src/calcium/qqbar.jl @@ -360,11 +360,7 @@ end # ############################################################################### -function -(a::QQBarFieldElem) - z = QQBarFieldElem() - ccall((:qqbar_neg, libflint), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a) - return z -end +-(a::QQBarFieldElem) = neg!(QQBarFieldElem(), a) ############################################################################### # @@ -1499,6 +1495,16 @@ function zero!(z::QQBarFieldElem) return z end +function one!(z::QQBarFieldElem) + ccall((:qqbar_one, libflint), Nothing, (Ref{QQBarFieldElem},), z) + return z +end + +function neg!(z::QQBarFieldElem, a::QQBarFieldElem) + ccall((:qqbar_neg, libflint), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a) + return z +end + function mul!(z::QQBarFieldElem, x::QQBarFieldElem, y::QQBarFieldElem) ccall((:qqbar_mul, libflint), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, x, y) diff --git a/src/flint/fmpq.jl b/src/flint/fmpq.jl index 5a865f6b2..bc276429c 100644 --- a/src/flint/fmpq.jl +++ b/src/flint/fmpq.jl @@ -1039,6 +1039,11 @@ function one!(c::QQFieldElemOrPtr) set!(c, 1) end +function neg!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr) + ccall((:fmpq_neg, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, a) + return z +end + function set!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr) @ccall libflint.fmpq_set(c::Ref{QQFieldElem}, a::Ref{QQFieldElem})::Nothing return c @@ -1170,11 +1175,6 @@ function sub!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) return z end -function neg!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr) - ccall((:fmpq_neg, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, a) - return z -end - function divexact!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) ccall((:fmpq_div, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b) return z diff --git a/src/flint/fmpq_abs_series.jl b/src/flint/fmpq_abs_series.jl index 07e939fa7..010b4a601 100644 --- a/src/flint/fmpq_abs_series.jl +++ b/src/flint/fmpq_abs_series.jl @@ -721,6 +721,13 @@ function zero!(z::QQAbsPowerSeriesRingElem) return z end +function one!(z::QQAbsPowerSeriesRingElem) + ccall((:fmpq_poly_one, libflint), Nothing, + (Ref{QQAbsPowerSeriesRingElem},), z) + z.prec = parent(z).prec_max + return z +end + function fit!(z::QQAbsPowerSeriesRingElem, n::Int) ccall((:fmpq_poly_fit_length, libflint), Nothing, (Ref{QQAbsPowerSeriesRingElem}, Int), z, n) diff --git a/src/flint/fmpq_mat.jl b/src/flint/fmpq_mat.jl index 0c54f93c3..3bcf730e1 100644 --- a/src/flint/fmpq_mat.jl +++ b/src/flint/fmpq_mat.jl @@ -203,12 +203,7 @@ canonical_unit(a::QQMatrix) = canonical_unit(a[1, 1]) # ############################################################################### -function -(x::QQMatrix) - z = similar(x) - ccall((:fmpq_mat_neg, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}), z, x) - return z -end +-(x::QQMatrix) = neg!(similar(x), x) ############################################################################### # @@ -752,6 +747,24 @@ end # ############################################################################### +function zero!(z::QQMatrix) + ccall((:fmpq_mat_zero, libflint), Nothing, + (Ref{QQMatrix},), z) + return z +end + +function one!(z::QQMatrix) + ccall((:fmpq_mat_one, libflint), Nothing, + (Ref{QQMatrix},), z) + return z +end + +function neg!(z::QQMatrix, a::QQMatrix) + ccall((:fmpq_mat_neg, libflint), Nothing, + (Ref{QQMatrix}, Ref{QQMatrix}), z, a) + return z +end + function mul!(z::QQMatrix, x::QQMatrix, y::QQMatrix) ccall((:fmpq_mat_mul, libflint), Nothing, (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, x, y) @@ -815,12 +828,6 @@ mul!(x::QQMatrix, y::IntegerUnion) = mul!(x, x, y) mul!(z::QQMatrix, y::QQMatrix, x::Integer) = mul!(z, y, ZZ(x)) -function zero!(z::QQMatrix) - ccall((:fmpq_mat_zero, libflint), Nothing, - (Ref{QQMatrix},), z) - return z -end - function Generic.add_one!(a::QQMatrix, i::Int, j::Int) @boundscheck _checkbounds(a, i, j) GC.@preserve a begin diff --git a/src/flint/fmpq_mpoly.jl b/src/flint/fmpq_mpoly.jl index 24cbea21b..540e018c6 100644 --- a/src/flint/fmpq_mpoly.jl +++ b/src/flint/fmpq_mpoly.jl @@ -818,8 +818,6 @@ function neg!(a::QQMPolyRingElem, b::QQMPolyRingElem) return a end -neg!(a::QQMPolyRingElem) = neg!(a, a) - function add!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::QQMPolyRingElem) ccall((:fmpq_mpoly_add, libflint), Nothing, (Ref{QQMPolyRingElem}, Ref{QQMPolyRingElem}, diff --git a/src/flint/fmpq_poly.jl b/src/flint/fmpq_poly.jl index 435cf5df6..6476b2998 100644 --- a/src/flint/fmpq_poly.jl +++ b/src/flint/fmpq_poly.jl @@ -120,12 +120,7 @@ canonical_unit(a::QQPolyRingElem) = canonical_unit(leading_coefficient(a)) # ############################################################################### -function -(x::QQPolyRingElem) - z = parent(x)() - ccall((:fmpq_poly_neg, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, x) - return z -end +-(x::QQPolyRingElem) = neg!(parent(x)(), x) ############################################################################### # @@ -783,6 +778,18 @@ function zero!(z::QQPolyRingElem) return z end +function one!(z::QQPolyRingElem) + ccall((:fmpq_poly_one, libflint), Nothing, + (Ref{QQPolyRingElem},), z) + return z +end + +function neg!(z::QQPolyRingElem, a::QQPolyRingElem) + ccall((:fmpq_poly_neg, libflint), Nothing, + (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, a) + return z +end + function fit!(z::QQPolyRingElem, n::Int) ccall((:fmpq_poly_fit_length, libflint), Nothing, (Ref{QQPolyRingElem}, Int), z, n) diff --git a/src/flint/fmpq_rel_series.jl b/src/flint/fmpq_rel_series.jl index fc025aaef..70c146ba1 100644 --- a/src/flint/fmpq_rel_series.jl +++ b/src/flint/fmpq_rel_series.jl @@ -900,6 +900,14 @@ function zero!(z::QQRelPowerSeriesRingElem) return z end +function one!(z::QQRelPowerSeriesRingElem) + ccall((:fmpq_poly_one, libflint), Nothing, + (Ref{QQRelPowerSeriesRingElem},), z) + z.prec = parent(z).prec_max + z.val = parent(z).prec_max + return z +end + function fit!(z::QQRelPowerSeriesRingElem, n::Int) ccall((:fmpq_poly_fit_length, libflint), Nothing, (Ref{QQRelPowerSeriesRingElem}, Int), z, n) diff --git a/src/flint/fmpz.jl b/src/flint/fmpz.jl index 72918b27c..302c20cb6 100644 --- a/src/flint/fmpz.jl +++ b/src/flint/fmpz.jl @@ -2521,6 +2521,13 @@ function one!(z::ZZRingElemOrPtr) set!(z, UInt(1)) end +function neg!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr) + ccall((:fmpz_neg, libflint), Nothing, + (Ref{ZZRingElem}, Ref{ZZRingElem}), + z, a) + return z +end + function set!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr) @ccall libflint.fmpz_set(z::Ref{ZZRingElem}, a::Ref{ZZRingElem})::Nothing return z @@ -2568,15 +2575,6 @@ end add!(z::ZZRingElem, a::ZZRingElem, b::Integer) = add!(z, a, ZZRingElem(b)) add!(z::ZZRingElem, x::Int, y::ZZRingElem) = add!(z, y, x) -function neg!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr) - ccall((:fmpz_neg, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}), - z, a) - return z -end - -neg!(a::ZZRingElemOrPtr) = neg!(a, a) - function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) ccall((:fmpz_sub, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), diff --git a/src/flint/fmpz_abs_series.jl b/src/flint/fmpz_abs_series.jl index 587b09db4..6e2a97ed4 100644 --- a/src/flint/fmpz_abs_series.jl +++ b/src/flint/fmpz_abs_series.jl @@ -529,6 +529,13 @@ function zero!(z::ZZAbsPowerSeriesRingElem) return z end +function one!(z::ZZAbsPowerSeriesRingElem) + ccall((:fmpz_poly_one, libflint), Nothing, + (Ref{ZZAbsPowerSeriesRingElem},), z) + z.prec = parent(z).prec_max + return z +end + function fit!(z::ZZAbsPowerSeriesRingElem, n::Int) ccall((:fmpz_poly_fit_length, libflint), Nothing, (Ref{ZZAbsPowerSeriesRingElem}, Int), z, n) diff --git a/src/flint/fmpz_mat.jl b/src/flint/fmpz_mat.jl index f3c516e1c..92607c5c9 100644 --- a/src/flint/fmpz_mat.jl +++ b/src/flint/fmpz_mat.jl @@ -1820,6 +1820,23 @@ end # ############################################################################### +function zero!(z::ZZMatrix) + ccall((:fmpz_mat_zero, libflint), Nothing, + (Ref{ZZMatrix},), z) + return z +end + +function one!(z::ZZMatrix) + ccall((:fmpz_mat_one, libflint), Nothing, + (Ref{ZZMatrix},), z) + return z +end + +function neg!(z::ZZMatrix, w::ZZMatrix) + ccall((:fmpz_mat_neg, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), z, w) + return z +end + function add!(z::ZZMatrix, x::ZZMatrix, y::ZZMatrix) ccall((:fmpz_mat_add, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}), z, x, y) @@ -1870,19 +1887,6 @@ function addmul!(z::ZZMatrix, y::ZZMatrix, x::Int) return z end -function zero!(z::ZZMatrix) - ccall((:fmpz_mat_zero, libflint), Nothing, - (Ref{ZZMatrix},), z) - return z -end - -function neg!(z::ZZMatrix, w::ZZMatrix) - ccall((:fmpz_mat_neg, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), z, w) - return z -end - -neg!(z::ZZMatrix) = neg!(z, z) - function mul!(z::Vector{ZZRingElem}, a::ZZMatrix, b::Vector{ZZRingElem}) ccall((:fmpz_mat_mul_fmpz_vec_ptr, libflint), Nothing, (Ptr{Ref{ZZRingElem}}, Ref{ZZMatrix}, Ptr{Ref{ZZRingElem}}, Int), @@ -2079,9 +2083,7 @@ function identity_matrix(R::ZZRing, n::Int) if n < 0 error("dimension must not be negative") end - z = ZZMatrix(n, n) - ccall((:fmpz_mat_one, libflint), Nothing, (Ref{ZZMatrix}, ), z) - return z + return one!(ZZMatrix(n, n)) end ################################################################################ diff --git a/src/flint/fmpz_mod.jl b/src/flint/fmpz_mod.jl index d267f2122..a0cfde48e 100644 --- a/src/flint/fmpz_mod.jl +++ b/src/flint/fmpz_mod.jl @@ -342,6 +342,20 @@ function zero!(z::ZZModRingElem) return z end +function one!(z::ZZModRingElem) + one!(z.data) + return z +end + +function neg!(z::ZZModRingElem, a::ZZModRingElem) + if iszero(a.data) + z.data = zero!(z.data) + else + z.data = sub!(z.data, R.n, a.data) + end + return z +end + function mul!(z::ZZModRingElem, x::ZZModRingElem, y::ZZModRingElem) R = parent(z) ccall((:fmpz_mod_mul, libflint), Nothing, diff --git a/src/flint/fmpz_mod_abs_series.jl b/src/flint/fmpz_mod_abs_series.jl index 9d3716192..569a7c32f 100644 --- a/src/flint/fmpz_mod_abs_series.jl +++ b/src/flint/fmpz_mod_abs_series.jl @@ -559,6 +559,14 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in ( return z end + function one!(z::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)}, Ref{($ctype)}), + z, z.parent.base_ring.ninv) + z.prec = parent(z).prec_max + return z + end + function fit!(z::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int, Ref{($ctype)}), diff --git a/src/flint/fmpz_mod_mat.jl b/src/flint/fmpz_mod_mat.jl index 15423cc67..08d3b7956 100644 --- a/src/flint/fmpz_mod_mat.jl +++ b/src/flint/fmpz_mod_mat.jl @@ -98,10 +98,7 @@ base_ring(a::T) where T <: Zmod_fmpz_mat = a.base_ring function one(a::ZZModMatrixSpace) (nrows(a) != ncols(a)) && error("Matrices must be square") - z = a() - ccall((:fmpz_mod_mat_one, libflint), Nothing, - (Ref{ZZModMatrix}, Ref{fmpz_mod_ctx_struct}), z, base_ring(a).ninv) - return z + return one!(a()) end function iszero(a::T) where T <: Zmod_fmpz_mat @@ -198,12 +195,7 @@ reverse_cols(x::T) where T <: Zmod_fmpz_mat = reverse_cols!(deepcopy(x)) # ################################################################################ -function -(x::T) where T <: Zmod_fmpz_mat - z = similar(x) - ccall((:fmpz_mod_mat_neg, libflint), Nothing, - (Ref{T}, Ref{T}, Ref{fmpz_mod_ctx_struct}), z, x, base_ring(x).ninv) - return z -end +-(x::T) where T <: Zmod_fmpz_mat = neg!(similar(x), x) ################################################################################ # @@ -246,6 +238,24 @@ end # ################################################################################ +function zero!(a::T) where T <: Zmod_fmpz_mat + ccall((:fmpz_mod_mat_zero, libflint), Nothing, + (Ref{T}, Ref{fmpz_mod_ctx_struct}), a, base_ring(a).ninv) + return a +end + +function one!(a::T) where T <: Zmod_fmpz_mat + ccall((:fmpz_mod_mat_one, libflint), Nothing, + (Ref{T}, Ref{fmpz_mod_ctx_struct}), a, base_ring(a).ninv) + return a +end + +function neg!(z::T, a::T) where T <: Zmod_fmpz_mat + ccall((:fmpz_mod_mat_neg, libflint), Nothing, + (Ref{T}, Ref{T}, Ref{fmpz_mod_ctx_struct}), z, a, base_ring(a).ninv) + return z +end + function mul!(a::T, b::T, c::T) where T <: Zmod_fmpz_mat ccall((:fmpz_mod_mat_mul, libflint), Nothing, (Ref{T}, Ref{T}, Ref{T}, Ref{fmpz_mod_ctx_struct}), @@ -260,12 +270,6 @@ function add!(a::T, b::T, c::T) where T <: Zmod_fmpz_mat return a end -function zero!(a::T) where T <: Zmod_fmpz_mat - ccall((:fmpz_mod_mat_zero, libflint), Nothing, - (Ref{T}, Ref{fmpz_mod_ctx_struct}), a, base_ring(a).ninv) - return a -end - function mul!(z::Vector{ZZRingElem}, a::T, b::Vector{ZZRingElem}) where T <: Zmod_fmpz_mat ccall((:fmpz_mod_mat_mul_fmpz_vec_ptr, libflint), Nothing, (Ptr{Ref{ZZRingElem}}, Ref{T}, Ptr{Ref{ZZRingElem}}, Int, Ref{fmpz_mod_ctx_struct}), diff --git a/src/flint/fmpz_mod_mpoly.jl b/src/flint/fmpz_mod_mpoly.jl index db15e2d90..026441028 100644 --- a/src/flint/fmpz_mod_mpoly.jl +++ b/src/flint/fmpz_mod_mpoly.jl @@ -99,21 +99,9 @@ for (etype, rtype, ftype, ctype) in ( # a, a.parent) end - function one(R::($rtype)) - z = R() - ccall((:fmpz_mod_mpoly_one, libflint), Nothing, - (Ref{($etype)}, Ref{($rtype)}), - z, R) - return z - end + one(R::($rtype)) = one!(R()) - function zero(R::($rtype)) - z = R() - ccall((:fmpz_mod_mpoly_zero, libflint), Nothing, - (Ref{($etype)}, Ref{($rtype)}), - z, R) - return z - end + zero(R::($rtype)) = zero!(R()) function isone(a::($etype)) return Bool(ccall((:fmpz_mod_mpoly_is_one, libflint), Cint, @@ -311,13 +299,7 @@ for (etype, rtype, ftype, ctype) in ( # ############################################################################### - function -(a::($etype)) - z = parent(a)() - ccall((:fmpz_mod_mpoly_neg, libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), - z, a, parent(a)) - return z - end + -(a::($etype)) = neg!(parent(a)(), a) function +(a::($etype), b::($etype)) check_parent(a, b) @@ -414,13 +396,7 @@ for (etype, rtype, ftype, ctype) in ( -(a::($etype), b::Integer) = a - base_ring(parent(a))(b) - function -(b::Integer, a::($etype)) - z = a - b - ccall((:fmpz_mod_mpoly_neg, libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), - z, z, parent(a)) - return z - end + -(b::Integer, a::($etype)) = neg!(a - b) function *(a::($etype), b::UInt) z = parent(a)() @@ -772,6 +748,20 @@ for (etype, rtype, ftype, ctype) in ( return a end + function one!(a::($etype)) + ccall((:fmpz_mod_mpoly_one, libflint), Nothing, + (Ref{($etype)}, Ref{($rtype)}), + a, parent(a)) + return a + end + + function neg!(z::($etype), a::($etype)) + ccall((:fmpz_mod_mpoly_neg, libflint), Nothing, + (Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), + z, a, parent(a)) + return z + end + function add!(a::($etype), b::($etype), c::($etype)) ccall((:fmpz_mod_mpoly_add, libflint), Nothing, (Ref{($etype)}, Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), diff --git a/src/flint/fmpz_mod_poly.jl b/src/flint/fmpz_mod_poly.jl index d753f59ac..ac6982c31 100644 --- a/src/flint/fmpz_mod_poly.jl +++ b/src/flint/fmpz_mod_poly.jl @@ -125,13 +125,7 @@ canonical_unit(a::Zmodn_fmpz_poly) = canonical_unit(leading_coefficient(a)) # ################################################################################ -function -(x::T) where {T <: Zmodn_fmpz_poly} - z = parent(x)() - ccall((:fmpz_mod_poly_neg, libflint), Nothing, - (Ref{T}, Ref{T}, Ref{fmpz_mod_ctx_struct}), - z, x, x.parent.base_ring.ninv) - return z -end +-(x::T) where {T <: Zmodn_fmpz_poly} = neg!(parent(x)(), x) ################################################################################ # @@ -876,6 +870,20 @@ function zero!(x::T) where {T <: Zmodn_fmpz_poly} return x end +function one!(x::T) where {T <: Zmodn_fmpz_poly} + ccall((:fmpz_mod_poly_one, libflint), Nothing, + (Ref{T}, Ref{fmpz_mod_ctx_struct}), + x, x.parent.base_ring.ninv) + return x +end + +function neg!(z::T, x::T) where {T <: Zmodn_fmpz_poly} + ccall((:fmpz_mod_poly_neg, libflint), Nothing, + (Ref{T}, Ref{T}, Ref{fmpz_mod_ctx_struct}), + z, x, x.parent.base_ring.ninv) + return z +end + function fit!(x::T, n::Int) where {T <: Zmodn_fmpz_poly} ccall((:fmpz_mod_poly_fit_length, libflint), Nothing, (Ref{T}, Int, Ref{fmpz_mod_ctx_struct}), diff --git a/src/flint/fmpz_mod_rel_series.jl b/src/flint/fmpz_mod_rel_series.jl index bd101204c..42e281b68 100644 --- a/src/flint/fmpz_mod_rel_series.jl +++ b/src/flint/fmpz_mod_rel_series.jl @@ -694,6 +694,15 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in ( return x end + function one!(x::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)}, Ref{($ctype)}), + x, x.parent.base_ring.ninv) + x.prec = parent(x).prec_max + x.val = parent(x).prec_max + return x + end + function fit!(x::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int, Ref{($ctype)}), diff --git a/src/flint/fmpz_mpoly.jl b/src/flint/fmpz_mpoly.jl index 3ab6dfeff..ceda5a97d 100644 --- a/src/flint/fmpz_mpoly.jl +++ b/src/flint/fmpz_mpoly.jl @@ -80,19 +80,9 @@ function length(a::ZZMPolyRingElem) return n end -function one(R::ZZMPolyRing) - z = R() - ccall((:fmpz_mpoly_one, libflint), Nothing, - (Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, R) - return z -end +one(R::ZZMPolyRing) = one!(R()) -function zero(R::ZZMPolyRing) - z = R() - ccall((:fmpz_mpoly_zero, libflint), Nothing, - (Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, R) - return z -end +zero(R::ZZMPolyRing) = zero!(R()) function isone(a::ZZMPolyRingElem) b = ccall((:fmpz_mpoly_is_one, libflint), Cint, @@ -781,6 +771,18 @@ function zero!(a::ZZMPolyRingElem) return a end +function one!(a::ZZMPolyRingElem) + ccall((:fmpz_mpoly_one, libflint), Nothing, + (Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), a, a.parent) + return a +end + +function neg!(z::ZZMPolyRingElem, a::ZZMPolyRingElem) + ccall((:fmpz_mpoly_neg, libflint), Nothing, + (Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, a, a.parent) + return z +end + function add!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::ZZMPolyRingElem) ccall((:fmpz_mpoly_add, libflint), Nothing, (Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, diff --git a/src/flint/fmpz_poly.jl b/src/flint/fmpz_poly.jl index c1ccf333e..a3c82075a 100644 --- a/src/flint/fmpz_poly.jl +++ b/src/flint/fmpz_poly.jl @@ -115,12 +115,7 @@ canonical_unit(a::ZZPolyRingElem) = canonical_unit(leading_coefficient(a)) # ############################################################################### -function -(x::ZZPolyRingElem) - z = parent(x)() - ccall((:fmpz_poly_neg, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), z, x) - return z -end +-(x::ZZPolyRingElem) = neg!(parent(x)(), x) ############################################################################### # @@ -860,12 +855,24 @@ end # ############################################################################### -function zero!(z::ZZPolyRingElem) +function zero!(z::ZZPolyRingElemOrPtr) ccall((:fmpz_poly_zero, libflint), Nothing, (Ref{ZZPolyRingElem},), z) return z end +function one!(z::ZZPolyRingElemOrPtr) + ccall((:fmpz_poly_one, libflint), Nothing, + (Ref{ZZPolyRingElem},), z) + return z +end + +function neg!(z::ZZPolyRingElemOrPtr, a::ZZPolyRingElemOrPtr) + ccall((:fmpz_poly_neg, libflint), Nothing, + (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), z, a) + return z +end + function fit!(z::ZZPolyRingElem, n::Int) ccall((:fmpz_poly_fit_length, libflint), Nothing, (Ref{ZZPolyRingElem}, Int), z, n) diff --git a/src/flint/fmpz_rel_series.jl b/src/flint/fmpz_rel_series.jl index 76493ee8f..e5af1624e 100644 --- a/src/flint/fmpz_rel_series.jl +++ b/src/flint/fmpz_rel_series.jl @@ -591,6 +591,14 @@ function zero!(x::ZZRelPowerSeriesRingElem) return x end +function one!(x::ZZRelPowerSeriesRingElem) + ccall((:fmpz_poly_one, libflint), Nothing, + (Ref{ZZRelPowerSeriesRingElem},), x) + x.prec = parent(x).prec_max + x.val = parent(x).prec_max + return x +end + function fit!(z::ZZRelPowerSeriesRingElem, n::Int) ccall((:fmpz_poly_fit_length, libflint), Nothing, (Ref{ZZRelPowerSeriesRingElem}, Int), z, n) diff --git a/src/flint/fq.jl b/src/flint/fq.jl index f202ffd95..eb6b54d67 100644 --- a/src/flint/fq.jl +++ b/src/flint/fq.jl @@ -54,17 +54,9 @@ function coeff(x::FqPolyRepFieldElem, n::Int) return z end -function zero(a::FqPolyRepField) - d = a() - ccall((:fq_zero, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), d, a) - return d -end +zero(a::FqPolyRepField) = zero!(a()) -function one(a::FqPolyRepField) - d = a() - ccall((:fq_one, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), d, a) - return d -end +one(a::FqPolyRepField) = one!(a()) @doc raw""" gen(a::FqPolyRepField) @@ -177,12 +169,7 @@ end # ############################################################################### -function -(x::FqPolyRepFieldElem) - z = parent(x)() - ccall((:fq_neg, libflint), Nothing, - (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), z, x, x.parent) - return z -end +-(x::FqPolyRepFieldElem) = neg!(parent(x)(), x) ############################################################################### # @@ -479,6 +466,18 @@ function zero!(z::FqPolyRepFieldElem) return z end +function one!(z::FqPolyRepFieldElem) + ccall((:fq_one, libflint), Nothing, + (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), z, z.parent) + return z +end + +function neg!(z::FqPolyRepFieldElem, a::FqPolyRepFieldElem) + ccall((:fq_neg, libflint), Nothing, + (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), z, a, a.parent) + return z +end + function mul!(z::FqPolyRepFieldElem, x::FqPolyRepFieldElem, y::FqPolyRepFieldElem) ccall((:fq_mul, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), z, x, y, y.parent) diff --git a/src/flint/fq_abs_series.jl b/src/flint/fq_abs_series.jl index 1b628cb95..7b8f97238 100644 --- a/src/flint/fq_abs_series.jl +++ b/src/flint/fq_abs_series.jl @@ -583,6 +583,13 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in ( return z end + function one!(z::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)}, Ref{($ctype)}), z, base_ring(z)) + z.prec = parent(z).prec_max + return z + end + function fit!(z::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int, Ref{($ctype)}), diff --git a/src/flint/fq_default.jl b/src/flint/fq_default.jl index 0bc393851..1d77f3789 100644 --- a/src/flint/fq_default.jl +++ b/src/flint/fq_default.jl @@ -46,17 +46,9 @@ function _coeff(x::FqFieldElem, n::Int) return z end -function zero(a::FqField) - d = a() - ccall((:fq_default_zero, libflint), Nothing, (Ref{FqFieldElem}, Ref{FqField}), d, a) - return d -end +zero(a::FqField) = zero!(a()) -function one(a::FqField) - d = a() - ccall((:fq_default_one, libflint), Nothing, (Ref{FqFieldElem}, Ref{FqField}), d, a) - return d -end +one(a::FqField) = one!(a()) function _gen(a::FqField) d = a() @@ -400,12 +392,7 @@ end # ############################################################################### -function -(x::FqFieldElem) - z = parent(x)() - ccall((:fq_default_neg, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, x.parent) - return z -end +-(x::FqFieldElem) = neg!(parent(x)(), x) ############################################################################### # @@ -675,6 +662,19 @@ function zero!(z::FqFieldElem) return z end +function one!(z::FqFieldElem) + ccall((:fq_default_one, libflint), Nothing, + (Ref{FqFieldElem}, Ref{FqField}), z, z.parent) + z.poly = nothing + return z +end + +function neg!(z::FqFieldElem, a::FqFieldElem) + ccall((:fq_default_neg, libflint), Nothing, + (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, a, a.parent) + return z +end + function mul!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) ccall((:fq_default_mul, libflint), Nothing, (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, y.parent) diff --git a/src/flint/fq_default_abs_series.jl b/src/flint/fq_default_abs_series.jl index 3e1f176a0..e979bdfd6 100644 --- a/src/flint/fq_default_abs_series.jl +++ b/src/flint/fq_default_abs_series.jl @@ -165,14 +165,7 @@ end # ############################################################################### -function -(x::FqAbsPowerSeriesRingElem) - z = parent(x)() - ccall((:fq_default_poly_neg, libflint), Nothing, - (Ref{FqAbsPowerSeriesRingElem}, Ref{FqAbsPowerSeriesRingElem}, Ref{FqField}), - z, x, base_ring(x)) - z.prec = x.prec - return z -end +-(x::FqAbsPowerSeriesRingElem) = neg!(parent(x)(), x) ############################################################################### # @@ -609,6 +602,21 @@ function zero!(z::FqAbsPowerSeriesRingElem) return z end +function one!(z::FqAbsPowerSeriesRingElem) + ccall((:fq_default_poly_one, libflint), Nothing, + (Ref{FqAbsPowerSeriesRingElem}, Ref{FqField}), z, base_ring(z)) + z.prec = parent(z).prec_max + return z +end + +function neg!(z::FqAbsPowerSeriesRingElem, a::FqAbsPowerSeriesRingElem) + ccall((:fq_default_poly_neg, libflint), Nothing, + (Ref{FqAbsPowerSeriesRingElem}, Ref{FqAbsPowerSeriesRingElem}, Ref{FqField}), + z, a, base_ring(a)) + z.prec = a.prec + return z +end + function fit!(z::FqAbsPowerSeriesRingElem, n::Int) ccall((:fq_default_poly_fit_length, libflint), Nothing, (Ref{FqAbsPowerSeriesRingElem}, Int, Ref{FqField}), diff --git a/src/flint/fq_default_mat.jl b/src/flint/fq_default_mat.jl index e522668b7..1cdc18518 100644 --- a/src/flint/fq_default_mat.jl +++ b/src/flint/fq_default_mat.jl @@ -213,12 +213,7 @@ reverse_cols(x::FqMatrix) = reverse_cols!(deepcopy(x)) # ################################################################################ -function -(x::FqMatrix) - z = similar(x) - ccall((:fq_default_mat_neg, libflint), Nothing, - (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), z, x, base_ring(x)) - return z -end +-(x::FqMatrix) = neg!(similar(x), x) ################################################################################ # @@ -261,6 +256,24 @@ end # ################################################################################ +function zero!(a::FqMatrix) + ccall((:fq_default_mat_zero, libflint), Nothing, + (Ref{FqMatrix}, Ref{FqField}), a, base_ring(a)) + return a +end + +function one!(a::FqMatrix) + ccall((:fq_default_mat_one, libflint), Nothing, + (Ref{FqMatrix}, Ref{FqField}), a, base_ring(a)) + return a +end + +function neg!(z::FqMatrix, a::FqMatrix) + ccall((:fq_default_mat_neg, libflint), Nothing, + (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), z, a, base_ring(a)) + return z +end + function mul!(a::FqMatrix, b::FqMatrix, c::FqMatrix) ccall((:fq_default_mat_mul, libflint), Nothing, (Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqMatrix}, Ref{FqField}), @@ -295,12 +308,6 @@ function add!(a::FqMatrix, b::FqMatrix, c::FqMatrix) return a end -function zero!(a::FqMatrix) - ccall((:fq_default_mat_zero, libflint), Nothing, - (Ref{FqMatrix}, Ref{FqField}), a, base_ring(a)) - return a -end - function Generic.add_one!(a::FqMatrix, i::Int, j::Int) @boundscheck _checkbounds(a, i, j) F = base_ring(a) diff --git a/src/flint/fq_default_mpoly.jl b/src/flint/fq_default_mpoly.jl index 0ac498131..9bb39f75d 100644 --- a/src/flint/fq_default_mpoly.jl +++ b/src/flint/fq_default_mpoly.jl @@ -441,6 +441,16 @@ function zero!(a::FqMPolyRingElem) return a end +function one!(a::FqMPolyRingElem) + a.data = one!(a.data) + return a +end + +function neg!(z::FqMPolyRingElem, a::FqMPolyRingElem) + z.data = neg!(z.data, a.data) + return z +end + function add!(a::FqMPolyRingElem, b::FqMPolyRingElem, c::FqMPolyRingElem) a.data = add!(a.data, b.data, c.data) return a diff --git a/src/flint/fq_default_poly.jl b/src/flint/fq_default_poly.jl index 3ea91c36f..c632e74de 100644 --- a/src/flint/fq_default_poly.jl +++ b/src/flint/fq_default_poly.jl @@ -109,13 +109,7 @@ canonical_unit(a::FqPolyRingElem) = canonical_unit(leading_coefficient(a)) # ################################################################################ -function -(x::FqPolyRingElem) - z = parent(x)() - ccall((:fq_default_poly_neg, libflint), Nothing, - (Ref{FqPolyRingElem}, Ref{FqPolyRingElem}, Ref{FqField}), - z, x, base_ring(parent(x))) - return z -end +-(x::FqPolyRingElem) = neg!(parent(x)(), x) ################################################################################ # @@ -744,6 +738,20 @@ function zero!(z::FqPolyRingElem) return z end +function one!(z::FqPolyRingElem) + ccall((:fq_default_poly_one, libflint), Nothing, + (Ref{FqPolyRingElem}, Ref{FqField}), + z, base_ring(parent(z))) + return z +end + +function neg!(z::FqPolyRingElem, a::FqPolyRingElem) + ccall((:fq_default_poly_neg, libflint), Nothing, + (Ref{FqPolyRingElem}, Ref{FqPolyRingElem}, Ref{FqField}), + z, a, base_ring(parent(a))) + return z +end + function fit!(z::FqPolyRingElem, n::Int) ccall((:fq_default_poly_fit_length, libflint), Nothing, (Ref{FqPolyRingElem}, Int, Ref{FqField}), diff --git a/src/flint/fq_default_rel_series.jl b/src/flint/fq_default_rel_series.jl index 3d31a1cd7..82f693ea2 100644 --- a/src/flint/fq_default_rel_series.jl +++ b/src/flint/fq_default_rel_series.jl @@ -161,15 +161,7 @@ end # ############################################################################### -function -(x::FqRelPowerSeriesRingElem) - z = parent(x)() - ccall((:fq_default_poly_neg, libflint), Nothing, - (Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Ref{FqField}), - z, x, base_ring(x)) - z.prec = x.prec - z.val = x.val - return z -end +-(x::FqRelPowerSeriesRingElem) = neg!(parent(x)(), x) ############################################################################### # @@ -243,9 +235,7 @@ function -(a::FqRelPowerSeriesRingElem, b::FqRelPowerSeriesRingElem) ccall((:fq_default_poly_shift_left, libflint), Nothing, (Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Int, Ref{FqField}), z, z, b.val - a.val, ctx) - ccall((:fq_default_poly_neg, libflint), Nothing, - (Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Ref{FqField}), - z, z, ctx) + z = neg!(z) ccall((:fq_default_poly_add_series, libflint), Nothing, (Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Int, Ref{FqField}), @@ -671,6 +661,22 @@ function zero!(x::FqRelPowerSeriesRingElem) return x end +function one!(x::FqRelPowerSeriesRingElem) + ccall((:fq_default_poly_one, libflint), Nothing, + (Ref{FqRelPowerSeriesRingElem}, Ref{FqField}), x, base_ring(x)) + x.prec = parent(x).prec_max + x.val = parent(x).prec_max + return x +end + +function neg!(z::FqRelPowerSeriesRingElem, x::FqRelPowerSeriesRingElem) + ccall((:fq_default_poly_neg, libflint), Nothing, + (Ref{FqRelPowerSeriesRingElem}, Ref{FqRelPowerSeriesRingElem}, Ref{FqField}), z, x, base_ring(x)) + z.prec = x.prec + z.val = x.val + return z +end + function fit!(z::FqRelPowerSeriesRingElem, n::Int) ccall((:fq_default_poly_fit_length, libflint), Nothing, (Ref{FqRelPowerSeriesRingElem}, Int, Ref{FqField}), diff --git a/src/flint/fq_mat.jl b/src/flint/fq_mat.jl index bfdd0c84e..670bbc501 100644 --- a/src/flint/fq_mat.jl +++ b/src/flint/fq_mat.jl @@ -208,12 +208,7 @@ reverse_cols(x::FqPolyRepMatrix) = reverse_cols!(deepcopy(x)) # ################################################################################ -function -(x::FqPolyRepMatrix) - z = similar(x) - ccall((:fq_mat_neg, libflint), Nothing, - (Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), z, x, base_ring(x)) - return z -end +-(x::FqPolyRepMatrix) = neg!(similar(x), x) ################################################################################ # @@ -249,6 +244,24 @@ end # ################################################################################ +function zero!(a::FqPolyRepMatrix) + ccall((:fq_mat_zero, libflint), Nothing, + (Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), a, base_ring(a)) + return a +end + +function one!(a::FqPolyRepMatrix) + ccall((:fq_mat_one, libflint), Nothing, + (Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), a, base_ring(a)) + return a +end + +function neg!(z::FqPolyRepMatrix, a::FqPolyRepMatrix) + ccall((:fq_mat_neg, libflint), Nothing, + (Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), z, a, base_ring(a)) + return z +end + function mul!(a::FqPolyRepMatrix, b::FqPolyRepMatrix, c::FqPolyRepMatrix) ccall((:fq_mat_mul, libflint), Nothing, (Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), @@ -270,12 +283,6 @@ function sub!(a::FqPolyRepMatrix, b::FqPolyRepMatrix, c::FqPolyRepMatrix) return a end -function zero!(a::FqPolyRepMatrix) - ccall((:fq_mat_zero, libflint), Nothing, - (Ref{FqPolyRepMatrix}, Ref{FqPolyRepField}), a, base_ring(a)) - return a -end - function mul!(z::Vector{FqPolyRepFieldElem}, a::FqPolyRepMatrix, b::Vector{FqPolyRepFieldElem}) ccall((:fq_mat_mul_vec_ptr, libflint), Nothing, (Ptr{Ref{FqPolyRepFieldElem}}, Ref{FqPolyRepMatrix}, Ptr{Ref{FqPolyRepFieldElem}}, Int, Ref{FqPolyRepField}), diff --git a/src/flint/fq_nmod.jl b/src/flint/fq_nmod.jl index 7c33ae01e..227fb3b8e 100644 --- a/src/flint/fq_nmod.jl +++ b/src/flint/fq_nmod.jl @@ -67,19 +67,9 @@ function setindex_raw!(x::fqPolyRepFieldElem, c::UInt, i::Int) return x end -function zero(a::fqPolyRepField) - d = a() - ccall((:fq_nmod_zero, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), d, a) - return d -end +zero(a::fqPolyRepField) = zero!(a()) -function one(a::fqPolyRepField) - d = a() - ccall((:fq_nmod_one, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), d, a) - return d -end +one(a::fqPolyRepField) = one!(a()) function gen(a::fqPolyRepField) d = a() @@ -172,12 +162,7 @@ end # ############################################################################### -function -(x::fqPolyRepFieldElem) - z = parent(x)() - ccall((:fq_nmod_neg, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, x, x.parent) - return z -end +-(x::fqPolyRepFieldElem) = neg!(parent(x)(), x) ############################################################################### # @@ -459,6 +444,24 @@ end # ############################################################################### +function zero!(z::fqPolyRepFieldElem) + ccall((:fq_nmod_zero, libflint), Nothing, + (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, z.parent) + return z +end + +function one!(z::fqPolyRepFieldElem) + ccall((:fq_nmod_one, libflint), Nothing, + (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, z.parent) + return z +end + +function neg!(z::fqPolyRepFieldElem, a::fqPolyRepFieldElem) + ccall((:fq_nmod_neg, libflint), Nothing, + (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, a, a.parent) + return z +end + function set!(z::fqPolyRepFieldElem, x::fqPolyRepFieldElem) ccall((:fq_nmod_set, libflint), Nothing, (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), @@ -489,12 +492,6 @@ function set!(z::fqPolyRepFieldElem, x::fpPolyRingElem) z, x, parent(z)) end -function zero!(z::fqPolyRepFieldElem) - ccall((:fq_nmod_zero, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), z, z.parent) - return z -end - function mul!(z::fqPolyRepFieldElem, x::fqPolyRepFieldElem, y::fqPolyRepFieldElem) ccall((:fq_nmod_mul, libflint), Nothing, (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), diff --git a/src/flint/fq_nmod_mat.jl b/src/flint/fq_nmod_mat.jl index 0c6843809..dc31cf139 100644 --- a/src/flint/fq_nmod_mat.jl +++ b/src/flint/fq_nmod_mat.jl @@ -194,12 +194,7 @@ reverse_cols(x::fqPolyRepMatrix) = reverse_cols!(deepcopy(x)) # ################################################################################ -function -(x::fqPolyRepMatrix) - z = similar(x) - ccall((:fq_nmod_mat_neg, libflint), Nothing, - (Ref{fqPolyRepMatrix}, Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), z, x, base_ring(x)) - return z -end +-(x::fqPolyRepMatrix) = neg!(similar(x), x) ################################################################################ # @@ -236,6 +231,24 @@ end # ################################################################################ +function zero!(a::fqPolyRepMatrix) + ccall((:fq_nmod_mat_zero, libflint), Nothing, + (Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), a, base_ring(a)) + return a +end + +function one!(a::fqPolyRepMatrix) + ccall((:fq_nmod_mat_one, libflint), Nothing, + (Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), a, base_ring(a)) + return a +end + +function neg!(z::fqPolyRepMatrix, a::fqPolyRepMatrix) + ccall((:fq_nmod_mat_neg, libflint), Nothing, + (Ref{fqPolyRepMatrix}, Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), z, a, base_ring(a)) + return z +end + function mul!(a::fqPolyRepMatrix, b::fqPolyRepMatrix, c::fqPolyRepMatrix) ccall((:fq_nmod_mat_mul, libflint), Nothing, (Ref{fqPolyRepMatrix}, Ref{fqPolyRepMatrix}, Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), @@ -257,12 +270,6 @@ function sub!(a::fqPolyRepMatrix, b::fqPolyRepMatrix, c::fqPolyRepMatrix) return a end -function zero!(a::fqPolyRepMatrix) - ccall((:fq_nmod_mat_zero, libflint), Nothing, - (Ref{fqPolyRepMatrix}, Ref{fqPolyRepField}), a, base_ring(a)) - return a -end - function mul!(z::Vector{fqPolyRepFieldElem}, a::fqPolyRepMatrix, b::Vector{fqPolyRepFieldElem}) ccall((:fq_nmod_mat_mul_vec_ptr, libflint), Nothing, (Ptr{Ref{fqPolyRepFieldElem}}, Ref{fqPolyRepMatrix}, Ptr{Ref{fqPolyRepFieldElem}}, Int, diff --git a/src/flint/fq_nmod_mpoly.jl b/src/flint/fq_nmod_mpoly.jl index 5720c7a6c..182c548a3 100644 --- a/src/flint/fq_nmod_mpoly.jl +++ b/src/flint/fq_nmod_mpoly.jl @@ -81,21 +81,9 @@ function length(a::fqPolyRepMPolyRingElem) return n end -function one(R::fqPolyRepMPolyRing) - z = R() - ccall((:fq_nmod_mpoly_one, libflint), Nothing, - (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}), - z, R) - return z -end +one(R::fqPolyRepMPolyRing) = one!(R()) -function zero(R::fqPolyRepMPolyRing) - z = R() - ccall((:fq_nmod_mpoly_zero, libflint), Nothing, - (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}), - z, R) - return z -end +zero(R::fqPolyRepMPolyRing) = zero!(R()) function isone(a::fqPolyRepMPolyRingElem) b = ccall((:fq_nmod_mpoly_is_one, libflint), Cint, @@ -295,13 +283,7 @@ end # ############################################################################### -function -(a::fqPolyRepMPolyRingElem) - z = parent(a)() - ccall((:fq_nmod_mpoly_neg, libflint), Nothing, - (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}), - z, a, a.parent) - return z -end +-(a::fqPolyRepMPolyRingElem) = neg!(parent(a)(), a) function +(a::fqPolyRepMPolyRingElem, b::fqPolyRepMPolyRingElem) check_parent(a, b) @@ -765,6 +747,20 @@ function zero!(a::fqPolyRepMPolyRingElem) return a end +function one!(a::fqPolyRepMPolyRingElem) + ccall((:fq_nmod_mpoly_one, libflint), Nothing, + (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}), + a, a.parent) + return a +end + +function neg!(z::fqPolyRepMPolyRingElem, a::fqPolyRepMPolyRingElem) + ccall((:fq_nmod_mpoly_neg, libflint), Nothing, + (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}), + z, a, a.parent) + return z +end + function add!(a::fqPolyRepMPolyRingElem, b::fqPolyRepMPolyRingElem, c::fqPolyRepMPolyRingElem) ccall((:fq_nmod_mpoly_add, libflint), Nothing, (Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRingElem}, diff --git a/src/flint/fq_nmod_poly.jl b/src/flint/fq_nmod_poly.jl index 195b0745a..da0c5f474 100644 --- a/src/flint/fq_nmod_poly.jl +++ b/src/flint/fq_nmod_poly.jl @@ -117,13 +117,7 @@ canonical_unit(a::fqPolyRepPolyRingElem) = canonical_unit(leading_coefficient(a) # ################################################################################ -function -(x::fqPolyRepPolyRingElem) - z = parent(x)() - ccall((:fq_nmod_poly_neg, libflint), Nothing, - (Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepField}), - z, x, base_ring(parent(x))) - return z -end +-(x::fqPolyRepPolyRingElem) = neg!(parent(x)(), x) ################################################################################ # @@ -740,6 +734,20 @@ function zero!(z::fqPolyRepPolyRingElem) return z end +function one!(z::fqPolyRepPolyRingElem) + ccall((:fq_nmod_poly_one, libflint), Nothing, + (Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepField}), + z, base_ring(parent(z))) + return z +end + +function neg!(z::fqPolyRepPolyRingElem, a::fqPolyRepPolyRingElem) + ccall((:fq_nmod_poly_neg, libflint), Nothing, + (Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepField}), + z, a, base_ring(parent(a))) + return z +end + function fit!(z::fqPolyRepPolyRingElem, n::Int) ccall((:fq_nmod_poly_fit_length, libflint), Nothing, (Ref{fqPolyRepPolyRingElem}, Int, Ref{fqPolyRepField}), diff --git a/src/flint/fq_poly.jl b/src/flint/fq_poly.jl index 5217d7e60..8f4bae140 100644 --- a/src/flint/fq_poly.jl +++ b/src/flint/fq_poly.jl @@ -117,13 +117,7 @@ canonical_unit(a::FqPolyRepPolyRingElem) = canonical_unit(leading_coefficient(a) # ################################################################################ -function -(x::FqPolyRepPolyRingElem) - z = parent(x)() - ccall((:fq_poly_neg, libflint), Nothing, - (Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepField}), - z, x, base_ring(parent(x))) - return z -end +-(x::FqPolyRepPolyRingElem) = neg!(parent(x)(), x) ################################################################################ # @@ -738,6 +732,20 @@ function zero!(z::FqPolyRepPolyRingElem) return z end +function one!(z::FqPolyRepPolyRingElem) + ccall((:fq_poly_one, libflint), Nothing, + (Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepField}), + z, base_ring(parent(z))) + return z +end + +function neg!(z::FqPolyRepPolyRingElem, a::FqPolyRepPolyRingElem) + ccall((:fq_poly_neg, libflint), Nothing, + (Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepField}), + z, a, base_ring(parent(a))) + return z +end + function fit!(z::FqPolyRepPolyRingElem, n::Int) ccall((:fq_poly_fit_length, libflint), Nothing, (Ref{FqPolyRepPolyRingElem}, Int, Ref{FqPolyRepField}), diff --git a/src/flint/fq_rel_series.jl b/src/flint/fq_rel_series.jl index 2f21f506a..63aca2a56 100644 --- a/src/flint/fq_rel_series.jl +++ b/src/flint/fq_rel_series.jl @@ -166,15 +166,7 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in ( # ############################################################################### - function -(x::($etype)) - z = parent(x)() - ccall(($(flint_fn*"_neg"), libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}, Ref{($ctype)}), - z, x, base_ring(x)) - z.prec = x.prec - z.val = x.val - return z - end + -(x::($etype)) = neg!(parent(x)(), x) ############################################################################### # @@ -643,6 +635,22 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in ( return x end + function one!(x::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)}, Ref{($ctype)}), x, base_ring(x)) + x.prec = parent(x).prec_max + x.val = parent(x).prec_max + return x + end + + function neg!(z::($etype), x::($etype)) + ccall(($(flint_fn*"_neg"), libflint), Nothing, + (Ref{($etype)}, Ref{($etype)}, Ref{($ctype)}), z, x, base_ring(x)) + z.prec = x.prec + z.val = x.val + return z + end + function fit!(z::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int, Ref{($ctype)}), diff --git a/src/flint/gfp_elem.jl b/src/flint/gfp_elem.jl index 3b4e50d3a..4951e62f7 100644 --- a/src/flint/gfp_elem.jl +++ b/src/flint/gfp_elem.jl @@ -344,6 +344,15 @@ function zero!(z::fpFieldElem) return fpFieldElem(UInt(0), R) end +function one!(z::fpFieldElem) + R = parent(z) + return fpFieldElem(UInt(0), R) +end + +function neg!(z::fpFieldElem, x::fpFieldElem) + return -x +end + function mul!(z::fpFieldElem, x::fpFieldElem, y::ZZRingElem) R = parent(x) d = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), y, R.n) diff --git a/src/flint/gfp_fmpz_elem.jl b/src/flint/gfp_fmpz_elem.jl index 5d906532f..ccc77c4da 100644 --- a/src/flint/gfp_fmpz_elem.jl +++ b/src/flint/gfp_fmpz_elem.jl @@ -354,6 +354,16 @@ function zero!(z::FpFieldElem) return z end +function one!(z::FpFieldElem) + one!(z.data) + return z +end + +function neg!(z::FpFieldElem, x::FpFieldElem) + z.data = R.n - x.data + return z +end + function mul!(z::FpFieldElem, x::FpFieldElem, y::FpFieldElem) R = parent(z) ccall((:fmpz_mod_mul, libflint), Nothing, diff --git a/src/flint/nmod.jl b/src/flint/nmod.jl index dfef43014..87af288f2 100644 --- a/src/flint/nmod.jl +++ b/src/flint/nmod.jl @@ -355,6 +355,15 @@ function zero!(z::zzModRingElem) return zzModRingElem(UInt(0), R) end +function one!(z::zzModRingElem) + R = parent(z) + return zzModRingElem(UInt(1), R) +end + +function neg!(z::zzModRingElem, x::zzModRingElem) + return -x +end + function mul!(z::zzModRingElem, x::zzModRingElem, y::zzModRingElem) return x*y end diff --git a/src/flint/nmod_abs_series.jl b/src/flint/nmod_abs_series.jl index a300e6312..1dde26be4 100644 --- a/src/flint/nmod_abs_series.jl +++ b/src/flint/nmod_abs_series.jl @@ -167,13 +167,7 @@ for (etype, rtype, mtype, brtype, flint_fn) in ( # ############################################################################### - function -(x::($etype)) - z = parent(x)() - ccall(($(flint_fn*"_neg"), libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}), z, x) - z.prec = x.prec - return z - end + -(x::($etype)) = neg!(parent(x)(), x) ############################################################################### # @@ -510,6 +504,20 @@ for (etype, rtype, mtype, brtype, flint_fn) in ( return z end + function one!(z::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)},), z) + z.prec = parent(z).prec_max + return z + end + + function neg!(z::($etype), x::($etype)) + ccall(($(flint_fn*"_neg"), libflint), Nothing, + (Ref{($etype)}, Ref{($etype)}), z, x) + z.prec = x.prec + return z + end + function fit!(z::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int), z, n) diff --git a/src/flint/nmod_mat.jl b/src/flint/nmod_mat.jl index e47300de9..c61643399 100644 --- a/src/flint/nmod_mat.jl +++ b/src/flint/nmod_mat.jl @@ -104,9 +104,7 @@ base_ring(a::T) where T <: Zmodn_mat = a.base_ring function one(a::zzModMatrixSpace) (nrows(a) != ncols(a)) && error("Matrices must be square") - z = a() - ccall((:nmod_mat_one, libflint), Nothing, (Ref{zzModMatrix}, ), z) - return z + return one!(a()) end function iszero(a::T) where T <: Zmodn_mat @@ -202,12 +200,7 @@ reverse_cols(x::T) where T <: Zmodn_mat = reverse_cols!(deepcopy(x)) # ################################################################################ -function -(x::T) where T <: Zmodn_mat - z = similar(x) - ccall((:nmod_mat_neg, libflint), Nothing, - (Ref{T}, Ref{T}), z, x) - return z -end +-(x::T) where T <: Zmodn_mat = neg!(similar(x), x) ################################################################################ # @@ -247,6 +240,21 @@ end # ################################################################################ +function zero!(a::T) where T <: Zmodn_mat + ccall((:nmod_mat_zero, libflint), Nothing, (Ref{T}, ), a) + return a +end + +function one!(a::T) where T <: Zmodn_mat + ccall((:nmod_mat_one, libflint), Nothing, (Ref{T}, ), a) + return a +end + +function neg!(z::T, a::T) where T <: Zmodn_mat + ccall((:nmod_mat_neg, libflint), Nothing, (Ref{T}, Ref{T}), z, a) + return z +end + function mul!(a::T, b::T, c::T) where T <: Zmodn_mat ccall((:nmod_mat_mul, libflint), Nothing, (Ref{T}, Ref{T}, Ref{T}), a, b, c) return a @@ -262,11 +270,6 @@ function sub!(a::T, b::T, c::T) where T <: Zmodn_mat return a end -function zero!(a::T) where T <: Zmodn_mat - ccall((:nmod_mat_zero, libflint), Nothing, (Ref{T}, ), a) - return a -end - # entries of b required to be in [0,n) function mul!(z::Vector{UInt}, a::T, b::Vector{UInt}) where T <: Zmodn_mat ccall((:nmod_mat_mul_nmod_vec, libflint), Nothing, diff --git a/src/flint/nmod_mpoly.jl b/src/flint/nmod_mpoly.jl index e45392de4..dda924467 100644 --- a/src/flint/nmod_mpoly.jl +++ b/src/flint/nmod_mpoly.jl @@ -93,21 +93,9 @@ for (etype, rtype, ftype, ctype, utype) in ( # a, a.parent) end - function one(R::($rtype)) - z = R() - ccall((:nmod_mpoly_one, libflint), Nothing, - (Ref{($etype)}, Ref{($rtype)}), - z, R) - return z - end + one(R::($rtype)) = one!(R()) - function zero(R::($rtype)) - z = R() - ccall((:nmod_mpoly_zero, libflint), Nothing, - (Ref{($etype)}, Ref{($rtype)}), - z, R) - return z - end + zero(R::($rtype)) = zero!(R()) function isone(a::($etype)) return Bool(ccall((:nmod_mpoly_is_one, libflint), Cint, @@ -297,13 +285,7 @@ for (etype, rtype, ftype, ctype, utype) in ( # ############################################################################### - function -(a::($etype)) - z = parent(a)() - ccall((:nmod_mpoly_neg, libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), - z, a, parent(a)) - return z - end + -(a::($etype)) = neg!(parent(a)(), a) function +(a::($etype), b::($etype)) check_parent(a, b) @@ -808,6 +790,20 @@ for (etype, rtype, ftype, ctype, utype) in ( return a end + function one!(a::($etype)) + ccall((:nmod_mpoly_one, libflint), Nothing, + (Ref{($etype)}, Ref{($rtype)}), + a, parent(a)) + return a + end + + function neg!(z::($etype), a::($etype)) + ccall((:nmod_mpoly_neg, libflint), Nothing, + (Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), + z, a, parent(a)) + return z + end + function add!(a::($etype), b::($etype), c::($etype)) ccall((:nmod_mpoly_add, libflint), Nothing, (Ref{($etype)}, Ref{($etype)}, Ref{($etype)}, Ref{($rtype)}), diff --git a/src/flint/nmod_poly.jl b/src/flint/nmod_poly.jl index 11e885ec3..074c43107 100644 --- a/src/flint/nmod_poly.jl +++ b/src/flint/nmod_poly.jl @@ -140,12 +140,7 @@ end # ################################################################################ -function -(x::T) where T <: Zmodn_poly - z = parent(x)() - ccall((:nmod_poly_neg, libflint), Nothing, - (Ref{T}, Ref{T}), z, x) - return z -end +-(x::T) where T <: Zmodn_poly = neg!(parent(x)(), x) ################################################################################ # @@ -895,6 +890,11 @@ function one!(a::T) where T <: Zmodn_poly return a end +function neg!(z::T, a::T) where T <: Zmodn_poly + ccall((:nmod_poly_neg, libflint), Nothing, (Ref{T}, Ref{T}), z, a) + return z +end + function fit!(x::T, n::Int) where T <: Zmodn_poly ccall((:nmod_poly_fit_length, libflint), Nothing, (Ref{T}, Int), x, n) diff --git a/src/flint/nmod_rel_series.jl b/src/flint/nmod_rel_series.jl index bedf3e4ef..1fe57dd66 100644 --- a/src/flint/nmod_rel_series.jl +++ b/src/flint/nmod_rel_series.jl @@ -161,15 +161,7 @@ for (etype, rtype, mtype, brtype, flint_fn) in ( # ############################################################################### - function -(x::($etype)) - z = parent(x)() - ccall(($(flint_fn*"_neg"), libflint), Nothing, - (Ref{($etype)}, Ref{($etype)}), - z, x) - z.prec = x.prec - z.val = x.val - return z - end + -(x::($etype)) = neg!(parent(x)(), x) ############################################################################### # @@ -695,6 +687,23 @@ for (etype, rtype, mtype, brtype, flint_fn) in ( return x end + function one!(x::($etype)) + ccall(($(flint_fn*"_one"), libflint), Nothing, + (Ref{($etype)},), x) + x.prec = parent(x).prec_max + x.val = parent(x).prec_max + return x + end + + function neg!(z::($etype), x::($etype)) + ccall(($(flint_fn*"_neg"), libflint), Nothing, + (Ref{($etype)}, Ref{($etype)}), + z, x) + z.prec = x.prec + z.val = x.val + return z + end + function fit!(x::($etype), n::Int) ccall(($(flint_fn*"_fit_length"), libflint), Nothing, (Ref{($etype)}, Int), x, n) From 6bcd58f046acbccf581c706b6eabac90a626b1a0 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 1 Oct 2024 01:30:38 +0200 Subject: [PATCH 2/3] Update src/flint/gfp_elem.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lars Göttgens --- src/flint/gfp_elem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flint/gfp_elem.jl b/src/flint/gfp_elem.jl index 4951e62f7..0883df730 100644 --- a/src/flint/gfp_elem.jl +++ b/src/flint/gfp_elem.jl @@ -346,7 +346,7 @@ end function one!(z::fpFieldElem) R = parent(z) - return fpFieldElem(UInt(0), R) + return fpFieldElem(UInt(1), R) end function neg!(z::fpFieldElem, x::fpFieldElem) From 587d0342dc7ac322eabc65a2b156a03c6150d45c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 1 Oct 2024 09:29:55 +0200 Subject: [PATCH 3/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lars Göttgens --- src/flint/fmpq_rel_series.jl | 2 +- src/flint/fmpz_mod_rel_series.jl | 2 +- src/flint/fmpz_rel_series.jl | 2 +- src/flint/fq_default_rel_series.jl | 2 +- src/flint/fq_rel_series.jl | 2 +- src/flint/nmod_rel_series.jl | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/flint/fmpq_rel_series.jl b/src/flint/fmpq_rel_series.jl index 70c146ba1..56d59afad 100644 --- a/src/flint/fmpq_rel_series.jl +++ b/src/flint/fmpq_rel_series.jl @@ -904,7 +904,7 @@ function one!(z::QQRelPowerSeriesRingElem) ccall((:fmpq_poly_one, libflint), Nothing, (Ref{QQRelPowerSeriesRingElem},), z) z.prec = parent(z).prec_max - z.val = parent(z).prec_max + z.val = 0 return z end diff --git a/src/flint/fmpz_mod_rel_series.jl b/src/flint/fmpz_mod_rel_series.jl index 42e281b68..07905aef7 100644 --- a/src/flint/fmpz_mod_rel_series.jl +++ b/src/flint/fmpz_mod_rel_series.jl @@ -699,7 +699,7 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in ( (Ref{($etype)}, Ref{($ctype)}), x, x.parent.base_ring.ninv) x.prec = parent(x).prec_max - x.val = parent(x).prec_max + x.val = 0 return x end diff --git a/src/flint/fmpz_rel_series.jl b/src/flint/fmpz_rel_series.jl index e5af1624e..59c9185ba 100644 --- a/src/flint/fmpz_rel_series.jl +++ b/src/flint/fmpz_rel_series.jl @@ -595,7 +595,7 @@ function one!(x::ZZRelPowerSeriesRingElem) ccall((:fmpz_poly_one, libflint), Nothing, (Ref{ZZRelPowerSeriesRingElem},), x) x.prec = parent(x).prec_max - x.val = parent(x).prec_max + x.val = 0 return x end diff --git a/src/flint/fq_default_rel_series.jl b/src/flint/fq_default_rel_series.jl index 82f693ea2..f5e850701 100644 --- a/src/flint/fq_default_rel_series.jl +++ b/src/flint/fq_default_rel_series.jl @@ -665,7 +665,7 @@ function one!(x::FqRelPowerSeriesRingElem) ccall((:fq_default_poly_one, libflint), Nothing, (Ref{FqRelPowerSeriesRingElem}, Ref{FqField}), x, base_ring(x)) x.prec = parent(x).prec_max - x.val = parent(x).prec_max + x.val = 0 return x end diff --git a/src/flint/fq_rel_series.jl b/src/flint/fq_rel_series.jl index 63aca2a56..43b9e6f31 100644 --- a/src/flint/fq_rel_series.jl +++ b/src/flint/fq_rel_series.jl @@ -639,7 +639,7 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in ( ccall(($(flint_fn*"_one"), libflint), Nothing, (Ref{($etype)}, Ref{($ctype)}), x, base_ring(x)) x.prec = parent(x).prec_max - x.val = parent(x).prec_max + x.val = 0 return x end diff --git a/src/flint/nmod_rel_series.jl b/src/flint/nmod_rel_series.jl index 1fe57dd66..7c60d3aac 100644 --- a/src/flint/nmod_rel_series.jl +++ b/src/flint/nmod_rel_series.jl @@ -691,7 +691,7 @@ for (etype, rtype, mtype, brtype, flint_fn) in ( ccall(($(flint_fn*"_one"), libflint), Nothing, (Ref{($etype)},), x) x.prec = parent(x).prec_max - x.val = parent(x).prec_max + x.val = 0 return x end