diff --git a/src/AbsSeries.jl b/src/AbsSeries.jl index 8a25508a2..395a4a8a7 100644 --- a/src/AbsSeries.jl +++ b/src/AbsSeries.jl @@ -81,6 +81,13 @@ function Base.hash(a::AbsPowerSeriesRingElem, h::UInt) return b end +function set_precision!(a::AbsPowerSeriesRingElem, prec::Int) + prec < 0 && throw(DomainError(prec, "Precision must be non-negative")) + a = truncate!(a, prec) + _set_precision_raw!(a, prec) + return a +end + ############################################################################### # # Similar and zero @@ -405,22 +412,7 @@ end Return $a$ truncated to $n$ terms. """ function truncate(a::AbsPowerSeriesRingElem{T}, n::Int) where T <: RingElement - n < 0 && throw(DomainError(n, "n must be >= 0")) - len = length(a) - if precision(a) <= n - return a - end - z = parent(a)() - fit!(z, n) - z = _set_precision_raw!(z, n) - for i = 1:min(n, len) - z = setcoeff!(z, i - 1, coeff(a, i - 1)) - end - for i = len + 1:n - z = setcoeff!(z, i - 1, zero(base_ring(a))) - end - z = set_length!(z, normalise(z, n)) - return z + return truncate!(deepcopy(a), n) end ############################################################################### diff --git a/src/RelSeries.jl b/src/RelSeries.jl index 8a07f658c..04d1cde86 100644 --- a/src/RelSeries.jl +++ b/src/RelSeries.jl @@ -95,15 +95,14 @@ function set_length!(a::SeriesElem, len::Int) return a end -# TODO: set_precision! for the generic types RelSeries and AbsSeries -# truncates the underlying polynomial since #1773. In a breaking release, -# this should possibly also happen for the abstract types. Alternatively, -# this set_precision! should be renamed (and only be kept as a purely internal -# setter function). -function set_precision!(a::SeriesElem, prec::Int) - # TODO - _set_precision_raw!(a, prec) - return a +function set_precision!(a::RelPowerSeriesRingElem, prec::Int) + prec < 0 && throw(DomainError(prec, "Precision must be non-negative")) + a = truncate!(a, prec) + _set_precision_raw!(a, prec) + if is_zero(a) + set_valuation!(a, prec) + end + return a end function _set_precision_raw!(a::SeriesElem, prec::Int) @@ -581,27 +580,7 @@ end Return $a$ truncated to (absolute) precision $n$. """ function truncate(a::RelPowerSeriesRingElem{T}, n::Int) where T <: RingElement - n < 0 && throw(DomainError(n, "n must be >= 0")) - alen = pol_length(a) - aprec = precision(a) - aval = valuation(a) - if aprec <= n - return a - end - z = parent(a)() - z = _set_precision_raw!(z, n) - if n <= aval - z = set_length!(z, 0) - z = set_valuation!(z, n) - else - fit!(z, n - aval) - z = set_valuation!(z, aval) - for i = 1:min(n - aval, alen) - z = setcoeff!(z, i - 1, polcoeff(a, i - 1)) - end - z = set_length!(z, normalise(z, n - aval)) - end - return z + return truncate!(deepcopy(a), n) end # Intended only for internal use, does not renormalize, assumes n >= 0 diff --git a/src/generic/AbsSeries.jl b/src/generic/AbsSeries.jl index d81020e58..d12315ee8 100644 --- a/src/generic/AbsSeries.jl +++ b/src/generic/AbsSeries.jl @@ -72,13 +72,6 @@ function characteristic(a::AbsPowerSeriesRing{T}) where T <: RingElement return characteristic(base_ring(a)) end -function set_precision!(a::AbsSeries, prec::Int) - prec < 0 && throw(DomainError(prec, "Precision must be non-negative")) - a = truncate!(a, prec) - a.prec = prec - return a -end - ############################################################################### # # Binary operations diff --git a/src/generic/RelSeries.jl b/src/generic/RelSeries.jl index 40bfda5fe..2acf06e9f 100644 --- a/src/generic/RelSeries.jl +++ b/src/generic/RelSeries.jl @@ -62,16 +62,6 @@ function characteristic(a::RelPowerSeriesRing{T}) where T <: RingElement return characteristic(base_ring(a)) end -function set_precision!(a::RelSeries, prec::Int) - prec < 0 && throw(DomainError(prec, "Precision must be non-negative")) - a = truncate!(a, prec) - a.prec = prec - if is_zero(a) - a.val = prec - end - return a -end - ############################################################################### # # Binary operators diff --git a/src/generic/exports.jl b/src/generic/exports.jl index f5bf2a319..ce1aa4623 100644 --- a/src/generic/exports.jl +++ b/src/generic/exports.jl @@ -124,6 +124,7 @@ export to_univariate export total_degree export trailing_coefficient export truncate +export truncate! export unit export universal_polynomial_ring export upscale