Skip to content

Commit 75944ec

Browse files
committed
WIP
1 parent 8ad387a commit 75944ec

File tree

17 files changed

+448
-101
lines changed

17 files changed

+448
-101
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ ERROR: Polynomials must have same variable.
9696
#### Integrals and Derivatives
9797

9898
Integrate the polynomial `p` term by term, optionally adding constant
99-
term `k`. The order of the resulting polynomial is one higher than the
100-
order of `p`.
99+
term `k`. The degree of the resulting polynomial is one higher than the
100+
degree of `p`.
101101

102102
```julia
103103
julia> integrate(Polynomial([1, 0, -1]))
@@ -107,8 +107,8 @@ julia> integrate(Polynomial([1, 0, -1]), 2)
107107
Polynomial(2.0 + x - 0.3333333333333333x^3)
108108
```
109109

110-
Differentiate the polynomial `p` term by term. The order of the
111-
resulting polynomial is one lower than the order of `p`.
110+
Differentiate the polynomial `p` term by term. The degree of the
111+
resulting polynomial is one lower than the degree of `p`.
112112

113113
```julia
114114
julia> derivative(Polynomial([1, 3, -1]))
@@ -119,7 +119,7 @@ Polynomial(3 - 2x)
119119

120120

121121
Return the roots (zeros) of `p`, with multiplicity. The number of
122-
roots returned is equal to the order of `p`. By design, this is not type-stable,
122+
roots returned is equal to the degree of `p`. By design, this is not type-stable,
123123
the returned roots may be real or complex.
124124

125125
```julia
@@ -141,7 +141,7 @@ julia> roots(Polynomial([0, 0, 1]))
141141

142142
#### Fitting arbitrary data
143143

144-
Fit a polynomial (of order `deg`) to `x` and `y` using a least-squares approximation.
144+
Fit a polynomial (of degree `deg`) to `x` and `y` using a least-squares approximation.
145145

