Skip to content

Commit 6a5c1b6

Browse files
committed
update documentation
1 parent 92e9936 commit 6a5c1b6

File tree

7 files changed

+99
-23
lines changed

7 files changed

+99
-23
lines changed

docs/src/extending.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
# Extending Polynomials
22

3-
The [`AbstractPolynomial`](@ref) type was made to be extended via a rich interface. To implement a new polynomial type, the following methods should be implemented.
3+
The [`AbstractPolynomial`](@ref) type was made to be extended via a rich interface.
44

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

9-
| Function | Required | Description |
9+
To implement a new polynomial type, `P`, the following methods should be implemented.
10+
11+
!!! note
12+
Promotion rules will always coerce towards the [`Polynomial`](@ref) type, so not all methods have to be implemented if you provide a conversion function.
13+
14+
As always, if the default implementation does not work or there are more efficient ways of implementing, feel free to overwrite functions from `common.jl` for your type.
15+
16+
| Function | Required | Notes |
1017
|----------|:--------:|:------------|
1118
| Constructor | x | |
12-
| Type function (`(::MyType)()`) | x | |
19+
| Type function (`(::P)(x)`) | x | |
1320
| `convert(::Polynomial, ...)` | | Not required, but the library is built off the [`Polynomial`](@ref) type, so all operations are guaranteed to work with it. Also consider writing the inverse conversion method. |
14-
| `domain` | x | |
21+
| `domain` | x | Should return an [`AbstractInterval`](https://invenia.github.io/Intervals.jl/stable/#Intervals-1) |
1522
| `vander` | | Required for [`fit`](@ref) |
1623
| `companion` | | Required for [`roots`](@ref) |
17-
| `fromroots` | | |
18-
| `+` | | |
19-
| `-` | | |
20-
| `*` | | |
21-
| `divrem` | | |
22-
24+
| `fromroots` | | By default, will form polynomials using `prod(variable(::P) - r)` for reach root `r`|
25+
| `+(::P, ::P)` | | Addition of polynomials |
26+
| `-(::P, ::P)` | | Subtraction of polynomials |
27+
| `*(::P, ::P)` | | Multiplication of polynomials |
28+
| `divrem` | | Required for [`gcd`](@ref)|
2329

30+
Check out both the [`Polynomial`](@ref) and [`ChebyshevT`](@ref) for examples of this interface being extended!

docs/src/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ the returned roots may be real or complex.
132132
```jldoctest
133133
julia> roots(Polynomial([1, 0, -1]))
134134
2-element Array{Float64,1}:
135-
-1.0
136135
1.0
136+
-1.0
137137
138138
julia> roots(Polynomial([1, 0, 1]))
139139
2-element Array{Complex{Float64},1}:
@@ -142,8 +142,8 @@ julia> roots(Polynomial([1, 0, 1]))
142142
143143
julia> roots(Polynomial([0, 0, 1]))
144144
2-element Array{Float64,1}:
145-
-0.0
146145
0.0
146+
-0.0
147147
```
148148

149149
### Fitting arbitrary data

docs/src/polynomials/chebyshev.md

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

9-
!!! warning
10-
The Chebyshev polynomials are not fully implemented, yet.
11-
129
## First Kind
1310

1411
```@docs
1512
ChebyshevT
16-
ChebyshevT
13+
ChebyshevT()
1714
```
1815

19-
## Second Kind
16+
### Conversion
17+
18+
[`ChebyshevT`](@ref) can be converted to [`Polynomial`](@ref) and vice-versa.
19+
20+
```jldoctest
21+
julia> c = ChebyshevT([1, 0, 3, 4])
22+
ChebyshevT([1, 0, 3, 4])
23+
24+
julia> p = convert(Polynomial, c)
25+
Polynomial(-2 - 12*x + 6*x^2 + 16*x^3)
26+
27+
julia> convert(ChebyshevT, p)
28+
ChebyshevT([1.0, 0.0, 3.0, 4.0])
29+
```

docs/src/polynomials/polynomial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88

99
```@docs
1010
Polynomial
11-
variable
11+
Polynomial()
1212
```
1313

1414
```@docs

docs/src/reference.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ degree
2121
length
2222
size
2323
domain
24-
scale_to_domain
24+
mapdomain
2525
chop
2626
chop!
2727
truncate
@@ -62,6 +62,9 @@ gcd
6262
## Mathematical Function
6363

6464
```@docs
65+
zero
66+
one
67+
variable
6568
fromroots
6669
roots
6770
derivative
@@ -87,3 +90,23 @@ plot(::AbstractPolynomial, a, b; kwds...)
8790
```
8891

8992
will plot the polynomial within the range `[a, b]`.
93+
94+
### Example: The Polynomials.jl logo
95+
```@example
96+
using Plots, Polynomials
97+
xs = range(-1, 1, length=100)
98+
chebs = [
99+
ChebyshevT([0, 1]),
100+
ChebyshevT([0, 0, 1]),
101+
ChebyshevT([0, 0, 0, 1]),
102+
ChebyshevT([0, 0, 0, 0, 1]),
103+
]
104+
colors = ["#4063D8", "#389826", "#CB3C33", "#9558B2"]
105+
plot() # hide
106+
for (cheb, col) in zip(chebs, colors)
107+
plot!(xs, cheb.(xs), c=col, lw=5, label="")
108+
end
109+
savefig("chebs.svg"); nothing # hide
110+
```
111+
112+
![](chebs.svg)

src/polynomials/ChebyshevT.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ terms of the given variable `x`. `x` can be a character, symbol, or string.
1111
# Examples
1212
1313
```jldoctest
14-
julia> c = ChebyshevT([1, 0, 3, 4])
14+
julia> ChebyshevT([1, 0, 3, 4])
1515
ChebyshevT([1, 0, 3, 4])
1616
1717
julia> ChebyshevT([1, 2, 3, 0], :s)
1818
ChebyshevT([1, 2, 3])
1919
2020
julia> one(ChebyshevT)
2121
ChebyshevT([1.0])
22-
23-
julia> convert(Polynomial, c)
24-
Polynomial(-2 - 12*x + 6*x^2 + 16*x^3)
2522
```
2623
"""
2724
struct ChebyshevT{T <: Number} <: AbstractPolynomial{T}
@@ -65,8 +62,26 @@ domain(::Type{<:ChebyshevT}) = Interval(-1, 1)
6562
(::ChebyshevT)(x)
6663
6764
Evaluate the Chebyshev polynomial at `x`. If `x` is outside of the domain of [-1, 1], an error will be thrown. The evaluation uses Clenshaw Recursion.
65+
66+
# Examples
67+
```jldoctest
68+
julia> c = ChebyshevT([2.5, 1.5, 1.0])
69+
ChebyshevT([2.5, 1.5, 1.0])
70+
71+
julia> c(0)
72+
1.5
73+
74+
julia> c.(-1:0.5:1)
75+
5-element Array{Float64,1}:
76+
2.0
77+
1.25
78+
1.5
79+
2.75
80+
5.0
81+
```
6882
"""
6983
function (ch::ChebyshevT{T})(x::S) where {T,S}
84+
any(x .∉ domain(ch)) && error("$x outside of domain")
7085
R = promote_type(T, S)
7186
length(ch) == 0 && return zero(R)
7287
length(ch) == 1 && return R(ch[0])

src/polynomials/Polynomial.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ end
4242
domain(::Type{<:Polynomial}) = Interval(-Inf, Inf)
4343
mapdomain(::Type{<:Polynomial}, x::AbstractArray) = x
4444

45+
"""
46+
(p::Polynomial)(x)
47+
48+
Evaluate the polynomial using [Horner's Method](https://en.wikipedia.org/wiki/Horner%27s_method), also known as synthetic division.
49+
50+
# Examples
51+
```jldoctest
52+
julia> p = Polynomial([1, 0, 3])
53+
Polynomial(1 + 3*x^2)
54+
55+
julia> p(0)
56+
1
57+
58+
julia> p.(0:3)
59+
4-element Array{Int64,1}:
60+
1
61+
4
62+
13
63+
28
64+
```
65+
"""
4566
function (p::Polynomial{T})(x::S) where {T,S}
4667
R = promote_type(T, S)
4768
length(p) == 0 && return zero(R)

0 commit comments

Comments
 (0)