Mercurial > hg > octave-nkf
annotate src/data.cc @ 10843:229675bb7647 ss-3-3-52
version is now 3.3.52
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 01 Aug 2010 11:49:45 -0400 |
parents | 89f4d7e294cc |
children | a4f482e66b65 |
rev | line source |
---|---|
523 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
9428
b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
John W. Eaton <jwe@octave.org>
parents:
9351
diff
changeset
|
4 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5 Copyright (C) 2009 Jaroslav Hajek |
10277
703038d648f1
update some copyright notices
Jaroslav Hajek <highegg@gmail.com>
parents:
10269
diff
changeset
|
6 Copyright (C) 2009, 2010 VZLU Prague |
523 | 7 |
8 This file is part of Octave. | |
9 | |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
523 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
7016 | 21 along with Octave; see the file COPYING. If not, see |
22 <http://www.gnu.org/licenses/>. | |
523 | 23 |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
1192 | 27 #include <config.h> |
523 | 28 #endif |
29 | |
7078 | 30 #include <sys/types.h> |
10302
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
31 #include <sys/times.h> |
7078 | 32 |
33 #ifdef HAVE_SYS_RESOURCE_H | |
34 #include <sys/resource.h> | |
35 #endif | |
36 | |
2184 | 37 #include <cfloat> |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
38 #include <ctime> |
2184 | 39 |
1728 | 40 #include <string> |
41 | |
2184 | 42 #include "lo-ieee.h" |
7231 | 43 #include "lo-math.h" |
10240
fa7b5751730c
use gnulib time, sys_time, and sys_times modules
John W. Eaton <jwe@octave.org>
parents:
10185
diff
changeset
|
44 #include "oct-time.h" |
1755 | 45 #include "str-vec.h" |
4153 | 46 #include "quit.h" |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
47 #include "mx-base.h" |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
48 #include "oct-binmap.h" |
1755 | 49 |
6953 | 50 #include "Cell.h" |
1352 | 51 #include "defun.h" |
52 #include "error.h" | |
53 #include "gripes.h" | |
6953 | 54 #include "oct-map.h" |
55 #include "oct-obj.h" | |
2366 | 56 #include "ov.h" |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
57 #include "ov-float.h" |
5476 | 58 #include "ov-complex.h" |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
59 #include "ov-flt-complex.h" |
5476 | 60 #include "ov-cx-mat.h" |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
61 #include "ov-flt-cx-mat.h" |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
62 #include "ov-cx-sparse.h" |
6953 | 63 #include "parse.h" |
64 #include "pt-mat.h" | |
523 | 65 #include "utils.h" |
6953 | 66 #include "variables.h" |
7045 | 67 #include "pager.h" |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
68 #include "xnorm.h" |
523 | 69 |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
70 #if ! defined (CLOCKS_PER_SEC) |
10302
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
71 #if defined (CLK_TCK) |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
72 #define CLOCKS_PER_SEC CLK_TCK |
10302
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
73 #else |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
74 #error "no definition for CLOCKS_PER_SEC!" |
10302
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
75 #endif |
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
76 #endif |
5669a0b893db
data.cc: include <sys/times.h> and ensure that HZ is defined
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
77 |
7914
e998e81224b5
Various compilation fixes for MSVC.
John W. Eaton <jwe@octave.org>
parents:
7815
diff
changeset
|
78 #if ! defined (HAVE_HYPOTF) && defined (HAVE__HYPOTF) |
e998e81224b5
Various compilation fixes for MSVC.
John W. Eaton <jwe@octave.org>
parents:
7815
diff
changeset
|
79 #define hypotf _hypotf |
e998e81224b5
Various compilation fixes for MSVC.
John W. Eaton <jwe@octave.org>
parents:
7815
diff
changeset
|
80 #define HAVE_HYPOTF 1 |
e998e81224b5
Various compilation fixes for MSVC.
John W. Eaton <jwe@octave.org>
parents:
7815
diff
changeset
|
81 #endif |
e998e81224b5
Various compilation fixes for MSVC.
John W. Eaton <jwe@octave.org>
parents:
7815
diff
changeset
|
82 |
4015 | 83 #define ANY_ALL(FCN) \ |
84 \ | |
4233 | 85 octave_value retval; \ |
4015 | 86 \ |
87 int nargin = args.length (); \ | |
88 \ | |
4021 | 89 if (nargin == 1 || nargin == 2) \ |
4015 | 90 { \ |
4021 | 91 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
92 \ | |
93 if (! error_state) \ | |
94 { \ | |
4556 | 95 if (dim >= -1) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
96 retval = args(0).FCN (dim); \ |
4021 | 97 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
98 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
4021 | 99 } \ |
4015 | 100 else \ |
4021 | 101 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015 | 102 } \ |
4021 | 103 else \ |
5823 | 104 print_usage (); \ |
4015 | 105 \ |
106 return retval | |
107 | |
1957 | 108 DEFUN (all, args, , |
3369 | 109 "-*- texinfo -*-\n\ |
4015 | 110 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369 | 111 The function @code{all} behaves like the function @code{any}, except\n\ |
112 that it returns true only if all the elements of a vector, or all the\n\ | |
4015 | 113 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369 | 114 @end deftypefn") |
523 | 115 { |
4015 | 116 ANY_ALL (all); |
523 | 117 } |
118 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
119 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
120 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
121 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
122 %! x = ones (3); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
123 %! x(1,1) = 0; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
124 %! assert((all (all (rand (3) + 1) == [1, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
125 %! && all (all (x) == [0, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
126 %! && all (x, 1) == [0, 1, 1] |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
127 %! && all (x, 2) == [0; 1; 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
128 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
129 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
130 %! x = ones (3, 'single'); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
131 %! x(1,1) = 0; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
132 %! assert((all (all (single (rand (3) + 1)) == [1, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
133 %! && all (all (x) == [0, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
134 %! && all (x, 1) == [0, 1, 1] |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
135 %! && all (x, 2) == [0; 1; 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
136 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
137 %!error <Invalid call to all.*> all (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
138 %!error <Invalid call to all.*> all (1, 2, 3); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
139 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
140 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
141 |
1957 | 142 DEFUN (any, args, , |
3369 | 143 "-*- texinfo -*-\n\ |
4015 | 144 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369 | 145 For a vector argument, return 1 if any element of the vector is\n\ |
146 nonzero.\n\ | |
147 \n\ | |
148 For a matrix argument, return a row vector of ones and\n\ | |
149 zeros with each element indicating whether any of the elements of the\n\ | |
10840 | 150 corresponding column of the matrix are nonzero. For example:\n\ |
3369 | 151 \n\ |
152 @example\n\ | |
153 @group\n\ | |
154 any (eye (2, 4))\n\ | |
155 @result{} [ 1, 1, 0, 0 ]\n\ | |
156 @end group\n\ | |
157 @end example\n\ | |
158 \n\ | |
4015 | 159 If the optional argument @var{dim} is supplied, work along dimension\n\ |
10840 | 160 @var{dim}. For example:\n\ |
3369 | 161 \n\ |
162 @example\n\ | |
4015 | 163 @group\n\ |
164 any (eye (2, 4), 2)\n\ | |
165 @result{} [ 1; 1 ]\n\ | |
166 @end group\n\ | |
3369 | 167 @end example\n\ |
168 @end deftypefn") | |
523 | 169 { |
4015 | 170 ANY_ALL (any); |
523 | 171 } |
172 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
173 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
174 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
175 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
176 %! x = zeros (3); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
177 %! x(3,3) = 1; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
178 %! assert((all (any (x) == [0, 0, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
179 %! && all (any (ones (3)) == [1, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
180 %! && any (x, 1) == [0, 0, 1] |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
181 %! && any (x, 2) == [0; 0; 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
182 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
183 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
184 %! x = zeros (3,'single'); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
185 %! x(3,3) = 1; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
186 %! assert((all (any (x) == [0, 0, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
187 %! && all (any (ones (3, 'single')) == [1, 1, 1]) == 1 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
188 %! && any (x, 1) == [0, 0, 1] |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
189 %! && any (x, 2) == [0; 0; 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
190 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
191 %!error <Invalid call to any.*> any (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
192 %!error <Invalid call to any.*> any (1, 2, 3); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
193 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
194 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
195 |
649 | 196 // These mapping functions may also be useful in other places, eh? |
197 | |
1957 | 198 DEFUN (atan2, args, , |
3428 | 199 "-*- texinfo -*-\n\ |
200 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ | |
201 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ | |
9155
ad20b967e1c9
Update section 17.3 (Trigonometry) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9142
diff
changeset
|
202 and @var{x}. Signal an error if @var{y} and @var{x} do not match in size\n\ |
ad20b967e1c9
Update section 17.3 (Trigonometry) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9142
diff
changeset
|
203 and orientation.\n\ |
3439 | 204 @end deftypefn") |
649 | 205 { |
4233 | 206 octave_value retval; |
649 | 207 |
712 | 208 int nargin = args.length (); |
209 | |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
210 if (nargin == 2) |
649 | 211 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
212 if (! args(0).is_numeric_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
213 gripe_wrong_type_arg ("atan2", args(0)); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
214 else if (! args(1).is_numeric_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
215 gripe_wrong_type_arg ("atan2", args(1)); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
216 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
217 error ("atan2: not defined for complex numbers"); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
218 else if (args(0).is_single_type () || args(1).is_single_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
219 { |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
220 if (args(0).is_scalar_type () && args(1).is_scalar_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
221 retval = atan2f (args(0).float_value (), args(1).float_value ()); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
222 else |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
223 { |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
224 FloatNDArray a0 = args(0).float_array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
225 FloatNDArray a1 = args(1).float_array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
226 retval = binmap<float> (a0, a1, ::atan2f, "atan2"); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
227 } |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
228 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
229 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
230 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
231 bool a0_scalar = args(0).is_scalar_type (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
232 bool a1_scalar = args(1).is_scalar_type (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
233 if (a0_scalar && a1_scalar) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
234 retval = atan2 (args(0).scalar_value (), args(1).scalar_value ()); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
235 else if ((a0_scalar || args(0).is_sparse_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
236 && (a1_scalar || args(1).is_sparse_type ())) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
237 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
238 SparseMatrix m0 = args(0).sparse_matrix_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
239 SparseMatrix m1 = args(1).sparse_matrix_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
240 retval = binmap<double> (m0, m1, ::atan2, "atan2"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
241 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
242 else |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
243 { |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
244 NDArray a0 = args(0).array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
245 NDArray a1 = args(1).array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
246 retval = binmap<double> (a0, a1, ::atan2, "atan2"); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
247 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
248 } |
649 | 249 } |
712 | 250 else |
5823 | 251 print_usage (); |
649 | 252 |
253 return retval; | |
254 } | |
255 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
256 /* |
7741 | 257 %!assert (size (atan2 (zeros (0, 2), zeros (0, 2))), [0, 2]) |
258 %!assert (size (atan2 (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) | |
259 %!assert (size (atan2 (rand (2, 3, 4), 1)), [2, 3, 4]) | |
260 %!assert (size (atan2 (1, rand (2, 3, 4))), [2, 3, 4]) | |
261 %!assert (size (atan2 (1, 2)), [1, 1]) | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
262 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
263 %!test |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
264 %! rt2 = sqrt (2); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
265 %! rt3 = sqrt (3); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
266 %! v = [0, pi/6, pi/4, pi/3, -pi/3, -pi/4, -pi/6, 0]; |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
267 %! y = [0, rt3, 1, rt3, -rt3, -1, -rt3, 0]; |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
268 %! x = [1, 3, 1, 1, 1, 1, 3, 1]; |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
269 %! assert(atan2 (y, x), v, sqrt (eps)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
270 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
271 %!test |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
272 %! rt2 = sqrt (2); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
273 %! rt3 = sqrt (3); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
274 %! v = single([0, pi/6, pi/4, pi/3, -pi/3, -pi/4, -pi/6, 0]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
275 %! y = single([0, rt3, 1, rt3, -rt3, -1, -rt3, 0]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
276 %! x = single([1, 3, 1, 1, 1, 1, 3, 1]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
277 %! assert(atan2 (y, x), v, sqrt (eps('single'))); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
278 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
279 %!error <Invalid call to atan2.*> atan2 (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
280 %!error <Invalid call to atan2.*> atan2 (1, 2, 3); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
281 |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
282 */ |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
283 |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
284 |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
285 static octave_value |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
286 do_hypot (const octave_value& x, const octave_value& y) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
287 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
288 octave_value retval; |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
289 |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
290 octave_value arg0 = x, arg1 = y; |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
291 if (! arg0.is_numeric_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
292 gripe_wrong_type_arg ("hypot", arg0); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
293 else if (! arg1.is_numeric_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
294 gripe_wrong_type_arg ("hypot", arg1); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
295 else |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
296 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
297 if (arg0.is_complex_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
298 arg0 = arg0.abs (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
299 if (arg1.is_complex_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
300 arg1 = arg1.abs (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
301 |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
302 if (arg0.is_single_type () || arg1.is_single_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
303 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
304 if (arg0.is_scalar_type () && arg1.is_scalar_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
305 retval = hypotf (arg0.float_value (), arg1.float_value ()); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
306 else |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
307 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
308 FloatNDArray a0 = arg0.float_array_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
309 FloatNDArray a1 = arg1.float_array_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
310 retval = binmap<float> (a0, a1, ::hypotf, "hypot"); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
311 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
312 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
313 else |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
314 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
315 bool a0_scalar = arg0.is_scalar_type (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
316 bool a1_scalar = arg1.is_scalar_type (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
317 if (a0_scalar && a1_scalar) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
318 retval = hypot (arg0.scalar_value (), arg1.scalar_value ()); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
319 else if ((a0_scalar || arg0.is_sparse_type ()) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
320 && (a1_scalar || arg1.is_sparse_type ())) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
321 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
322 SparseMatrix m0 = arg0.sparse_matrix_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
323 SparseMatrix m1 = arg1.sparse_matrix_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
324 retval = binmap<double> (m0, m1, ::hypot, "hypot"); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
325 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
326 else |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
327 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
328 NDArray a0 = arg0.array_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
329 NDArray a1 = arg1.array_value (); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
330 retval = binmap<double> (a0, a1, ::hypot, "hypot"); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
331 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
332 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
333 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
334 |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
335 return retval; |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
336 } |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
337 |
7631 | 338 DEFUN (hypot, args, , |
339 "-*- texinfo -*-\n\ | |
10840 | 340 @deftypefn {Built-in Function} {} hypot (@var{x}, @var{y})\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
341 @deftypefnx {Built-in Function} {} hypot (@var{x}, @var{y}, @var{z}, @dots{})\n\ |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9163
diff
changeset
|
342 Compute the element-by-element square root of the sum of the squares of\n\ |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9163
diff
changeset
|
343 @var{x} and @var{y}. This is equivalent to\n\ |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9163
diff
changeset
|
344 @code{sqrt (@var{x}.^2 + @var{y}.^2)}, but calculated in a manner that\n\ |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9163
diff
changeset
|
345 avoids overflows for large values of @var{x} or @var{y}.\n\ |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
346 @code{hypot} can also be called with more than 2 arguments; in this case,\n\ |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
347 the arguments are accumulated from left to right:\n\ |
10840 | 348 \n\ |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
349 @example\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
350 @group\n\ |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
351 hypot (hypot (@var{x}, @var{y}), @var{z})\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
352 hypot (hypot (hypot (@var{x}, @var{y}), @var{z}), @var{w}), etc.\n\ |
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
353 @end group\n\ |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
354 @end example\n\ |
7631 | 355 @end deftypefn") |
356 { | |
357 octave_value retval; | |
358 | |
359 int nargin = args.length (); | |
360 | |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
361 if (nargin == 2) |
7631 | 362 { |
10538
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
363 retval = do_hypot (args(0), args(1)); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
364 } |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
365 else if (nargin >= 3) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
366 { |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
367 retval = args(0); |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
368 for (int i = 1; i < nargin && ! error_state; i++) |
26673015caec
extend hypot to accept >2 args
Jaroslav Hajek <highegg@gmail.com>
parents:
10533
diff
changeset
|
369 retval = do_hypot (retval, args(i)); |
7631 | 370 } |
371 else | |
372 print_usage (); | |
373 | |
374 return retval; | |
375 } | |
376 | |
377 /* | |
7741 | 378 %!assert (size (hypot (zeros (0, 2), zeros (0, 2))), [0, 2]) |
379 %!assert (size (hypot (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) | |
380 %!assert (size (hypot (rand (2, 3, 4), 1)), [2, 3, 4]) | |
381 %!assert (size (hypot (1, rand (2, 3, 4))), [2, 3, 4]) | |
382 %!assert (size (hypot (1, 2)), [1, 1]) | |
383 %!assert (hypot (1:10, 1:10), sqrt(2) * [1:10], 16*eps) | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
384 %!assert (hypot (single(1:10), single(1:10)), single(sqrt(2) * [1:10])); |
7631 | 385 */ |
386 | |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
387 template<typename T, typename ET> |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
388 void |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
389 map_2_xlog2 (const Array<T>& x, Array<T>& f, Array<ET>& e) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
390 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
391 f = Array<T>(x.dims ()); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
392 e = Array<ET>(x.dims ()); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
393 for (octave_idx_type i = 0; i < x.numel (); i++) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
394 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
395 int exp; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
396 f.xelem (i) = xlog2 (x(i), exp); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
397 e.xelem (i) = exp; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
398 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
399 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
400 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
401 DEFUN (log2, args, nargout, |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
402 "-*- texinfo -*-\n\ |
10840 | 403 @deftypefn {Mapping Function} {} log2 (@var{x})\n\ |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
404 @deftypefnx {Mapping Function} {[@var{f}, @var{e}] =} log2 (@var{x})\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
405 Compute the base-2 logarithm of each element of @var{x}.\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
406 \n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
407 If called with two output arguments, split @var{x} into\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
408 binary mantissa and exponent so that\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
409 @tex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
410 ${1 \\over 2} \\le \\left| f \\right| < 1$\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
411 @end tex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
412 @ifnottex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
413 @code{1/2 <= abs(f) < 1}\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
414 @end ifnottex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
415 and @var{e} is an integer. If\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
416 @tex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
417 $x = 0$, $f = e = 0$.\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
418 @end tex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
419 @ifnottex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
420 @code{x = 0}, @code{f = e = 0}.\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
421 @end ifnottex\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
422 @seealso{pow2, log, log10, exp}\n\ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
423 @end deftypefn") |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
424 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
425 octave_value_list retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
426 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
427 if (args.length () == 1) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
428 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
429 if (nargout < 2) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
430 retval(0) = args(0).log2 (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
431 else if (args(0).is_single_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
432 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
433 if (args(0).is_real_type ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
434 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
435 FloatNDArray f; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
436 FloatNDArray x = args(0).float_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
437 // FIXME -- should E be an int value? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
438 FloatMatrix e; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
439 map_2_xlog2 (x, f, e); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
440 retval (1) = e; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
441 retval (0) = f; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
442 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
443 else if (args(0).is_complex_type ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
444 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
445 FloatComplexNDArray f; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
446 FloatComplexNDArray x = args(0).float_complex_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
447 // FIXME -- should E be an int value? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
448 FloatNDArray e; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
449 map_2_xlog2 (x, f, e); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
450 retval (1) = e; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
451 retval (0) = f; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
452 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
453 } |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
454 else if (args(0).is_real_type ()) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
455 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
456 NDArray f; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
457 NDArray x = args(0).array_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
458 // FIXME -- should E be an int value? |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
459 Matrix e; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
460 map_2_xlog2 (x, f, e); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
461 retval (1) = e; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
462 retval (0) = f; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
463 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
464 else if (args(0).is_complex_type ()) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
465 { |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
466 ComplexNDArray f; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
467 ComplexNDArray x = args(0).complex_array_value (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
468 // FIXME -- should E be an int value? |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
469 NDArray e; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
470 map_2_xlog2 (x, f, e); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
471 retval (1) = e; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
472 retval (0) = f; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
473 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
474 else |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
475 gripe_wrong_type_arg ("log2", args(0)); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
476 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
477 else |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
478 print_usage (); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
479 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
480 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
481 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
482 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
483 /* |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
484 %!assert(log2 ([1/4, 1/2, 1, 2, 4]), [-2, -1, 0, 1, 2]); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
485 %!assert(log2(Inf), Inf); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
486 %!assert(isnan(log2(NaN))); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
487 %!assert(log2(4*i), 2 + log2(1*i)); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
488 %!assert(log2(complex(0,Inf)), Inf + log2(i)); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
489 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
490 %!test |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
491 %! [f, e] = log2 ([0,-1; 2,-4; Inf,-Inf]); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
492 %! assert (f, [0,-0.5; 0.5,-0.5; Inf,-Inf]); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
493 %! assert (e(1:2,:), [0,1;2,3]) |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
494 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
495 %!test |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
496 %! [f, e] = log2 (complex (zeros (3, 2), [0,-1; 2,-4; Inf,-Inf])); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
497 %! assert (f, complex (zeros (3, 2), [0,-0.5; 0.5,-0.5; Inf,-Inf])); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
498 %! assert (e(1:2,:), [0,1; 2,3]); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
499 */ |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
500 |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
501 DEFUN (rem, args, , |
4311 | 502 "-*- texinfo -*-\n\ |
10840 | 503 @deftypefn {Mapping Function} {} rem (@var{x}, @var{y})\n\ |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
504 @deftypefnx {Mapping Function} {} fmod (@var{x}, @var{y})\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
505 Return the remainder of the division @code{@var{x} / @var{y}}, computed \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
506 using the expression\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
507 \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
508 @example\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
509 x - y .* fix (x ./ y)\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
510 @end example\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
511 \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
512 An error message is printed if the dimensions of the arguments do not\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
513 agree, or if either of the arguments is complex.\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
514 @seealso{mod}\n\ |
4311 | 515 @end deftypefn") |
516 { | |
517 octave_value retval; | |
518 | |
519 int nargin = args.length (); | |
520 | |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
521 if (nargin == 2) |
4311 | 522 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
523 if (! args(0).is_numeric_type ()) |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
524 gripe_wrong_type_arg ("rem", args(0)); |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
525 else if (! args(1).is_numeric_type ()) |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
526 gripe_wrong_type_arg ("rem", args(1)); |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
527 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
528 error ("rem: not defined for complex numbers"); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
529 else if (args(0).is_integer_type () || args(1).is_integer_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
530 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
531 builtin_type_t btyp0 = args(0).builtin_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
532 builtin_type_t btyp1 = args(1).builtin_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
533 if (btyp0 == btyp_double || btyp0 == btyp_float) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
534 btyp0 = btyp1; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
535 if (btyp1 == btyp_double || btyp1 == btyp_float) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
536 btyp1 = btyp0; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
537 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
538 if (btyp0 == btyp1) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
539 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
540 switch (btyp0) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
541 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
542 #define MAKE_INT_BRANCH(X) \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
543 case btyp_ ## X: \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
544 { \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
545 X##NDArray a0 = args(0).X##_array_value (); \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
546 X##NDArray a1 = args(1).X##_array_value (); \ |
10682
7b4ffe27bbb4
Specify template arguments explicitely [MSVC].
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10538
diff
changeset
|
547 retval = binmap<octave_##X,octave_##X,octave_##X> (a0, a1, rem, "rem"); \ |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
548 } \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
549 break |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
550 MAKE_INT_BRANCH (int8); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
551 MAKE_INT_BRANCH (int16); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
552 MAKE_INT_BRANCH (int32); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
553 MAKE_INT_BRANCH (int64); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
554 MAKE_INT_BRANCH (uint8); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
555 MAKE_INT_BRANCH (uint16); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
556 MAKE_INT_BRANCH (uint32); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
557 MAKE_INT_BRANCH (uint64); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
558 #undef MAKE_INT_BRANCH |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
559 default: |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
560 panic_impossible (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
561 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
562 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
563 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
564 error ("rem: cannot combine %s and %d", |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
565 args(0).class_name ().c_str (), args(1).class_name ().c_str ()); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
566 } |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
567 else if (args(0).is_single_type () || args(1).is_single_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
568 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
569 if (args(0).is_scalar_type () && args(1).is_scalar_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
570 retval = fmodf (args(0).float_value (), args(1).float_value ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
571 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
572 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
573 FloatNDArray a0 = args(0).float_array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
574 FloatNDArray a1 = args(1).float_array_value (); |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
575 retval = binmap<float> (a0, a1, fmodf, "rem"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
576 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
577 } |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
578 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
579 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
580 bool a0_scalar = args(0).is_scalar_type (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
581 bool a1_scalar = args(1).is_scalar_type (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
582 if (a0_scalar && a1_scalar) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
583 retval = fmod (args(0).scalar_value (), args(1).scalar_value ()); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
584 else if ((a0_scalar || args(0).is_sparse_type ()) |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
585 && (a1_scalar || args(1).is_sparse_type ())) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
586 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
587 SparseMatrix m0 = args(0).sparse_matrix_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
588 SparseMatrix m1 = args(1).sparse_matrix_value (); |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
589 retval = binmap<double> (m0, m1, fmod, "rem"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
590 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
591 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
592 { |
10435
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
593 NDArray a0 = args(0).array_value (); |
6a271334750c
implement general binary mapping facility
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
594 NDArray a1 = args(1).array_value (); |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
595 retval = binmap<double> (a0, a1, fmod, "rem"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
596 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
597 } |
4311 | 598 } |
599 else | |
5823 | 600 print_usage (); |
4311 | 601 |
602 return retval; | |
603 } | |
604 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
605 /* |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
606 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
607 %!assert(rem ([1, 2, 3; -1, -2, -3], 2), [1, 0, 1; -1, 0, -1]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
608 %!assert(rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3)),[1, 0, 1; -1, 0, -1]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
609 %!error rem (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
610 %!error rem (1, 2, 3); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
611 %!error rem ([1, 2], [3, 4, 5]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
612 %!error rem (i, 1); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
613 %!assert(rem (uint8([1, 2, 3; -1, -2, -3]), uint8 (2)), uint8([1, 0, 1; -1, 0, -1])); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
614 %!assert(uint8(rem ([1, 2, 3; -1, -2, -3], 2 * ones (2, 3))),uint8([1, 0, 1; -1, 0, -1])); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
615 %!error rem (uint(8),int8(5)); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
616 %!error rem (uint8([1, 2]), uint8([3, 4, 5])); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
617 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
618 */ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
619 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
620 /* |
7741 | 621 %!assert (size (fmod (zeros (0, 2), zeros (0, 2))), [0, 2]) |
622 %!assert (size (fmod (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) | |
7743 | 623 %!assert (size (fmod (rand (2, 3, 4), 1)), [2, 3, 4]) |
624 %!assert (size (fmod (1, rand (2, 3, 4))), [2, 3, 4]) | |
625 %!assert (size (fmod (1, 2)), [1, 1]) | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
626 */ |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
627 |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
628 DEFALIAS (fmod, rem) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
629 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
630 DEFUN (mod, args, , |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
631 "-*- texinfo -*-\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
632 @deftypefn {Mapping Function} {} mod (@var{x}, @var{y})\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
633 Compute the modulo of @var{x} and @var{y}. Conceptually this is given by\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
634 \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
635 @example\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
636 x - y .* floor (x ./ y)\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
637 @end example\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
638 \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
639 and is written such that the correct modulus is returned for\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
640 integer types. This function handles negative values correctly. That\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
641 is, @code{mod (-1, 3)} is 2, not -1, as @code{rem (-1, 3)} returns.\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
642 @code{mod (@var{x}, 0)} returns @var{x}.\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
643 \n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
644 An error results if the dimensions of the arguments do not agree, or if\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
645 either of the arguments is complex.\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
646 @seealso{rem}\n\ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
647 @end deftypefn") |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
648 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
649 octave_value retval; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
650 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
651 int nargin = args.length (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
652 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
653 if (nargin == 2) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
654 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
655 if (! args(0).is_numeric_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
656 gripe_wrong_type_arg ("mod", args(0)); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
657 else if (! args(1).is_numeric_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
658 gripe_wrong_type_arg ("mod", args(1)); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
659 else if (args(0).is_complex_type () || args(1).is_complex_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
660 error ("mod: not defined for complex numbers"); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
661 else if (args(0).is_integer_type () || args(1).is_integer_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
662 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
663 builtin_type_t btyp0 = args(0).builtin_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
664 builtin_type_t btyp1 = args(1).builtin_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
665 if (btyp0 == btyp_double || btyp0 == btyp_float) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
666 btyp0 = btyp1; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
667 if (btyp1 == btyp_double || btyp1 == btyp_float) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
668 btyp1 = btyp0; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
669 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
670 if (btyp0 == btyp1) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
671 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
672 switch (btyp0) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
673 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
674 #define MAKE_INT_BRANCH(X) \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
675 case btyp_ ## X: \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
676 { \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
677 X##NDArray a0 = args(0).X##_array_value (); \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
678 X##NDArray a1 = args(1).X##_array_value (); \ |
10682
7b4ffe27bbb4
Specify template arguments explicitely [MSVC].
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10538
diff
changeset
|
679 retval = binmap<octave_##X,octave_##X,octave_##X> (a0, a1, mod, "mod"); \ |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
680 } \ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
681 break |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
682 MAKE_INT_BRANCH (int8); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
683 MAKE_INT_BRANCH (int16); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
684 MAKE_INT_BRANCH (int32); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
685 MAKE_INT_BRANCH (int64); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
686 MAKE_INT_BRANCH (uint8); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
687 MAKE_INT_BRANCH (uint16); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
688 MAKE_INT_BRANCH (uint32); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
689 MAKE_INT_BRANCH (uint64); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
690 #undef MAKE_INT_BRANCH |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
691 default: |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
692 panic_impossible (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
693 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
694 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
695 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
696 error ("mod: cannot combine %s and %d", |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
697 args(0).class_name ().c_str (), args(1).class_name ().c_str ()); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
698 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
699 else if (args(0).is_single_type () || args(1).is_single_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
700 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
701 if (args(0).is_scalar_type () && args(1).is_scalar_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
702 retval = mod (args(0).float_value (), args(1).float_value ()); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
703 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
704 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
705 FloatNDArray a0 = args(0).float_array_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
706 FloatNDArray a1 = args(1).float_array_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
707 retval = binmap<float> (a0, a1, mod, "mod"); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
708 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
709 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
710 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
711 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
712 bool a0_scalar = args(0).is_scalar_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
713 bool a1_scalar = args(1).is_scalar_type (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
714 if (a0_scalar && a1_scalar) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
715 retval = mod (args(0).scalar_value (), args(1).scalar_value ()); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
716 else if ((a0_scalar || args(0).is_sparse_type ()) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
717 && (a1_scalar || args(1).is_sparse_type ())) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
718 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
719 SparseMatrix m0 = args(0).sparse_matrix_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
720 SparseMatrix m1 = args(1).sparse_matrix_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
721 retval = binmap<double> (m0, m1, mod, "mod"); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
722 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
723 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
724 { |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
725 NDArray a0 = args(0).array_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
726 NDArray a1 = args(1).array_value (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
727 retval = binmap<double> (a0, a1, mod, "mod"); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
728 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
729 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
730 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
731 else |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
732 print_usage (); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
733 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
734 return retval; |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
735 } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
736 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
737 /* |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
738 ## empty input test |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
739 %!assert (isempty(mod([], []))); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
740 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
741 ## x mod y, y != 0 tests |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
742 %!assert (mod(5, 3), 2); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
743 %!assert (mod(-5, 3), 1); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
744 %!assert (mod(0, 3), 0); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
745 %!assert (mod([-5, 5, 0], [3, 3, 3]), [1, 2, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
746 %!assert (mod([-5; 5; 0], [3; 3; 3]), [1; 2; 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
747 %!assert (mod([-5, 5; 0, 3], [3, 3 ; 3, 1]), [1, 2 ; 0, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
748 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
749 ## x mod 0 tests |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
750 %!assert (mod(5, 0), 5); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
751 %!assert (mod(-5, 0), -5); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
752 %!assert (mod([-5, 5, 0], [3, 0, 3]), [1, 5, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
753 %!assert (mod([-5; 5; 0], [3; 0; 3]), [1; 5; 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
754 %!assert (mod([-5, 5; 0, 3], [3, 0 ; 3, 1]), [1, 5 ; 0, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
755 %!assert (mod([-5, 5; 0, 3], [0, 0 ; 0, 0]), [-5, 5; 0, 3]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
756 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
757 ## mixed scalar/matrix tests |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
758 %!assert (mod([-5, 5; 0, 3], 0), [-5, 5; 0, 3]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
759 %!assert (mod([-5, 5; 0, 3], 3), [1, 2; 0, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
760 %!assert (mod(-5,[0,0; 0,0]), [-5, -5; -5, -5]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
761 %!assert (mod(-5,[3,0; 3,1]), [1, -5; 1, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
762 %!assert (mod(-5,[3,2; 3,1]), [1, 1; 1, 0]); |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
763 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
764 ## integer types |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
765 %!assert (mod(uint8(5),uint8(4)),uint8(1)) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
766 %!assert (mod(uint8([1:5]),uint8(4)),uint8([1,2,3,0,1])) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
767 %!assert (mod(uint8([1:5]),uint8(0)),uint8([1:5])) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
768 %!error (mod(uint8(5),int8(4))) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
769 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
770 ## mixed integer/real types |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
771 %!assert (mod(uint8(5),4),uint8(1)) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
772 %!assert (mod(5,uint8(4)),uint8(1)) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
773 %!assert (mod(uint8([1:5]),4),uint8([1,2,3,0,1])) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
774 */ |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10435
diff
changeset
|
775 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
776 // FIXME Need to convert the reduction functions of this file for single precision |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
777 |
7112 | 778 #define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \ |
779 (arg.is_ ## TYPE ## _type ()) \ | |
780 { \ | |
781 TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \ | |
782 \ | |
783 if (! error_state) \ | |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
784 { \ |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
785 retval = tmp.FCN (DIM); \ |
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
786 } \ |
7112 | 787 } |
788 | |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
789 #define NATIVE_REDUCTION(FCN, BOOL_FCN) \ |
7112 | 790 \ |
791 octave_value retval; \ | |
792 \ | |
793 int nargin = args.length (); \ | |
794 \ | |
795 bool isnative = false; \ | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
796 bool isdouble = false; \ |
7112 | 797 \ |
798 if (nargin > 1 && args(nargin - 1).is_string ()) \ | |
799 { \ | |
800 std::string str = args(nargin - 1).string_value (); \ | |
801 \ | |
802 if (! error_state) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
803 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
804 if (str == "native") \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
805 isnative = true; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
806 else if (str == "double") \ |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
807 isdouble = true; \ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
808 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
809 error ("sum: unrecognized string argument"); \ |
7112 | 810 nargin --; \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
811 } \ |
7112 | 812 } \ |
813 \ | |
814 if (nargin == 1 || nargin == 2) \ | |
815 { \ | |
816 octave_value arg = args(0); \ | |
817 \ | |
818 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ | |
819 \ | |
820 if (! error_state) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
821 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
822 if (dim >= -1) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
823 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
824 if (arg.is_sparse_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
825 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
826 if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
827 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
828 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
829 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
830 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
831 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
832 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
833 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
834 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
835 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
836 \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
837 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
838 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
839 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
840 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
841 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
842 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
843 if (isnative) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
844 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
845 if NATIVE_REDUCTION_1 (FCN, uint8, dim) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
846 else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
847 else if NATIVE_REDUCTION_1 (FCN, uint32, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
848 else if NATIVE_REDUCTION_1 (FCN, uint64, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
849 else if NATIVE_REDUCTION_1 (FCN, int8, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
850 else if NATIVE_REDUCTION_1 (FCN, int16, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
851 else if NATIVE_REDUCTION_1 (FCN, int32, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
852 else if NATIVE_REDUCTION_1 (FCN, int64, dim) \ |
8756
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
853 else if (arg.is_bool_type ()) \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
854 { \ |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
855 boolNDArray tmp = arg.bool_array_value (); \ |
8756
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
856 if (! error_state) \ |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
857 retval = boolNDArray (tmp.BOOL_FCN (dim)); \ |
8756
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
858 } \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
859 else if (arg.is_char_matrix ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
860 { \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
861 error (#FCN, ": invalid char type"); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
862 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
863 else if (!isdouble && arg.is_single_type ()) \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
864 { \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
865 if (arg.is_complex_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
866 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
867 FloatComplexNDArray tmp = \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
868 arg.float_complex_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
869 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
870 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
871 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
872 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
873 else if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
874 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
875 FloatNDArray tmp = arg.float_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
876 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
877 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
878 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
879 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
880 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
881 else if (arg.is_complex_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
882 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
883 ComplexNDArray tmp = arg.complex_array_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
884 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
885 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
886 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
887 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
888 else if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
889 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
890 NDArray tmp = arg.array_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
891 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
892 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
893 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
894 } \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
895 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
896 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
897 gripe_wrong_type_arg (#FCN, arg); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
898 return retval; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
899 } \ |
7112 | 900 } \ |
8756
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
901 else if (arg.is_bool_type ()) \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
902 { \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
903 boolNDArray tmp = arg.bool_array_value (); \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
904 if (! error_state) \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
905 retval = tmp.FCN (dim); \ |
d0755c9db5ed
implement fast logical sum (counting)
Jaroslav Hajek <highegg@gmail.com>
parents:
8734
diff
changeset
|
906 } \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
907 else if (!isdouble && arg.is_single_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
908 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
909 if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
910 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
911 FloatNDArray tmp = arg.float_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
912 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
913 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
914 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
915 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
916 else if (arg.is_complex_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
917 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
918 FloatComplexNDArray tmp = \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
919 arg.float_complex_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
920 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
921 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
922 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
923 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
924 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
925 else if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
926 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
927 NDArray tmp = arg.array_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
928 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
929 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
930 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
931 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
932 else if (arg.is_complex_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
933 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
934 ComplexNDArray tmp = arg.complex_array_value (); \ |
7112 | 935 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
936 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
937 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
938 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
939 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
940 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
941 gripe_wrong_type_arg (#FCN, arg); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
942 return retval; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
943 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
944 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
945 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
946 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
947 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
948 } \ |
7112 | 949 \ |
950 } \ | |
951 else \ | |
952 print_usage (); \ | |
953 \ | |
954 return retval | |
955 | |
3723 | 956 #define DATA_REDUCTION(FCN) \ |
957 \ | |
4233 | 958 octave_value retval; \ |
3723 | 959 \ |
960 int nargin = args.length (); \ | |
961 \ | |
962 if (nargin == 1 || nargin == 2) \ | |
963 { \ | |
964 octave_value arg = args(0); \ | |
965 \ | |
3864 | 966 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723 | 967 \ |
968 if (! error_state) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
969 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
970 if (dim >= -1) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
971 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
972 if (arg.is_real_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
973 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
974 if (arg.is_sparse_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
975 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
976 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
3723 | 977 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
978 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
979 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
980 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
981 else if (arg.is_single_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
982 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
983 FloatNDArray tmp = arg.float_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
984 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
985 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
986 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
987 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
988 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
989 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
990 NDArray tmp = arg.array_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
991 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
992 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
993 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
994 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
995 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
996 else if (arg.is_complex_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
997 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
998 if (arg.is_sparse_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
999 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1000 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ |
3723 | 1001 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1002 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1003 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1004 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1005 else if (arg.is_single_type ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1006 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1007 FloatComplexNDArray tmp = arg.float_complex_array_value (); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
1008 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1009 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1010 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1011 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1012 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1013 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1014 ComplexNDArray tmp = arg.complex_array_value (); \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1015 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1016 if (! error_state) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1017 retval = tmp.FCN (dim); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1018 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1019 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1020 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1021 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1022 gripe_wrong_type_arg (#FCN, arg); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1023 return retval; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1024 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1025 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1026 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1027 error (#FCN ": invalid dimension argument = %d", dim + 1); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1028 } \ |
3723 | 1029 } \ |
1030 else \ | |
5823 | 1031 print_usage (); \ |
3723 | 1032 \ |
1033 return retval | |
1034 | |
1957 | 1035 DEFUN (cumprod, args, , |
3428 | 1036 "-*- texinfo -*-\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1037 @deftypefn {Built-in Function} {} cumprod (@var{x})\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1038 @deftypefnx {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
3723 | 1039 Cumulative product of elements along dimension @var{dim}. If\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
1040 @var{dim} is omitted, it defaults to the first non-singleton dimension.\n\ |
5061 | 1041 \n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1042 @seealso{prod, cumsum}\n\ |
3428 | 1043 @end deftypefn") |
523 | 1044 { |
3723 | 1045 DATA_REDUCTION (cumprod); |
523 | 1046 } |
1047 | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1048 /* |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1049 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1050 %!assert (cumprod ([1, 2, 3]), [1, 2, 6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1051 %!assert (cumprod ([-1; -2; -3]), [-1; 2; -6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1052 %!assert (cumprod ([i, 2+i, -3+2i, 4]), [i, -1+2i, -1-8i, -4-32i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1053 %!assert (cumprod ([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i]), [1, 2, 3; i, 4i, 9i; -1+i, -8+8i, -27+27i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1054 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1055 %!assert (cumprod (single([1, 2, 3])), single([1, 2, 6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1056 %!assert (cumprod (single([-1; -2; -3])), single([-1; 2; -6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1057 %!assert (cumprod (single([i, 2+i, -3+2i, 4])), single([i, -1+2i, -1-8i, -4-32i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1058 %!assert (cumprod (single([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i])), single([1, 2, 3; i, 4i, 9i; -1+i, -8+8i, -27+27i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1059 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1060 %!error <Invalid call to cumprod.*> cumprod (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1061 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1062 %!assert (cumprod ([2, 3; 4, 5], 1), [2, 3; 8, 15]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1063 %!assert (cumprod ([2, 3; 4, 5], 2), [2, 6; 4, 20]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1064 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1065 %!assert (cumprod (single([2, 3; 4, 5]), 1), single([2, 3; 8, 15])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1066 %!assert (cumprod (single([2, 3; 4, 5]), 2), single([2, 6; 4, 20])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1067 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1068 */ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1069 |
1957 | 1070 DEFUN (cumsum, args, , |
3428 | 1071 "-*- texinfo -*-\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1072 @deftypefn {Built-in Function} {} cumsum (@var{x})\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1073 @deftypefnx {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
1074 @deftypefnx {Built-in Function} {} cumsum (@dots{}, 'native')\n\ |
3723 | 1075 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
1076 is omitted, it defaults to the first non-singleton dimension.\n\ |
8780
ea76466605ba
support native cumsum, gripe on overflow in sum/cumsum
Jaroslav Hajek <highegg@gmail.com>
parents:
8756
diff
changeset
|
1077 \n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1078 The \"native\" argument implies the summation is performed in native type.\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1079 See @code{sum} for a complete description and example of the use of\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1080 \"native\".\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1081 @seealso{sum, cumprod}\n\ |
3428 | 1082 @end deftypefn") |
523 | 1083 { |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1084 octave_value retval; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1085 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1086 int nargin = args.length (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1087 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1088 bool isnative = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1089 bool isdouble = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1090 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1091 if (nargin > 1 && args(nargin - 1).is_string ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1092 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1093 std::string str = args(nargin - 1).string_value (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1094 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1095 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1096 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1097 if (str == "native") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1098 isnative = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1099 else if (str == "double") |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1100 isdouble = true; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1101 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1102 error ("sum: unrecognized string argument"); |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1103 nargin --; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1104 } |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1105 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1106 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1107 if (error_state) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1108 return retval; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1109 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1110 if (nargin == 1 || nargin == 2) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1111 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1112 octave_value arg = args(0); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1113 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1114 int dim = -1; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1115 if (nargin == 2) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1116 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1117 dim = args(1).int_value () - 1; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1118 if (dim < 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1119 error ("cumsum: invalid dimension argument = %d", dim + 1); |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1120 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1121 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1122 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1123 { |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1124 switch (arg.builtin_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1125 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1126 case btyp_double: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1127 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1128 retval = arg.sparse_matrix_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1129 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1130 retval = arg.array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1131 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1132 case btyp_complex: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1133 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1134 retval = arg.sparse_complex_matrix_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1135 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1136 retval = arg.complex_array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1137 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1138 case btyp_float: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1139 if (isdouble) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1140 retval = arg.array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1141 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1142 retval = arg.float_array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1143 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1144 case btyp_float_complex: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1145 if (isdouble) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1146 retval = arg.complex_array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1147 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1148 retval = arg.float_complex_array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1149 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1150 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1151 #define MAKE_INT_BRANCH(X) \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1152 case btyp_ ## X: \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1153 if (isnative) \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1154 retval = arg.X ## _array_value ().cumsum (dim); \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1155 else \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1156 retval = arg.array_value ().cumsum (dim); \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1157 break |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1158 MAKE_INT_BRANCH (int8); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1159 MAKE_INT_BRANCH (int16); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1160 MAKE_INT_BRANCH (int32); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1161 MAKE_INT_BRANCH (int64); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1162 MAKE_INT_BRANCH (uint8); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1163 MAKE_INT_BRANCH (uint16); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1164 MAKE_INT_BRANCH (uint32); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1165 MAKE_INT_BRANCH (uint64); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1166 #undef MAKE_INT_BRANCH |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1167 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1168 case btyp_bool: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1169 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1170 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1171 SparseMatrix cs = arg.sparse_matrix_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1172 if (isnative) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1173 retval = cs != 0.0; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1174 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1175 retval = cs; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1176 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1177 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1178 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1179 NDArray cs = arg.bool_array_value ().cumsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1180 if (isnative) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1181 retval = cs != 0.0; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1182 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1183 retval = cs; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1184 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1185 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1186 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1187 default: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1188 gripe_wrong_type_arg ("cumsum", arg); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1189 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1190 } |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1191 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1192 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1193 print_usage (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1194 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
1195 return retval; |
523 | 1196 } |
1197 | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1198 /* |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1199 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1200 %!assert (cumsum ([1, 2, 3]), [1, 3, 6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1201 %!assert (cumsum ([-1; -2; -3]), [-1; -3; -6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1202 %!assert (cumsum ([i, 2+i, -3+2i, 4]), [i, 2+2i, -1+4i, 3+4i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1203 %!assert (cumsum ([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i]), [1, 2, 3; 1+i, 2+2i, 3+3i; 2+2i, 4+4i, 6+6i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1204 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1205 %!assert (cumsum (single([1, 2, 3])), single([1, 3, 6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1206 %!assert (cumsum (single([-1; -2; -3])), single([-1; -3; -6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1207 %!assert (cumsum (single([i, 2+i, -3+2i, 4])), single([i, 2+2i, -1+4i, 3+4i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1208 %!assert (cumsum (single([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i])), single([1, 2, 3; 1+i, 2+2i, 3+3i; 2+2i, 4+4i, 6+6i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1209 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1210 %!error <Invalid call to cumsum.*> cumsum (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1211 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1212 %!assert (cumsum ([1, 2; 3, 4], 1), [1, 2; 4, 6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1213 %!assert (cumsum ([1, 2; 3, 4], 2), [1, 3; 3, 7]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1214 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1215 %!assert (cumsum (single([1, 2; 3, 4]), 1), single([1, 2; 4, 6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1216 %!assert (cumsum (single([1, 2; 3, 4]), 2), single([1, 3; 3, 7])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1217 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1218 */ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1219 |
1957 | 1220 DEFUN (diag, args, , |
3369 | 1221 "-*- texinfo -*-\n\ |
1222 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ | |
1223 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ | |
1224 second argument is optional. If it is positive, the vector is placed on\n\ | |
1225 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ | |
1226 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ | |
10840 | 1227 vector is placed on the main diagonal. For example:\n\ |
3369 | 1228 \n\ |
1229 @example\n\ | |
1230 @group\n\ | |
1231 diag ([1, 2, 3], 1)\n\ | |
1232 @result{} 0 1 0 0\n\ | |
1233 0 0 2 0\n\ | |
1234 0 0 0 3\n\ | |
1235 0 0 0 0\n\ | |
1236 @end group\n\ | |
1237 @end example\n\ | |
6772 | 1238 \n\ |
1239 @noindent\n\ | |
1240 Given a matrix argument, instead of a vector, @code{diag} extracts the\n\ | |
6774 | 1241 @var{k}-th diagonal of the matrix.\n\ |
3369 | 1242 @end deftypefn") |
523 | 1243 { |
4233 | 1244 octave_value retval; |
523 | 1245 |
1246 int nargin = args.length (); | |
1247 | |
712 | 1248 if (nargin == 1 && args(0).is_defined ()) |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1249 retval = args(0).diag(); |
712 | 1250 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1251 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1252 octave_idx_type k = args(1).int_value (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1253 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1254 if (error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1255 error ("diag: invalid second argument"); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1256 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1257 retval = args(0).diag(k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1258 } |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1259 else if (nargin == 3) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1260 { |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1261 octave_value arg0 = args(0); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1262 if (arg0.ndims () == 2 && (args(0).rows () == 1 || args(0).columns () == 1)) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1263 { |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1264 octave_idx_type m = args(1).int_value (), n = args(2).int_value (); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1265 if (! error_state) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1266 retval = arg0.diag ().resize (dim_vector (m, n)); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1267 else |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1268 error ("diag: invalid dimensions"); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1269 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1270 else |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1271 error ("diag: first argument must be a vector"); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8347
diff
changeset
|
1272 } |
523 | 1273 else |
5823 | 1274 print_usage (); |
523 | 1275 |
1276 return retval; | |
1277 } | |
1278 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1279 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1280 |
8459
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
1281 %!assert(full (diag ([1; 2; 3])), [1, 0, 0; 0, 2, 0; 0, 0, 3]); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1282 %!assert(diag ([1; 2; 3], 1), [0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1283 %!assert(diag ([1; 2; 3], 2), [0, 0, 1, 0, 0; 0, 0, 0, 2, 0; 0, 0, 0, 0, 3; 0, 0, 0, 0, 0; 0, 0, 0, 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1284 %!assert(diag ([1; 2; 3],-1), [0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1285 %!assert(diag ([1; 2; 3],-2), [0, 0, 0, 0, 0; 0, 0, 0, 0, 0; 1, 0, 0, 0, 0; 0, 2, 0, 0, 0; 0, 0, 3, 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1286 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1287 %!assert(diag ([1, 0, 0; 0, 2, 0; 0, 0, 3]), [1; 2; 3]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1288 %!assert(diag ([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0], 1), [1; 2; 3]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1289 %!assert(diag ([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0], -1), [1; 2; 3]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1290 |
8459
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
1291 %!assert(full (diag (single([1; 2; 3]))), single([1, 0, 0; 0, 2, 0; 0, 0, 3])); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1292 %!assert(diag (single([1; 2; 3]), 1), single([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1293 %!assert(diag (single([1; 2; 3]), 2), single([0, 0, 1, 0, 0; 0, 0, 0, 2, 0; 0, 0, 0, 0, 3; 0, 0, 0, 0, 0; 0, 0, 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1294 %!assert(diag (single([1; 2; 3]),-1), single([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1295 %!assert(diag (single([1; 2; 3]),-2), single([0, 0, 0, 0, 0; 0, 0, 0, 0, 0; 1, 0, 0, 0, 0; 0, 2, 0, 0, 0; 0, 0, 3, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1296 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1297 %!assert(diag (single([1, 0, 0; 0, 2, 0; 0, 0, 3])), single([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1298 %!assert(diag (single([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0]), 1), single([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1299 %!assert(diag (single([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0]), -1), single([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1300 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1301 %!assert(diag (int8([1; 2; 3])), int8([1, 0, 0; 0, 2, 0; 0, 0, 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1302 %!assert(diag (int8([1; 2; 3]), 1), int8([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1303 %!assert(diag (int8([1; 2; 3]), 2), int8([0, 0, 1, 0, 0; 0, 0, 0, 2, 0; 0, 0, 0, 0, 3; 0, 0, 0, 0, 0; 0, 0, 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1304 %!assert(diag (int8([1; 2; 3]),-1), int8([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1305 %!assert(diag (int8([1; 2; 3]),-2), int8([0, 0, 0, 0, 0; 0, 0, 0, 0, 0; 1, 0, 0, 0, 0; 0, 2, 0, 0, 0; 0, 0, 3, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1306 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1307 %!assert(diag (int8([1, 0, 0; 0, 2, 0; 0, 0, 3])), int8([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1308 %!assert(diag (int8([0, 1, 0, 0; 0, 0, 2, 0; 0, 0, 0, 3; 0, 0, 0, 0]), 1), int8([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1309 %!assert(diag (int8([0, 0, 0, 0; 1, 0, 0, 0; 0, 2, 0, 0; 0, 0, 3, 0]), -1), int8([1; 2; 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1310 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1311 %!error <Invalid call to diag.*> diag (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1312 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1313 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1314 |
1957 | 1315 DEFUN (prod, args, , |
3428 | 1316 "-*- texinfo -*-\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1317 @deftypefn {Built-in Function} {} prod (@var{x})\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1318 @deftypefnx {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
3723 | 1319 Product of elements along dimension @var{dim}. If @var{dim} is\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
1320 omitted, it defaults to the first non-singleton dimension.\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
1321 @seealso{cumprod, sum}\n\ |
3428 | 1322 @end deftypefn") |
523 | 1323 { |
3723 | 1324 DATA_REDUCTION (prod); |
523 | 1325 } |
1326 | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1327 /* |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1328 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1329 %!assert (prod ([1, 2, 3]), 6); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1330 %!assert (prod ([-1; -2; -3]), -6); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1331 %!assert (prod ([i, 2+i, -3+2i, 4]), -4 - 32i); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1332 %!assert (prod ([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i]), [-1+i, -8+8i, -27+27i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1333 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1334 %!assert (prod (single([1, 2, 3])), single(6)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1335 %!assert (prod (single([-1; -2; -3])), single(-6)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1336 %!assert (prod (single([i, 2+i, -3+2i, 4])), single(-4 - 32i)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1337 %!assert (prod (single([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i])), single([-1+i, -8+8i, -27+27i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1338 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1339 %!error <Invalid call to prod.*> prod (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1340 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1341 %!assert (prod ([1, 2; 3, 4], 1), [3, 8]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1342 %!assert (prod ([1, 2; 3, 4], 2), [2; 12]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1343 %!assert (prod (zeros (1, 0)), 1); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1344 %!assert (prod (zeros (1, 0), 1), zeros (1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1345 %!assert (prod (zeros (1, 0), 2), 1); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1346 %!assert (prod (zeros (0, 1)), 1); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1347 %!assert (prod (zeros (0, 1), 1), 1); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1348 %!assert (prod (zeros (0, 1), 2), zeros (0, 1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1349 %!assert (prod (zeros (2, 0)), zeros (1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1350 %!assert (prod (zeros (2, 0), 1), zeros (1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1351 %!assert (prod (zeros (2, 0), 2), [1; 1]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1352 %!assert (prod (zeros (0, 2)), [1, 1]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1353 %!assert (prod (zeros (0, 2), 1), [1, 1]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1354 %!assert (prod (zeros (0, 2), 2), zeros(0, 1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1355 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1356 %!assert (prod (single([1, 2; 3, 4]), 1), single([3, 8])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1357 %!assert (prod (single([1, 2; 3, 4]), 2), single([2; 12])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1358 %!assert (prod (zeros (1, 0, 'single')), single(1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1359 %!assert (prod (zeros (1, 0, 'single'), 1), zeros (1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1360 %!assert (prod (zeros (1, 0, 'single'), 2), single(1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1361 %!assert (prod (zeros (0, 1, 'single')), single(1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1362 %!assert (prod (zeros (0, 1, 'single'), 1), single(1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1363 %!assert (prod (zeros (0, 1, 'single'), 2), zeros (0, 1, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1364 %!assert (prod (zeros (2, 0, 'single')), zeros (1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1365 %!assert (prod (zeros (2, 0, 'single'), 1), zeros (1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1366 %!assert (prod (zeros (2, 0, 'single'), 2), single([1; 1])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1367 %!assert (prod (zeros (0, 2, 'single')), single([1, 1])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1368 %!assert (prod (zeros (0, 2, 'single'), 1), single([1, 1])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1369 %!assert (prod (zeros (0, 2, 'single'), 2), zeros(0, 1, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1370 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1371 */ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
1372 |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1373 static bool |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1374 all_scalar_1x1 (const octave_value_list& args) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1375 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1376 int n_args = args.length (); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1377 for (int i = 0; i < n_args; i++) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1378 if (args(i).numel () != 1) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1379 return false; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1380 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1381 return true; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1382 } |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1383 |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1384 template <class TYPE, class T> |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1385 static void |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1386 single_type_concat (Array<T>& result, |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1387 const octave_value_list& args, |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1388 int dim) |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1389 { |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1390 int n_args = args.length (); |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1391 if (! (equal_types<T, char>::value |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1392 || equal_types<T, octave_value>::value) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1393 && all_scalar_1x1 (args)) |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1394 { |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1395 // Optimize all scalars case. |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1396 dim_vector dv (1, 1); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1397 if (dim == -1 || dim == -2) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1398 dim = -dim - 1; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1399 else if (dim >= 2) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1400 dv.resize (dim+1, 1); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1401 dv(dim) = n_args; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1402 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1403 result.clear (dv); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1404 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1405 for (int j = 0; j < n_args && ! error_state; j++) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1406 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1407 octave_quit (); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1408 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1409 result(j) = octave_value_extract<T> (args(j)); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1410 } |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1411 } |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1412 else |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1413 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1414 OCTAVE_LOCAL_BUFFER (Array<T>, array_list, n_args); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1415 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1416 for (int j = 0; j < n_args && ! error_state; j++) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1417 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1418 octave_quit (); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1419 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1420 array_list[j] = octave_value_extract<TYPE> (args(j)); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1421 } |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1422 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1423 if (! error_state) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1424 result = Array<T>::cat (dim, n_args, array_list); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1425 } |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1426 } |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1427 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1428 template <class TYPE, class T> |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1429 static void |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1430 single_type_concat (Sparse<T>& result, |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1431 const octave_value_list& args, |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1432 int dim) |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1433 { |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1434 int n_args = args.length (); |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1435 OCTAVE_LOCAL_BUFFER (Sparse<T>, sparse_list, n_args); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1436 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1437 for (int j = 0; j < n_args && ! error_state; j++) |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1438 { |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1439 octave_quit (); |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1440 |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1441 sparse_list[j] = octave_value_extract<TYPE> (args(j)); |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1442 } |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1443 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1444 if (! error_state) |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1445 result = Sparse<T>::cat (dim, n_args, sparse_list); |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1446 } |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1447 |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1448 // Dispatcher. |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1449 template<class TYPE> |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1450 static TYPE |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1451 do_single_type_concat (const octave_value_list& args, int dim) |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1452 { |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1453 TYPE result; |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1454 |
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1455 single_type_concat<TYPE, typename TYPE::element_type> (result, args, dim); |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1456 |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1457 return result; |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1458 } |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1459 |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1460 template<class MAP> |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1461 static void |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1462 single_type_concat_map (octave_map& result, |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1463 const octave_value_list& args, |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1464 int dim) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1465 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1466 int n_args = args.length (); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1467 OCTAVE_LOCAL_BUFFER (MAP, map_list, n_args); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1468 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1469 for (int j = 0; j < n_args && ! error_state; j++) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1470 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1471 octave_quit (); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1472 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1473 map_list[j] = octave_value_extract<MAP> (args(j)); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1474 } |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1475 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1476 if (! error_state) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1477 result = octave_map::cat (dim, n_args, map_list); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1478 } |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1479 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1480 static octave_map |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1481 do_single_type_concat_map (const octave_value_list& args, |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1482 int dim) |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1483 { |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1484 octave_map result; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1485 if (all_scalar_1x1 (args)) // optimize all scalars case. |
10763
b397b8edd8c5
fix off-by-1 dim in scalar map horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10758
diff
changeset
|
1486 single_type_concat_map<octave_scalar_map> (result, args, dim); |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1487 else |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1488 single_type_concat_map<octave_map> (result, args, dim); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1489 |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1490 return result; |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1491 } |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1492 |
4824 | 1493 static octave_value |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1494 do_cat (const octave_value_list& args, int dim, std::string fname) |
4806 | 1495 { |
1496 octave_value retval; | |
1497 | |
4824 | 1498 int n_args = args.length (); |
4806 | 1499 |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1500 if (n_args == 0) |
5714 | 1501 retval = Matrix (); |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1502 else if (n_args == 1) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1503 retval = args(0); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1504 else if (n_args > 1) |
4824 | 1505 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1506 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1507 std::string result_type = args(0).class_name (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1508 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1509 bool all_sq_strings_p = args(0).is_sq_string (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1510 bool all_dq_strings_p = args(0).is_dq_string (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1511 bool all_real_p = args(0).is_real_type (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1512 bool any_sparse_p = args(0).is_sparse_type(); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1513 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1514 for (int i = 1; i < args.length (); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1515 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1516 result_type = |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1517 get_concat_class (result_type, args(i).class_name ()); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1518 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1519 if (all_sq_strings_p && ! args(i).is_sq_string ()) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1520 all_sq_strings_p = false; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1521 if (all_dq_strings_p && ! args(i).is_dq_string ()) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1522 all_dq_strings_p = false; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1523 if (all_real_p && ! args(i).is_real_type ()) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1524 all_real_p = false; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1525 if (!any_sparse_p && args(i).is_sparse_type ()) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1526 any_sparse_p = true; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1527 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1528 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1529 if (result_type == "double") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1530 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1531 if (any_sparse_p) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1532 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1533 if (all_real_p) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1534 retval = do_single_type_concat<SparseMatrix> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1535 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1536 retval = do_single_type_concat<SparseComplexMatrix> (args, dim); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1537 } |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1538 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1539 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1540 if (all_real_p) |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1541 retval = do_single_type_concat<NDArray> (args, dim); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1542 else |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1543 retval = do_single_type_concat<ComplexNDArray> (args, dim); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1544 } |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1545 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1546 else if (result_type == "single") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1547 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1548 if (all_real_p) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1549 retval = do_single_type_concat<FloatNDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1550 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1551 retval = do_single_type_concat<FloatComplexNDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1552 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1553 else if (result_type == "char") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1554 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1555 char type = all_dq_strings_p ? '"' : '\''; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1556 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1557 maybe_warn_string_concat (all_dq_strings_p, all_sq_strings_p); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1558 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1559 charNDArray result = do_single_type_concat<charNDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1560 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1561 retval = octave_value (result, type); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1562 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1563 else if (result_type == "logical") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1564 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1565 if (any_sparse_p) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1566 retval = do_single_type_concat<SparseBoolMatrix> (args, dim); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1567 else |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1568 retval = do_single_type_concat<boolNDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1569 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1570 else if (result_type == "int8") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1571 retval = do_single_type_concat<int8NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1572 else if (result_type == "int16") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1573 retval = do_single_type_concat<int16NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1574 else if (result_type == "int32") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1575 retval = do_single_type_concat<int32NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1576 else if (result_type == "int64") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1577 retval = do_single_type_concat<int64NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1578 else if (result_type == "uint8") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1579 retval = do_single_type_concat<uint8NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1580 else if (result_type == "uint16") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1581 retval = do_single_type_concat<uint16NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1582 else if (result_type == "uint32") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1583 retval = do_single_type_concat<uint32NDArray> (args, dim); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1584 else if (result_type == "uint64") |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1585 retval = do_single_type_concat<uint64NDArray> (args, dim); |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1586 else if (result_type == "cell") |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1587 retval = do_single_type_concat<Cell> (args, dim); |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1588 else if (result_type == "struct") |
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10716
diff
changeset
|
1589 retval = do_single_type_concat_map (args, dim); |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1590 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1591 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1592 dim_vector dv = args(0).dims (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1593 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1594 // Default concatenation. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1595 bool (dim_vector::*concat_rule) (const dim_vector&, int) = &dim_vector::concat; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1596 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1597 if (dim == -1 || dim == -2) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1598 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1599 concat_rule = &dim_vector::hvcat; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1600 dim = -dim - 1; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1601 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1602 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1603 for (int i = 1; i < args.length (); i++) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1604 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1605 if (! (dv.*concat_rule) (args(i).dims (), dim)) |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1606 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1607 // Dimensions do not match. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1608 error ("cat: dimension mismatch"); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1609 return retval; |
10533
f094ac9bc93e
reuse Array<T>::cat and Sparse<T>::cat in cat/horzcat/vertcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
1610 } |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1611 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1612 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1613 // The lines below might seem crazy, since we take a copy |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1614 // of the first argument, resize it to be empty and then resize |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1615 // it to be full. This is done since it means that there is no |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1616 // recopying of data, as would happen if we used a single resize. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1617 // It should be noted that resize operation is also significantly |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1618 // slower than the do_cat_op function, so it makes sense to have |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1619 // an empty matrix and copy all data. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1620 // |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1621 // We might also start with a empty octave_value using |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1622 // tmp = octave_value_typeinfo::lookup_type |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1623 // (args(1).type_name()); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1624 // and then directly resize. However, for some types there might |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1625 // be some additional setup needed, and so this should be avoided. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1626 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1627 octave_value tmp = args (0); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1628 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1629 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1630 if (error_state) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1631 return retval; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1632 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1633 int dv_len = dv.length (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1634 Array<octave_idx_type> ra_idx (dv_len, 1, 0); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1635 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1636 for (int j = 0; j < n_args; j++) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1637 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1638 // Can't fast return here to skip empty matrices as something |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1639 // like cat(1,[],single([])) must return an empty matrix of |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1640 // the right type. |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1641 tmp = do_cat_op (tmp, args (j), ra_idx); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1642 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1643 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1644 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1645 |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1646 dim_vector dv_tmp = args (j).dims (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1647 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1648 if (dim >= dv_len) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1649 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1650 if (j > 1) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1651 error ("%s: indexing error", fname.c_str ()); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1652 break; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1653 } |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1654 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1655 ra_idx (dim) += (dim < dv_tmp.length () ? |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1656 dv_tmp (dim) : 1); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1657 } |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1658 retval = tmp; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1659 } |
4806 | 1660 } |
1661 else | |
5823 | 1662 print_usage (); |
8113
38a797766a2a
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8111
diff
changeset
|
1663 |
4806 | 1664 return retval; |
1665 } | |
1666 | |
1667 DEFUN (horzcat, args, , | |
4824 | 1668 "-*- texinfo -*-\n\ |
4806 | 1669 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
1670 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ | |
1671 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ | |
5642 | 1672 @seealso{cat, vertcat}\n\ |
1673 @end deftypefn") | |
4806 | 1674 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1675 return do_cat (args, -2, "horzcat"); |
4806 | 1676 } |
1677 | |
1678 DEFUN (vertcat, args, , | |
1679 "-*- texinfo -*-\n\ | |
1680 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ | |
1681 Return the vertical concatenation of N-d array objects, @var{array1},\n\ | |
1682 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ | |
5642 | 1683 @seealso{cat, horzcat}\n\ |
1684 @end deftypefn") | |
4806 | 1685 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1686 return do_cat (args, -1, "vertcat"); |
4806 | 1687 } |
1688 | |
4758 | 1689 DEFUN (cat, args, , |
1690 "-*- texinfo -*-\n\ | |
1691 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ | |
4806 | 1692 Return the concatenation of N-d array objects, @var{array1},\n\ |
1693 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ | |
4758 | 1694 \n\ |
1695 @example\n\ | |
1696 @group\n\ | |
1697 A = ones (2, 2);\n\ | |
1698 B = zeros (2, 2);\n\ | |
1699 cat (2, A, B)\n\ | |
1700 @result{} ans =\n\ | |
1701 \n\ | |
1702 1 1 0 0\n\ | |
1703 1 1 0 0\n\ | |
1704 @end group\n\ | |
1705 @end example\n\ | |
1706 \n\ | |
1707 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ | |
1708 second dimension the following way:\n\ | |
1709 \n\ | |
1710 @example\n\ | |
1711 @group\n\ | |
1712 [A, B].\n\ | |
1713 @end group\n\ | |
1714 @end example\n\ | |
1715 \n\ | |
1716 @var{dim} can be larger than the dimensions of the N-d array objects\n\ | |
1717 and the result will thus have @var{dim} dimensions as the\n\ | |
1718 following example shows:\n\ | |
10840 | 1719 \n\ |
4758 | 1720 @example\n\ |
1721 @group\n\ | |
1722 cat (4, ones(2, 2), zeros (2, 2))\n\ | |
1723 @result{} ans =\n\ | |
1724 \n\ | |
1725 ans(:,:,1,1) =\n\ | |
1726 \n\ | |
1727 1 1\n\ | |
1728 1 1\n\ | |
1729 \n\ | |
1730 ans(:,:,1,2) =\n\ | |
1731 0 0\n\ | |
1732 0 0\n\ | |
1733 @end group\n\ | |
1734 @end example\n\ | |
5642 | 1735 @seealso{horzcat, vertcat}\n\ |
1736 @end deftypefn") | |
4758 | 1737 { |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1738 octave_value retval; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1739 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1740 if (args.length () > 0) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1741 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1742 int dim = args(0).int_value () - 1; |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1743 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1744 if (! error_state) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1745 { |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1746 if (dim >= 0) |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1747 retval = do_cat (args.slice (1, args.length () - 1), dim, "cat"); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1748 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1749 error ("cat: invalid dimension specified"); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1750 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1751 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1752 error ("cat: expecting first argument to be a integer"); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1753 } |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1754 else |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1755 print_usage (); |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1756 |
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1757 return retval; |
4758 | 1758 } |
1759 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1760 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1761 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1762 %!function ret = testcat (t1, t2, tr, cmplx) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1763 %! assert (cat (1, cast ([], t1), cast([], t2)), cast ([], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1764 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1765 %! assert (cat (1, cast (1, t1), cast (2, t2)), cast ([1; 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1766 %! assert (cat (1, cast (1, t1), cast ([2; 3], t2)), cast ([1; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1767 %! assert (cat (1, cast ([1; 2], t1), cast (3, t2)), cast ([1; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1768 %! assert (cat (1, cast ([1; 2], t1), cast ([3; 4], t2)), cast ([1; 2; 3; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1769 %! assert (cat (2, cast (1, t1), cast (2, t2)), cast ([1, 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1770 %! assert (cat (2, cast (1, t1), cast ([2, 3], t2)), cast ([1, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1771 %! assert (cat (2, cast ([1, 2], t1), cast (3, t2)), cast ([1, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1772 %! assert (cat (2, cast ([1, 2], t1), cast ([3, 4], t2)), cast ([1, 2, 3, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1773 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1774 %! assert ([cast(1, t1); cast(2, t2)], cast ([1; 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1775 %! assert ([cast(1, t1); cast([2; 3], t2)], cast ([1; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1776 %! assert ([cast([1; 2], t1); cast(3, t2)], cast ([1; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1777 %! assert ([cast([1; 2], t1); cast([3; 4], t2)], cast ([1; 2; 3; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1778 %! assert ([cast(1, t1), cast(2, t2)], cast ([1, 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1779 %! assert ([cast(1, t1), cast([2, 3], t2)], cast ([1, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1780 %! assert ([cast([1, 2], t1), cast(3, t2)], cast ([1, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1781 %! assert ([cast([1, 2], t1), cast([3, 4], t2)], cast ([1, 2, 3, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1782 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1783 %! if (nargin == 3 || cmplx) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1784 %! assert (cat (1, cast (1i, t1), cast (2, t2)), cast ([1i; 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1785 %! assert (cat (1, cast (1i, t1), cast ([2; 3], t2)), cast ([1i; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1786 %! assert (cat (1, cast ([1i; 2], t1), cast (3, t2)), cast ([1i; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1787 %! assert (cat (1, cast ([1i; 2], t1), cast ([3; 4], t2)), cast ([1i; 2; 3; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1788 %! assert (cat (2, cast (1i, t1), cast (2, t2)), cast ([1i, 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1789 %! assert (cat (2, cast (1i, t1), cast ([2, 3], t2)), cast ([1i, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1790 %! assert (cat (2, cast ([1i, 2], t1), cast (3, t2)), cast ([1i, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1791 %! assert (cat (2, cast ([1i, 2], t1), cast ([3, 4], t2)), cast ([1i, 2, 3, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1792 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1793 %! assert ([cast(1i, t1); cast(2, t2)], cast ([1i; 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1794 %! assert ([cast(1i, t1); cast([2; 3], t2)], cast ([1i; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1795 %! assert ([cast([1i; 2], t1); cast(3, t2)], cast ([1i; 2; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1796 %! assert ([cast([1i; 2], t1); cast([3; 4], t2)], cast ([1i; 2; 3; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1797 %! assert ([cast(1i, t1), cast(2, t2)], cast ([1i, 2], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1798 %! assert ([cast(1i, t1), cast([2, 3], t2)], cast ([1i, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1799 %! assert ([cast([1i, 2], t1), cast(3, t2)], cast ([1i, 2, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1800 %! assert ([cast([1i, 2], t1), cast([3, 4], t2)], cast ([1i, 2, 3, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1801 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1802 %! assert (cat (1, cast (1, t1), cast (2i, t2)), cast ([1; 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1803 %! assert (cat (1, cast (1, t1), cast ([2i; 3], t2)), cast ([1; 2i; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1804 %! assert (cat (1, cast ([1; 2], t1), cast (3i, t2)), cast ([1; 2; 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1805 %! assert (cat (1, cast ([1; 2], t1), cast ([3i; 4], t2)), cast ([1; 2; 3i; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1806 %! assert (cat (2, cast (1, t1), cast (2i, t2)), cast ([1, 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1807 %! assert (cat (2, cast (1, t1), cast ([2i, 3], t2)), cast ([1, 2i, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1808 %! assert (cat (2, cast ([1, 2], t1), cast (3i, t2)), cast ([1, 2, 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1809 %! assert (cat (2, cast ([1, 2], t1), cast ([3i, 4], t2)), cast ([1, 2, 3i, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1810 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1811 %! assert ([cast(1, t1); cast(2i, t2)], cast ([1; 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1812 %! assert ([cast(1, t1); cast([2i; 3], t2)], cast ([1; 2i; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1813 %! assert ([cast([1; 2], t1); cast(3i, t2)], cast ([1; 2; 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1814 %! assert ([cast([1; 2], t1); cast([3i; 4], t2)], cast ([1; 2; 3i; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1815 %! assert ([cast(1, t1), cast(2i, t2)], cast ([1, 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1816 %! assert ([cast(1, t1), cast([2i, 3], t2)], cast ([1, 2i, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1817 %! assert ([cast([1, 2], t1), cast(3i, t2)], cast ([1, 2, 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1818 %! assert ([cast([1, 2], t1), cast([3i, 4], t2)], cast ([1, 2, 3i, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1819 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1820 %! assert (cat (1, cast (1i, t1), cast (2i, t2)), cast ([1i; 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1821 %! assert (cat (1, cast (1i, t1), cast ([2i; 3], t2)), cast ([1i; 2i; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1822 %! assert (cat (1, cast ([1i; 2], t1), cast (3i, t2)), cast ([1i; 2; 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1823 %! assert (cat (1, cast ([1i; 2], t1), cast ([3i; 4], t2)), cast ([1i; 2; 3i; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1824 %! assert (cat (2, cast (1i, t1), cast (2i, t2)), cast ([1i, 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1825 %! assert (cat (2, cast (1i, t1), cast ([2i, 3], t2)), cast ([1i, 2i, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1826 %! assert (cat (2, cast ([1i, 2], t1), cast (3i, t2)), cast ([1i, 2, 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1827 %! assert (cat (2, cast ([1i, 2], t1), cast ([3i, 4], t2)), cast ([1i, 2, 3i, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1828 %! |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1829 %! assert ([cast(1i, t1); cast(2i, t2)], cast ([1i; 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1830 %! assert ([cast(1i, t1); cast([2i; 3], t2)], cast ([1i; 2i; 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1831 %! assert ([cast([1i; 2], t1); cast(3i, t2)], cast ([1i; 2; 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1832 %! assert ([cast([1i; 2], t1); cast([3i; 4], t2)], cast ([1i; 2; 3i; 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1833 %! assert ([cast(1i, t1), cast(2i, t2)], cast ([1i, 2i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1834 %! assert ([cast(1i, t1), cast([2i, 3], t2)], cast ([1i, 2i, 3], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1835 %! assert ([cast([1i, 2], t1), cast(3i, t2)], cast ([1i, 2, 3i], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1836 %! assert ([cast([1i, 2], t1), cast([3i, 4], t2)], cast ([1i, 2, 3i, 4], tr)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1837 %! endif |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1838 %! ret = true; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1839 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1840 %!assert (testcat('double', 'double', 'double')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1841 %!assert (testcat('single', 'double', 'single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1842 %!assert (testcat('double', 'single', 'single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1843 %!assert (testcat('single', 'single', 'single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1844 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1845 %!assert (testcat('double', 'int8', 'int8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1846 %!assert (testcat('int8', 'double', 'int8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1847 %!assert (testcat('single', 'int8', 'int8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1848 %!assert (testcat('int8', 'single', 'int8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1849 %!assert (testcat('int8', 'int8', 'int8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1850 %!assert (testcat('double', 'int16', 'int16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1851 %!assert (testcat('int16', 'double', 'int16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1852 %!assert (testcat('single', 'int16', 'int16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1853 %!assert (testcat('int16', 'single', 'int16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1854 %!assert (testcat('int16', 'int16', 'int16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1855 %!assert (testcat('double', 'int32', 'int32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1856 %!assert (testcat('int32', 'double', 'int32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1857 %!assert (testcat('single', 'int32', 'int32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1858 %!assert (testcat('int32', 'single', 'int32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1859 %!assert (testcat('int32', 'int32', 'int32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1860 %!assert (testcat('double', 'int64', 'int64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1861 %!assert (testcat('int64', 'double', 'int64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1862 %!assert (testcat('single', 'int64', 'int64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1863 %!assert (testcat('int64', 'single', 'int64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1864 %!assert (testcat('int64', 'int64', 'int64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1865 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1866 %!assert (testcat('double', 'uint8', 'uint8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1867 %!assert (testcat('uint8', 'double', 'uint8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1868 %!assert (testcat('single', 'uint8', 'uint8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1869 %!assert (testcat('uint8', 'single', 'uint8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1870 %!assert (testcat('uint8', 'uint8', 'uint8', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1871 %!assert (testcat('double', 'uint16', 'uint16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1872 %!assert (testcat('uint16', 'double', 'uint16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1873 %!assert (testcat('single', 'uint16', 'uint16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1874 %!assert (testcat('uint16', 'single', 'uint16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1875 %!assert (testcat('uint16', 'uint16', 'uint16', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1876 %!assert (testcat('double', 'uint32', 'uint32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1877 %!assert (testcat('uint32', 'double', 'uint32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1878 %!assert (testcat('single', 'uint32', 'uint32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1879 %!assert (testcat('uint32', 'single', 'uint32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1880 %!assert (testcat('uint32', 'uint32', 'uint32', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1881 %!assert (testcat('double', 'uint64', 'uint64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1882 %!assert (testcat('uint64', 'double', 'uint64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1883 %!assert (testcat('single', 'uint64', 'uint64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1884 %!assert (testcat('uint64', 'single', 'uint64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1885 %!assert (testcat('uint64', 'uint64', 'uint64', false)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1886 |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1887 %! assert (cat (3, [], [1,2;3,4]), [1,2;3,4]); |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1888 %! assert (cat (3, [1,2;3,4], []), [1,2;3,4]); |
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1889 %! assert (cat (3, [], [1,2;3,4], []), [1,2;3,4]); |
10695
701b969db6a3
data.cc: Fix test for concatentating empty nd-arrays.
Ben Abbott <bpabbott@mac.com>
parents:
10694
diff
changeset
|
1890 %! assert (cat (3, [], [], []), zeros (0, 0, 3)); |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10436
diff
changeset
|
1891 |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1892 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
1893 |
4593 | 1894 static octave_value |
6959 | 1895 do_permute (const octave_value_list& args, bool inv) |
4593 | 1896 { |
1897 octave_value retval; | |
1898 | |
5148 | 1899 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593 | 1900 { |
1901 Array<int> vec = args(1).int_vector_value (); | |
1902 | |
5775 | 1903 // FIXME -- maybe we should create an idx_vector object |
5148 | 1904 // here and pass that to permute? |
1905 | |
1906 int n = vec.length (); | |
1907 | |
1908 for (int i = 0; i < n; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1909 vec(i)--; |
5148 | 1910 |
4593 | 1911 octave_value ret = args(0).permute (vec, inv); |
1912 | |
1913 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
1914 retval = ret; |
4593 | 1915 } |
1916 else | |
5823 | 1917 print_usage (); |
4593 | 1918 |
1919 return retval; | |
1920 } | |
1921 | |
1922 DEFUN (permute, args, , | |
1923 "-*- texinfo -*-\n\ | |
1924 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ | |
1925 Return the generalized transpose for an N-d array object @var{a}.\n\ | |
1926 The permutation vector @var{perm} must contain the elements\n\ | |
1927 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ | |
5642 | 1928 @seealso{ipermute}\n\ |
1929 @end deftypefn") | |
4593 | 1930 { |
6959 | 1931 return do_permute (args, false); |
4593 | 1932 } |
1933 | |
1934 DEFUN (ipermute, args, , | |
1935 "-*- texinfo -*-\n\ | |
1936 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ | |
1937 The inverse of the @code{permute} function. The expression\n\ | |
1938 \n\ | |
1939 @example\n\ | |
1940 ipermute (permute (a, perm), perm)\n\ | |
1941 @end example\n\ | |
10840 | 1942 \n\ |
4593 | 1943 returns the original array @var{a}.\n\ |
5642 | 1944 @seealso{permute}\n\ |
1945 @end deftypefn") | |
4593 | 1946 { |
6959 | 1947 return do_permute (args, true); |
4593 | 1948 } |
1949 | |
3195 | 1950 DEFUN (length, args, , |
3373 | 1951 "-*- texinfo -*-\n\ |
1952 @deftypefn {Built-in Function} {} length (@var{a})\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1953 Return the \"length\" of the object @var{a}. For matrix objects, the\n\ |
3373 | 1954 length is the number of rows or columns, whichever is greater (this\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
1955 odd definition is used for compatibility with @sc{matlab}).\n\ |
3373 | 1956 @end deftypefn") |
3195 | 1957 { |
1958 octave_value retval; | |
1959 | |
1960 if (args.length () == 1) | |
9706
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
1961 retval = args(0).length (); |
3195 | 1962 else |
5823 | 1963 print_usage (); |
3195 | 1964 |
1965 return retval; | |
1966 } | |
1967 | |
4554 | 1968 DEFUN (ndims, args, , |
1969 "-*- texinfo -*-\n\ | |
1970 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1971 Return the number of dimensions of @var{a}.\n\ |
4554 | 1972 For any array, the result will always be larger than or equal to 2.\n\ |
1973 Trailing singleton dimensions are not counted.\n\ | |
10840 | 1974 \n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1975 @example\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
1976 @group\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1977 ndims (ones (4, 1, 2, 1)\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1978 @result{} 3\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
1979 @end group\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1980 @end example\n\ |
4554 | 1981 @end deftypefn") |
1982 { | |
1983 octave_value retval; | |
1984 | |
1985 if (args.length () == 1) | |
9706
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
1986 retval = args(0).ndims (); |
4554 | 1987 else |
5823 | 1988 print_usage (); |
4554 | 1989 |
1990 return retval; | |
1991 } | |
1992 | |
4559 | 1993 DEFUN (numel, args, , |
1994 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1995 @deftypefn {Built-in Function} {} numel (@var{a})\n\ |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
1996 @deftypefnx {Built-in Function} {} numel (@var{a}, @var{idx1}, @var{idx2}, @dots{})\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
1997 Return the number of elements in the object @var{a}.\n\ |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
1998 Optionally, if indices @var{idx1}, @var{idx2}, @dots{} are supplied,\n\ |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
1999 return the number of elements that would result from the indexing\n\ |
10840 | 2000 \n\ |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2001 @example\n\ |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2002 @var{a}(@var{idx1}, @var{idx2}, @dots{})\n\ |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2003 @end example\n\ |
10840 | 2004 \n\ |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2005 This method is also called when an object appears as lvalue with cs-list\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
2006 indexing, i.e., @code{object@{@dots{}@}} or @code{object(@dots{}).field}.\n\ |
5724 | 2007 @seealso{size}\n\ |
4559 | 2008 @end deftypefn") |
2009 { | |
2010 octave_value retval; | |
9329
67fc970dad7d
improve indexed assignment using indexed numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9319
diff
changeset
|
2011 octave_idx_type nargin = args.length (); |
67fc970dad7d
improve indexed assignment using indexed numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9319
diff
changeset
|
2012 |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2013 if (nargin == 1) |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2014 retval = args(0).numel (); |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2015 else if (nargin > 1) |
4559 | 2016 { |
9705
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2017 // Don't use numel (const octave_value_list&) here as that corresponds to |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2018 // an overloaded call, not to builtin! |
5acd99c3e794
avoid recursive overloaded calls in builtin numel
Jaroslav Hajek <highegg@gmail.com>
parents:
9689
diff
changeset
|
2019 retval = dims_to_numel (args(0).dims (), args.slice (1, nargin-1)); |
4559 | 2020 } |
2021 else | |
5823 | 2022 print_usage (); |
4559 | 2023 |
2024 return retval; | |
2025 } | |
2026 | |
1957 | 2027 DEFUN (size, args, nargout, |
3373 | 2028 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
2029 @deftypefn {Built-in Function} {} size (@var{a})\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
2030 @deftypefnx {Built-in Function} {} size (@var{a}, @var{dim})\n\ |
3373 | 2031 Return the number rows and columns of @var{a}.\n\ |
2032 \n\ | |
2033 With one input argument and one output argument, the result is returned\n\ | |
4741 | 2034 in a row vector. If there are multiple output arguments, the number of\n\ |
2035 rows is assigned to the first, and the number of columns to the second,\n\ | |
10840 | 2036 etc. For example:\n\ |
3373 | 2037 \n\ |
2038 @example\n\ | |
2039 @group\n\ | |
2040 size ([1, 2; 3, 4; 5, 6])\n\ | |
2041 @result{} [ 3, 2 ]\n\ | |
1031 | 2042 \n\ |
3373 | 2043 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
2044 @result{} nr = 3\n\ | |
2045 @result{} nc = 2\n\ | |
2046 @end group\n\ | |
2047 @end example\n\ | |
2048 \n\ | |
4741 | 2049 If given a second argument, @code{size} will return the size of the\n\ |
10840 | 2050 corresponding dimension. For example,\n\ |
1031 | 2051 \n\ |
3373 | 2052 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
2053 @group\n\ |
3373 | 2054 size ([1, 2; 3, 4; 5, 6], 2)\n\ |
2055 @result{} 2\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
2056 @end group\n\ |
3373 | 2057 @end example\n\ |
2058 \n\ | |
2059 @noindent\n\ | |
2060 returns the number of columns in the given matrix.\n\ | |
5724 | 2061 @seealso{numel}\n\ |
3373 | 2062 @end deftypefn") |
523 | 2063 { |
2086 | 2064 octave_value_list retval; |
523 | 2065 |
2066 int nargin = args.length (); | |
2067 | |
4513 | 2068 if (nargin == 1) |
523 | 2069 { |
9706
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2070 const dim_vector dimensions = args(0).dims (); |
4513 | 2071 |
2072 if (nargout > 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2073 { |
9706
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2074 const dim_vector rdims = dimensions.redim (nargout); |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2075 retval.resize (nargout); |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2076 for (int i = 0; i < nargout; i++) |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2077 retval(i) = rdims(i); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2078 } |
4513 | 2079 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2080 { |
9706
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2081 int ndims = dimensions.length (); |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2082 |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2083 NoAlias<Matrix> m (1, ndims); |
71160b139b07
simplify length,ndims,size and size_equal
Jaroslav Hajek <highegg@gmail.com>
parents:
9705
diff
changeset
|
2084 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2085 for (int i = 0; i < ndims; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2086 m(i) = dimensions(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2087 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2088 retval(0) = m; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2089 } |
1031 | 2090 } |
2091 else if (nargin == 2 && nargout < 2) | |
2092 { | |
5275 | 2093 octave_idx_type nd = args(1).int_value (true); |
1031 | 2094 |
2095 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2096 error ("size: expecting scalar as second argument"); |
712 | 2097 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2098 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2099 const dim_vector dv = args(0).dims (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2100 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2101 if (nd > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2102 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2103 if (nd <= dv.length ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2104 retval(0) = dv(nd-1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2105 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2106 retval(0) = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2107 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2108 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2109 error ("size: requested dimension (= %d) out of range", nd); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2110 } |
523 | 2111 } |
712 | 2112 else |
5823 | 2113 print_usage (); |
523 | 2114 |
2115 return retval; | |
2116 } | |
2117 | |
6156 | 2118 DEFUN (size_equal, args, , |
2119 "-*- texinfo -*-\n\ | |
6561 | 2120 @deftypefn {Built-in Function} {} size_equal (@var{a}, @var{b}, @dots{})\n\ |
2121 Return true if the dimensions of all arguments agree.\n\ | |
6156 | 2122 Trailing singleton dimensions are ignored.\n\ |
10002
e93c9bfff8b8
allow size_equal with 0 arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
9866
diff
changeset
|
2123 Called with a single or no argument, size_equal returns true.\n\ |
6156 | 2124 @seealso{size, numel}\n\ |
2125 @end deftypefn") | |
2126 { | |
2127 octave_value retval; | |
2128 | |
6561 | 2129 int nargin = args.length (); |
2130 | |
10002
e93c9bfff8b8
allow size_equal with 0 arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
9866
diff
changeset
|
2131 retval = true; |
e93c9bfff8b8
allow size_equal with 0 arguments
Jaroslav Hajek <highegg@gmail.com>
parents:
9866
diff
changeset
|
2132 |
8947
1e4b3149365a
allow size_equal called with 1 arg
Jaroslav Hajek <highegg@gmail.com>
parents:
8934
diff
changeset
|
2133 if (nargin >= 1) |
6156 | 2134 { |
2135 dim_vector a_dims = args(0).dims (); | |
6561 | 2136 |
2137 for (int i = 1; i < nargin; ++i) | |
2138 { | |
2139 dim_vector b_dims = args(i).dims (); | |
2140 | |
2141 if (a_dims != b_dims) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2142 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2143 retval = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2144 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2145 } |
6561 | 2146 } |
6156 | 2147 } |
2148 | |
2149 return retval; | |
2150 } | |
2151 | |
5602 | 2152 DEFUN (nnz, args, , |
2153 "-*- texinfo -*-\n\ | |
6156 | 2154 @deftypefn {Built-in Function} {@var{scalar} =} nnz (@var{a})\n\ |
2155 Returns the number of non zero elements in @var{a}.\n\ | |
5602 | 2156 @seealso{sparse}\n\ |
2157 @end deftypefn") | |
2158 { | |
2159 octave_value retval; | |
2160 | |
2161 if (args.length () == 1) | |
2162 retval = args(0).nnz (); | |
2163 else | |
5823 | 2164 print_usage (); |
5602 | 2165 |
2166 return retval; | |
2167 } | |
2168 | |
5604 | 2169 DEFUN (nzmax, args, , |
2170 "-*- texinfo -*-\n\ | |
6156 | 2171 @deftypefn {Built-in Function} {@var{scalar} =} nzmax (@var{SM})\n\ |
5604 | 2172 Return the amount of storage allocated to the sparse matrix @var{SM}.\n\ |
7001 | 2173 Note that Octave tends to crop unused memory at the first opportunity\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
2174 for sparse objects. There are some cases of user created sparse objects\n\ |
9066
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
2175 where the value returned by @dfn{nzmax} will not be the same as @dfn{nnz},\n\ |
5604 | 2176 but in general they will give the same result.\n\ |
2177 @seealso{sparse, spalloc}\n\ | |
2178 @end deftypefn") | |
2179 { | |
2180 octave_value retval; | |
2181 | |
2182 if (args.length() == 1) | |
2183 retval = args(0).nzmax (); | |
2184 else | |
5823 | 2185 print_usage (); |
5604 | 2186 |
2187 return retval; | |
2188 } | |
2189 | |
5677 | 2190 DEFUN (rows, args, , |
2191 "-*- texinfo -*-\n\ | |
2192 @deftypefn {Built-in Function} {} rows (@var{a})\n\ | |
2193 Return the number of rows of @var{a}.\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
2194 @seealso{columns, size, length, numel, isscalar, isvector, ismatrix}\n\ |
5677 | 2195 @end deftypefn") |
2196 { | |
2197 octave_value retval; | |
2198 | |
2199 if (args.length () == 1) | |
2200 retval = args(0).rows (); | |
2201 else | |
5823 | 2202 print_usage (); |
5677 | 2203 |
2204 return retval; | |
2205 } | |
2206 | |
2207 DEFUN (columns, args, , | |
2208 "-*- texinfo -*-\n\ | |
2209 @deftypefn {Built-in Function} {} columns (@var{a})\n\ | |
2210 Return the number of columns of @var{a}.\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
2211 @seealso{rows, size, length, numel, isscalar, isvector, ismatrix}\n\ |
5677 | 2212 @end deftypefn") |
2213 { | |
2214 octave_value retval; | |
2215 | |
2216 if (args.length () == 1) | |
2217 retval = args(0).columns (); | |
2218 else | |
5823 | 2219 print_usage (); |
5677 | 2220 |
2221 return retval; | |
2222 } | |
2223 | |
1957 | 2224 DEFUN (sum, args, , |
3428 | 2225 "-*- texinfo -*-\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2226 @deftypefn {Built-in Function} {} sum (@var{x})\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2227 @deftypefnx {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
7112 | 2228 @deftypefnx {Built-in Function} {} sum (@dots{}, 'native')\n\ |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2229 @deftypefnx {Built-in Function} {} sum (@dots{}, 'double')\n\ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2230 @deftypefnx {Built-in Function} {} sum (@dots{}, 'extra')\n\ |
3723 | 2231 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
2232 omitted, it defaults to the first non-singleton dimension.\n\ |
7112 | 2233 \n\ |
2234 If the optional argument 'native' is given, then the sum is performed\n\ | |
2235 in the same type as the original argument, rather than in the default\n\ | |
10840 | 2236 double type. For example:\n\ |
7112 | 2237 \n\ |
2238 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
2239 @group\n\ |
7112 | 2240 sum ([true, true])\n\ |
2241 @result{} 2\n\ | |
2242 sum ([true, true], 'native')\n\ | |
2243 @result{} true\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
2244 @end group\n\ |
7112 | 2245 @end example\n\ |
10840 | 2246 \n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
2247 \n\ |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2248 On the contrary, if 'double' is given, the sum is performed in double precision\n\ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2249 even for single precision inputs.\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
2250 \n\ |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2251 For double precision inputs, 'extra' indicates that a more accurate algorithm\n\ |
10840 | 2252 than straightforward summation is to be used. For single precision inputs,\n\ |
2253 'extra' is the same as 'double'. Otherwise, 'extra' has no effect.\n\ | |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2254 @seealso{cumsum, sumsq, prod}\n\ |
3428 | 2255 @end deftypefn") |
523 | 2256 { |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2257 octave_value retval; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2258 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2259 int nargin = args.length (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2260 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2261 bool isnative = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2262 bool isdouble = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2263 bool isextra = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2264 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2265 if (nargin > 1 && args(nargin - 1).is_string ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2266 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2267 std::string str = args(nargin - 1).string_value (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2268 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2269 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2270 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2271 if (str == "native") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2272 isnative = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2273 else if (str == "double") |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2274 isdouble = true; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2275 else if (str == "extra") |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2276 isextra = true; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2277 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2278 error ("sum: unrecognized string argument"); |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2279 nargin --; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2280 } |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2281 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2282 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2283 if (error_state) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2284 return retval; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2285 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2286 if (nargin == 1 || nargin == 2) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2287 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2288 octave_value arg = args(0); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2289 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2290 int dim = -1; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2291 if (nargin == 2) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2292 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2293 dim = args(1).int_value () - 1; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2294 if (dim < 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2295 error ("sum: invalid dimension argument = %d", dim + 1); |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2296 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2297 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2298 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2299 { |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2300 switch (arg.builtin_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2301 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2302 case btyp_double: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2303 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2304 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2305 if (isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2306 warning ("sum: 'extra' not yet implemented for sparse matrices"); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2307 retval = arg.sparse_matrix_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2308 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2309 else if (isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2310 retval = arg.array_value ().xsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2311 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2312 retval = arg.array_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2313 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2314 case btyp_complex: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2315 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2316 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2317 if (isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2318 warning ("sum: 'extra' not yet implemented for sparse matrices"); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2319 retval = arg.sparse_complex_matrix_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2320 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2321 else if (isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2322 retval = arg.complex_array_value ().xsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2323 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2324 retval = arg.complex_array_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2325 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2326 case btyp_float: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2327 if (isdouble || isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2328 retval = arg.float_array_value ().dsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2329 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2330 retval = arg.float_array_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2331 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2332 case btyp_float_complex: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2333 if (isdouble || isextra) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2334 retval = arg.float_complex_array_value ().dsum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2335 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2336 retval = arg.float_complex_array_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2337 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2338 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2339 #define MAKE_INT_BRANCH(X) \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2340 case btyp_ ## X: \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2341 if (isnative) \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2342 retval = arg.X ## _array_value ().sum (dim); \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2343 else \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2344 retval = arg.X ## _array_value ().dsum (dim); \ |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2345 break |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2346 MAKE_INT_BRANCH (int8); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2347 MAKE_INT_BRANCH (int16); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2348 MAKE_INT_BRANCH (int32); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2349 MAKE_INT_BRANCH (int64); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2350 MAKE_INT_BRANCH (uint8); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2351 MAKE_INT_BRANCH (uint16); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2352 MAKE_INT_BRANCH (uint32); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2353 MAKE_INT_BRANCH (uint64); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2354 #undef MAKE_INT_BRANCH |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2355 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2356 case btyp_bool: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2357 if (arg.is_sparse_type ()) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2358 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2359 if (isnative) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2360 retval = arg.sparse_bool_matrix_value ().any (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2361 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2362 retval = arg.sparse_matrix_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2363 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2364 else if (isnative) |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2365 retval = arg.bool_array_value ().any (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2366 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2367 retval = arg.bool_array_value ().sum (dim); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2368 break; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2369 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2370 default: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2371 gripe_wrong_type_arg ("sum", arg); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2372 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2373 } |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2374 } |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2375 else |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2376 print_usage (); |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2377 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9706
diff
changeset
|
2378 return retval; |
523 | 2379 } |
2380 | |
7112 | 2381 /* |
2382 | |
2383 %!assert (sum([true,true]), 2) | |
2384 %!assert (sum([true,true],'native'), true) | |
2385 %!assert (sum(int8([127,10,-20])), 117); | |
2386 %!assert (sum(int8([127,10,-20]),'native'), int8(107)); | |
2387 | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2388 %!assert(sum ([1, 2, 3]), 6) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2389 %!assert(sum ([-1; -2; -3]), -6); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2390 %!assert(sum ([i, 2+i, -3+2i, 4]), 3+4i); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2391 %!assert(sum ([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i]), [2+2i, 4+4i, 6+6i]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2392 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2393 %!assert(sum (single([1, 2, 3])), single(6)) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2394 %!assert(sum (single([-1; -2; -3])), single(-6)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2395 %!assert(sum (single([i, 2+i, -3+2i, 4])), single(3+4i)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2396 %!assert(sum (single([1, 2, 3; i, 2i, 3i; 1+i, 2+2i, 3+3i])), single([2+2i, 4+4i, 6+6i])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2397 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2398 %!error <Invalid call to sum.*> sum (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2399 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2400 %!assert (sum ([1, 2; 3, 4], 1), [4, 6]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2401 %!assert (sum ([1, 2; 3, 4], 2), [3; 7]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2402 %!assert (sum (zeros (1, 0)), 0); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2403 %!assert (sum (zeros (1, 0), 1), zeros(1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2404 %!assert (sum (zeros (1, 0), 2), 0); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2405 %!assert (sum (zeros (0, 1)), 0); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2406 %!assert (sum (zeros (0, 1), 1), 0); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2407 %!assert (sum (zeros (0, 1), 2), zeros(0, 1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2408 %!assert (sum (zeros (2, 0)), zeros(1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2409 %!assert (sum (zeros (2, 0), 1), zeros(1, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2410 %!assert (sum (zeros (2, 0), 2), [0; 0]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2411 %!assert (sum (zeros (0, 2)), [0, 0]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2412 %!assert (sum (zeros (0, 2), 1), [0, 0]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2413 %!assert (sum (zeros (0, 2), 2), zeros(0, 1)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2414 %!assert (sum (zeros (2, 2, 0, 3)), zeros(1, 2, 0, 3)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2415 %!assert (sum (zeros (2, 2, 0, 3), 2), zeros(2, 1, 0, 3)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2416 %!assert (sum (zeros (2, 2, 0, 3), 3), zeros(2, 2, 1, 3)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2417 %!assert (sum (zeros (2, 2, 0, 3), 4), zeros(2, 2, 0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2418 %!assert (sum (zeros (2, 2, 0, 3), 7), zeros(2, 2, 0, 3)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2419 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2420 %!assert (sum (single([1, 2; 3, 4]), 1), single([4, 6])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2421 %!assert (sum (single([1, 2; 3, 4]), 2), single([3; 7])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2422 %!assert (sum (zeros (1, 0, 'single')), single(0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2423 %!assert (sum (zeros (1, 0, 'single'), 1), zeros(1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2424 %!assert (sum (zeros (1, 0, 'single'), 2), single(0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2425 %!assert (sum (zeros (0, 1, 'single')), single(0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2426 %!assert (sum (zeros (0, 1, 'single'), 1), single(0)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2427 %!assert (sum (zeros (0, 1, 'single'), 2), zeros(0, 1, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2428 %!assert (sum (zeros (2, 0, 'single')), zeros(1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2429 %!assert (sum (zeros (2, 0, 'single'), 1), zeros(1, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2430 %!assert (sum (zeros (2, 0, 'single'), 2), single([0; 0])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2431 %!assert (sum (zeros (0, 2, 'single')), single([0, 0])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2432 %!assert (sum (zeros (0, 2, 'single'), 1), single([0, 0])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2433 %!assert (sum (zeros (0, 2, 'single'), 2), zeros(0, 1, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2434 %!assert (sum (zeros (2, 2, 0, 3, 'single')), zeros(1, 2, 0, 3, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2435 %!assert (sum (zeros (2, 2, 0, 3, 'single'), 2), zeros(2, 1, 0, 3, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2436 %!assert (sum (zeros (2, 2, 0, 3, 'single'), 3), zeros(2, 2, 1, 3, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2437 %!assert (sum (zeros (2, 2, 0, 3, 'single'), 4), zeros(2, 2, 0, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2438 %!assert (sum (zeros (2, 2, 0, 3, 'single'), 7), zeros(2, 2, 0, 3, 'single')); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2439 |
7112 | 2440 */ |
2441 | |
1957 | 2442 DEFUN (sumsq, args, , |
3428 | 2443 "-*- texinfo -*-\n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2444 @deftypefn {Built-in Function} {} sumsq (@var{x})\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2445 @deftypefnx {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
3723 | 2446 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
9723
f426899f4b9c
fix reduction functions docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9721
diff
changeset
|
2447 is omitted, it defaults to the first non-singleton dimension.\n\ |
5061 | 2448 \n\ |
2449 This function is conceptually equivalent to computing\n\ | |
10840 | 2450 \n\ |
3723 | 2451 @example\n\ |
2452 sum (x .* conj (x), dim)\n\ | |
2453 @end example\n\ | |
10840 | 2454 \n\ |
9163
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2455 but it uses less memory and avoids calling @code{conj} if @var{x} is real.\n\ |
9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9155
diff
changeset
|
2456 @seealso{sum}\n\ |
3428 | 2457 @end deftypefn") |
523 | 2458 { |
3723 | 2459 DATA_REDUCTION (sumsq); |
523 | 2460 } |
2461 | |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2462 /* |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2463 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2464 %!assert(sumsq ([1, 2, 3]), 14) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2465 %!assert(sumsq ([-1; -2; 4i]), 21); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2466 %!assert(sumsq ([1, 2, 3; 2, 3, 4; 4i, 6i, 2]), [21, 49, 29]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2467 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2468 %!assert(sumsq (single([1, 2, 3])), single(14)) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2469 %!assert(sumsq (single([-1; -2; 4i])), single(21)); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2470 %!assert(sumsq (single([1, 2, 3; 2, 3, 4; 4i, 6i, 2])), single([21, 49, 29])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2471 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2472 %!error <Invalid call to sumsq.*> sumsq (); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2473 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2474 %!assert (sumsq ([1, 2; 3, 4], 1), [10, 20]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2475 %!assert (sumsq ([1, 2; 3, 4], 2), [5; 25]); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2476 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2477 %!assert (sumsq (single([1, 2; 3, 4]), 1), single([10, 20])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2478 %!assert (sumsq (single([1, 2; 3, 4]), 2), single([5; 25])); |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2479 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2480 */ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2481 |
6688 | 2482 DEFUN (islogical, args, , |
3428 | 2483 "-*- texinfo -*-\n\ |
7144 | 2484 @deftypefn {Built-in Function} {} islogical (@var{x})\n\ |
6688 | 2485 Return true if @var{x} is a logical object.\n\ |
3439 | 2486 @end deftypefn") |
3209 | 2487 { |
2488 octave_value retval; | |
2489 | |
2490 if (args.length () == 1) | |
3258 | 2491 retval = args(0).is_bool_type (); |
3209 | 2492 else |
5823 | 2493 print_usage (); |
3209 | 2494 |
2495 return retval; | |
2496 } | |
2497 | |
6688 | 2498 DEFALIAS (isbool, islogical); |
3209 | 2499 |
7815
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2500 /* |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2501 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2502 %!assert (islogical(true), true) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2503 %!assert (islogical(false), true) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2504 %!assert (islogical([true, false]), true) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2505 %!assert (islogical(1), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2506 %!assert (islogical(1i), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2507 %!assert (islogical([1,1]), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2508 %!assert (islogical(single(1)), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2509 %!assert (islogical(single(1i)), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2510 %!assert (islogical(single([1,1])), false) |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2511 |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2512 */ |
a41df65f3f00
Add some single precision test code and fix resulting bugs
David Bateman <dbateman@free.fr>
parents:
7814
diff
changeset
|
2513 |
6223 | 2514 DEFUN (isinteger, args, , |
2515 "-*- texinfo -*-\n\ | |
6230 | 2516 @deftypefn {Built-in Function} {} isinteger (@var{x})\n\ |
6223 | 2517 Return true if @var{x} is an integer object (int8, uint8, int16, etc.).\n\ |
2518 Note that @code{isinteger (14)} is false because numeric constants in\n\ | |
8429
f34ab2b433e8
One word missing from int8 help string.
Francesco Potortì <pot@gnu.org>
parents:
8366
diff
changeset
|
2519 Octave are double precision floating point values.\n\ |
6223 | 2520 @seealso{isreal, isnumeric, class, isa}\n\ |
2521 @end deftypefn") | |
2522 { | |
2523 octave_value retval; | |
2524 | |
2525 if (args.length () == 1) | |
2526 retval = args(0).is_integer_type (); | |
2527 else | |
2528 print_usage (); | |
2529 | |
2530 return retval; | |
2531 } | |
2532 | |
4028 | 2533 DEFUN (iscomplex, args, , |
3428 | 2534 "-*- texinfo -*-\n\ |
4028 | 2535 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428 | 2536 Return true if @var{x} is a complex-valued numeric object.\n\ |
2537 @end deftypefn") | |
3186 | 2538 { |
2539 octave_value retval; | |
2540 | |
2541 if (args.length () == 1) | |
3258 | 2542 retval = args(0).is_complex_type (); |
3186 | 2543 else |
5823 | 2544 print_usage (); |
3186 | 2545 |
2546 return retval; | |
2547 } | |
2548 | |
7576 | 2549 DEFUN (isfloat, args, , |
2550 "-*- texinfo -*-\n\ | |
2551 @deftypefn {Built-in Function} {} isfloat (@var{x})\n\ | |
2552 Return true if @var{x} is a floating-point numeric object.\n\ | |
2553 @end deftypefn") | |
2554 { | |
2555 octave_value retval; | |
2556 | |
2557 if (args.length () == 1) | |
2558 retval = args(0).is_float_type (); | |
2559 else | |
2560 print_usage (); | |
2561 | |
2562 return retval; | |
2563 } | |
2564 | |
5775 | 2565 // FIXME -- perhaps this should be implemented with an |
5476 | 2566 // octave_value member function? |
2567 | |
2568 DEFUN (complex, args, , | |
2569 "-*- texinfo -*-\n\ | |
9309
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2570 @deftypefn {Built-in Function} {} complex (@var{x})\n\ |
5476 | 2571 @deftypefnx {Built-in Function} {} complex (@var{re}, @var{im})\n\ |
9309
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2572 Return a complex result from real arguments. With 1 real argument @var{x},\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2573 return the complex result @code{@var{x} + 0i}. With 2 real arguments,\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2574 return the complex result @code{@var{re} + @var{im}}. @code{complex} can\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2575 often be more convenient than expressions such as @code{a + i*b}.\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2576 For example:\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2577 \n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2578 @example\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
2579 @group\n\ |
9309
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2580 complex ([1, 2], [3, 4])\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2581 @result{}\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2582 1 + 3i 2 + 4i\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
2583 @end group\n\ |
9309
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2584 @end example\n\ |
fb8b8589dd46
Expand documentation for 'complex' function
Rik <rdrider0-list@yahoo.com>
parents:
9308
diff
changeset
|
2585 @seealso{real, imag, iscomplex}\n\ |
5476 | 2586 @end deftypefn") |
2587 { | |
2588 octave_value retval; | |
2589 | |
2590 int nargin = args.length (); | |
2591 | |
2592 if (nargin == 1) | |
2593 { | |
2594 octave_value arg = args(0); | |
2595 | |
2596 if (arg.is_complex_type ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2597 retval = arg; |
5476 | 2598 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2599 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2600 if (arg.is_sparse_type ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2601 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2602 SparseComplexMatrix val = arg.sparse_complex_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2603 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2604 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2605 retval = octave_value (new octave_sparse_complex_matrix (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2606 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2607 else if (arg.is_single_type ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2608 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2609 if (arg.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2610 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2611 FloatComplex val = arg.float_complex_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2612 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2613 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2614 retval = octave_value (new octave_float_complex (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2615 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2616 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2617 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2618 FloatComplexNDArray val = arg.float_complex_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2619 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2620 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2621 retval = octave_value (new octave_float_complex_matrix (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2622 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2623 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2624 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2625 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2626 if (arg.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2627 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2628 Complex val = arg.complex_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2629 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2630 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2631 retval = octave_value (new octave_complex (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2632 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2633 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2634 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2635 ComplexNDArray val = arg.complex_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2636 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2637 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2638 retval = octave_value (new octave_complex_matrix (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2639 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2640 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2641 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2642 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2643 error ("complex: invalid conversion"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2644 } |
5476 | 2645 } |
2646 else if (nargin == 2) | |
2647 { | |
2648 octave_value re = args(0); | |
2649 octave_value im = args(1); | |
2650 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
2651 if (re.is_sparse_type () && im.is_sparse_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2652 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2653 const SparseMatrix re_val = re.sparse_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2654 const SparseMatrix im_val = im.sparse_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2655 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2656 if (!error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2657 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2658 if (re.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2659 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2660 SparseComplexMatrix result; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2661 if (re_val.nnz () == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2662 result = Complex(0, 1) * SparseComplexMatrix (im_val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2663 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2664 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2665 result = SparseComplexMatrix (im_val.dims (), re_val (0)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2666 octave_idx_type nr = im_val.rows (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2667 octave_idx_type nc = im_val.cols (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2668 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2669 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2670 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2671 octave_idx_type off = j * nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2672 for (octave_idx_type i = im_val.cidx(j); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2673 i < im_val.cidx(j + 1); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2674 result.data (im_val.ridx(i) + off) = |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2675 result.data (im_val.ridx(i) + off) + |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2676 Complex (0, im_val.data (i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2677 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2678 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2679 retval = octave_value (new octave_sparse_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2680 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2681 else if (im.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2682 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2683 SparseComplexMatrix result; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2684 if (im_val.nnz () == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2685 result = SparseComplexMatrix (re_val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2686 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2687 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2688 result = SparseComplexMatrix (re_val.rows(), re_val.cols(), Complex(0, im_val (0))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2689 octave_idx_type nr = re_val.rows (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2690 octave_idx_type nc = re_val.cols (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2691 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2692 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2693 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2694 octave_idx_type off = j * nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2695 for (octave_idx_type i = re_val.cidx(j); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2696 i < re_val.cidx(j + 1); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2697 result.data (re_val.ridx(i) + off) = |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2698 result.data (re_val.ridx(i) + off) + |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2699 re_val.data (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2700 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2701 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2702 retval = octave_value (new octave_sparse_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2703 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2704 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2705 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2706 if (re_val.dims () == im_val.dims ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2707 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2708 SparseComplexMatrix result = SparseComplexMatrix(re_val) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2709 + Complex(0, 1) * SparseComplexMatrix (im_val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2710 retval = octave_value (new octave_sparse_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2711 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2712 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2713 error ("complex: dimension mismatch"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2714 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2715 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2716 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
2717 else if (re.is_single_type () || im.is_single_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2718 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2719 if (re.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2720 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2721 float re_val = re.float_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2722 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2723 if (im.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2724 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2725 float im_val = im.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2726 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2727 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2728 retval = octave_value (new octave_float_complex (FloatComplex (re_val, im_val))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2729 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2730 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2731 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2732 const FloatNDArray im_val = im.float_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2733 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2734 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2735 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2736 FloatComplexNDArray result (im_val.dims (), FloatComplex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2737 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2738 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2739 result.xelem (i) = FloatComplex (re_val, im_val(i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2740 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2741 retval = octave_value (new octave_float_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2742 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2743 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2744 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2745 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2746 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2747 const FloatNDArray re_val = re.float_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2748 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2749 if (im.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2750 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2751 float im_val = im.float_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2752 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2753 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2754 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2755 FloatComplexNDArray result (re_val.dims (), FloatComplex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2756 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2757 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2758 result.xelem (i) = FloatComplex (re_val(i), im_val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2759 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2760 retval = octave_value (new octave_float_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2761 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2762 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2763 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2764 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2765 const FloatNDArray im_val = im.float_array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2766 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2767 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2768 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2769 if (re_val.dims () == im_val.dims ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2770 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2771 FloatComplexNDArray result (re_val.dims (), FloatComplex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2772 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2773 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2774 result.xelem (i) = FloatComplex (re_val(i), im_val(i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2775 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2776 retval = octave_value (new octave_float_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2777 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2778 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2779 error ("complex: dimension mismatch"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2780 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2781 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2782 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2783 } |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
2784 else if (re.numel () == 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2785 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2786 double re_val = re.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2787 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2788 if (im.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2789 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2790 double im_val = im.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2791 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2792 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2793 retval = octave_value (new octave_complex (Complex (re_val, im_val))); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2794 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2795 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2796 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2797 const NDArray im_val = im.array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2798 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2799 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2800 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2801 ComplexNDArray result (im_val.dims (), Complex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2802 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2803 for (octave_idx_type i = 0; i < im_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2804 result.xelem (i) = Complex (re_val, im_val(i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2805 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2806 retval = octave_value (new octave_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2807 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2808 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2809 } |
5476 | 2810 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2811 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2812 const NDArray re_val = re.array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2813 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2814 if (im.numel () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2815 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2816 double im_val = im.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2817 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2818 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2819 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2820 ComplexNDArray result (re_val.dims (), Complex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2821 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2822 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2823 result.xelem (i) = Complex (re_val(i), im_val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2824 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2825 retval = octave_value (new octave_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2826 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2827 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2828 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2829 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2830 const NDArray im_val = im.array_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2831 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2832 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2833 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2834 if (re_val.dims () == im_val.dims ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2835 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2836 ComplexNDArray result (re_val.dims (), Complex ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2837 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2838 for (octave_idx_type i = 0; i < re_val.numel (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2839 result.xelem (i) = Complex (re_val(i), im_val(i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2840 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2841 retval = octave_value (new octave_complex_matrix (result)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2842 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2843 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2844 error ("complex: dimension mismatch"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2845 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2846 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2847 } |
5476 | 2848 |
2849 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2850 error ("complex: invalid conversion"); |
5476 | 2851 } |
2852 else | |
5823 | 2853 print_usage (); |
5476 | 2854 |
2855 return retval; | |
2856 } | |
2857 | |
3258 | 2858 DEFUN (isreal, args, , |
3428 | 2859 "-*- texinfo -*-\n\ |
2860 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ | |
10291
fc879f361bda
make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10290
diff
changeset
|
2861 Return true if @var{x} is a non-complex matrix or scalar.\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
2862 For compatibility with @sc{matlab}, this includes logical and character\n\ |
10291
fc879f361bda
make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10290
diff
changeset
|
2863 matrices.\n\ |
3428 | 2864 @end deftypefn") |
3258 | 2865 { |
2866 octave_value retval; | |
2867 | |
2868 if (args.length () == 1) | |
2869 retval = args(0).is_real_type (); | |
2870 else | |
5823 | 2871 print_usage (); |
3258 | 2872 |
2873 return retval; | |
2874 } | |
2875 | |
3202 | 2876 DEFUN (isempty, args, , |
3373 | 2877 "-*- texinfo -*-\n\ |
2878 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ | |
2879 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ | |
2880 the number of columns, or both are zero). Otherwise, return 0.\n\ | |
2881 @end deftypefn") | |
3202 | 2882 { |
4233 | 2883 octave_value retval = false; |
3202 | 2884 |
2885 if (args.length () == 1) | |
4559 | 2886 retval = args(0).is_empty (); |
3202 | 2887 else |
5823 | 2888 print_usage (); |
3202 | 2889 |
2890 return retval; | |
2891 } | |
2892 | |
3206 | 2893 DEFUN (isnumeric, args, , |
3428 | 2894 "-*- texinfo -*-\n\ |
2895 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ | |
10840 | 2896 Return nonzero if @var{x} is a numeric object, i.e., an integer, real or complex\n\ |
2897 array. Logical and character arrays are not considered to be numeric.\n\ | |
10290
1756b3035282
clarify isnumeric and ismatrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10285
diff
changeset
|
2898 @seealso{ischar, islogical, isinteger}\n\ |
3428 | 2899 @end deftypefn") |
3206 | 2900 { |
2901 octave_value retval; | |
2902 | |
2903 if (args.length () == 1) | |
3258 | 2904 retval = args(0).is_numeric_type (); |
3206 | 2905 else |
5823 | 2906 print_usage (); |
3206 | 2907 |
2908 return retval; | |
2909 } | |
2910 | |
4028 | 2911 DEFUN (ismatrix, args, , |
3321 | 2912 "-*- texinfo -*-\n\ |
4028 | 2913 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
10290
1756b3035282
clarify isnumeric and ismatrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10285
diff
changeset
|
2914 Return 1 if @var{a} is a numeric, logical or character matrix or scalar.\n\ |
1756b3035282
clarify isnumeric and ismatrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10285
diff
changeset
|
2915 Otherwise, return 0.\n\ |
3333 | 2916 @end deftypefn") |
3202 | 2917 { |
4233 | 2918 octave_value retval = false; |
3202 | 2919 |
2920 if (args.length () == 1) | |
2921 { | |
2922 octave_value arg = args(0); | |
2923 | |
9866
b5aa5d9ee6b9
make ismatrix consistent for empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9858
diff
changeset
|
2924 retval = arg.is_matrix_type () || arg.is_scalar_type () || arg.is_range (); |
3202 | 2925 } |
2926 else | |
5823 | 2927 print_usage (); |
3202 | 2928 |
2929 return retval; | |
2930 } | |
2931 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2932 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2933 |
9866
b5aa5d9ee6b9
make ismatrix consistent for empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9858
diff
changeset
|
2934 %!assert(ismatrix ([])); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2935 %!assert(ismatrix (1)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2936 %!assert(ismatrix ([1, 2, 3])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2937 %!assert(ismatrix ([1, 2; 3, 4])); |
9866
b5aa5d9ee6b9
make ismatrix consistent for empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9858
diff
changeset
|
2938 %!assert(ismatrix (zeros (3, 2, 4))); |
b5aa5d9ee6b9
make ismatrix consistent for empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9858
diff
changeset
|
2939 |
b5aa5d9ee6b9
make ismatrix consistent for empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9858
diff
changeset
|
2940 %!assert(ismatrix (single([]))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2941 %!assert(ismatrix (single(1))); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2942 %!assert(ismatrix (single([1, 2, 3]))); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2943 %!assert(ismatrix (single([1, 2; 3, 4]))); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2944 |
10024
184060864627
allow ismatrix on character matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10002
diff
changeset
|
2945 %!assert(ismatrix ("t")); |
184060864627
allow ismatrix on character matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10002
diff
changeset
|
2946 %!assert(ismatrix ("test")); |
184060864627
allow ismatrix on character matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10002
diff
changeset
|
2947 %!assert(ismatrix (["test"; "ing"])); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2948 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2949 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2950 %! s.a = 1; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2951 %! assert(ismatrix (s), false); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2952 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2953 %!error <Invalid call to ismatrix.*> ismatrix (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2954 %!error <Invalid call to ismatrix.*> ismatrix ([1, 2; 3, 4], 2); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2955 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2956 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
2957 |
3354 | 2958 static octave_value |
5747 | 2959 fill_matrix (const octave_value_list& args, int val, const char *fcn) |
523 | 2960 { |
3354 | 2961 octave_value retval; |
523 | 2962 |
2963 int nargin = args.length (); | |
2964 | |
4946 | 2965 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481 | 2966 |
4946 | 2967 dim_vector dims (1, 1); |
4481 | 2968 |
2969 if (nargin > 0 && args(nargin-1).is_string ()) | |
2970 { | |
4946 | 2971 std::string nm = args(nargin-1).string_value (); |
4481 | 2972 nargin--; |
2973 | |
4946 | 2974 dt = oct_data_conv::string_to_data_type (nm); |
2975 | |
2976 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2977 return retval; |
4481 | 2978 } |
2979 | |
523 | 2980 switch (nargin) |
2981 { | |
712 | 2982 case 0: |
2983 break; | |
777 | 2984 |
610 | 2985 case 1: |
4481 | 2986 get_dimensions (args(0), fcn, dims); |
610 | 2987 break; |
777 | 2988 |
4563 | 2989 default: |
2990 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2991 dims.resize (nargin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2992 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2993 for (int i = 0; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2994 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2995 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2996 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2997 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2998 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
2999 error ("%s: expecting scalar integer arguments", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3000 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3001 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3002 } |
4563 | 3003 } |
3004 break; | |
4481 | 3005 } |
3006 | |
3007 if (! error_state) | |
3008 { | |
4946 | 3009 dims.chop_trailing_singletons (); |
4565 | 3010 |
4481 | 3011 check_dimensions (dims, fcn); |
3354 | 3012 |
5775 | 3013 // FIXME -- perhaps this should be made extensible by |
4946 | 3014 // using the class name to lookup a function to call to create |
3015 // the new value. | |
3016 | |
3017 // Note that automatic narrowing will handle conversion from | |
3018 // NDArray to scalar. | |
3019 | |
4481 | 3020 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3021 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3022 switch (dt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3023 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3024 case oct_data_conv::dt_int8: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3025 retval = int8NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3026 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3027 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3028 case oct_data_conv::dt_uint8: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3029 retval = uint8NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3030 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3031 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3032 case oct_data_conv::dt_int16: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3033 retval = int16NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3034 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3035 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3036 case oct_data_conv::dt_uint16: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3037 retval = uint16NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3038 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3039 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3040 case oct_data_conv::dt_int32: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3041 retval = int32NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3042 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3043 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3044 case oct_data_conv::dt_uint32: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3045 retval = uint32NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3046 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3047 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3048 case oct_data_conv::dt_int64: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3049 retval = int64NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3050 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3051 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3052 case oct_data_conv::dt_uint64: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3053 retval = uint64NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3054 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3055 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3056 case oct_data_conv::dt_single: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3057 retval = FloatNDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3058 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3059 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3060 case oct_data_conv::dt_double: |
8972
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3061 { |
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3062 if (val == 1 && dims.length () == 2 && dims (0) == 1) |
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3063 retval = Range (1.0, 0.0, dims (1)); // packed form |
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3064 else |
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3065 retval = NDArray (dims, val); |
5fa53d1b6247
more indexing optimizations
Jaroslav Hajek <highegg@gmail.com>
parents:
8947
diff
changeset
|
3066 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3067 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3068 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3069 case oct_data_conv::dt_logical: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3070 retval = boolNDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3071 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3072 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3073 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3074 error ("%s: invalid class name", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3075 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3076 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3077 } |
523 | 3078 } |
3079 | |
3080 return retval; | |
3081 } | |
3082 | |
5747 | 3083 static octave_value |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3084 fill_matrix (const octave_value_list& args, double val, float fval, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3085 const char *fcn) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3086 { |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3087 octave_value retval; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3088 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3089 int nargin = args.length (); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3090 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3091 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3092 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3093 dim_vector dims (1, 1); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3094 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3095 if (nargin > 0 && args(nargin-1).is_string ()) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3096 { |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3097 std::string nm = args(nargin-1).string_value (); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3098 nargin--; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3099 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3100 dt = oct_data_conv::string_to_data_type (nm); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3101 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3102 if (error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3103 return retval; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3104 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3105 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3106 switch (nargin) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3107 { |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3108 case 0: |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3109 break; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3110 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3111 case 1: |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3112 get_dimensions (args(0), fcn, dims); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3113 break; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3114 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3115 default: |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3116 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3117 dims.resize (nargin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3118 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3119 for (int i = 0; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3120 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3121 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3122 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3123 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3124 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3125 error ("%s: expecting scalar integer arguments", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3126 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3127 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3128 } |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3129 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3130 break; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3131 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3132 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3133 if (! error_state) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3134 { |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3135 dims.chop_trailing_singletons (); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3136 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3137 check_dimensions (dims, fcn); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3138 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3139 // Note that automatic narrowing will handle conversion from |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3140 // NDArray to scalar. |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3141 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3142 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3143 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3144 switch (dt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3145 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3146 case oct_data_conv::dt_single: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3147 retval = FloatNDArray (dims, fval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3148 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3149 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3150 case oct_data_conv::dt_double: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3151 retval = NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3152 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3153 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3154 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3155 error ("%s: invalid class name", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3156 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3157 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3158 } |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3159 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3160 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3161 return retval; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3162 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3163 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3164 static octave_value |
5747 | 3165 fill_matrix (const octave_value_list& args, double val, const char *fcn) |
3166 { | |
3167 octave_value retval; | |
3168 | |
3169 int nargin = args.length (); | |
3170 | |
3171 oct_data_conv::data_type dt = oct_data_conv::dt_double; | |
3172 | |
3173 dim_vector dims (1, 1); | |
3174 | |
3175 if (nargin > 0 && args(nargin-1).is_string ()) | |
3176 { | |
3177 std::string nm = args(nargin-1).string_value (); | |
3178 nargin--; | |
3179 | |
3180 dt = oct_data_conv::string_to_data_type (nm); | |
3181 | |
3182 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3183 return retval; |
5747 | 3184 } |
3185 | |
3186 switch (nargin) | |
3187 { | |
3188 case 0: | |
3189 break; | |
3190 | |
3191 case 1: | |
3192 get_dimensions (args(0), fcn, dims); | |
3193 break; | |
3194 | |
3195 default: | |
3196 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3197 dims.resize (nargin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3198 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3199 for (int i = 0; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3200 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3201 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3202 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3203 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3204 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3205 error ("%s: expecting scalar integer arguments", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3206 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3207 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3208 } |
5747 | 3209 } |
3210 break; | |
3211 } | |
3212 | |
3213 if (! error_state) | |
3214 { | |
3215 dims.chop_trailing_singletons (); | |
3216 | |
3217 check_dimensions (dims, fcn); | |
3218 | |
3219 // Note that automatic narrowing will handle conversion from | |
3220 // NDArray to scalar. | |
3221 | |
3222 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3223 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3224 switch (dt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3225 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3226 case oct_data_conv::dt_single: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3227 retval = FloatNDArray (dims, static_cast <float> (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3228 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3229 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3230 case oct_data_conv::dt_double: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3231 retval = NDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3232 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3233 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3234 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3235 error ("%s: invalid class name", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3236 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3237 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3238 } |
5747 | 3239 } |
3240 | |
3241 return retval; | |
3242 } | |
3243 | |
3244 static octave_value | |
3245 fill_matrix (const octave_value_list& args, const Complex& val, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3246 const char *fcn) |
5747 | 3247 { |
3248 octave_value retval; | |
3249 | |
3250 int nargin = args.length (); | |
3251 | |
3252 oct_data_conv::data_type dt = oct_data_conv::dt_double; | |
3253 | |
3254 dim_vector dims (1, 1); | |
3255 | |
3256 if (nargin > 0 && args(nargin-1).is_string ()) | |
3257 { | |
3258 std::string nm = args(nargin-1).string_value (); | |
3259 nargin--; | |
3260 | |
3261 dt = oct_data_conv::string_to_data_type (nm); | |
3262 | |
3263 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3264 return retval; |
5747 | 3265 } |
3266 | |
3267 switch (nargin) | |
3268 { | |
3269 case 0: | |
3270 break; | |
3271 | |
3272 case 1: | |
3273 get_dimensions (args(0), fcn, dims); | |
3274 break; | |
3275 | |
3276 default: | |
3277 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3278 dims.resize (nargin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3279 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3280 for (int i = 0; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3281 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3282 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3283 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3284 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3285 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3286 error ("%s: expecting scalar integer arguments", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3287 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3288 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3289 } |
5747 | 3290 } |
3291 break; | |
3292 } | |
3293 | |
3294 if (! error_state) | |
3295 { | |
3296 dims.chop_trailing_singletons (); | |
3297 | |
3298 check_dimensions (dims, fcn); | |
3299 | |
3300 // Note that automatic narrowing will handle conversion from | |
3301 // NDArray to scalar. | |
3302 | |
3303 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3304 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3305 switch (dt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3306 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3307 case oct_data_conv::dt_single: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3308 retval = FloatComplexNDArray (dims, static_cast<FloatComplex> (val)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3309 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3310 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3311 case oct_data_conv::dt_double: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3312 retval = ComplexNDArray (dims, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3313 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3314 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3315 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3316 error ("%s: invalid class name", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3317 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3318 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3319 } |
5747 | 3320 } |
3321 | |
3322 return retval; | |
3323 } | |
3324 | |
3325 static octave_value | |
3326 fill_matrix (const octave_value_list& args, bool val, const char *fcn) | |
3327 { | |
3328 octave_value retval; | |
3329 | |
3330 int nargin = args.length (); | |
3331 | |
3332 dim_vector dims (1, 1); | |
3333 | |
3334 switch (nargin) | |
3335 { | |
3336 case 0: | |
3337 break; | |
3338 | |
3339 case 1: | |
3340 get_dimensions (args(0), fcn, dims); | |
3341 break; | |
3342 | |
3343 default: | |
3344 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3345 dims.resize (nargin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3346 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3347 for (int i = 0; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3348 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3349 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3350 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3351 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3352 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3353 error ("%s: expecting scalar integer arguments", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3354 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3355 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3356 } |
5747 | 3357 } |
3358 break; | |
3359 } | |
3360 | |
3361 if (! error_state) | |
3362 { | |
3363 dims.chop_trailing_singletons (); | |
3364 | |
3365 check_dimensions (dims, fcn); | |
3366 | |
3367 // Note that automatic narrowing will handle conversion from | |
3368 // NDArray to scalar. | |
3369 | |
3370 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3371 retval = boolNDArray (dims, val); |
5747 | 3372 } |
3373 | |
3374 return retval; | |
3375 } | |
3376 | |
3354 | 3377 DEFUN (ones, args, , |
3369 | 3378 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3379 @deftypefn {Built-in Function} {} ones (@var{x})\n\ |
3369 | 3380 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ |
4948 | 3381 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
3382 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ | |
4481 | 3383 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
9428
b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
John W. Eaton <jwe@octave.org>
parents:
9351
diff
changeset
|
3384 If invoked with a single scalar integer argument, return a square\n\ |
b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
John W. Eaton <jwe@octave.org>
parents:
9351
diff
changeset
|
3385 matrix of the specified size. If invoked with two or more scalar\n\ |
b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
John W. Eaton <jwe@octave.org>
parents:
9351
diff
changeset
|
3386 integer arguments, or a vector of integer values, return an array with\n\ |
b317debeb828
data.cc (Fones, Fzeros, Ftrue, Ffalse): update docstring
John W. Eaton <jwe@octave.org>
parents:
9351
diff
changeset
|
3387 given dimensions.\n\ |
3369 | 3388 \n\ |
3389 If you need to create a matrix whose values are all the same, you should\n\ | |
3390 use an expression like\n\ | |
3391 \n\ | |
3392 @example\n\ | |
3393 val_matrix = val * ones (n, m)\n\ | |
3394 @end example\n\ | |
4945 | 3395 \n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3396 The optional argument @var{class} specifies the class of the return array\n\ |
10840 | 3397 and defaults to double. For example:\n\ |
4945 | 3398 \n\ |
3399 @example\n\ | |
3400 val = ones (n,m, \"uint8\")\n\ | |
3401 @end example\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3402 @seealso{zeros}\n\ |
3369 | 3403 @end deftypefn") |
523 | 3404 { |
5747 | 3405 return fill_matrix (args, 1, "ones"); |
523 | 3406 } |
3407 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3408 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3409 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3410 %!assert(ones (3), [1, 1, 1; 1, 1, 1; 1, 1, 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3411 %!assert(ones (2, 3), [1, 1, 1; 1, 1, 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3412 %!assert(ones (3, 2), [1, 1; 1, 1; 1, 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3413 %!assert(size (ones (3, 4, 5)), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3414 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3415 %!assert(ones (3,'single'), single([1, 1, 1; 1, 1, 1; 1, 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3416 %!assert(ones (2, 3,'single'), single([1, 1, 1; 1, 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3417 %!assert(ones (3, 2,'single'), single([1, 1; 1, 1; 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3418 %!assert(size (ones (3, 4, 5, 'single')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3419 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3420 %!assert(ones (3,'int8'), int8([1, 1, 1; 1, 1, 1; 1, 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3421 %!assert(ones (2, 3,'int8'), int8([1, 1, 1; 1, 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3422 %!assert(ones (3, 2,'int8'), int8([1, 1; 1, 1; 1, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3423 %!assert(size (ones (3, 4, 5, 'int8')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3424 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3425 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3426 |
3354 | 3427 DEFUN (zeros, args, , |
3369 | 3428 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3429 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ |
3369 | 3430 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ |
4948 | 3431 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
3432 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ | |
4481 | 3433 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3434 If invoked with a single scalar integer argument, return a square\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3435 matrix of the specified size. If invoked with two or more scalar\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3436 integer arguments, or a vector of integer values, return an array with\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3437 given dimensions.\n\ |
4945 | 3438 \n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3439 The optional argument @var{class} specifies the class of the return array\n\ |
10840 | 3440 and defaults to double. For example:\n\ |
4945 | 3441 \n\ |
3442 @example\n\ | |
3443 val = zeros (n,m, \"uint8\")\n\ | |
3444 @end example\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3445 @seealso{ones}\n\ |
3369 | 3446 @end deftypefn") |
523 | 3447 { |
5747 | 3448 return fill_matrix (args, 0, "zeros"); |
3449 } | |
3450 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3451 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3452 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3453 %!assert(zeros (3), [0, 0, 0; 0, 0, 0; 0, 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3454 %!assert(zeros (2, 3), [0, 0, 0; 0, 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3455 %!assert(zeros (3, 2), [0, 0; 0, 0; 0, 0]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3456 %!assert(size (zeros (3, 4, 5)), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3457 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3458 %!assert(zeros (3,'single'), single([0, 0, 0; 0, 0, 0; 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3459 %!assert(zeros (2, 3,'single'), single([0, 0, 0; 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3460 %!assert(zeros (3, 2,'single'), single([0, 0; 0, 0; 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3461 %!assert(size (zeros (3, 4, 5, 'single')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3462 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3463 %!assert(zeros (3,'int8'), int8([0, 0, 0; 0, 0, 0; 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3464 %!assert(zeros (2, 3,'int8'), int8([0, 0, 0; 0, 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3465 %!assert(zeros (3, 2,'int8'), int8([0, 0; 0, 0; 0, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3466 %!assert(size (zeros (3, 4, 5, 'int8')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3467 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3468 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3469 |
5747 | 3470 DEFUN (Inf, args, , |
3471 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3472 @deftypefn {Built-in Function} {} Inf\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3473 @deftypefnx {Built-in Function} {} Inf (@var{n})\n\ |
5747 | 3474 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m})\n\ |
3475 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3476 @deftypefnx {Built-in Function} {} Inf (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3477 Return a scalar, matrix or N-dimensional array whose elements are all equal\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3478 to the IEEE representation for positive infinity.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3479 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3480 Infinity is produced when results are too large to be represented using the\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3481 the IEEE floating point format for numbers. Two common examples which\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3482 produce infinity are division by zero and overflow.\n\ |
10840 | 3483 \n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3484 @example\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3485 @group\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3486 [1/0 e^800]\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3487 @result{}\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3488 Inf Inf\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3489 @end group\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3490 @end example\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3491 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3492 When called with no arguments, return a scalar with the value @samp{Inf}.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3493 When called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3494 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3495 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3496 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3497 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3498 either \"double\" or \"single\".\n\ |
9308
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3499 @seealso{isinf}\n\ |
5747 | 3500 @end deftypefn") |
3501 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3502 return fill_matrix (args, lo_ieee_inf_value (), |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3503 lo_ieee_float_inf_value (), "Inf"); |
5747 | 3504 } |
3505 | |
3506 DEFALIAS (inf, Inf); | |
3507 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3508 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3509 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3510 %!assert(inf (3), [Inf, Inf, Inf; Inf, Inf, Inf; Inf, Inf, Inf]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3511 %!assert(inf (2, 3), [Inf, Inf, Inf; Inf, Inf, Inf]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3512 %!assert(inf (3, 2), [Inf, Inf; Inf, Inf; Inf, Inf]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3513 %!assert(size (inf (3, 4, 5)), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3514 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3515 %!assert(inf (3,'single'), single([Inf, Inf, Inf; Inf, Inf, Inf; Inf, Inf, Inf])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3516 %!assert(inf (2, 3,'single'), single([Inf, Inf, Inf; Inf, Inf, Inf])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3517 %!assert(inf (3, 2,'single'), single([Inf, Inf; Inf, Inf; Inf, Inf])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3518 %!assert(size (inf (3, 4, 5, 'single')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3519 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3520 %!error(inf (3,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3521 %!error(inf (2, 3,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3522 %!error(inf (3, 2,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3523 %!error(inf (3, 4, 5, 'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3524 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3525 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3526 |
5747 | 3527 DEFUN (NaN, args, , |
3528 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3529 @deftypefn {Built-in Function} {} NaN\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3530 @deftypefnx {Built-in Function} {} NaN (@var{n})\n\ |
5747 | 3531 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m})\n\ |
3532 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3533 @deftypefnx {Built-in Function} {} NaN (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3534 Return a scalar, matrix, or N-dimensional array whose elements are all equal\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3535 to the IEEE symbol NaN (Not a Number).\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3536 NaN is the result of operations which do not produce a well defined numerical\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3537 result. Common operations which produce a NaN are arithmetic with infinity\n\ |
5747 | 3538 @tex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3539 ($\\infty - \\infty$), zero divided by zero ($0/0$),\n\ |
5747 | 3540 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3541 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3542 (Inf - Inf), zero divided by zero (0/0),\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3543 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3544 and any operation involving another NaN value (5 + NaN).\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3545 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3546 Note that NaN always compares not equal to NaN (NaN != NaN). This behavior\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3547 is specified by the IEEE standard for floating point arithmetic. To\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3548 find NaN values, use the @code{isnan} function.\n\ |
5747 | 3549 \n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3550 When called with no arguments, return a scalar with the value @samp{NaN}.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3551 When called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3552 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3553 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3554 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3555 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3556 either \"double\" or \"single\".\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3557 @seealso{isnan}\n\ |
5747 | 3558 @end deftypefn") |
3559 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3560 return fill_matrix (args, lo_ieee_nan_value (), |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3561 lo_ieee_float_nan_value (), "NaN"); |
5747 | 3562 } |
3563 | |
3564 DEFALIAS (nan, NaN); | |
3565 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3566 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3567 %!assert(NaN (3), [NaN, NaN, NaN; NaN, NaN, NaN; NaN, NaN, NaN]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3568 %!assert(NaN (2, 3), [NaN, NaN, NaN; NaN, NaN, NaN]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3569 %!assert(NaN (3, 2), [NaN, NaN; NaN, NaN; NaN, NaN]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3570 %!assert(size (NaN (3, 4, 5)), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3571 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3572 %!assert(NaN (3,'single'), single([NaN, NaN, NaN; NaN, NaN, NaN; NaN, NaN, NaN])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3573 %!assert(NaN (2, 3,'single'), single([NaN, NaN, NaN; NaN, NaN, NaN])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3574 %!assert(NaN (3, 2,'single'), single([NaN, NaN; NaN, NaN; NaN, NaN])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3575 %!assert(size (NaN (3, 4, 5, 'single')), [3, 4, 5]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3576 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3577 %!error(NaN (3,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3578 %!error(NaN (2, 3,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3579 %!error(NaN (3, 2,'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3580 %!error(NaN (3, 4, 5, 'int8')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3581 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3582 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
3583 |
5747 | 3584 DEFUN (e, args, , |
3585 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3586 @deftypefn {Built-in Function} {} e\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3587 @deftypefnx {Built-in Function} {} e (@var{n})\n\ |
5747 | 3588 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m})\n\ |
3589 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3590 @deftypefnx {Built-in Function} {} e (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3591 Return a scalar, matrix, or N-dimensional array whose elements are all equal\n\ |
5747 | 3592 to the base of natural logarithms. The constant\n\ |
3593 @tex\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3594 $e$ satisfies the equation $\\log (e) = 1$.\n\ |
5747 | 3595 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3596 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3597 @samp{e} satisfies the equation @code{log} (e) = 1.\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3598 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3599 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3600 When called with no arguments, return a scalar with the value @math{e}. When\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3601 called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3602 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3603 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3604 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3605 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3606 either \"double\" or \"single\".\n\ |
5747 | 3607 @end deftypefn") |
3608 { | |
3609 #if defined (M_E) | |
3610 double e_val = M_E; | |
3611 #else | |
3612 double e_val = exp (1.0); | |
3613 #endif | |
3614 | |
3615 return fill_matrix (args, e_val, "e"); | |
3616 } | |
3617 | |
3618 DEFUN (eps, args, , | |
3619 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3620 @deftypefn {Built-in Function} {} eps\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3621 @deftypefnx {Built-in Function} {} eps (@var{x})\n\ |
5747 | 3622 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m})\n\ |
3623 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3624 @deftypefnx {Built-in Function} {} eps (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3625 Return a scalar, matrix or N-dimensional array whose elements are all eps,\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3626 the machine precision. More precisely, @code{eps} is the relative spacing\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3627 between any two adjacent numbers in the machine's floating point system.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3628 This number is obviously system dependent. On machines that support IEEE\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3629 floating point arithmetic, @code{eps} is approximately\n\ |
5747 | 3630 @tex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3631 $2.2204\\times10^{-16}$ for double precision and $1.1921\\times10^{-7}$\n\ |
5747 | 3632 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3633 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3634 2.2204e-16 for double precision and 1.1921e-07\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3635 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3636 for single precision.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3637 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3638 When called with no arguments, return a scalar with the value\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3639 @code{eps(1.0)}.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3640 Given a single argument @var{x}, return the distance between @var{x} and\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3641 the next largest value.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3642 When called with more than one argument the first two arguments are taken as\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3643 the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3644 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3645 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3646 either \"double\" or \"single\".\n\ |
5747 | 3647 @end deftypefn") |
3648 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3649 int nargin = args.length (); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3650 octave_value retval; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3651 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3652 if (nargin == 1 && ! args(0).is_string ()) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3653 { |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3654 if (args(0).is_single_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3655 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3656 float val = args(0).float_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3657 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3658 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3659 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3660 val = ::fabsf(val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3661 if (xisnan (val) || xisinf (val)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3662 retval = fill_matrix (octave_value ("single"), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3663 lo_ieee_nan_value (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3664 lo_ieee_float_nan_value (), "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3665 else if (val < FLT_MIN) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3666 retval = fill_matrix (octave_value ("single"), 0e0, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3667 powf (2.0, -149e0), "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3668 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3669 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3670 int expon; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3671 frexpf (val, &expon); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3672 val = std::pow (static_cast <float> (2.0), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3673 static_cast <float> (expon - 24)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3674 retval = fill_matrix (octave_value ("single"), DBL_EPSILON, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3675 val, "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3676 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3677 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3678 } |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3679 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3680 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3681 double val = args(0).double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3682 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3683 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3684 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3685 val = ::fabs(val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3686 if (xisnan (val) || xisinf (val)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3687 retval = fill_matrix (octave_value_list (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3688 lo_ieee_nan_value (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3689 lo_ieee_float_nan_value (), "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3690 else if (val < DBL_MIN) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3691 retval = fill_matrix (octave_value_list (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3692 pow (2.0, -1074e0), 0e0, "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3693 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3694 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3695 int expon; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3696 frexp (val, &expon); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3697 val = std::pow (static_cast <double> (2.0), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3698 static_cast <double> (expon - 53)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3699 retval = fill_matrix (octave_value_list (), val, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3700 FLT_EPSILON, "eps"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3701 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3702 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3703 } |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3704 } |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3705 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3706 retval = fill_matrix (args, DBL_EPSILON, FLT_EPSILON, "eps"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3707 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3708 return retval; |
5747 | 3709 } |
3710 | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3711 /* |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3712 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3713 %!assert(eps(1/2),2^(-53)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3714 %!assert(eps(1),2^(-52)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3715 %!assert(eps(2),2^(-51)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3716 %!assert(eps(realmax),2^971) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3717 %!assert(eps(0),2^(-1074)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3718 %!assert(eps(realmin/2),2^(-1074)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3719 %!assert(eps(realmin/16),2^(-1074)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3720 %!assert(eps(Inf),NaN) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3721 %!assert(eps(NaN),NaN) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3722 %!assert(eps(single(1/2)),single(2^(-24))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3723 %!assert(eps(single(1)),single(2^(-23))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3724 %!assert(eps(single(2)),single(2^(-22))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3725 %!assert(eps(realmax('single')),single(2^104)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3726 %!assert(eps(single(0)),single(2^(-149))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3727 %!assert(eps(realmin('single')/2),single(2^(-149))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3728 %!assert(eps(realmin('single')/16),single(2^(-149))) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3729 %!assert(eps(single(Inf)),single(NaN)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3730 %!assert(eps(single(NaN)),single(NaN)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3731 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3732 */ |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3733 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3734 |
5747 | 3735 DEFUN (pi, args, , |
3736 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3737 @deftypefn {Built-in Function} {} pi\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3738 @deftypefnx {Built-in Function} {} pi (@var{n})\n\ |
5747 | 3739 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m})\n\ |
3740 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3741 @deftypefnx {Built-in Function} {} pi (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3742 Return a scalar, matrix, or N-dimensional array whose elements are all equal\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3743 to the ratio of the circumference of a circle to its\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3744 @tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3745 diameter($\\pi$).\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3746 @end tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3747 @ifnottex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3748 diameter.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3749 @end ifnottex\n\ |
5747 | 3750 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3751 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3752 When called with no arguments, return a scalar with the value of\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3753 @tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3754 $\\pi$.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3755 @end tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3756 @ifnottex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3757 pi.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3758 @end ifnottex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3759 When called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3760 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3761 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3762 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3763 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3764 either \"double\" or \"single\".\n\ |
5747 | 3765 @end deftypefn") |
3766 { | |
3767 #if defined (M_PI) | |
3768 double pi_val = M_PI; | |
3769 #else | |
3770 double pi_val = 4.0 * atan (1.0); | |
3771 #endif | |
3772 | |
3773 return fill_matrix (args, pi_val, "pi"); | |
3774 } | |
3775 | |
3776 DEFUN (realmax, args, , | |
3777 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3778 @deftypefn {Built-in Function} {} realmax\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3779 @deftypefnx {Built-in Function} {} realmax (@var{n})\n\ |
5747 | 3780 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m})\n\ |
3781 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3782 @deftypefnx {Built-in Function} {} realmax (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3783 Return a scalar, matrix or N-dimensional array whose elements are all equal\n\ |
5747 | 3784 to the largest floating point number that is representable. The actual\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3785 value is system dependent. On machines that support IEEE\n\ |
5747 | 3786 floating point arithmetic, @code{realmax} is approximately\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3787 @tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3788 $1.7977\\times10^{308}$ for double precision and $3.4028\\times10^{38}$\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3789 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3790 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3791 1.7977e+308 for double precision and 3.4028e+38\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3792 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3793 for single precision.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3794 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3795 When called with no arguments, return a scalar with the value\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3796 @code{realmax(\"double\")}.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3797 When called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3798 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3799 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3800 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3801 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3802 either \"double\" or \"single\".\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3803 @seealso{realmin, intmax, bitmax}\n\ |
5747 | 3804 @end deftypefn") |
3805 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3806 return fill_matrix (args, DBL_MAX, FLT_MAX, "realmax"); |
5747 | 3807 } |
3808 | |
3809 DEFUN (realmin, args, , | |
3810 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3811 @deftypefn {Built-in Function} {} realmin\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3812 @deftypefnx {Built-in Function} {} realmin (@var{n})\n\ |
5747 | 3813 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m})\n\ |
3814 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3815 @deftypefnx {Built-in Function} {} realmin (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3816 Return a scalar, matrix or N-dimensional array whose elements are all equal\n\ |
5747 | 3817 to the smallest normalized floating point number that is representable.\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3818 The actual value is system dependent. On machines that support\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3819 IEEE floating point arithmetic, @code{realmin} is approximately\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3820 @tex\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3821 $2.2251\\times10^{-308}$ for double precision and $1.1755\\times10^{-38}$\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3822 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3823 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3824 2.2251e-308 for double precision and 1.1755e-38\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3825 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3826 for single precision.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3827 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3828 When called with no arguments, return a scalar with the value\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3829 @code{realmin(\"double\")}.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3830 When called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3831 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3832 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3833 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3834 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3835 either \"double\" or \"single\".\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3836 @seealso{realmax, intmin}\n\ |
5747 | 3837 @end deftypefn") |
3838 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3839 return fill_matrix (args, DBL_MIN, FLT_MIN, "realmin"); |
5747 | 3840 } |
3841 | |
3842 DEFUN (I, args, , | |
3843 "-*- texinfo -*-\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3844 @deftypefn {Built-in Function} {} I\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3845 @deftypefnx {Built-in Function} {} I (@var{n})\n\ |
5747 | 3846 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m})\n\ |
3847 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3848 @deftypefnx {Built-in Function} {} I (@dots{}, @var{class})\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3849 Return a scalar, matrix, or N-dimensional array whose elements are all equal\n\ |
5747 | 3850 to the pure imaginary unit, defined as\n\ |
3851 @tex\n\ | |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3852 $\\sqrt{-1}$.\n\ |
5747 | 3853 @end tex\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3854 @ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3855 @code{sqrt (-1)}.\n\ |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8487
diff
changeset
|
3856 @end ifnottex\n\ |
9197
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3857 I, and its equivalents i, J, and j, are functions so any of the names may\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3858 be reused for other purposes (such as i for a counter variable).\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3859 \n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3860 When called with no arguments, return a scalar with the value @math{i}. When\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3861 called with a single argument, return a square matrix with the dimension\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3862 specified. When called with more than one scalar argument the first two\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3863 arguments are taken as the number of rows and columns and any further\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3864 arguments specify additional matrix dimensions.\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3865 The optional argument @var{class} specifies the return type and may be\n\ |
3c27a0340a64
Update documentation for section 17.8 (Mathematical Constants) of arith.texi
Rik <rdrider0-list@yahoo.com>
parents:
9194
diff
changeset
|
3866 either \"double\" or \"single\".\n\ |
5747 | 3867 @end deftypefn") |
3868 { | |
3869 return fill_matrix (args, Complex (0.0, 1.0), "I"); | |
3870 } | |
3871 | |
3872 DEFALIAS (i, I); | |
3873 DEFALIAS (J, I); | |
3874 DEFALIAS (j, I); | |
3875 | |
3876 DEFUN (NA, args, , | |
3877 "-*- texinfo -*-\n\ | |
9308
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3878 @deftypefn {Built-in Function} {} NA\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3879 @deftypefnx {Built-in Function} {} NA (@var{n})\n\ |
5747 | 3880 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m})\n\ |
3881 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3882 @deftypefnx {Built-in Function} {} NA (@dots{}, @var{class})\n\ | |
9308
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3883 Return a scalar, matrix, or N-dimensional array whose elements are all equal\n\ |
5747 | 3884 to the special constant used to designate missing values.\n\ |
9308
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3885 \n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3886 Note that NA always compares not equal to NA (NA != NA).\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3887 To find NA values, use the @code{isna} function.\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3888 \n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3889 When called with no arguments, return a scalar with the value @samp{NA}.\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3890 When called with a single argument, return a square matrix with the dimension\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3891 specified. When called with more than one scalar argument the first two\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3892 arguments are taken as the number of rows and columns and any further\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3893 arguments specify additional matrix dimensions.\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3894 The optional argument @var{class} specifies the return type and may be\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3895 either \"double\" or \"single\".\n\ |
f2bf62f62286
Update documentation for NA and isna functions
Rik <rdrider0-list@yahoo.com>
parents:
9198
diff
changeset
|
3896 @seealso{isna}\n\ |
5747 | 3897 @end deftypefn") |
3898 { | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
3899 return fill_matrix (args, lo_ieee_na_value (), |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3900 lo_ieee_float_na_value (), "NA"); |
5747 | 3901 } |
3902 | |
7991
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3903 /* |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3904 |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3905 %!assert(single(NA('double')),NA('single')) |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3906 %!assert(double(NA('single')),NA('double')) |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3907 |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3908 */ |
139f47cf17ab
Change NA value to support single to double precision conversion
David Bateman <dbateman@free.fr>
parents:
7919
diff
changeset
|
3909 |
5747 | 3910 DEFUN (false, args, , |
3911 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3912 @deftypefn {Built-in Function} {} false (@var{x})\n\ |
5747 | 3913 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m})\n\ |
3914 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3915 Return a matrix or N-dimensional array whose elements are all logical 0.\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3916 If invoked with a single scalar integer argument, return a square\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3917 matrix of the specified size. If invoked with two or more scalar\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3918 integer arguments, or a vector of integer values, return an array with\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3919 given dimensions.\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3920 @seealso{true}\n\ |
5747 | 3921 @end deftypefn") |
3922 { | |
3923 return fill_matrix (args, false, "false"); | |
3924 } | |
3925 | |
3926 DEFUN (true, args, , | |
3927 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3928 @deftypefn {Built-in Function} {} true (@var{x})\n\ |
5747 | 3929 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m})\n\ |
3930 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
3931 Return a matrix or N-dimensional array whose elements are all logical 1.\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3932 If invoked with a single scalar integer argument, return a square\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3933 matrix of the specified size. If invoked with two or more scalar\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3934 integer arguments, or a vector of integer values, return an array with\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3935 given dimensions.\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
3936 @seealso{false}\n\ |
5747 | 3937 @end deftypefn") |
3938 { | |
3939 return fill_matrix (args, true, "true"); | |
3354 | 3940 } |
523 | 3941 |
4946 | 3942 template <class MT> |
3943 octave_value | |
3944 identity_matrix (int nr, int nc) | |
3945 { | |
3946 octave_value retval; | |
3947 | |
9685 | 3948 typename MT::element_type one (1); |
4946 | 3949 |
3950 if (nr == 1 && nc == 1) | |
3951 retval = one; | |
3952 else | |
3953 { | |
3954 dim_vector dims (nr, nc); | |
3955 | |
9685 | 3956 typename MT::element_type zero (0); |
4946 | 3957 |
3958 MT m (dims, zero); | |
3959 | |
3960 if (nr > 0 && nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3961 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3962 int n = std::min (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3963 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3964 for (int i = 0; i < n; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3965 m(i,i) = one; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
3966 } |
4946 | 3967 |
3968 retval = m; | |
3969 } | |
3970 | |
3971 return retval; | |
3972 } | |
3973 | |
5058 | 3974 #define INSTANTIATE_EYE(T) \ |
3975 template octave_value identity_matrix<T> (int, int) | |
3976 | |
3977 INSTANTIATE_EYE (int8NDArray); | |
3978 INSTANTIATE_EYE (uint8NDArray); | |
3979 INSTANTIATE_EYE (int16NDArray); | |
3980 INSTANTIATE_EYE (uint16NDArray); | |
3981 INSTANTIATE_EYE (int32NDArray); | |
3982 INSTANTIATE_EYE (uint32NDArray); | |
3983 INSTANTIATE_EYE (int64NDArray); | |
3984 INSTANTIATE_EYE (uint64NDArray); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
3985 INSTANTIATE_EYE (FloatNDArray); |
5058 | 3986 INSTANTIATE_EYE (NDArray); |
3987 INSTANTIATE_EYE (boolNDArray); | |
3988 | |
4945 | 3989 static octave_value |
4948 | 3990 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945 | 3991 { |
3992 octave_value retval; | |
3993 | |
5775 | 3994 // FIXME -- perhaps this should be made extensible by using |
4946 | 3995 // the class name to lookup a function to call to create the new |
3996 // value. | |
3997 | |
3998 if (! error_state) | |
3999 { | |
4000 switch (dt) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4001 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4002 case oct_data_conv::dt_int8: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4003 retval = identity_matrix<int8NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4004 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4005 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4006 case oct_data_conv::dt_uint8: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4007 retval = identity_matrix<uint8NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4008 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4009 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4010 case oct_data_conv::dt_int16: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4011 retval = identity_matrix<int16NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4012 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4013 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4014 case oct_data_conv::dt_uint16: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4015 retval = identity_matrix<uint16NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4016 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4017 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4018 case oct_data_conv::dt_int32: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4019 retval = identity_matrix<int32NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4020 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4021 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4022 case oct_data_conv::dt_uint32: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4023 retval = identity_matrix<uint32NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4024 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4025 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4026 case oct_data_conv::dt_int64: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4027 retval = identity_matrix<int64NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4028 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4029 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4030 case oct_data_conv::dt_uint64: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4031 retval = identity_matrix<uint64NDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4032 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4033 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4034 case oct_data_conv::dt_single: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4035 retval = FloatDiagMatrix (nr, nc, 1.0f); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4036 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4037 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4038 case oct_data_conv::dt_double: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4039 retval = DiagMatrix (nr, nc, 1.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4040 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4041 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4042 case oct_data_conv::dt_logical: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4043 retval = identity_matrix<boolNDArray> (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4044 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4045 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4046 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4047 error ("eye: invalid class name"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4048 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4049 } |
4945 | 4050 } |
4051 | |
4052 return retval; | |
4053 } | |
4054 | |
4946 | 4055 #undef INT_EYE_MATRIX |
4056 | |
1957 | 4057 DEFUN (eye, args, , |
3369 | 4058 "-*- texinfo -*-\n\ |
10840 | 4059 @deftypefn {Built-in Function} {} eye (@var{x})\n\ |
3369 | 4060 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ |
4948 | 4061 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369 | 4062 Return an identity matrix. If invoked with a single scalar argument,\n\ |
4063 @code{eye} returns a square matrix with the dimension specified. If you\n\ | |
4064 supply two scalar arguments, @code{eye} takes them to be the number of\n\ | |
4065 rows and columns. If given a vector with two elements, @code{eye} uses\n\ | |
4066 the values of the elements as the number of rows and columns,\n\ | |
10840 | 4067 respectively. For example:\n\ |
3369 | 4068 \n\ |
4069 @example\n\ | |
4070 @group\n\ | |
4071 eye (3)\n\ | |
4072 @result{} 1 0 0\n\ | |
4073 0 1 0\n\ | |
4074 0 0 1\n\ | |
4075 @end group\n\ | |
4076 @end example\n\ | |
4077 \n\ | |
4078 The following expressions all produce the same result:\n\ | |
4079 \n\ | |
4080 @example\n\ | |
4081 @group\n\ | |
4082 eye (2)\n\ | |
4083 @equiv{}\n\ | |
4084 eye (2, 2)\n\ | |
4085 @equiv{}\n\ | |
4086 eye (size ([1, 2; 3, 4])\n\ | |
4087 @end group\n\ | |
4088 @end example\n\ | |
4089 \n\ | |
4945 | 4090 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
4091 the specified type, like\n\ | |
4092 \n\ | |
4093 @example\n\ | |
4094 val = zeros (n,m, \"uint8\")\n\ | |
4095 @end example\n\ | |
4096 \n\ | |
6556 | 4097 Calling @code{eye} with no arguments is equivalent to calling it\n\ |
4098 with an argument of 1. This odd definition is for compatibility\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
4099 with @sc{matlab}.\n\ |
3369 | 4100 @end deftypefn") |
523 | 4101 { |
3354 | 4102 octave_value retval; |
523 | 4103 |
4948 | 4104 int nargin = args.length (); |
4945 | 4105 |
4948 | 4106 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523 | 4107 |
4945 | 4108 // Check for type information. |
4109 | |
4110 if (nargin > 0 && args(nargin-1).is_string ()) | |
4111 { | |
4948 | 4112 std::string nm = args(nargin-1).string_value (); |
4945 | 4113 nargin--; |
4948 | 4114 |
4115 dt = oct_data_conv::string_to_data_type (nm); | |
4116 | |
4117 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4118 return retval; |
4945 | 4119 } |
4120 | |
523 | 4121 switch (nargin) |
4122 { | |
712 | 4123 case 0: |
4948 | 4124 retval = identity_matrix (1, 1, dt); |
712 | 4125 break; |
777 | 4126 |
610 | 4127 case 1: |
3354 | 4128 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4129 octave_idx_type nr, nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4130 get_dimensions (args(0), "eye", nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4131 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4132 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4133 retval = identity_matrix (nr, nc, dt); |
3354 | 4134 } |
610 | 4135 break; |
777 | 4136 |
523 | 4137 case 2: |
3354 | 4138 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4139 octave_idx_type nr, nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4140 get_dimensions (args(0), args(1), "eye", nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4141 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4142 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4143 retval = identity_matrix (nr, nc, dt); |
3354 | 4144 } |
523 | 4145 break; |
777 | 4146 |
523 | 4147 default: |
5823 | 4148 print_usage (); |
523 | 4149 break; |
4150 } | |
4151 | |
4152 return retval; | |
4153 } | |
4154 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4155 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4156 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4157 |
8459
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
4158 %!assert (full (eye(3)), [1, 0, 0; 0, 1, 0; 0, 0, 1]); |
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
4159 %!assert (full (eye(2, 3)), [1, 0, 0; 0, 1, 0]); |
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
4160 |
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
4161 %!assert (full (eye(3,'single')), single([1, 0, 0; 0, 1, 0; 0, 0, 1])); |
d17874a6f161
improve indexing of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8458
diff
changeset
|
4162 %!assert (full (eye(2, 3,'single')), single([1, 0, 0; 0, 1, 0])); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4163 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4164 %!assert (eye(3,'int8'), int8([1, 0, 0; 0, 1, 0; 0, 0, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4165 %!assert (eye(2, 3,'int8'), int8([1, 0, 0; 0, 1, 0])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4166 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4167 %!error <Invalid call to eye.*> eye (1, 2, 3); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4168 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4169 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4170 |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4171 template <class MT> |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4172 static octave_value |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4173 do_linspace (const octave_value& base, const octave_value& limit, |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4174 octave_idx_type n) |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4175 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4176 typedef typename MT::column_vector_type CVT; |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4177 typedef typename MT::element_type T; |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4178 |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4179 octave_value retval; |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4180 |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4181 if (base.is_scalar_type ()) |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4182 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4183 T bs = octave_value_extract<T> (base); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4184 if (limit.is_scalar_type ()) |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4185 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4186 T ls = octave_value_extract<T> (limit); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4187 retval = linspace (bs, ls, n); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4188 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4189 else |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4190 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4191 CVT lv = octave_value_extract<CVT> (limit); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4192 CVT bv (lv.length (), bs); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4193 retval = linspace (bv, lv, n); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4194 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4195 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4196 else |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4197 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4198 CVT bv = octave_value_extract<CVT> (base); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4199 if (limit.is_scalar_type ()) |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4200 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4201 T ls = octave_value_extract<T> (limit); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4202 CVT lv (bv.length (), ls); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4203 retval = linspace (bv, lv, n); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4204 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4205 else |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4206 { |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4207 CVT lv = octave_value_extract<CVT> (limit); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4208 retval = linspace (bv, lv, n); |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4209 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4210 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4211 |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4212 return retval; |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4213 } |
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4214 |
1957 | 4215 DEFUN (linspace, args, , |
3369 | 4216 "-*- texinfo -*-\n\ |
4217 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ | |
4218 Return a row vector with @var{n} linearly spaced elements between\n\ | |
6630 | 4219 @var{base} and @var{limit}. If the number of elements is greater than one,\n\ |
4220 then the @var{base} and @var{limit} are always included in\n\ | |
3369 | 4221 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
4222 stored in decreasing order. If the number of points is not specified, a\n\ | |
4223 value of 100 is used.\n\ | |
1100 | 4224 \n\ |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4225 The @code{linspace} function always returns a row vector if both\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
4226 @var{base} and @var{limit} are scalars. If one of them or both are column\n\ |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4227 vectors, @code{linspace} returns a matrix.\n\ |
6630 | 4228 \n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
4229 For compatibility with @sc{matlab}, return the second argument if\n\ |
6630 | 4230 fewer than two values are requested.\n\ |
3369 | 4231 @end deftypefn") |
1100 | 4232 { |
3418 | 4233 octave_value retval; |
1100 | 4234 |
4235 int nargin = args.length (); | |
4236 | |
6133 | 4237 octave_idx_type npoints = 100; |
1100 | 4238 |
1940 | 4239 if (nargin != 2 && nargin != 3) |
4240 { | |
5823 | 4241 print_usage (); |
1940 | 4242 return retval; |
4243 } | |
4244 | |
1100 | 4245 if (nargin == 3) |
6133 | 4246 npoints = args(2).idx_type_value (); |
1100 | 4247 |
4248 if (! error_state) | |
4249 { | |
3322 | 4250 octave_value arg_1 = args(0); |
4251 octave_value arg_2 = args(1); | |
1100 | 4252 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7743
diff
changeset
|
4253 if (arg_1.is_single_type () || arg_2.is_single_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4254 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4255 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4256 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4257 else |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4258 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4259 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4260 } |
1100 | 4261 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4262 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4263 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4264 retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4265 else |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9562
diff
changeset
|
4266 retval = do_linspace<Matrix> (arg_1, arg_2, npoints); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4267 } |
1100 | 4268 } |
4732 | 4269 else |
4270 error ("linspace: expecting third argument to be an integer"); | |
1100 | 4271 |
4272 return retval; | |
4273 } | |
4274 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4275 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4276 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4277 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4278 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4279 %! x1 = linspace (1, 2); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4280 %! x2 = linspace (1, 2, 10); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4281 %! x3 = linspace (1, -2, 10); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4282 %! assert((size (x1) == [1, 100] && x1(1) == 1 && x1(100) == 2 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4283 %! && size (x2) == [1, 10] && x2(1) == 1 && x2(10) == 2 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4284 %! && size (x3) == [1, 10] && x3(1) == 1 && x3(10) == -2)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4285 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4286 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4287 % assert(linspace ([1, 2; 3, 4], 5, 6), linspace (1, 5, 6)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4288 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4289 %!error <Invalid call to linspace.*> linspace (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4290 %!error <Invalid call to linspace.*> linspace (1, 2, 3, 4); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4291 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4292 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4293 %! fail("linspace ([1, 2; 3, 4], 5, 6)","warning"); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4294 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4295 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4296 |
5775 | 4297 // FIXME -- should accept dimensions as separate args for N-d |
5734 | 4298 // arrays as well as 1-d and 2-d arrays. |
4299 | |
5731 | 4300 DEFUN (resize, args, , |
4301 "-*- texinfo -*-\n\ | |
10840 | 4302 @deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\ |
5731 | 4303 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\ |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4304 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n}, @dots{})\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4305 Resize @var{x} cutting off elements as necessary.\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4306 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4307 In the result, element with certain indices is equal to the corresponding\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4308 element of @var{x} if the indices are within the bounds of @var{x};\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4309 otherwise, the element is set to zero.\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4310 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4311 In other words, the statement\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4312 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4313 @example\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4314 y = resize (x, dv);\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4315 @end example\n\ |
6174 | 4316 \n\ |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4317 @noindent\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4318 is equivalent to the following code:\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4319 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4320 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
4321 @group\n\ |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4322 y = zeros (dv, class (x));\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4323 sz = min (dv, size (x));\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4324 for i = 1:length (sz), idx@{i@} = 1:sz(i); endfor\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4325 y(idx@{:@}) = x(idx@{:@});\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
4326 @end group\n\ |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4327 @end example\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4328 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4329 @noindent\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4330 but is performed more efficiently.\n\ |
6174 | 4331 \n\ |
4332 If only @var{m} is supplied and it is a scalar, the dimension of the\n\ | |
4333 result is @var{m}-by-@var{m}. If @var{m} is a vector, then the\n\ | |
4334 dimensions of the result are given by the elements of @var{m}.\n\ | |
4335 If both @var{m} and @var{n} are scalars, then the dimensions of\n\ | |
4336 the result are @var{m}-by-@var{n}.\n\ | |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4337 \n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4338 An object can be resized to more dimensions than it has;\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4339 in such case the missing dimensions are assumed to be 1.\n\ |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4340 Resizing an object to fewer dimensions is not possible.\n\ |
8433
3292bd1bbaa4
Cross reference between postpad and resize
Francesco Potortì <pot@gnu.org>
parents:
8429
diff
changeset
|
4341 @seealso{reshape, postpad}\n\ |
5731 | 4342 @end deftypefn") |
4343 { | |
4344 octave_value retval; | |
4345 int nargin = args.length (); | |
4346 | |
4347 if (nargin == 2) | |
4348 { | |
4349 Array<double> vec = args(1).vector_value (); | |
4350 int ndim = vec.length (); | |
4351 if (ndim == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4352 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4353 octave_idx_type m = static_cast<octave_idx_type> (vec(0)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4354 retval = args(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4355 retval = retval.resize (dim_vector (m, m), true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4356 } |
5731 | 4357 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4358 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4359 dim_vector dv; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4360 dv.resize (ndim); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4361 for (int i = 0; i < ndim; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4362 dv(i) = static_cast<octave_idx_type> (vec(i)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4363 retval = args(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4364 retval = retval.resize (dv, true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4365 } |
5731 | 4366 } |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4367 else if (nargin > 2) |
5731 | 4368 { |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4369 dim_vector dv; |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4370 dv.resize (nargin - 1); |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4371 for (octave_idx_type i = 1; i < nargin; i++) |
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4372 dv(i-1) = static_cast<octave_idx_type> (args(i).scalar_value ()); |
5731 | 4373 if (!error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4374 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4375 retval = args(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4376 retval = retval.resize (dv, true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4377 } |
8799
f6dc6eb57045
improve resize & resize on assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8780
diff
changeset
|
4378 |
5731 | 4379 } |
4380 else | |
5823 | 4381 print_usage (); |
5731 | 4382 return retval; |
4383 } | |
4384 | |
5775 | 4385 // FIXME -- should use octave_idx_type for dimensions. |
5734 | 4386 |
4567 | 4387 DEFUN (reshape, args, , |
4388 "-*- texinfo -*-\n\ | |
10840 | 4389 @deftypefn {Built-in Function} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8972
diff
changeset
|
4390 @deftypefnx {Built-in Function} {} reshape (@var{a}, @var{size})\n\ |
4567 | 4391 Return a matrix with the given dimensions whose elements are taken\n\ |
6671 | 4392 from the matrix @var{a}. The elements of the matrix are accessed in\n\ |
4567 | 4393 column-major order (like Fortran arrays are stored).\n\ |
4394 \n\ | |
10840 | 4395 For example:\n\ |
4567 | 4396 \n\ |
4397 @example\n\ | |
4398 @group\n\ | |
4399 reshape ([1, 2, 3, 4], 2, 2)\n\ | |
4400 @result{} 1 3\n\ | |
4401 2 4\n\ | |
4402 @end group\n\ | |
4403 @end example\n\ | |
4404 \n\ | |
4405 @noindent\n\ | |
4406 Note that the total number of elements in the original\n\ | |
4407 matrix must match the total number of elements in the new matrix.\n\ | |
5013 | 4408 \n\ |
4409 A single dimension of the return matrix can be unknown and is flagged\n\ | |
4410 by an empty argument.\n\ | |
4567 | 4411 @end deftypefn") |
4412 { | |
4413 octave_value retval; | |
4414 | |
4415 int nargin = args.length (); | |
4416 | |
10693 | 4417 dim_vector new_dims; |
4567 | 4418 |
4419 if (nargin == 2) | |
10693 | 4420 { |
4421 Array<octave_idx_type> new_size = args(1).octave_idx_type_vector_value (); | |
4422 | |
4423 new_dims = dim_vector::alloc (new_size.length ()); | |
4424 | |
4425 for (octave_idx_type i = 0; i < new_size.length (); i++) | |
4426 { | |
4427 if (new_size(i) < 0) | |
4428 { | |
4429 error ("reshape: size must be nonnegative"); | |
4430 break; | |
4431 } | |
4432 else | |
4433 new_dims(i) = new_size(i); | |
4434 } | |
4435 } | |
4567 | 4436 else if (nargin > 2) |
4437 { | |
10693 | 4438 new_dims = dim_vector::alloc (nargin-1); |
5013 | 4439 int empty_dim = -1; |
4440 | |
4567 | 4441 for (int i = 1; i < nargin; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4442 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4443 if (args(i).is_empty ()) |
10693 | 4444 { |
4445 if (empty_dim > 0) | |
4446 { | |
4447 error ("reshape: only a single dimension can be unknown"); | |
4448 break; | |
4449 } | |
4450 else | |
4451 { | |
4452 empty_dim = i; | |
4453 new_dims(i-1) = 1; | |
4454 } | |
4455 } | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4456 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4457 { |
10693 | 4458 new_dims(i-1) = args(i).idx_type_value (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4459 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4460 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4461 break; |
10693 | 4462 else if (new_dims(i-1) < 0) |
4463 { | |
4464 error ("reshape: size must be nonnegative"); | |
4465 break; | |
4466 } | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4467 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4468 } |
5013 | 4469 |
4470 if (! error_state && (empty_dim > 0)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4471 { |
10693 | 4472 octave_idx_type nel = new_dims.numel (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4473 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4474 if (nel == 0) |
10693 | 4475 new_dims(empty_dim-1) = 0; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4476 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4477 { |
10693 | 4478 octave_idx_type a_nel = args(0).numel (); |
4479 octave_idx_type size_empty_dim = a_nel / nel; | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4480 |
10693 | 4481 if (a_nel != size_empty_dim * nel) |
4482 error ("reshape: size is not divisible by the product of known dimensions (= %d)", nel); | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4483 else |
10693 | 4484 new_dims(empty_dim-1) = size_empty_dim; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4485 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4486 } |
4567 | 4487 } |
4488 else | |
4489 { | |
5823 | 4490 print_usage (); |
4567 | 4491 return retval; |
4492 } | |
4493 | |
10352 | 4494 if (! error_state) |
4495 retval = args(0).reshape (new_dims); | |
4567 | 4496 |
4497 return retval; | |
4498 } | |
4499 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4500 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4501 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4502 %!assert(size (reshape (ones (4, 4), 2, 8)), [2, 8]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4503 %!assert(size (reshape (ones (4, 4), 8, 2)), [8, 2]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4504 %!assert(size (reshape (ones (15, 4), 1, 60)), [1, 60]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4505 %!assert(size (reshape (ones (15, 4), 60, 1)), [60, 1]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4506 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4507 %!assert(size (reshape (ones (4, 4, 'single'), 2, 8)), [2, 8]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4508 %!assert(size (reshape (ones (4, 4, 'single'), 8, 2)), [8, 2]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4509 %!assert(size (reshape (ones (15, 4, 'single'), 1, 60)), [1, 60]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4510 %!assert(size (reshape (ones (15, 4, 'single'), 60, 1)), [60, 1]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4511 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4512 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4513 %! s.a = 1; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4514 %! fail("reshape (s, 2, 3)"); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4515 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4516 %!error <Invalid call to reshape.*> reshape (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4517 %!error reshape (1, 2, 3, 4); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4518 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4519 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4520 |
10694
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4521 DEFUN (vec, args, , |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4522 "-*- texinfo -*-\n\ |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4523 @deftypefn {Built-in Function} {} vec (@var{x})\n\ |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4524 Return the vector obtained by stacking the columns of the matrix @var{x}\n\ |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10707
diff
changeset
|
4525 one above the other. Equivalent to @code{@var{x}(:)}. Useful for functional\n\ |
10694
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4526 programming.\n\ |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4527 @end deftypefn") |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4528 { |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4529 octave_value retval; |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4530 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4531 if (args.length () == 1) |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4532 { |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4533 octave_value colon (octave_value::magic_colon_t); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4534 octave_value arg = args(0); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4535 retval = arg.single_subsref ("(", colon); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4536 } |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4537 else |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4538 print_usage (); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4539 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4540 return retval; |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4541 } |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4542 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4543 /* |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4544 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4545 %!assert(vec ([1, 2; 3, 4]), [1; 3; 2; 4]) |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4546 %!assert(vec ([1, 3, 2, 4]) == [1; 3; 2; 4]); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4547 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4548 %!error vec (); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4549 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4550 %!error vec (1, 2); |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4551 |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4552 */ |
d97165928d05
implement built-in vec()
Jaroslav Hajek <highegg@gmail.com>
parents:
10693
diff
changeset
|
4553 |
4532 | 4554 DEFUN (squeeze, args, , |
4555 "-*- texinfo -*-\n\ | |
4556 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ | |
4557 Remove singleton dimensions from @var{x} and return the result.\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
4558 Note that for compatibility with @sc{matlab}, all objects have\n\ |
7007 | 4559 a minimum of two dimensions and row vectors are left unchanged.\n\ |
4532 | 4560 @end deftypefn") |
4561 { | |
4562 octave_value retval; | |
4563 | |
4564 if (args.length () == 1) | |
4545 | 4565 retval = args(0).squeeze (); |
4532 | 4566 else |
5823 | 4567 print_usage (); |
4532 | 4568 |
4569 return retval; | |
4570 } | |
4571 | |
8458
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4572 DEFUN (full, args, , |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4573 "-*- texinfo -*-\n\ |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4574 @deftypefn {Loadable Function} {@var{FM} =} full (@var{SM})\n\ |
10840 | 4575 returns a full storage matrix from a sparse, diagonal, permutation matrix or a\n\ |
4576 range.\n\ | |
8458
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4577 @seealso{sparse}\n\ |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4578 @end deftypefn") |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4579 { |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4580 octave_value retval; |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4581 |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4582 if (args.length () == 1) |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4583 retval = args(0).full_value (); |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4584 else |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4585 print_usage (); |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4586 |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4587 return retval; |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4588 } |
d254a21e0120
reimplement full as method of octave_base_value
Jaroslav Hajek <highegg@gmail.com>
parents:
8433
diff
changeset
|
4589 |
6945 | 4590 // Compute various norms of the vector X. |
4591 | |
6953 | 4592 DEFUN (norm, args, , |
6508 | 4593 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4594 @deftypefn {Built-in Function} {} norm (@var{a})\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4595 @deftypefnx {Built-in Function} {} norm (@var{a}, @var{p})\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4596 @deftypefnx {Built-in Function} {} norm (@var{a}, @var{p}, @var{opt})\n\ |
6953 | 4597 Compute the p-norm of the matrix @var{a}. If the second argument is\n\ |
4598 missing, @code{p = 2} is assumed.\n\ | |
4599 \n\ | |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4600 If @var{a} is a matrix (or sparse matrix):\n\ |
6953 | 4601 \n\ |
4602 @table @asis\n\ | |
4603 @item @var{p} = @code{1}\n\ | |
4604 1-norm, the largest column sum of the absolute values of @var{a}.\n\ | |
4605 \n\ | |
4606 @item @var{p} = @code{2}\n\ | |
4607 Largest singular value of @var{a}.\n\ | |
4608 \n\ | |
7189 | 4609 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953 | 4610 @cindex infinity norm\n\ |
4611 Infinity norm, the largest row sum of the absolute values of @var{a}.\n\ | |
4612 \n\ | |
4613 @item @var{p} = @code{\"fro\"}\n\ | |
4614 @cindex Frobenius norm\n\ | |
4615 Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}.\n\ | |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4616 \n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4617 @item other @var{p}, @code{@var{p} > 1}\n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4618 @cindex general p-norm \n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4619 maximum @code{norm (A*x, p)} such that @code{norm (x, p) == 1}\n\ |
6953 | 4620 @end table\n\ |
4621 \n\ | |
4622 If @var{a} is a vector or a scalar:\n\ | |
4623 \n\ | |
4624 @table @asis\n\ | |
7189 | 4625 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953 | 4626 @code{max (abs (@var{a}))}.\n\ |
4627 \n\ | |
4628 @item @var{p} = @code{-Inf}\n\ | |
4629 @code{min (abs (@var{a}))}.\n\ | |
4630 \n\ | |
7189 | 4631 @item @var{p} = @code{\"fro\"}\n\ |
4632 Frobenius norm of @var{a}, @code{sqrt (sumsq (abs (a)))}.\n\ | |
4633 \n\ | |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4634 @item @var{p} = 0\n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4635 Hamming norm - the number of nonzero elements.\n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4636 \n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4637 @item other @var{p}, @code{@var{p} > 1}\n\ |
6953 | 4638 p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}.\n\ |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4639 \n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4640 @item other @var{p} @code{@var{p} < 1}\n\ |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4641 the p-pseudonorm defined as above.\n\ |
6953 | 4642 @end table\n\ |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4643 \n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4644 If @var{opt} is the value @code{\"rows\"}, treat each row as a vector and\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4645 compute its norm. The result is returned as a column vector.\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4646 Similarly, if @var{opt} is @code{\"columns\"} or @code{\"cols\"} then compute\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10682
diff
changeset
|
4647 the norms of each column and return a row vector.\n\ |
6953 | 4648 @seealso{cond, svd}\n\ |
6508 | 4649 @end deftypefn") |
4650 { | |
6953 | 4651 octave_value_list retval; |
6508 | 4652 |
4653 int nargin = args.length (); | |
4654 | |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4655 if (nargin >= 1 && nargin <= 3) |
6508 | 4656 { |
6953 | 4657 octave_value x_arg = args(0); |
4658 | |
10185
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10182
diff
changeset
|
4659 if (x_arg.ndims () == 2) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4660 { |
8303
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4661 enum { sfmatrix, sfcols, sfrows, sffrob, sfinf } strflag = sfmatrix; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4662 if (nargin > 1 && args(nargin-1).is_string ()) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4663 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4664 std::string str = args(nargin-1).string_value (); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4665 if (str == "cols" || str == "columns") |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4666 strflag = sfcols; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4667 else if (str == "rows") |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4668 strflag = sfrows; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4669 else if (str == "fro") |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4670 strflag = sffrob; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4671 else if (str == "inf") |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4672 strflag = sfinf; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4673 else |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4674 error ("norm: unrecognized option: %s", str.c_str ()); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4675 // we've handled the last parameter, so act as if it was removed |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4676 nargin --; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4677 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4678 else if (nargin > 1 && ! args(1).is_scalar_type ()) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4679 gripe_wrong_type_arg ("norm", args(1), true); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4680 |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4681 if (! error_state) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4682 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4683 octave_value p_arg = (nargin > 1) ? args(1) : octave_value (2); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4684 switch (strflag) |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4685 { |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4686 case sfmatrix: |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4687 retval(0) = xnorm (x_arg, p_arg); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4688 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4689 case sfcols: |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4690 retval(0) = xcolnorms (x_arg, p_arg); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4691 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4692 case sfrows: |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4693 retval(0) = xrownorms (x_arg, p_arg); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4694 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4695 case sffrob: |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4696 retval(0) = xfrobnorm (x_arg); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4697 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4698 case sfinf: |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4699 retval(0) = xnorm (x_arg, octave_Inf); |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4700 break; |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4701 } |
b11c31849b44
improve norm computation capabilities
Jaroslav Hajek <highegg@gmail.com>
parents:
8286
diff
changeset
|
4702 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4703 } |
6508 | 4704 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
4705 error ("norm: only valid for 2-D objects"); |
6508 | 4706 } |
4707 else | |
4708 print_usage (); | |
4709 | |
4710 return retval; | |
4711 } | |
4712 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4713 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4714 %!shared x |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4715 %! x = [1, -3, 4, 5, -7]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4716 %!assert(norm(x,1), 20); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4717 %!assert(norm(x,2), 10); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4718 %!assert(norm(x,3), 8.24257059961711, -4*eps); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4719 %!assert(norm(x,Inf), 7); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4720 %!assert(norm(x,-Inf), 1); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4721 %!assert(norm(x,"inf"), 7); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4722 %!assert(norm(x,"fro"), 10, -eps); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4723 %!assert(norm(x), 10); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4724 %!assert(norm([1e200, 1]), 1e200); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4725 %!assert(norm([3+4i, 3-4i, sqrt(31)]), 9, -4*eps); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4726 %!shared m |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4727 %! m = magic (4); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4728 %!assert(norm(m,1), 34); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4729 %!assert(norm(m,2), 34, -eps); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4730 %!assert(norm(m,Inf), 34); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4731 %!assert(norm(m,"inf"), 34); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4732 %!shared m2, flo, fhi |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4733 %! m2 = [1,2;3,4]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4734 %! flo = 1e-300; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4735 %! fhi = 1e+300; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4736 %!assert (norm(flo*m2,"fro"), sqrt(30)*flo, -eps) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4737 %!assert (norm(fhi*m2,"fro"), sqrt(30)*fhi, -eps) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4738 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4739 %!shared x |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4740 %! x = single([1, -3, 4, 5, -7]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4741 %!assert(norm(x,1), single(20)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4742 %!assert(norm(x,2), single(10)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4743 %!assert(norm(x,3), single(8.24257059961711), -4*eps('single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4744 %!assert(norm(x,Inf), single(7)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4745 %!assert(norm(x,-Inf), single(1)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4746 %!assert(norm(x,"inf"), single(7)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4747 %!assert(norm(x,"fro"), single(10), -eps('single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4748 %!assert(norm(x), single(10)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4749 %!assert(norm(single([1e200, 1])), single(1e200)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4750 %!assert(norm(single([3+4i, 3-4i, sqrt(31)])), single(9), -4*eps('single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4751 %!shared m |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4752 %! m = single(magic (4)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4753 %!assert(norm(m,1), single(34)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4754 %!assert(norm(m,2), single(34), -eps('single')); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4755 %!assert(norm(m,Inf), single(34)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4756 %!assert(norm(m,"inf"), single(34)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4757 %!shared m2, flo, fhi |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4758 %! m2 = single([1,2;3,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4759 %! flo = single(1e-300); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4760 %! fhi = single(1e+300); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4761 %!assert (norm(flo*m2,"fro"), single(sqrt(30)*flo), -eps('single')) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4762 %!assert (norm(fhi*m2,"fro"), single(sqrt(30)*fhi), -eps('single')) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4763 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4764 |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4765 static octave_value |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4766 unary_op_defun_body (octave_value::unary_op op, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4767 const octave_value_list& args) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4768 { |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4769 octave_value retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4770 if (args.length () == 1) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4771 retval = do_unary_op (op, args(0)); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4772 else |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4773 print_usage (); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4774 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4775 return retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4776 } |
6518 | 4777 |
4778 DEFUN (not, args, , | |
4779 "-*- texinfo -*-\n\ | |
4780 @deftypefn {Built-in Function} {} not (@var{x})\n\ | |
4781 This function is equivalent to @code{! x}.\n\ | |
4782 @end deftypefn") | |
4783 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4784 return unary_op_defun_body (octave_value::op_not, args); |
6518 | 4785 } |
4786 | |
4787 DEFUN (uplus, args, , | |
4788 "-*- texinfo -*-\n\ | |
4789 @deftypefn {Built-in Function} {} uplus (@var{x})\n\ | |
4790 This function is equivalent to @code{+ x}.\n\ | |
4791 @end deftypefn") | |
4792 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4793 return unary_op_defun_body (octave_value::op_uplus, args); |
6518 | 4794 } |
4795 | |
4796 DEFUN (uminus, args, , | |
4797 "-*- texinfo -*-\n\ | |
4798 @deftypefn {Built-in Function} {} uminus (@var{x})\n\ | |
4799 This function is equivalent to @code{- x}.\n\ | |
4800 @end deftypefn") | |
4801 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4802 return unary_op_defun_body (octave_value::op_uminus, args); |
6518 | 4803 } |
4804 | |
4805 DEFUN (transpose, args, , | |
4806 "-*- texinfo -*-\n\ | |
4807 @deftypefn {Built-in Function} {} transpose (@var{x})\n\ | |
4808 This function is equivalent to @code{x.'}.\n\ | |
4809 @end deftypefn") | |
4810 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4811 return unary_op_defun_body (octave_value::op_transpose, args); |
6518 | 4812 } |
4813 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4814 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4815 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4816 %!assert (2.', 2); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4817 %!assert (2i.',2i); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4818 %!assert ([1:4].',[1;2;3;4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4819 %!assert ([1;2;3;4].',[1:4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4820 %!assert ([1,2;3,4].',[1,3;2,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4821 %!assert ([1,2i;3,4].',[1,3;2i,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4822 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4823 %!assert (transpose ([1,2;3,4]),[1,3;2,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4824 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4825 %!assert (single(2).', single(2)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4826 %!assert (single(2i).',single(2i)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4827 %!assert (single([1:4]).',single([1;2;3;4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4828 %!assert (single([1;2;3;4]).',single([1:4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4829 %!assert (single([1,2;3,4]).',single([1,3;2,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4830 %!assert (single([1,2i;3,4]).',single([1,3;2i,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4831 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4832 %!assert (transpose (single([1,2;3,4])),single([1,3;2,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4833 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4834 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4835 |
6518 | 4836 DEFUN (ctranspose, args, , |
4837 "-*- texinfo -*-\n\ | |
4838 @deftypefn {Built-in Function} {} ctranspose (@var{x})\n\ | |
4839 This function is equivalent to @code{x'}.\n\ | |
4840 @end deftypefn") | |
4841 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4842 return unary_op_defun_body (octave_value::op_hermitian, args); |
6518 | 4843 } |
4844 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4845 /* |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4846 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4847 %!assert (2', 2); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4848 %!assert (2i',-2i); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4849 %!assert ([1:4]',[1;2;3;4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4850 %!assert ([1;2;3;4]',[1:4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4851 %!assert ([1,2;3,4]',[1,3;2,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4852 %!assert ([1,2i;3,4]',[1,3;-2i,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4853 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4854 %!assert (ctranspose ([1,2i;3,4]),[1,3;-2i,4]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4855 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4856 %!assert (single(2)', single(2)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4857 %!assert (single(2i)',single(-2i)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4858 %!assert (single([1:4])',single([1;2;3;4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4859 %!assert (single([1;2;3;4])',single([1:4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4860 %!assert (single([1,2;3,4])',single([1,3;2,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4861 %!assert (single([1,2i;3,4])',single([1,3;-2i,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4862 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4863 %!assert (ctranspose (single([1,2i;3,4])),single([1,3;-2i,4])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4864 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4865 */ |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
4866 |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4867 static octave_value |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4868 binary_op_defun_body (octave_value::binary_op op, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4869 const octave_value_list& args) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4870 { |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4871 octave_value retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4872 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4873 if (args.length () == 2) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4874 retval = do_binary_op (op, args(0), args(1)); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4875 else |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4876 print_usage (); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4877 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4878 return retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4879 } |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4880 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4881 static octave_value |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4882 binary_assoc_op_defun_body (octave_value::binary_op op, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4883 octave_value::assign_op aop, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4884 const octave_value_list& args) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4885 { |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4886 octave_value retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4887 int nargin = args.length (); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4888 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4889 switch (nargin) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4890 { |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4891 case 0: |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4892 print_usage (); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4893 break; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4894 case 1: |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4895 retval = args(0); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4896 break; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4897 case 2: |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4898 retval = do_binary_op (op, args(0), args(1)); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4899 break; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4900 default: |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4901 retval = do_binary_op (op, args(0), args(1)); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4902 for (int i = 2; i < nargin; i++) |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4903 retval.assign (aop, args(i)); |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4904 break; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4905 } |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4906 |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4907 return retval; |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4908 } |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4909 |
6518 | 4910 DEFUN (plus, args, , |
4911 "-*- texinfo -*-\n\ | |
10840 | 4912 @deftypefn {Built-in Function} {} plus (@var{x}, @var{y})\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4913 @deftypefnx {Built-in Function} {} plus (@var{x1}, @var{x2}, @dots{})\n\ |
6518 | 4914 This function is equivalent to @code{x + y}.\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4915 If more arguments are given, the summation is applied\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4916 cumulatively from left to right:\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4917 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4918 @example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4919 (@dots{}((x1 + x2) + x3) + @dots{})\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4920 @end example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4921 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4922 At least one argument is needed.\n\ |
6518 | 4923 @end deftypefn") |
4924 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4925 return binary_assoc_op_defun_body (octave_value::op_add, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4926 octave_value::op_add_eq, args); |
6518 | 4927 } |
4928 | |
4929 DEFUN (minus, args, , | |
4930 "-*- texinfo -*-\n\ | |
4931 @deftypefn {Built-in Function} {} minus (@var{x}, @var{y})\n\ | |
4932 This function is equivalent to @code{x - y}.\n\ | |
4933 @end deftypefn") | |
4934 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4935 return binary_op_defun_body (octave_value::op_sub, args); |
6518 | 4936 } |
4937 | |
4938 DEFUN (mtimes, args, , | |
4939 "-*- texinfo -*-\n\ | |
10840 | 4940 @deftypefn {Built-in Function} {} mtimes (@var{x}, @var{y})\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4941 @deftypefnx {Built-in Function} {} mtimes (@var{x1}, @var{x2}, @dots{})\n\ |
6518 | 4942 This function is equivalent to @code{x * y}.\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4943 If more arguments are given, the multiplication is applied\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4944 cumulatively from left to right:\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4945 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4946 @example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4947 (@dots{}((x1 * x2) * x3) * @dots{})\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4948 @end example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4949 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
4950 At least one argument is needed.\n\ |
6518 | 4951 @end deftypefn") |
4952 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4953 return binary_assoc_op_defun_body (octave_value::op_mul, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4954 octave_value::op_mul_eq, args); |
6518 | 4955 } |
4956 | |
4957 DEFUN (mrdivide, args, , | |
4958 "-*- texinfo -*-\n\ | |
4959 @deftypefn {Built-in Function} {} mrdivide (@var{x}, @var{y})\n\ | |
4960 This function is equivalent to @code{x / y}.\n\ | |
4961 @end deftypefn") | |
4962 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4963 return binary_op_defun_body (octave_value::op_div, args); |
6518 | 4964 } |
4965 | |
4966 DEFUN (mpower, args, , | |
4967 "-*- texinfo -*-\n\ | |
4968 @deftypefn {Built-in Function} {} mpower (@var{x}, @var{y})\n\ | |
4969 This function is equivalent to @code{x ^ y}.\n\ | |
4970 @end deftypefn") | |
4971 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4972 return binary_op_defun_body (octave_value::op_pow, args); |
6518 | 4973 } |
4974 | |
4975 DEFUN (mldivide, args, , | |
4976 "-*- texinfo -*-\n\ | |
4977 @deftypefn {Built-in Function} {} mldivide (@var{x}, @var{y})\n\ | |
4978 This function is equivalent to @code{x \\ y}.\n\ | |
4979 @end deftypefn") | |
4980 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4981 return binary_op_defun_body (octave_value::op_ldiv, args); |
6518 | 4982 } |
4983 | |
4984 DEFUN (lt, args, , | |
4985 "-*- texinfo -*-\n\ | |
4986 @deftypefn {Built-in Function} {} lt (@var{x}, @var{y})\n\ | |
4987 This function is equivalent to @code{x < y}.\n\ | |
4988 @end deftypefn") | |
4989 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4990 return binary_op_defun_body (octave_value::op_lt, args); |
6518 | 4991 } |
4992 | |
4993 DEFUN (le, args, , | |
4994 "-*- texinfo -*-\n\ | |
4995 @deftypefn {Built-in Function} {} le (@var{x}, @var{y})\n\ | |
4996 This function is equivalent to @code{x <= y}.\n\ | |
4997 @end deftypefn") | |
4998 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
4999 return binary_op_defun_body (octave_value::op_le, args); |
6518 | 5000 } |
5001 | |
5002 DEFUN (eq, args, , | |
5003 "-*- texinfo -*-\n\ | |
5004 @deftypefn {Built-in Function} {} eq (@var{x}, @var{y})\n\ | |
5005 This function is equivalent to @code{x == y}.\n\ | |
5006 @end deftypefn") | |
5007 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5008 return binary_op_defun_body (octave_value::op_eq, args); |
6518 | 5009 } |
5010 | |
5011 DEFUN (ge, args, , | |
5012 "-*- texinfo -*-\n\ | |
5013 @deftypefn {Built-in Function} {} ge (@var{x}, @var{y})\n\ | |
5014 This function is equivalent to @code{x >= y}.\n\ | |
5015 @end deftypefn") | |
5016 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5017 return binary_op_defun_body (octave_value::op_ge, args); |
6518 | 5018 } |
5019 | |
5020 DEFUN (gt, args, , | |
5021 "-*- texinfo -*-\n\ | |
5022 @deftypefn {Built-in Function} {} gt (@var{x}, @var{y})\n\ | |
5023 This function is equivalent to @code{x > y}.\n\ | |
5024 @end deftypefn") | |
5025 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5026 return binary_op_defun_body (octave_value::op_gt, args); |
6518 | 5027 } |
5028 | |
5029 DEFUN (ne, args, , | |
5030 "-*- texinfo -*-\n\ | |
5031 @deftypefn {Built-in Function} {} ne (@var{x}, @var{y})\n\ | |
5032 This function is equivalent to @code{x != y}.\n\ | |
5033 @end deftypefn") | |
5034 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5035 return binary_op_defun_body (octave_value::op_ne, args); |
6518 | 5036 } |
5037 | |
5038 DEFUN (times, args, , | |
5039 "-*- texinfo -*-\n\ | |
10840 | 5040 @deftypefn {Built-in Function} {} times (@var{x}, @var{y})\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5041 @deftypefnx {Built-in Function} {} times (@var{x1}, @var{x2}, @dots{})\n\ |
6518 | 5042 This function is equivalent to @code{x .* y}.\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5043 If more arguments are given, the multiplication is applied\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5044 cumulatively from left to right:\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5045 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5046 @example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5047 (@dots{}((x1 .* x2) .* x3) .* @dots{})\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5048 @end example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5049 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5050 At least one argument is needed.\n\ |
6518 | 5051 @end deftypefn") |
5052 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5053 return binary_assoc_op_defun_body (octave_value::op_el_mul, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5054 octave_value::op_el_mul_eq, args); |
6518 | 5055 } |
5056 | |
5057 DEFUN (rdivide, args, , | |
5058 "-*- texinfo -*-\n\ | |
5059 @deftypefn {Built-in Function} {} rdivide (@var{x}, @var{y})\n\ | |
5060 This function is equivalent to @code{x ./ y}.\n\ | |
5061 @end deftypefn") | |
5062 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5063 return binary_op_defun_body (octave_value::op_el_div, args); |
6518 | 5064 } |
5065 | |
5066 DEFUN (power, args, , | |
5067 "-*- texinfo -*-\n\ | |
5068 @deftypefn {Built-in Function} {} power (@var{x}, @var{y})\n\ | |
7623 | 5069 This function is equivalent to @code{x .^ y}.\n\ |
6518 | 5070 @end deftypefn") |
5071 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5072 return binary_op_defun_body (octave_value::op_el_pow, args); |
6518 | 5073 } |
5074 | |
5075 DEFUN (ldivide, args, , | |
5076 "-*- texinfo -*-\n\ | |
5077 @deftypefn {Built-in Function} {} ldivide (@var{x}, @var{y})\n\ | |
7623 | 5078 This function is equivalent to @code{x .\\ y}.\n\ |
6518 | 5079 @end deftypefn") |
5080 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5081 return binary_op_defun_body (octave_value::op_el_ldiv, args); |
6518 | 5082 } |
5083 | |
5084 DEFUN (and, args, , | |
5085 "-*- texinfo -*-\n\ | |
10840 | 5086 @deftypefn {Built-in Function} {} and (@var{x}, @var{y})\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5087 @deftypefnx {Built-in Function} {} and (@var{x1}, @var{x2}, @dots{})\n\ |
6518 | 5088 This function is equivalent to @code{x & y}.\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5089 If more arguments are given, the logical and is applied\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5090 cumulatively from left to right:\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5091 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5092 @example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5093 (@dots{}((x1 & x2) & x3) & @dots{})\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5094 @end example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5095 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5096 At least one argument is needed.\n\ |
6518 | 5097 @end deftypefn") |
5098 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5099 return binary_assoc_op_defun_body (octave_value::op_el_and, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5100 octave_value::op_el_and_eq, args); |
6518 | 5101 } |
5102 | |
5103 DEFUN (or, args, , | |
5104 "-*- texinfo -*-\n\ | |
10840 | 5105 @deftypefn {Built-in Function} {} or (@var{x}, @var{y})\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5106 @deftypefnx {Built-in Function} {} or (@var{x1}, @var{x2}, @dots{})\n\ |
6518 | 5107 This function is equivalent to @code{x | y}.\n\ |
9339
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5108 If more arguments are given, the logical or is applied\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5109 cumulatively from left to right:\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5110 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5111 @example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5112 (@dots{}((x1 | x2) | x3) | @dots{})\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5113 @end example\n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5114 \n\ |
c0b104835d0d
allow multiple arguments in plus,times,mtimes,and,or
Jaroslav Hajek <highegg@gmail.com>
parents:
9329
diff
changeset
|
5115 At least one argument is needed.\n\ |
6518 | 5116 @end deftypefn") |
5117 { | |
9555
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5118 return binary_assoc_op_defun_body (octave_value::op_el_or, |
76ecc571879e
improve unop/binop implementation in data.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9517
diff
changeset
|
5119 octave_value::op_el_or_eq, args); |
6518 | 5120 } |
5121 | |
7065 | 5122 static double tic_toc_timestamp = -1.0; |
7045 | 5123 |
5124 DEFUN (tic, args, nargout, | |
5125 "-*- texinfo -*-\n\ | |
10840 | 5126 @deftypefn {Built-in Function} {} tic ()\n\ |
7045 | 5127 @deftypefnx {Built-in Function} {} toc ()\n\ |
5128 Set or check a wall-clock timer. Calling @code{tic} without an\n\ | |
5129 output argument sets the timer. Subsequent calls to @code{toc}\n\ | |
5130 return the number of seconds since the timer was set. For example,\n\ | |
5131 \n\ | |
5132 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
5133 @group\n\ |
7045 | 5134 tic ();\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
5135 # many computations later@dots{}\n\ |
7045 | 5136 elapsed_time = toc ();\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
5137 @end group\n\ |
7045 | 5138 @end example\n\ |
5139 \n\ | |
5140 @noindent\n\ | |
5141 will set the variable @code{elapsed_time} to the number of seconds since\n\ | |
5142 the most recent call to the function @code{tic}.\n\ | |
5143 \n\ | |
5144 If called with one output argument then this function returns a scalar\n\ | |
5145 of type @code{uint64} and the wall-clock timer is not started.\n\ | |
5146 \n\ | |
5147 @example\n\ | |
5148 @group\n\ | |
5149 t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6\n\ | |
5150 @result{} 5\n\ | |
5151 @end group\n\ | |
5152 @end example\n\ | |
5153 \n\ | |
5154 Nested timing with @code{tic} and @code{toc} is not supported.\n\ | |
5155 Therefore @code{toc} will always return the elapsed time from the most\n\ | |
5156 recent call to @code{tic}.\n\ | |
5157 \n\ | |
5158 If you are more interested in the CPU time that your process used, you\n\ | |
5159 should use the @code{cputime} function instead. The @code{tic} and\n\ | |
5160 @code{toc} functions report the actual wall clock time that elapsed\n\ | |
5161 between the calls. This may include time spent processing other jobs or\n\ | |
10840 | 5162 doing nothing at all. For example:\n\ |
7045 | 5163 \n\ |
5164 @example\n\ | |
5165 @group\n\ | |
5166 tic (); sleep (5); toc ()\n\ | |
5167 @result{} 5\n\ | |
5168 t = cputime (); sleep (5); cputime () - t\n\ | |
5169 @result{} 0\n\ | |
5170 @end group\n\ | |
5171 @end example\n\ | |
5172 \n\ | |
5173 @noindent\n\ | |
5174 (This example also illustrates that the CPU timer may have a fairly\n\ | |
5175 coarse resolution.)\n\ | |
5176 @end deftypefn") | |
5177 { | |
5178 octave_value retval; | |
7065 | 5179 |
7045 | 5180 int nargin = args.length (); |
5181 | |
5182 if (nargin != 0) | |
5183 warning ("tic: ignoring extra arguments"); | |
5184 | |
7065 | 5185 octave_time now; |
5186 | |
5187 double tmp = now.double_value (); | |
5188 | |
7045 | 5189 if (nargout > 0) |
7065 | 5190 retval = static_cast<octave_uint64> (1e6 * tmp); |
7045 | 5191 else |
7065 | 5192 tic_toc_timestamp = tmp; |
7045 | 5193 |
5194 return retval; | |
5195 } | |
5196 | |
5197 DEFUN (toc, args, nargout, | |
5198 "-*- texinfo -*-\n\ | |
5199 @deftypefn {Built-in Function} {} toc ()\n\ | |
5200 See tic.\n\ | |
5201 @end deftypefn") | |
5202 { | |
5203 octave_value retval; | |
7065 | 5204 |
7045 | 5205 int nargin = args.length (); |
5206 | |
5207 if (nargin != 0) | |
5208 warning ("tic: ignoring extra arguments"); | |
5209 | |
7065 | 5210 if (tic_toc_timestamp < 0) |
7045 | 5211 { |
5212 warning ("toc called before timer set"); | |
5213 if (nargout > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5214 retval = Matrix (); |
7045 | 5215 } |
5216 else | |
7065 | 5217 { |
5218 octave_time now; | |
5219 | |
5220 double tmp = now.double_value () - tic_toc_timestamp; | |
5221 | |
5222 if (nargout > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5223 retval = tmp; |
7065 | 5224 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5225 octave_stdout << "Elapsed time is " << tmp << " seconds.\n"; |
7065 | 5226 } |
7045 | 5227 |
5228 return retval; | |
5229 } | |
5230 | |
5231 DEFUN (cputime, args, , | |
5232 "-*- texinfo -*-\n\ | |
5233 @deftypefn {Built-in Function} {[@var{total}, @var{user}, @var{system}] =} cputime ();\n\ | |
5234 Return the CPU time used by your Octave session. The first output is\n\ | |
5235 the total time spent executing your process and is equal to the sum of\n\ | |
5236 second and third outputs, which are the number of CPU seconds spent\n\ | |
5237 executing in user mode and the number of CPU seconds spent executing in\n\ | |
5238 system mode, respectively. If your system does not have a way to report\n\ | |
5239 CPU time usage, @code{cputime} returns 0 for each of its output values.\n\ | |
5240 Note that because Octave used some CPU time to start, it is reasonable\n\ | |
5241 to check to see if @code{cputime} works by checking to see if the total\n\ | |
5242 CPU time used is nonzero.\n\ | |
5243 @end deftypefn") | |
5244 { | |
5245 octave_value_list retval; | |
5246 int nargin = args.length (); | |
5247 double usr = 0.0; | |
5248 double sys = 0.0; | |
5249 | |
5250 if (nargin != 0) | |
5251 warning ("tic: ignoring extra arguments"); | |
5252 | |
5253 #if defined (HAVE_GETRUSAGE) | |
5254 | |
5255 struct rusage ru; | |
5256 | |
5257 getrusage (RUSAGE_SELF, &ru); | |
5258 | |
5259 usr = static_cast<double> (ru.ru_utime.tv_sec) + | |
5260 static_cast<double> (ru.ru_utime.tv_usec) * 1e-6; | |
5261 | |
5262 sys = static_cast<double> (ru.ru_stime.tv_sec) + | |
5263 static_cast<double> (ru.ru_stime.tv_usec) * 1e-6; | |
5264 | |
10240
fa7b5751730c
use gnulib time, sys_time, and sys_times modules
John W. Eaton <jwe@octave.org>
parents:
10185
diff
changeset
|
5265 #else |
7045 | 5266 |
5267 struct tms t; | |
5268 | |
5269 times (&t); | |
5270 | |
5271 unsigned long ticks; | |
5272 unsigned long seconds; | |
5273 unsigned long fraction; | |
5274 | |
5275 ticks = t.tms_utime + t.tms_cutime; | |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5276 fraction = ticks % CLOCKS_PER_SEC; |
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5277 seconds = ticks / CLOCKS_PER_SEC; |
7045 | 5278 |
5279 usr = static_cast<double> (seconds) + static_cast<double>(fraction) / | |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5280 static_cast<double>(CLOCKS_PER_SEC); |
7045 | 5281 |
5282 ticks = t.tms_stime + t.tms_cstime; | |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5283 fraction = ticks % CLOCKS_PER_SEC; |
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5284 seconds = ticks / CLOCKS_PER_SEC; |
7045 | 5285 |
5286 sys = static_cast<double> (seconds) + static_cast<double>(fraction) / | |
10303
e4899d6320b6
data.cc: use CLOCKS_PER_SEC instead of HZ
John W. Eaton <jwe@octave.org>
parents:
10302
diff
changeset
|
5287 static_cast<double>(CLOCKS_PER_SEC); |
7045 | 5288 |
5289 #endif | |
5290 | |
5291 retval (2) = sys; | |
5292 retval (1) = usr; | |
5293 retval (0) = sys + usr; | |
5294 | |
5295 return retval; | |
5296 } | |
5297 | |
7433 | 5298 DEFUN (sort, args, nargout, |
5299 "-*- texinfo -*-\n\ | |
10840 | 5300 @deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})\n\ |
7433 | 5301 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim})\n\ |
5302 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{mode})\n\ | |
5303 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim}, @var{mode})\n\ | |
5304 Return a copy of @var{x} with the elements arranged in increasing\n\ | |
5305 order. For matrices, @code{sort} orders the elements in each column.\n\ | |
5306 \n\ | |
10840 | 5307 For example:\n\ |
7433 | 5308 \n\ |
5309 @example\n\ | |
5310 @group\n\ | |
5311 sort ([1, 2; 2, 3; 3, 1])\n\ | |
5312 @result{} 1 1\n\ | |
5313 2 2\n\ | |
5314 3 3\n\ | |
5315 @end group\n\ | |
5316 @end example\n\ | |
5317 \n\ | |
5318 The @code{sort} function may also be used to produce a matrix\n\ | |
5319 containing the original row indices of the elements in the sorted\n\ | |
10840 | 5320 matrix. For example:\n\ |
7433 | 5321 \n\ |
5322 @example\n\ | |
5323 @group\n\ | |
5324 [s, i] = sort ([1, 2; 2, 3; 3, 1])\n\ | |
5325 @result{} s = 1 1\n\ | |
5326 2 2\n\ | |
5327 3 3\n\ | |
5328 @result{} i = 1 3\n\ | |
5329 2 1\n\ | |
5330 3 2\n\ | |
5331 @end group\n\ | |
5332 @end example\n\ | |
5333 \n\ | |
5334 If the optional argument @var{dim} is given, then the matrix is sorted\n\ | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8972
diff
changeset
|
5335 along the dimension defined by @var{dim}. The optional argument @code{mode}\n\ |
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8972
diff
changeset
|
5336 defines the order in which the values will be sorted. Valid values of\n\ |
7433 | 5337 @code{mode} are `ascend' or `descend'.\n\ |
5338 \n\ | |
5339 For equal elements, the indices are such that the equal elements are listed\n\ | |
5340 in the order that appeared in the original list.\n\ | |
5341 \n\ | |
5342 The @code{sort} function may also be used to sort strings and cell arrays\n\ | |
5343 of strings, in which case the dictionary order of the strings is used.\n\ | |
5344 \n\ | |
5345 The algorithm used in @code{sort} is optimized for the sorting of partially\n\ | |
5346 ordered lists.\n\ | |
5347 @end deftypefn") | |
5348 { | |
5349 octave_value_list retval; | |
5350 | |
5351 int nargin = args.length (); | |
5352 sortmode smode = ASCENDING; | |
5353 | |
5354 if (nargin < 1 || nargin > 3) | |
5355 { | |
5356 print_usage (); | |
5357 return retval; | |
5358 } | |
5359 | |
5360 bool return_idx = nargout > 1; | |
5361 | |
5362 octave_value arg = args(0); | |
5363 | |
5364 int dim = 0; | |
5365 if (nargin > 1) | |
5366 { | |
5367 if (args(1).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5368 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5369 std::string mode = args(1).string_value(); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5370 if (mode == "ascend") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5371 smode = ASCENDING; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5372 else if (mode == "descend") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5373 smode = DESCENDING; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5374 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5375 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5376 error ("sort: mode must be either \"ascend\" or \"descend\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5377 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5378 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5379 } |
7433 | 5380 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5381 dim = args(1).nint_value () - 1; |
7433 | 5382 } |
5383 | |
5384 if (nargin > 2) | |
5385 { | |
5386 if (args(1).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5387 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5388 print_usage (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5389 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5390 } |
7433 | 5391 |
5392 if (! args(2).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5393 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5394 error ("sort: mode must be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5395 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5396 } |
7433 | 5397 std::string mode = args(2).string_value(); |
5398 if (mode == "ascend") | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5399 smode = ASCENDING; |
7433 | 5400 else if (mode == "descend") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5401 smode = DESCENDING; |
7433 | 5402 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5403 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5404 error ("sort: mode must be either \"ascend\" or \"descend\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5405 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5406 } |
7433 | 5407 } |
5408 | |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
5409 const dim_vector dv = arg.dims (); |
7433 | 5410 if (nargin == 1 || args(1).is_string ()) |
5411 { | |
5412 // Find first non singleton dimension | |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
5413 dim = dv.first_non_singleton (); |
7433 | 5414 } |
5415 else | |
5416 { | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10695
diff
changeset
|
5417 if (dim < 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5418 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5419 error ("sort: dim must be a valid dimension"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5420 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5421 } |
7433 | 5422 } |
5423 | |
5424 if (return_idx) | |
5425 { | |
5426 Array<octave_idx_type> sidx; | |
5427 | |
5428 retval (0) = arg.sort (sidx, dim, smode); | |
10285
22a7913bbeb5
optimize return values of find and sort
Jaroslav Hajek <highegg@gmail.com>
parents:
10277
diff
changeset
|
5429 retval (1) = idx_vector (sidx, dv(dim)); // No checking, the extent is known. |
7433 | 5430 } |
5431 else | |
5432 retval(0) = arg.sort (dim, smode); | |
5433 | |
5434 return retval; | |
5435 } | |
5436 | |
5437 /* | |
5438 | |
5439 %% Double | |
5440 %!assert (sort ([NaN, 1, -1, 2, Inf]), [-1, 1, 2, Inf, NaN]) | |
5441 %!assert (sort ([NaN, 1, -1, 2, Inf], 1), [NaN, 1, -1, 2, Inf]) | |
5442 %!assert (sort ([NaN, 1, -1, 2, Inf], 2), [-1, 1, 2, Inf, NaN]) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5443 %!assert (sort ([NaN, 1, -1, 2, Inf], 3), [NaN, 1, -1, 2, Inf]) |
7433 | 5444 %!assert (sort ([NaN, 1, -1, 2, Inf], "ascend"), [-1, 1, 2, Inf, NaN]) |
5445 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "ascend"), [-1, 1, 2, Inf, NaN]) | |
5446 %!assert (sort ([NaN, 1, -1, 2, Inf], "descend"), [NaN, Inf, 2, 1, -1]) | |
5447 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, 1, -1]) | |
5448 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4]), [3, 1, 6, 4; 8, 2, 7, 5]) | |
5449 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 1), [3, 1, 6, 4; 8, 2, 7, 5]) | |
5450 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 2), [1, 3, 5, 7; 2, 4, 6, 8]) | |
5451 %!assert (sort (1), 1) | |
5452 | |
5453 %!test | |
5454 %! [v, i] = sort ([NaN, 1, -1, Inf, 1]); | |
5455 %! assert (v, [-1, 1, 1, Inf, NaN]) | |
5456 %! assert (i, [3, 2, 5, 4, 1]) | |
5457 | |
5458 %% Complex | |
5459 %!assert (sort ([NaN, 1i, -1, 2, Inf]), [1i, -1, 2, Inf, NaN]) | |
5460 %!assert (sort ([NaN, 1i, -1, 2, Inf], 1), [NaN, 1i, -1, 2, Inf]) | |
5461 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2), [1i, -1, 2, Inf, NaN]) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5462 %!assert (sort ([NaN, 1i, -1, 2, Inf], 3), [NaN, 1i, -1, 2, Inf]) |
7433 | 5463 %!assert (sort ([NaN, 1i, -1, 2, Inf], "ascend"), [1i, -1, 2, Inf, NaN]) |
5464 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "ascend"), [1i, -1, 2, Inf, NaN]) | |
5465 %!assert (sort ([NaN, 1i, -1, 2, Inf], "descend"), [NaN, Inf, 2, -1, 1i]) | |
5466 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, -1, 1i]) | |
5467 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4]), [3, 1i, 6, 4; 8, 2, 7, 5]) | |
5468 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 1), [3, 1i, 6, 4; 8, 2, 7, 5]) | |
5469 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 2), [1i, 3, 5, 7; 2, 4, 6, 8]) | |
5470 %!assert (sort (1i), 1i) | |
5471 | |
5472 %!test | |
5473 %! [v, i] = sort ([NaN, 1i, -1, Inf, 1, 1i]); | |
5474 %! assert (v, [1, 1i, 1i, -1, Inf, NaN]) | |
5475 %! assert (i, [5, 2, 6, 3, 4, 1]) | |
5476 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5477 %% Single |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5478 %!assert (sort (single([NaN, 1, -1, 2, Inf])), single([-1, 1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5479 %!assert (sort (single([NaN, 1, -1, 2, Inf]), 1), single([NaN, 1, -1, 2, Inf])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5480 %!assert (sort (single([NaN, 1, -1, 2, Inf]), 2), single([-1, 1, 2, Inf, NaN])) |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5481 %!assert (sort (single([NaN, 1, -1, 2, Inf]), 3), single([NaN, 1, -1, 2, Inf])) |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5482 %!assert (sort (single([NaN, 1, -1, 2, Inf]), "ascend"), single([-1, 1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5483 %!assert (sort (single([NaN, 1, -1, 2, Inf]), 2, "ascend"), single([-1, 1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5484 %!assert (sort (single([NaN, 1, -1, 2, Inf]), "descend"), single([NaN, Inf, 2, 1, -1])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5485 %!assert (sort (single([NaN, 1, -1, 2, Inf]), 2, "descend"), single([NaN, Inf, 2, 1, -1])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5486 %!assert (sort (single([3, 1, 7, 5; 8, 2, 6, 4])), single([3, 1, 6, 4; 8, 2, 7, 5])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5487 %!assert (sort (single([3, 1, 7, 5; 8, 2, 6, 4]), 1), single([3, 1, 6, 4; 8, 2, 7, 5])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5488 %!assert (sort (single([3, 1, 7, 5; 8, 2, 6, 4]), 2), single([1, 3, 5, 7; 2, 4, 6, 8])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5489 %!assert (sort (single(1)), single(1)) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5490 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5491 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5492 %! [v, i] = sort (single([NaN, 1, -1, Inf, 1])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5493 %! assert (v, single([-1, 1, 1, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5494 %! assert (i, [3, 2, 5, 4, 1]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5495 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5496 %% Single Complex |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5497 %!assert (sort (single([NaN, 1i, -1, 2, Inf])), single([1i, -1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5498 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), 1), single([NaN, 1i, -1, 2, Inf])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5499 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), 2), single([1i, -1, 2, Inf, NaN])) |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5500 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), 3), single([NaN, 1i, -1, 2, Inf])) |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5501 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), "ascend"), single([1i, -1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5502 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), 2, "ascend"), single([1i, -1, 2, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5503 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), "descend"), single([NaN, Inf, 2, -1, 1i])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5504 %!assert (sort (single([NaN, 1i, -1, 2, Inf]), 2, "descend"), single([NaN, Inf, 2, -1, 1i])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5505 %!assert (sort (single([3, 1i, 7, 5; 8, 2, 6, 4])), single([3, 1i, 6, 4; 8, 2, 7, 5])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5506 %!assert (sort (single([3, 1i, 7, 5; 8, 2, 6, 4]), 1), single([3, 1i, 6, 4; 8, 2, 7, 5])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5507 %!assert (sort (single([3, 1i, 7, 5; 8, 2, 6, 4]), 2), single([1i, 3, 5, 7; 2, 4, 6, 8])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5508 %!assert (sort (single(1i)),single( 1i)) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5509 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5510 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5511 %! [v, i] = sort (single([NaN, 1i, -1, Inf, 1, 1i])); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5512 %! assert (v, single([1, 1i, 1i, -1, Inf, NaN])) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5513 %! assert (i, [5, 2, 6, 3, 4, 1]) |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5514 |
7433 | 5515 %% Bool |
5516 %!assert (sort ([true, false, true, false]), [false, false, true, true]) | |
5517 %!assert (sort ([true, false, true, false], 1), [true, false, true, false]) | |
5518 %!assert (sort ([true, false, true, false], 2), [false, false, true, true]) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5519 %!assert (sort ([true, false, true, false], 3), [true, false, true, false]) |
7433 | 5520 %!assert (sort ([true, false, true, false], "ascend"), [false, false, true, true]) |
5521 %!assert (sort ([true, false, true, false], 2, "ascend"), [false, false, true, true]) | |
5522 %!assert (sort ([true, false, true, false], "descend"), [true, true, false, false]) | |
5523 %!assert (sort ([true, false, true, false], 2, "descend"), [true, true, false, false]) | |
5524 %!assert (sort (true), true) | |
5525 | |
5526 %!test | |
5527 %! [v, i] = sort ([true, false, true, false]); | |
5528 %! assert (v, [false, false, true, true]) | |
5529 %! assert (i, [2, 4, 1, 3]) | |
5530 | |
5531 %% Sparse Double | |
5532 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf])), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
5533 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1, 0, -1, 2, Inf])) | |
5534 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5535 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 3), sparse ([0, NaN, 1, 0, -1, 2, Inf])) |
7433 | 5536 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) |
5537 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
5538 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) | |
5539 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) | |
5540 | |
5541 %!shared a | |
5542 %! a = randn (10, 10); | |
5543 %! a (a < 0) = 0; | |
5544 %!assert (sort (sparse (a)), sparse (sort (a))) | |
5545 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) | |
5546 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) | |
5547 %!test | |
5548 %! [v, i] = sort (a); | |
5549 %! [vs, is] = sort (sparse (a)); | |
5550 %! assert (vs, sparse (v)) | |
5551 %! assert (is, i) | |
5552 | |
5553 %% Sparse Complex | |
5554 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf])), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
5555 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1i, 0, -1, 2, Inf])) | |
5556 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5557 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 3), sparse ([0, NaN, 1i, 0, -1, 2, Inf])) |
7433 | 5558 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) |
5559 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
5560 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) | |
5561 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) | |
5562 | |
5563 %!shared a | |
5564 %! a = randn (10, 10); | |
5565 %! a (a < 0) = 0; | |
5566 %! a = 1i * a; | |
5567 %!assert (sort (sparse (a)), sparse (sort (a))) | |
5568 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) | |
5569 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) | |
5570 %!test | |
5571 %! [v, i] = sort (a); | |
5572 %! [vs, is] = sort (sparse (a)); | |
5573 %! assert (vs, sparse (v)) | |
5574 %! assert (is, i) | |
5575 | |
5576 %% Sparse Bool | |
5577 %!assert (sort (sparse ([true, false, true, false])), sparse ([false, false, true, true])) | |
5578 %!assert (sort (sparse([true, false, true, false]), 1), sparse ([true, false, true, false])) | |
5579 %!assert (sort (sparse ([true, false, true, false]), 2), sparse ([false, false, true, true])) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5580 %!assert (sort (sparse ([true, false, true, false]), 3), sparse ([true, false, true, false])) |
7433 | 5581 %!assert (sort (sparse ([true, false, true, false]), "ascend"), sparse([false, false, true, true])) |
5582 %!assert (sort (sparse ([true, false, true, false]), 2, "ascend"), sparse([false, false, true, true])) | |
5583 %!assert (sort (sparse ([true, false, true, false]), "descend"), sparse ([true, true, false, false])) | |
5584 %!assert (sort (sparse ([true, false, true, false]), 2, "descend"), sparse([true, true, false, false])) | |
5585 | |
5586 %!test | |
5587 %! [v, i] = sort (sparse([true, false, true, false])); | |
5588 %! assert (v, sparse([false, false, true, true])) | |
5589 %! assert (i, [2, 4, 1, 3]) | |
5590 | |
5591 %% Cell string array | |
5592 %!shared a, b, c | |
5593 %! a = {"Alice", "Cecile", "Eric", "Barry", "David"}; | |
5594 %! b = {"Alice", "Barry", "Cecile", "David", "Eric"}; | |
5595 %! c = {"Eric", "David", "Cecile", "Barry", "Alice"}; | |
5596 %!assert (sort (a), b); | |
5597 %!assert (sort (a, 1), a) | |
5598 %!assert (sort (a, 2), b) | |
10707
69c5f5ec55d7
fix invalid sort tests
Jaroslav Hajek <highegg@gmail.com>
parents:
10703
diff
changeset
|
5599 %!assert (sort (a, 3), a) |
7433 | 5600 %!assert (sort (a, "ascend"), b) |
5601 %!assert (sort (a, 2, "ascend"), b) | |
5602 %!assert (sort (a, "descend"), c) | |
5603 %!assert (sort (a, 2, "descend"), c) | |
5604 | |
5605 %!test | |
5606 %! [v, i] = sort (a); | |
5607 %! assert (i, [1, 4, 2, 5, 3]) | |
5608 | |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5609 %!error <Invalid call to sort.*> sort (); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5610 %!error <Invalid call to sort.*> sort (1, 2, 3, 4); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7795
diff
changeset
|
5611 |
7433 | 5612 */ |
5613 | |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5614 // Sort the rows of the matrix @var{a} according to the order |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5615 // specified by @var{mode}, which can either be `ascend' or `descend' |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5616 // and return the index vector corresponding to the sort order. |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5617 // |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5618 // This function does not yet support sparse matrices. |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5619 |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
5620 DEFUN (__sort_rows_idx__, args, , |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5621 "-*- texinfo -*-\n\ |
9517
a85dbf7b6ff9
fix some functions help markup
Jaroslav Hajek <highegg@gmail.com>
parents:
9513
diff
changeset
|
5622 @deftypefn {Built-in Function} {} __sort_rows_idx__ (@var{a}, @var{mode})\n\ |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8799
diff
changeset
|
5623 Undocumented internal function.\n\ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5624 @end deftypefn\n") |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5625 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5626 octave_value retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5627 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5628 int nargin = args.length (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5629 sortmode smode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5630 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5631 if (nargin < 1 || nargin > 2 || (nargin == 2 && ! args(1).is_string ())) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5632 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5633 print_usage (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5634 return retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5635 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5636 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5637 if (nargin > 1) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5638 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5639 std::string mode = args(1).string_value(); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5640 if (mode == "ascend") |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5641 smode = ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5642 else if (mode == "descend") |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5643 smode = DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5644 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5645 { |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
5646 error ("__sort_rows_idx__: mode must be either \"ascend\" or \"descend\""); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5647 return retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5648 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5649 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5650 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5651 octave_value arg = args(0); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5652 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5653 if (arg.is_sparse_type ()) |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
5654 error ("__sort_rows_idx__: sparse matrices not yet supported"); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5655 if (arg.ndims () == 2) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5656 { |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
5657 Array<octave_idx_type> idx = arg.sort_rows_idx (smode); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5658 |
9351
e2344f4af0cb
autocache indices returned from find et al.
Jaroslav Hajek <highegg@gmail.com>
parents:
9339
diff
changeset
|
5659 retval = octave_value (idx, true, true); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5660 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5661 else |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8721
diff
changeset
|
5662 error ("__sort_rows_idx__: needs a 2-dimensional object"); |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5663 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5664 return retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5665 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5666 |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5667 static sortmode |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5668 get_sort_mode_option (const octave_value& arg, const char *argn) |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5669 { |
10076
4b270d1540f7
avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents:
10073
diff
changeset
|
5670 // FIXME -- we initialize to UNSORTED here to avoid a GCC warning |
4b270d1540f7
avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents:
10073
diff
changeset
|
5671 // about possibly using sortmode uninitialized. |
4b270d1540f7
avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents:
10073
diff
changeset
|
5672 // FIXME -- shouldn't these modes be scoped inside a class? |
4b270d1540f7
avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents:
10073
diff
changeset
|
5673 sortmode smode = UNSORTED; |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5674 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5675 std::string mode = arg.string_value (); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5676 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5677 if (error_state) |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5678 error ("issorted: expecting %s argument to be a character string", argn); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5679 else if (mode == "ascending") |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5680 smode = ASCENDING; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5681 else if (mode == "descending") |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5682 smode = DESCENDING; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5683 else if (mode == "either") |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5684 smode = UNSORTED; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5685 else |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5686 error ("issorted: expecting MODE to be \"ascending\", \"descending\", or \"either\""); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5687 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5688 return smode; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5689 } |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5690 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5691 DEFUN (issorted, args, , |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5692 "-*- texinfo -*-\n\ |
10840 | 5693 @deftypefn {Built-in Function} {} issorted (@var{a}, @var{mode})\n\ |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5694 @deftypefnx {Built-in Function} {} issorted (@var{a}, @code{\"rows\"}, @var{mode})\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5695 Returns true if the array is sorted according to @var{mode}, which\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5696 may be either \"ascending\", \"descending\", or \"either\". By default,\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5697 @var{mode} is \"ascending\". NaNs are treated as by @code{sort}.\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5698 If @var{rows} is supplied and has the value \"rows\", checks whether\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5699 the array is sorted by rows as if output by @code{sortrows} (with no\n\ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5700 options).\n\ |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5701 \n\ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5702 This function does not yet support sparse matrices.\n\ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5703 @seealso{sortrows, sort}\n\ |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5704 @end deftypefn\n") |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5705 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5706 octave_value retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5707 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5708 int nargin = args.length (); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5709 |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5710 if (nargin < 1 || nargin > 3) |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5711 { |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5712 print_usage (); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5713 return retval; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5714 } |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5715 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5716 bool by_rows = false; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5717 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5718 sortmode smode = ASCENDING; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5719 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5720 if (nargin > 1) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5721 { |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5722 octave_value mode_arg; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5723 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5724 if (nargin == 3) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5725 smode = get_sort_mode_option (args(2), "third"); |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5726 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5727 std::string tmp = args(1).string_value (); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5728 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5729 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5730 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5731 if (tmp == "rows") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5732 by_rows = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5733 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5734 smode = get_sort_mode_option (args(1), "second"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5735 } |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5736 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5737 error ("expecting second argument to be character string"); |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5738 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5739 if (error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5740 return retval; |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5741 } |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5742 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5743 octave_value arg = args(0); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5744 |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5745 if (by_rows) |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5746 { |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5747 if (arg.is_sparse_type ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5748 error ("issorted: sparse matrices not yet supported"); |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5749 if (arg.ndims () == 2) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5750 retval = arg.is_sorted_rows (smode) != UNSORTED; |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5751 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5752 error ("issorted: needs a 2-dimensional object"); |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5753 } |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5754 else |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5755 { |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5756 if (arg.dims ().is_vector ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10303
diff
changeset
|
5757 retval = args(0).is_sorted (smode) != UNSORTED; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5758 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5759 error ("issorted: needs a vector"); |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5760 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5761 |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5762 return retval; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5763 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5764 |
9765
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5765 /* |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5766 %!shared sm, um, sv, uv |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5767 %! sm = [1, 2; 3, 4]; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5768 %! um = [3, 1; 2, 4]; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5769 %! sv = [1, 2, 3, 4]; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5770 %! uv = [2, 1, 4, 3]; |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5771 %!assert(issorted (sm, "rows")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5772 %!assert(!issorted (um, "rows")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5773 %!assert(issorted (sv)); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5774 %!assert(!issorted (uv)); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5775 %!assert(issorted (sv')); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5776 %!assert(!issorted (uv')); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5777 %!assert(issorted (sm, "rows", "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5778 %!assert(!issorted (um, "rows", "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5779 %!assert(issorted (sv, "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5780 %!assert(!issorted (uv, "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5781 %!assert(issorted (sv', "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5782 %!assert(!issorted (uv', "ascending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5783 %!assert(!issorted (sm, "rows", "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5784 %!assert(issorted (flipud (sm), "rows", "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5785 %!assert(!issorted (sv, "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5786 %!assert(issorted (fliplr (sv), "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5787 %!assert(!issorted (sv', "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5788 %!assert(issorted (fliplr (sv)', "descending")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5789 %!assert(!issorted (um, "rows", "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5790 %!assert(!issorted (uv, "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5791 %!assert(issorted (sm, "rows", "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5792 %!assert(issorted (flipud (sm), "rows", "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5793 %!assert(issorted (sv, "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5794 %!assert(issorted (fliplr (sv), "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5795 %!assert(issorted (sv', "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5796 %!assert(issorted (fliplr (sv)', "either")); |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5797 */ |
7fc1c8c47b86
issorted: new option for sort mode
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
5798 |
9725 | 5799 DEFUN (nth_element, args, , |
5800 "-*- texinfo -*-\n\ | |
10840 | 5801 @deftypefn {Built-in Function} {} nth_element (@var{x}, @var{n})\n\ |
9725 | 5802 @deftypefnx {Built-in Function} {} nth_element (@var{x}, @var{n}, @var{dim})\n\ |
10840 | 5803 Select the n-th smallest element of a vector, using the ordering defined by\n\ |
5804 @code{sort}. In other words, the result is equivalent to\n\ | |
5805 @code{sort(@var{x})(@var{n})}.\n\ | |
9725 | 5806 @var{n} can also be a contiguous range, either ascending @code{l:u}\n\ |
5807 or descending @code{u:-1:l}, in which case a range of elements is returned.\n\ | |
10840 | 5808 If @var{x} is an array, @code{nth_element} operates along the dimension defined\n\ |
5809 by @var{dim}, or the first non-singleton dimension if @var{dim} is not given.\n\ | |
9725 | 5810 \n\ |
10840 | 5811 nth_element encapsulates the C++ standard library algorithms nth_element and\n\ |
5812 partial_sort. On average, the complexity of the operation is O(M*log(K)), where\n\ | |
9725 | 5813 @code{M = size(@var{x}, @var{dim})} and @code{K = length (@var{n})}.\n\ |
5814 This function is intended for cases where the ratio K/M is small; otherwise,\n\ | |
5815 it may be better to use @code{sort}.\n\ | |
5816 @seealso{sort, min, max}\n\ | |
5817 @end deftypefn") | |
5818 { | |
5819 octave_value retval; | |
5820 int nargin = args.length (); | |
5821 | |
5822 if (nargin == 2 || nargin == 3) | |
5823 { | |
5824 octave_value argx = args(0); | |
5825 | |
5826 int dim = -1; | |
5827 if (nargin == 3) | |
5828 { | |
5829 dim = args(2).int_value (true) - 1; | |
10703
5eb420d92307
fix sort and nth_element when trailing singleton dim is specified
Jaroslav Hajek <highegg@gmail.com>
parents:
10695
diff
changeset
|
5830 if (dim < 0) |
9725 | 5831 error ("nth_element: dim must be a valid dimension"); |
5832 } | |
5833 if (dim < 0) | |
5834 dim = argx.dims ().first_non_singleton (); | |
5835 | |
5836 idx_vector n = args(1).index_vector (); | |
5837 | |
5838 if (error_state) | |
5839 return retval; | |
5840 | |
5841 switch (argx.builtin_type ()) | |
5842 { | |
5843 case btyp_double: | |
5844 retval = argx.array_value ().nth_element (n, dim); | |
5845 break; | |
5846 case btyp_float: | |
5847 retval = argx.float_array_value ().nth_element (n, dim); | |
5848 break; | |
5849 case btyp_complex: | |
5850 retval = argx.complex_array_value ().nth_element (n, dim); | |
5851 break; | |
5852 case btyp_float_complex: | |
5853 retval = argx.float_complex_array_value ().nth_element (n, dim); | |
5854 break; | |
5855 #define MAKE_INT_BRANCH(X) \ | |
5856 case btyp_ ## X: \ | |
5857 retval = argx.X ## _array_value ().nth_element (n, dim); \ | |
5858 break | |
5859 | |
5860 MAKE_INT_BRANCH (int8); | |
5861 MAKE_INT_BRANCH (int16); | |
5862 MAKE_INT_BRANCH (int32); | |
5863 MAKE_INT_BRANCH (int64); | |
5864 MAKE_INT_BRANCH (uint8); | |
5865 MAKE_INT_BRANCH (uint16); | |
5866 MAKE_INT_BRANCH (uint32); | |
5867 MAKE_INT_BRANCH (uint64); | |
5868 #undef MAKE_INT_BRANCH | |
5869 default: | |
5870 if (argx.is_cellstr ()) | |
5871 retval = argx.cellstr_value ().nth_element (n, dim); | |
5872 else | |
5873 gripe_wrong_type_arg ("nth_element", argx); | |
5874 } | |
5875 } | |
5876 else | |
5877 print_usage (); | |
5878 | |
5879 return retval; | |
5880 } | |
5881 | |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5882 template <class NDT> |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5883 static NDT |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5884 do_accumarray_sum (const idx_vector& idx, const NDT& vals, |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5885 octave_idx_type n = -1) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5886 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5887 typedef typename NDT::element_type T; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5888 if (n < 0) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5889 n = idx.extent (0); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5890 else if (idx.extent (n) > n) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5891 error ("accumarray: index out of range"); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5892 |
9858
47c5af1868df
move idx_add methods to MArrayN
Jaroslav Hajek <highegg@gmail.com>
parents:
9856
diff
changeset
|
5893 NDT retval (dim_vector (n, 1), T()); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5894 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5895 if (vals.numel () == 1) |
9858
47c5af1868df
move idx_add methods to MArrayN
Jaroslav Hajek <highegg@gmail.com>
parents:
9856
diff
changeset
|
5896 retval.idx_add (idx, vals (0)); |
47c5af1868df
move idx_add methods to MArrayN
Jaroslav Hajek <highegg@gmail.com>
parents:
9856
diff
changeset
|
5897 else if (vals.numel () == idx.length (n)) |
47c5af1868df
move idx_add methods to MArrayN
Jaroslav Hajek <highegg@gmail.com>
parents:
9856
diff
changeset
|
5898 retval.idx_add (idx, vals); |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5899 else |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5900 error ("accumarray: dimensions mismatch"); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5901 |
9858
47c5af1868df
move idx_add methods to MArrayN
Jaroslav Hajek <highegg@gmail.com>
parents:
9856
diff
changeset
|
5902 return retval; |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5903 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5904 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5905 DEFUN (__accumarray_sum__, args, , |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5906 "-*- texinfo -*-\n\ |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5907 @deftypefn {Built-in Function} {} __accumarray_sum__ (@var{idx}, @var{vals}, @var{n})\n\ |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5908 Undocumented internal function.\n\ |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5909 @end deftypefn") |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5910 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5911 octave_value retval; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5912 int nargin = args.length (); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5913 if (nargin >= 2 && nargin <= 3 && args(0).is_numeric_type ()) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5914 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5915 idx_vector idx = args(0).index_vector (); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5916 octave_idx_type n = -1; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5917 if (nargin == 3) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5918 n = args(2).idx_type_value (true); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5919 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5920 if (! error_state) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5921 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5922 octave_value vals = args(1); |
9198
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5923 if (vals.is_range ()) |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5924 { |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5925 Range r = vals.range_value (); |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5926 if (r.inc () == 0) |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5927 vals = r.base (); |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5928 } |
3b9aacf07744
optimize accumarray with constant ranges
Jaroslav Hajek <highegg@gmail.com>
parents:
9197
diff
changeset
|
5929 |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5930 if (vals.is_single_type ()) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5931 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5932 if (vals.is_complex_type ()) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5933 retval = do_accumarray_sum (idx, vals.float_complex_array_value (), n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5934 else |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5935 retval = do_accumarray_sum (idx, vals.float_array_value (), n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5936 } |
10269
217d36560dfa
small fixes to accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10268
diff
changeset
|
5937 else if (vals.is_numeric_type () || vals.is_bool_type ()) |
8934
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5938 { |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5939 if (vals.is_complex_type ()) |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5940 retval = do_accumarray_sum (idx, vals.complex_array_value (), n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5941 else |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5942 retval = do_accumarray_sum (idx, vals.array_value (), n); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5943 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5944 else |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5945 gripe_wrong_type_arg ("accumarray", vals); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5946 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5947 } |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5948 else |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5949 print_usage (); |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5950 |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5951 return retval; |
c2099a4d12ea
partially optimize accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5952 } |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
5953 |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
5954 template <class NDT> |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5955 static NDT |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5956 do_accumarray_minmax (const idx_vector& idx, const NDT& vals, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5957 octave_idx_type n, bool ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5958 const typename NDT::element_type& zero_val) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5959 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5960 typedef typename NDT::element_type T; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5961 if (n < 0) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5962 n = idx.extent (0); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5963 else if (idx.extent (n) > n) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5964 error ("accumarray: index out of range"); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5965 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5966 NDT retval (dim_vector (n, 1), zero_val); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5967 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5968 // Pick minimizer or maximizer. |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
5969 void (MArray<T>::*op) (const idx_vector&, const MArray<T>&) = |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
5970 ismin ? (&MArray<T>::idx_min) : (&MArray<T>::idx_max); |
10268
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5971 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5972 octave_idx_type l = idx.length (n); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5973 if (vals.numel () == 1) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5974 (retval.*op) (idx, NDT (dim_vector (l, 1), vals(0))); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5975 else if (vals.numel () == l) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5976 (retval.*op) (idx, vals); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5977 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5978 error ("accumarray: dimensions mismatch"); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5979 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5980 return retval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5981 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5982 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5983 static octave_value_list |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5984 do_accumarray_minmax_fun (const octave_value_list& args, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5985 bool ismin) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5986 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5987 octave_value retval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5988 int nargin = args.length (); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5989 if (nargin >= 3 && nargin <= 4 && args(0).is_numeric_type ()) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5990 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5991 idx_vector idx = args(0).index_vector (); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5992 octave_idx_type n = -1; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5993 if (nargin == 4) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5994 n = args(3).idx_type_value (true); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5995 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5996 if (! error_state) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5997 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5998 octave_value vals = args(1), zero = args (2); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
5999 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6000 switch (vals.builtin_type ()) |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6001 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6002 case btyp_double: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6003 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6004 zero.double_value ()); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6005 break; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6006 case btyp_float: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6007 retval = do_accumarray_minmax (idx, vals.float_array_value (), n, ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6008 zero.float_value ()); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6009 break; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6010 case btyp_complex: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6011 retval = do_accumarray_minmax (idx, vals.complex_array_value (), n, ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6012 zero.complex_value ()); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6013 break; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6014 case btyp_float_complex: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6015 retval = do_accumarray_minmax (idx, vals.float_complex_array_value (), n, ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6016 zero.float_complex_value ()); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6017 break; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6018 #define MAKE_INT_BRANCH(X) \ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6019 case btyp_ ## X: \ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6020 retval = do_accumarray_minmax (idx, vals.X ## _array_value (), n, ismin, \ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6021 zero.X ## _scalar_value ()); \ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6022 break |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6023 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6024 MAKE_INT_BRANCH (int8); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6025 MAKE_INT_BRANCH (int16); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6026 MAKE_INT_BRANCH (int32); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6027 MAKE_INT_BRANCH (int64); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6028 MAKE_INT_BRANCH (uint8); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6029 MAKE_INT_BRANCH (uint16); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6030 MAKE_INT_BRANCH (uint32); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6031 MAKE_INT_BRANCH (uint64); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6032 #undef MAKE_INT_BRANCH |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6033 case btyp_bool: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6034 retval = do_accumarray_minmax (idx, vals.array_value (), n, ismin, |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6035 zero.bool_value ()); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6036 break; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6037 default: |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6038 gripe_wrong_type_arg ("accumarray", vals); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6039 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6040 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6041 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6042 else |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6043 print_usage (); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6044 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6045 return retval; |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6046 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6047 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6048 DEFUN (__accumarray_min__, args, , |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6049 "-*- texinfo -*-\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6050 @deftypefn {Built-in Function} {} __accumarray_min__ (@var{idx}, @var{vals}, @var{zero}, @var{n})\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6051 Undocumented internal function.\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6052 @end deftypefn") |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6053 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6054 return do_accumarray_minmax_fun (args, true); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6055 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6056 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6057 DEFUN (__accumarray_max__, args, , |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6058 "-*- texinfo -*-\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6059 @deftypefn {Built-in Function} {} __accumarray_max__ (@var{idx}, @var{vals}, @var{zero}, @var{n})\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6060 Undocumented internal function.\n\ |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6061 @end deftypefn") |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6062 { |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6063 return do_accumarray_minmax_fun (args, false); |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6064 } |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6065 |
9a16a61ed43d
new optimizations for accumarray
Jaroslav Hajek <highegg@gmail.com>
parents:
10240
diff
changeset
|
6066 template <class NDT> |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6067 static NDT |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6068 do_accumdim_sum (const idx_vector& idx, const NDT& vals, |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6069 int dim = -1, octave_idx_type n = -1) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6070 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6071 typedef typename NDT::element_type T; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6072 if (n < 0) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6073 n = idx.extent (0); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6074 else if (idx.extent (n) > n) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6075 error ("accumarray: index out of range"); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6076 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6077 dim_vector rdv = vals.dims (); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6078 if (dim < 0) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6079 dim = vals.dims ().first_non_singleton (); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6080 else if (dim >= rdv.length ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6081 rdv.resize (dim+1, 1); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6082 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6083 rdv(dim) = n; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6084 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6085 NDT retval (rdv, T()); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6086 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6087 retval.idx_add_nd (idx, vals, dim); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6088 return retval; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6089 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6090 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6091 DEFUN (__accumdim_sum__, args, , |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6092 "-*- texinfo -*-\n\ |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6093 @deftypefn {Built-in Function} {} __accumdim_sum__ (@var{idx}, @var{vals}, @var{dim}, @var{n})\n\ |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6094 Undocumented internal function.\n\ |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6095 @end deftypefn") |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6096 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6097 octave_value retval; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6098 int nargin = args.length (); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6099 if (nargin >= 2 && nargin <= 4 && args(0).is_numeric_type ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6100 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6101 idx_vector idx = args(0).index_vector (); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6102 int dim = -1; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6103 if (nargin >= 3) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6104 dim = args(2).int_value () - 1; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6105 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6106 octave_idx_type n = -1; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6107 if (nargin == 4) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6108 n = args(3).idx_type_value (true); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6109 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6110 if (! error_state) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6111 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6112 octave_value vals = args(1); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6113 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6114 if (vals.is_single_type ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6115 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6116 if (vals.is_complex_type ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6117 retval = do_accumdim_sum (idx, vals.float_complex_array_value (), dim, n); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6118 else |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6119 retval = do_accumdim_sum (idx, vals.float_array_value (), dim, n); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6120 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6121 else if (vals.is_numeric_type () || vals.is_bool_type ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6122 { |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6123 if (vals.is_complex_type ()) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6124 retval = do_accumdim_sum (idx, vals.complex_array_value (), dim, n); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6125 else |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6126 retval = do_accumdim_sum (idx, vals.array_value (), dim, n); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6127 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6128 else |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6129 gripe_wrong_type_arg ("accumdim", vals); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6130 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6131 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6132 else |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6133 print_usage (); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6134 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6135 return retval; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6136 } |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6137 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
6138 template <class NDT> |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6139 static NDT |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6140 do_merge (const Array<bool>& mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6141 const NDT& tval, const NDT& fval) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6142 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6143 typedef typename NDT::element_type T; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6144 dim_vector dv = mask.dims (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6145 NDT retval (dv); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6146 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6147 bool tscl = tval.numel () == 1, fscl = fval.numel () == 1; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6148 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6149 if ((! tscl && tval.dims () != dv) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6150 || (! fscl && fval.dims () != dv)) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6151 error ("merge: dimensions mismatch"); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6152 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6153 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6154 T *rv = retval.fortran_vec (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6155 octave_idx_type n = retval.numel (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6156 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6157 const T *tv = tval.data (), *fv = fval.data (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6158 const bool *mv = mask.data (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6159 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6160 if (tscl) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6161 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6162 if (fscl) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6163 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6164 T ts = tv[0], fs = fv[0]; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6165 for (octave_idx_type i = 0; i < n; i++) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6166 rv[i] = mv[i] ? ts : fs; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6167 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6168 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6169 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6170 T ts = tv[0]; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6171 for (octave_idx_type i = 0; i < n; i++) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6172 rv[i] = mv[i] ? ts : fv[i]; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6173 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6174 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6175 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6176 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6177 if (fscl) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6178 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6179 T fs = fv[0]; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6180 for (octave_idx_type i = 0; i < n; i++) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6181 rv[i] = mv[i] ? tv[i] : fs; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6182 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6183 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6184 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6185 for (octave_idx_type i = 0; i < n; i++) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6186 rv[i] = mv[i] ? tv[i] : fv[i]; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6187 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6188 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6189 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6190 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6191 return retval; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6192 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6193 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6194 #define MAKE_INT_BRANCH(INTX) \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6195 else if (tval.is_ ## INTX ## _type () && fval.is_ ## INTX ## _type ()) \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6196 { \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6197 retval = do_merge (mask, \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6198 tval.INTX ## _array_value (), \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6199 fval.INTX ## _array_value ()); \ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6200 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6201 |
9505
a321a1c227c8
make ifelse an alias or merge
Jaroslav Hajek <highegg@gmail.com>
parents:
9475
diff
changeset
|
6202 DEFUN (merge, args, , |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6203 "-*- texinfo -*-\n\ |
10840 | 6204 @deftypefn {Built-in Function} {} merge (@var{mask}, @var{tval}, @var{fval})\n\ |
9505
a321a1c227c8
make ifelse an alias or merge
Jaroslav Hajek <highegg@gmail.com>
parents:
9475
diff
changeset
|
6205 @deftypefnx {Built-in Function} {} ifelse (@var{mask}, @var{tval}, @var{fval})\n\ |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6206 Merges elements of @var{true_val} and @var{false_val}, depending on the value of\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
6207 @var{mask}. If @var{mask} is a logical scalar, the other two arguments can be\n\ |
10840 | 6208 arbitrary values. Otherwise, @var{mask} must be a logical array, and\n\ |
6209 @var{tval},\n\ | |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6210 @var{fval} should be arrays of matching class, or cell arrays.\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6211 In the scalar mask case, @var{tval} is returned if @var{mask} is true, otherwise\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6212 @var{fval} is returned.\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6213 \n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6214 In the array mask case, both @var{tval} and @var{fval} must be either scalars or\n\ |
10840 | 6215 arrays with dimensions equal to @var{mask}. The result is constructed as\n\ |
6216 follows: \n\ | |
6217 \n\ | |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6218 @example\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
6219 @group\n\ |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6220 result(mask) = tval(mask);\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6221 result(! mask) = fval(! mask);\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9736
diff
changeset
|
6222 @end group\n\ |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6223 @end example\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6224 \n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6225 @var{mask} can also be arbitrary numeric type, in which case\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6226 it is first converted to logical.\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6227 @seealso{logical}\n\ |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6228 @end deftypefn") |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6229 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6230 int nargin = args.length (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6231 octave_value retval; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6232 |
9856 | 6233 if (nargin == 3 && (args(0).is_bool_type () || args(0).is_numeric_type ())) |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6234 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6235 octave_value mask_val = args(0); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6236 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6237 if (mask_val.is_scalar_type ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6238 retval = mask_val.is_true () ? args(1) : args(2); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6239 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6240 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6241 boolNDArray mask = mask_val.bool_array_value (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6242 octave_value tval = args(1), fval = args(2); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6243 if (tval.is_double_type () && fval.is_double_type ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6244 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6245 if (tval.is_complex_type () || fval.is_complex_type ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6246 retval = do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6247 tval.complex_array_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6248 fval.complex_array_value ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6249 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6250 retval = do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6251 tval.array_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6252 fval.array_value ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6253 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6254 else if (tval.is_single_type () && fval.is_single_type ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6255 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6256 if (tval.is_complex_type () || fval.is_complex_type ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6257 retval = do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6258 tval.float_complex_array_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6259 fval.float_complex_array_value ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6260 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6261 retval = do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6262 tval.float_array_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6263 fval.float_array_value ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6264 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6265 else if (tval.is_string () && fval.is_string ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6266 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6267 bool sq_string = tval.is_sq_string () || fval.is_sq_string (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6268 retval = octave_value (do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6269 tval.char_array_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6270 fval.char_array_value ()), |
9689
34d6f005db4b
eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents:
9685
diff
changeset
|
6271 sq_string ? '\'' : '"'); |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6272 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6273 else if (tval.is_cell () && fval.is_cell ()) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6274 { |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6275 retval = do_merge (mask, |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6276 tval.cell_value (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6277 fval.cell_value ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6278 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6279 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6280 MAKE_INT_BRANCH (int8) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6281 MAKE_INT_BRANCH (int16) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6282 MAKE_INT_BRANCH (int32) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6283 MAKE_INT_BRANCH (int64) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6284 MAKE_INT_BRANCH (uint8) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6285 MAKE_INT_BRANCH (uint16) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6286 MAKE_INT_BRANCH (uint32) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6287 MAKE_INT_BRANCH (uint64) |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6288 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6289 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6290 error ("merge: cannot merge %s with %s with array mask", |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6291 tval.class_name ().c_str (), |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6292 fval.class_name ().c_str ()); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6293 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6294 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6295 else |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6296 print_usage (); |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6297 |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6298 return retval; |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6299 } |
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6300 |
9505
a321a1c227c8
make ifelse an alias or merge
Jaroslav Hajek <highegg@gmail.com>
parents:
9475
diff
changeset
|
6301 DEFALIAS (ifelse, merge); |
a321a1c227c8
make ifelse an alias or merge
Jaroslav Hajek <highegg@gmail.com>
parents:
9475
diff
changeset
|
6302 |
9473
833109a9f37f
implement merge function
Jaroslav Hajek <highegg@gmail.com>
parents:
9428
diff
changeset
|
6303 #undef MAKE_INT_BRANCH |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8527
diff
changeset
|
6304 |
9513
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6305 template <class SparseT> |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6306 static SparseT |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6307 do_sparse_diff (const SparseT& array, octave_idx_type order, |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6308 int dim) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6309 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6310 SparseT retval = array; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6311 if (dim == 1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6312 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6313 octave_idx_type k = retval.columns (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6314 while (order > 0 && k > 0) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6315 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6316 idx_vector col1 (':'), col2 (':'), sl1 (1, k), sl2 (0, k-1); |
10785
c2041adcf234
remove unnecessary sparse indexing overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
10763
diff
changeset
|
6317 retval = SparseT (retval.index (col1, sl1)) - SparseT (retval.index (col2, sl2)); |
9513
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6318 assert (retval.columns () == k-1); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6319 order--; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6320 k--; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6321 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6322 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6323 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6324 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6325 octave_idx_type k = retval.rows (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6326 while (order > 0 && k > 0) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6327 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6328 idx_vector col1 (':'), col2 (':'), sl1 (1, k), sl2 (0, k-1); |
10785
c2041adcf234
remove unnecessary sparse indexing overloads
Jaroslav Hajek <highegg@gmail.com>
parents:
10763
diff
changeset
|
6329 retval = SparseT (retval.index (sl1, col1)) - SparseT (retval.index (sl2, col2)); |
9513
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6330 assert (retval.rows () == k-1); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6331 order--; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6332 k--; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6333 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6334 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6335 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6336 return retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6337 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6338 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6339 static octave_value |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6340 do_diff (const octave_value& array, octave_idx_type order, |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6341 int dim = -1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6342 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6343 octave_value retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6344 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6345 const dim_vector& dv = array.dims (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6346 if (dim == -1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6347 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6348 dim = array.dims ().first_non_singleton (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6349 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6350 // Bother Matlab. This behavior is really wicked. |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6351 if (dv(dim) <= order) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6352 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6353 if (dv(dim) == 1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6354 retval = array.resize (dim_vector (0, 0)); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6355 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6356 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6357 retval = array; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6358 while (order > 0) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6359 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6360 if (dim == dv.length ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6361 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6362 retval = do_diff (array, order, dim - 1); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6363 order = 0; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6364 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6365 else if (dv(dim) == 1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6366 dim++; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6367 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6368 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6369 retval = do_diff (array, dv(dim) - 1, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6370 order -= dv(dim) - 1; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6371 dim++; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6372 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6373 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6374 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6375 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6376 return retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6377 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6378 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6379 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6380 if (array.is_integer_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6381 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6382 if (array.is_int8_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6383 retval = array.int8_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6384 else if (array.is_int16_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6385 retval = array.int16_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6386 else if (array.is_int32_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6387 retval = array.int32_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6388 else if (array.is_int64_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6389 retval = array.int64_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6390 else if (array.is_uint8_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6391 retval = array.uint8_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6392 else if (array.is_uint16_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6393 retval = array.uint16_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6394 else if (array.is_uint32_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6395 retval = array.uint32_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6396 else if (array.is_uint64_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6397 retval = array.uint64_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6398 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6399 panic_impossible (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6400 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6401 else if (array.is_sparse_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6402 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6403 if (array.is_complex_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6404 retval = do_sparse_diff (array.sparse_complex_matrix_value (), order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6405 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6406 retval = do_sparse_diff (array.sparse_matrix_value (), order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6407 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6408 else if (array.is_single_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6409 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6410 if (array.is_complex_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6411 retval = array.float_complex_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6412 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6413 retval = array.float_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6414 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6415 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6416 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6417 if (array.is_complex_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6418 retval = array.complex_array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6419 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6420 retval = array.array_value ().diff (order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6421 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6422 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6423 return retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6424 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6425 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6426 DEFUN (diff, args, , |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6427 "-*- texinfo -*-\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6428 @deftypefn {Built-in Function} {} diff (@var{x}, @var{k}, @var{dim})\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6429 If @var{x} is a vector of length @var{n}, @code{diff (@var{x})} is the\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6430 vector of first differences\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6431 @tex\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6432 $x_2 - x_1, \\ldots{}, x_n - x_{n-1}$.\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6433 @end tex\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6434 @ifnottex\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6435 @var{x}(2) - @var{x}(1), @dots{}, @var{x}(n) - @var{x}(n-1).\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6436 @end ifnottex\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6437 \n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6438 If @var{x} is a matrix, @code{diff (@var{x})} is the matrix of column\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6439 differences along the first non-singleton dimension.\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6440 \n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6441 The second argument is optional. If supplied, @code{diff (@var{x},\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6442 @var{k})}, where @var{k} is a non-negative integer, returns the\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6443 @var{k}-th differences. It is possible that @var{k} is larger than\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6444 then first non-singleton dimension of the matrix. In this case,\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6445 @code{diff} continues to take the differences along the next\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6446 non-singleton dimension.\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6447 \n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6448 The dimension along which to take the difference can be explicitly\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6449 stated with the optional variable @var{dim}. In this case the \n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6450 @var{k}-th order differences are calculated along this dimension.\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6451 In the case where @var{k} exceeds @code{size (@var{x}, @var{dim})}\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6452 then an empty matrix is returned.\n\ |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6453 @end deftypefn") |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6454 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6455 int nargin = args.length (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6456 octave_value retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6457 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6458 if (nargin < 1 || nargin > 3) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6459 print_usage (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6460 else if (! args(0).is_numeric_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6461 error ("diff: X must be numeric"); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6462 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6463 if (! error_state) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6464 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6465 int dim = -1; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6466 octave_idx_type order = 1; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6467 if (nargin > 1) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6468 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6469 if (args(1).is_scalar_type ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6470 order = args(1).idx_type_value (true, false); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6471 else if (! args(1).is_zero_by_zero ()) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6472 error ("order must be a scalar or []"); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6473 if (! error_state && order < 0) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6474 error ("order must be non-negative"); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6475 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6476 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6477 if (nargin > 2) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6478 { |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6479 dim = args(2).int_value (true, false); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6480 if (! error_state && (dim < 1 || dim > args(0).ndims ())) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6481 error ("needs a valid dimension"); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6482 else |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6483 dim -= 1; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6484 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6485 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6486 if (! error_state) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6487 retval = do_diff (args(0), order, dim); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6488 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6489 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6490 return retval; |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6491 } |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6492 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6493 /* |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6494 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6495 %!assert (diff ([1, 2, 3, 4]), [1, 1, 1]) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6496 %!assert (diff ([1, 3, 7, 19], 2), [2, 8]) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6497 %!assert (diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1]), [4, 2; 3, 3; 1, -1; -6, -5]) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6498 %!assert (diff ([1, 2; 5, 4; 8, 7; 9, 6; 3, 1], 3), [-1, -5; -5, 0]) |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6499 %!assert (isempty (diff (1))); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6500 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6501 %!error diff ([1, 2; 3, 4], -1); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6502 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6503 %!error diff ("foo"); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6504 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6505 %!error diff (); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6506 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6507 %!error diff (1, 2, 3, 4); |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6508 |
9f870f73ab7d
implement built-in diff
Jaroslav Hajek <highegg@gmail.com>
parents:
9505
diff
changeset
|
6509 */ |
10799
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6510 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6511 template <class T> |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6512 static Array<T> |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6513 do_repelems (const Array<T>& src, const Array<octave_idx_type>& rep) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6514 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6515 Array<T> retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6516 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6517 assert (rep.ndims () == 2 && rep.rows () == 2); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6518 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6519 octave_idx_type n = rep.columns (), l = 0; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6520 for (octave_idx_type i = 0; i < n; i++) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6521 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6522 octave_idx_type k = rep(1, i); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6523 if (k < 0) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6524 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6525 error ("repelems: second row must contain nonnegative numbers"); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6526 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6527 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6528 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6529 l += k; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6530 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6531 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6532 retval.clear (1, l); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6533 T *dest = retval.fortran_vec (); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6534 l = 0; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6535 for (octave_idx_type i = 0; i < n; i++) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6536 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6537 octave_idx_type k = rep(1, i); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6538 std::fill_n (dest, k, src.checkelem (rep(0, i) - 1)); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6539 dest += k; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6540 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6541 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6542 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6543 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6544 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6545 DEFUN (repelems, args, , |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6546 "-*- texinfo -*-\n\ |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6547 @deftypefn {Built-in Function} {} repelems (@var{x}, @var{r})\n\ |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6548 Construct a vector of repeated elements from @var{x}. @var{r}\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6549 is a 2x@var{N} integer matrix specifying which elements to repeat and\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6550 how often to repeat each element.\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6551 \n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6552 Entries in the first row, @var{r}(1,j), select an element to repeat.\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6553 The corresponding entry in the second row, @var{r}(2,j), specifies\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6554 the repeat count. If @var{x} is a matrix then the columns of @var{x} are\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6555 imagined to be stacked on top of each other for purposes of the selection\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6556 index. A row vector is always returned.\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6557 \n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6558 Conceptually the result is calculated as follows:\n\ |
10799
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6559 \n\ |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6560 @example\n\ |
10840 | 6561 @group\n\ |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6562 y = [];\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6563 for i = 1:columns (@var{r})\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6564 y = [y, @var{x}(@var{r}(1,i)*ones(1, @var{r}(2,i)))];\n\ |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6565 endfor\n\ |
10840 | 6566 @end group\n\ |
10799
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6567 @end example\n\ |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10799
diff
changeset
|
6568 @seealso{repmat}\n\ |
10799
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6569 @end deftypefn") |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6570 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6571 octave_value retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6572 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6573 if (args.length () == 2) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6574 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6575 octave_value x = args(0); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6576 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6577 const Matrix rm = args(1).matrix_value (); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6578 if (error_state) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6579 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6580 else if (rm.rows () != 2 || rm.ndims () != 2) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6581 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6582 error ("repelems: second argument must be a matrix with two rows"); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6583 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6584 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6585 else |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6586 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6587 NoAlias< Array<octave_idx_type> > r (rm.dims ()); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6588 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6589 for (octave_idx_type i = 0; i < rm.numel (); i++) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6590 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6591 octave_idx_type rx = rm(i); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6592 if (static_cast<double> (rx) != rm(i)) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6593 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6594 error ("repelems: a matrix of integers is expected."); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6595 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6596 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6597 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6598 r(i) = rx; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6599 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6600 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6601 switch (x.builtin_type ()) |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6602 { |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6603 #define BTYP_BRANCH(X, EX) \ |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6604 case btyp_ ## X: \ |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6605 retval = do_repelems (x.EX ## _value (), r); \ |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6606 break |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6607 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6608 BTYP_BRANCH (double, array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6609 BTYP_BRANCH (float, float_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6610 BTYP_BRANCH (complex, complex_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6611 BTYP_BRANCH (float_complex, float_complex_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6612 BTYP_BRANCH (bool, bool_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6613 BTYP_BRANCH (char, char_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6614 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6615 BTYP_BRANCH (int8, int8_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6616 BTYP_BRANCH (int16, int16_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6617 BTYP_BRANCH (int32, int32_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6618 BTYP_BRANCH (int64, int64_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6619 BTYP_BRANCH (uint8, uint8_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6620 BTYP_BRANCH (uint16, uint16_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6621 BTYP_BRANCH (uint32, uint32_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6622 BTYP_BRANCH (uint64, uint64_array); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6623 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6624 BTYP_BRANCH (cell, cell); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6625 //BTYP_BRANCH (struct, map);//FIXME |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6626 #undef BTYP_BRANCH |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6627 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6628 default: |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6629 gripe_wrong_type_arg ("repelems", x); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6630 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6631 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6632 } |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6633 else |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6634 print_usage (); |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6635 |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6636 return retval; |
177f1ad7c7c1
new built-in: repelems
Jaroslav Hajek <highegg@gmail.com>
parents:
10785
diff
changeset
|
6637 } |