3910
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2002, 2005, 2006, 2007 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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3910
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3910
|
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" |
4153
|
31 #include "quit.h" |
3910
|
32 |
|
33 #include "defun-dld.h" |
|
34 #include "error.h" |
|
35 #include "oct-obj.h" |
|
36 |
|
37 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
38 extern void |
|
39 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
40 |
|
41 extern void |
|
42 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
43 #endif |
|
44 |
|
45 template <class T> |
|
46 void |
|
47 kron (const Array2<T>& A, const Array2<T>& B, Array2<T>& C) |
|
48 { |
|
49 C.resize (A.rows () * B.rows (), A.columns () * B.columns ()); |
|
50 |
5275
|
51 octave_idx_type Ac, Ar, Cc, Cr; |
3910
|
52 |
|
53 for (Ac = Cc = 0; Ac < A.columns (); Ac++, Cc += B.columns ()) |
|
54 for (Ar = Cr = 0; Ar < A.rows (); Ar++, Cr += B.rows ()) |
|
55 { |
|
56 const T v = A (Ar, Ac); |
5275
|
57 for (octave_idx_type Bc = 0; Bc < B.columns (); Bc++) |
|
58 for (octave_idx_type Br = 0; Br < B.rows (); Br++) |
4153
|
59 { |
|
60 OCTAVE_QUIT; |
|
61 C.xelem (Cr+Br, Cc+Bc) = v * B.elem (Br, Bc); |
|
62 } |
3910
|
63 } |
|
64 } |
|
65 |
|
66 template void |
|
67 kron (const Array2<double>&, const Array2<double>&, Array2<double>&); |
|
68 |
|
69 template void |
|
70 kron (const Array2<Complex>&, const Array2<Complex>&, Array2<Complex>&); |
|
71 |
|
72 DEFUN_DLD (kron, args, nargout, "-*- texinfo -*-\n\ |
6678
|
73 @deftypefn {Loadable Function} {} kron (@var{a}, @var{b})\n\ |
3910
|
74 Form the kronecker product of two matrices, defined block by block as\n\ |
|
75 \n\ |
|
76 @example\n\ |
|
77 x = [a(i, j) b]\n\ |
|
78 @end example\n\ |
|
79 \n\ |
|
80 For example,\n\ |
|
81 \n\ |
|
82 @example\n\ |
|
83 @group\n\ |
|
84 kron (1:4, ones (3, 1))\n\ |
|
85 @result{} 1 2 3 4\n\ |
|
86 1 2 3 4\n\ |
|
87 1 2 3 4\n\ |
|
88 @end group\n\ |
|
89 @end example\n\ |
|
90 @end deftypefn") |
|
91 { |
|
92 octave_value_list retval; |
|
93 |
|
94 int nargin = args.length (); |
|
95 |
|
96 if (nargin != 2 || nargout > 1) |
|
97 { |
5823
|
98 print_usage (); |
3910
|
99 } |
|
100 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
|
101 { |
|
102 ComplexMatrix a (args(0).complex_matrix_value()); |
|
103 ComplexMatrix b (args(1).complex_matrix_value()); |
|
104 |
|
105 if (! error_state) |
|
106 { |
|
107 ComplexMatrix c; |
|
108 kron (a, b, c); |
|
109 retval(0) = c; |
|
110 } |
|
111 } |
|
112 else |
|
113 { |
|
114 Matrix a (args(0).matrix_value ()); |
|
115 Matrix b (args(1).matrix_value ()); |
|
116 |
|
117 if (! error_state) |
|
118 { |
|
119 Matrix c; |
|
120 kron (a, b, c); |
|
121 retval (0) = c; |
|
122 } |
|
123 } |
|
124 |
|
125 return retval; |
|
126 } |