@@ -2,7 +2,7 @@ abstract type StandardBasisPolynomial{T} <: AbstractPolynomial{T} end
2
2
3
3
4
4
5
- function showterm (io:: IO , :: Type{<:StandardBasisPolynomial} , pj:: T , var, j, first:: Bool , mimetype) where {T}
5
+ function showterm (io:: IO , :: Type{<:StandardBasisPolynomial} , pj:: T , var, j, first:: Bool , mimetype) where {T}
6
6
if iszero (pj) return false end
7
7
pj = printsign (io, pj, first, mimetype)
8
8
if ! (pj == one (T) && ! (showone (T) || j == 0 ))
@@ -34,6 +34,9 @@ function fromroots(P::Type{<:StandardBasisPolynomial}, r::AbstractVector{T}; var
34
34
c[(i + 1 )] = c[(i + 1 )] - r[j] * c[i]
35
35
end
36
36
# return P(c, var)
37
+ if sort (r[imag (r).> 0 ], lt = (x,y) -> real (x)== real (y) ? imag (x)< imag (y) : real (x)< real (y)) == sort (conj (r[imag (r).< 0 ]), lt = (x,y) -> real (x)== real (y) ? imag (x)< imag (y) : real (x)< real (y))
38
+ c = real (c) # if complex poles come in conjugate pairs, the coeffs are real
39
+ end
37
40
return P (reverse (c), var)
38
41
end
39
42
@@ -52,7 +55,7 @@ function derivative(p::P, order::Integer = 1) where {T, P <: StandardBasisPolyno
52
55
# we avoid usage like Base.promote_op(*, T, Int) here, say, as
53
56
# Base.promote_op(*, Rational, Int) is Any, not Rational in analogy to
54
57
# Base.promote_op(*, Complex, Int)
55
- R = eltype (one (T)* 1 )
58
+ R = eltype (one (T)* 1 )
56
59
order == 0 && return p
57
60
hasnan (p) && return ⟒ (P){R}(R[NaN ], p. var)
58
61
order > length (p) && return zero (⟒ (P){R},p. var)
@@ -87,14 +90,14 @@ function Base.divrem(num::P, den::Q) where {T, P <: StandardBasisPolynomial{T},
87
90
88
91
check_same_variable (num, den) || error (" Polynomials must have same variable" )
89
92
var = num. var
90
-
91
-
93
+
94
+
92
95
n = degree (num)
93
96
m = degree (den)
94
97
95
98
m == - 1 && throw (DivideError ())
96
99
if m == 0 && den[0 ] ≈ 0 throw (DivideError ()) end
97
-
100
+
98
101
R = eltype (one (T)/ one (S))
99
102
100
103
deg = n - m + 1
@@ -117,7 +120,7 @@ function Base.divrem(num::P, den::Q) where {T, P <: StandardBasisPolynomial{T},
117
120
resize! (r_coeff, min (length (r_coeff), m))
118
121
119
122
return ⟒ (P)(q_coeff, var), ⟒ (P)(r_coeff, var)
120
-
123
+
121
124
end
122
125
123
126
"""
@@ -135,7 +138,7 @@ function Base.gcd(p1::P, p2::Q, args...;
135
138
method= :euclidean ,
136
139
kwargs...
137
140
) where {T, P <: StandardBasisPolynomial{T} , Q <: StandardBasisPolynomial{T} }
138
-
141
+
139
142
gcd (Val (method), p1, p2, args... ; kwargs... )
140
143
end
141
144
@@ -144,15 +147,15 @@ function Base.gcd(::Val{:euclidean},
144
147
atol= zero (real (T)),
145
148
rtol= Base. rtoldefault (real (T)),
146
149
kwargs... ) where {T}
147
-
148
-
150
+
151
+
149
152
iter = 1
150
153
itermax = length (r₁)
151
-
154
+
152
155
while ! iszero (r₁) && iter ≤ itermax
153
156
_, rtemp = divrem (r₀, r₁)
154
157
r₀ = r₁
155
- r₁ = truncate (rtemp; atol= atol, rtol= rtol)
158
+ r₁ = truncate (rtemp; atol= atol, rtol= rtol)
156
159
iter += 1
157
160
end
158
161
return r₀
@@ -165,8 +168,8 @@ Base.gcd(::Val{:noda_sasaki}, p, q; kwargs...) = gcd_noda_sasaki(p,q; kwargs...)
165
168
gcd_noda_sasaki(p,q; atol, rtol)
166
169
167
170
Greatest common divisor of two polynomials.
168
- Compute the greatest common divisor `d` of two polynomials `a` and `b` using
169
- the Euclidian Algorithm with scaling as of [1].
171
+ Compute the greatest common divisor `d` of two polynomials `a` and `b` using
172
+ the Euclidian Algorithm with scaling as of [1].
170
173
171
174
References:
172
175
@@ -196,7 +199,7 @@ function _gcd_noda_sasaki(a::Vector{T}, b::Vector{S};
196
199
rtol:: Real = Base. rtoldefault (real (promote_type (T,S)))
197
200
) where {T,S}
198
201
199
- R = eltype (one (T)/ one (S))
202
+ R = eltype (one (T)/ one (S))
200
203
201
204
na1 = findlast (! iszero,a) # degree(a) + 1
202
205
na1 === nothing && return (ones (R, 1 ))
@@ -205,17 +208,17 @@ function _gcd_noda_sasaki(a::Vector{T}, b::Vector{S};
205
208
nb1 === nothing && return (ones (R, 1 ))
206
209
207
210
a1 = R[a[i] for i in 1 : na1]
208
- b1 = R[b[i] for i in 1 : nb1]
211
+ b1 = R[b[i] for i in 1 : nb1]
209
212
a1 ./= norm (a1)
210
213
b1 ./= norm (b1)
211
214
212
215
tol = atol + rtol
213
216
214
- # determine the degree of GCD as the nullity of the Sylvester matrix
217
+ # determine the degree of GCD as the nullity of the Sylvester matrix
215
218
# this computation can be replaced by simply setting nd = 1, in which case the Sylvester matrix is not formed
216
219
217
220
nd = na1 + nb1 - 2 - rank ([NGCD. convmtx (a1,nb1- 1 ) NGCD. convmtx (b1,na1- 1 )], atol = tol) # julia 1.1
218
- nd == 0 && (return [one (R)])
221
+ nd == 0 && (return [one (R)])
219
222
220
223
sc = one (R)
221
224
while na1 > nd
@@ -248,9 +251,9 @@ function companion(p::P) where {T, P <: StandardBasisPolynomial{T}}
248
251
d < 1 && error (" Series must have degree greater than 1" )
249
252
d == 1 && return diagm (0 => [- p[0 ] / p[1 ]])
250
253
251
-
254
+
252
255
R = eltype (one (T)/ one (T))
253
-
256
+
254
257
comp = diagm (- 1 => ones (R, d - 1 ))
255
258
ani = 1 / p[end ]
256
259
for j in 0 : (degree (p)- 1 )
261
264
262
265
function roots (p:: P ; kwargs... ) where {T, P <: StandardBasisPolynomial{T} }
263
266
264
- R = eltype (one (T)/ one (T))
267
+ R = eltype (one (T)/ one (T))
265
268
d = degree (p)
266
269
if d < 1
267
270
return []
@@ -294,4 +297,3 @@ function vander(P::Type{<:StandardBasisPolynomial}, x::AbstractVector{T}, n::Int
294
297
end
295
298
return A
296
299
end
297
-
0 commit comments