Re: [Numpy-discussion] array of matrices
On Wed, Apr 01, 2009 at 01:40:54AM +, Hans-Andreas Engel wrote: By the way, matrix multiplication is one of the testcases for the generalized ufuncs in numpy 1.3 -- this makes playing around with it easy: In [1]: N = 10; a = randn(N, 4, 4); b = randn(N, 4, 4) In [2]: import numpy.core.umath_tests In [3]: (numpy.core.umath_tests.matrix_multiply(a, b) == [dot(ai, bi) for (ai, bi) in zip(a, b)]).all() Out[3]: True Hey Hans-Andreas, I am jumping on your message to stress that generalized ufunc realy need a bit more documentation. I have been trying to push a few collegues to use them, but I am getting not traction, because they are unsure of what generalized ufuncs do. I wasn't aware of the example you mentioned. It certainly is useful. Maybe it would be useful to add it as an example to the generalized-ufunc documnetation embryo (http://docs.scipy.org/numpy/docs/numpy-docs/reference/generalized_ufuncs.rst), with a discussion of the example. If you, or anyone else who knows generalized ufuncs, register on the above webpage, I can give rights to edit the page, so that you can improve the docs. Cheers, Gaël ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
Le Friday 27 March 2009 23:38:25 Bryan Cole, vous avez écrit : I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? I think dot will work, though you'll need to work a little bit to get the answer: import numpy as np a = np.array([[1,2], [3,4]], np.float) aa = np.array([a,a+1,a+2]) bb = np.array((a*5, a*6, a*7, a*8)) np.dot(aa, bb).shape (3, 2, 4, 2) for i, a_ in enumerate(aa): ... for j, b_ in enumerate(bb): ... print (np.dot(a_, b_) == np.dot(aa, bb)[i,:,j,:]).all() ... True True True True True True True True True True True True -- Alexandre Fayolle LOGILAB, Paris (France) Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations Développement logiciel sur mesure: http://www.logilab.fr/services Informatique scientifique: http://www.logilab.fr/science ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
I think dot will work, though you'll need to work a little bit to get the answer: import numpy as np a = np.array([[1,2], [3,4]], np.float) aa = np.array([a,a+1,a+2]) bb = np.array((a*5, a*6, a*7, a*8)) np.dot(aa, bb).shape (3, 2, 4, 2) for i, a_ in enumerate(aa): ... for j, b_ in enumerate(bb): ... print (np.dot(a_, b_) == np.dot(aa, bb)[i,:,j,:]).all() ... True Thanks. Your comment has helped me understand what dot() does for ndims2 . It's a pity this will be too memory inefficient for large N. Bryan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
Robert Kern robert.kern at gmail.com writes: On Sat, Mar 28, 2009 at 23:15, Anne Archibald peridot.faceted at gmail.com wrote: 2009/3/28 Geoffrey Irving irving at naml.us: On Sat, Mar 28, 2009 at 12:47 AM, Robert Kern robert.kern at gmail.com wrote: 2009/3/27 Charles R Harris charlesr.harris at gmail.com: On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern robert.kern at gmail.com wrote: On Fri, Mar 27, 2009 at 17:38, Bryan Cole bryan at cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (...) It'd be great if this operation existed as a primitive. (...) The infrastructure to support such generalized ufuncs has been added to numpy, but as far as I know no functions yet make use of it. I don't think there is a way to do it in general with dot(). Some cases are ambiguous. I think you will need separate matrix-matrix, matrix-vector, and vector-vector gufuncs, to coin a term. By the way, matrix multiplication is one of the testcases for the generalized ufuncs in numpy 1.3 -- this makes playing around with it easy: In [1]: N = 10; a = randn(N, 4, 4); b = randn(N, 4, 4) In [2]: import numpy.core.umath_tests In [3]: (numpy.core.umath_tests.matrix_multiply(a, b) == [dot(ai, bi) for (ai, bi) in zip(a, b)]).all() Out[3]: True Best, Hansres ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
2009/3/27 Charles R Harris charlesr.har...@gmail.com: On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern robert.k...@gmail.com wrote: On Fri, Mar 27, 2009 at 17:38, Bryan Cole br...@cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? dot(a,b) was specifically designed for this use case. I think maybe he wants to treat them as stacked matrices. Oh, right. Sorry. dot(a, b) works when a is (N, 4, 4) and b is just (4, 4). Never mind. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
2009/3/28 Geoffrey Irving irv...@naml.us: On Sat, Mar 28, 2009 at 12:47 AM, Robert Kern robert.k...@gmail.com wrote: 2009/3/27 Charles R Harris charlesr.har...@gmail.com: On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern robert.k...@gmail.com wrote: On Fri, Mar 27, 2009 at 17:38, Bryan Cole br...@cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? dot(a,b) was specifically designed for this use case. I think maybe he wants to treat them as stacked matrices. Oh, right. Sorry. dot(a, b) works when a is (N, 4, 4) and b is just (4, 4). Never mind. It'd be great if this operation existed as a primitive. What do you think would be the best way in which to add it? One option would be to add a keyword argument to dot giving a set of axes to map over. E.g., dot(a, b, map=0) = array([dot(u,v) for u,v in zip(a,b)]) # but in C map isn't a very good name for the argument, though. I think the right long-term solution is to make dot (and some other linear algebra functions) into generalized ufuncs, so that when you dot two multidimensional objects together, they are treated as arrays of two-dimensional arrays, broadcasting is done on all but the last two dimensions, and then the linear algebra is applied elementwise. This covers basically all stacked matrices uses in a very general way, but would require some redesigning of the linear algebra system - for example, dot() currently works on both two- and one-dimensional arrays, which can't work in such a setting. The infrastructure to support such generalized ufuncs has been added to numpy, but as far as I know no functions yet make use of it. Anne Geoffrey ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
On Sat, Mar 28, 2009 at 23:15, Anne Archibald peridot.face...@gmail.com wrote: 2009/3/28 Geoffrey Irving irv...@naml.us: On Sat, Mar 28, 2009 at 12:47 AM, Robert Kern robert.k...@gmail.com wrote: 2009/3/27 Charles R Harris charlesr.har...@gmail.com: On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern robert.k...@gmail.com wrote: On Fri, Mar 27, 2009 at 17:38, Bryan Cole br...@cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? dot(a,b) was specifically designed for this use case. I think maybe he wants to treat them as stacked matrices. Oh, right. Sorry. dot(a, b) works when a is (N, 4, 4) and b is just (4, 4). Never mind. It'd be great if this operation existed as a primitive. What do you think would be the best way in which to add it? One option would be to add a keyword argument to dot giving a set of axes to map over. E.g., dot(a, b, map=0) = array([dot(u,v) for u,v in zip(a,b)]) # but in C map isn't a very good name for the argument, though. I think the right long-term solution is to make dot (and some other linear algebra functions) into generalized ufuncs, so that when you dot two multidimensional objects together, they are treated as arrays of two-dimensional arrays, broadcasting is done on all but the last two dimensions, and then the linear algebra is applied elementwise. This covers basically all stacked matrices uses in a very general way, but would require some redesigning of the linear algebra system - for example, dot() currently works on both two- and one-dimensional arrays, which can't work in such a setting. The infrastructure to support such generalized ufuncs has been added to numpy, but as far as I know no functions yet make use of it. I don't think there is a way to do it in general with dot(). Some cases are ambiguous. I think you will need separate matrix-matrix, matrix-vector, and vector-vector gufuncs, to coin a term. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
On Fri, Mar 27, 2009 at 17:38, Bryan Cole br...@cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? dot(a,b) was specifically designed for this use case. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] array of matrices
On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern robert.k...@gmail.com wrote: On Fri, Mar 27, 2009 at 17:38, Bryan Cole br...@cole.uklinux.net wrote: I have a number of arrays of shape (N,4,4). I need to perform a vectorised matrix-multiplication between pairs of them I.e. matrix-multiplication rules for the last two dimensions, usual element-wise rule for the 1st dimension (of length N). (How) is this possible with numpy? dot(a,b) was specifically designed for this use case. I think maybe he wants to treat them as stacked matrices. In [13]: a = arange(8).reshape(2,2,2) In [14]: (a[:,:,:,newaxis]*a[:,newaxis,:,:]).sum(-2) Out[14]: array([[[ 2, 3], [ 6, 11]], [[46, 55], [66, 79]]]) In [15]: for i in range(2) : dot(a[i],a[i]) : Out[15]: array([[ 2, 3], [ 6, 11]]) Out[15]: array([[46, 55], [66, 79]]) Although it might be easier to keep them in a list. Chuck ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Array of matrices - Inverse and dot
On Mon, Jan 26, 2009 at 11:59 PM, Jean-Baptiste Rudant boogalo...@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
Re: [Numpy-discussion] Array of matrices - Inverse and dot
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-tp21666949p21670624.html Sent from the Numpy-discussion mailing list archive at Nabble.com. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion