Mercurial > octave
annotate libinterp/corefcn/sub2ind.cc @ 20956:850e3d2533d4
maint: Eliminate more useless statements after error().
* __ilu__.cc, display.cc, file-io.cc, graphics.cc, graphics.in.h, hex2num.cc,
load-save.cc, ls-mat-ascii.cc, oct-errno.in.cc, pager.cc, sighandlers.cc,
sub2ind.cc, symtab.h, syscalls.cc, toplev.h, txt-eng-ft.cc, __eigs__.cc,
ov-bool-mat.cc, ov-classdef.h, ov-cx-mat.cc, ov-fcn-handle.cc,
ov-flt-cx-mat.cc, ov-flt-re-mat.cc, ov-re-mat.cc, ov-str-mat.cc, ov-struct.cc,
ov-typeinfo.cc, ov-usr-fcn.cc:
maint: Eliminate more useless statements after error().
author | Rik <rik@octave.org> |
---|---|
date | Sun, 20 Dec 2015 13:26:17 -0800 |
parents | 48b2ad5ee801 |
children | 5e00ed38a58b |
rev | line source |
---|---|
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
1 /* |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
2 |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19269
diff
changeset
|
3 Copyright (C) 2009-2015 VZLU Prague |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
4 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
5 This file is part of Octave. |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
6 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
10 option) any later version. |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
11 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
15 for more details. |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
16 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
20 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
21 */ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
22 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
24 #include <config.h> |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
25 #endif |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
26 |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
27 #include "Array-util.h" |
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
28 #include "oct-locbuf.h" |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
29 #include "quit.h" |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
30 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
31 #include "defun.h" |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
32 #include "error.h" |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
33 #include "gripes.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
34 #include "ovl.h" |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
35 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
36 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
37 static dim_vector |
10258 | 38 get_dim_vector (const octave_value& val, const char *name) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
39 { |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
40 RowVector dimsv = val.row_vector_value (false, true); |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
41 dim_vector dv; |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20172
diff
changeset
|
42 octave_idx_type n = dimsv.numel (); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
43 |
9666
a531dec450c4
allow 1D case for sub2ind and ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9479
diff
changeset
|
44 if (n < 1) |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
45 error ("%s: dimension vector DIMS must not be empty", name); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
46 else |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
47 { |
10258 | 48 dv.resize (std::max (n, static_cast<octave_idx_type> (2))); |
9666
a531dec450c4
allow 1D case for sub2ind and ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9479
diff
changeset
|
49 dv(1) = 1; |
a531dec450c4
allow 1D case for sub2ind and ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9479
diff
changeset
|
50 for (octave_idx_type i = 0; i < n; i++) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
51 { |
10258 | 52 octave_idx_type ii = dimsv(i); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
53 if (ii == dimsv(i) && ii >= 0) |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
54 dv(i) = ii; |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
55 else |
20956
850e3d2533d4
maint: Eliminate more useless statements after error().
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
56 error ("%s: dimension vector DIMS must contain integers", name); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
57 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
58 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
59 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
60 return dv; |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
61 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
62 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
63 DEFUN (sub2ind, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20819
diff
changeset
|
65 @deftypefn {} {@var{ind} =} sub2ind (@var{dims}, @var{i}, @var{j})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20819
diff
changeset
|
66 @deftypefnx {} {@var{ind} =} sub2ind (@var{dims}, @var{s1}, @var{s2}, @dots{}, @var{sN})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 Convert subscripts to a linear index.\n\ |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
68 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
69 The following example shows how to convert the two-dimensional index\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
70 @code{(2,3)} of a 3-by-3 matrix to a linear index. The matrix is linearly\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
71 indexed moving from one column to next, filling up all rows in each column.\n\ |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
72 \n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
73 @example\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
74 @group\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
75 linear_index = sub2ind ([3, 3], 2, 3)\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
76 @result{} 8\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
77 @end group\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
78 @end example\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
79 @seealso{ind2sub}\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
80 @end deftypefn") |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
81 { |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
82 int nargin = args.length (); |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
83 |
9666
a531dec450c4
allow 1D case for sub2ind and ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
9479
diff
changeset
|
84 if (nargin < 2) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
85 print_usage (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
86 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
87 dim_vector dv = get_dim_vector (args(0), "sub2ind"); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
88 Array<idx_vector> idxa (dim_vector (nargin-1, 1)); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
89 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
90 dv = dv.redim (nargin - 1); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
91 for (int j = 0; j < nargin - 1; j++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
92 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
93 if (! args(j+1).is_numeric_type ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
94 error ("sub2ind: subscripts must be numeric"); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
95 |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
96 try |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
97 { |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
98 idxa(j) = args(j+1).index_vector (); |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
99 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
100 if (j > 0 && args(j+1).dims () != args(1).dims ()) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
101 error ("sub2ind: all subscripts must be of the same size"); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
102 } |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
103 catch (index_exception& e) |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
104 { |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
105 e.set_pos_if_unset (nargin-1, j+1); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
106 e.set_var (); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
107 std::string msg = e.message (); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
108 error_with_id (e.err_id (), msg.c_str ()); |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
109 } |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
110 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
111 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
112 return ovl (sub2ind (dv, idxa)); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
113 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
114 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
115 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
116 ## Test evaluation |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
117 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
118 %! s1 = [ 1 1 1 1 ; 2 2 2 2 ]; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
119 %! s2 = [ 1 1 2 2 ; 1 1 2 2 ]; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
120 %! s3 = [ 1 2 1 2 ; 1 2 1 2 ]; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
121 %! in = [ 1 101 11 111 ; 2 102 12 112 ]; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
122 %! assert (sub2ind ([10 10 10], s1, s2, s3), in); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
123 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
124 # Test low index |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
125 %!assert (sub2ind ([10 10 10], 1, 1, 1), 1) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
126 %!error <index \(0,_,_\)> sub2ind ([10 10 10], 0, 1, 1) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
127 %!error <index \(_,0,_\)> sub2ind ([10 10 10], 1, 0, 1) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
128 %!error <index \(_,_,0\)> sub2ind ([10 10 10], 1, 1, 0) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
129 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
130 # Test high index |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
131 %!assert (sub2ind ([10 10 10], 10, 10, 10), 1000) |
20795
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
132 %!error <index \(11,_,_\): out of bound 10> sub2ind ([10 10 10], 11, 10, 10) |
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
133 %!error <index \(_,11,_\): out of bound 10> sub2ind ([10 10 10], 10, 11, 10) |
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
134 %!error <index \(_,_,11\): out of bound 10> sub2ind ([10 10 10], 10, 10, 11) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
135 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
136 # Test high index in the trailing dimensions |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
137 %!assert (sub2ind ([10, 1], 2, 1, 1), 2) |
20795
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
138 %!error <index \(_,2,_\): out of bound 1> sub2ind ([10, 1], 1, 2, 1) |
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
139 %!error <index \(_,_,2\): out of bound 1> sub2ind ([10, 1], 1, 1, 2) |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
140 %!assert (sub2ind ([10 10], 2, 2, 1), 12) |
20795
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
141 %!error <index \(_,_,2\): out of bound 1> sub2ind ([10 10], 2, 1, 2) |
ba2367658dc8
Use ':' rather than ';' in error messages from bad indexing (bug #46536).
Rik <rik@octave.org>
parents:
20715
diff
changeset
|
142 %!error <index \(_,_,2\): out of bound 1> sub2ind ([10 10], 1, 2, 2) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
143 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
144 # Test handling of empty arguments |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
145 %!assert (sub2ind ([10 10], zeros (0,0), zeros (0,0)), zeros (0,0)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
146 %!assert (sub2ind ([10 10], zeros (2,0), zeros (2,0)), zeros (2,0)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
147 %!assert (sub2ind ([10 10], zeros (0,2), zeros (0,2)), zeros (0,2)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
148 %!error <all subscripts .* same size> sub2ind ([10 10 10], zeros (0,2), zeros (2,0)) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
149 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
150 # Test handling of arguments of different size |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
151 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (1,3)) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
152 %!error <all subscripts .* same size> sub2ind ([10 10], ones (1,2), ones (2,1)) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
153 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
154 ## Test input validation |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14360
diff
changeset
|
155 %!error <dimension vector> sub2ind ([10 10.5], 1, 1) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
156 %!error <index \(1.5,_\)> sub2ind ([10 10], 1.5, 1) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
157 %!error <index \(_,1.5\)> sub2ind ([10 10], 1, 1.5) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
158 */ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
159 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
160 DEFUN (ind2sub, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
161 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20819
diff
changeset
|
162 @deftypefn {} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} ind2sub (@var{dims}, @var{ind})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
163 Convert a linear index to subscripts.\n\ |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
164 \n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
165 The following example shows how to convert the linear index @code{8}\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
166 in a 3-by-3 matrix into a subscript. The matrix is linearly indexed\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
167 moving from one column to next, filling up all rows in each column.\n\ |
10840 | 168 \n\ |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
169 @example\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
170 @group\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
171 [r, c] = ind2sub ([3, 3], 8)\n\ |
14360
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
172 @result{} r = 2\n\ |
97883071e8e4
doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
173 @result{} c = 3\n\ |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
174 @end group\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
175 @end example\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
176 @seealso{sub2ind}\n\ |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
177 @end deftypefn") |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
178 { |
20819
f428cbe7576f
eliminate unnecessary uses of nargin
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
179 if (args.length () != 2) |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
180 print_usage (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
181 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
182 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
183 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
184 dim_vector dv = get_dim_vector (args(0), "ind2sub"); |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
185 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
186 try |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
187 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
188 idx_vector idx = args(1).index_vector (); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
189 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
190 if (nargout > dv.length ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
191 dv = dv.redim (nargout); |
20715
5b7643257978
Remove trailing whitespace at end of lines.
Rik <rik@octave.org>
parents:
20652
diff
changeset
|
192 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
193 retval = Array<octave_value> (ind2sub (dv, idx)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
194 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
195 catch (const index_exception& e) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
196 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
197 std::string idx = e.idx (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
198 std::string msg = e.details (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20795
diff
changeset
|
199 error ("ind2sub: Invalid index %s. %s", idx.c_str (), msg.c_str ()); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
200 } |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
201 |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
202 return retval; |
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
diff
changeset
|
203 } |