Mercurial > octave
annotate libinterp/corefcn/find.cc @ 31605:e88a07dec498 stable
maint: Use macros to begin/end C++ namespaces.
* oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE,
OCTAVE_END_NAMESPACE) that can be used to start/end a namespace.
* mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc,
__dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc,
__ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc,
auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h,
besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h,
call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc,
colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc,
defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc,
display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc,
ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h,
event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc,
fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc,
getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc,
gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc,
graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc,
help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h,
interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h,
inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc,
latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h,
lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h,
mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc,
oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h,
oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h,
oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h,
oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy,
ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc,
pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc,
qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h,
sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc,
sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h,
stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc,
symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h,
symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h,
text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc,
toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc,
url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc,
variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h,
__delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc,
audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh,
mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc,
cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h,
cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h,
cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc,
ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h,
ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc,
ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h,
ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc,
op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc,
op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc,
op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc,
op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc,
op-i8-i8.cc, op-int-concat.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc,
op-m-scm.cc, op-m-sm.cc, op-mi.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc,
op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc,
op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc,
op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc,
op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc,
op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc,
op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h,
anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h,
comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc,
oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h,
pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h,
pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h,
pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h,
pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h,
pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc,
pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc,
pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc,
pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc,
pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc,
pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc,
pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h,
pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h,
range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc,
chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h,
lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h,
oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc,
qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc,
randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc,
sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc,
child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc,
file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h,
mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h,
oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc,
oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h,
base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h,
file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc,
lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h,
oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h,
oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h,
oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h,
pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h,
url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 01 Dec 2022 14:23:45 -0800 |
parents | 8b522a359143 |
children | aac27ad79be6 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29961
diff
changeset
|
3 // Copyright (C) 1996-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
2928 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
27 # include "config.h" |
2928 | 28 #endif |
29 | |
4153 | 30 #include "quit.h" |
31 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
32 #include "defun.h" |
2928 | 33 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
34 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
35 #include "ovl.h" |
2928 | 36 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30709
diff
changeset
|
37 OCTAVE_BEGIN_NAMESPACE(octave) |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
38 |
6002 | 39 // Find at most N_TO_FIND nonzero elements in NDA. Search forward if |
40 // DIRECTION is 1, backward if it is -1. NARGOUT is the number of | |
41 // output arguments. If N_TO_FIND is -1, find all nonzero elements. | |
4678 | 42 |
43 template <typename T> | |
44 octave_value_list | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
45 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
|
46 octave_idx_type n_to_find, int direction) |
4678 | 47 { |
6002 | 48 octave_value_list retval ((nargout == 0 ? 1 : nargout), Matrix ()); |
4678 | 49 |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
50 Array<octave_idx_type> idx; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
51 if (n_to_find >= 0) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
52 idx = nda.find (n_to_find, direction == -1); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
53 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
54 idx = nda.find (); |
5130 | 55 |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
56 // The maximum element is always at the end. |
30346
91c6288781ba
maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29961
diff
changeset
|
57 octave_idx_type iext = (idx.isempty () ? 0 |
91c6288781ba
maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29961
diff
changeset
|
58 : idx.xelem (idx.numel () - 1) + 1); |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
59 |
2928 | 60 switch (nargout) |
61 { | |
6254 | 62 default: |
2928 | 63 case 3: |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
64 retval(2) = Array<T> (nda.index (idx_vector (idx))); |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
65 OCTAVE_FALLTHROUGH; |
2928 | 66 |
67 case 2: | |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
68 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
69 Array<octave_idx_type> jdx (idx.dims ()); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20181
diff
changeset
|
70 octave_idx_type n = idx.numel (); |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
71 octave_idx_type nr = nda.rows (); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
72 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
|
73 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
74 jdx.xelem (i) = idx.xelem (i) / nr; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
75 idx.xelem (i) %= nr; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
76 } |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
77 iext = -1; |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
78 retval(1) = idx_vector (jdx, -1); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
79 } |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
80 OCTAVE_FALLTHROUGH; |
2928 | 81 |
6254 | 82 case 1: |
83 case 0: | |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
84 retval(0) = idx_vector (idx, iext); |
2928 | 85 break; |
86 } | |
87 | |
88 return retval; | |
89 } | |
90 | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
91 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
|
92 octave_value_list |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
93 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
|
94 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
|
95 { |
20758
f819397c0bc1
Stop segfault when find() called with 6 outputs (bug #42424).
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
96 nargout = std::min (nargout, 5); |
7505
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_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
|
98 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
103 // 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
|
104 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
|
105 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
|
106 octave_idx_type count; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
107 |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
108 // 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
|
109 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
|
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 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
|
112 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
|
113 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
|
114 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
115 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
|
116 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
117 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
|
118 { |
22860
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
119 octave_quit (); |
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
120 |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
121 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
|
122 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
|
123 if (v.cidx (j+1) >= n_to_find) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
124 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
125 end_nc = j + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
126 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
127 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
128 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
129 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
130 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
131 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
132 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
|
133 { |
22860
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
134 octave_quit (); |
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
135 |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
140 start_nc = j - 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
141 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
142 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
143 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
144 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
145 |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
146 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
|
147 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
|
148 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
149 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
|
150 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
|
151 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
152 // 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
|
153 // 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
|
154 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
|
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 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
|
157 result_nc = count; |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
158 } |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
159 else |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
160 { |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
161 result_nr = count; |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
162 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
|
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 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
165 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
|
166 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
167 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
|
168 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
|
169 |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9544
diff
changeset
|
170 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
|
171 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
172 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
|
173 { |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
174 // 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
|
175 // 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
|
176 for (octave_idx_type j = start_nc, cx = 0; j < end_nc; j++) |
18678
6113e0c6920b
maint: Clean up extra spaces before/after parentheses.
Rik <rik@octave.org>
parents:
18676
diff
changeset
|
177 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
|
178 { |
22860
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
179 octave_quit (); |
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
180 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
181 if (direction < 0 && i < nz - count) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
182 continue; |
14854
5ae9f0f77635
maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
183 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
|
184 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
|
185 idx(cx) = j * nr + v.ridx (i) + 1; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
186 val(cx) = v.data(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
187 cx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
188 if (cx == count) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
189 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
190 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
191 } |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
192 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
|
193 { |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
194 // 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
|
195 // The behavior to match is documented in Array.cc (Array<T>::find). |
18793
332450e56698
Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents:
18779
diff
changeset
|
196 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1)) |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
197 { |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
198 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
|
199 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
200 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
|
201 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
|
202 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
203 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
|
204 } |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
205 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
206 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
207 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
|
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 0: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
210 case 1: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
211 retval(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
|
212 break; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
213 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
214 case 5: |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
215 retval(4) = nc; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
216 OCTAVE_FALLTHROUGH; |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
217 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
218 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
|
219 retval(3) = nr; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
220 OCTAVE_FALLTHROUGH; |
7505
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 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
|
223 retval(2) = val; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
224 OCTAVE_FALLTHROUGH; |
7505
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 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
|
227 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
|
228 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
|
229 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
230 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
231 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
|
232 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
233 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
234 octave_value_list |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
235 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
|
236 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
|
237 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
238 // There are far fewer special cases to handle for a PermMatrix. |
20758
f819397c0bc1
Stop segfault when find() called with 6 outputs (bug #42424).
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
239 nargout = std::min (nargout, 5); |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
240 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
|
241 |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
242 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
|
243 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
|
244 octave_idx_type start_nc, count; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
245 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
246 // 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
|
247 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
|
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 = nc; |
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 if (direction > 0) |
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 = 0; |
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 else |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
258 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
259 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
|
260 count = n_to_find; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
261 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
262 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
263 Matrix idx (count, 1); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
264 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
|
265 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
|
266 // Every value is 1. |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9544
diff
changeset
|
267 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
|
268 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
269 if (count > 0) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
270 { |
18849
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
271 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:
18812
diff
changeset
|
272 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
|
273 { |
22860
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
274 octave_quit (); |
0b1e25cc4457
eliminate use of OCTAVE_QUIT macro in C++ sources
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
275 |
18849
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
276 const octave_idx_type j = start_nc + k; |
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
277 const octave_idx_type i = p(j); |
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
278 i_idx(k) = static_cast<double> (1+i); |
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
279 j_idx(k) = static_cast<double> (1+j); |
aa9ca67f09fb
make all permutation matrices column permutations (bug #42418)
David Spies <dnspies@gmail.com>
parents:
18812
diff
changeset
|
280 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
|
281 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
282 } |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
283 else |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
284 { |
18793
332450e56698
Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents:
18779
diff
changeset
|
285 // 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:
18779
diff
changeset
|
286 // 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:
18779
diff
changeset
|
287 // 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:
18779
diff
changeset
|
288 // such a matrix. |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
289 // 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
|
290 // The behavior to match is documented in Array.cc (Array<T>::find). |
18793
332450e56698
Fix compilation warning about use of '&' versus '&&' in find.cc.
Rik <rik@octave.org>
parents:
18779
diff
changeset
|
291 if ((nr == 0 && nc == 0) || (nr == 1 && nc == 1)) |
18779
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
292 { |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
293 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
|
294 |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
295 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
|
296 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
|
297 |
a5286fb173cd
Match Matlab return dimensions for find on empty sparse matrices (bug #42408).
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
298 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
|
299 } |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
300 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
301 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
302 switch (nargout) |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
303 { |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
304 case 0: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
305 case 1: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
306 retval(0) = idx; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
307 break; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
308 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
309 case 5: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
310 retval(4) = nc; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
311 OCTAVE_FALLTHROUGH; |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
312 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
313 case 4: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
314 retval(3) = nc; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
315 OCTAVE_FALLTHROUGH; |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
316 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
317 case 3: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
318 retval(2) = val; |
23826
d69021d58a61
avoid fallthrough warnings
John W. Eaton <jwe@octave.org>
parents:
23803
diff
changeset
|
319 OCTAVE_FALLTHROUGH; |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
320 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
321 case 2: |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
322 retval(1) = j_idx; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
323 retval(0) = i_idx; |
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 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
326 return retval; |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
327 } |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
328 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
329 DEFUN (find, args, nargout, |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
330 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
331 @deftypefn {} {@var{idx} =} find (@var{x}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
332 @deftypefnx {} {@var{idx} =} find (@var{x}, @var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
333 @deftypefnx {} {@var{idx} =} find (@var{x}, @var{n}, @var{direction}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
334 @deftypefnx {} {[i, j] =} find (@dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
335 @deftypefnx {} {[i, j, v] =} find (@dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
336 Return a vector of indices of nonzero elements of a matrix, as a row if |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
337 @var{x} is a row vector or as a column otherwise. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
338 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
339 To obtain a single index for each matrix element, Octave pretends that the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
340 columns of a matrix form one long vector (like Fortran arrays are stored). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
341 For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
342 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
343 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
344 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
345 find (eye (2)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
346 @result{} [ 1; 4 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
347 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
348 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
349 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
350 If two inputs are given, @var{n} indicates the maximum number of elements to |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
351 find from the beginning of the matrix or vector. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
352 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
353 If three inputs are given, @var{direction} should be one of |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
354 @qcode{"first"} or @qcode{"last"}, requesting only the first or last |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
355 @var{n} indices, respectively. However, the indices are always returned in |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
356 ascending order. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
357 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
358 If two outputs are requested, @code{find} returns the row and column |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
359 indices of nonzero elements of a matrix. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
360 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
361 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
362 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
363 [i, j] = find (2 * eye (2)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
364 @result{} i = [ 1; 2 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
365 @result{} j = [ 1; 2 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
366 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
367 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
368 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
369 If three outputs are requested, @code{find} also returns a vector |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
370 containing the nonzero values. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
371 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
372 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
373 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
374 [i, j, v] = find (3 * eye (2)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
375 @result{} i = [ 1; 2 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
376 @result{} j = [ 1; 2 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
377 @result{} v = [ 3; 3 ] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
378 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
379 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
380 |
30708
17a558588b8f
doc: use hyphen in find docstring per standard Octave usage.
Rik <rik@octave.org>
parents:
30707
diff
changeset
|
381 If @var{x} is a multi-dimensional array of size m x n x p x @dots{}, @var{j} |
30707
ebda4b3a0482
doc: correct typo/grammar in find docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30706
diff
changeset
|
382 contains the column locations as if @var{x} was flattened into a |
ebda4b3a0482
doc: correct typo/grammar in find docstring
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30706
diff
changeset
|
383 two-dimensional matrix of size m x (n + p + @dots{}). |
30706
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
384 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
385 Note that this function is particularly useful for sparse matrices, as |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
386 it extracts the nonzero elements as vectors, which can then be used to |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
387 create the original matrix. For example: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
388 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
389 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
390 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
391 sz = size (a); |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
392 [i, j, v] = find (a); |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
393 b = sparse (i, j, v, sz(1), sz(2)); |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
394 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
395 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
396 @seealso{nonzeros} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
397 @end deftypefn */) |
2928 | 398 { |
399 int nargin = args.length (); | |
400 | |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
401 if (nargin < 1 || nargin > 3) |
20802
8bb38ba1bad6
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20773
diff
changeset
|
402 print_usage (); |
2928 | 403 |
6002 | 404 // Setup the default options. |
405 octave_idx_type n_to_find = -1; | |
406 if (nargin > 1) | |
407 { | |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20557
diff
changeset
|
408 double val = args(1).xscalar_value ("find: N must be an integer"); |
9310 | 409 |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
410 if (val < 0 || (! math::isinf (val) |
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
411 && val != math::fix (val))) |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20557
diff
changeset
|
412 error ("find: N must be a non-negative integer"); |
29961
7d6709900da7
eliminate octave:: namespace tags in DEFUN and DEFMETHOD and more
John W. Eaton <jwe@octave.org>
parents:
29958
diff
changeset
|
413 else if (! math::isinf (val)) |
9310 | 414 n_to_find = val; |
6002 | 415 } |
416 | |
417 // Direction to do the searching (1 == forward, -1 == reverse). | |
418 int direction = 1; | |
419 if (nargin > 2) | |
420 { | |
421 std::string s_arg = args(2).string_value (); | |
422 | |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
423 if (s_arg == "first") |
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
424 direction = 1; |
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
425 else if (s_arg == "last") |
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
426 direction = -1; |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
427 else |
23803
90689bdbe048
Use C++11 raw string literals to avoid escaping double quotes.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
428 error (R"(find: DIRECTION must be "first" or "last")"); |
6002 | 429 } |
430 | |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
431 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
432 |
2928 | 433 octave_value arg = args(0); |
434 | |
23579
c20a0fa91c0c
maint: Deprecate is_bool_type and replace with islogical.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
435 if (arg.islogical ()) |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
436 { |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
437 if (arg.issparse ()) |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
438 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
439 SparseBoolMatrix v = arg.sparse_bool_matrix_value (); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
440 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
441 retval = find_nonzero_elem_idx (v, nargout, n_to_find, direction); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
442 } |
9544
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
443 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
|
444 { |
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
445 // This case is equivalent to extracting indices from a logical |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
446 // matrix. Try to reuse the possibly cached index vector. |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
447 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
448 // No need to catch index_exception, since arg is bool. |
25195
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
449 // Out-of-range errors have already set pos, and will be |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
450 // caught later. |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
451 |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
452 octave_value result = arg.index_vector ().unmask (); |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
453 |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
454 dim_vector dv = result.dims (); |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
455 |
25263
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
456 retval(0) = (dv.all_zero () || dv.isvector () |
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
457 ? result : result.reshape (dv.as_column ())); |
9544
df0abc63c230
cache/use cached index vector on full find in logical masks
Jaroslav Hajek <highegg@gmail.com>
parents:
9351
diff
changeset
|
458 } |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
459 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
460 { |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
461 boolNDArray v = arg.bool_array_value (); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
462 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
463 retval = find_nonzero_elem_idx (v, nargout, n_to_find, direction); |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
464 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
465 } |
23580
2230f9e10fb3
maint: Deprecate is_integer_type and replace with isinteger.
Rik <rik@octave.org>
parents:
23579
diff
changeset
|
466 else if (arg.isinteger ()) |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
467 { |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
468 #define DO_INT_BRANCH(INTT) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
469 else if (arg.is_ ## INTT ## _type ()) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
470 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
471 INTT ## NDArray v = arg.INTT ## _array_value (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
21966
diff
changeset
|
472 \ |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
473 retval = find_nonzero_elem_idx (v, nargout, n_to_find, direction); \ |
9028
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
474 } |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
475 |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
476 if (false) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
477 ; |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
478 DO_INT_BRANCH (int8) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
479 DO_INT_BRANCH (int16) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
480 DO_INT_BRANCH (int32) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
481 DO_INT_BRANCH (int64) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
482 DO_INT_BRANCH (uint8) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
483 DO_INT_BRANCH (uint16) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
484 DO_INT_BRANCH (uint32) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
485 DO_INT_BRANCH (uint64) |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
486 else |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
487 panic_impossible (); |
e67dc11ed6e8
use Array<T>::find in find
Jaroslav Hajek <highegg@gmail.com>
parents:
9019
diff
changeset
|
488 } |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
489 else if (arg.issparse ()) |
2928 | 490 { |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
491 if (arg.isreal ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
492 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
493 SparseMatrix v = arg.sparse_matrix_value (); |
2928 | 494 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
495 retval = find_nonzero_elem_idx (v, nargout, n_to_find, direction); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
496 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23580
diff
changeset
|
497 else if (arg.iscomplex ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
498 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
499 SparseComplexMatrix v = arg.sparse_complex_matrix_value (); |
5107 | 500 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
501 retval = find_nonzero_elem_idx (v, nargout, n_to_find, direction); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
502 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
503 else |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
504 err_wrong_type_arg ("find", arg); |
5107 | 505 } |
10550 | 506 else if (arg.is_perm_matrix ()) |
507 { | |
508 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
|
509 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
510 retval = find_nonzero_elem_idx (P, nargout, n_to_find, direction); |
10550 | 511 } |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
512 else if (arg.is_string ()) |
2928 | 513 { |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
514 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
|
515 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
516 retval = find_nonzero_elem_idx (chnda, nargout, n_to_find, direction); |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
517 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
518 else if (arg.is_single_type ()) |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
519 { |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
520 if (arg.isreal ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
521 { |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
522 FloatNDArray nda = arg.float_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
523 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
524 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
525 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23580
diff
changeset
|
526 else if (arg.iscomplex ()) |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
527 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
528 FloatComplexNDArray cnda = arg.float_complex_array_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
529 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
530 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9894
diff
changeset
|
531 } |
2928 | 532 } |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
533 else if (arg.isreal ()) |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
534 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
535 NDArray nda = arg.array_value (); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
536 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
537 retval = find_nonzero_elem_idx (nda, nargout, n_to_find, direction); |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
538 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23580
diff
changeset
|
539 else if (arg.iscomplex ()) |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
540 { |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
541 ComplexNDArray cnda = arg.complex_array_value (); |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
542 |
20557
b10432a40432
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
543 retval = find_nonzero_elem_idx (cnda, nargout, n_to_find, direction); |
10826
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
544 } |
9e6aed3c6704
fix find for character arrays
John W. Eaton <jwe@octave.org>
parents:
10550
diff
changeset
|
545 else |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
546 err_wrong_type_arg ("find", arg); |
2928 | 547 |
548 return retval; | |
549 } | |
550 | |
551 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
552 %!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
|
553 %!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
|
554 %!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
|
555 %!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
|
556 |
25195
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
557 %!assert <*53603> (find (ones (1,1,2) > 0), [1;2]) |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
558 %!assert <*53603> (find (ones (1,1,1,3) > 0), [1;2;3]) |
2f0c0c0652d5
find: return column vector for N-d logical arrays (bug #53603)
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
559 |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
560 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
561 %! [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
|
562 %! |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
563 %! 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
|
564 %! 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
|
565 %! 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
|
566 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
567 %!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
|
568 %!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
|
569 %!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
|
570 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
571 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
572 %! [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
|
573 %! |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
574 %! 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
|
575 %! 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
|
576 %! 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
|
577 |
8955
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
578 %!test |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
579 %! 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
|
580 %! P = eye (5) (:, pcol); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
581 %! [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
|
582 %! [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
|
583 %! assert (i, ifull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
584 %! assert (j, jfull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
585 %! assert (all (v == 1)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
586 |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
587 %!test |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
588 %! 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
|
589 %! P = eye (5) (prow, :); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
590 %! [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
|
591 %! [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
|
592 %! assert (i, ifull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
593 %! assert (j, jfull); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
594 %! assert (all (v == 1)); |
6d3fcbf89267
Add an override to Octave's find() for permutation matrices.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
595 |
30706
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
596 %!test <*61986> |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
597 %! P = cat (3, eye(3), eye(3)); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
598 %! loc = find (P); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
599 %! [i, j, v] = find(P); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
600 %! assert (loc, [1, 5, 9, 10, 14, 18]'); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
601 %! assert (i, [1, 2, 3, 1, 2, 3]'); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
602 %! assert (j, [1, 2, 3, 4, 5, 6]'); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
603 %! assert (v, [1, 1, 1, 1, 1, 1]'); |
6fff5d9291ce
describe find's indexing behavior for dim > 2 and add BIST (bug #61986)
Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
parents:
30564
diff
changeset
|
604 |
25263
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
605 %!assert <*53655> (find (false), zeros (0, 0)) |
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
606 %!assert <*53655> (find ([false, false]), zeros (1, 0)) |
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
607 %!assert <*53655> (find ([false; false]), zeros (0, 1)) |
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
608 %!assert <*53655> (find ([false, false; false, false]), zeros (0, 1)) |
316b75b0d886
restore find (false) to be compatible with Matlab again (bug #53655)
Mike Miller <mtmiller@octave.org>
parents:
25195
diff
changeset
|
609 |
9310 | 610 %!assert (find ([2 0 1 0 5 0], 1), 1) |
611 %!assert (find ([2 0 1 0 5 0], 2, "last"), [3, 5]) | |
612 | |
613 %!assert (find ([2 0 1 0 5 0], Inf), [1, 3, 5]) | |
614 %!assert (find ([2 0 1 0 5 0], Inf, "last"), [1, 3, 5]) | |
615 | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
616 %!error find () |
2928 | 617 */ |
29958
32c3a5805893
move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
618 |
31605
e88a07dec498
maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents:
30709
diff
changeset
|
619 OCTAVE_END_NAMESPACE(octave) |