Skip to content

Commit 46d5df9

Browse files
authored
Merge pull request #61 from jverzani/pull-request/678a5803
Pull request/678a5803
2 parents ef324c9 + 678a580 commit 46d5df9

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

README.md

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,34 @@ Basic arithmetic, integration, differentiation, evaluation, and root finding ove
55
[![Build Status](https://travis-ci.org/Keno/Polynomials.jl.png?branch=master)](https://travis-ci.org/Keno/Polynomials.jl)
66

77
#### Poly{T<:Number}(a::Vector)
8+
89
Construct a polynomial from its coefficients, lowest order first.
10+
911
```julia
1012
julia> Poly([1,0,3,4])
1113
Poly(1 + 3x^2 + 4x^3)
1214
```
1315

1416
An optional variable parameter can be added.
17+
1518
```julia
1619
julia> Poly([1,2,3], :s)
1720
Poly(1 + 2s + 3s^2)
1821
```
1922

2023
#### poly(r::AbstractVector)
21-
Construct a polynomial from its roots. This is in contrast to the `Poly` constructor, which constructs a polynomial from its coefficients.
24+
25+
Construct a polynomial from its roots. This is in contrast to the
26+
`Poly` constructor, which constructs a polynomial from its
27+
coefficients.
28+
2229
```julia
2330
// Represents (x-1)*(x-2)*(x-3)
2431
julia> poly([1,2,3])
2532
Poly(-6 + 11x - 6x^2 + x^3)
2633
```
2734

28-
#### +, -, *, /, ==
35+
#### +, -, *, /, div, ==
2936

3037
The usual arithmetic operators are overloaded to work on polynomials, and combinations of polynomials and scalars.
3138
```julia
@@ -44,22 +51,27 @@ Poly(3 + 2x)
4451
julia> p - q
4552
Poly(2x + x^2)
4653

47-
julia> p*q
54+
julia> p * q
4855
Poly(1 + 2x - x^2 - 2x^3)
4956

50-
julia> q/2
57+
julia> q / 2
5158
Poly(0.5 - 0.5x^2)
59+
60+
julia> q ÷ p # `div`, also `rem` and `divrem`
61+
Poly(0.25 - 0.5x)
5262
```
5363

5464
Note that operations involving polynomials with different variables will error.
65+
5566
```julia
5667
julia> p = Poly([1, 2, 3], :x)
5768
julia> q = Poly([1, 2, 3], :s)
5869
julia> p + q
5970
ERROR: Polynomials must have same variable.
6071
```
6172

62-
To get the degree of the polynomial use `degree` method
73+
To get the degree of the polynomial use the `degree` method
74+
6375
```
6476
julia> degree(p)
6577
1
@@ -72,15 +84,29 @@ julia> degree(p-p)
7284
```
7385

7486
#### polyval(p::Poly, x::Number)
87+
7588
Evaluate the polynomial `p` at `x`.
7689

7790
```julia
78-
julia> polyval(Poly([1, 0, -1]), 0.1)
91+
julia> p = Poly([1, 0, -1])
92+
julia> polyval(p, 0.1)
7993
0.99
8094
```
8195

96+
A call method is also available:
97+
98+
```julia
99+
julia> p(0.1)
100+
0.99
101+
```
102+
103+
82104
#### polyint(p::Poly, k::Number=0)
83-
Integrate the polynomial `p` term by term, optionally adding constant term `k`. The order of the resulting polynomial is one higher than the order of `p`.
105+
106+
Integrate the polynomial `p` term by term, optionally adding constant
107+
term `k`. The order of the resulting polynomial is one higher than the
108+
order of `p`.
109+
84110
```julia
85111
julia> polyint(Poly([1, 0, -1]))
86112
Poly(x - 0.3333333333333333x^3)
@@ -90,14 +116,20 @@ Poly(2.0 + x - 0.3333333333333333x^3)
90116
```
91117

92118
#### polyder(p::Poly)
93-
Differentiate the polynomial `p` term by term. The order of the resulting polynomial is one lower than the order of `p`.
119+
120+
Differentiate the polynomial `p` term by term. The order of the
121+
resulting polynomial is one lower than the order of `p`.
122+
94123
```julia
95124
julia> polyder(Poly([1, 3, -1]))
96125
Poly(3 - 2x)
97126
```
98127

99128
#### roots(p::Poly)
100-
Return the roots (zeros) of `p`, with multiplicity. The number of roots returned is equal to the order of `p`. The returned roots may be real or complex.
129+
130+
Return the roots (zeros) of `p`, with multiplicity. The number of
131+
roots returned is equal to the order of `p`. By design, this is not type-stable,
132+
the returned roots may be real or complex.
101133

102134
```julia
103135
julia> roots(Poly([1, 0, -1]))
@@ -115,3 +147,50 @@ julia> roots(Poly([0, 0, 1]))
115147
0.0
116148
0.0
117149
```
150+
151+
#### Polyfit
152+
153+
* `polyfit`: fits a polynomial of minimal degree fitting the points
154+
specified by `x` and `y` using the least-squares fit.
155+
156+
```julia
157+
julia> xs = 1:4; ys = exp(xs); polyfit(xs, ys)
158+
Poly(-7.717211620141281 + 17.9146616149694x - 9.77757245502143x^2 + 2.298404288652356x^3)
159+
```
160+
161+
#### Other methods
162+
163+
Polynomial objects also have other methods:
164+
165+
* 0-based indexing is used to extract the coefficients of $a_0 + a_1
166+
x + a_2 x^2 + ...$, coefficients may be changed using indexing
167+
notation.
168+
169+
* `coeffs`: returns the entire coefficient vector
170+
171+
* `degree`: returns the polynomial degree, `length` is 1 plus the degree
172+
173+
* `variable`: returns the polynomial symbol as a degree 1 polynomial
174+
175+
* `norm`: find the `p`-norm of a polynomial
176+
177+
* `conj`: finds the conjugate of a polynomial over a complex fiel
178+
179+
* `truncate`: set to 0 all small terms in a polynomial; `chop` chops off
180+
any small leading values that may arise due to floating point
181+
operations.
182+
183+
* `gcd`: greatest common divisor of two polynomials.
184+
185+
* `Pade`: Return the
186+
[Pade approximant](https://en.wikipedia.org/wiki/Pad%C3%A9_approximant)
187+
of order `m/n` for a polynomial as a `Pade` object.
188+
189+
190+
## See also
191+
192+
* [MultiPoly.jl](https://github.com/daviddelaat/MultiPoly.jl) for sparse multivariate polynomials
193+
194+
* [Nemo.jl](https://github.com/wbhart/Nemo.jl) for generic polynomial rings, matrix spaces, fraction fields, residue rings, power series
195+
196+
* [PolynomialRoots.jl](https://github.com/giordano/PolynomialRoots.jl) for a fast complex polynomial root finder

src/Polynomials.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export Poly, poly
1111
export degree, coeffs, variable
1212
export polyval, polyint, polyder, roots, polyfit
1313
export Pade, padeval
14-
export truncate!
1514

1615
import Base: length, endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
1716
import Base: show, print, *, /, //, -, +, ==, divrem, div, rem, eltype, .*, .-, .+

0 commit comments

Comments
 (0)