146146
```julia
147147
julia> xs = 0:4; ys = @. exp(-xs) + sin(xs);
@@ -174,7 +174,7 @@ Polynomial objects also have other methods:
174174

175175
* `conj`: finds the conjugate of a polynomial over a complex fiel
176176

177-
* `truncate`: set to 0 all small terms in a polynomial;
177+
* `truncate`: set to 0 all small terms in a polynomial;
178178
* `chop` chops off any small leading values that may arise due to floating point operations.
179179

180180
* `gcd`: greatest common divisor of two polynomials.

docs/src/extending.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Extending Polynomials
22

3-
The [`AbstractPolynomial`](@ref) type was made to be extended via a rich interface.
3+
The [`AbstractPolynomial`](@ref) type was made to be extended via a rich interface.
44

55
```@docs
66
AbstractPolynomial
77
```
88

9-
To implement a new polynomial type, `P`, the following methods should be implemented.
9+
A polynomial's coefficients are relative to some *basis*. The `Polynomial` type relates coefficients `[a0, a1, ..., an]`, say, to the polynomial `a0 + a1*x + a2*x^ + ... + an*x^n`, through the natural basis `1, x, x^2, ..., x^n`. New p olynomial types typically represent the polynomial through a different basis. For example, `CheyshevT` uses a basis `T_0=1, T_1=x, T_2=2x^2-1, ..., T_n = 2xT_{n-1} - T_{n-2}`. For this type the coefficients `[a0,a1,...,an]` are associated with the polynomial `a0*T0 + a1*T_1 + ... + an*T_n`.
10+
11+
To implement a new polynomial type, `P`, the following methods should
12+
be implemented.
1013

1114
!!! note
1215
Promotion rules will always coerce towards the [`Polynomial`](@ref) type, so not all methods have to be implemented if you provide a conversion function.
@@ -26,5 +29,6 @@ As always, if the default implementation does not work or there are more efficie
2629
| `-(::P, ::P)` | | Subtraction of polynomials |
2730
| `*(::P, ::P)` | | Multiplication of polynomials |
2831
| `divrem` | | Required for [`gcd`](@ref)|
32+
| `variable`| | Convenience to find monomial `x` in new basis|
2933

30-
Check out both the [`Polynomial`](@ref) and [`ChebyshevT`](@ref) for examples of this interface being extended!
34+
Check out both the [`Polynomial`](@ref) and [`ChebyshevT`](@ref) for examples of this interface being extended. [`Bernstein`](@ref) is an example where the basis depends on the degree of the polynomials being represented.

docs/src/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,17 @@ savefig("polyfit.svg"); nothing # hide
163163

164164
![](polyfit.svg)
165165

166+
### Iteration
167+
168+
A polynomial, e.g. `a_0 + a_1 x + a_2 x^2 + ... + a_n x^n` can be seen as a collection of coefficients, `[a_0, a_1, ..., a_n]`, reelative to some polynomial basis. The most familiar basis being `1`, `x`, `x^2`, ... If the basis is implicit, then a polynomial is just a vector. Vectors or 1-based, but polynomial types are 0-based, for purposes of indexing (e.g. `getindex`, `setindex!`, `eachindex`). Iteration over a polynomial steps through the basis vectors, e.g. `a_0`, `a_1 x`, ...
166169

167170
## Related Packages
168171

169172
* [MultiPoly.jl](https://github.com/daviddelaat/MultiPoly.jl) for sparse multivariate polynomials
170173

171174
* [MultivariatePolynomials.jl](https://github.com/blegat/MultivariatePolynomials.jl) for multivariate polynomials and moments of commutative or non-commutative variables
172175

173-
* [Nemo.jl](https://github.com/wbhart/Nemo.jl) for generic polynomial rings, matrix spaces, fraction fields, residue rings, power series
176+
* [AbstractAlgeebra.jl](https://github.com/wbhart/AbstractAlgebra.jl) for generic polynomial rings, matrix spaces, fraction fields, residue rings, power series.
174177

175178
* [PolynomialRoots.jl](https://github.com/giordano/PolynomialRoots.jl) for a fast complex polynomial root finder
176179

docs/src/polynomials/bernstein.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Bernstein Polynomials
2+
3+
```@meta
4+
DocTestSetup = quote
5+
using Polynomials
6+
end
7+
```
8+
9+
10+
The [Bernstein polynomials](https://en.wikipedia.org/wiki/Bernstein_polynomial) are a family of polynomials defined on the interval `[0,1]`. For each `n` there are `n+1` polynomials, given by: `b(n,nu) = choose(n,nu) x^nu * (1-x)^(n-nu)` for `nu` in `0:n`. Together, these form a basis that can represent any polynomial of degree `n` or less through a linear combination.
11+
12+
13+
## Bernstein(N,T)
14+
15+
```@docs
16+
Bernstein
17+
Bernstein{N,T}()
18+
```
19+
20+
The `Bernstein{N,T}` type holds coefficients representing the polynomial `a_0 b(n,0) + a_1 b(n,1) + ... + a_n b(n,n)`.
21+
22+
For example, using `n=3`, the monomial `x` is represented by `0 b(3,0) + 1/3 b(3,1) + 2/3 b(3,2) + 1 b(3,3)`, which can be constructed through `Bernstein{3, T}([0, 1/3, 2/3, 1])`
23+
24+
25+
### Conversion
26+
27+
[`Bernsteing`](@ref) can be converted to [`Polynomial`](@ref) and vice-versa.
28+
29+
```jldoctest
30+
julia> b = Bernstein([1, 0, 3, 4])
31+
Bernstein(1⋅β(3, 0)(x) + 3⋅β(3, 2)(x) + 4⋅β(3, 3)(x))
32+
33+
julia> p = convert(Polynomial{Int}, b)
34+
Polynomial(1 - 3*x + 12*x^2 - 6*x^3)
35+
36+
julia> convert(Bernstein{3, Float64}, p)
37+
Bernstein(1.0⋅β(3, 0)(x) + 3.0⋅β(3, 2)(x) + 4.0⋅β(3, 3)(x))
38+
```
39+
40+
Bernstein polynomials may be converted into a basis with a larger degree:
41+
42+
```jldoctest
43+
julia> convert(Bernstein{4, Float64}, b)
44+
Bernstein(1.0⋅β(4, 0)(x) + 0.25⋅β(4, 1)(x) + 1.5⋅β(4, 2)(x) + 3.25⋅β(4, 3)(x) + 4.0⋅β(4, 4)(x))
45+
```

docs/src/polynomials/chebyshev.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,34 @@ DocTestSetup = quote
66
end
77
```
88

