3910
|
1 /* |
|
2 |
3911
|
3 Copyright (C) 2002 John W. Eaton |
3910
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
3911
|
23 // Author: Paul Kienzle <pkienzle@users.sf.net> |
|
24 |
3910
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include "dMatrix.h" |
|
30 #include "CMatrix.h" |
|
31 |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "oct-obj.h" |
|
35 |
|
36 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
37 extern void |
|
38 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
39 |
|
40 extern void |
|
41 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
42 #endif |
|
43 |
|
44 template <class T> |
|
45 void |
|
46 kron (const Array2<T>& A, const Array2<T>& B, Array2<T>& C) |
|
47 { |
|
48 C.resize (A.rows () * B.rows (), A.columns () * B.columns ()); |
|
49 |
|
50 int Ac, Ar, Cc, Cr; |
|
51 |
|
52 for (Ac = Cc = 0; Ac < A.columns (); Ac++, Cc += B.columns ()) |
|
53 for (Ar = Cr = 0; Ar < A.rows (); Ar++, Cr += B.rows ()) |
|
54 { |
|
55 const T v = A (Ar, Ac); |
|
56 for (int Bc = 0; Bc < B.columns (); Bc++) |
|
57 for (int Br = 0; Br < B.rows (); Br++) |
|
58 C.xelem (Cr+Br, Cc+Bc) = v * B.elem (Br, Bc); |
|
59 } |
|
60 } |
|
61 |
|
62 template void |
|
63 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
64 |
|
65 template void |
|
66 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
67 |
|
68 DEFUN_DLD (kron, args, nargout, "-*- texinfo -*-\n\ |
|
69 @deftypefn {Function File} {} kron (@var{a}, @var{b})\n\ |
|
70 Form the kronecker product of two matrices, defined block by block as\n\ |
|
71 \n\ |
|
72 @example\n\ |
|
73 x = [a(i, j) b]\n\ |
|
74 @end example\n\ |
|
75 \n\ |
|
76 For example,\n\ |
|
77 \n\ |
|
78 @example\n\ |
|
79 @group\n\ |
|
80 kron (1:4, ones (3, 1))\n\ |
|
81 @result{} 1 2 3 4\n\ |
|
82 1 2 3 4\n\ |
|
83 1 2 3 4\n\ |
|
84 @end group\n\ |
|
85 @end example\n\ |
|
86 @end deftypefn") |
|
87 { |
|
88 octave_value_list retval; |
|
89 |
|
90 int nargin = args.length (); |
|
91 |
|
92 if (nargin != 2 || nargout > 1) |
|
93 { |
|
94 print_usage ("kron"); |
|
95 } |
|
96 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
|
97 { |
|
98 ComplexMatrix a (args(0).complex_matrix_value()); |
|
99 ComplexMatrix b (args(1).complex_matrix_value()); |
|
100 |
|
101 if (! error_state) |
|
102 { |
|
103 ComplexMatrix c; |
|
104 kron (a, b, c); |
|
105 retval(0) = c; |
|
106 } |
|
107 } |
|
108 else |
|
109 { |
|
110 Matrix a (args(0).matrix_value ()); |
|
111 Matrix b (args(1).matrix_value ()); |
|
112 |
|
113 if (! error_state) |
|
114 { |
|
115 Matrix c; |
|
116 kron (a, b, c); |
|
117 retval (0) = c; |
|
118 } |
|
119 } |
|
120 |
|
121 return retval; |
|
122 } |