Skip to content

Commit 302d0af

Browse files
committed
Added lead and update README
1 parent a632f16 commit 302d0af

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
This module defines the `SimplePolynomial` type. These are polynomials
13-
with exact coefficients (integers, Gaussian integers, or Gaussian rationals).
13+
with exact coefficients (integers, Gaussian integers, Gaussian rationals,
14+
or `Mod`s).
1415

1516

1617
## Construction
@@ -33,6 +34,14 @@ julia> SimplePolynomial(3,-2,0,1) == SimplePolynomial(3,-2,0,1,0,0)
3334
true
3435
```
3536

37+
These polynomials support `Mod` coefficients:
38+
```
39+
julia> using Mods
40+
41+
julia> p = SimplePolynomial( Mod{7}.(1:5) )
42+
Mod{7}(1) + Mod{7}(2)*x + Mod{7}(3)*x^2 + Mod{7}(4)*x^3 + Mod{7}(5)*x^4
43+
```
44+
3645
#### Using `getx`
3746

3847
The function `getx()` is a short cut that returns
@@ -63,6 +72,7 @@ assign to a coefficient; that is, `p[k]=c` does not work.
6372
* `monic(p)` returns a new `SimplePolynomial` formed by dividing
6473
all the coefficients by the leading term.
6574
* `eltype(p)` returns the data type of the coefficients.
75+
* `lead(p)` returns the coefficient of the highest power of `x` in `p`.
6676

6777

6878
## Arithmetic

src/SimplePolynomials.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ using Mods
44
import Base: getindex, (==), show, zero, one, eltype, adjoint
55
import Polynomials: degree, Polynomial, coeffs, roots, derivative, integrate
66

7-
export SimplePolynomial, degree, coeffs, getx, Polynomial, roots, derivative, integrate
7+
export SimplePolynomial, degree, coeffs, getx, Polynomial, roots
8+
export derivative, integrate, lead
89

910
# IntegerX is any sort of real or Gaussian integer
1011
IntegerX = Union{S,Complex{S},Mod} where S<:Integer
@@ -45,6 +46,13 @@ SimplePolynomial() = SimplePolynomial([0])
4546
coeffs(p::SimplePolynomial) = copy(p.data)
4647
eltype(p::SimplePolynomial) = eltype(p.data)
4748

49+
"""
50+
`lead(p::SimplePolynomial)` returns the coefficient of the highest
51+
power of `x` in the polynomial `p`. This is nonzero unless `p` is
52+
the zero polynomial.
53+
"""
54+
lead(p::SimplePolynomial) = p.data[end]
55+
4856

4957
# conversion to/from Polynomial type
5058
SimplePolynomial(P::Polynomial) = SimplePolynomial(P.coeffs)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ a = SimplePolynomial(1,1,1)
44
@test a(10) == 111
55
@test a[0] == 1
66
@test a[9] == 0
7+
@test lead(a) == 1
78

89

910
@test a+a == 2a

0 commit comments

Comments
 (0)