Skip to content

Commit f98eef0

Browse files
authored
adjust some doc strings (#468)
1 parent 2c14b8f commit f98eef0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/common.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ extrema(p, cps) # (-2.0, Inf)
231231
cps = Polynomials.critical_points(p, (0, 2))
232232
extrema(p, cps) # (-2.0, 2.0)
233233
```
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)`.
234237
"""
235238
function critical_points(p::AbstractPolynomial{T}, I = domain(p);
236239
endpoints::Bool=true) where {T <: Real}

src/polynomials/standard-basis.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ Fit a degree ``n`` or less polynomial through the points ``(x_i, y_i)`` using Ar
550550
551551
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.
552552
553+
This representation is useful for *evaluating* the polynomial, but does not lend itself to other polynomial manipulations.
554+
553555
# Returns
554556
555557
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:
562564
PABLO D. BRUBECK, YUJI NAKATSUKASA, AND LLOYD N. TREFETHEN;
563565
[arXiv:1911.09988](https://people.maths.ox.ac.uk/trefethen/vander_revised.pdf)
564566
567+
For more details, see also:
568+
569+
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+
565573
# Examples:
566574
567575
```
@@ -571,22 +579,26 @@ p = fit(Polynomial, xs, f.(xs));
571579
q = fit(ArnoldiFit, xs, f.(xs));
572580
maximum(abs, p(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 3.304586010148457e16
573581
maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 1.1939520722092922e-7
582+
```
574583
584+
```
575585
N = 250; xs = [cos(j*pi/N) for j in N:-1:0];
576586
p = fit(Polynomial, xs, f.(xs));
577587
q = fit(ArnoldiFit, xs, f.(xs));
578588
maximum(abs, p(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 3.55318186254542e92
579589
maximum(abs, q(x) - f(x) for x ∈ range(-1,stop=1,length=500)) # 8.881784197001252e-16
590+
```
580591
592+
```
581593
p = fit(Polynomial, xs, f.(xs), 10); # least-squares fit
582594
q = fit(ArnoldiFit, xs, f.(xs), 10);
583595
maximum(abs, q(x) - p(x) for x ∈ range(-1,stop=1,length=500)) # 4.6775083806238626e-14
584596
Polynomials.norm(q-p, Inf) # 2.2168933355715126e-12 # promotes `q` to `Polynomial`
585597
```
586598
587-
"""
588-
polyfit
599+
To manipulate the fitted polynomial, conversion is necessary. Conversion can lead to wildly divergent polynomials when n is large.
589600
601+
"""
590602
function polyfitA(x, y, n=length(x)-1; var=:x)
591603
m = length(x)
592604
T = eltype(y)
@@ -596,6 +608,7 @@ function polyfitA(x, y, n=length(x)-1; var=:x)
596608

597609
q = zeros(T, m)
598610

611+
# we have Vₓ = QR, y = Vₓa = Q(Ra) = Qd, so d = Q \ y
599612
@inbounds for k = 1:n
600613
q .= x .* Q[:,k]
601614
for j in 1:k
@@ -610,6 +623,8 @@ function polyfitA(x, y, n=length(x)-1; var=:x)
610623
ArnoldiFit{eltype(d),typeof(H),Symbol(var)}(d, H)
611624
end
612625

626+
# from Vₓ = QR, we get Vₛ = WR and f = Vₛa = WRa = W(d) stored above
627+
# this finds W
613628
function polyvalA(d, H::AbstractMatrix{S}, s::T) where {T, S}
614629
R = promote_type(T,S)
615630
n = length(d) - 1
@@ -629,6 +644,9 @@ end
629644
ArnoldiFit
630645
631646
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.
649+
632650
"""
633651
struct ArnoldiFit{T, M<:AbstractArray{T,2}, X} <: AbstractPolynomial{T,X}
634652
coeffs::Vector{T}

0 commit comments

Comments
 (0)