3910
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 Paul Kienzle |
|
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 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "dMatrix.h" |
|
28 #include "CMatrix.h" |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "oct-obj.h" |
|
33 |
|
34 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
35 extern void |
|
36 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
37 |
|
38 extern void |
|
39 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
40 #endif |
|
41 |
|
42 template <class T> |
|
43 void |
|
44 kron (const Array2<T>& A, const Array2<T>& B, Array2<T>& C) |
|
45 { |
|
46 C.resize (A.rows () * B.rows (), A.columns () * B.columns ()); |
|
47 |
|
48 int Ac, Ar, Cc, Cr; |
|
49 |
|
50 for (Ac = Cc = 0; Ac < A.columns (); Ac++, Cc += B.columns ()) |
|
51 for (Ar = Cr = 0; Ar < A.rows (); Ar++, Cr += B.rows ()) |
|
52 { |
|
53 const T v = A (Ar, Ac); |
|
54 for (int Bc = 0; Bc < B.columns (); Bc++) |
|
55 for (int Br = 0; Br < B.rows (); Br++) |
|
56 C.xelem (Cr+Br, Cc+Bc) = v * B.elem (Br, Bc); |
|
57 } |
|
58 } |
|
59 |
|
60 template void |
|
61 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
62 |
|
63 template void |
|
64 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
65 |
|
66 DEFUN_DLD (kron, args, nargout, "-*- texinfo -*-\n\ |
|
67 @deftypefn {Function File} {} kron (@var{a}, @var{b})\n\ |
|
68 Form the kronecker product of two matrices, defined block by block as\n\ |
|
69 \n\ |
|
70 @example\n\ |
|
71 x = [a(i, j) b]\n\ |
|
72 @end example\n\ |
|
73 \n\ |
|
74 For example,\n\ |
|
75 \n\ |
|
76 @example\n\ |
|
77 @group\n\ |
|
78 kron (1:4, ones (3, 1))\n\ |
|
79 @result{} 1 2 3 4\n\ |
|
80 1 2 3 4\n\ |
|
81 1 2 3 4\n\ |
|
82 @end group\n\ |
|
83 @end example\n\ |
|
84 @end deftypefn") |
|
85 { |
|
86 octave_value_list retval; |
|
87 |
|
88 int nargin = args.length (); |
|
89 |
|
90 if (nargin != 2 || nargout > 1) |
|
91 { |
|
92 print_usage ("kron"); |
|
93 } |
|
94 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
|
95 { |
|
96 ComplexMatrix a (args(0).complex_matrix_value()); |
|
97 ComplexMatrix b (args(1).complex_matrix_value()); |
|
98 |
|
99 if (! error_state) |
|
100 { |
|
101 ComplexMatrix c; |
|
102 kron (a, b, c); |
|
103 retval(0) = c; |
|
104 } |
|
105 } |
|
106 else |
|
107 { |
|
108 Matrix a (args(0).matrix_value ()); |
|
109 Matrix b (args(1).matrix_value ()); |
|
110 |
|
111 if (! error_state) |
|
112 { |
|
113 Matrix c; |
|
114 kron (a, b, c); |
|
115 retval (0) = c; |
|
116 } |
|
117 } |
|
118 |
|
119 return retval; |
|
120 } |