# [Numpy-discussion] linalg.eigh orders eigenvalues/eigenvectors differently than linalg.eig

Nils Wagner nwagner@iam.uni-stuttgart...
Mon Feb 19 07:19:20 CST 2007

```Johannes Loehnert wrote:
> On Monday 19 February 2007 12:06, Sven Schreiber wrote:
>
>> Zachary Pincus schrieb:
>>
>>> Hello all,
>>>
>>> It seems that the 'eigh' routine from numpy.linalg does not follow
>>> the same convention as numpy.linalg.eig in terms of the order of the
>>> returned eigenvalues. (And thus eigenvectors as well...)
>>>
>> I was told on this list that the ordering should not be relied upon, and
>> that it might change in the future. So it seems that user code should
>> explicitly re-order the eigenvalues (and corresponding eigenvectors,
>> probably using argsort and fancy indexing -- if those are the right terms).
>>
>
> Indeed. eig and eigh are wrappers for lapack functions, so the result is
> whatever those give back. Do not rely on a particular order of eigenvalues,
> sort yourself.
>
> Short example for convenience:
> #---------
> eigvals, eigvecs = eig(some_matrix)
> ind = argsort(eigvals)
> eigvals = eigvals[ind]
> eigvecs = eigvecs[:, ind]   # second axis !!
> # etc.
> #------------
>
> Johannes
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

eigh is based on *heev  (* placeholder for s,d,c,z)
http://www.netlib.org/lapack/complex16/zheev.f

*  W       (output) DOUBLE PRECISION array, dimension (N)
*          If INFO = 0, the eigenvalues in ascending order.

Apparently the eigenvalues are in ascending order for eigh.

Hence, It would be nice if scipy could offer more wrappers for matrices
with certain properties, e.g.

http://www.netlib.org/lapack/double/dsygv.f
http://www.netlib.org/lapack/double/dsyevr.f
http://www.netlib.org/lapack/double/dsygvx.f
http://www.netlib.org/lapack/double/dstevr.f

Nils

Also LAPACK3.1 has many improved eigensolvers. Please try suzuki.py with

LAPACK3.0

>>> w_e
array([-200.        +0.j        , -100.        +0.j        ,
700.00307816+0.00103379j,  700.00307816-0.00103379j,
699.9993562 +0.00318262j,  699.9993562 -0.00318262j,
699.99756564+0.00214883j,  699.99756564-0.00214883j])

versus

LAPACK3.1.
>>> w_e
array([-200.        +0.j        , -100.        +0.j        ,
699.99891191+0.00188463j,  699.99891191-0.00188463j,
700.00217619+0.j        ,  700.00294523+0.j        ,
699.99852738+0.0025507j ,  699.99852738-0.0025507j ])

-------------- next part --------------
A non-text attachment was scrubbed...
Name: suzuki.py
Type: text/x-python
Size: 890 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070219/eca2c2d7/attachment-0001.py
```