Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Andre Alexander Bell

On 09/16/2010 08:24 AM, Andre Alexander Bell wrote:

or you could write the loop

 >>> m1m2 = np.empty_like(m1)
 >>> for i in range(m1m2.shape[0]):
... m1m2[i] = np.dot(m1, m2)


This should have been

... m1m2[i] = np.dot(m1[i], m2[i])

Sorry for the typo.


Andre
--
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Andre Alexander Bell

Hi,

I assume you have arrays like these:

>>> import numpy as np
>>> m1 = np.random.rand(size=(4,3,3))
>>> m2 = np.random.rand(size=(4,3,3))

So that m1[0] is a 3x3 Matrix and m1[1] is another one, i.e. you have 
four matrices.


On 09/15/2010 01:54 AM, Gregory Ewing wrote:

I had thought that dot() might do this, but it appears
not, because e.g. applying it to two 3-d arrays gives
a 4-d array, not another 3-d array.


You now want to compute the matrixproducts like this

>>> np.dot(m1[0], m2[0])

and most likely you want to do this for all of the pairs

>>> m1m2 = np.array(map(lambda (a,b): np.dot(a,b), zip(m1,m2)))

or you could write the loop

>>> m1m2 = np.empty_like(m1)
>>> for i in range(m1m2.shape[0]):
... m1m2[i] = np.dot(m1, m2)

which might scale better


I'd also like to be able to find the inverse of each
matrix in one of these arrays, but again, inv() doesn't
do what I want -- it only works on 2-d arrays.


Same as before

>>> m1inv = np.array(map(np.linalg.inv, m1))

or writing the loop

>>> m1inv = np.empty_like(m1)
>>> for i in range(m1inv.shape[0]):
... m1inv[i] = np.linalg.inv(m1[i])

Once again, I'm not sure whether or not it is acceptable to have the 
overhead of treating the array as a list.



Andre
--
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Carl Banks
On Sep 14, 4:54 pm, Gregory Ewing  wrote:
> Suppose I have two N+2 dimensional arrays, representing
> N-d arrays of 2-d matrices. I want to perform matrix
> multiplication between corresponding matrices in these
> arrays.
>
> I had thought that dot() might do this, but it appears
> not, because e.g. applying it to two 3-d arrays gives
> a 4-d array, not another 3-d array.
>
> I'd also like to be able to find the inverse of each
> matrix in one of these arrays, but again, inv() doesn't
> do what I want -- it only works on 2-d arrays.
>
> Any thoughts on how to achieve these things using numpy
> functions?

I find for situations like this the best thing I can do is hand code
the bounded operation and use the slicing to handle the arbitrarily
large stuff with slicing.

So,

r[:,:,1,1] = a[:,:,1,1]*b[:,:,1,1] + a[:,:,2,1]*b[:,:,1,2]
r[:,:,1,2] = a[:,:,1,2]*b[:,:,1,1] + a[:,:,2,2]*b[:,:,1,2]
etc.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Robert Kern

On 9/15/10 11:36 AM, Colin J. Williams wrote:

On 14-Sep-10 19:54 PM, Gregory Ewing wrote:

Suppose I have two N+2 dimensional arrays, representing
N-d arrays of 2-d matrices. I want to perform matrix
multiplication between corresponding matrices in these
arrays.

I had thought that dot() might do this, but it appears
not, because e.g. applying it to two 3-d arrays gives
a 4-d array, not another 3-d array.

I'd also like to be able to find the inverse of each
matrix in one of these arrays, but again, inv() doesn't
do what I want -- it only works on 2-d arrays.

Any thoughts on how to achieve these things using numpy
functions?


There is a Matrix sub-class which permit you to do that sort of thimg.


No, it doesn't.

--
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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Shashwat Anand
On Tue, Sep 14, 2010 at 7:54 PM, Gregory Ewing
wrote:

> Suppose I have two N+2 dimensional arrays, representing
> N-d arrays of 2-d matrices. I want to perform matrix
> multiplication between corresponding matrices in these
> arrays.
>
> I had thought that dot() might do this, but it appears
> not, because e.g. applying it to two 3-d arrays gives
> a 4-d array, not another 3-d array.
>
> I'd also like to be able to find the inverse of each
> matrix in one of these arrays, but again, inv() doesn't
> do what I want -- it only works on 2-d arrays.
>
> Any thoughts on how to achieve these things using numpy
> functions?
>

Unrelated to numpy, but you can apply the basics of dynamic programming to
optimize the stuff.


> --
> Greg
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
~l0nwlf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy: Multiplying arrays of matrices

2010-09-15 Thread Colin J. Williams

On 14-Sep-10 19:54 PM, Gregory Ewing wrote:

Suppose I have two N+2 dimensional arrays, representing
N-d arrays of 2-d matrices. I want to perform matrix
multiplication between corresponding matrices in these
arrays.

I had thought that dot() might do this, but it appears
not, because e.g. applying it to two 3-d arrays gives
a 4-d array, not another 3-d array.

I'd also like to be able to find the inverse of each
matrix in one of these arrays, but again, inv() doesn't
do what I want -- it only works on 2-d arrays.

Any thoughts on how to achieve these things using numpy
functions?




There is a Matrix sub-class which permit you to do that sort of thimg.

Colin W.
--
http://mail.python.org/mailman/listinfo/python-list


Numpy: Multiplying arrays of matrices

2010-09-14 Thread Gregory Ewing

Suppose I have two N+2 dimensional arrays, representing
N-d arrays of 2-d matrices. I want to perform matrix
multiplication between corresponding matrices in these
arrays.

I had thought that dot() might do this, but it appears
not, because e.g. applying it to two 3-d arrays gives
a 4-d array, not another 3-d array.

I'd also like to be able to find the inverse of each
matrix in one of these arrays, but again, inv() doesn't
do what I want -- it only works on 2-d arrays.

Any thoughts on how to achieve these things using numpy
functions?

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list