11# Poly type manipulations
22
3- isdefined (Base, :__precompile__ ) && __precompile__ ()
3+ __precompile__ ()
4+
45
56module Polynomials
67# todo: sparse polynomials?
@@ -76,7 +77,7 @@ Poly(0.5 - 0.5⋅x^2)
7677struct Poly{T}
7778 a:: Vector{T}
7879 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 }
8081 # if a == [] we replace it with a = [0]
8182 if length (a) == 0
8283 return new {T} (zeros (T,1 ),Symbol (var))
@@ -90,7 +91,7 @@ struct Poly{T}
9091end
9192
9293Poly (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} =
9495 Poly (convert (Vector{T}, x), var)
9596
9697# create a Poly object from its roots
@@ -403,21 +404,21 @@ given `norm` function. The tolerances `rtol` and `atol` are passed to both
403404`truncate` and `isapprox`.
404405"""
405406function 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}
407408 p1. var == p2. var || error (" Polynomials must have same variable" )
408409 p1t = truncate (p1; rtol = rtol, atol = atol)
409410 p2t = truncate (p2; rtol = rtol, atol = atol)
410411 length (p1t) == length (p2t) && isapprox (coeffs (p1t), coeffs (p2t); rtol = rtol,
411412 atol = atol, norm = norm)
412413end
413414
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 )),
415416 atol:: Real = 0 ) where {T,S<: Number }
416417 p1t = truncate (p1; rtol = rtol, atol = atol)
417418 degree (p1t) == 0 && isapprox (coeffs (p1), [n]; rtol = rtol, atol = atol)
418419end
419420
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 )),
421422 atol:: Real = 0 ) where {T,S<: Number } = isapprox (p1, n; rtol = rtol, atol = atol)
422423
423424hash (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)
505506function _polyint (p:: Poly{T} , k:: S ) where {T,S<: Number }
506507 n = length (p)
507508 R = promote_type (typeof (one (T)/ 1 ), S)
508- a2 = @compat Vector {R} (undef, n+ 1 )
509+ a2 = Vector {R} (undef, n+ 1 )
509510 a2[1 ] = k
510511 for i = 1 : n
511512 a2[i+ 1 ] = p[i- 1 ] / i
566567
567568function _polyder (p:: Poly{T} , order:: Int = 1 ) where {T}
568569 n = length (p)
569- a2 = @compat Vector {T} (undef, n- order)
570+ a2 = Vector {T} (undef, n- order)
570571 for i = order: n- 1
571572 a2[i- order+ 1 ] = p[i] * prod ((i- order+ 1 ): i)
572573 end
@@ -634,7 +635,7 @@ function roots(p::Poly{T}) where {T}
634635 n = lastindex (p)- (num_leading_zeros + num_trailing_zeros)
635636 n < 1 && return zeros (R, length (p) - num_trailing_zeros - 1 )
636637
637- companion = @compat diagm (- 1 => ones (R, n- 1 ))
638+ companion = diagm (- 1 => ones (R, n- 1 ))
638639 an = p[end - num_trailing_zeros]
639640 companion[1 ,:] = - p[(end - num_trailing_zeros- 1 ): - 1 : num_leading_zeros] / an
640641
0 commit comments