1
- # Polynomials
1
+ # Polynomials.jl
2
2
3
3
Basic arithmetic, integration, differentiation, evaluation, and root finding over dense univariate polynomials.
4
4
5
- [ ![ Polynomials] ( http://pkg.julialang.org/badges/Polynomials_0.6.svg )] ( http://pkg.julialang.org/?pkg=Polynomials )
6
-
7
- Master branch:
5
+ [ ![ ] ( https://img.shields.io/badge/docs-stable-blue.svg )] ( https://JuliaMath.github.io/Polynomials.jl/stable )
6
+ [ ![ ] ( https://img.shields.io/badge/docs-latest-blue.svg )] ( https://JuliaMath.github.io/Polynomials.jl/dev )
8
7
[ ![ Build Status] ( https://travis-ci.org/JuliaMath/Polynomials.jl.svg?branch=master )] ( https://travis-ci.org/JuliaMath/Polynomials.jl )
9
- [ ![ Coverage Status ] ( https://coveralls .io/repos/github/ JuliaMath/Polynomials.jl/badge.svg )] ( https://coveralls .io/github /JuliaMath/Polynomials.jl )
8
+ [ ![ codecov ] ( https://codecov .io/gh/ JuliaMath/Polynomials.jl/branch/master/graph/ badge.svg )] ( https://codecov .io/gh /JuliaMath/Polynomials.jl )
10
9
11
- Documentation:
12
- [ ![ ] ( https://img.shields.io/badge/docs-stable-blue.svg )] ( https://JuliaMath.github.io/Polynomials.jl/stable )
13
- [ ![ ] ( https://img.shields.io/badge/docs-latest-blue.svg )] ( https://JuliaMath.github.io/Polynomials.jl/latest )
14
10
15
- #### Poly(a::Vector) where {T<: Number }
11
+ ## Installation
12
+
13
+ ``` julia
14
+ (v1.2 ) pkg> add Polynomials
15
+
16
+ julia> using Polynomials
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ #### Available Polynomials
22
+
23
+ * ` Polynomial ` - Standard polynomials
24
+ * ` ChebyshevT ` - Chebyshev polynomials of the first kind
25
+
26
+ #### Construction and Evaluation
16
27
17
28
Construct a polynomial from its coefficients, lowest order first.
18
29
19
30
``` julia
20
- julia> Poly ([1 ,0 ,3 ,4 ])
21
- Poly (1 + 3 x^ 2 + 4 x^ 3 )
31
+ julia> Polynomial ([1 ,0 ,3 ,4 ])
32
+ Polynomial (1 + 3 x^ 2 + 4 x^ 3 )
22
33
```
23
34
24
35
An optional variable parameter can be added.
25
36
26
37
``` julia
27
- julia> Poly ([1 ,2 ,3 ], :s )
28
- Poly (1 + 2 s + 3 s^ 2 )
38
+ julia> Polynomial ([1 ,2 ,3 ], :s )
39
+ Polynomial (1 + 2 s + 3 s^ 2 )
29
40
```
30
41
31
- #### poly(r::AbstractVector)
42
+ Construct a polynomial from its roots.
32
43
33
- Construct a polynomial from its roots. This is in contrast to the
34
- ` Poly ` constructor, which constructs a polynomial from its
35
- coefficients.
44
+ ``` julia
45
+ julia> fromroots ([1 ,2 ,3 ]) # (x-1)*(x-2)*(x-3)
46
+ Polynomial (- 6 + 11 x - 6 x^ 2 + x^ 3 )
47
+ ```
48
+
49
+ Evaluate the polynomial ` p ` at ` x ` .
36
50
37
51
``` julia
38
- // Represents (x - 1 ) * (x - 2 ) * (x - 3 )
39
- julia> poly ([ 1 , 2 , 3 ] )
40
- Poly ( - 6 + 11 x - 6 x ^ 2 + x ^ 3 )
52
+ julia > p = Polynomial ([ 1 , 0 , - 1 ] )
53
+ julia> p ( 0.1 )
54
+ 0.99
41
55
```
42
56
43
- #### +, -, * , /, div, ==
57
+ #### Arithmetic
44
58
45
59
The usual arithmetic operators are overloaded to work on polynomials, and combinations of polynomials and scalars.
60
+
46
61
``` julia
47
- julia> p = Poly ([1 ,2 ])
48
- Poly (1 + 2 x)
62
+ julia> p = Polynomial ([1 ,2 ])
63
+ Polynomial (1 + 2 x)
49
64
50
- julia> q = Poly ([1 , 0 , - 1 ])
51
- Poly (1 - x^ 2 )
65
+ julia> q = Polynomial ([1 , 0 , - 1 ])
66
+ Polynomial (1 - x^ 2 )
52
67
53
68
julia> 2 p
54
- Poly (2 + 4 x)
69
+ Polynomial (2 + 4 x)
55
70
56
71
julia> 2 + p
57
- Poly (3 + 2 x)
72
+ Polynomial (3 + 2 x)
58
73
59
74
julia> p - q
60
75
Poly (2 x + x^ 2 )
61
76
62
77
julia> p * q
63
- Poly (1 + 2 x - x^ 2 - 2 x^ 3 )
78
+ Polynomial (1 + 2 x - x^ 2 - 2 x^ 3 )
64
79
65
80
julia> q / 2
66
- Poly (0.5 - 0.5 x^ 2 )
81
+ Polynomial (0.5 - 0.5 x^ 2 )
67
82
68
- julia> q ÷ p # `div`, also `rem` and `divrem`
69
- Poly (0.25 - 0.5 x)
83
+ julia> q ÷ p # `div`, also `rem` and `divrem`
84
+ Polynomial (0.25 - 0.5 x)
70
85
```
71
86
72
87
Note that operations involving polynomials with different variables will error.
73
88
74
89
``` julia
75
- julia> p = Poly ([1 , 2 , 3 ], :x )
76
- julia> q = Poly ([1 , 2 , 3 ], :s )
90
+ julia> p = Polynomial ([1 , 2 , 3 ], :x )
91
+ julia> q = Polynomial ([1 , 2 , 3 ], :s )
77
92
julia> p + q
78
93
ERROR: Polynomials must have same variable.
79
94
```
80
95
81
- To get the degree of the polynomial use the ` degree ` method
82
-
83
- ```
84
- julia> degree(p)
85
- 2
86
-
87
- julia> degree(p^2)
88
- 4
89
-
90
- julia> degree(p-p)
91
- -1
92
- ```
93
-
94
- #### polyval(p::Poly, x::Number)
95
-
96
- Evaluate the polynomial ` p ` at ` x ` .
97
-
98
- ``` julia
99
- julia> p = Poly ([1 , 0 , - 1 ])
100
- julia> polyval (p, 0.1 )
101
- 0.99
102
- ```
103
-
104
- A call method is also available:
105
-
106
- ``` julia
107
- julia> p (0.1 )
108
- 0.99
109
- ```
110
-
111
-
112
- #### polyint(p::Poly, k::Number=0)
96
+ #### Integrals and Derivatives
113
97
114
98
Integrate the polynomial ` p ` term by term, optionally adding constant
115
99
term ` k ` . The order of the resulting polynomial is one higher than the
116
100
order of ` p ` .
117
101
118
102
``` julia
119
- julia> polyint ( Poly ([1 , 0 , - 1 ]))
120
- Poly (x - 0.3333333333333333 x^ 3 )
103
+ julia> integrate ( Polynomial ([1 , 0 , - 1 ]))
104
+ Polynomial (x - 0.3333333333333333 x^ 3 )
121
105
122
- julia> polyint ( Poly ([1 , 0 , - 1 ]), 2 )
123
- Poly (2.0 + x - 0.3333333333333333 x^ 3 )
106
+ julia> integrate ( Polynomial ([1 , 0 , - 1 ]), 2 )
107
+ Polynomial (2.0 + x - 0.3333333333333333 x^ 3 )
124
108
```
125
109
126
- #### polyder(p::Poly)
127
-
128
110
Differentiate the polynomial ` p ` term by term. The order of the
129
111
resulting polynomial is one lower than the order of ` p ` .
130
112
131
113
``` julia
132
- julia> polyder ( Poly ([1 , 3 , - 1 ]))
133
- Poly (3 - 2 x)
114
+ julia> derivative ( Polynomial ([1 , 3 , - 1 ]))
115
+ Polynomial (3 - 2 x)
134
116
```
135
117
136
- #### roots(p::Poly)
118
+ #### Root-finding
119
+
137
120
138
121
Return the roots (zeros) of ` p ` , with multiplicity. The number of
139
122
roots returned is equal to the order of ` p ` . By design, this is not type-stable,
140
123
the returned roots may be real or complex.
141
124
142
125
``` julia
143
- julia> roots (Poly ([1 , 0 , - 1 ]))
126
+ julia> roots (Polynomial ([1 , 0 , - 1 ]))
144
127
2 - element Array{Float64,1 }:
145
128
- 1.0
146
129
1.0
147
130
148
- julia> roots (Poly ([1 , 0 , 1 ]))
131
+ julia> roots (Polynomial ([1 , 0 , 1 ]))
149
132
2 - element Array{Complex{Float64},1 }:
150
133
0.0 + 1.0im
151
134
0.0 - 1.0im
152
135
153
- julia> roots (Poly ([0 , 0 , 1 ]))
136
+ julia> roots (Polynomial ([0 , 0 , 1 ]))
154
137
2 - element Array{Float64,1 }:
155
138
0.0
156
139
0.0
157
140
```
158
141
159
- #### polyfit(x, y, n=length(x)-1)
142
+ #### Fitting arbitrary data
160
143
161
- * ` polyfit ` : fits a polynomial (of order ` n ` ) to ` x ` and ` y ` using a least-squares approximation.
144
+ Fit a polynomial (of order ` deg ` ) to ` x ` and ` y ` using a least-squares approximation.
162
145
163
146
``` julia
164
- julia> xs = 1 : 4 ; ys = exp .(xs); polyfit (xs, ys)
165
- Poly (- 7.717211620141281 + 17.9146616149694 x - 9.77757245502143 x^ 2 + 2.298404288652356 x^ 3 )
147
+ julia> xs = 0 : 4 ; ys = @. exp (- xs) + sin (xs);
148
+
149
+ julia> fit (xs, ys)
150
+ Polynomial (1.0000000000000016 + 0.059334723072240664 * x + 0.39589720602859824 * x^ 2 - 0.2845598112184312 * x^ 3 + 0.03867830809692903 * x^ 4 )
151
+
152
+ julia> fit (ChebyshevT, xs, ys, deg= 2 )
153
+ ChebyshevT ([0.541280671210034 , - 0.8990834124779993 , - 0.4237852336242923 ])
166
154
```
167
155
168
156
Visual example:
169
157
170
- ![ newplot 42 ] ( https://user-images.githubusercontent.com/3156114/41799777-9ba00582-7627-11e8-94ef-15297ec8790e .png )
158
+ ![ fit example ] ( https://user-images.githubusercontent.com/14099459/70382587-9e055500-1902-11ea-8952-3f03ae08b7dc .png )
171
159
172
160
#### Other methods
173
161
174
162
Polynomial objects also have other methods:
175
163
176
- * 0-based indexing is used to extract the coefficients of $a_0 + a_1
177
- x + a_2 x^2 + ...$, coefficients may be changed using indexing
164
+ * 0-based indexing is used to extract the coefficients of ` [a0, a1, a2, ...] ` , coefficients may be changed using indexing
178
165
notation.
179
166
180
167
* ` coeffs ` : returns the entire coefficient vector
@@ -187,18 +174,16 @@ Polynomial objects also have other methods:
187
174
188
175
* ` conj ` : finds the conjugate of a polynomial over a complex fiel
189
176
190
- * ` truncate ` : set to 0 all small terms in a polynomial; ` chop ` chops off
191
- any small leading values that may arise due to floating point
192
- operations.
177
+ * ` truncate ` : set to 0 all small terms in a polynomial;
178
+ * ` chop ` chops off any small leading values that may arise due to floating point operations.
193
179
194
180
* ` gcd ` : greatest common divisor of two polynomials.
195
181
196
182
* ` Pade ` : Return the
197
- [ Pade approximant] ( https://en.wikipedia.org/wiki/Pad%C3%A9_approximant )
198
- of order ` m/n ` for a polynomial as a ` Pade ` object.
183
+ [ Pade approximant] ( https://en.wikipedia.org/wiki/Pad%C3%A9_approximant ) of order ` m/n ` for a polynomial as a ` Pade ` object.
199
184
200
185
201
- ## See also
186
+ ## Related Packages
202
187
203
188
* [ MultiPoly.jl] ( https://github.com/daviddelaat/MultiPoly.jl ) for sparse multivariate polynomials
204
189
@@ -207,3 +192,8 @@ Polynomial objects also have other methods:
207
192
* [ Nemo.jl] ( https://github.com/wbhart/Nemo.jl ) for generic polynomial rings, matrix spaces, fraction fields, residue rings, power series
208
193
209
194
* [ PolynomialRoots.jl] ( https://github.com/giordano/PolynomialRoots.jl ) for a fast complex polynomial root finder
195
+
196
+
197
+ ## Contributing
198
+
199
+ If you are interested in contributing, feel free to open an issue or pull request to get started.
0 commit comments