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