5323
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 John W. Eaton |
|
4 Copyright (C) 2005 David Bateman |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5323
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5323
|
21 |
|
22 */ |
|
23 |
|
24 // Author: Paul Kienzle <pkienzle@users.sf.net> |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include "dSparse.h" |
|
31 #include "CSparse.h" |
|
32 #include "quit.h" |
|
33 |
|
34 #include "defun-dld.h" |
|
35 #include "error.h" |
|
36 #include "oct-obj.h" |
|
37 |
|
38 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
39 extern void |
|
40 kron (const Sparse<double>&, const Sparse<double>&, Sparse<double>&); |
|
41 |
|
42 extern void |
|
43 kron (const Sparse<Complex>&, const Sparse<Complex>&, Sparse<Complex>&); |
|
44 #endif |
|
45 |
|
46 template <class T> |
|
47 void |
|
48 kron (const Sparse<T>& A, const Sparse<T>& B, Sparse<T>& C) |
|
49 { |
|
50 octave_idx_type idx = 0; |
|
51 C = Sparse<T> (A.rows () * B.rows (), A.columns () * B.columns (), |
5604
|
52 A.nzmax () * B.nzmax ()); |
5323
|
53 |
|
54 C.cidx (0) = 0; |
|
55 |
|
56 for (octave_idx_type Aj = 0; Aj < A.columns (); Aj++) |
|
57 for (octave_idx_type Bj = 0; Bj < B.columns (); Bj++) |
|
58 { |
|
59 for (octave_idx_type Ai = A.cidx (Aj); Ai < A.cidx (Aj+1); Ai++) |
|
60 { |
|
61 octave_idx_type Ci = A.ridx(Ai) * B.rows (); |
|
62 const T v = A.data (Ai); |
|
63 |
|
64 for (octave_idx_type Bi = B.cidx (Bj); Bi < B.cidx (Bj+1); Bi++) |
|
65 { |
|
66 OCTAVE_QUIT; |
|
67 C.data (idx) = v * B.data (Bi); |
|
68 C.ridx (idx++) = Ci + B.ridx (Bi); |
|
69 } |
|
70 } |
|
71 C.cidx (Aj * B.columns () + Bj + 1) = idx; |
|
72 } |
|
73 } |
|
74 |
|
75 template void |
|
76 kron (const Sparse<double>&, const Sparse<double>&, Sparse<double>&); |
|
77 |
|
78 template void |
|
79 kron (const Sparse<Complex>&, const Sparse<Complex>&, Sparse<Complex>&); |
|
80 |
5534
|
81 // PKG_ADD: dispatch ("kron", "spkron", "sparse matrix"); |
|
82 // PKG_ADD: dispatch ("kron", "spkron", "sparse complex matrix"); |
|
83 // PKG_ADD: dispatch ("kron", "spkron", "sparse bool matrix"); |
5323
|
84 DEFUN_DLD (spkron, args, nargout, "-*- texinfo -*-\n\ |
6678
|
85 @deftypefn {Loadable Function} {} spkron (@var{a}, @var{b})\n\ |
5323
|
86 Form the kronecker product of two sparse matrices. This is defined\n\ |
|
87 block by block as\n\ |
|
88 \n\ |
|
89 @example\n\ |
|
90 x = [a(i, j) b]\n\ |
|
91 @end example\n\ |
|
92 \n\ |
|
93 For example,\n\ |
|
94 \n\ |
|
95 @example\n\ |
|
96 @group\n\ |
5989
|
97 kron(speye(3),spdiag([1,2,3]))\n\ |
|
98 @result{}\n\ |
|
99 Compressed Column Sparse (rows = 9, cols = 9, nnz = 9)\n\ |
|
100 \n\ |
|
101 (1, 1) -> 1\n\ |
|
102 (2, 2) -> 2\n\ |
|
103 (3, 3) -> 3\n\ |
|
104 (4, 4) -> 1\n\ |
|
105 (5, 5) -> 2\n\ |
|
106 (6, 6) -> 3\n\ |
|
107 (7, 7) -> 1\n\ |
|
108 (8, 8) -> 2\n\ |
|
109 (9, 9) -> 3\n\ |
5323
|
110 @end group\n\ |
|
111 @end example\n\ |
|
112 @end deftypefn") |
|
113 { |
|
114 octave_value_list retval; |
|
115 |
|
116 int nargin = args.length (); |
|
117 |
|
118 if (nargin != 2 || nargout > 1) |
|
119 { |
5823
|
120 print_usage (); |
5323
|
121 } |
|
122 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
|
123 { |
|
124 SparseComplexMatrix a (args(0).sparse_complex_matrix_value()); |
|
125 SparseComplexMatrix b (args(1).sparse_complex_matrix_value()); |
|
126 |
|
127 if (! error_state) |
|
128 { |
|
129 SparseComplexMatrix c; |
|
130 kron (a, b, c); |
|
131 retval(0) = c; |
|
132 } |
|
133 } |
|
134 else |
|
135 { |
|
136 SparseMatrix a (args(0).sparse_matrix_value ()); |
|
137 SparseMatrix b (args(1).sparse_matrix_value ()); |
|
138 |
|
139 if (! error_state) |
|
140 { |
|
141 SparseMatrix c; |
|
142 kron (a, b, c); |
|
143 retval (0) = c; |
|
144 } |
|
145 } |
|
146 |
|
147 return retval; |
|
148 } |
5610
|
149 |
|
150 /* |
|
151 |
|
152 %!assert(spkron(spdiag([1,2,3]),spdiag([1,2,3])),sparse(kron(diag([1,2,3]),diag([1,2,3])))) |
|
153 %!assert(spkron(spdiag([1i,2,3]),spdiag([1i,2,3])),sparse(kron(diag([1i,2,3]),diag([1i,2,3])))) |
|
154 |
|
155 */ |
|
156 |
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |