Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab VS Python - eig(A,B) VS sc.linalg.eig(A,B)

I have the following matrices sigma and sigmad:

sigma:

    1.9958   0.7250
    0.7250   1.3167

sigmad:

    4.8889   1.1944
    1.1944   4.2361

If I try to solve the generalized eigenvalue problem in python I obtain:

    d,V = sc.linalg.eig(matrix(sigmad),matrix(sigma))

V:

    -1     -0.5614
    -0.4352    1

If I try to solve the g. e. problem in matlab I obtain:

    [V,d]=eig(sigmad,sigma)

V:

    -0.5897    -0.5278
    -0.2564    0.9400

But the d's do coincide.

like image 942
JEquihua Avatar asked Jul 27 '12 16:07

JEquihua


People also ask

Which is better between MATLAB and Python?

Python is a high-level language, it is more user friendly, more readable and more portable. MATLAB is a low-level language and not good at some algorithms such as bioinformatics. MATLAB has the function of the matrix, and Python can use NumPy, and the library can achieve similar results.

What is Linalg Eig?

eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array. Syntax: numpy.linalg.eig() Parameter: An square array.

What is the difference between EIG and eigs in MATLAB?

The eig() function calculates wrong eigenvalues with the default algorithm (Cholesky) and the right eigenvalues with the QZ-algorithm (Due to the condition?) The eigs() function calculates the right eigenvalues if the number of requested eigenvalues is and the wrong eigenvalues if.

Is MATLAB or Python better for data analysis?

This section has an easy answer: Python is the winner over Matlab. Python has tons of libraries and packages for both old school and new school machine learning models. Plus, Python is the most widely used language for modern machine learning research in industry and academia.


1 Answers

Any (nonzero) scalar multiple of an eigenvector will also be an eigenvector; only the direction is meaningful, not the overall normalization. Different routines use different conventions -- often you'll see the magnitude set to 1, or the maximum value set to 1 or -1 -- and some routines don't even bother being internally consistent for performance reasons. Your two different results are multiples of each other:

In [227]: sc = array([[-1., -0.5614], [-0.4352,  1.    ]])

In [228]: ml = array([[-.5897, -0.5278], [-0.2564, 0.94]])

In [229]: sc/ml
Out[229]: 
array([[ 1.69577751,  1.06366048],
       [ 1.69734789,  1.06382979]])

and so they're actually the same eigenvectors. Think of the matrix as an operator which changes a vector: the eigenvectors are the special directions where a vector pointing that way won't be twisted by the matrix, and the eigenvalues are the factors measuring how much the matrix expands or contracts the vector.

like image 100
DSM Avatar answered Sep 23 '22 20:09

DSM