6011
|
1 /* |
|
2 |
|
3 Copyright (C) 2006 David Bateman |
|
4 |
7016
|
5 This file is part of Octave. |
|
6 |
6011
|
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. |
6011
|
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/>. |
6011
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstdlib> |
|
28 #include <string> |
|
29 |
|
30 #include "variables.h" |
|
31 #include "utils.h" |
|
32 #include "pager.h" |
|
33 #include "defun-dld.h" |
|
34 #include "gripes.h" |
|
35 #include "quit.h" |
|
36 |
|
37 #include "ov-re-sparse.h" |
|
38 #include "ov-cx-sparse.h" |
|
39 |
|
40 template <typename T, typename M> |
|
41 octave_value_list |
|
42 sparse_find_non_zero_elem_idx (const T& v, M& val, int nargout, |
|
43 octave_idx_type n_to_find, int direction) |
|
44 { |
|
45 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
|
46 |
|
47 |
|
48 octave_idx_type nc = v.cols(); |
|
49 octave_idx_type nr = v.rows(); |
|
50 octave_idx_type nz = v.nnz(); |
|
51 |
|
52 // Search in the default range. |
|
53 octave_idx_type start_nc = -1; |
|
54 octave_idx_type end_nc = -1; |
|
55 octave_idx_type count; |
|
56 |
|
57 // Search for the range to search |
|
58 if (n_to_find < 0) |
|
59 { |
|
60 start_nc = 0; |
|
61 end_nc = nc; |
|
62 n_to_find = nz; |
|
63 count = nz; |
|
64 } |
|
65 else if (direction > 0) |
|
66 { |
|
67 for (octave_idx_type j = 0; j < nc; j++) |
|
68 { |
|
69 OCTAVE_QUIT; |
|
70 if (v.cidx(j) == 0 && v.cidx(j+1) != 0) |
|
71 start_nc = j; |
|
72 if (v.cidx(j+1) >= n_to_find) |
|
73 { |
|
74 end_nc = j + 1; |
|
75 break; |
|
76 } |
|
77 } |
|
78 } |
|
79 else |
|
80 { |
|
81 for (octave_idx_type j = nc; j > 0; j--) |
|
82 { |
|
83 OCTAVE_QUIT; |
|
84 if (v.cidx(j) == nz && v.cidx(j-1) != nz) |
|
85 end_nc = j; |
|
86 if (nz - v.cidx(j-1) >= n_to_find) |
|
87 { |
|
88 start_nc = j - 1; |
|
89 break; |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 count = (n_to_find > v.cidx(end_nc) - v.cidx(start_nc) ? |
|
95 v.cidx(end_nc) - v.cidx(start_nc) : n_to_find); |
|
96 |
|
97 // If the original argument was a row vector, force a row vector of |
|
98 // the overall indices to be returned. But see below for scalar |
|
99 // case... |
|
100 |
|
101 octave_idx_type result_nr = count; |
|
102 octave_idx_type result_nc = 1; |
|
103 |
|
104 bool scalar_arg = false; |
|
105 |
|
106 if (v.rows () == 1) |
|
107 { |
|
108 result_nr = 1; |
|
109 result_nc = count; |
|
110 |
|
111 scalar_arg = (v.columns () == 1); |
|
112 } |
|
113 |
|
114 Matrix idx (result_nr, result_nc); |
|
115 |
|
116 Matrix i_idx (result_nr, result_nc); |
|
117 Matrix j_idx (result_nr, result_nc); |
|
118 |
|
119 val = M(result_nr, result_nc); |
|
120 |
|
121 if (count > 0) |
|
122 { |
|
123 // Search for elements to return. Only search the region where |
|
124 // there are elements to be found using the count that we want |
|
125 // to find. |
|
126 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++) |
|
127 for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++ ) |
|
128 { |
|
129 OCTAVE_QUIT; |
|
130 if (direction < 0 && i < nz - count) |
|
131 continue; |
|
132 i_idx (cx) = static_cast<double> (v.ridx(i) + 1); |
|
133 j_idx (cx) = static_cast<double> (j + 1); |
6013
|
134 idx (cx) = j * nr + v.ridx(i) + 1; |
6011
|
135 val (cx) = v.data(i); |
|
136 cx++; |
|
137 if (cx == count) |
|
138 break; |
|
139 } |
|
140 } |
|
141 else if (scalar_arg) |
|
142 { |
|
143 idx.resize (0, 0); |
|
144 |
|
145 i_idx.resize (0, 0); |
|
146 j_idx.resize (0, 0); |
|
147 |
|
148 val.resize (0, 0); |
|
149 } |
|
150 |
|
151 switch (nargout) |
|
152 { |
|
153 case 0: |
|
154 case 1: |
|
155 retval(0) = idx; |
|
156 break; |
|
157 |
|
158 case 5: |
|
159 retval(4) = nc; |
|
160 // Fall through |
|
161 |
|
162 case 4: |
|
163 retval(3) = nr; |
|
164 // Fall through |
|
165 |
|
166 case 3: |
|
167 retval(2) = val; |
|
168 // Fall through! |
|
169 |
|
170 case 2: |
|
171 retval(1) = j_idx; |
|
172 retval(0) = i_idx; |
|
173 break; |
|
174 |
|
175 default: |
|
176 panic_impossible (); |
|
177 break; |
|
178 } |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 template octave_value_list sparse_find_non_zero_elem_idx |
|
184 (const SparseMatrix&, Matrix&, int, octave_idx_type, int); |
|
185 |
|
186 template octave_value_list sparse_find_non_zero_elem_idx |
|
187 (const SparseComplexMatrix&, ComplexMatrix&, int, octave_idx_type, int); |
|
188 |
|
189 |
|
190 // PKG_ADD: dispatch ("find", "spfind", "sparse matrix"); |
|
191 // PKG_ADD: dispatch ("find", "spfind", "sparse complex matrix"); |
|
192 // PKG_ADD: dispatch ("find", "spfind", "sparse bool matrix"); |
|
193 DEFUN_DLD (spfind, args, nargout, |
|
194 "-*- texinfo -*-\n\ |
|
195 @deftypefn {Loadable Function} {} spfind (@var{x})\n\ |
|
196 @deftypefnx {Loadable Function} {} spfind (@var{x}, @var{n})\n\ |
|
197 @deftypefnx {Loadable Function} {} spfind (@var{x}, @var{n}, @var{direction})\n\ |
|
198 @deftypefnx {Loadable Function} {[@var{i}, @var{j}, @var{v}} spfind (@dots{})\n\ |
|
199 \n\ |
|
200 A sparse version of the @code{find} function. Please see the @code{find}\n\ |
|
201 for details of its use.\n\ |
|
202 \n\ |
|
203 Note that this function is particularly useful for sparse matrices, as\n\ |
|
204 it extracts the non-zero elements as vectors, which can then be used to\n\ |
|
205 create the original matrix. For example,\n\ |
|
206 \n\ |
|
207 @example\n\ |
|
208 @group\n\ |
|
209 sz = size(a);\n\ |
|
210 [i, j, v] = spfind (a);\n\ |
|
211 b = sparse(i, j, v, sz(1), sz(2));\n\ |
|
212 @end group\n\ |
|
213 @end example\n\ |
|
214 @seealso{sparse}\n\ |
|
215 @end deftypefn") |
|
216 { |
|
217 octave_value_list retval; |
|
218 int nargin = args.length (); |
|
219 |
|
220 if (nargin > 3 || nargin < 1) |
|
221 { |
|
222 print_usage (); |
|
223 return retval; |
|
224 } |
|
225 |
|
226 // Setup the default options. |
|
227 octave_idx_type n_to_find = -1; |
|
228 if (nargin > 1) |
|
229 { |
|
230 n_to_find = args(1).int_value (); |
|
231 if (error_state) |
|
232 { |
|
233 error ("find: expecting second argument to be an integer"); |
|
234 return retval; |
|
235 } |
|
236 } |
|
237 |
|
238 // Direction to do the searching (1 == forward, -1 == reverse). |
|
239 int direction = 1; |
|
240 if (nargin > 2) |
|
241 { |
|
242 direction = 0; |
|
243 |
|
244 std::string s_arg = args(2).string_value (); |
|
245 |
|
246 if (! error_state) |
|
247 { |
|
248 if (s_arg == "first") |
|
249 direction = 1; |
|
250 else if (s_arg == "last") |
|
251 direction = -1; |
|
252 } |
|
253 |
|
254 if (direction == 0) |
|
255 { |
|
256 error ("find: expecting third argument to be \"first\" or \"last\""); |
|
257 return retval; |
|
258 } |
|
259 } |
|
260 |
|
261 octave_value arg = args(0); |
|
262 |
|
263 if (arg.is_real_type ()) |
|
264 { |
|
265 SparseMatrix v = arg.sparse_matrix_value (); |
|
266 Matrix val; |
|
267 |
|
268 if (! error_state) |
|
269 retval = sparse_find_non_zero_elem_idx (v, val, nargout, n_to_find, direction); |
|
270 } |
|
271 else if (arg.is_complex_type ()) |
|
272 { |
|
273 SparseComplexMatrix v = arg.sparse_complex_matrix_value (); |
|
274 ComplexMatrix val; |
|
275 |
|
276 if (! error_state) |
|
277 retval = sparse_find_non_zero_elem_idx (v, val, nargout, n_to_find, direction); |
|
278 } |
|
279 else |
|
280 gripe_wrong_type_arg ("spfind", arg); |
|
281 |
|
282 return retval; |
|
283 } |
|
284 |
|
285 /* |
|
286 ;;; Local Variables: *** |
|
287 ;;; mode: C++ *** |
|
288 ;;; End: *** |
|
289 */ |