Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/find.cc @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | 5b49cafe0599 |
children | 97883071e8e4 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14062
diff
changeset
|
3 Copyright (C) 1996-2012 John W. Eaton |
2928 | 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. | |
2928 | 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/>. | |
2928 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
4153 | 27 #include "quit.h" |
28 | |
2928 | 29 #include "defun-dld.h" |
30 #include "error.h" | |
31 #include "gripes.h" | |
32 #include "oct-obj.h" | |
33 | |
6002 | 34 // Find at most N_TO_FIND nonzero elements in NDA. Search forward if |
35 // DIRECTION is 1, backward if it is -1. NARGOUT is the number of | |
36 // output arguments. If N_TO_FIND is -1, find all nonzero elements. | |
4678 | 37 |
38 template <typename T> | |
39 octave_value_list | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
40 find_nonzero_elem_idx (const Array<T>& nda, int nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
41 octave_idx_type n_to_find, int direction) |
4678 | 42 { |
6002 | 43 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
4678 | 44 |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
45 Array<octave_idx_type> idx; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
46 if (n_to_find >= 0) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
47 idx = nda.find (n_to_find, direction == -1); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
48 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
49 idx = nda.find (); |
5130 | 50 |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
51 // The maximum element is always at the end. |
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
52 octave_idx_type iext = idx.is_empty () ? 0 : idx.xelem (idx.numel () - 1) + 1; |
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
53 |
2928 | 54 switch (nargout) |
55 { | |
6254 | 56 default: |
2928 | 57 case 3: |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9544
diff
changeset
|
58 retval(2) = Array<T> (nda.index (idx_vector (idx))); |
2928 | 59 // Fall through! |
60 | |
61 case 2: | |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
62 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
63 Array<octave_idx_type> jdx (idx.dims ()); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
64 octave_idx_type n = idx.length (), nr = nda.rows (); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
65 for (octave_idx_type i = 0; i < n; i++) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
66 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
67 jdx.xelem (i) = idx.xelem (i) / nr; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
68 idx.xelem (i) %= nr; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
69 } |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
70 iext = -1; |
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
71 retval(1) = idx_vector (jdx, -1); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
72 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
73 // Fall through! |
2928 | 74 |
6254 | 75 case 1: |
76 case 0: | |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
77 retval(0) = idx_vector (idx, iext); |
2928 | 78 break; |
79 } | |
80 | |
81 return retval; | |
82 } | |
83 | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
84 template <typename T> |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
85 octave_value_list |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
86 find_nonzero_elem_idx (const Sparse<T>& v, int nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
87 octave_idx_type n_to_find, int direction) |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
88 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
89 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
90 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
91 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
92 octave_idx_type nc = v.cols(); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
93 octave_idx_type nr = v.rows(); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
94 octave_idx_type nz = v.nnz(); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
95 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
96 // Search in the default range. |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
97 octave_idx_type start_nc = -1; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
98 octave_idx_type end_nc = -1; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
99 octave_idx_type count; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
100 |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
101 // Search for the range to search |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
102 if (n_to_find < 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
103 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
104 start_nc = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
105 end_nc = nc; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
106 n_to_find = nz; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
107 count = nz; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
108 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
109 else if (direction > 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
110 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
111 for (octave_idx_type j = 0; j < nc; j++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
112 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
113 OCTAVE_QUIT; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
114 if (v.cidx(j) == 0 && v.cidx(j+1) != 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
115 start_nc = j; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
116 if (v.cidx(j+1) >= n_to_find) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
117 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
118 end_nc = j + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
119 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
120 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
121 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
122 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
123 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
124 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
125 for (octave_idx_type j = nc; j > 0; j--) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
126 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
127 OCTAVE_QUIT; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
128 if (v.cidx(j) == nz && v.cidx(j-1) != nz) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
129 end_nc = j; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
130 if (nz - v.cidx(j-1) >= n_to_find) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
131 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
132 start_nc = j - 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
133 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
134 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
135 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
136 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
137 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
138 count = (n_to_find > v.cidx(end_nc) - v.cidx(start_nc) ? |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
139 v.cidx(end_nc) - v.cidx(start_nc) : n_to_find); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
140 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
141 // If the original argument was a row vector, force a row vector of |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
142 // the overall indices to be returned. But see below for scalar |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
143 // case... |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
144 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
145 octave_idx_type result_nr = count; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
146 octave_idx_type result_nc = 1; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
147 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
148 bool scalar_arg = false; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
149 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
150 if (v.rows () == 1) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
151 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
152 result_nr = 1; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
153 result_nc = count; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
154 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
155 scalar_arg = (v.columns () == 1); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
156 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
157 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
158 Matrix idx (result_nr, result_nc); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
159 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
160 Matrix i_idx (result_nr, result_nc); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
161 Matrix j_idx (result_nr, result_nc); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
162 |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9544
diff
changeset
|
163 Array<T> val (dim_vector (result_nr, result_nc)); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
164 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
165 if (count > 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
166 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
167 // Search for elements to return. Only search the region where |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
168 // there are elements to be found using the count that we want |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
169 // to find. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
170 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
171 for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++ ) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
172 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
173 OCTAVE_QUIT; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
174 if (direction < 0 && i < nz - count) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
175 continue; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
176 i_idx(cx) = static_cast<double> (v.ridx(i) + 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
177 j_idx(cx) = static_cast<double> (j + 1); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
178 idx(cx) = j * nr + v.ridx(i) + 1; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
179 val(cx) = v.data(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
180 cx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
181 if (cx == count) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
182 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
183 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
184 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
185 else if (scalar_arg) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
186 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
187 idx.resize (0, 0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
188 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
189 i_idx.resize (0, 0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
190 j_idx.resize (0, 0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
191 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
192 val.resize (dim_vector (0, 0)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
193 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
194 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
195 switch (nargout) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
196 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
197 case 0: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
198 case 1: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
199 retval(0) = idx; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
200 break; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
201 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
202 case 5: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
203 retval(4) = nc; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
204 // Fall through |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
205 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
206 case 4: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
207 retval(3) = nr; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
208 // Fall through |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
209 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
210 case 3: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
211 retval(2) = val; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
212 // Fall through! |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
213 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
214 case 2: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
215 retval(1) = j_idx; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
216 retval(0) = i_idx; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
217 break; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
218 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
219 default: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
220 panic_impossible (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
221 break; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
222 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
223 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
224 return retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
225 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
226 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
227 octave_value_list |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
228 find_nonzero_elem_idx (const PermMatrix& v, int nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
229 octave_idx_type n_to_find, int direction) |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
230 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
231 // There are far fewer special cases to handle for a PermMatrix. |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
232 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
233 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
234 octave_idx_type nc = v.cols(); |
13219
cf5ebc0e47e4
fix warnings for unused but set variables and shadowed variables
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
235 octave_idx_type start_nc, count; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
236 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
237 // Determine the range to search. |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
238 if (n_to_find < 0 || n_to_find >= nc) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
239 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
240 start_nc = 0; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
241 n_to_find = nc; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
242 count = nc; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
243 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
244 else if (direction > 0) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
245 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
246 start_nc = 0; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
247 count = n_to_find; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
248 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
249 else |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
250 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
251 start_nc = nc - n_to_find; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
252 count = n_to_find; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
253 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
254 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
255 bool scalar_arg = (v.rows () == 1 && v.cols () == 1); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
256 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
257 Matrix idx (count, 1); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
258 Matrix i_idx (count, 1); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
259 Matrix j_idx (count, 1); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
260 // Every value is 1. |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9544
diff
changeset
|
261 Array<double> val (dim_vector (count, 1), 1.0); |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
262 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
263 if (count > 0) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
264 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
265 const octave_idx_type* p = v.data (); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
266 if (v.is_col_perm ()) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
267 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
268 for (octave_idx_type k = 0; k < count; k++) |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
269 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
270 OCTAVE_QUIT; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
271 const octave_idx_type j = start_nc + k; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
272 const octave_idx_type i = p[j]; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
273 i_idx(k) = static_cast<double> (1+i); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
274 j_idx(k) = static_cast<double> (1+j); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
275 idx(k) = j * nc + i + 1; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
276 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
277 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
278 else |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
279 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
280 for (octave_idx_type k = 0; k < count; k++) |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
281 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
282 OCTAVE_QUIT; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
283 const octave_idx_type i = start_nc + k; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
284 const octave_idx_type j = p[i]; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
285 // Scatter into the index arrays according to |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
286 // j adjusted by the start point. |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
287 const octave_idx_type koff = j - start_nc; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
288 i_idx(koff) = static_cast<double> (1+i); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
289 j_idx(koff) = static_cast<double> (1+j); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
290 idx(koff) = j * nc + i + 1; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
291 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
292 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
293 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
294 else if (scalar_arg) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
295 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
296 // Same odd compatibility case as the other overrides. |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
297 idx.resize (0, 0); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
298 i_idx.resize (0, 0); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
299 j_idx.resize (0, 0); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
300 val.resize (dim_vector (0, 0)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
301 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
302 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
303 switch (nargout) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
304 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
305 case 0: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
306 case 1: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
307 retval(0) = idx; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
308 break; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
309 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
310 case 5: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
311 retval(4) = nc; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
312 // Fall through |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
313 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
314 case 4: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
315 retval(3) = nc; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
316 // Fall through |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
317 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
318 case 3: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
319 retval(2) = val; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
320 // Fall through! |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
321 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
322 case 2: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
323 retval(1) = j_idx; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
324 retval(0) = i_idx; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
325 break; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
326 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
327 default: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
328 panic_impossible (); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
329 break; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
330 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
331 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
332 return retval; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
333 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
334 |
2928 | 335 DEFUN_DLD (find, args, nargout, |
3369 | 336 "-*- texinfo -*-\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
337 @deftypefn {Loadable Function} {@var{idx} =} find (@var{x})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
338 @deftypefnx {Loadable Function} {@var{idx} =} find (@var{x}, @var{n})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
339 @deftypefnx {Loadable Function} {@var{idx} =} find (@var{x}, @var{n}, @var{direction})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
340 @deftypefnx {Loadable Function} {[i, j] =} find (@dots{})\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
341 @deftypefnx {Loadable Function} {[i, j, v] =} find (@dots{})\n\ |
6524 | 342 Return a vector of indices of nonzero elements of a matrix, as a row if\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
343 @var{x} is a row vector or as a column otherwise. To obtain a single index\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
344 for each matrix element, Octave pretends that the columns of a matrix form\n\ |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
345 one long vector (like Fortran arrays are stored). For example:\n\ |
3369 | 346 \n\ |
347 @example\n\ | |
348 @group\n\ | |
349 find (eye (2))\n\ | |
350 @result{} [ 1; 4 ]\n\ | |
351 @end group\n\ | |
352 @end example\n\ | |
353 \n\ | |
354 If two outputs are requested, @code{find} returns the row and column\n\ | |
10840 | 355 indices of nonzero elements of a matrix. For example:\n\ |
3369 | 356 \n\ |
357 @example\n\ | |
358 @group\n\ | |
359 [i, j] = find (2 * eye (2))\n\ | |
360 @result{} i = [ 1; 2 ]\n\ | |
361 @result{} j = [ 1; 2 ]\n\ | |
362 @end group\n\ | |
363 @end example\n\ | |
364 \n\ | |
365 If three outputs are requested, @code{find} also returns a vector\n\ | |
10840 | 366 containing the nonzero values. For example:\n\ |
3369 | 367 \n\ |
368 @example\n\ | |
369 @group\n\ | |
370 [i, j, v] = find (3 * eye (2))\n\ | |
371 @result{} i = [ 1; 2 ]\n\ | |
372 @result{} j = [ 1; 2 ]\n\ | |
373 @result{} v = [ 3; 3 ]\n\ | |
374 @end group\n\ | |
375 @end example\n\ | |
6002 | 376 \n\ |
9310 | 377 If two inputs are given, @var{n} indicates the maximum number of\n\ |
378 elements to find from the beginning of the matrix or vector.\n\ | |
6002 | 379 \n\ |
380 If three inputs are given, @var{direction} should be one of \"first\" or\n\ | |
9310 | 381 \"last\", requesting only the first or last @var{n} indices, respectively.\n\ |
382 However, the indices are always returned in ascending order.\n\ | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
383 \n\ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
384 Note that this function is particularly useful for sparse matrices, as\n\ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
385 it extracts the non-zero elements as vectors, which can then be used to\n\ |
10840 | 386 create the original matrix. For example:\n\ |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
387 \n\ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
388 @example\n\ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
389 @group\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
390 sz = size (a);\n\ |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
391 [i, j, v] = find (a);\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
392 b = sparse (i, j, v, sz(1), sz(2));\n\ |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
393 @end group\n\ |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
394 @end example\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
395 @seealso{nonzeros}\n\ |
3369 | 396 @end deftypefn") |
2928 | 397 { |
398 octave_value_list retval; | |
399 | |
400 int nargin = args.length (); | |
401 | |
6002 | 402 if (nargin > 3 || nargin < 1) |
2928 | 403 { |
5823 | 404 print_usage (); |
2928 | 405 return retval; |
406 } | |
407 | |
6002 | 408 // Setup the default options. |
409 octave_idx_type n_to_find = -1; | |
410 if (nargin > 1) | |
411 { | |
9310 | 412 double val = args(1).scalar_value (); |
413 | |
10338
21dd58bd683c
fix unsafe conversion in find
Jaroslav Hajek <highegg@gmail.com>
parents:
10285
diff
changeset
|
414 if (error_state || (val < 0 || (! xisinf (val) && val != xround (val)))) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
415 { |
14062
5b49cafe0599
Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents:
13915
diff
changeset
|
416 error ("find: N must be a non-negative integer"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
417 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
418 } |
10338
21dd58bd683c
fix unsafe conversion in find
Jaroslav Hajek <highegg@gmail.com>
parents:
10285
diff
changeset
|
419 else if (! xisinf (val)) |
9310 | 420 n_to_find = val; |
6002 | 421 } |
422 | |
423 // Direction to do the searching (1 == forward, -1 == reverse). | |
424 int direction = 1; | |
425 if (nargin > 2) | |
426 { | |
427 direction = 0; | |
428 | |
429 std::string s_arg = args(2).string_value (); | |
430 | |
431 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
432 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
433 if (s_arg == "first") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
434 direction = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
435 else if (s_arg == "last") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
436 direction = -1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
437 } |
6002 | 438 |
439 if (direction == 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
440 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
441 error ("find: DIRECTION must be \"first\" or \"last\""); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
442 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
443 } |
6002 | 444 } |
445 | |
2928 | 446 octave_value arg = args(0); |
447 | |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
448 if (arg.is_bool_type ()) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
449 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
450 if (arg.is_sparse_type ()) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
451 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
452 SparseBoolMatrix v = arg.sparse_bool_matrix_value (); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
453 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
454 if (! error_state) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
455 retval = find_nonzero_elem_idx (v, nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
456 n_to_find, direction); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
457 } |
9544
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
458 else if (nargout <= 1 && n_to_find == -1 && direction == 1) |
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
459 { |
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
460 // This case is equivalent to extracting indices from a logical |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
461 // matrix. Try to reuse the possibly cached index vector. |
9894
83bd7f34f9da
improve idx_vector->octave_value conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
9732
diff
changeset
|
462 retval(0) = arg.index_vector ().unmask (); |
9544
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
463 } |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
464 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
465 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
466 boolNDArray v = arg.bool_array_value (); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
467 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
468 if (! error_state) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
469 retval = find_nonzero_elem_idx (v, nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
470 n_to_find, direction); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
471 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
472 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
473 else if (arg.is_integer_type ()) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
474 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
475 #define DO_INT_BRANCH(INTT) \ |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
476 else if (arg.is_ ## INTT ## _type ()) \ |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
477 { \ |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
478 INTT ## NDArray v = arg.INTT ## _array_value (); \ |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
479 \ |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
480 if (! error_state) \ |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
481 retval = find_nonzero_elem_idx (v, nargout, \ |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
482 n_to_find, direction);\ |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
483 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
484 |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
485 if (false) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
486 ; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
487 DO_INT_BRANCH (int8) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
488 DO_INT_BRANCH (int16) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
489 DO_INT_BRANCH (int32) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
490 DO_INT_BRANCH (int64) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
491 DO_INT_BRANCH (uint8) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
492 DO_INT_BRANCH (uint16) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
493 DO_INT_BRANCH (uint32) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
494 DO_INT_BRANCH (uint64) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
495 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
496 panic_impossible (); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
497 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
498 else if (arg.is_sparse_type ()) |
2928 | 499 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
500 if (arg.is_real_type ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
501 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
502 SparseMatrix v = arg.sparse_matrix_value (); |
2928 | 503 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
504 if (! error_state) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
505 retval = find_nonzero_elem_idx (v, nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
506 n_to_find, direction); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
507 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
508 else if (arg.is_complex_type ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
509 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
510 SparseComplexMatrix v = arg.sparse_complex_matrix_value (); |
5107 | 511 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
512 if (! error_state) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
513 retval = find_nonzero_elem_idx (v, nargout, |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
514 n_to_find, direction); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
515 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
516 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
517 gripe_wrong_type_arg ("find", arg); |
5107 | 518 } |
10550 | 519 else if (arg.is_perm_matrix ()) |
520 { | |
521 PermMatrix P = arg.perm_matrix_value (); | |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
522 |
10550 | 523 if (! error_state) |
524 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction); | |
525 } | |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
526 else if (arg.is_string ()) |
2928 | 527 { |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
528 charNDArray chnda = arg.char_array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
529 |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
530 if (! error_state) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
531 retval = find_nonzero_elem_idx (chnda, nargout, n_to_find, direction); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
532 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
533 else if (arg.is_single_type ()) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
534 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
535 if (arg.is_real_type ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
536 { |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
537 FloatNDArray nda = arg.float_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
538 |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
539 if (! error_state) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
540 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
541 direction); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
542 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
543 else if (arg.is_complex_type ()) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
544 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
545 FloatComplexNDArray cnda = arg.float_complex_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
546 |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
547 if (! error_state) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
548 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
549 direction); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
550 } |
2928 | 551 } |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
552 else if (arg.is_real_type ()) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
553 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
554 NDArray nda = arg.array_value (); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
555 |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
556 if (! error_state) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
557 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
558 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
559 else if (arg.is_complex_type ()) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
560 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
561 ComplexNDArray cnda = arg.complex_array_value (); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
562 |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
563 if (! error_state) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
564 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
565 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
566 else |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
567 gripe_wrong_type_arg ("find", arg); |
2928 | 568 |
569 return retval; | |
570 } | |
571 | |
572 /* | |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
573 %!assert(find (char ([0, 97])), 2); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
574 %!assert(find ([1, 0, 1, 0, 1]), [1, 3, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
575 %!assert(find ([1; 0; 3; 0; 1]), [1; 3; 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
576 %!assert(find ([0, 0, 2; 0, 3, 0; -1, 0, 0]), [3; 5; 7]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
577 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
578 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
579 %! [i, j, v] = find ([0, 0, 2; 0, 3, 0; -1, 0, 0]); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
580 %! |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
581 %! assert(i, [3; 2; 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
582 %! assert(j, [1; 2; 3]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
583 %! assert(v, [-1; 3; 2]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
584 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
585 %!assert(find (single([1, 0, 1, 0, 1])), [1, 3, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
586 %!assert(find (single([1; 0; 3; 0; 1])), [1; 3; 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
587 %!assert(find (single([0, 0, 2; 0, 3, 0; -1, 0, 0])), [3; 5; 7]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
588 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
589 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
590 %! [i, j, v] = find (single([0, 0, 2; 0, 3, 0; -1, 0, 0])); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
591 %! |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
592 %! assert(i, [3; 2; 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
593 %! assert(j, [1; 2; 3]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
594 %! assert(v, single([-1; 3; 2])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
595 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
596 %!test |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
597 %! pcol = [5 1 4 3 2]; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
598 %! P = eye (5) (:, pcol); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
599 %! [i, j, v] = find (P); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
600 %! [ifull, jfull, vfull] = find (full (P)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
601 %! assert (i, ifull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
602 %! assert (j, jfull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
603 %! assert (all (v == 1)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
604 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
605 %!test |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
606 %! prow = [5 1 4 3 2]; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
607 %! P = eye (5) (prow, :); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
608 %! [i, j, v] = find (P); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
609 %! [ifull, jfull, vfull] = find (full (P)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
610 %! assert (i, ifull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
611 %! assert (j, jfull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
612 %! assert (all (v == 1)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
613 |
9310 | 614 %!assert (find ([2 0 1 0 5 0], 1), 1) |
615 %!assert (find ([2 0 1 0 5 0], 2, "last"), [3, 5]) | |
616 | |
617 %!assert (find ([2 0 1 0 5 0], Inf), [1, 3, 5]) | |
618 %!assert (find ([2 0 1 0 5 0], Inf, "last"), [1, 3, 5]) | |
619 | |
13915
5fa482628bf6
Remove unnecessary regular expression '.*' from ends of %!error blocks
Rik <octave@nomad.inbox5.com>
parents:
13219
diff
changeset
|
620 %!error <Invalid call to find> find (); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
621 |
2928 | 622 */ |