Eigenvalues of a matrix with symbolic coefficients

20 views
Skip to first unread message

Oscar Benjamin

unread,
Dec 4, 2014, 11:57:59 AM12/4/14
to sy...@googlegroups.com
I'm having some problems getting sympy to compute eigenvalues of a
matrix with symbolic coefficients.

A trivial example is:

In [4]: M = Matrix([[y, 0], [0, z]])

In [5]: M
Out[5]:
⎡y 0⎤
⎢ ⎥
⎣0 z⎦

In [6]: M.eigenvals()
Out[6]:
⎧ __________ __________ ⎫
⎪ ╱ 2 ╱ 2 ⎪
⎨y z ╲╱ (y - z) y z ╲╱ (y - z) ⎬
⎪─ + ─ - ─────────────: 1, ─ + ─ + ─────────────: 1⎪
⎩2 2 2 2 2 2 ⎭

Clearly the eigenvalues should be y and z so something has gone wrong.

It seems that perhaps what happens is that the characteristic
polynomial is expanded and then not re-factored when finding
solutions:

In [7]: (M - x*eye(2))
Out[7]:
⎡-x + y 0 ⎤
⎢ ⎥
⎣ 0 -x + z⎦

In [8]: (M - x*eye(2)).det()
Out[8]:
2
x - x⋅y - x⋅z + y⋅z

In [9]: solve((M - x*eye(2)).det(), x)
Out[9]:
⎡ __________ __________⎤
⎢ ╱ 2 ╱ 2 ⎥
⎢y z ╲╱ (y - z) y z ╲╱ (y - z) ⎥
⎢─ + ─ - ─────────────, ─ + ─ + ─────────────⎥
⎣2 2 2 2 2 2 ⎦

In [10]: factor((M - x*eye(2)).det())
Out[10]: (x - y)⋅(x - z)

In [11]: solve(factor((M - x*eye(2)).det()), x)
Out[11]: [y, z]

Presumably det calls expand and I'm not sure why it would want to do
that. Also solve has clearly spotted that this is a second order
polynomial since we're using the quadratic formula but no attempt at
factorisation is made. I guess that without knowing knowing the sign
of y - z you can't know whether sqrt((y-z)**2) is y - z or z - y so
the quadratic formula cannot be simplified even though the original
polynomial could.

Is there some way to adjust this behaviour with flags etc.? Or is it
just a case where sympy has some room for improvement?

Oscar

Colin Macdonald

unread,
Dec 4, 2014, 12:44:52 PM12/4/14
to sy...@googlegroups.com
On 04/12/14 16:57, Oscar Benjamin wrote:
> I'm having some problems getting sympy to compute eigenvalues of a
> matrix with symbolic coefficients.
>
> A trivial example is:
>
> In [4]: M = Matrix([[y, 0], [0, z]])
>
> In [5]: M
> Out[5]:
> ⎡y 0⎤
> ⎢ ⎥
> ⎣0 z⎦
>
> In [6]: M.eigenvals()
> Out[6]:
> ⎧ __________ __________ ⎫
> ⎪ ╱ 2 ╱ 2 ⎪
> ⎨y z ╲╱ (y - z) y z ╲╱ (y - z) ⎬
> ⎪─ + ─ - ─────────────: 1, ─ + ─ + ─────────────: 1⎪
> ⎩2 2 2 2 2 2 ⎭

I think this has been improved in the dev version after 0.7.6. I
remember commenting on an old issue on this which was then recently
closed, but I cannot locate at the moment... test on git master?

Colin

signature.asc

Oscar Benjamin

unread,
Dec 4, 2014, 1:37:22 PM12/4/14
to sympy
On 4 December 2014 at 17:44, Colin Macdonald <macd...@maths.ox.ac.uk> wrote:
>> In [4]: M = Matrix([[y, 0], [0, z]])
>>
>> In [5]: M
>> Out[5]:
>> ⎡y 0⎤
>> ⎢ ⎥
>> ⎣0 z⎦
>>
>> In [6]: M.eigenvals()
>> Out[6]:
>> ⎧ __________ __________ ⎫
>> ⎪ ╱ 2 ╱ 2 ⎪
>> ⎨y z ╲╱ (y - z) y z ╲╱ (y - z) ⎬
>> ⎪─ + ─ - ─────────────: 1, ─ + ─ + ─────────────: 1⎪
>> ⎩2 2 2 2 2 2 ⎭
>
> I think this has been improved in the dev version after 0.7.6. I
> remember commenting on an old issue on this which was then recently
> closed, but I cannot locate at the moment... test on git master?

Yes, you're right. git master shows:

In [3]: Matrix([[y, 0], [0, z]]).eigenvals()
Out[3]: {y: 1, z: 1}

And also:

In [8]: (M - x*eye(2)).det()
Out[8]:
2
x - x⋅y - x⋅z + y⋅z

In [9]: solve((M - x*eye(2)).det(), x)
Out[9]: [y, z]

Thanks for that,
Oscar
Reply all
Reply to author
Forward
0 new messages