Skip to content

Commit 820d8a5

Browse files
authored
Merge pull request #1 from aytekinar/constructor
Solve constructor issue with Jacobi.jl
2 parents a9f86d8 + a6bcebc commit 820d8a5

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/Polynomials.jl

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,24 @@ p + q # ERROR: Polynomials must have same variable.
5959
```
6060
6161
"""
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
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))
7573
end
74+
end
7675
end
7776

78-
Poly{T <: Number}(x::Vector{T}, var::SymbolLike=:x) = Poly{T}(x, var)
7977
Poly(n::Number, var::SymbolLike = :x) = Poly([n], var)
80-
78+
@compat (::Type{Poly{T}}){T,S}(x::Vector{S}, var::SymbolLike = :x) =
79+
Poly(convert(Vector{T}, x), var)
8180

8281
# create a Poly object from its roots
8382
"""
@@ -303,7 +302,7 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
303302
end
304303

305304
## older . operators, hack to avoid warning on v0.6
306-
dot_operators = quote
305+
dot_operators = quote
307306
@compat Base.:.+{T<:Number}(c::T, p::Poly) = +(p, c)
308307
@compat Base.:.+{T<:Number}(p::Poly, c::T) = +(p, c)
309308
@compat Base.:.-{T<:Number}(p::Poly, c::T) = +(p, -c)
@@ -530,7 +529,7 @@ function roots{T}(p::Poly{T})
530529
companion = diagm(ones(R, n-1), -1)
531530
an = p[end-num_trailing_zeros]
532531
companion[1,:] = -p[(end-num_trailing_zeros-1):-1:num_leading_zeros] / an
533-
532+
534533
D = eigvals(companion)
535534
r = zeros(eltype(D),length(p)-num_trailing_zeros-1)
536535
r[1:n] = D

src/show.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494

9595

9696
## print the sign
97-
## returns aspos(pj)
97+
## returns aspos(pj)
9898
function printsign{T}(io::IO, pj::T, j, first, mimetype)
9999
neg = isneg(pj)
100100
if first
@@ -111,12 +111,12 @@ function printproductsign{T}(io::IO, pj::T, j, mimetype)
111111
j == 0 && return
112112
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
113113
end
114-
114+
115115
function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
116116

117117
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
118118
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
119-
119+
120120
if hasreal & hasimag
121121
print(io, '(')
122122
show(io, mimetype, pj)
@@ -134,8 +134,8 @@ function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
134134
end
135135
end
136136

137-
138-
## show a single term
137+
138+
## show a single term
139139
function printcoefficient{T}(io::IO, pj::T, j, mimetype)
140140
pj == one(T) && !(showone(T) || j == 0) && return
141141
show(io, mimetype, pj)

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,3 @@ pint = polyint(p)
283283
@test isnan(p(1)) # p(1) evaluates to NaN
284284
@test isequal(pder, Poly([NaN])) # derivative will give Poly([NaN])
285285
@test isequal(pint, Poly([NaN])) # integral will give Poly([NaN])
286-

0 commit comments

Comments
 (0)