Skip to content

Commit 6e71cb3

Browse files
committed
close #213; update tests; fix bug in isconstant for SparsePolynomials
1 parent 6fdb7ba commit 6e71cb3

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/polynomials/Polynomial.jl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,38 @@ julia> p.(0:3)
8282

8383

8484
function Base.:+(p1::Polynomial{T}, p2::Polynomial{S}) where {T, S}
85-
86-
#isconstant(p1) && return p2 + p1[0] # factor of 2 slower with this check
87-
#isconstant(p2) && return p1 + p2[0]
88-
p1.var != p2.var && error("Polynomials must have same variable")
89-
9085
n1, n2 = length(p1), length(p2)
91-
c = [p1[i] + p2[i] for i = 0:max(n1, n2)]
92-
return Polynomial(c, p1.var)
86+
if n1 > 1 && n2 > 1
87+
p1.var != p2.var && error("Polynomials must have same variable")
88+
c = [p1[i] + p2[i] for i = 0:max(n1, n2)]
89+
return Polynomial(c, p1.var)
90+
elseif n1 <= 1
91+
c = copy(p2.coeffs )
92+
c[1] += p1[0]
93+
return Polynomial(c, p2.var)
94+
else
95+
c = copy(p1.coeffs )
96+
c[1] += p2[0]
97+
return Polynomial(c, p1.var)
98+
end
9399
end
94100

95101

96102
function Base.:*(p1::Polynomial{T}, p2::Polynomial{S}) where {T,S}
97103

98-
#isconstant(p1) && return p2 * p1[0] # no real effect
99-
#isconstant(p2) && return p1 * p2[0]
100-
p1.var != p2.var && error("Polynomials must have same variable")
101-
n,m = length(p1)-1, length(p2)-1 # not degree, so pNULL works
102-
R = promote_type(T, S)
103-
c = zeros(R, m + n + 1)
104-
for i in 0:n, j in 0:m
105-
@inbounds c[i + j + 1] += p1[i] * p2[j]
104+
n, m = length(p1)-1, length(p2)-1 # not degree, so pNULL works
105+
if n > 0 && m > 0
106+
p1.var != p2.var && error("Polynomials must have same variable")
107+
R = promote_type(T, S)
108+
c = zeros(R, m + n + 1)
109+
for i in 0:n, j in 0:m
110+
@inbounds c[i + j + 1] += p1[i] * p2[j]
111+
end
112+
return Polynomial(c, p1.var)
113+
elseif n <= 0
114+
return Polynomial(copy(p2.coeffs) * p1[0], p2.var)
115+
else
116+
return Polynomial(copy(p1.coeffs) * p2[0], p1.var)
106117
end
107-
return Polynomial(c, p1.var)
118+
108119
end

src/polynomials/SparsePolynomial.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ end
9292
degree(p::SparsePolynomial) = isempty(p.coeffs) ? -1 : maximum(keys(p.coeffs))
9393
function isconstant(p::SparsePolynomial)
9494
n = length(keys(p.coeffs))
95-
(n > 1 || iszero(p[0])) && return false
95+
(n > 1 || (n==1 && iszero(p[0]))) && return false
9696
return true
9797
end
9898

@@ -188,7 +188,7 @@ function Base.:+(p1::SparsePolynomial{T}, p2::SparsePolynomial{S}) where {T, S}
188188

189189
isconstant(p1) && return p2 + p1[0]
190190
isconstant(p2) && return p1 + p2[0]
191-
191+
192192
p1.var != p2.var && error("SparsePolynomials must have same variable")
193193

194194
R = promote_type(T,S)

test/StandardBasis.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555
end
5656

5757
@testset "Other Construction" begin
58-
for P in (ImmutablePolynomial{10}, Polynomial)
58+
for P in (ImmutablePolynomial{10}, Polynomial, SparsePolynomial, LaurentPolynomial)
5959

6060
# Leading 0s
6161
p = P([1, 2, 0, 0])
@@ -77,11 +77,11 @@ end
7777

7878
pNULL = P(Int[])
7979
@test iszero(pNULL)
80-
@test degree(pNULL) == -1
80+
P != LaurentPolynomial && @test degree(pNULL) == -1
8181

8282
p0 = P([0])
8383
@test iszero(p0)
84-
@test degree(p0) == -1
84+
P != LaurentPolynomial && @test degree(p0) == -1
8585

8686
# variable(), P() to generate `x` in given basis
8787
@test degree(variable(P)) == 1
@@ -91,7 +91,7 @@ end
9191
@test variable(P, :y) == P(:y)
9292

9393
# test degree, isconstant
94-
@test degree(zero(P)) == -1
94+
P != LaurentPolynomial && @test degree(zero(P)) == -1
9595
@test degree(one(P)) == 0
9696
@test degree(P(1)) == 0
9797
@test degree(P([1])) == 0
@@ -506,7 +506,7 @@ end
506506
end
507507

508508
# issue 206 with mixed variable types and promotion
509-
for P in (ImmutablePolynomial,)
509+
for P in Ps
510510
λ = P([0,1],)
511511
A = [1 λ; λ^2 λ^3]
512512
@test A == diagm(0 => [1, λ^3], 1=>[λ], -1=>^2])

0 commit comments

Comments
 (0)