# Code to calculate characteristic polynomial?

**URL:** https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903
**Category:** General Usage
**Created:** [January 22, 2019, 12:19am UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903 "2019-01-22T00:19:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [January 22, 2019, 12:19am UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/1 "2019-01-22T00:19:05Z")

</div>

I’m referring to

`det(\lambda I - A)`

I’m not finding a function to calculate this (it’s charpoly in Matlab).

Happy to write my own, just want to make sure i’m not duplicating effort.

Thanks

p.s. well actually i’m probably not “happy” to write my own, i sure it’s non-trivial…

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 22, 2019, 2:11am UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/2 "2019-01-22T02:11:40Z")

</div>

Why do you need this? For numerical calculations, the coefficients of the characteristic polynomial are generally useless (because even a tiny error in the coefficients radically changes the roots).

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [January 22, 2019, 5:23am UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/3 "2019-01-22T05:23:25Z")

</div>

for Cayley Hamilton. i wanted to play with it as a method for matrix multiplication. Not as a serious method to calculate it, just for educational purposes.

Looks like this works.

```julia
julia> using LinearAlgebra

julia> using Polynomials

julia> a=[[1,3] [2,4]]
2×2 Array{Int64,2}:
 1 2
 3 4
julia> poly(eigvals(a))
Poly(-1.9999999999999998 - 5.0*x + 1.0*x^2)

```

That was easy.

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [November 21, 2020, 5:12pm UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/4 "2020-11-21T17:12:37Z")

</div>

Here is code that gives a more symbolic computation of the characteristic polynomial (with integer coefficients as you’d want for this matrix):

```julia
using AbstractAlgebra
R, x = ZZ["x"] # make "x" like a symbolic variable
A = [1 2; 3 4] # normal Julia matrix
M = matrix(R, A) # special matrix object
B = x*one(M) - M # think B = x*I - A
det(B) # x^2 - 5*x - 2 : characteristic polynomial of A

```

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [November 23, 2020, 12:36pm UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/5 "2020-11-23T12:36:59Z")

</div>

One can actually get the characteristic polynomial directly without building the matrix over the polynomial ring (this is also more efficient):

```julia
julia> using AbstractAlgebra

julia> A = [1 2; 3 4];

julia> B = matrix(ZZ, A);

julia> Zx, x = ZZ["x"];

julia> f = charpoly(Zx, B)
x^2 - 5*x - 2

julia> f(B)
[0 0]
[0 0]

```

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [November 23, 2020, 3:27pm UTC](https://discourse.julialang.org/t/code-to-calculate-characteristic-polynomial/19903/6 "2020-11-23T15:27:43Z")

</div>

Cool! Seems like something like this 1-line addition to the package would be useful?

```julia
AbstractAlgebra.charpoly(A::Matrix) = charpoly(ZZ["x"][1], matrix(ZZ,A))

```

Then `charpoly(A)` would work “as expected” out of the box 🙂  
(I realize that one might want a different letter than `x`…)
