On 07/11/2011 11:01 PM, Daniel Wheeler wrote:
> Hi, I am trying to find the eigenvalues and eigenvectors as well as
> the inverse for a large number of small matrices. The matrix size
> (MxM) will typically range from 2x2 to 8x8 at most. The number of
> matrices (N) can be from 100 up to a million or more. My current
> solution is to define "eig" and "inv" to be,
>
> def inv(A):
>      """
>      Inverts N MxM matrices, A.shape = (M, M, N), inv(A).shape = (M, M, N).
>      """
>      return np.array(map(np.linalg.inv, A.transpose(2, 0, 1))).transpose(1, 
> 2, 0)
>
> def eig(A):
>      """
>      Calculate the eigenvalues and eigenvectors of N MxM matrices,
> A.shape = (M, M, N), eig(A)[0].shape = (M, N), eig(A)[1].shape = (M,
> M, N)
>      """
>      tmp = zip(*map(np.linalg.eig, A.transpose(2, 0, 1)))
>      return (np.array(tmp[0]).swapaxes(0,1), 
> np.array(tmp[1]).transpose(1,2,0))
>
> The above uses "map" to fake a vector solution, but this is heinously
> slow. Are there any better ways to do this without resorting to cython
> or weave (would it even be faster (or possible) to use "np.linalg.eig"
> and "np.linalg.inv" within cython)? I could write specialized versions

If you want to go the Cython route, here's a start:

http://www.vetta.org/2009/09/tokyo-a-cython-blas-wrapper-for-fast-matrix-math/

Dag Sverre
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to