Skip to content

Commit 9a57291

Browse files
committed
constructor fix
1 parent cbf5b77 commit 9a57291

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/Polynomials.jl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,39 @@ q = Poly([1, 2, 3], :s)
5858
p + q # ERROR: Polynomials must have same variable.
5959
```
6060
61-
"""
62-
immutable Poly{T}
63-
a::Vector{T}
64-
var::Symbol
65-
@compat function (::Type{Poly}){T<:Number}(a::Vector{T}, var::SymbolLike = :x)
66-
# if a == [] we replace it with a = [0]
67-
if length(a) == 0
68-
return new{T}(zeros(T,1), @compat Symbol(var))
69-
else
70-
# determine the last nonzero element and truncate a accordingly
71-
a_last = max(1,findlast(x->x!=zero(T), a))
72-
new{T}(a[1:a_last], @compat Symbol(var))
61+
"""
62+
63+
immutable Poly{T<:Number}
64+
a::Vector{T}
65+
var::Symbol
66+
function Poly(a::Vector{T}, var::SymbolLike=:x)
67+
# if a == [] we replace it with a = [0]
68+
if length(a) == 0
69+
return new{T}(zeros(T,1), @compat Symbol(var))
70+
else
71+
# determine the last nonzero element and truncate a accordingly
72+
a_last = max(1,findlast(x->x!=zero(T), a))
73+
new{T}(a[1:a_last], @compat Symbol(var))
74+
end
7375
end
74-
end
7576
end
7677

78+
# immutable Poly{T}
79+
# a::Vector{T}
80+
# var::Symbol
81+
# @compat function (::Type{Poly}){T<:Number}(a::Vector{T}, var::SymbolLike = :x)
82+
# # if a == [] we replace it with a = [0]
83+
# if length(a) == 0
84+
# return new{T}(zeros(T,1), @compat Symbol(var))
85+
# else
86+
# # determine the last nonzero element and truncate a accordingly
87+
# a_last = max(1,findlast(x->x!=zero(T), a))
88+
# new{T}(a[1:a_last], @compat Symbol(var))
89+
# end
90+
# end
91+
# end
92+
93+
Poly{T <: Number}(x::Vector{T}, var::SymbolLike=:x) = Poly{T}(x, var)
7794
Poly(n::Number, var::SymbolLike = :x) = Poly([n], var)
7895

7996

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ p5 = Poly([1,4,6,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0])
1313
pN = Poly([276,3,87,15,24,0])
1414
pR = Poly([3//4, -2//1, 1//1])
1515
X = Poly([0.0, 1.0])
16+
T = Int64
17+
Poly{T}([zero(T), one(T)])
18+
Poly{T}([zero(T), one(T)], :y)
19+
1620
p1000 = Poly(randn(1000))
1721

1822
@test length(pNULL) == 1
@@ -279,3 +283,4 @@ pint = polyint(p)
279283
@test isnan(p(1)) # p(1) evaluates to NaN
280284
@test isequal(pder, Poly([NaN])) # derivative will give Poly([NaN])
281285
@test isequal(pint, Poly([NaN])) # integral will give Poly([NaN])
286+

0 commit comments

Comments
 (0)