Skip to content

Commit 06df8be

Browse files
committed
add some docs on methods
1 parent 3f4fddc commit 06df8be

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

README.md

Lines changed: 67 additions & 7 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,14 +51,18 @@ 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)
@@ -60,6 +71,7 @@ ERROR: Polynomials must have same variable.
6071
```
6172

6273
To get the degree of the polynomial use `degree` method
74+
6375
```
6476
julia> degree(p)
6577
1
@@ -72,6 +84,7 @@ julia> degree(p-p)
7284
```
7385

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

7790
```julia
@@ -80,7 +93,11 @@ julia> polyval(Poly([1, 0, -1]), 0.1)
8093
```
8194

8295
#### 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`.
96+
97+
Integrate the polynomial `p` term by term, optionally adding constant
98+
term `k`. The order of the resulting polynomial is one higher than the
99+
order of `p`.
100+
84101
```julia
85102
julia> polyint(Poly([1, 0, -1]))
86103
Poly(x - 0.3333333333333333x^3)
@@ -90,14 +107,20 @@ Poly(2.0 + x - 0.3333333333333333x^3)
90107
```
91108

92109
#### 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`.
110+
111+
Differentiate the polynomial `p` term by term. The order of the
112+
resulting polynomial is one lower than the order of `p`.
113+
94114
```julia
95115
julia> polyder(Poly([1, 3, -1]))
96116
Poly(3 - 2x)
97117
```
98118

99119
#### 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.
120+
121+
Return the roots (zeros) of `p`, with multiplicity. The number of
122+
roots returned is equal to the order of `p`. The returned roots may be
123+
real or complex.
101124

102125
```julia
103126
julia> roots(Poly([1, 0, -1]))
@@ -114,4 +137,41 @@ julia> roots(Poly([0, 0, 1]))
114137
2-element Array{Float64,1}:
115138
0.0
116139
0.0
140+
```
141+
142+
#### Polyfit
143+
144+
* `polyfit`: fits a polynomial of minimal degree fitting the points
145+
specified by `x` and `y` using least squares fit.
146+
117147
```
148+
julia> xs = 1:4; ys = exp(xs); polyfit(xs, ys)
149+
Poly(-7.717211620141281 + 17.9146616149694x - 9.77757245502143x^2 + 2.298404288652356x^3)
150+
```
151+
152+
#### Other methods
153+
154+
Polynomial objects also have other methods:
155+
156+
* 0-based indexing is used to extract the coefficients of $a_0 + a_1
157+
x + a_2 x^2 + ...$, coefficients may be changed using indexing
158+
notation.
159+
160+
* `coeffs`: returns the entire coefficient vector
161+
162+
* `degree`: returns the polynomial degree, `length` is 1 plus the degree
163+
164+
* `variable`: returns the polynomial symbol as a degree 1 polynomial
165+
166+
* `norm`: find the `p`-norm of a polynomial
167+
168+
* `truncate`: set to 0 small terms in a polynomial; `chop` chops off
169+
any small leading values that may arise due to floating point
170+
operations.
171+
172+
* `gcd`: greatest common divisor of two polynomials.
173+
174+
175+
* `Pade`: Return the
176+
[Pade approximant](https://en.wikipedia.org/wiki/Pad%C3%A9_approximant)
177+
of order `m/n` for a polynomial as a `Pade` object.

0 commit comments

Comments
 (0)