home *** CD-ROM | disk | FTP | other *** search
- //-------------------------------------------------------------------//
-
- // Synopsis: Kronecker tensor product.
-
- // Syntax: kron ( x , y )
-
- // Description:
-
- // kron(X,Y) is the Kronecker tensor product of X and Y. The
- // result is a large matrix formed by taking all possible
- // products between the elements of X and those of Y. For
- // example, if X is 2 by 3, then KRON(X,Y) is
- //
- // [ X[1;1]*Y X[1;2]*Y X[1;3]*Y
- // X[2;1]*Y X[2;2]*Y X[2;3]*Y ]
- //
- // If either X or Y is sparse, only nonzero elements are
- // multiplied in the computation, and the result is sparse.
-
- // Full: J. N. Little, 4-21-85.
- // Sparse: T. R. Gardos (Georgia Tech), 5-4-93.
- // Copyright (c) 1984-93 by The MathWorks, Inc.
- //
- // This version not strictly part of the toolbox - included here
- // because version supplied with Matlab 4.0 is not sparse aware.
- // (This comment does not apply to RLaB)
-
- // This file is a translation of kron.m from version 2.0 of
- // "The Test Matrix Toolbox for Matlab", described in Numerical
- // Analysis Report No. 237, December 1993, by N. J. Higham.
-
- //-------------------------------------------------------------------//
-
- kron = function ( A , B )
- {
- ma = A.nr; na = A.nr;
- mb = A.nr; nb = B.nc;
-
- // full inputs
-
- K = zeros(ma*mb,na*nb);
- if (ma*na <= mb*nb)
- {
- for (i in 1:ma)
- {
- ik = 1+(i-1)*mb:i*mb;
- for (j in 1:na)
- {
- jk = 1+(j-1)*nb:j*nb;
- K[ik;jk] = A[i;j]*B;
- }
- }
- else
- for (i in 1:mb)
- {
- ik = i:mb:(ma-1)*mb+i;
- for (j in 1:nb)
- {
- jk = j:nb:(na-1)*nb+j;
- K[ik;jk] = B[i;j]*A;
- }
- }
- }
-
- return K;
- };
-