9+
10+
The [Chebyshev polynomials](https://en.wikipedia.org/wiki/Chebyshev_polynomials) are two sequences of polynomials, `T_n` and `U_n`. The Chebyshev polynomials of the first kind, `T_n`, can be defined by the recurrence relation `T_0(x)=1`, `T_1(x)=x`, and `T_{n+1}(x) = 2xT_n{x}-T_{n-1}(x)`. The Chebyshev polynomioals of the second kind, `U_n(x)`, can be defined by `U_0(x)=1`, `U_1(x)=2x`, and `U_{n+1}(x) = 2xU_n(x) - U_{n-1}(x)`. Both `T_n` and `U_n` have degree `n`, and any polynomial of degree `n` may be written as a linear combination of the polynomials `T_0`, `T_1`, ..., `T_n` (similarly with `U`).
11+
12+
913
## First Kind
1014

1115
```@docs
1216
ChebyshevT
1317
ChebyshevT()
1418
```
1519

20+
The `ChebyshevT` type holds coefficients representing the polynomial `a_0 T_0 + a_1 T_1 + ... + a_n T_n`.
21+
22+
For example, the basis polynomial `T_4` can be represented with `ChebyshevT([0,0,0,0,1])`.
23+
24+
1625
### Conversion
1726

1827
[`ChebyshevT`](@ref) can be converted to [`Polynomial`](@ref) and vice-versa.
1928

2029
```jldoctest
2130
julia> c = ChebyshevT([1, 0, 3, 4])
22-
ChebyshevT([1, 0, 3, 4])
31+
ChebyshevT(1⋅T_0(x) + 3⋅T_2(x) + 4⋅T_3(x))
32+
2333
2434
julia> p = convert(Polynomial, c)
2535
Polynomial(-2 - 12*x + 6*x^2 + 16*x^3)
2636
2737
julia> convert(ChebyshevT, p)
28-
ChebyshevT([1.0, 0.0, 3.0, 4.0])
38+
ChebyshevT(1.0⋅T_0(x) + 3.0⋅T_2(x) + 4.0⋅T_3(x))
2939
```

docs/src/reference.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,19 @@ will plot the polynomial within the range `[a, b]`.
9393
### Example: The Polynomials.jl logo
9494
```@example
9595
using Plots, Polynomials
96-
xs = range(-1, 1, length=100)
96+
# T1, T2, T3, and T4:
9797
chebs = [
9898
ChebyshevT([0, 1]),
9999
ChebyshevT([0, 0, 1]),
100100
ChebyshevT([0, 0, 0, 1]),
101101
ChebyshevT([0, 0, 0, 0, 1]),
102102
]
103103
colors = ["#4063D8", "#389826", "#CB3C33", "#9558B2"]
104-
plot() # hide
105-
for (cheb, col) in zip(chebs, colors)
106-
plot!(xs, cheb.(xs), c=col, lw=5, label="")
104+
itr = zip(chebs, colors)
105+
(cheb,col), state = iterate(itr)
106+
p = plot(cheb, c=col, lw=5, legend=false, label="")
107+
for (cheb, col) in Base.Iterators.rest(itr, state)
108+
plot!(cheb, c=col, lw=5)
107109
end
108110
savefig("chebs.svg"); nothing # hide
109111
```

src/Polynomials.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include("contrib.jl")
1212
include("polynomials/Polynomial.jl")
1313
include("polynomials/ChebyshevT.jl")
1414
include("polynomials/ChebyshevU.jl")
15+
include("polynomials/Bernstein.jl")
1516

1617
include("polynomials/Poly.jl") # Deprecated -> Will be removed
1718
include("pade.jl")

0 commit comments

Comments
 (0)