annotate libinterp/corefcn/find.cc @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents a9574e3c6e9e
children b10432a40432
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
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18883
diff changeset
3 Copyright (C) 1996-2015 John W. Eaton
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
4
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7 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
8 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
9 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
10 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
11
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
12 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
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 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
15 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17 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
18 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
19 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
20
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 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
24 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3418
diff changeset
27 #include "quit.h"
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3418
diff changeset
28
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
29 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
30 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
32 #include "oct-obj.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
33
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
34 // 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
35 // 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
36 // 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
37
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
38 template <typename T>
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
39 octave_value_list
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
40 find_nonzero_elem_idx (const Array<T>& nda, int nargout,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
41 octave_idx_type n_to_find, int direction)
4678
e1c2d8ca8bc0 [project @ 2003-12-17 00:36:36 by jwe]
jwe
parents: 4628
diff changeset
42 {
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
43 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
44
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
45 Array<octave_idx_type> idx;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
46 if (n_to_find >= 0)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
47 idx = nda.find (n_to_find, direction == -1);
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
48 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
49 idx = nda.find ();
5130
3bf4c0cd775d [project @ 2005-02-08 03:01:13 by jwe]
jwe
parents: 5107
diff changeset
50
10285
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
51 // The maximum element is always at the end.
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
52 octave_idx_type iext = idx.is_empty () ? 0 : idx.xelem (idx.numel () - 1) + 1;
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
53
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
54 switch (nargout)
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
55 {
6254
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
56 default:
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
57 case 3:
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
58 retval(2) = Array<T> (nda.index (idx_vector (idx)));
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
59 // Fall through!
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
60
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
61 case 2:
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
62 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
63 Array<octave_idx_type> jdx (idx.dims ());
20267
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20216
diff changeset
64 octave_idx_type n = idx.numel ();
18099
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
65 octave_idx_type nr = nda.rows ();
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
66 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
67 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
68 jdx.xelem (i) = idx.xelem (i) / nr;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
69 idx.xelem (i) %= nr;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
70 }
10285
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
71 iext = -1;
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
72 retval(1) = idx_vector (jdx, -1);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
73 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
74 // Fall through!
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
75
6254
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
76 case 1:
5b43c2332b69 [project @ 2007-01-29 17:35:15 by jwe]
jwe
parents: 6026
diff changeset
77 case 0:
10285
22a7913bbeb5 optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
78 retval(0) = idx_vector (idx, iext);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
79 break;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
80 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
81
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
82 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
83 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
84
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
85 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
86 octave_value_list
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
87 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
88 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
89 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
90 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
91
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
92 octave_idx_type nr = v.rows ();
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
93 octave_idx_type nc = v.cols ();
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
94 octave_idx_type nz = v.nnz ();
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
95
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
96 // Search in the default range.
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
97 octave_idx_type start_nc = -1;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
98 octave_idx_type end_nc = -1;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
99 octave_idx_type count;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
100
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
101 // Search for the range to search
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
102 if (n_to_find < 0)
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
103 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
104 start_nc = 0;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
105 end_nc = nc;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
106 n_to_find = nz;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
107 count = nz;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
108 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
109 else if (direction > 0)
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
110 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
111 for (octave_idx_type j = 0; j < nc; j++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
112 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
113 OCTAVE_QUIT;
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
114 if (v.cidx (j) == 0 && v.cidx (j+1) != 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
115 start_nc = j;
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
116 if (v.cidx (j+1) >= n_to_find)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
117 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
118 end_nc = j + 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
119 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
120 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
121 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
122 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
123 else
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
124 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
125 for (octave_idx_type j = nc; j > 0; j--)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
126 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
127 OCTAVE_QUIT;
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
128 if (v.cidx (j) == nz && v.cidx (j-1) != nz)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
129 end_nc = j;
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
130 if (nz - v.cidx (j-1) >= n_to_find)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
131 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
132 start_nc = j - 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
133 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
134 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
135 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
136 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
137
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
138 count = (n_to_find > v.cidx (end_nc) - v.cidx (start_nc) ?
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
139 v.cidx (end_nc) - v.cidx (start_nc) : n_to_find);
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
140
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
141 octave_idx_type result_nr;
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
142 octave_idx_type 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
143
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
144 // Default case is to return a column vector, however, if the original
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
145 // argument was a row vector, then force return of a row vector.
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
146 if (nr == 1)
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
147 {
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
148 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
149 result_nc = count;
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
150 }
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
151 else
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
152 {
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
153 result_nr = count;
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
154 result_nc = 1;
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
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
157 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
158
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
159 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
160 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
161
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
162 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
163
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
164 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
165 {
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
166 // Search for elements to return. Only search the region where there
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
167 // are elements to be found using the count that we want to find.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
168 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++)
18712
6113e0c6920b maint: Clean up extra spaces before/after parentheses.
Rik <rik@octave.org>
parents: 18710
diff changeset
169 for (octave_idx_type i = v.cidx (j); i < v.cidx (j+1); i++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
170 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
171 OCTAVE_QUIT;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
172 if (direction < 0 && i < nz - count)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
173 continue;
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
174 i_idx(cx) = static_cast<double> (v.ridx (i) + 1);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
175 j_idx(cx) = static_cast<double> (j + 1);
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
176 idx(cx) = j * nr + v.ridx (i) + 1;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
177 val(cx) = v.data(i);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
178 cx++;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
179 if (cx == count)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
180 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
181 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
182 }
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
183 else
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
184 {
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
185 // No items found. Fixup return dimensions for Matlab compatibility.
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
186 // The behavior to match is documented in Array.cc (Array<T>::find).
18826
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
187 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1))
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
188 {
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
189 idx.resize (0, 0);
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
190
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
191 i_idx.resize (0, 0);
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
192 j_idx.resize (0, 0);
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
193
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
194 val.resize (dim_vector (0, 0));
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
195 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
196 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
197
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
198 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
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 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
201 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
202 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
203 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
204
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
205 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
206 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
207 // 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
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 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
210 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
211 // 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
212
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 3:
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(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
215 // 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
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 2:
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(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
219 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
220 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
221
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
222 default:
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
223 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
224 break;
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
225 }
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
226
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
227 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
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
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
230 octave_value_list
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
231 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
232 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
233 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
234 // 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
235 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
236
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
237 octave_idx_type nr = v.rows ();
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14501
diff changeset
238 octave_idx_type nc = v.cols ();
13219
cf5ebc0e47e4 fix warnings for unused but set variables and shadowed variables
John W. Eaton <jwe@octave.org>
parents: 12639
diff changeset
239 octave_idx_type start_nc, count;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
240
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
241 // 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
242 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
243 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
244 start_nc = 0;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
245 count = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
246 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
247 else if (direction > 0)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
248 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
249 start_nc = 0;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
250 count = n_to_find;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
251 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
252 else
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
253 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
254 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
255 count = n_to_find;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
256 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
257
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
258 Matrix idx (count, 1);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
259 Matrix i_idx (count, 1);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
260 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
261 // Every value is 1.
9732
b4fdfee405b5 remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents: 9544
diff changeset
262 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
263
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
264 if (count > 0)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
265 {
18883
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
266 const Array<octave_idx_type>& p = v.col_perm_vec ();
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
267 for (octave_idx_type k = 0; k < count; k++)
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
268 {
18883
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
269 OCTAVE_QUIT;
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
270 const octave_idx_type j = start_nc + k;
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
271 const octave_idx_type i = p(j);
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
272 i_idx(k) = static_cast<double> (1+i);
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
273 j_idx(k) = static_cast<double> (1+j);
aa9ca67f09fb make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents: 18851
diff changeset
274 idx(k) = j * nc + i + 1;
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
275 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
276 }
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
277 else
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
278 {
18826
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
279 // FIXME: Is this case even possible? A scalar permutation matrix seems
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
280 // to devolve to a scalar full matrix, at least from the Octave command
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
281 // line. Perhaps this function could be called internally from C++ with
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
282 // such a matrix.
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
283 // No items found. Fixup return dimensions for Matlab compatibility.
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
284 // The behavior to match is documented in Array.cc (Array<T>::find).
18826
332450e56698 Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents: 18813
diff changeset
285 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1))
18813
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
286 {
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
287 idx.resize (0, 0);
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
288
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
289 i_idx.resize (0, 0);
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
290 j_idx.resize (0, 0);
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
291
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
292 val.resize (dim_vector (0, 0));
a5286fb173cd Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents: 17787
diff changeset
293 }
8955
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
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
296 switch (nargout)
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
297 {
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
298 case 0:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
299 case 1:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
300 retval(0) = idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
301 break;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
302
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
303 case 5:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
304 retval(4) = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
305 // Fall through
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 case 4:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
308 retval(3) = nc;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
309 // Fall through
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
310
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
311 case 3:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
312 retval(2) = val;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
313 // Fall through!
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
314
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
315 case 2:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
316 retval(1) = j_idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
317 retval(0) = i_idx;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
318 break;
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 default:
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
321 panic_impossible ();
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
322 break;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
323 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
324
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
325 return retval;
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
326 }
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
327
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
328 DEFUN (find, args, nargout,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
329 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
330 @deftypefn {Built-in Function} {@var{idx} =} find (@var{x})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
331 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
332 @deftypefnx {Built-in Function} {@var{idx} =} find (@var{x}, @var{n}, @var{direction})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
333 @deftypefnx {Built-in Function} {[i, j] =} find (@dots{})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
334 @deftypefnx {Built-in Function} {[i, j, v] =} find (@dots{})\n\
6524
03c89ade4070 [project @ 2007-04-13 12:19:30 by jwe]
jwe
parents: 6254
diff changeset
335 Return a vector of indices of nonzero elements of a matrix, as a row if\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
336 @var{x} is a row vector or as a column otherwise.\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
337 \n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
338 To obtain a single index for each matrix element, Octave pretends that the\n\
20216
aa36fb998a4d maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents: 20207
diff changeset
339 columns of a matrix form one long vector (like Fortran arrays are stored).\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
340 For example:\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
341 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
342 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
343 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
344 find (eye (2))\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
345 @result{} [ 1; 4 ]\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
346 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
347 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
348 \n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
349 If two inputs are given, @var{n} indicates the maximum number of elements to\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
350 find from the beginning of the matrix or vector.\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
351 \n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
352 If three inputs are given, @var{direction} should be one of\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
353 @qcode{\"first\"} or @qcode{\"last\"}, requesting only the first or last\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
354 @var{n} indices, respectively. However, the indices are always returned in\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
355 ascending order.\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
356 \n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
357 If two outputs are requested, @code{find} returns the row and column\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10826
diff changeset
358 indices of nonzero elements of a matrix. For example:\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
359 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
360 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
361 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
362 [i, j] = find (2 * eye (2))\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
363 @result{} i = [ 1; 2 ]\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
364 @result{} j = [ 1; 2 ]\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
365 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
366 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
367 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
368 If three outputs are requested, @code{find} also returns a vector\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10826
diff changeset
369 containing the nonzero values. For example:\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
370 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
371 @example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
372 @group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
373 [i, j, v] = find (3 * eye (2))\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
374 @result{} i = [ 1; 2 ]\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
375 @result{} j = [ 1; 2 ]\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
376 @result{} v = [ 3; 3 ]\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
377 @end group\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
378 @end example\n\
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
379 \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
380 Note that this function is particularly useful for sparse matrices, as\n\
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 18827
diff changeset
381 it extracts the nonzero elements as vectors, which can then be used to\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10826
diff changeset
382 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
383 \n\
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
384 @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
385 @group\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
386 sz = size (a);\n\
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
387 [i, j, v] = find (a);\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
388 b = sparse (i, j, v, sz(1), sz(2));\n\
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
389 @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
390 @end example\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
391 @seealso{nonzeros}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3014
diff changeset
392 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
393 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
394 octave_value_list retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
395
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
396 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
397
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
398 if (nargin > 3 || nargin < 1)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
399 {
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5307
diff changeset
400 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
401 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
402 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
403
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
404 // Setup the default options.
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
405 octave_idx_type n_to_find = -1;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
406 if (nargin > 1)
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
407 {
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
408 double val = args(1).scalar_value ();
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
409
10338
21dd58bd683c fix unsafe conversion in find
Jaroslav Hajek <highegg@gmail.com>
parents: 10285
diff changeset
410 if (error_state || (val < 0 || (! xisinf (val) && val != xround (val))))
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
411 {
14062
5b49cafe0599 Use non-negative, non-positive with hyphens in error messages.
Rik <octave@nomad.inbox5.com>
parents: 13915
diff changeset
412 error ("find: N must be a non-negative integer");
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
413 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
414 }
10338
21dd58bd683c fix unsafe conversion in find
Jaroslav Hajek <highegg@gmail.com>
parents: 10285
diff changeset
415 else if (! xisinf (val))
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
416 n_to_find = val;
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
417 }
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
418
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
419 // Direction to do the searching (1 == forward, -1 == reverse).
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
420 int direction = 1;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
421 if (nargin > 2)
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
422 {
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
423 direction = 0;
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
424
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
425 std::string s_arg = args(2).string_value ();
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
426
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
427 if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
428 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
429 if (s_arg == "first")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
430 direction = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
431 else if (s_arg == "last")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
432 direction = -1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
433 }
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
434
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
435 if (direction == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
436 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
437 error ("find: DIRECTION must be \"first\" or \"last\"");
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
438 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
439 }
6002
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
440 }
c5d0490aad55 [project @ 2006-09-26 19:45:42 by jwe]
jwe
parents: 5823
diff changeset
441
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
442 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
443
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
444 if (arg.is_bool_type ())
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
445 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
446 if (arg.is_sparse_type ())
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
447 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
448 SparseBoolMatrix v = arg.sparse_bool_matrix_value ();
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
449
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
450 if (! error_state)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
451 retval = find_nonzero_elem_idx (v, nargout,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
452 n_to_find, direction);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
453 }
9544
df0abc63c230 cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents: 9351
diff changeset
454 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
455 {
df0abc63c230 cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents: 9351
diff changeset
456 // This case is equivalent to extracting indices from a logical
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
457 // matrix. Try to reuse the possibly cached index vector.
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20267
diff changeset
458
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20267
diff changeset
459 // No need to catch index_exception, since arg is bool.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20267
diff changeset
460 // Out-of-range errors have already set pos, and will be
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20267
diff changeset
461 // caught later.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20267
diff changeset
462
9894
83bd7f34f9da improve idx_vector->octave_value conversion
Jaroslav Hajek <highegg@gmail.com>
parents: 9732
diff changeset
463 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
464 }
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
465 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
466 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
467 boolNDArray v = arg.bool_array_value ();
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
468
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
469 if (! error_state)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
470 retval = find_nonzero_elem_idx (v, nargout,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
471 n_to_find, direction);
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
472 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
473 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
474 else if (arg.is_integer_type ())
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
475 {
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
476 #define DO_INT_BRANCH(INTT) \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
477 else if (arg.is_ ## INTT ## _type ()) \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
478 { \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
479 INTT ## NDArray v = arg.INTT ## _array_value (); \
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
480 \
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
481 if (! error_state) \
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
482 retval = find_nonzero_elem_idx (v, nargout, \
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
483 n_to_find, direction);\
9028
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
484 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
485
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
486 if (false)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
487 ;
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
488 DO_INT_BRANCH (int8)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
489 DO_INT_BRANCH (int16)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
490 DO_INT_BRANCH (int32)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
491 DO_INT_BRANCH (int64)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
492 DO_INT_BRANCH (uint8)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
493 DO_INT_BRANCH (uint16)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
494 DO_INT_BRANCH (uint32)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
495 DO_INT_BRANCH (uint64)
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
496 else
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
497 panic_impossible ();
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
498 }
e67dc11ed6e8 use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents: 9019
diff changeset
499 else if (arg.is_sparse_type ())
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
500 {
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
501 if (arg.is_real_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
502 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
503 SparseMatrix v = arg.sparse_matrix_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
504
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
505 if (! error_state)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
506 retval = find_nonzero_elem_idx (v, nargout,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
507 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
508 }
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
509 else if (arg.is_complex_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
510 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
511 SparseComplexMatrix v = arg.sparse_complex_matrix_value ();
5107
c891dd97b837 [project @ 2005-01-12 19:49:18 by jwe]
jwe
parents: 4826
diff changeset
512
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
513 if (! error_state)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
514 retval = find_nonzero_elem_idx (v, nargout,
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
515 n_to_find, direction);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
516 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
517 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
518 gripe_wrong_type_arg ("find", arg);
5107
c891dd97b837 [project @ 2005-01-12 19:49:18 by jwe]
jwe
parents: 4826
diff changeset
519 }
10550
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
520 else if (arg.is_perm_matrix ())
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
521 {
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
522 PermMatrix P = arg.perm_matrix_value ();
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
523
10550
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
524 if (! error_state)
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
525 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction);
c48b7048e720 style fixes
John W. Eaton <jwe@octave.org>
parents: 10338
diff changeset
526 }
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
527 else if (arg.is_string ())
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
528 {
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
529 charNDArray chnda = arg.char_array_value ();
7505
f5005d9510f4 Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
530
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
531 if (! error_state)
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
532 retval = find_nonzero_elem_idx (chnda, nargout, n_to_find, direction);
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
533 }
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
534 else if (arg.is_single_type ())
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
535 {
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
536 if (arg.is_real_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
537 {
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
538 FloatNDArray nda = arg.float_array_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
539
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
540 if (! error_state)
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
541 retval = find_nonzero_elem_idx (nda, nargout, n_to_find,
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
542 direction);
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
543 }
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
544 else if (arg.is_complex_type ())
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
545 {
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
546 FloatComplexNDArray cnda = arg.float_complex_array_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
547
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
548 if (! error_state)
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
549 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find,
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
550 direction);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9894
diff changeset
551 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
552 }
10826
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
553 else if (arg.is_real_type ())
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
554 {
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
555 NDArray nda = arg.array_value ();
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
556
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
557 if (! error_state)
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
558 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction);
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
559 }
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
560 else if (arg.is_complex_type ())
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
561 {
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
562 ComplexNDArray cnda = arg.complex_array_value ();
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
563
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
564 if (! error_state)
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
565 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction);
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
566 }
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
567 else
9e6aed3c6704 fix find for character arrays
John W. Eaton <jwe@octave.org>
parents: 10550
diff changeset
568 gripe_wrong_type_arg ("find", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
569
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
570 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
571 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
572
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
573 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
574 %!assert (find (char ([0, 97])), 2)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
575 %!assert (find ([1, 0, 1, 0, 1]), [1, 3, 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
576 %!assert (find ([1; 0; 3; 0; 1]), [1; 3; 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
577 %!assert (find ([0, 0, 2; 0, 3, 0; -1, 0, 0]), [3; 5; 7])
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
578
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
579 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
580 %! [i, j, v] = find ([0, 0, 2; 0, 3, 0; -1, 0, 0]);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
581 %!
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
582 %! assert (i, [3; 2; 1]);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
583 %! assert (j, [1; 2; 3]);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
584 %! assert (v, [-1; 3; 2]);
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
585
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
586 %!assert (find (single ([1, 0, 1, 0, 1])), [1, 3, 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
587 %!assert (find (single ([1; 0; 3; 0; 1])), [1; 3; 5])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
588 %!assert (find (single ([0, 0, 2; 0, 3, 0; -1, 0, 0])), [3; 5; 7])
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
589
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
590 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
591 %! [i, j, v] = find (single ([0, 0, 2; 0, 3, 0; -1, 0, 0]));
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
592 %!
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
593 %! assert (i, [3; 2; 1]);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
594 %! assert (j, [1; 2; 3]);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
595 %! assert (v, single ([-1; 3; 2]));
7814
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
596
8955
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
597 %!test
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
598 %! 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
599 %! P = eye (5) (:, pcol);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
600 %! [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
601 %! [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
602 %! assert (i, ifull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
603 %! assert (j, jfull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
604 %! assert (all (v == 1));
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
605
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
606 %!test
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
607 %! 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
608 %! P = eye (5) (prow, :);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
609 %! [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
610 %! [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
611 %! assert (i, ifull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
612 %! assert (j, jfull);
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
613 %! assert (all (v == 1));
6d3fcbf89267 Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents: 8920
diff changeset
614
9310
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
615 %!assert (find ([2 0 1 0 5 0], 1), 1)
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
616 %!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
617
256c0db275b6 fix behavior of find
Jaroslav Hajek <highegg@gmail.com>
parents: 9046
diff changeset
618 %!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
619 %!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
620
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
621 %!error find ()
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
622 */