Skip to content

Commit

Permalink
feat: improve SparsePoly evaluation (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Oct 1, 2024
1 parent fc9baf7 commit 3296e67
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/generic/SparsePoly.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
###############################################################################
##############################################################################
#
# SparsePoly.jl : Generic sparse univariate polynomials over rings
#
Expand Down Expand Up @@ -478,13 +478,13 @@ function evaluate(a::SparsePoly{T}, b::S) where {S <: RingElement, T <: RingElem
if a.length == 0
return base_ring(a)()
end
r = a.coeffs[a.length]
r = deepcopy(a.coeffs[a.length])
for i = 1:a.length - 1
r *= b^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i]))
r += a.coeffs[a.length - i]
r = mul!(r, r, b^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i])))
r = add!(r, r, a.coeffs[a.length - i])
end
if a.exps[1] != 0
r *= b^(reinterpret(Int, a.exps[1]))
r = mul!(r, r, b^(reinterpret(Int, a.exps[1])))
end
return r
end
Expand All @@ -493,13 +493,13 @@ function evaluate(a::SparsePoly{T}, b::Rational{S}) where {S <: Integer, T <: Ri
if a.length == 0
return base_ring(a)()
end
r = a.coeffs[a.length]
r = deepcopy(a.coeffs[a.length])
for i = 1:a.length - 1
r *= b^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i]))
r += a.coeffs[a.length - i]
r = mul!(r, r, b^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i])))
r = add!(r, r, a.coeffs[a.length - i])
end
if a.exps[1] != 0
r *= b^(reinterpret(Int, a.exps[1]))
r = mul!(r, r, b^(reinterpret(Int, a.exps[1])))
end
return r
end
Expand All @@ -509,13 +509,13 @@ function evaluate(a::SparsePoly{T}, b::Integer) where {T <: RingElement}
return base_ring(a)()
end
R = base_ring(a)
r = a.coeffs[a.length]
r = deepcopy(a.coeffs[a.length])
for i = 1:a.length - 1
r *= R(b)^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i]))
r += a.coeffs[a.length - i]
r = mul!(r, r, R(b)^(reinterpret(Int, a.exps[a.length - i + 1] - a.exps[a.length - i])))
r = add!(r, r, a.coeffs[a.length - i])
end
if a.exps[1] != 0
r *= R(b)^(reinterpret(Int, a.exps[1]))
r = mul!(r, r, R(b)^(reinterpret(Int, a.exps[1])))
end
return r
end
Expand Down

0 comments on commit 3296e67

Please sign in to comment.