1
1
# Poly type manipulations
2
2
3
- isdefined (Base, :__precompile__ ) && __precompile__ ()
3
+ __precompile__ ()
4
+
4
5
5
6
module Polynomials
6
7
# todo: sparse polynomials?
@@ -76,7 +77,7 @@ Poly(0.5 - 0.5⋅x^2)
76
77
struct Poly{T}
77
78
a:: Vector{T}
78
79
var:: Symbol
79
- function ( :: Type{ Poly} ) (a:: AbstractVector{T} , var:: SymbolLike = :x ) where {T<: Number }
80
+ function Poly (a:: AbstractVector{T} , var:: SymbolLike = :x ) where {T<: Number }
80
81
# if a == [] we replace it with a = [0]
81
82
if length (a) == 0
82
83
return new {T} (zeros (T,1 ),Symbol (var))
@@ -90,7 +91,7 @@ struct Poly{T}
90
91
end
91
92
92
93
Poly (n:: Number , var:: SymbolLike = :x ) = Poly ([n], var)
93
- ( :: Type{ Poly{T}} ) (x:: AbstractVector{S} , var:: SymbolLike = :x ) where {T,S} =
94
+ Poly {T} (x:: AbstractVector{S} , var:: SymbolLike = :x ) where {T,S} =
94
95
Poly (convert (Vector{T}, x), var)
95
96
96
97
# create a Poly object from its roots
@@ -403,21 +404,21 @@ given `norm` function. The tolerances `rtol` and `atol` are passed to both
403
404
`truncate` and `isapprox`.
404
405
"""
405
406
function isapprox (p1:: Poly{T} , p2:: Poly{S} ;
406
- rtol:: Real = (@compat Base. rtoldefault (T,S, 0 )), atol:: Real = 0 , norm:: Function = vecnorm) where {T,S}
407
+ rtol:: Real = (Base. rtoldefault (T,S, 0 )), atol:: Real = 0 , norm:: Function = vecnorm) where {T,S}
407
408
p1. var == p2. var || error (" Polynomials must have same variable" )
408
409
p1t = truncate (p1; rtol = rtol, atol = atol)
409
410
p2t = truncate (p2; rtol = rtol, atol = atol)
410
411
length (p1t) == length (p2t) && isapprox (coeffs (p1t), coeffs (p2t); rtol = rtol,
411
412
atol = atol, norm = norm)
412
413
end
413
414
414
- function isapprox (p1:: Poly{T} , n:: S ; rtol:: Real = (@compat Base. rtoldefault (T,S, 0 )),
415
+ function isapprox (p1:: Poly{T} , n:: S ; rtol:: Real = (Base. rtoldefault (T,S, 0 )),
415
416
atol:: Real = 0 ) where {T,S<: Number }
416
417
p1t = truncate (p1; rtol = rtol, atol = atol)
417
418
degree (p1t) == 0 && isapprox (coeffs (p1), [n]; rtol = rtol, atol = atol)
418
419
end
419
420
420
- isapprox (n:: S , p1:: Poly{T} ; rtol:: Real = (@compat Base. rtoldefault (T,S, 0 )),
421
+ isapprox (n:: S , p1:: Poly{T} ; rtol:: Real = (Base. rtoldefault (T,S, 0 )),
421
422
atol:: Real = 0 ) where {T,S<: Number } = isapprox (p1, n; rtol = rtol, atol = atol)
422
423
423
424
hash (f:: Poly , h:: UInt ) = hash (f. var, hash (f. a, h))
@@ -505,7 +506,7 @@ polyint(p::Poly{T}, k::S) where {T,S<:Number} = _polyint(p, k)
505
506
function _polyint (p:: Poly{T} , k:: S ) where {T,S<: Number }
506
507
n = length (p)
507
508
R = promote_type (typeof (one (T)/ 1 ), S)
508
- a2 = @compat Vector {R} (undef, n+ 1 )
509
+ a2 = Vector {R} (undef, n+ 1 )
509
510
a2[1 ] = k
510
511
for i = 1 : n
511
512
a2[i+ 1 ] = p[i- 1 ] / i
566
567
567
568
function _polyder (p:: Poly{T} , order:: Int = 1 ) where {T}
568
569
n = length (p)
569
- a2 = @compat Vector {T} (undef, n- order)
570
+ a2 = Vector {T} (undef, n- order)
570
571
for i = order: n- 1
571
572
a2[i- order+ 1 ] = p[i] * prod ((i- order+ 1 ): i)
572
573
end
@@ -634,7 +635,7 @@ function roots(p::Poly{T}) where {T}
634
635
n = lastindex (p)- (num_leading_zeros + num_trailing_zeros)
635
636
n < 1 && return zeros (R, length (p) - num_trailing_zeros - 1 )
636
637
637
- companion = @compat diagm (- 1 => ones (R, n- 1 ))
638
+ companion = diagm (- 1 => ones (R, n- 1 ))
638
639
an = p[end - num_trailing_zeros]
639
640
companion[1 ,:] = - p[(end - num_trailing_zeros- 1 ): - 1 : num_leading_zeros] / an
640
641
0 commit comments