Skip to content

Commit

Permalink
Merge pull request #1839 from joschmitt/js/prec
Browse files Browse the repository at this point in the history
Make `set_precision` reduce the element
  • Loading branch information
fieker authored Aug 27, 2024
2 parents 1ca3c48 + b3bafa5 commit 735c97c
Show file tree
Hide file tree
Showing 30 changed files with 761 additions and 160 deletions.
28 changes: 19 additions & 9 deletions src/flint/fmpq_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ end

characteristic(::QQAbsPowerSeriesRing) = 0

function set_precision!(z::QQAbsPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -340,17 +347,20 @@ end
#
###############################################################################

function truncate(x::QQAbsPowerSeriesRingElem, prec::Int)
prec < 0 && throw(DomainError(prec, "Precision must be non-negative"))
if x.prec <= prec
function truncate(x::QQAbsPowerSeriesRingElem, k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::QQAbsPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
ccall((:fmpq_poly_set_trunc, libflint), Nothing,
(Ref{QQAbsPowerSeriesRingElem}, Ref{QQAbsPowerSeriesRingElem}, Int),
z, x, prec)
return z
ccall((:fmpq_poly_truncate, libflint), Nothing,
(Ref{QQAbsPowerSeriesRingElem}, Int),
x, k)
x.prec = k
return x
end

###############################################################################
Expand Down
42 changes: 25 additions & 17 deletions src/flint/fmpq_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ end

characteristic(::QQRelPowerSeriesRing) = 0

function set_precision!(z::QQRelPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
if is_zero(z)
z.val = k
end
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -388,27 +398,25 @@ end
#
###############################################################################

function truncate(x::QQRelPowerSeriesRingElem, prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
xlen = pol_length(x)
xprec = precision(x)
xval = valuation(x)
if xprec + xval <= prec
function truncate(x::QQRelPowerSeriesRingElem, k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::QQRelPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
if prec <= xval
z = parent(x)()
z.val = prec
z.prec = prec
if k <= valuation(x)
x = zero!(x)
x.val = k
else
z.val = xval
ccall((:fmpq_poly_set_trunc, libflint), Nothing,
(Ref{QQRelPowerSeriesRingElem}, Ref{QQRelPowerSeriesRingElem}, Int),
z, x, min(prec - xval, xlen))
ccall((:fmpq_poly_truncate, libflint), Nothing,
(Ref{QQRelPowerSeriesRingElem}, Int),
x, k - valuation(x))
end
return z
x.prec = k
return x
end

###############################################################################
Expand Down
28 changes: 19 additions & 9 deletions src/flint/fmpz_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ end

characteristic(::ZZAbsPowerSeriesRing) = 0

function set_precision!(z::ZZAbsPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -328,17 +335,20 @@ end
#
###############################################################################

function truncate(x::ZZAbsPowerSeriesRingElem, prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
if x.prec <= prec
function truncate(x::ZZAbsPowerSeriesRingElem, k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::ZZAbsPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
ccall((:fmpz_poly_set_trunc, libflint), Nothing,
(Ref{ZZAbsPowerSeriesRingElem}, Ref{ZZAbsPowerSeriesRingElem}, Int),
z, x, prec)
return z
ccall((:fmpz_poly_truncate, libflint), Nothing,
(Ref{ZZAbsPowerSeriesRingElem}, Int),
x, k)
x.prec = k
return x
end

###############################################################################
Expand Down
29 changes: 19 additions & 10 deletions src/flint/fmpz_mod_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in (

characteristic(R::($rtype)) = characteristic(base_ring(R))

function set_precision!(z::($etype), k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -345,18 +352,20 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in (
#
###############################################################################

function truncate(x::($etype), prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
if x.prec <= prec
function truncate(x::($etype), k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::($etype), k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
ccall(($(flint_fn*"_set_trunc"), libflint), Nothing,
(Ref{($etype)}, Ref{($etype)}, Int,
Ref{($ctype)}),
z, x, prec, x.parent.base_ring.ninv)
return z
ccall(($(flint_fn*"_truncate"), libflint), Nothing,
(Ref{($etype)}, Int, Ref{($ctype)}),
x, k, x.parent.base_ring.ninv)
x.prec = k
return x
end

###############################################################################
Expand Down
43 changes: 25 additions & 18 deletions src/flint/fmpz_mod_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in (

characteristic(R::($rtype)) = modulus(R)

function set_precision!(z::($etype), k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
if is_zero(z)
z.val = k
end
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -399,28 +409,25 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in (
#
###############################################################################

function truncate(x::($etype), prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
xlen = pol_length(x)
xprec = precision(x)
xval = valuation(x)
if xprec + xval <= prec
function truncate(x::($etype), k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::($etype), k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
if prec <= xval
z = parent(x)()
z.val = prec
z.prec = prec
if k <= valuation(x)
x = zero!(x)
x.val = k
else
z.val = xval
ccall(($(flint_fn*"_set_trunc"), libflint), Nothing,
(Ref{($etype)}, Ref{($etype)}, Int,
Ref{($ctype)}),
z, x, min(prec - xval, xlen), x.parent.base_ring.ninv)
ccall(($(flint_fn*"_truncate"), libflint), Nothing,
(Ref{($etype)}, Int, Ref{$(ctype)}),
x, k - valuation(x), x.parent.base_ring.ninv)
end
return z
x.prec = k
return x
end

###############################################################################
Expand Down
42 changes: 25 additions & 17 deletions src/flint/fmpz_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ end

characteristic(::ZZRelPowerSeriesRing) = 0

function set_precision!(z::ZZRelPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
if is_zero(z)
z.val = k
end
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -357,27 +367,25 @@ end
#
###############################################################################

function truncate(x::ZZRelPowerSeriesRingElem, prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
xlen = pol_length(x)
xprec = precision(x)
xval = valuation(x)
if xprec + xval <= prec
function truncate(x::ZZRelPowerSeriesRingElem, k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::ZZRelPowerSeriesRingElem, k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
if prec <= xval
z = parent(x)()
z.val = prec
z.prec = prec
if k <= valuation(x)
x = zero!(x)
x.val = k
else
z.val = xval
ccall((:fmpz_poly_set_trunc, libflint), Nothing,
(Ref{ZZRelPowerSeriesRingElem}, Ref{ZZRelPowerSeriesRingElem}, Int),
z, x, min(prec - xval, xlen))
ccall((:fmpz_poly_truncate, libflint), Nothing,
(Ref{ZZRelPowerSeriesRingElem}, Int),
x, k - valuation(x))
end
return z
x.prec = k
return x
end

###############################################################################
Expand Down
28 changes: 19 additions & 9 deletions src/flint/fq_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in (

characteristic(R::($rtype)) = characteristic(base_ring(R))

function set_precision!(z::($etype), k::Int)
k < 0 && throw(DomainError(k, "Precision must be non-negative"))
z = truncate!(z, k)
z.prec = k
return z
end

###############################################################################
#
# Similar
Expand Down Expand Up @@ -305,17 +312,20 @@ for (etype, rtype, ctype, btype, flint_fn, flint_tail) in (
#
###############################################################################

function truncate(x::($etype), prec::Int)
prec < 0 && throw(DomainError(prec, "Index must be non-negative"))
if x.prec <= prec
function truncate(x::($etype), k::Int)
return truncate!(deepcopy(x), k)
end

function truncate!(x::($etype), k::Int)
k < 0 && throw(DomainError(k, "Index must be non-negative"))
if precision(x) <= k
return x
end
z = parent(x)()
z.prec = prec
ccall(($(flint_fn*"_set_trunc"), libflint), Nothing,
(Ref{($etype)}, Ref{($etype)}, Int, Ref{($ctype)}),
z, x, prec, base_ring(x))
return z
ccall(($(flint_fn*"_truncate"), libflint), Nothing,
(Ref{($etype)}, Int, Ref{($ctype)}),
x, k, base_ring(x))
x.prec = k
return x
end

###############################################################################
Expand Down
Loading

0 comments on commit 735c97c

Please sign in to comment.