You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/polynomials/LaurentPolynomial.jl
+89-63Lines changed: 89 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ export LaurentPolynomial
5
5
6
6
A [Laurent](https://en.wikipedia.org/wiki/Laurent_polynomial) polynomial is of the form `a_{m}x^m + ... + a_{n}x^n` where `m,n` are integers (not necessarily positive) with ` m <= n`.
7
7
8
-
The `coeffs` specify `a_{m}, a_{m-1}, ..., a_{n}`. The range specified is of the form `m:n`, if left empty, `0:length(coeffs)-1` is used (i.e., the coefficients refer to the standard basis). Alternatively, the coefficients can be specified using an `OffsetVector` from the `OffsetArrays` package.
8
+
The `coeffs` specify `a_{m}, a_{m-1}, ..., a_{n}`. The range specified is of the form `m` (or `m:n`), if left empty, `m` is taken to be `0` (i.e., the coefficients refer to the standard basis). Alternatively, the coefficients can be specified using an `OffsetVector` from the `OffsetArrays` package.
9
9
10
10
Laurent polynomials and standard basis polynomials promote to Laurent polynomials. Laurent polynomials may be converted to a standard basis polynomial when `m >= 0`
function LinearAlgebra.conj(p::P) where {P <:LaurentPolynomial}
306
330
ps =coeffs(p)
307
-
m,n=extrema(p)
308
-
⟒(P)(conj(ps),m:n, p.var)
331
+
m =firstindex(p)
332
+
⟒(P)(conj(ps), m, p.var)
309
333
end
310
334
311
335
@@ -341,8 +365,8 @@ true
341
365
functionparaconj(p::LaurentPolynomial)
342
366
cs = p.coeffs
343
367
ds =adjoint.(cs)
344
-
m,n =extrema(p)
345
-
LaurentPolynomial(reverse(ds), -n:-m, p.var)
368
+
n =degree(p)
369
+
LaurentPolynomial(reverse(ds), -n, p.var)
346
370
end
347
371
348
372
"""
@@ -383,13 +407,13 @@ true
383
407
"""
384
408
functioncconj(p::LaurentPolynomial)
385
409
ps =conj.(coeffs(p))
386
-
m,n =extrema(p)
410
+
m,n =(extrema∘ degreerange)(p)
387
411
for i in m:n
388
412
ifisodd(i)
389
413
ps[i+1-m] *=-1
390
414
end
391
415
end
392
-
LaurentPolynomial(ps, m:n, p.var)
416
+
LaurentPolynomial(ps, m, p.var)
393
417
end
394
418
395
419
@@ -401,7 +425,7 @@ end
401
425
402
426
# evaluation uses `evalpoly`
403
427
function (p::LaurentPolynomial{T})(x::S) where {T,S}
404
-
m,n =extrema(p)
428
+
m,n =(extrema∘ degreerange)(p)
405
429
m == n ==0&&return p[0] *_one(S)
406
430
if m >=0
407
431
evalpoly(x, NTuple{n+1,T}(p[i] for i in0:n))
@@ -419,22 +443,22 @@ end
419
443
420
444
421
445
# scalar operattoinis
422
-
Base.:-(p::P) where {P <:LaurentPolynomial} =P(-coeffs(p), range(p), p.var)
446
+
Base.:-(p::P) where {P <:LaurentPolynomial} =P(-coeffs(p), firstindex(p), p.var)
423
447
424
448
function Base.:+(p::LaurentPolynomial{T}, c::S) where {T, S <:Number}
425
-
q =LaurentPolynomial([c], 0:0, p.var)
449
+
q =LaurentPolynomial([c], 0, p.var)
426
450
p + q
427
451
end
428
452
429
453
function Base.:*(p::P, c::S) where {T,P <:LaurentPolynomial, S <:Number}
430
454
as = c *copy(coeffs(p))
431
-
return⟒(P)(as, range(p), p.var)
455
+
return⟒(P)(as, firstindex(p), p.var)
432
456
end
433
457
434
458
435
459
function Base.:/(p::P, c::S) where {T,P <:LaurentPolynomial{T},S <:Number}
436
460
R =promote_type(P, eltype(one(T) /one(S)))
437
-
returnR(coeffs(p) ./ c, range(p), p.var)
461
+
returnR(coeffs(p) ./ c, firstindex(p), p.var)
438
462
end
439
463
440
464
##
@@ -443,25 +467,25 @@ end
443
467
function Base.:+(p1::P1, p2::P2) where {T,P1<:LaurentPolynomial{T}, S, P2<:LaurentPolynomial{S}}
444
468
445
469
ifisconstant(p1)
446
-
p1 =P1(p1.coeffs, range(p1), p2.var)
470
+
p1 =P1(p1.coeffs, firstindex(p1), p2.var)
447
471
elseifisconstant(p2)
448
-
p2 =P2(p2.coeffs, range(p2), p1.var)
472
+
p2 =P2(p2.coeffs, firstindex(p2), p1.var)
449
473
end
450
474
451
475
p1.var != p2.var &&error("LaurentPolynomials must have same variable")
452
476
453
477
R =promote_type(T,S)
454
478
455
-
m1,n1 =extrema(p1)
456
-
m2,n2 =extrema(p2)
479
+
m1,n1 =(extrema∘ degreerange)(p1)
480
+
m2,n2 =(extrema∘ degreerange)(p2)
457
481
m,n =min(m1,m2), max(n1, n2)
458
482
459
483
as =zeros(R, length(m:n))
460
484
for i in m:n
461
485
as[1+ i-m] = p1[i] + p2[i]
462
486
end
463
487
464
-
q =LaurentPolynomial{R}(as, m:n, p1.var)
488
+
q =LaurentPolynomial{R}(as, m, p1.var)
465
489
chop!(q)
466
490
467
491
return q
@@ -477,8 +501,8 @@ function Base.:*(p1::LaurentPolynomial{T}, p2::LaurentPolynomial{S}) where {T,S}
477
501
478
502
R =promote_type(T,S)
479
503
480
-
m1,n1 =extrema(p1)
481
-
m2,n2 =extrema(p2)
504
+
m1,n1 =(extrema∘ degreerange)(p1)
505
+
m2,n2 =(extrema∘ degreerange)(p2)
482
506
m,n = m1 + m2, n1+n2
483
507
484
508
as =zeros(R, length(m:n))
@@ -489,7 +513,7 @@ function Base.:*(p1::LaurentPolynomial{T}, p2::LaurentPolynomial{S}) where {T,S}
489
513
end
490
514
end
491
515
492
-
p =LaurentPolynomial(as, m:n, p1.var)
516
+
p =LaurentPolynomial(as, m, p1.var)
493
517
chop!(p)
494
518
495
519
return p
@@ -521,11 +545,13 @@ julia> roots(a)
521
545
"""
522
546
functionroots(p::P; kwargs...) where {T, P <:LaurentPolynomial{T}}
523
547
c =coeffs(p)
524
-
r =range(p)
525
-
d = r[end]-min(0,r[1])+1# Length of the coefficient vector, taking into consideration the case when the lower degree is strictly positive (like p=3z^2).
526
-
z =zeros(T,d) # Reserves space for the coefficient vector.
527
-
z[max(0,r[1])+1:end] = c # Leaves the coeffs of the lower powers as zeros.
528
-
a =Polynomial(z,p.var) # The root is then the root of the numerator polynomial.
548
+
r =degreerange(p)
549
+
d = r[end] -min(0, r[1]) +1# Length of the coefficient vector, taking into consideration
550
+
# the case when the lower degree is strictly positive
551
+
# (like p=3z^2).
552
+
z =zeros(T, d) # Reserves space for the coefficient vector.
553
+
z[max(0, r[1]) +1:end] = c # Leaves the coeffs of the lower powers as zeros.
554
+
a =Polynomial(z, p.var) # The root is then the root of the numerator polynomial.
529
555
returnroots(a; kwargs...)
530
556
end
531
557
@@ -539,7 +565,7 @@ function derivative(p::P, order::Integer = 1) where {T, P<:LaurentPolynomial{T}}
539
565
540
566
hasnan(p) &&return⟒(P)(T[NaN], 0:0, p.var)
541
567
542
-
m,n =extrema(p)
568
+
m,n =(extrema∘ degreerange)(p)
543
569
m = m - order
544
570
n = n - order
545
571
as =zeros(T, length(m:n))
@@ -553,7 +579,7 @@ function derivative(p::P, order::Integer = 1) where {T, P<:LaurentPolynomial{T}}
553
579
end
554
580
end
555
581
556
-
chop!(LaurentPolynomial(as, m:n, p.var))
582
+
chop!(LaurentPolynomial(as, m, p.var))
557
583
558
584
end
559
585
@@ -564,11 +590,11 @@ function integrate(p::P, k::S) where {T, P<: LaurentPolynomial{T}, S<:Number}
564
590
R =eltype((one(T)+one(S))/1)
565
591
566
592
ifhasnan(p) ||isnan(k)
567
-
returnP([NaN], 0:0, p.var) # not R(NaN)!! don't like XXX
593
+
returnP([NaN], 0, p.var) # not R(NaN)!! don't like XXX
568
594
end
569
595
570
596
571
-
m,n =extrema(p)
597
+
m,n =(extrema∘ degreerange)(p)
572
598
if n <0
573
599
n =0
574
600
else
@@ -587,14 +613,14 @@ function integrate(p::P, k::S) where {T, P<: LaurentPolynomial{T}, S<:Number}
587
613
588
614
as[1-m] = k
589
615
590
-
return⟒(P)(as, m:n, p.var)
616
+
return⟒(P)(as, m, p.var)
591
617
592
618
end
593
619
594
620
595
621
function Base.gcd(p::LaurentPolynomial{T}, q::LaurentPolynomial{T}, args...; kwargs...) where {T}
596
-
mp, Mp =extrema(p)
597
-
mq, Mq =extrema(q)
622
+
mp, Mp =(extrema∘ degreerange)(p)
623
+
mq, Mq =(extrema∘ degreerange)(q)
598
624
if mp <0|| mq <0
599
625
throw(ArgumentError("GCD is not defined when there are `x⁻¹` terms"))
0 commit comments