annotate src/DLD-FUNCTIONS/find.cc @ 10155:d0ce5e973937

DLD-FUNCTIONS/*.cc: delete Emacs local-variables settings
author John W. Eaton <jwe@octave.org>
date Wed, 20 Jan 2010 17:40:53 -0500
parents 40dfc0c99116
children 22a7913bbeb5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
1 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, 2006,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8810
diff changeset
4 2007, 2008, 2009 John W. Eaton
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6524
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6524
diff changeset
11 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
12
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6524
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6524
diff changeset
20 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
21
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
22 */
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
23
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
27
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3418
diff changeset
28 #include "quit.h"
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3418
diff changeset
29
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
30 #include "defun-dld.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
32 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
33 #include "oct-obj.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
34
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
35 // Find at most N_TO_FIND nonzero elements in NDA. Search forward if
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
36 // DIRECTION is 1, backward if it is -1. NARGOUT is the number of
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
37 // output arguments. If N_TO_FIND is -1, find all nonzero elements.
4678
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
38
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
39 template <typename T>
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
40 octave_value_list
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
41 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
42 octave_idx_type n_to_find, int direction)
4678
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
43 {
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
44 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ());
4678
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
45
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
46 Array<octave_idx_type> idx;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
47 if (n_to_find >= 0)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
48 idx = nda.find (n_to_find, direction == -1);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
49 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
50 idx = nda.find ();
5130
3bf4c0cd775d [project @ 2005-02-08 03:01:13 by jwe]
jwe
parents: 5107
diff changeset
51
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
52 switch (nargout)
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
53 {
6254
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
54 default:
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
55 case 3:
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
56 retval(2) = Array<T> (nda.index (idx_vector (idx)));
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
57 // Fall through!
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
58
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
59 case 2:
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
60 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
61 Array<octave_idx_type> jdx (idx.dims ());
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
62 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
63 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
64 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
65 jdx.xelem (i) = idx.xelem (i) / nr;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
66 idx.xelem (i) %= nr;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
67 }
9351
e2344f4af0cb autocache indices returned from find et al.
Jaroslav Hajek <highegg@gmail.com>
parents: 9310
diff changeset
68 retval(1) = octave_value (jdx, true, true);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
69 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
70 // Fall through!
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
71
6254
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
72 case 1:
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
73 case 0:
9351
e2344f4af0cb autocache indices returned from find et al.
Jaroslav Hajek <highegg@gmail.com>
parents: 9310
diff changeset
74 retval(0) = octave_value (idx, true, true);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
75 break;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
76 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
77
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
78 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
79 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
80
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
81 #define INSTANTIATE_FIND_ARRAY(T) \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
82 template octave_value_list find_nonzero_elem_idx (const Array<T>&, int, \
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
83 octave_idx_type, int)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
84
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
85 INSTANTIATE_FIND_ARRAY(double);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
86 INSTANTIATE_FIND_ARRAY(float);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
87 INSTANTIATE_FIND_ARRAY(Complex);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
88 INSTANTIATE_FIND_ARRAY(FloatComplex);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
89 INSTANTIATE_FIND_ARRAY(bool);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
90 INSTANTIATE_FIND_ARRAY(octave_int8);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
91 INSTANTIATE_FIND_ARRAY(octave_int16);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
92 INSTANTIATE_FIND_ARRAY(octave_int32);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
93 INSTANTIATE_FIND_ARRAY(octave_int64);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
94 INSTANTIATE_FIND_ARRAY(octave_uint8);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
95 INSTANTIATE_FIND_ARRAY(octave_uint16);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
96 INSTANTIATE_FIND_ARRAY(octave_uint32);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
97 INSTANTIATE_FIND_ARRAY(octave_uint64);
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
98
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
99 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
100 octave_value_list
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
101 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
102 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
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 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
105
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
106
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
107 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
108 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
109 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
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 // 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
112 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
113 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
114 octave_idx_type count;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
115
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
116 // 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
117 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
118 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
119 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
120 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
121 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
122 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
123 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
124 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
125 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
126 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
127 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
128 OCTAVE_QUIT;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
129 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
130 start_nc = j;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
131 if (v.cidx(j+1) >= n_to_find)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
132 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
133 end_nc = j + 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
134 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
135 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
136 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
137 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
138 else
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
139 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
140 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
141 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
142 OCTAVE_QUIT;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
143 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
144 end_nc = j;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
145 if (nz - v.cidx(j-1) >= n_to_find)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
146 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
147 start_nc = j - 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
148 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
149 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
150 }
7505
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
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
153 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
154 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
155
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
156 // 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
157 // 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
158 // case...
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 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
161 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
162
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
163 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
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 (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
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 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
168 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
169
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
170 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
171 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
172
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
173 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
174
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
175 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
176 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
177
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
178 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
179
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
180 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
181 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
182 // 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
183 // 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
184 // to find.
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
185 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
186 for (octave_idx_type i = v.cidx(j); i < v.cidx(j+1); i++ )
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
187 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
188 OCTAVE_QUIT;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
189 if (direction < 0 && i < nz - count)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
190 continue;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
191 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
192 j_idx(cx) = static_cast<double> (j + 1);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
193 idx(cx) = j * nr + v.ridx(i) + 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
194 val(cx) = v.data(i);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
195 cx++;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
196 if (cx == count)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
197 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
198 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
199 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
200 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
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 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
203
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
204 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
205 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
206
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
207 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
208 }
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 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
211 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
212 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
213 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
214 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
215 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
216
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
217 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
218 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
219 // 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
220
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
221 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
222 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
223 // 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
224
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
225 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
226 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
227 // 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
228
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
229 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
230 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
231 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
232 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
233
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
234 default:
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
235 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
236 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
237 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
238
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
239 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
240 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
241
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
242 template octave_value_list find_nonzero_elem_idx (const Sparse<double>&, int,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
243 octave_idx_type, int);
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
244
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
245 template octave_value_list find_nonzero_elem_idx (const Sparse<Complex>&, int,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
246 octave_idx_type, int);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
247
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
248 template octave_value_list find_nonzero_elem_idx (const Sparse<bool>&, int,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
249 octave_idx_type, int);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
250
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
251 octave_value_list
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
252 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
253 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
254 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
255 // 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
256 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
257
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
258 octave_idx_type nc = v.cols();
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
259 octave_idx_type start_nc, end_nc, count;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
260
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
261 // 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
262 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
263 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
264 start_nc = 0;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
265 end_nc = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
266 n_to_find = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
267 count = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
268 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
269 else if (direction > 0)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
270 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
271 start_nc = 0;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
272 end_nc = n_to_find;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
273 count = n_to_find;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
274 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
275 else
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 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
278 end_nc = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
279 count = n_to_find;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
280 }
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 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
283
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
284 Matrix idx (count, 1);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
285 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
286 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
287 // Every value is 1.
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
288 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
289
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
290 if (count > 0)
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 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
293 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
294 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
295 for (octave_idx_type k = 0; k < count; k++)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
296 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
297 OCTAVE_QUIT;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
298 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
299 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
300 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
301 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
302 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
303 }
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 else
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
306 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
307 for (octave_idx_type k = 0; k < count; k++)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
308 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
309 OCTAVE_QUIT;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
310 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
311 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
312 // 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
313 // 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
314 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
315 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
316 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
317 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
318 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
319 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
320 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
321 else if (scalar_arg)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
322 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
323 // 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
324 idx.resize (0, 0);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
325 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
326 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
327 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
328 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
329
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
330 switch (nargout)
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 case 0:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
333 case 1:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
334 retval(0) = idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
335 break;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
336
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
337 case 5:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
338 retval(4) = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
339 // Fall through
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
340
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
341 case 4:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
342 retval(3) = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
343 // Fall through
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
344
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
345 case 3:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
346 retval(2) = val;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
347 // Fall through!
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
348
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
349 case 2:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
350 retval(1) = j_idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
351 retval(0) = i_idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
352 break;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
353
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
354 default:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
355 panic_impossible ();
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
356 break;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
357 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
358
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
359 return retval;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
360 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
361
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
362 DEFUN_DLD (find, args, nargout,
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
363 "-*- texinfo -*-\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
364 @deftypefn {Loadable Function} {} find (@var{x})\n\
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
365 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n})\n\
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
366 @deftypefnx {Loadable Function} {} find (@var{x}, @var{n}, @var{direction})\n\
6524
03c89ade4070 [project @ 2007-04-13 12:19:30 by jwe]
jwe
parents: 6254
diff changeset
367 Return a vector of indices of nonzero elements of a matrix, as a row if\n\
03c89ade4070 [project @ 2007-04-13 12:19:30 by jwe]
jwe
parents: 6254
diff changeset
368 @var{x} is a row or as a column otherwise. To obtain a single index for\n\
03c89ade4070 [project @ 2007-04-13 12:19:30 by jwe]
jwe
parents: 6254
diff changeset
369 each matrix element, Octave pretends that the columns of a matrix form one\n\
03c89ade4070 [project @ 2007-04-13 12:19:30 by jwe]
jwe
parents: 6254
diff changeset
370 long vector (like Fortran arrays are stored). For example,\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
371 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
372 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
373 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
374 find (eye (2))\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
375 @result{} [ 1; 4 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
376 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
377 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
378 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
379 If two outputs are requested, @code{find} returns the row and column\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
380 indices of nonzero elements of a matrix. For example,\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
381 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
382 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
383 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
384 [i, j] = find (2 * eye (2))\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
385 @result{} i = [ 1; 2 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
386 @result{} j = [ 1; 2 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
387 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
388 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
389 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
390 If three outputs are requested, @code{find} also returns a vector\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
391 containing the nonzero values. For example,\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
392 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
393 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
394 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
395 [i, j, v] = find (3 * eye (2))\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
396 @result{} i = [ 1; 2 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
397 @result{} j = [ 1; 2 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
398 @result{} v = [ 3; 3 ]\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
399 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
400 @end example\n\
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
401 \n\
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
402 If two inputs are given, @var{n} indicates the maximum number of\n\
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
403 elements to find from the beginning of the matrix or vector.\n\
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
404 \n\
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
405 If three inputs are given, @var{direction} should be one of \"first\" or\n\
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
406 \"last\", requesting only the first or last @var{n} indices, respectively.\n\
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
407 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
408 \n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
409 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
410 it extracts the non-zero elements as vectors, which can then be used to\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8955
diff changeset
411 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
412 \n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
413 @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
414 @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
415 sz = size(a);\n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
416 [i, j, v] = find (a);\n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
417 b = sparse(i, j, v, sz(1), sz(2));\n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
418 @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
419 @end 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
420 @seealso{sparse}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
421 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
422 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
423 octave_value_list retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
424
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
425 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
426
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
427 if (nargin > 3 || nargin < 1)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
428 {
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5307
diff changeset
429 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
430 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
431 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
432
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
433 // Setup the default options.
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
434 octave_idx_type n_to_find = -1;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
435 if (nargin > 1)
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
436 {
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
437 double val = args(1).scalar_value ();
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
438
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
439 if (error_state || (! xisinf (val) && (val < 0 || val != xround (val))))
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
440 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
441 error ("find: expecting second argument to be a nonnegative integer");
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 }
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
444 else
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
445 n_to_find = val;
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
446 }
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
447
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
448 // Direction to do the searching (1 == forward, -1 == reverse).
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
449 int direction = 1;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
450 if (nargin > 2)
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
451 {
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
452 direction = 0;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
453
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
454 std::string s_arg = args(2).string_value ();
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
455
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
456 if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
457 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
458 if (s_arg == "first")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
459 direction = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
460 else if (s_arg == "last")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
461 direction = -1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
462 }
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
463
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
464 if (direction == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
465 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
466 error ("find: expecting third argument to be \"first\" or \"last\"");
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
467 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
468 }
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
469 }
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
470
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
471 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
472
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
473 if (arg.is_bool_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 if (arg.is_sparse_type ())
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
476 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
477 SparseBoolMatrix v = arg.sparse_bool_matrix_value ();
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
478
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
479 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
480 retval = find_nonzero_elem_idx (v, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
481 n_to_find, direction);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
482 }
9544
df0abc63c230 cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents: 9351
diff changeset
483 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
484 {
df0abc63c230 cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents: 9351
diff changeset
485 // This case is equivalent to extracting indices from a logical
9894
83bd7f34f9da improve idx_vector->octave_value conversion
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
486 // matrix. Try to reuse the possibly cached index vector.
83bd7f34f9da improve idx_vector->octave_value conversion
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
487 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
488 }
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
489 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
490 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
491 boolNDArray v = arg.bool_array_value ();
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
492
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
493 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
494 retval = find_nonzero_elem_idx (v, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
495 n_to_find, direction);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
496 }
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_integer_type ())
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
499 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
500 #define DO_INT_BRANCH(INTT) \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
501 else if (arg.is_ ## INTT ## _type ()) \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
502 { \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
503 INTT ## NDArray v = arg.INTT ## _array_value (); \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
504 \
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
505 if (! error_state) \
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
506 retval = find_nonzero_elem_idx (v, nargout, \
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
507 n_to_find, direction);\
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
508 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
509
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
510 if (false)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
511 ;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
512 DO_INT_BRANCH (int8)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
513 DO_INT_BRANCH (int16)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
514 DO_INT_BRANCH (int32)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
515 DO_INT_BRANCH (int64)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
516 DO_INT_BRANCH (uint8)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
517 DO_INT_BRANCH (uint16)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
518 DO_INT_BRANCH (uint32)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
519 DO_INT_BRANCH (uint64)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
520 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
521 panic_impossible ();
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
522 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
523 else if (arg.is_sparse_type ())
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
524 {
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
525 if (arg.is_real_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
526 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
527 SparseMatrix v = arg.sparse_matrix_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
528
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
529 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
530 retval = find_nonzero_elem_idx (v, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
531 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
532 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
533 else if (arg.is_complex_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
534 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
535 SparseComplexMatrix v = arg.sparse_complex_matrix_value ();
5107
c891dd97b837 [project @ 2005-01-12 19:49:18 by jwe]
jwe
parents: 4826
diff changeset
536
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
537 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
538 retval = find_nonzero_elem_idx (v, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
539 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
540 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
541 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
542 gripe_wrong_type_arg ("find", arg);
5107
c891dd97b837 [project @ 2005-01-12 19:49:18 by jwe]
jwe
parents: 4826
diff changeset
543 }
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
544 else if (arg.is_perm_matrix ()) {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
545 PermMatrix P = arg.perm_matrix_value ();
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
546
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
547 if (! error_state)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
548 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
549 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
550 else
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
551 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
552 if (arg.is_single_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
553 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
554 if (arg.is_real_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
555 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
556 FloatNDArray nda = arg.float_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
557
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
558 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
559 retval = find_nonzero_elem_idx (nda, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
560 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
561 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
562 else if (arg.is_complex_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
563 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
564 FloatComplexNDArray cnda = arg.float_complex_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
565
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
566 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
567 retval = find_nonzero_elem_idx (cnda, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
568 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
569 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
570 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
571 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
572 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
573 if (arg.is_real_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
574 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
575 NDArray nda = arg.array_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
576
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
577 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
578 retval = find_nonzero_elem_idx (nda, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
579 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
580 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
581 else if (arg.is_complex_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
582 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
583 ComplexNDArray cnda = arg.complex_array_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
584
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
585 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
586 retval = find_nonzero_elem_idx (cnda, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
587 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
588 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
589 else if (arg.is_string ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
590 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
591 charNDArray cnda = arg.char_array_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
592
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
593 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
594 retval = find_nonzero_elem_idx (cnda, nargout,
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
595 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
596 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
597 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
598 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
599 gripe_wrong_type_arg ("find", arg);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
600 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
601 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
602 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
603
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
604 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
605 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
606
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
607 /*
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
608 %!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
609 %!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
610 %!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
611
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
612 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
613 %! [i, j, v] = find ([0, 0, 2; 0, 3, 0; -1, 0, 0]);
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
614 %!
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
615 %! 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
616 %! 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
617 %! 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
618
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
619 %!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
620 %!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
621 %!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
622
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
623 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
624 %! [i, j, v] = find (single([0, 0, 2; 0, 3, 0; -1, 0, 0]));
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
625 %!
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
626 %! 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
627 %! 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
628 %! 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
629
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
630 %!test
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
631 %! 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
632 %! P = eye (5) (:, pcol);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
633 %! [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
634 %! [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
635 %! assert (i, ifull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
636 %! assert (j, jfull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
637 %! assert (all (v == 1));
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
638
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
639 %!test
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
640 %! 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
641 %! P = eye (5) (prow, :);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
642 %! [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
643 %! [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
644 %! assert (i, ifull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
645 %! assert (j, jfull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
646 %! assert (all (v == 1));
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
647
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
648 %!assert (find ([2 0 1 0 5 0], 1), 1)
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
649 %!assert (find ([2 0 1 0 5 0], 2, "last"), [3, 5])
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
650
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
651 %!assert (find ([2 0 1 0 5 0], Inf), [1, 3, 5])
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
652 %!assert (find ([2 0 1 0 5 0], Inf, "last"), [1, 3, 5])
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
653
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
654 %!error <Invalid call to find.*> find ();
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
655
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
656 */