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/common.jl
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -231,6 +231,9 @@ extrema(p, cps) # (-2.0, Inf)
231
231
cps = Polynomials.critical_points(p, (0, 2))
232
232
extrema(p, cps) # (-2.0, 2.0)
233
233
```
234
+
235
+
!!! note
236
+
There is a *big* difference between `minimum(p)` and `minimum(p, cps)`. The former takes the viewpoint that a polynomial `p` is a certain type of vector of its coefficients; returning the smallest coefficient. The latter uses `p` as a callable object, returning the smallest of the values `p.(cps)`.
234
237
"""
235
238
functioncritical_points(p::AbstractPolynomial{T}, I =domain(p);
Copy file name to clipboardExpand all lines: src/polynomials/standard-basis.jl
+20-2Lines changed: 20 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -550,6 +550,8 @@ Fit a degree ``n`` or less polynomial through the points ``(x_i, y_i)`` using Ar
550
550
551
551
The use of a Vandermonde matrix to fit a polynomial to data is exponentially ill-conditioned for larger values of ``n``. The Arnoldi orthogonalization fixes this problem.
552
552
553
+
This representation is useful for *evaluating* the polynomial, but does not lend itself to other polynomial manipulations.
554
+
553
555
# Returns
554
556
555
557
Returns an instance of `ArnoldiFit`. This object can be used to evaluate the polynomial. To manipulate the polynomial, the object can be `convert`ed to other polynomial types, though there may be some loss in accuracy when doing polynomial evaluations afterwards for higher-degree polynomials.
@@ -562,6 +564,12 @@ The two main functions are translations from example code in:
562
564
PABLO D. BRUBECK, YUJI NAKATSUKASA, AND LLOYD N. TREFETHEN;
Lei-Hong Zhang, Yangfeng Su, Ren-Cang Li. Accurate polynomial fitting and evaluation via Arnoldi. Numerical Algebra, Control and Optimization. doi: 10.3934/naco.2023002
570
+
571
+
572
+
565
573
# Examples:
566
574
567
575
```
@@ -571,22 +579,26 @@ p = fit(Polynomial, xs, f.(xs));
571
579
q = fit(ArnoldiFit, xs, f.(xs));
572
580
maximum(abs, p(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 3.304586010148457e16
573
581
maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 1.1939520722092922e-7
582
+
```
574
583
584
+
```
575
585
N = 250; xs = [cos(j*pi/N) for j in N:-1:0];
576
586
p = fit(Polynomial, xs, f.(xs));
577
587
q = fit(ArnoldiFit, xs, f.(xs));
578
588
maximum(abs, p(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 3.55318186254542e92
579
589
maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 8.881784197001252e-16
590
+
```
580
591
592
+
```
581
593
p = fit(Polynomial, xs, f.(xs), 10); # least-squares fit
582
594
q = fit(ArnoldiFit, xs, f.(xs), 10);
583
595
maximum(abs, q(x) - p(x) for x ∈ range(-1,stop=1,length=500)) # 4.6775083806238626e-14
584
596
Polynomials.norm(q-p, Inf) # 2.2168933355715126e-12 # promotes `q` to `Polynomial`
585
597
```
586
598
587
-
"""
588
-
polyfit
599
+
To manipulate the fitted polynomial, conversion is necessary. Conversion can lead to wildly divergent polynomials when n is large.
589
600
601
+
"""
590
602
functionpolyfitA(x, y, n=length(x)-1; var=:x)
591
603
m =length(x)
592
604
T =eltype(y)
@@ -596,6 +608,7 @@ function polyfitA(x, y, n=length(x)-1; var=:x)
596
608
597
609
q =zeros(T, m)
598
610
611
+
# we have Vₓ = QR, y = Vₓa = Q(Ra) = Qd, so d = Q \ y
599
612
@inboundsfor k =1:n
600
613
q .= x .* Q[:,k]
601
614
for j in1:k
@@ -610,6 +623,8 @@ function polyfitA(x, y, n=length(x)-1; var=:x)
610
623
ArnoldiFit{eltype(d),typeof(H),Symbol(var)}(d, H)
611
624
end
612
625
626
+
# from Vₓ = QR, we get Vₛ = WR and f = Vₛa = WRa = W(d) stored above
627
+
# this finds W
613
628
functionpolyvalA(d, H::AbstractMatrix{S}, s::T) where {T, S}
614
629
R =promote_type(T,S)
615
630
n =length(d) -1
@@ -629,6 +644,9 @@ end
629
644
ArnoldiFit
630
645
631
646
A polynomial type produced through fitting a degree ``n`` or less polynomial to data ``(x_1,y_1),…,(x_N, y_N), N ≥ n+1``, This uses Arnoldi orthogonalization to avoid the exponentially ill-conditioned Vandermonde polynomial. See [`Polynomials.polyfitA`](@ref) for details.
647
+
648
+
This is useful for polynomial evaluation, but other polynomial operations are not defined. Though these fitted polynomials may be converted to other types, for larger degrees this will prove unstable.
0 commit comments