@@ -59,25 +59,24 @@ p + q # ERROR: Polynomials must have same variable.
59
59
```
60
60
61
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
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))
75
73
end
74
+ end
76
75
end
77
76
78
- Poly {T <: Number} (x:: Vector{T} , var:: SymbolLike = :x ) = Poly {T} (x, var)
79
77
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)
81
80
82
81
# create a Poly object from its roots
83
82
"""
@@ -303,7 +302,7 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
303
302
end
304
303
305
304
# # older . operators, hack to avoid warning on v0.6
306
- dot_operators = quote
305
+ dot_operators = quote
307
306
@compat Base.:.+ {T<: Number }(c:: T , p:: Poly ) = + (p, c)
308
307
@compat Base.:.+ {T<: Number }(p:: Poly , c:: T ) = + (p, c)
309
308
@compat Base.:.- {T<: Number }(p:: Poly , c:: T ) = + (p, - c)
@@ -530,7 +529,7 @@ function roots{T}(p::Poly{T})
530
529
companion = diagm (ones (R, n- 1 ), - 1 )
531
530
an = p[end - num_trailing_zeros]
532
531
companion[1 ,:] = - p[(end - num_trailing_zeros- 1 ): - 1 : num_leading_zeros] / an
533
-
532
+
534
533
D = eigvals (companion)
535
534
r = zeros (eltype (D),length (p)- num_trailing_zeros- 1 )
536
535
r[1 : n] = D
0 commit comments