Skip to content

Commit 09ef314

Browse files
authored
fix generic conv (#497)
* fix generic conv * fix test
1 parent 7be9e7e commit 09ef314

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/polynomials/standard-basis.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ end
100100
## put here, not with type definition, in case reuse is possible
101101
## `conv` can be used with matrix entries, unlike `fastconv`
102102
function conv(p::Vector{T}, q::Vector{S}) where {T,S}
103+
(isempty(p) || isempty(q)) && return promote_type(T, S)[]
103104
as = [p[1]*q[1]]
104-
z = 0 * as[1]
105+
z = zero(as[1])
105106
n,m = length(p)-1, length(q)-1
106107
for i 1:n+m
107108
Σ = z

test/StandardBasis.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,30 @@ end
442442

443443
end
444444

445+
@testset "generic arithmetics" begin
446+
P = Polynomial
447+
# define a set algebra
448+
struct Setalg # not a number
449+
content::Vector{Int}
450+
end
451+
Base.:(+)(a::Setalg, b::Setalg) = Setalg(a.content b.content)
452+
Base.:(*)(a::Setalg, b::Setalg) = Setalg(vec([x * y for x in a.content, y in b.content]))
453+
Base.zero(::Setalg) = Setalg(Int[])
454+
Base.one(::Setalg) = Setalg(Int[1])
455+
Base.zero(::Type{Setalg}) = Setalg(Int[])
456+
Base.one(::Type{Setalg}) = Setalg(Int[1])
457+
Base.:(==)(a::Setalg, b::Setalg) = a.content == b.content
458+
459+
a = Setalg([1])
460+
b = Setalg([4,2])
461+
pNULL = P(Setalg[])
462+
p0 = P([a])
463+
p1 = P([a, b, b])
464+
@test pNULL * p0 == pNULL
465+
@test pNULL * p1 == pNULL
466+
@test p0 * p1 == p1
467+
end
468+
445469
@testset for P in Ps # ensure promotion of scalar +,*,/
446470
p = P([1,2,3])
447471
@test p + 0.5 ==P([1.5, 2.0, 3.0])

0 commit comments

Comments
 (0)