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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
|
22 |
|
23 */ |
|
24 |
|
25 // Author: Paul Kienzle <pkienzle@users.sf.net> |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "dSparse.h" |
|
32 #include "CSparse.h" |
|
33 #include "quit.h" |
|
34 |
|
35 #include "defun-dld.h" |
|
36 #include "error.h" |
|
37 #include "oct-obj.h" |
|
38 |
|
39 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
|
40 extern void |
|
41 kron (const Sparse<double>&, const Sparse<double>&, Sparse<double>&); |
|
42 |
|
43 extern void |
|
44 kron (const Sparse<Complex>&, const Sparse<Complex>&, Sparse<Complex>&); |
|
45 #endif |
|
46 |
|
47 template <class T> |
|
48 void |
|
49 kron (const Sparse<T>& A, const Sparse<T>& B, Sparse<T>& C) |
|
50 { |
|
51 octave_idx_type idx = 0; |
|
52 C = Sparse<T> (A.rows () * B.rows (), A.columns () * B.columns (), |
|
53 A.nnz () * B.nnz ()); |
|
54 |
|
55 C.cidx (0) = 0; |
|
56 |
|
57 for (octave_idx_type Aj = 0; Aj < A.columns (); Aj++) |
|
58 for (octave_idx_type Bj = 0; Bj < B.columns (); Bj++) |
|
59 { |
|
60 for (octave_idx_type Ai = A.cidx (Aj); Ai < A.cidx (Aj+1); Ai++) |
|
61 { |
|
62 octave_idx_type Ci = A.ridx(Ai) * B.rows (); |
|
63 const T v = A.data (Ai); |
|
64 |
|
65 for (octave_idx_type Bi = B.cidx (Bj); Bi < B.cidx (Bj+1); Bi++) |
|
66 { |
|
67 OCTAVE_QUIT; |
|
68 C.data (idx) = v * B.data (Bi); |
|
69 C.ridx (idx++) = Ci + B.ridx (Bi); |
|
70 } |
|
71 } |
|
72 C.cidx (Aj * B.columns () + Bj + 1) = idx; |
|
73 } |
|
74 } |
|
75 |
|
76 template void |
|
77 kron (const Sparse<double>&, const Sparse<double>&, Sparse<double>&); |
|
78 |
|
79 template void |
|
80 kron (const Sparse<Complex>&, const Sparse<Complex>&, Sparse<Complex>&); |
|
81 |
|
82 // PKG_ADD: dispatch ("kron", "spkron", "sparse matrix") |
|
83 // PKG_ADD: dispatch ("kron", "spkron", "sparse complex matrix") |
|
84 // PKG_ADD: dispatch ("kron", "spkron", "sparse bool matrix") |
|
85 DEFUN_DLD (spkron, args, nargout, "-*- texinfo -*-\n\ |
|
86 @deftypefn {Function File} {} spkron (@var{a}, @var{b})\n\ |
|
87 Form the kronecker product of two sparse matrices. This is defined\n\ |
|
88 block by block as\n\ |
|
89 \n\ |
|
90 @example\n\ |
|
91 x = [a(i, j) b]\n\ |
|
92 @end example\n\ |
|
93 \n\ |
|
94 For example,\n\ |
|
95 \n\ |
|
96 @example\n\ |
|
97 @group\n\ |
|
98 kron (1:4, ones (3, 1))\n\ |
|
99 @result{} 1 2 3 4\n\ |
|
100 1 2 3 4\n\ |
|
101 1 2 3 4\n\ |
|
102 @end group\n\ |
|
103 @end example\n\ |
|
104 @end deftypefn") |
|
105 { |
|
106 octave_value_list retval; |
|
107 |
|
108 int nargin = args.length (); |
|
109 |
|
110 if (nargin != 2 || nargout > 1) |
|
111 { |
|
112 print_usage ("kron"); |
|
113 } |
|
114 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
|
115 { |
|
116 SparseComplexMatrix a (args(0).sparse_complex_matrix_value()); |
|
117 SparseComplexMatrix b (args(1).sparse_complex_matrix_value()); |
|
118 |
|
119 if (! error_state) |
|
120 { |
|
121 SparseComplexMatrix c; |
|
122 kron (a, b, c); |
|
123 retval(0) = c; |
|
124 } |
|
125 } |
|
126 else |
|
127 { |
|
128 SparseMatrix a (args(0).sparse_matrix_value ()); |
|
129 SparseMatrix b (args(1).sparse_matrix_value ()); |
|
130 |
|
131 if (! error_state) |
|
132 { |
|
133 SparseMatrix c; |
|
134 kron (a, b, c); |
|
135 retval (0) = c; |
|
136 } |
|
137 } |
|
138 |
|
139 return retval; |
|
140 } |