Skip to content

Commit 4e10427

Browse files
committed
Added char_poly
1 parent 7ccc360 commit 4e10427

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
name = "LinearAlgebraX"
22
uuid = "9b3f67b0-2d00-526e-9884-9e4938f8fb88"
3-
version = "0.0.1"
3+
version = "0.0.2"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
7+
SimplePolynomials = "cc47b68c-3164-5771-a705-2bc0097375a0"
8+
SimpleRationalFunctions = "1a520dc8-4f4e-523b-a9bd-3b3d46c5454b"
79

810
[compat]
911
julia = "1"
12+
SimplePolynomials = "0"
13+
SimpleRationalFunctions = "0"
1014

1115
[extras]
1216
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ For exact types (such as `Int`s) these functions give exact results.
2929
* `invx` -- exact inverse
3030
* `rrefx` -- row reduced echelon form
3131
* `eye` -- lovingly restored
32+
* `char_poly` -- characteristic polynomial
3233

3334
## Examples
3435

@@ -133,8 +134,31 @@ julia> A*B
133134
Mod{11}(0) Mod{11}(0) Mod{11}(0) Mod{11}(0) Mod{11}(1)
134135
```
135136

137+
#### Characteristic polynomial
138+
139+
```
140+
julia> using SimplePolynomials, LinearAlgebra
141+
142+
julia> x = getx()
143+
x
144+
145+
julia> A = triu(ones(Int,5,5))
146+
5×5 Array{Int64,2}:
147+
1 1 1 1 1
148+
0 1 1 1 1
149+
0 0 1 1 1
150+
0 0 0 1 1
151+
0 0 0 0 1
152+
153+
julia> char_poly(A)
154+
-1 + 5*x - 10*x^2 + 10*x^3 - 5*x^4 + x^5
155+
156+
julia> ans == (x-1)^5
157+
true
158+
```
159+
136160
#### Row reduced echelon form
137-
161+
138162
```
139163
julia> A = rand(Int,4,6) .% 10
140164
4×6 Array{Int64,2}:
@@ -164,3 +188,7 @@ julia> rrefx(A)
164188
0//1 0//1 1//1 1//1 0//1 551//650 512//325
165189
0//1 0//1 0//1 0//1 1//1 -379//325 204//325
166190
```
191+
192+
## To do
193+
194+
Still having some issues with integer overflow.

src/LinearAlgebraX.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module LinearAlgebraX
2-
using LinearAlgebra
2+
using LinearAlgebra, SimplePolynomials, SimpleRationalFunctions
33

44

55
# IntegerX is any sort of real or Gaussian integer
@@ -8,6 +8,8 @@ IntegerX = Union{S,Complex{S}} where S<:Integer
88
# RationalX is a Rational or Complex Rational based on integers
99
RationalX = Union{Rational{S},Complex{Rational{S}}} where S<:Integer
1010

11+
TypeX = Union{IntegerX, RationalX}
12+
1113

1214
function _recip(x::T) where T <: IntegerX
1315
return 1//x
@@ -22,7 +24,7 @@ include("rrefx.jl")
2224
include("invx.jl")
2325
include("rankx.jl")
2426
include("nullspacex.jl")
25-
27+
include("char_poly.jl")
2628

2729

2830
end # module

src/char_poly.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export char_poly
2+
3+
function char_poly(A::Matrix{T}) where T<:TypeX
4+
r,c = size(A)
5+
@assert r==c "Matrix must be square"
6+
7+
xI = zeros(SimplePolynomial,r,r)
8+
x = getx()
9+
for i=1:r
10+
xI[i,i] = x
11+
end
12+
13+
f = detx(xI-A)
14+
p = integerize(numerator(f))
15+
end

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ H = hilbert(12)
1515
A = ones(Int,3,5)
1616
N = nullspacex(A)
1717
@test all(0 .== A*N)
18+
19+
20+
using SimplePolynomials
21+
22+
A = triu(ones(Int,5,5))
23+
p = char_poly(A)
24+
x = getx()
25+
@test p == (x-1)^5
26+
27+
A = ones(Int,5,5)
28+
p = char_poly(A)
29+
@test p == x^4 * (x-5)

0 commit comments

Comments
 (0)