Array of matrices - Inverse and dot
Hello, I would like to operate in an easy and efficient way (without python loop) with arrays of matrices. Suppose a and b are some arrays of N1*N2 matrices of size 3*3, I would like to calculate inv_a and dot_ab, which would be arrays of N1*N2 (3*3)-matrices, such as : inv_a[i, j] = np.linalg.inv(a[i, j]) dot_ab[i, j] = np.dot(a[i, j], b[i, j]) (where a and b could be : N1 = 5 N2 = 6 a = np.random((N1, N2, 3, 3) b = np.random((N1, N2, 3, 3) ). Thank you, (Sorry to ask the list : I think it is quite a basic stuff, but searching for "array of matrices" on google didn't help me so much.) Jean-Baptiste Rudant
On Mon, Jan 26, 2009 at 11:59 PM, Jean-Baptiste Rudant <boogaloojb@yahoo.fr> wrote:
Hello, I would like to operate in an easy and efficient way (without python loop) with arrays of matrices. Suppose a and b are some arrays of N1*N2 matrices of size 3*3, I would like to calculate inv_a and dot_ab, which would be arrays of N1*N2 (3*3)-matrices, such as :
If you only care about 3*3 matrices, I would consider using "developed" formula of a 3x3 matrix inverse coded in numpy. numpy.inv will be quite slow for such small matrices anyway (all the machinery to call the underlying C code being almost certainly the bottleneck). David
Thank you, but my example was bad. I have to deal with matrices which can be 100*100. ________________________________ De : David Cournapeau <cournape@gmail.com> À : Discussion of Numerical Python <numpy-discussion@scipy.org> Envoyé le : Lundi, 26 Janvier 2009, 16h18mn 33s Objet : Re: [Numpy-discussion] Array of matrices - Inverse and dot On Mon, Jan 26, 2009 at 11:59 PM, Jean-Baptiste Rudant <boogaloojb@yahoo.fr> wrote:
Hello, I would like to operate in an easy and efficient way (without python loop) with arrays of matrices. Suppose a and b are some arrays of N1*N2 matrices of size 3*3, I would like to calculate inv_a and dot_ab, which would be arrays of N1*N2 (3*3)-matrices, such as :
If you only care about 3*3 matrices, I would consider using "developed" formula of a 3x3 matrix inverse coded in numpy. numpy.inv will be quite slow for such small matrices anyway (all the machinery to call the underlying C code being almost certainly the bottleneck). David _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Mon, Jan 26, 2009 at 10:25 AM, Jean-Baptiste Rudant <boogaloojb@yahoo.fr> wrote:
Thank you, but my example was bad. I have to deal with matrices which can be 100*100. ________________________________ De : David Cournapeau <cournape@gmail.com> À : Discussion of Numerical Python <numpy-discussion@scipy.org> Envoyé le : Lundi, 26 Janvier 2009, 16h18mn 33s Objet : Re: [Numpy-discussion] Array of matrices - Inverse and dot
On Mon, Jan 26, 2009 at 11:59 PM, Jean-Baptiste Rudant <boogaloojb@yahoo.fr> wrote:
Hello, I would like to operate in an easy and efficient way (without python loop) with arrays of matrices. Suppose a and b are some arrays of N1*N2 matrices of size 3*3, I would like to calculate inv_a and dot_ab, which would be arrays of N1*N2 (3*3)-matrices, such as :
If you only care about 3*3 matrices, I would consider using "developed" formula of a 3x3 matrix inverse coded in numpy. numpy.inv will be quite slow for such small matrices anyway (all the machinery to call the underlying C code being almost certainly the bottleneck).
David
There is numpy.tensordot and numpy.tensorinv which looks like it might be doing what you want, but I never used it. Josef
Jean-Baptiste Rudant wrote:
I would like to operate in an easy and efficient way (without python loop) with arrays of matrices.
Suppose a and b are some arrays of N1*N2 matrices of size 3*3, I would like to calculate inv_a and dot_ab, which would be arrays of N1*N2 (3*3)-matrices, such as :
inv_a[i, j] = np.linalg.inv(a[i, j]) dot_ab[i, j] = np.dot(a[i, j], b[i, j])
(where a and b could be : N1 = 5 N2 = 6 a = np.random((N1, N2, 3, 3) b = np.random((N1, N2, 3, 3) ).
Here's a one-liner: numpy.array(map(numpy.dot, a, b)) that works for matrix multiply if a, b are (n, 3, 3). Could do the same for linalg.inv. This comes up a lot in OFDM MIMO systems so I wrote C++ code for complex matrix multiply (arbitrary size), 2x2 complex inverse and 2x2 complex matrix singular values, and then swigged it. I know a colleague at work has extended this work to arbitrary size inverse. I wish I could share the code but it is something developed for my company which has fairly strict policies about posting these things (not worth the red-tape)... I vote "+1" for such features in Numpy. I haven't looked too much "under the hood" of numpy so I am not sure how you would do it or how hard it would be. Regards, Tom K. -- View this message in context: http://www.nabble.com/Array-of-matrices---Inverse-and-dot-tp21666949p2167062... Sent from the Numpy-discussion mailing list archive at Nabble.com.
participants (4)
-
David Cournapeau
-
Jean-Baptiste Rudant
-
josef.pktd@gmail.com
-
Tom K.