comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: matrix multiplication of 2 three-dimensional arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: matrix multiplication of 2 three-dimensional arrays [message #62022] Thu, 21 August 2008 06:48
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Aug 21, 6:00 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
wrote:
> On 20 Aug., 21:53, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
> wrote:
>
>
>
>> On 20 Aug., 18:11, Jeremy Bailin <astroco...@gmail.com> wrote:
>
>>> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
>>> wrote:
>
>>>> Dear experts,
>
>>>> I would like to matrix multiply two matrices with dimensions
>>>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>>>> with dimension [3,3]
>>>> I could do this with a for loop over the dimension [1500] but i
>>>> suppose this is not very elegant. Is there any other way to do this
>>>> time-efficient.
>
>>>> Best regards,
>
>>>> thomas
>
>>> My IDL-foo feels strong today... let's see:
>
>>> If A and B are 3x3, then C = A ## B is equivalent to:
>
>>> q = rebin(indgen(3),3,3)
>>> p = rebin(reform(indgen(3),1,3),3,3)
>>> c = total(rebin(reform(a[q,p],1,3,3),3,3,3) * reform(b[*,q],3,3,3), 2)
>
>>> Adding an extra dimension on the end of that gets tricky, but I think
>>> the following should work if A and B are each 3x3xNMATRIX and you want
>>> C to be a 3x3xNMATRIX array where each C[*,*,i] = A[*,*,i] ##
>>> B[*,*,i]:
>
>>> c = total(rebin(reform( (a[q,p,*])[0:2,3*lindgen(3),*],1,3,3,nmatrix),
>>> 3,3,3,nmatrix) * reform(b[*,q,*],
>>>   3,3,3,nmatrix), 2)
>
>>> Incidentally, does anyone have a better way of doing the (a[q,p,*])
>>> [0:2,3*lindgen(3),*] bit of it? The problem is that if A, Q and P are
>>> each 3x3 then A[Q,P] is 3x3, but if A is 3x3xN then A[Q,P,*] is 9x9xN.
>
>>> If I see another reform today, I'm going to scream. ;-)
>
>>> -Jeremy.
>
>> Thank you all, very much!
>> I like of course the code of jeremy most, but i think paolo's
>> suggestions is the most efficient one. But let us see, i will make
>> some investigations...
>
>> Best regards,
>
>> thomas
>
> Here is another suggestion from a very nice collegue:
>
> function matrix_multiply_3, A, B
> ;---only for quadratic matrices
>
> s = size(A)
> N2a = s(2)
> N3a = s(3)
>
> s = size(B)
> N1b = s(1)
>
> C = make_array([N1b,N2a,N3a],type=s(4))
>
> for k=0,N2a-1 do begin
> for n=0,N1b-1 do begin
>
> C(n,k,*) = total(A(*,k,*) * B(n,*,*),1)
>
> endfor
> endfor
>
> return, C
>
> end
>
> Advantage: Without rebin it can also handle complex numbers
>
> best regards,
> thomas

Yeah, that's basically Paolo's solution. I hadn't realized that REBIN
doesn't accept complex arguments - that's quite annoying!

-Jeremy.
Re: matrix multiplication of 2 three-dimensional arrays [message #62028 is a reply to message #62022] Thu, 21 August 2008 03:00 Go to previous message
thomas.jagdhuber@dlr. is currently offline  thomas.jagdhuber@dlr.
Messages: 19
Registered: February 2007
Junior Member
On 20 Aug., 21:53, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
wrote:
> On 20 Aug., 18:11, Jeremy Bailin <astroco...@gmail.com> wrote:
>
>
>
>> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
>> wrote:
>
>>> Dear experts,
>
>>> I would like to matrix multiply two matrices with dimensions
>>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>>> with dimension [3,3]
>>> I could do this with a for loop over the dimension [1500] but i
>>> suppose this is not very elegant. Is there any other way to do this
>>> time-efficient.
>
>>> Best regards,
>
>>> thomas
>
>> My IDL-foo feels strong today... let's see:
>
>> If A and B are 3x3, then C = A ## B is equivalent to:
>
>> q = rebin(indgen(3),3,3)
>> p = rebin(reform(indgen(3),1,3),3,3)
>> c = total(rebin(reform(a[q,p],1,3,3),3,3,3) * reform(b[*,q],3,3,3), 2)
>
>> Adding an extra dimension on the end of that gets tricky, but I think
>> the following should work if A and B are each 3x3xNMATRIX and you want
>> C to be a 3x3xNMATRIX array where each C[*,*,i] = A[*,*,i] ##
>> B[*,*,i]:
>
>> c = total(rebin(reform( (a[q,p,*])[0:2,3*lindgen(3),*],1,3,3,nmatrix),
>> 3,3,3,nmatrix) * reform(b[*,q,*],
>> 3,3,3,nmatrix), 2)
>
>> Incidentally, does anyone have a better way of doing the (a[q,p,*])
>> [0:2,3*lindgen(3),*] bit of it? The problem is that if A, Q and P are
>> each 3x3 then A[Q,P] is 3x3, but if A is 3x3xN then A[Q,P,*] is 9x9xN.
>
>> If I see another reform today, I'm going to scream. ;-)
>
>> -Jeremy.
>
> Thank you all, very much!
> I like of course the code of jeremy most, but i think paolo's
> suggestions is the most efficient one. But let us see, i will make
> some investigations...
>
> Best regards,
>
> thomas

Here is another suggestion from a very nice collegue:

function matrix_multiply_3, A, B
;---only for quadratic matrices

s = size(A)
N2a = s(2)
N3a = s(3)

s = size(B)
N1b = s(1)


C = make_array([N1b,N2a,N3a],type=s(4))

for k=0,N2a-1 do begin
for n=0,N1b-1 do begin

C(n,k,*) = total(A(*,k,*) * B(n,*,*),1)

endfor
endfor

return, C

end

Advantage: Without rebin it can also handle complex numbers

best regards,
thomas
Re: matrix multiplication of 2 three-dimensional arrays [message #62041 is a reply to message #62028] Wed, 20 August 2008 12:53 Go to previous message
thomas.jagdhuber@dlr. is currently offline  thomas.jagdhuber@dlr.
Messages: 19
Registered: February 2007
Junior Member
On 20 Aug., 18:11, Jeremy Bailin <astroco...@gmail.com> wrote:
> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
> wrote:
>
>> Dear experts,
>
>> I would like to matrix multiply two matrices with dimensions
>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>> with dimension [3,3]
>> I could do this with a for loop over the dimension [1500] but i
>> suppose this is not very elegant. Is there any other way to do this
>> time-efficient.
>
>> Best regards,
>
>> thomas
>
> My IDL-foo feels strong today... let's see:
>
> If A and B are 3x3, then C = A ## B is equivalent to:
>
> q = rebin(indgen(3),3,3)
> p = rebin(reform(indgen(3),1,3),3,3)
> c = total(rebin(reform(a[q,p],1,3,3),3,3,3) * reform(b[*,q],3,3,3), 2)
>
> Adding an extra dimension on the end of that gets tricky, but I think
> the following should work if A and B are each 3x3xNMATRIX and you want
> C to be a 3x3xNMATRIX array where each C[*,*,i] = A[*,*,i] ##
> B[*,*,i]:
>
> c = total(rebin(reform( (a[q,p,*])[0:2,3*lindgen(3),*],1,3,3,nmatrix),
> 3,3,3,nmatrix) * reform(b[*,q,*],
>   3,3,3,nmatrix), 2)
>
> Incidentally, does anyone have a better way of doing the (a[q,p,*])
> [0:2,3*lindgen(3),*] bit of it? The problem is that if A, Q and P are
> each 3x3 then A[Q,P] is 3x3, but if A is 3x3xN then A[Q,P,*] is 9x9xN.
>
> If I see another reform today, I'm going to scream. ;-)
>
> -Jeremy.

Thank you all, very much!
I like of course the code of jeremy most, but i think paolo's
suggestions is the most efficient one. But let us see, i will make
some investigations...

Best regards,

thomas
Re: matrix multiplication of 2 three-dimensional arrays [message #62043 is a reply to message #62041] Wed, 20 August 2008 09:27 Go to previous message
Juggernaut is currently offline  Juggernaut
Messages: 83
Registered: June 2008
Member
On Aug 20, 12:11 pm, Jeremy Bailin <astroco...@gmail.com> wrote:
> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
> wrote:
>
>> Dear experts,
>
>> I would like to matrix multiply two matrices with dimensions
>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>> with dimension [3,3]
>> I could do this with a for loop over the dimension [1500] but i
>> suppose this is not very elegant. Is there any other way to do this
>> time-efficient.
>
>> Best regards,
>
>> thomas
>
> My IDL-foo feels strong today... let's see:
>
> If A and B are 3x3, then C = A ## B is equivalent to:
>
> q = rebin(indgen(3),3,3)
> p = rebin(reform(indgen(3),1,3),3,3)
> c = total(rebin(reform(a[q,p],1,3,3),3,3,3) * reform(b[*,q],3,3,3), 2)
>
> Adding an extra dimension on the end of that gets tricky, but I think
> the following should work if A and B are each 3x3xNMATRIX and you want
> C to be a 3x3xNMATRIX array where each C[*,*,i] = A[*,*,i] ##
> B[*,*,i]:
>
> c = total(rebin(reform( (a[q,p,*])[0:2,3*lindgen(3),*],1,3,3,nmatrix),
> 3,3,3,nmatrix) * reform(b[*,q,*],
> 3,3,3,nmatrix), 2)
>
> Incidentally, does anyone have a better way of doing the (a[q,p,*])
> [0:2,3*lindgen(3),*] bit of it? The problem is that if A, Q and P are
> each 3x3 then A[Q,P] is 3x3, but if A is 3x3xN then A[Q,P,*] is 9x9xN.
>
> If I see another reform today, I'm going to scream. ;-)
>
> -Jeremy.

Nevermind I see what you're wanting... sorry for the confusion
Re: matrix multiplication of 2 three-dimensional arrays [message #62044 is a reply to message #62043] Wed, 20 August 2008 09:11 Go to previous message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
wrote:
> Dear experts,
>
> I would like to matrix multiply two matrices with dimensions
> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
> with dimension [3,3]
> I could do this with a for loop over the dimension [1500] but i
> suppose this is not very elegant. Is there any other way to do this
> time-efficient.
>
> Best regards,
>
> thomas

My IDL-foo feels strong today... let's see:

If A and B are 3x3, then C = A ## B is equivalent to:

q = rebin(indgen(3),3,3)
p = rebin(reform(indgen(3),1,3),3,3)
c = total(rebin(reform(a[q,p],1,3,3),3,3,3) * reform(b[*,q],3,3,3), 2)

Adding an extra dimension on the end of that gets tricky, but I think
the following should work if A and B are each 3x3xNMATRIX and you want
C to be a 3x3xNMATRIX array where each C[*,*,i] = A[*,*,i] ##
B[*,*,i]:

c = total(rebin(reform( (a[q,p,*])[0:2,3*lindgen(3),*],1,3,3,nmatrix),
3,3,3,nmatrix) * reform(b[*,q,*],
3,3,3,nmatrix), 2)

Incidentally, does anyone have a better way of doing the (a[q,p,*])
[0:2,3*lindgen(3),*] bit of it? The problem is that if A, Q and P are
each 3x3 then A[Q,P] is 3x3, but if A is 3x3xN then A[Q,P,*] is 9x9xN.

If I see another reform today, I'm going to scream. ;-)

-Jeremy.
Re: matrix multiplication of 2 three-dimensional arrays [message #62045 is a reply to message #62044] Wed, 20 August 2008 09:08 Go to previous message
Juggernaut is currently offline  Juggernaut
Messages: 83
Registered: June 2008
Member
On Aug 20, 10:51 am, pgri...@gmail.com wrote:
> thomas.jagdhuber wrote:
>> On 20 Aug., 16:34, pgri...@gmail.com wrote:
>>> Bennett wrote:
>>>> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
>>>> wrote:
>>>> > Dear experts,
>
>>>> > I would like to matrix multiply two matrices with dimensions
>>>> > [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>>>> > with dimension [3,3]
>>>> > I could do this with a for loop over the dimension [1500] but i
>>>> > suppose this is not very elegant. Is there any other way to do this
>>>> > time-efficient.
>
>>>> > Best regards,
>
>>>> > thomas
>
>>>> Have you searched help on product() and its dimensional keyword?
>>>> This could be useful for you.
>
>>> It is not clear to me how "product" can be used for solving
>>> matrix multiplications.
>
>>> To the original poster:
>
>>> 1) your problem is so small that I don't see any need for
>>> optimization.
>
>>> 2) however, if you really want to optimize in case that the number of
>>> matrices N should increase in the future, use loops over the 3x3
>>> matrix
>>> arrays and columns instead and treat the matrix elements as N-element
>>> vectors. This way, more work is done per loop for large values of N.
>
>>> Ciao,
>>> Paolo
>
>> I think i will calculate each matrix element alone by the linear
>> combination and then just use the whole vector of 1500 Values for
>> calculating each linear combination. this should be reasonable as long
>> as the 3x3-dimension is valid and not growing.
>
> Yes, that is exactly what I was suggesting. The code will look ugly
> though...;-)
>
> Ciao,
> Paol

Way I read it was that you had a matrix of 3x3x1500 and wanted to
multiply them together....not sure what it means to say 1500 times 2
matrices of 3x3 dimension unless you just mean 1500*[A]*[B] on an
element by element basis where A = [[a,b,c],[d,e,f],[g,h,i]] and B =
[[a2,b2,c2],[d2,e2,f2],[g2,h2,i2]]? That's how that reads to me... I
don't think I quite understand what it is that you're trying to do so
therein lies the confusion.
Re: matrix multiplication of 2 three-dimensional arrays [message #62046 is a reply to message #62045] Wed, 20 August 2008 07:51 Go to previous message
pgrigis is currently offline  pgrigis
Messages: 436
Registered: September 2007
Senior Member
thomas.jagdhuber wrote:
> On 20 Aug., 16:34, pgri...@gmail.com wrote:
>> Bennett wrote:
>>> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
>>> wrote:
>>>> Dear experts,
>>
>>>> I would like to matrix multiply two matrices with dimensions
>>>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>>>> with dimension [3,3]
>>>> I could do this with a for loop over the dimension [1500] but i
>>>> suppose this is not very elegant. Is there any other way to do this
>>>> time-efficient.
>>
>>>> Best regards,
>>
>>>> thomas
>>
>>> Have you searched help on product() and its dimensional keyword?
>>> This could be useful for you.
>>
>> It is not clear to me how "product" can be used for solving
>> matrix multiplications.
>>
>> To the original poster:
>>
>> 1) your problem is so small that I don't see any need for
>> optimization.
>>
>> 2) however, if you really want to optimize in case that the number of
>> matrices N should increase in the future, use loops over the 3x3
>> matrix
>> arrays and columns instead and treat the matrix elements as N-element
>> vectors. This way, more work is done per loop for large values of N.
>>
>> Ciao,
>> Paolo
>
> I think i will calculate each matrix element alone by the linear
> combination and then just use the whole vector of 1500 Values for
> calculating each linear combination. this should be reasonable as long
> as the 3x3-dimension is valid and not growing.

Yes, that is exactly what I was suggesting. The code will look ugly
though...;-)

Ciao,
Paolo
Re: matrix multiplication of 2 three-dimensional arrays [message #62047 is a reply to message #62046] Wed, 20 August 2008 07:39 Go to previous message
thomas.jagdhuber@dlr. is currently offline  thomas.jagdhuber@dlr.
Messages: 19
Registered: February 2007
Junior Member
On 20 Aug., 16:34, pgri...@gmail.com wrote:
> Bennett wrote:
>> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
>> wrote:
>>> Dear experts,
>
>>> I would like to matrix multiply two matrices with dimensions
>>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>>> with dimension [3,3]
>>> I could do this with a for loop over the dimension [1500] but i
>>> suppose this is not very elegant. Is there any other way to do this
>>> time-efficient.
>
>>> Best regards,
>
>>> thomas
>
>> Have you searched help on product() and its dimensional keyword?
>> This could be useful for you.
>
> It is not clear to me how "product" can be used for solving
> matrix multiplications.
>
> To the original poster:
>
> 1) your problem is so small that I don't see any need for
> optimization.
>
> 2) however, if you really want to optimize in case that the number of
> matrices N should increase in the future, use loops over the 3x3
> matrix
> arrays and columns instead and treat the matrix elements as N-element
> vectors. This way, more work is done per loop for large values of N.
>
> Ciao,
> Paolo

I think i will calculate each matrix element alone by the linear
combination and then just use the whole vector of 1500 Values for
calculating each linear combination. this should be reasonable as long
as the 3x3-dimension is valid and not growing.
Re: matrix multiplication of 2 three-dimensional arrays [message #62048 is a reply to message #62047] Wed, 20 August 2008 07:35 Go to previous message
thomas.jagdhuber@dlr. is currently offline  thomas.jagdhuber@dlr.
Messages: 19
Registered: February 2007
Junior Member
On 20 Aug., 15:36, Bennett <juggernau...@gmail.com> wrote:
> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
> wrote:
>
>> Dear experts,
>
>> I would like to matrix multiply two matrices with dimensions
>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>> with dimension [3,3]
>> I could do this with a for loop over the dimension [1500] but i
>> suppose this is not very elegant. Is there any other way to do this
>> time-efficient.
>
>> Best regards,
>
>> thomas
>
> Have you searched help on product() and its dimensional keyword?
> This could be useful for you.

No bad idea, but product() does not work as a matrix product, as far
as I understood the descripition of the function
Re: matrix multiplication of 2 three-dimensional arrays [message #62049 is a reply to message #62048] Wed, 20 August 2008 07:34 Go to previous message
pgrigis is currently offline  pgrigis
Messages: 436
Registered: September 2007
Senior Member
Bennett wrote:
> On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
> wrote:
>> Dear experts,
>>
>> I would like to matrix multiply two matrices with dimensions
>> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
>> with dimension [3,3]
>> I could do this with a for loop over the dimension [1500] but i
>> suppose this is not very elegant. Is there any other way to do this
>> time-efficient.
>>
>> Best regards,
>>
>> thomas
>
> Have you searched help on product() and its dimensional keyword?
> This could be useful for you.

It is not clear to me how "product" can be used for solving
matrix multiplications.

To the original poster:

1) your problem is so small that I don't see any need for
optimization.

2) however, if you really want to optimize in case that the number of
matrices N should increase in the future, use loops over the 3x3
matrix
arrays and columns instead and treat the matrix elements as N-element
vectors. This way, more work is done per loop for large values of N.

Ciao,
Paolo
Re: matrix multiplication of 2 three-dimensional arrays [message #62051 is a reply to message #62049] Wed, 20 August 2008 06:36 Go to previous message
Juggernaut is currently offline  Juggernaut
Messages: 83
Registered: June 2008
Member
On Aug 20, 8:22 am, "thomas.jagdhuber" <thomas.jagdhu...@gmail.com>
wrote:
> Dear experts,
>
> I would like to matrix multiply two matrices with dimensions
> [3,3,1500]. means: 1500 times a matrix multiplication of 2 matrices
> with dimension [3,3]
> I could do this with a for loop over the dimension [1500] but i
> suppose this is not very elegant. Is there any other way to do this
> time-efficient.
>
> Best regards,
>
> thomas

Have you searched help on product() and its dimensional keyword?
This could be useful for you.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: strange behaviour of SOCKET
Next Topic: overplot crosses with numbers for reference

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 20:02:18 PDT 2025

Total time taken to generate the page: 0.01874 seconds