Skip to content

Commit

Permalink
Move the "generic" set_precision! to "abstract"
Browse files Browse the repository at this point in the history
  • Loading branch information
joschmitt committed Aug 22, 2024
1 parent 44c56a0 commit 225232c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 63 deletions.
24 changes: 8 additions & 16 deletions src/AbsSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

###############################################################################
Expand Down
39 changes: 9 additions & 30 deletions src/RelSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions src/generic/AbsSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions src/generic/RelSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/generic/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 225232c

Please sign in to comment.