| Re: kronecker product [message #78612 is a reply to message #78583] |
Tue, 06 December 2011 06:31   |
Yngvar Larsen
Messages: 134 Registered: January 2010
|
Senior Member |
|
|
On Dec 6, 2:18 am, Ecaterina Coman <ecatc...@umbc.edu> wrote:
> Hey guys,
> I have two sparse arrays in IDL that I want to compute the kronecker
> product with. I've googled like a crazy person but can't find anything
> useful. Does IDL had any routines that I can use to get the krokecker
> product of these two sparse arrays?
> Thanks.
This straightforward implementation should work. It is probably
possible to optimize it.
function kroneckerProduct, A, B
;+
;
; Kronecker tensor product of A and B.
; Result is a large matrix formed by taking all
; possible products between the elements of A and those of B.
;
; Input: A - (a x b) matrix
; B - (c x d) matrix
;
; Output: C - (ac x bd) matrix
;
; C =
; A(0 , 0) B A( 0, 1) B ... A( 0, n-1) B
; A(1 , 0) B A( 1, 1) B ... A( 1, n-1) B
; : : : :
; A(m-1, 0) B A(m-1, 1) B ... A(m-1, n-1) B
;
; Written by Yngvar Larsen <yngvar.larsen@norut.no>
; 2004-07-01
;
; Fixed type bug. Let IDL do the type inference:
; Now returns array of type 'size(A[0]*B[0], /type)'.
; YL 2005-01-13
;-
sizeA = size(A, /dimensions)
sizeB = size(B, /dimensions)
if n_elements(sizeA) eq 1 then sizeA = [sizeA, 1]
if n_elements(sizeB) eq 1 then sizeB = [sizeB, 1]
C = make_array(sizeA[0]*sizeB[0], sizeA[1]*sizeB[1], $
type=size(A[0]*B[0], /type))
for x=0L,sizeA[0]-1 do begin
ix = x*sizeB[0]
for y=0L,sizeA[1]-1 do C[ix,y*sizeB[1]] = A[x,y]*B
endfor
return, C
end
--
Yngvar
|
|
|
|