954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiplication of matrices consisting of arrays

Hello,

I am wondering if there is an efficient way (other than for cycle) of solving following task:

I need to multiply e.g. two matrices and at every point in the matrix there is an array. The two matrices shall be multiplied using matrix multiplication and when the elements of these matrices are multiplied, this shall be done elementwise. So lets say such multiplication:

[[[1,1,1],[2,2,2]],
[[3,3,3],[4,4,4]]]
x
[[[1,1,1],[2,2,2]],
[[3,3,3],[4,4,4]]]
=
[[[1,1,1]*[1,1,1]+[2,2,2]*[3,3,3],[1,1,1]*[2,2,2]+[2,2,2]*[4,4,4]],
[[3,3,3]*[1,1,1]+[4,4,4]*[3,3,3]],[[3,3,3]*[2,2,2]+[4,4,4]*[4,4,4]]]

The multiplication * denotes elementwise multiplication and x denotes matrix multiplication. Dot() for arrays (numpy) or * for matrices (scipy) does not work as I need to.

Thank anybody for an idea, how to do it.

Petan Kl
Newbie Poster
3 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Are you always multiplying the matrix by itself?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

No. This was just an enxample, because here one an quickly see what I need to do.

The arrays in the matrix will consist of tens of at least thousands of elements. This is why I would like to skip for cycle.

Petan Kl
Newbie Poster
3 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

If the element multiplication is normal matrix multiplication, you could split_array the third dimension, multiply and sum the result matrixes. Don't know if it match your specification. Or you could define your own class (maybe derived from list or numpy array) with appropriate magic methods for multiplication to define the elementwice multiplication for the vector values in array.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
If the element multiplication is normal matrix multiplication, you could split_array the third dimension, multiply and sum the result matrixes. Don't know if it match your specification. Or you could define your own class (maybe derived from list or numpy array) with appropriate magic methods for multiplication to define the elementwice multiplication for the vector values in array.

Yes, this I think similar to what I've tried before in a little bit stupid way:

matrixA x matrixB =

[ [ matrixA[0][0]*matrixB[0][0] + matrixA[0][1]*matrixB[1][0] , matrixA[0][0]*matrixB[0][1] + matrixA[0][1]*matrixB[1][1] ] ,\
 [ matrixA[1][0]*matrixB[0][0] + matrixA[1][1]*matrixB[1][0] , matrixA[1][0]*matrixB[0][1] + matrixA[1][1]*matrixB[1][1] ] ]


where matrixA and matrixB are 2x2 matrices and at every position there is a standard array. It's not so slow as I am multiplying just 2x2 matrices and the most time consuming is the * multiplication between arrays which contain a lot of elements. However I was wondering if there isn't more elegant python like solution.

Isn't there some way in e.g. numpy.dot() how to specify how to treat the multiplications inside the matrix multiplication? Because it seem that if the matrix contain another array, numpy.dot() multiplies the outside matrices and the multiplication done "inside" are treated as matrix multiplications as well.

Anyway thank you for your time and help.

Petan Kl
Newbie Poster
3 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

I think if you really target a fast operation for matrices with tens of thousands of numbers, you should write your own C function to perform the multiplication and wrap it either with the Instant module http://heim.ifi.uio.no/~kent-and/software/Instant/doc/Instant.html if you have a linux system, or with swig http://swig.org/ if you don't, or perhaps use ctypes http://docs.python.org/library/ctypes.html#module-ctypes to access the C shared library.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: