Skip to content

Commit 236bbe3

Browse files
authored
immutable_poly_mult (#305)
1 parent 3c35470 commit 236bbe3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/polynomials/ImmutablePolynomial.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,19 @@ function Base.:+(p1::ImmutablePolynomial{T,N}, p2::ImmutablePolynomial{S,M}) whe
226226

227227
end
228228

229-
229+
# not type stable!!!
230230
function Base.:*(p1::ImmutablePolynomial{T,N}, p2::ImmutablePolynomial{S,M}) where {T,N,S,M}
231231
isconstant(p1) && return p2 * p1[0]
232232
isconstant(p2) && return p1 * p2[0]
233233
p1.var != p2.var && error("Polynomials must have same variable")
234234
R = promote_type(S,T)
235235
cs = (p1.coeffs) (p2.coeffs)
236-
ImmutablePolynomial{R, N+M-1}(cs, p1.var)
236+
if !iszero(cs[end])
237+
return ImmutablePolynomial{R, N+M-1}(cs, p1.var)
238+
else
239+
n = findlast(!iszero, cs)
240+
return ImmutablePolynomial{R, n}(cs[1:n], p1.var)
241+
end
237242
end
238243

239244
# Padded vector sum of two tuples assuming N > M

0 commit comments

Comments
 (0)