# [SciPy-user] Proper Use of NumPy's eig()

Bryan Van de Ven bryanv@enthought....
Tue Feb 27 15:11:13 CST 2007

```If by principal eigenvector you mean the eigenvector corresponding to the
largest-magnitude eigenvalue, then you can try something like:

a=array([[1,0],[0,-3]])
evals, evects = eig(a)
peig = evects[where(abs(evals)==max(abs(evals)))]

Rich Shepard wrote:
> On Tue, 27 Feb 2007, David Warde-Farley wrote:
>
>> If you're really keen on having short names for things you could use
>> 	from scipy.linalg import eig
>> or
>> 	import scipy.linalg as L
>> in the latter case you'd use L.eig().  I personally don't like cluttering up
>> the namespace too much but I think that's my own obsessive streak. :)
>
> David,
>
>    I've no preference one way or the other.
>
>    However, I don't see the answer I expected. My expectation is that all
> values would be in the range [0.00-1.00], but they're not.
>
>    When I print eigE I see:
>
> (array([  8.88174744e+00+0.j        ,   3.54286503e-01+2.48721395j,
>           3.54286503e-01-2.48721395j,  -3.11162331e-01+1.00980412j,
>          -3.11162331e-01-1.00980412j,  -2.79755841e-01+0.46954619j,
>          -2.79755841e-01-0.46954619j,  -4.08484096e-01+0.j        ]),
> array([[  6.24249034e-01 +0.00000000e+00j,
>            4.46875199e-01 -2.63555328e-01j,
>            4.46875199e-01 +2.63555328e-01j,
>           -1.42390973e-01 -5.61667808e-01j,
>           -1.42390973e-01 +5.61667808e-01j,
>           -6.46262210e-01 +0.00000000e+00j,
>           -6.46262210e-01 -0.00000000e+00j,
>            3.21082397e-01 +0.00000000e+00j],
>         [  5.11335982e-01 +0.00000000e+00j,
>            5.74875531e-01 +0.00000000e+00j,
>            5.74875531e-01 -0.00000000e+00j,
>            6.00826858e-01 +0.00000000e+00j,
>            6.00826858e-01 -0.00000000e+00j,
>            3.96191087e-01 -2.86601017e-01j,
>            3.96191087e-01 +2.86601017e-01j,
>           -1.11778945e-01 +0.00000000e+00j],
>         [  3.67333773e-01 +0.00000000e+00j,
>            2.17622233e-01 +2.64832461e-01j,
>            2.17622233e-01 -2.64832461e-01j,
>            5.23950814e-02 +3.20941855e-01j,
>            5.23950814e-02 -3.20941855e-01j,
>            9.79467401e-02 +4.26775994e-01j,
>            9.79467401e-02 -4.26775994e-01j,
>            3.25049690e-01 +0.00000000e+00j],
>         [  3.01189122e-01 +0.00000000e+00j,
>           -1.99915129e-03 +3.51819621e-01j,
>           -1.99915129e-03 -3.51819621e-01j,
>           -3.31543315e-01 +4.14230032e-02j,
>           -3.31543315e-01 -4.14230032e-02j,
>           -2.40393098e-01 -1.72385172e-01j,
>           -2.40393098e-01 +1.72385172e-01j,
>           -5.55853156e-01 +0.00000000e+00j],
>         [  2.43449050e-01 +0.00000000e+00j,
>           -2.39280748e-01 +1.81665666e-01j,
>           -2.39280748e-01 -1.81665666e-01j,
>            7.36841917e-02 -2.29964404e-01j,
>            7.36841917e-02 +2.29964404e-01j,
>            1.64067922e-01 -9.40947682e-02j,
>            1.64067922e-01 +9.40947682e-02j,
>            5.32558922e-01 +0.00000000e+00j],
>         [  1.82948476e-01 +0.00000000e+00j,
>           -1.73636742e-01 -5.63298529e-02j,
>           -1.73636742e-01 +5.63298529e-02j,
>            1.02155185e-01 +6.19835455e-02j,
>            1.02155185e-01 -6.19835455e-02j,
>           -5.20559632e-02 +1.19076414e-01j,
>           -5.20559632e-02 -1.19076414e-01j,
>           -3.31196888e-01 +0.00000000e+00j],
>         [  1.43655139e-01 +0.00000000e+00j,
>           -8.17472085e-02 -1.40491017e-01j,
>           -8.17472085e-02 +1.40491017e-01j,
>           -3.15727963e-03 +7.94710216e-02j,
>           -3.15727963e-03 -7.94710216e-02j,
>            4.00006513e-03 -8.97730556e-02j,
>            4.00006513e-03 +8.97730556e-02j,
>            2.50876482e-01 +0.00000000e+00j],
>         [  9.91225725e-02 +0.00000000e+00j,
>            3.30357162e-02 -8.93890809e-02j,
>            3.30357162e-02 +8.93890809e-02j,
>           -8.33994799e-02 +1.92423688e-03j,
>           -8.33994799e-02 -1.92423688e-03j,
>            4.33406761e-02 +3.72302707e-02j,
>            4.33406761e-02 -3.72302707e-02j,
>           -1.16327714e-01 +0.00000000e+00j]]))
>
>    Since eig(E) "Return[s] all solutions (lamda, x) to the equation Ax =
> lamda x. The first element of the return tuple contains all the eigenvalues.
> The second element of the return tuple contains the eigenvectors in the
> columns (x[:,i] is the ith eigenvector)."
>
>    I can't interpret the above. If the first tuple has all the Eigenvalues,
> how do I extract the principal Eigenvector from the rest? When I did this
> manually a couple of years ago, I used Octave to calculate the principal
> Eigenvector and the answer was easy for me to see.
>
> Rich
>

```