How to use eig with the nobalance option as in MATLAB?
Asked Answered
C

2

6

In MATLAB I can issue the command:

[X,L] = eig(A,'nobalance'); 

In order to compute the eigenvalues without the balance option.

What is the equivalent command in NumPy? When I run the NumPy version of eig, it does not produce the same result as the MATLAB result with nobalance turned on.

Colly answered 17/12, 2013 at 3:42 Comment(1)
Looks like there may not be an option. There has been an open ticket asking for such a capability for over a year. You might chime in to add your support for that and provide a use case, etc.Lens
P
2

NumPy can't currently do this. As horchler said, there has been an open ticket open for this for a while now. It is, however, possible to do it using external libraries. Here I write up how to do it using the Python bindings to the NAG library

http://www.walkingrandomly.com/?p=5303

It should be possible to do something similar using any interface to LAPACK such as the Intel MKL etc.

Pettaway answered 17/12, 2013 at 16:17 Comment(5)
That sounds like a good alternative, could you please check your URL though? It does not appear to be working.Colly
Hi, so I installed everything that seemed to be necessary for your example to work but I am getting errors when I run the example script. This is what I get: OSError: /opt/NAG/cll6i23dcl/lib/libnagc_nag.so: undefined symbol: __svml_sin2Colly
The tuicool website is stealing my content as far as I can tell. The walkingrandomly URL works for me. I used Windows in my example..will try it on Linux later todayPettaway
I contacted NAG support for that error. Here's the reply... A quick fix is to set the environment variable LD_PRELOAD=/opt/NAG/cll6i23dcl/rtl/libsvml.so:/opt/NAG/cll6i23dcl/rtl/libimf.so Apparently, this is because the bindings are not ‘officially’ supported with CLL6I23DCL yet but they may change that in the futurePettaway
I got it to work, thanks for your help. The only thing is, the output from your example is not exactly the same as Matlab's. If you normalize each of your eigenvectors j by max(abs(v[:,j])) then you get the correct answer. However, it does not work for all cases, in my case, after normalizing I get the correct magnitudes for each vector but the sign is not necessarily the same as Matlab's vectors. In any case I am accepting this as an answer. I think it is close enough and for my problem, I can make it work. Thanks again!Colly
C
3

You can also consider installing GNU Octave and embed it in Python using oct2py. For example, to determine the eigenvalue of matrix A without balancing,

from oct2py import octave
...
[X,L] = octave.eig(A)

The function eig in Octave does not perform balancing of matrix A.

If you want to balance the matrix A, you can go ahead and write:

from oct2py import octave
...
A = octave.balance(A)
[X,L] = octave.eig(A)

oct2py can be downloaded from this website: https://pypi.python.org/pypi/oct2py

Before you install oct2py, you need to make sure SciPy and GNU Octave have been already installed. Good luck!

Chore answered 13/11, 2015 at 12:35 Comment(2)
The other way works (@ WalkingRandomly). But this is very interesting. It has been nearly two years but I am still using the same code so I will give it a try and report back. Thanks!Colly
I was having the same problem as yours that I would like to have my matrix not balanced in the eigenvalue calculation using Python. I saw @ WalkingRandomly 's post, but my university does not have the commercial NAG library. Then, I wondered if free software, such as GNU Octave, can do so, and it works very well. Thanks for your commentsChore
P
2

NumPy can't currently do this. As horchler said, there has been an open ticket open for this for a while now. It is, however, possible to do it using external libraries. Here I write up how to do it using the Python bindings to the NAG library

http://www.walkingrandomly.com/?p=5303

It should be possible to do something similar using any interface to LAPACK such as the Intel MKL etc.

Pettaway answered 17/12, 2013 at 16:17 Comment(5)
That sounds like a good alternative, could you please check your URL though? It does not appear to be working.Colly
Hi, so I installed everything that seemed to be necessary for your example to work but I am getting errors when I run the example script. This is what I get: OSError: /opt/NAG/cll6i23dcl/lib/libnagc_nag.so: undefined symbol: __svml_sin2Colly
The tuicool website is stealing my content as far as I can tell. The walkingrandomly URL works for me. I used Windows in my example..will try it on Linux later todayPettaway
I contacted NAG support for that error. Here's the reply... A quick fix is to set the environment variable LD_PRELOAD=/opt/NAG/cll6i23dcl/rtl/libsvml.so:/opt/NAG/cll6i23dcl/rtl/libimf.so Apparently, this is because the bindings are not ‘officially’ supported with CLL6I23DCL yet but they may change that in the futurePettaway
I got it to work, thanks for your help. The only thing is, the output from your example is not exactly the same as Matlab's. If you normalize each of your eigenvectors j by max(abs(v[:,j])) then you get the correct answer. However, it does not work for all cases, in my case, after normalizing I get the correct magnitudes for each vector but the sign is not necessarily the same as Matlab's vectors. In any case I am accepting this as an answer. I think it is close enough and for my problem, I can make it work. Thanks again!Colly

© 2022 - 2024 — McMap. All rights reserved.