Skip to content

Commit 0e7c2a8

Browse files
committed
close #215 with @andreasvarga soln
1 parent 9b1de1e commit 0e7c2a8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/polynomials/Polynomial.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,27 @@ julia> p.(0:3)
8383

8484
function Base.:+(p1::Polynomial{T}, p2::Polynomial{S}) where {T, S}
8585
n1, n2 = length(p1), length(p2)
86+
R = promote_type(T,S)
8687
if n1 > 1 && n2 > 1
8788
p1.var != p2.var && error("Polynomials must have same variable")
8889
if n1 >= n2
89-
c = copy(p1.coeffs)
90+
c = R.(copy(p1.coeffs))
9091
for i = 1:n2
9192
c[i] += p2.coeffs[i]
9293
end
9394
else
94-
c = copy(p2.coeffs)
95+
c = R.(copy(p2.coeffs))
9596
for i = 1:n1
9697
c[i] += p1.coeffs[i]
9798
end
9899
end
99100
return Polynomial(c, p1.var)
100101
elseif n1 <= 1
101-
c = copy(p2.coeffs)
102+
c = R.(copy(p2.coeffs))
102103
c[1] += p1[0]
103104
return Polynomial(c, p2.var)
104105
else
105-
c = copy(p1.coeffs)
106+
c = R.(copy(p1.coeffs))
106107
c[1] += p2[0]
107108
return Polynomial(c, p1.var)
108109
end

test/StandardBasis.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ end
144144
@test p / 2 == P([1/2, 1.0, 3/2])
145145
@test p * 0.5 == P([1/2, 1.0, 3/2])
146146
end
147+
148+
# ensure promotion of +,*; issue 215
149+
for P in Ps
150+
p,q = P([1,2,3]), P(im, )
151+
@test p+q == P([1+im, 2, 3])
152+
@test p*q == P(im*[1,2,3])
153+
end
147154
end
148155

149156
@testset "Divrem" begin

0 commit comments

Comments
 (0)