Mercurial > hg > octave-max
annotate src/data.cc @ 7740:39930366b709
implement builtin log2
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 30 Apr 2008 03:30:34 -0400 |
parents | b2a5cda5c549 |
children | 72c0489653ac |
rev | line source |
---|---|
523 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
4 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
523 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
523 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
523 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
523 | 26 #endif |
27 | |
7078 | 28 #include "systime.h" |
29 | |
30 #ifdef HAVE_SYS_TYPES_H | |
31 #include <sys/types.h> | |
32 #endif | |
33 | |
34 #ifdef HAVE_SYS_RESOURCE_H | |
35 #include <sys/resource.h> | |
36 #endif | |
37 | |
2184 | 38 #include <cfloat> |
39 | |
1728 | 40 #include <string> |
41 | |
2184 | 42 #include "lo-ieee.h" |
7231 | 43 #include "lo-math.h" |
1755 | 44 #include "str-vec.h" |
4153 | 45 #include "quit.h" |
1755 | 46 |
6953 | 47 #include "Cell.h" |
1352 | 48 #include "defun.h" |
49 #include "error.h" | |
50 #include "gripes.h" | |
6953 | 51 #include "oct-map.h" |
52 #include "oct-obj.h" | |
2366 | 53 #include "ov.h" |
5476 | 54 #include "ov-complex.h" |
55 #include "ov-cx-mat.h" | |
6953 | 56 #include "parse.h" |
57 #include "pt-mat.h" | |
523 | 58 #include "utils.h" |
6953 | 59 #include "variables.h" |
7045 | 60 #include "pager.h" |
523 | 61 |
4015 | 62 #define ANY_ALL(FCN) \ |
63 \ | |
4233 | 64 octave_value retval; \ |
4015 | 65 \ |
66 int nargin = args.length (); \ | |
67 \ | |
4021 | 68 if (nargin == 1 || nargin == 2) \ |
4015 | 69 { \ |
4021 | 70 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
71 \ | |
72 if (! error_state) \ | |
73 { \ | |
4556 | 74 if (dim >= -1) \ |
4015 | 75 retval = args(0).FCN (dim); \ |
4021 | 76 else \ |
77 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | |
78 } \ | |
4015 | 79 else \ |
4021 | 80 error (#FCN ": expecting dimension argument to be an integer"); \ |
4015 | 81 } \ |
4021 | 82 else \ |
5823 | 83 print_usage (); \ |
4015 | 84 \ |
85 return retval | |
86 | |
1957 | 87 DEFUN (all, args, , |
3369 | 88 "-*- texinfo -*-\n\ |
4015 | 89 @deftypefn {Built-in Function} {} all (@var{x}, @var{dim})\n\ |
3369 | 90 The function @code{all} behaves like the function @code{any}, except\n\ |
91 that it returns true only if all the elements of a vector, or all the\n\ | |
4015 | 92 elements along dimension @var{dim} of a matrix, are nonzero.\n\ |
3369 | 93 @end deftypefn") |
523 | 94 { |
4015 | 95 ANY_ALL (all); |
523 | 96 } |
97 | |
1957 | 98 DEFUN (any, args, , |
3369 | 99 "-*- texinfo -*-\n\ |
4015 | 100 @deftypefn {Built-in Function} {} any (@var{x}, @var{dim})\n\ |
3369 | 101 For a vector argument, return 1 if any element of the vector is\n\ |
102 nonzero.\n\ | |
103 \n\ | |
104 For a matrix argument, return a row vector of ones and\n\ | |
105 zeros with each element indicating whether any of the elements of the\n\ | |
106 corresponding column of the matrix are nonzero. For example,\n\ | |
107 \n\ | |
108 @example\n\ | |
109 @group\n\ | |
110 any (eye (2, 4))\n\ | |
111 @result{} [ 1, 1, 0, 0 ]\n\ | |
112 @end group\n\ | |
113 @end example\n\ | |
114 \n\ | |
4015 | 115 If the optional argument @var{dim} is supplied, work along dimension\n\ |
116 @var{dim}. For example,\n\ | |
3369 | 117 \n\ |
118 @example\n\ | |
4015 | 119 @group\n\ |
120 any (eye (2, 4), 2)\n\ | |
121 @result{} [ 1; 1 ]\n\ | |
122 @end group\n\ | |
3369 | 123 @end example\n\ |
124 @end deftypefn") | |
523 | 125 { |
4015 | 126 ANY_ALL (any); |
523 | 127 } |
128 | |
649 | 129 // These mapping functions may also be useful in other places, eh? |
130 | |
131 typedef double (*d_dd_fcn) (double, double); | |
132 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
133 static NDArray |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
134 map_d_m (d_dd_fcn f, double x, const NDArray& y) |
649 | 135 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
136 NDArray retval (y.dims ()); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
137 double *r_data = retval.fortran_vec (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
138 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
139 const double *y_data = y.data (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
140 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
141 octave_idx_type nel = y.numel (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
142 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
143 for (octave_idx_type i = 0; i < nel; i++) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
144 { |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
145 OCTAVE_QUIT; |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
146 r_data[i] = f (x, y_data[i]); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
147 } |
649 | 148 |
149 return retval; | |
150 } | |
151 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
152 static NDArray |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
153 map_m_d (d_dd_fcn f, const NDArray& x, double y) |
649 | 154 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
155 NDArray retval (x.dims ()); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
156 double *r_data = retval.fortran_vec (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
157 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
158 const double *x_data = x.data (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
159 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
160 octave_idx_type nel = x.numel (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
161 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
162 for (octave_idx_type i = 0; i < nel; i++) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
163 { |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
164 OCTAVE_QUIT; |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
165 r_data[i] = f (x_data[i], y); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
166 } |
649 | 167 |
168 return retval; | |
169 } | |
170 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
171 static NDArray |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
172 map_m_m (d_dd_fcn f, const NDArray& x, const NDArray& y) |
649 | 173 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
174 assert (x.dims () == y.dims ()); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
175 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
176 NDArray retval (x.dims ()); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
177 double *r_data = retval.fortran_vec (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
178 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
179 const double *x_data = x.data (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
180 const double *y_data = y.data (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
181 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
182 octave_idx_type nel = x.numel (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
183 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
184 for (octave_idx_type i = 0; i < nel; i++) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
185 { |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
186 OCTAVE_QUIT; |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
187 r_data[i] = f (x_data[i], y_data[i]); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
188 } |
649 | 189 |
190 return retval; | |
191 } | |
192 | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
193 static SparseMatrix |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
194 map_d_s (d_dd_fcn f, double x, const SparseMatrix& y) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
195 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
196 octave_idx_type nr = y.rows (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
197 octave_idx_type nc = y.columns (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
198 SparseMatrix retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
199 double f_zero = f (x, 0.); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
200 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
201 if (f_zero != 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
202 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
203 retval = SparseMatrix (nr, nc, f_zero); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
204 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
205 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
206 for (octave_idx_type i = y.cidx (j); i < y.cidx (j+1); i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
207 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
208 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
209 retval.data (y.ridx(i) + j * nr) = f (x, y.data (i)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
210 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
211 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
212 retval.maybe_compress (true); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
213 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
214 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
215 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
216 octave_idx_type nz = y.nnz (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
217 retval = SparseMatrix (nr, nc, nz); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
218 octave_idx_type ii = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
219 retval.cidx (ii) = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
220 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
221 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
222 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
223 for (octave_idx_type i = y.cidx (j); i < y.cidx (j+1); i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
224 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
225 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
226 double val = f (x, y.data (i)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
227 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
228 if (val != 0.0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
229 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
230 retval.data (ii) = val; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
231 retval.ridx (ii++) = y.ridx (i); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
232 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
233 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
234 retval.cidx (j + 1) = ii; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
235 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
236 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
237 retval.maybe_compress (false); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
238 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
239 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
240 return retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
241 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
242 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
243 static SparseMatrix |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
244 map_s_d (d_dd_fcn f, const SparseMatrix& x, double y) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
245 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
246 octave_idx_type nr = x.rows (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
247 octave_idx_type nc = x.columns (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
248 SparseMatrix retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
249 double f_zero = f (0., y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
250 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
251 if (f_zero != 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
252 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
253 retval = SparseMatrix (nr, nc, f_zero); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
254 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
255 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
256 for (octave_idx_type i = x.cidx (j); i < x.cidx (j+1); i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
257 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
258 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
259 retval.data (x.ridx(i) + j * nr) = f (x.data (i), y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
260 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
261 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
262 retval.maybe_compress (true); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
263 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
264 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
265 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
266 octave_idx_type nz = x.nnz (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
267 retval = SparseMatrix (nr, nc, nz); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
268 octave_idx_type ii = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
269 retval.cidx (ii) = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
270 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
271 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
272 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
273 for (octave_idx_type i = x.cidx (j); i < x.cidx (j+1); i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
274 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
275 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
276 double val = f (x.data (i), y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
277 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
278 if (val != 0.0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
279 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
280 retval.data (ii) = val; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
281 retval.ridx (ii++) = x.ridx (i); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
282 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
283 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
284 retval.cidx (j + 1) = ii; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
285 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
286 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
287 retval.maybe_compress (false); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
288 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
289 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
290 return retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
291 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
292 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
293 static SparseMatrix |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
294 map_s_s (d_dd_fcn f, const SparseMatrix& x, const SparseMatrix& y) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
295 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
296 octave_idx_type nr = x.rows (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
297 octave_idx_type nc = x.columns (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
298 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
299 octave_idx_type y_nr = y.rows (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
300 octave_idx_type y_nc = y.columns (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
301 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
302 assert (nr == y_nr && nc == y_nc); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
303 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
304 SparseMatrix retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
305 double f_zero = f (0., 0.); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
306 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
307 if (f_zero != 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
308 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
309 retval = SparseMatrix (nr, nc, f_zero); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
310 octave_idx_type k1 = 0, k2 = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
311 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
312 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
313 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
314 while (k1 < x.cidx(j+1) && k2 < y.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
315 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
316 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
317 if (k1 >= x.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
318 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
319 retval.data (y.ridx(k2) + j * nr) = f (0.0, y.data (k2)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
320 k2++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
321 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
322 else if (k2 >= y.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
323 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
324 retval.data (x.ridx(k1) + j * nr) = f (x.data (k1), 0.0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
325 k1++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
326 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
327 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
328 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
329 octave_idx_type rx = x.ridx(k1); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
330 octave_idx_type ry = y.ridx(k2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
331 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
332 if (rx < ry) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
333 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
334 retval.data (rx + j * nr) = f (x.data (k1), 0.0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
335 k1++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
336 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
337 else if (rx > ry) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
338 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
339 retval.data (ry + j * nr) = f (0.0, y.data (k2)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
340 k2++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
341 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
342 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
343 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
344 retval.data (ry + j * nr) = f (x.data (k1), y.data (k2)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
345 k1++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
346 k2++; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
347 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
348 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
349 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
350 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
351 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
352 retval.maybe_compress (true); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
353 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
354 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
355 { |
7631 | 356 octave_idx_type nz = x.nnz () + y.nnz (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
357 retval = SparseMatrix (nr, nc, nz); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
358 octave_idx_type ii = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
359 retval.cidx (ii) = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
360 octave_idx_type k1 = 0, k2 = 0; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
361 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
362 for (octave_idx_type j = 0; j < nc; j++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
363 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
364 while (k1 < x.cidx(j+1) && k2 < y.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
365 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
366 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
367 double val; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
368 octave_idx_type r; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
369 if (k1 >= x.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
370 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
371 r = y.ridx (k2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
372 val = f (0.0, y.data (k2++)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
373 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
374 else if (k2 >= y.cidx(j+1)) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
375 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
376 r = x.ridx (k1); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
377 val = f (x.data (k1++), 0.0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
378 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
379 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
380 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
381 octave_idx_type rx = x.ridx(k1); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
382 octave_idx_type ry = y.ridx(k2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
383 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
384 if (rx < ry) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
385 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
386 r = x.ridx (k1); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
387 val = f (x.data (k1++), 0.0); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
388 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
389 else if (rx > ry) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
390 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
391 r = y.ridx (k2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
392 val = f (0.0, y.data (k2++)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
393 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
394 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
395 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
396 r = y.ridx (k2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
397 val = f (x.data (k1++), y.data (k2++)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
398 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
399 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
400 if (val != 0.0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
401 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
402 retval.data (ii) = val; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
403 retval.ridx (ii++) = r; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
404 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
405 } |
7631 | 406 retval.cidx (j + 1) = ii; |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
407 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
408 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
409 retval.maybe_compress (false); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
410 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
411 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
412 return retval; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
413 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
414 |
1957 | 415 DEFUN (atan2, args, , |
3428 | 416 "-*- texinfo -*-\n\ |
417 @deftypefn {Mapping Function} {} atan2 (@var{y}, @var{x})\n\ | |
418 Compute atan (@var{y} / @var{x}) for corresponding elements of @var{y}\n\ | |
419 and @var{x}. The result is in range -pi to pi.\n\ | |
3439 | 420 @end deftypefn") |
649 | 421 { |
4233 | 422 octave_value retval; |
649 | 423 |
712 | 424 int nargin = args.length (); |
425 | |
426 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) | |
649 | 427 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
428 if (args(0).is_integer_type () || args(1).is_integer_type ()) |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
429 error ("atan2: not defined for integer types"); |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
430 else |
649 | 431 { |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
432 octave_value arg_y = args(0); |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
433 octave_value arg_x = args(1); |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
434 |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
435 dim_vector y_dims = arg_y.dims (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
436 dim_vector x_dims = arg_x.dims (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
437 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
438 bool y_is_scalar = y_dims.all_ones (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
439 bool x_is_scalar = x_dims.all_ones (); |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
440 |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
441 if (y_is_scalar && x_is_scalar) |
649 | 442 { |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
443 double y = arg_y.double_value (); |
649 | 444 |
445 if (! error_state) | |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
446 { |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
447 double x = arg_x.double_value (); |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
448 |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
449 if (! error_state) |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
450 retval = atan2 (y, x); |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
451 } |
649 | 452 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
453 else if (y_is_scalar) |
649 | 454 { |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
455 double y = arg_y.double_value (); |
649 | 456 |
457 if (! error_state) | |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
458 { |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
459 // Even if x is sparse return a full matrix here |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
460 NDArray x = arg_x.array_value (); |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
461 |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
462 if (! error_state) |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
463 retval = map_d_m (atan2, y, x); |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
464 } |
649 | 465 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
466 else if (x_is_scalar) |
649 | 467 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
468 if (arg_y.is_sparse_type ()) |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
469 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
470 SparseMatrix y = arg_y.sparse_matrix_value (); |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
471 |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
472 if (! error_state) |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
473 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
474 double x = arg_x.double_value (); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
475 |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
476 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
477 retval = map_s_d (atan2, y, x); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
478 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
479 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
480 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
481 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
482 NDArray y = arg_y.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
483 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
484 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
485 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
486 double x = arg_x.double_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
487 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
488 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
489 retval = map_m_d (atan2, y, x); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
490 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
491 } |
649 | 492 } |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
493 else if (y_dims == x_dims) |
649 | 494 { |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
495 // Even if y is sparse return a full matrix here |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
496 if (arg_x.is_sparse_type ()) |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
497 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
498 SparseMatrix y = arg_y.sparse_matrix_value (); |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
499 |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
500 if (! error_state) |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
501 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
502 SparseMatrix x = arg_x.sparse_matrix_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
503 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
504 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
505 retval = map_s_s (atan2, y, x); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
506 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
507 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
508 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
509 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
510 NDArray y = arg_y.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
511 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
512 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
513 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
514 NDArray x = arg_x.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
515 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
516 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
517 retval = map_m_m (atan2, y, x); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
518 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
519 } |
649 | 520 } |
7494
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
521 else |
bd2bd04e68ca
Treat integer types for mod/rem correctly
David Bateman <dbateman@free.fr>
parents:
7433
diff
changeset
|
522 error ("atan2: nonconformant matrices"); |
649 | 523 } |
524 } | |
712 | 525 else |
5823 | 526 print_usage (); |
649 | 527 |
528 return retval; | |
529 } | |
530 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
531 /* |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
532 %! assert (size (atan2 (zeros (0, 2), zeros (0, 2))), [0, 2]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
533 %! assert (size (atan2 (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
534 %! assert (size (atan2 (rand (2, 3, 4), 1), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
535 %! assert (size (atan2 (1, rand (2, 3, 4)), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
536 %! assert (size (atan2 (1, 2), [1, 1]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
537 */ |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
538 |
7631 | 539 DEFUN (hypot, args, , |
540 "-*- texinfo -*-\n\ | |
541 @deftypefn {Mapping Function} {} hypot (@var{x}, @var{y})\n\ | |
542 Compute square-root of the squares of @var{x} and @var{y}\n\ | |
543 element-by-element. This equivalent to @code{sqrt (@var{x}.^ 2 + @var{y}\n\ | |
544 .^ 2)}, but calculated in a manner that avoids overflows for large\n\ | |
545 values of @var{x} or @var{y}.\n\ | |
546 @end deftypefn") | |
547 { | |
548 octave_value retval; | |
549 | |
550 int nargin = args.length (); | |
551 | |
552 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) | |
553 { | |
554 if (args(0).is_integer_type () || args(1).is_integer_type ()) | |
555 error ("hypot: not defined for integer types"); | |
556 else | |
557 { | |
558 octave_value arg_x = args(0); | |
559 octave_value arg_y = args(1); | |
560 | |
561 dim_vector x_dims = arg_x.dims (); | |
562 dim_vector y_dims = arg_y.dims (); | |
563 | |
564 bool x_is_scalar = x_dims.all_ones (); | |
565 bool y_is_scalar = y_dims.all_ones (); | |
566 | |
567 if (y_is_scalar && x_is_scalar) | |
568 { | |
569 double x; | |
570 if (arg_x.is_complex_type ()) | |
571 x = abs (arg_x.complex_value ()); | |
572 else | |
573 x = arg_x.double_value (); | |
574 | |
575 if (! error_state) | |
576 { | |
577 double y; | |
578 if (arg_y.is_complex_type ()) | |
579 y = abs (arg_y.complex_value ()); | |
580 else | |
581 y = arg_y.double_value (); | |
582 | |
583 if (! error_state) | |
584 retval = hypot (x, y); | |
585 } | |
586 } | |
587 else if (y_is_scalar) | |
588 { | |
589 NDArray x; | |
590 if (arg_x.is_complex_type ()) | |
591 x = arg_x.complex_array_value ().abs (); | |
592 else | |
593 x = arg_x.array_value (); | |
594 | |
595 if (! error_state) | |
596 { | |
597 double y; | |
598 if (arg_y.is_complex_type ()) | |
599 y = abs (arg_y.complex_value ()); | |
600 else | |
601 y = arg_y.double_value (); | |
602 | |
603 if (! error_state) | |
604 retval = map_m_d (hypot, x, y); | |
605 } | |
606 } | |
607 else if (x_is_scalar) | |
608 { | |
609 double x; | |
610 if (arg_x.is_complex_type ()) | |
611 x = abs (arg_x.complex_value ()); | |
612 else | |
613 x = arg_x.double_value (); | |
614 | |
615 if (! error_state) | |
616 { | |
617 NDArray y; | |
618 if (arg_y.is_complex_type ()) | |
619 y = arg_y.complex_array_value ().abs (); | |
620 else | |
621 y = arg_y.array_value (); | |
622 | |
623 if (! error_state) | |
624 retval = map_d_m (hypot, x, y); | |
625 } | |
626 } | |
627 else if (y_dims == x_dims) | |
628 { | |
629 if (arg_x.is_sparse_type () && arg_y.is_sparse_type ()) | |
630 { | |
631 SparseMatrix x; | |
632 if (arg_x.is_complex_type ()) | |
633 x = arg_x.sparse_complex_matrix_value ().abs (); | |
634 else | |
635 x = arg_x.sparse_matrix_value (); | |
636 | |
637 if (! error_state) | |
638 { | |
639 SparseMatrix y; | |
640 if (arg_y.is_complex_type ()) | |
641 y = arg_y.sparse_complex_matrix_value ().abs (); | |
642 else | |
643 y = arg_y.sparse_matrix_value (); | |
644 | |
645 if (! error_state) | |
646 retval = map_s_s (hypot, x, y); | |
647 } | |
648 } | |
649 else | |
650 { | |
651 NDArray x; | |
652 if (arg_x.is_complex_type ()) | |
653 x = arg_x.complex_array_value ().abs (); | |
654 else | |
655 x = arg_x.array_value (); | |
656 | |
657 if (! error_state) | |
658 { | |
659 NDArray y; | |
660 if (arg_y.is_complex_type ()) | |
661 y = arg_y.complex_array_value ().abs (); | |
662 else | |
663 y = arg_y.array_value (); | |
664 | |
665 if (! error_state) | |
666 retval = map_m_m (hypot, x, y); | |
667 } | |
668 } | |
669 } | |
670 else | |
671 error ("hypot: nonconformant matrices"); | |
672 } | |
673 } | |
674 else | |
675 print_usage (); | |
676 | |
677 return retval; | |
678 } | |
679 | |
680 /* | |
681 %! assert (size (hypot (zeros (0, 2), zeros (0, 2))), [0, 2]) | |
682 %! assert (size (hypot (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) | |
683 %! assert (size (hypot (rand (2, 3, 4), 1), [2, 3, 4]) | |
684 %! assert (size (hypot (1, rand (2, 3, 4)), [2, 3, 4]) | |
685 %! assert (size (hypot (1, 2), [1, 1]) | |
686 %! assert (hypot (1:10,1:10), sqrt(2) * [1:10]); | |
687 */ | |
688 | |
7740 | 689 template<typename T, typename ET> |
690 void | |
691 map_2_xlog2 (const Array<T>& x, Array<T>& f, Array<ET>& e) | |
692 { | |
693 f = Array<T>(x.dims ()); | |
694 e = Array<ET>(x.dims ()); | |
695 for (octave_idx_type i = 0; i < x.numel (); i++) | |
696 { | |
697 int exp; | |
698 f.xelem (i) = xlog2 (x(i), exp); | |
699 e.xelem (i) = exp; | |
700 } | |
701 } | |
702 | |
703 DEFUN (log2, args, nargout, | |
704 "-*- texinfo -*-\n\ | |
705 @deftypefn {Mapping Function} {} log2 (@var{x})\n\ | |
706 @deftypefnx {Mapping Function} {[@var{f}, @var{e}] = } log2 (@var{x})\n\ | |
707 Compute the base-2 logarithm for each element of @var{x}.\n\ | |
708 If called with two output arguments, split @var{x} to\n\ | |
709 binary mantissa and exponent so that @code{1/2 <= abs(f) < 1} and\n\ | |
710 @var{e} is an integer. If @code{x = 0}, @code{f = e = 0}.\n\ | |
711 @seealso{log, log10, log2, exp}\n\ | |
712 @end deftypefn") | |
713 { | |
714 octave_value_list retval; | |
715 | |
716 if (args.length () == 1) | |
717 { | |
718 if (nargout < 2) | |
719 retval(0) = args(0).log2 (); | |
720 else if (args(0).is_real_type ()) | |
721 { | |
722 NDArray f; | |
723 NDArray x = args(0).array_value (); | |
724 // FIXME -- should E be an int value? | |
725 Matrix e; | |
726 map_2_xlog2 (x, f, e); | |
727 retval (1) = e; | |
728 retval (0) = f; | |
729 } | |
730 else if (args(0).is_complex_type ()) | |
731 { | |
732 ComplexNDArray f; | |
733 ComplexNDArray x = args(0).complex_array_value (); | |
734 // FIXME -- should E be an int value? | |
735 NDArray e; | |
736 map_2_xlog2 (x, f, e); | |
737 retval (1) = e; | |
738 retval (0) = f; | |
739 } | |
740 else | |
741 gripe_wrong_type_arg ("log2", args(0)); | |
742 } | |
743 else | |
744 print_usage (); | |
745 | |
746 return retval; | |
747 } | |
748 | |
749 /* | |
750 %! assert(log2 ([1/4, 1/2, 1, 2, 4]), [-2, -1, 0, 1, 2]); | |
751 %! assert(log2(Inf), Inf); | |
752 %! assert(isnan(log2(NaN))); | |
753 %! assert(log2(4*i), 2 + log2(1*i)); | |
754 %! assert(log2(complex(0,Inf)), Inf + log2(i)); | |
755 %!test | |
756 %! [f, e] = log2 ([0,-1; 2,-4; Inf,-Inf]); | |
757 %! assert (f, [0,-0.5; 0.5,-0.5; Inf,-Inf]); | |
758 %! assert (e, [0,1;2,3;0,0]) | |
759 %!test | |
760 %! [f, e] = log2 (complex (zeros (3, 2), [0,-1; 2,-4; Inf,-Inf])); | |
761 %! assert (f, complex (zeros (3, 2), [0,-0.5; 0.5,-0.5; Inf,-Inf])); | |
762 %! assert (e, [0,1; 2,3; 0,0]); | |
763 */ | |
764 | |
4311 | 765 DEFUN (fmod, args, , |
766 "-*- texinfo -*-\n\ | |
767 @deftypefn {Mapping Function} {} fmod (@var{x}, @var{y})\n\ | |
4685 | 768 Compute the floating point remainder of dividing @var{x} by @var{y}\n\ |
769 using the C library function @code{fmod}. The result has the same\n\ | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
770 sign as @var{x}. If @var{y} is zero, the result is implementation-defined.\n\ |
4311 | 771 @end deftypefn") |
772 { | |
773 octave_value retval; | |
774 | |
775 int nargin = args.length (); | |
776 | |
777 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) | |
778 { | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
779 octave_value arg_y = args(0); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
780 octave_value arg_x = args(1); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
781 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
782 dim_vector y_dims = arg_y.dims (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
783 dim_vector x_dims = arg_x.dims (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
784 |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
785 bool y_is_scalar = y_dims.all_ones (); |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
786 bool x_is_scalar = x_dims.all_ones (); |
4311 | 787 |
788 if (y_is_scalar && x_is_scalar) | |
789 { | |
790 double y = arg_y.double_value (); | |
791 | |
792 if (! error_state) | |
793 { | |
794 double x = arg_x.double_value (); | |
795 | |
796 if (! error_state) | |
797 retval = fmod (x, y); | |
798 } | |
799 } | |
800 else if (y_is_scalar) | |
801 { | |
802 double y = arg_y.double_value (); | |
803 | |
804 if (! error_state) | |
805 { | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
806 if (arg_x.is_sparse_type ()) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
807 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
808 SparseMatrix x = arg_x.sparse_matrix_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
809 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
810 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
811 retval = map_s_d (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
812 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
813 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
814 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
815 NDArray x = arg_x.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
816 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
817 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
818 retval = map_m_d (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
819 } |
4311 | 820 } |
821 } | |
822 else if (x_is_scalar) | |
823 { | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
824 if (arg_y.is_sparse_type ()) |
4311 | 825 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
826 SparseMatrix y = arg_y.sparse_matrix_value (); |
4311 | 827 |
828 if (! error_state) | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
829 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
830 double x = arg_x.double_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
831 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
832 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
833 retval = map_d_s (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
834 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
835 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
836 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
837 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
838 NDArray y = arg_y.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
839 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
840 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
841 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
842 double x = arg_x.double_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
843 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
844 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
845 retval = map_d_m (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
846 } |
4311 | 847 } |
848 } | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
849 else if (y_dims == x_dims) |
4311 | 850 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
851 if (arg_y.is_sparse_type () || arg_x.is_sparse_type ()) |
4311 | 852 { |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
853 SparseMatrix y = arg_y.sparse_matrix_value (); |
4311 | 854 |
855 if (! error_state) | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
856 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
857 SparseMatrix x = arg_x.sparse_matrix_value (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
858 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
859 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
860 retval = map_s_s (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
861 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
862 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
863 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
864 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
865 NDArray y = arg_y.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
866 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
867 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
868 { |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
869 NDArray x = arg_x.array_value (); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
870 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
871 if (! error_state) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
872 retval = map_m_m (fmod, x, y); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7494
diff
changeset
|
873 } |
4311 | 874 } |
875 } | |
876 else | |
877 error ("fmod: nonconformant matrices"); | |
878 } | |
879 else | |
5823 | 880 print_usage (); |
4311 | 881 |
882 return retval; | |
883 } | |
884 | |
7506
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
885 /* |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
886 %! assert (size (fmod (zeros (0, 2), zeros (0, 2))), [0, 2]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
887 %! assert (size (fmod (rand (2, 3, 4), zeros (2, 3, 4))), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
888 %! assert (size (fmod (rand (2, 3, 4), 1), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
889 %! assert (size (fmod (1, rand (2, 3, 4)), [2, 3, 4]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
890 %! assert (size (fmod (1, 2), [1, 1]) |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
891 */ |
798b0a00e80c
atan2, fmod: accept N-d arrays
John W. Eaton <jwe@octave.org>
parents:
7505
diff
changeset
|
892 |
7112 | 893 #define NATIVE_REDUCTION_1(FCN, TYPE, DIM) \ |
894 (arg.is_ ## TYPE ## _type ()) \ | |
895 { \ | |
896 TYPE ## NDArray tmp = arg. TYPE ##_array_value (); \ | |
897 \ | |
898 if (! error_state) \ | |
899 retval = tmp.FCN (DIM); \ | |
900 } | |
901 | |
902 #define NATIVE_REDUCTION(FCN) \ | |
903 \ | |
904 octave_value retval; \ | |
905 \ | |
906 int nargin = args.length (); \ | |
907 \ | |
908 bool isnative = false; \ | |
909 \ | |
910 if (nargin > 1 && args(nargin - 1).is_string ()) \ | |
911 { \ | |
912 std::string str = args(nargin - 1).string_value (); \ | |
913 \ | |
914 if (! error_state) \ | |
915 { \ | |
916 if (str == "native") \ | |
917 isnative = true; \ | |
918 else if (str != "double") /* Ignore double as no single type */ \ | |
919 error ("sum: unrecognized string argument"); \ | |
920 nargin --; \ | |
921 } \ | |
922 } \ | |
923 \ | |
924 if (nargin == 1 || nargin == 2) \ | |
925 { \ | |
926 octave_value arg = args(0); \ | |
927 \ | |
928 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ | |
929 \ | |
930 if (! error_state) \ | |
931 { \ | |
932 if (dim >= -1) \ | |
933 { \ | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
934 if (arg.is_sparse_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
935 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
936 if (arg.is_real_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
937 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
938 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
939 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
940 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
941 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
942 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
943 else \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
944 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
945 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
946 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
947 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
948 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
949 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
950 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
951 else \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
952 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
953 if (isnative) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
954 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
955 if NATIVE_REDUCTION_1 (FCN, uint8, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
956 else if NATIVE_REDUCTION_1 (FCN, uint16, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
957 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
|
958 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
|
959 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
|
960 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
|
961 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
|
962 else if NATIVE_REDUCTION_1 (FCN, int64, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
963 else if NATIVE_REDUCTION_1 (FCN, bool, dim) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
964 else if (arg.is_char_matrix ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
965 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
966 error (#FCN, ": invalid char type"); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
967 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
968 else if (arg.is_complex_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
969 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
970 ComplexNDArray tmp = arg.complex_array_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
971 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
972 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
973 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
974 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
975 else if (arg.is_real_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
976 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
977 NDArray tmp = arg.array_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
978 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
979 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
980 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
981 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
982 else \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
983 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
984 gripe_wrong_type_arg (#FCN, arg); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
985 return retval; \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
986 } \ |
7112 | 987 } \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
988 else if (arg.is_real_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
989 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
990 NDArray tmp = arg.array_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
991 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
992 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
993 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
994 } \ |
7112 | 995 else if (arg.is_complex_type ()) \ |
996 { \ | |
997 ComplexNDArray tmp = arg.complex_array_value (); \ | |
998 \ | |
999 if (! error_state) \ | |
1000 retval = tmp.FCN (dim); \ | |
1001 } \ | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1002 else \ |
7112 | 1003 { \ |
1004 gripe_wrong_type_arg (#FCN, arg); \ | |
1005 return retval; \ | |
1006 } \ | |
1007 } \ | |
1008 } \ | |
1009 else \ | |
1010 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | |
1011 } \ | |
1012 \ | |
1013 } \ | |
1014 else \ | |
1015 print_usage (); \ | |
1016 \ | |
1017 return retval | |
1018 | |
3723 | 1019 #define DATA_REDUCTION(FCN) \ |
1020 \ | |
4233 | 1021 octave_value retval; \ |
3723 | 1022 \ |
1023 int nargin = args.length (); \ | |
1024 \ | |
1025 if (nargin == 1 || nargin == 2) \ | |
1026 { \ | |
1027 octave_value arg = args(0); \ | |
1028 \ | |
3864 | 1029 int dim = (nargin == 1 ? -1 : args(1).int_value (true) - 1); \ |
3723 | 1030 \ |
1031 if (! error_state) \ | |
1032 { \ | |
4556 | 1033 if (dim >= -1) \ |
3723 | 1034 { \ |
1035 if (arg.is_real_type ()) \ | |
1036 { \ | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1037 if (arg.is_sparse_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1038 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1039 SparseMatrix tmp = arg.sparse_matrix_value (); \ |
3723 | 1040 \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1041 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1042 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1043 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1044 else \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1045 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1046 NDArray tmp = arg.array_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1047 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1048 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1049 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1050 } \ |
3723 | 1051 } \ |
1052 else if (arg.is_complex_type ()) \ | |
1053 { \ | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1054 if (arg.is_sparse_type ()) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1055 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1056 SparseComplexMatrix tmp = arg.sparse_complex_matrix_value (); \ |
3723 | 1057 \ |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1058 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1059 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1060 } \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1061 else \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1062 { \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1063 ComplexNDArray tmp = arg.complex_array_value (); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1064 \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1065 if (! error_state) \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1066 retval = tmp.FCN (dim); \ |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7506
diff
changeset
|
1067 } \ |
3723 | 1068 } \ |
1069 else \ | |
1070 { \ | |
1071 gripe_wrong_type_arg (#FCN, arg); \ | |
1072 return retval; \ | |
1073 } \ | |
1074 } \ | |
1075 else \ | |
1076 error (#FCN ": invalid dimension argument = %d", dim + 1); \ | |
1077 } \ | |
1078 } \ | |
1079 else \ | |
5823 | 1080 print_usage (); \ |
3723 | 1081 \ |
1082 return retval | |
1083 | |
1957 | 1084 DEFUN (cumprod, args, , |
3428 | 1085 "-*- texinfo -*-\n\ |
3723 | 1086 @deftypefn {Built-in Function} {} cumprod (@var{x}, @var{dim})\n\ |
1087 Cumulative product of elements along dimension @var{dim}. If\n\ | |
1088 @var{dim} is omitted, it defaults to 1 (column-wise cumulative\n\ | |
1089 products).\n\ | |
5061 | 1090 \n\ |
1091 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ | |
1092 return the cumulative product of the elements as a vector with the\n\ | |
1093 same orientation as @var{x}.\n\ | |
3428 | 1094 @end deftypefn") |
523 | 1095 { |
3723 | 1096 DATA_REDUCTION (cumprod); |
523 | 1097 } |
1098 | |
1957 | 1099 DEFUN (cumsum, args, , |
3428 | 1100 "-*- texinfo -*-\n\ |
3723 | 1101 @deftypefn {Built-in Function} {} cumsum (@var{x}, @var{dim})\n\ |
1102 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ | |
1103 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ | |
5061 | 1104 \n\ |
1105 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ | |
1106 return the cumulative sum of the elements as a vector with the\n\ | |
1107 same orientation as @var{x}.\n\ | |
3428 | 1108 @end deftypefn") |
523 | 1109 { |
3723 | 1110 DATA_REDUCTION (cumsum); |
523 | 1111 } |
1112 | |
1957 | 1113 DEFUN (diag, args, , |
3369 | 1114 "-*- texinfo -*-\n\ |
1115 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k})\n\ | |
1116 Return a diagonal matrix with vector @var{v} on diagonal @var{k}. The\n\ | |
1117 second argument is optional. If it is positive, the vector is placed on\n\ | |
1118 the @var{k}-th super-diagonal. If it is negative, it is placed on the\n\ | |
1119 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the\n\ | |
1120 vector is placed on the main diagonal. For example,\n\ | |
1121 \n\ | |
1122 @example\n\ | |
1123 @group\n\ | |
1124 diag ([1, 2, 3], 1)\n\ | |
1125 @result{} 0 1 0 0\n\ | |
1126 0 0 2 0\n\ | |
1127 0 0 0 3\n\ | |
1128 0 0 0 0\n\ | |
1129 @end group\n\ | |
1130 @end example\n\ | |
6772 | 1131 \n\ |
1132 @noindent\n\ | |
1133 Given a matrix argument, instead of a vector, @code{diag} extracts the\n\ | |
6774 | 1134 @var{k}-th diagonal of the matrix.\n\ |
3369 | 1135 @end deftypefn") |
523 | 1136 { |
4233 | 1137 octave_value retval; |
523 | 1138 |
1139 int nargin = args.length (); | |
1140 | |
712 | 1141 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
|
1142 retval = args(0).diag(); |
712 | 1143 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
|
1144 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1145 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
|
1146 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1147 if (error_state) |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1148 error ("diag: invalid second argument"); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1149 else |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1150 retval = args(0).diag(k); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7618
diff
changeset
|
1151 } |
523 | 1152 else |
5823 | 1153 print_usage (); |
523 | 1154 |
1155 return retval; | |
1156 } | |
1157 | |
1957 | 1158 DEFUN (prod, args, , |
3428 | 1159 "-*- texinfo -*-\n\ |
3723 | 1160 @deftypefn {Built-in Function} {} prod (@var{x}, @var{dim})\n\ |
1161 Product of elements along dimension @var{dim}. If @var{dim} is\n\ | |
1162 omitted, it defaults to 1 (column-wise products).\n\ | |
5061 | 1163 \n\ |
1164 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ | |
1165 return the product of the elements.\n\ | |
3428 | 1166 @end deftypefn") |
523 | 1167 { |
3723 | 1168 DATA_REDUCTION (prod); |
523 | 1169 } |
1170 | |
4824 | 1171 static octave_value |
1172 do_cat (const octave_value_list& args, std::string fname) | |
4806 | 1173 { |
1174 octave_value retval; | |
1175 | |
4824 | 1176 int n_args = args.length (); |
4806 | 1177 |
5714 | 1178 if (n_args == 1) |
1179 retval = Matrix (); | |
1180 else if (n_args == 2) | |
1181 retval = args(1); | |
5507 | 1182 else if (n_args > 2) |
4824 | 1183 { |
5275 | 1184 octave_idx_type dim = args(0).int_value () - 1; |
4806 | 1185 |
4824 | 1186 if (error_state) |
4806 | 1187 { |
4824 | 1188 error ("cat: expecting first argument to be a integer"); |
4806 | 1189 return retval; |
1190 } | |
1191 | |
4824 | 1192 if (dim >= 0) |
1193 { | |
4915 | 1194 |
1195 dim_vector dv = args(1).dims (); | |
4824 | 1196 |
4915 | 1197 for (int i = 2; i < args.length (); i++) |
1198 { | |
1199 // add_dims constructs a dimension vector which holds the | |
4824 | 1200 // dimensions of the final array after concatenation. |
4806 | 1201 |
4915 | 1202 if (! dv.concat (args(i).dims (), dim)) |
4806 | 1203 { |
4824 | 1204 // Dimensions do not match. |
4915 | 1205 error ("cat: dimension mismatch"); |
4806 | 1206 return retval; |
1207 } | |
4824 | 1208 } |
1209 | |
4915 | 1210 // The lines below might seem crazy, since we take a copy |
1211 // of the first argument, resize it to be empty and then resize | |
1212 // it to be full. This is done since it means that there is no | |
1213 // recopying of data, as would happen if we used a single resize. | |
1214 // It should be noted that resize operation is also significantly | |
1215 // slower than the do_cat_op function, so it makes sense to have an | |
1216 // empty matrix and copy all data. | |
4824 | 1217 // |
4915 | 1218 // We might also start with a empty octave_value using |
1219 // tmp = octave_value_typeinfo::lookup_type (args(1).type_name()); | |
1220 // and then directly resize. However, for some types there might be | |
1221 // some additional setup needed, and so this should be avoided. | |
5533 | 1222 |
4915 | 1223 octave_value tmp; |
5533 | 1224 |
6399 | 1225 int i; |
1226 for (i = 1; i < n_args; i++) | |
5533 | 1227 { |
1228 if (! args (i).all_zero_dims ()) | |
1229 { | |
1230 tmp = args (i); | |
1231 break; | |
1232 } | |
1233 } | |
5164 | 1234 |
6401 | 1235 if (i == n_args) |
1236 retval = Matrix (); | |
1237 else | |
4915 | 1238 { |
6401 | 1239 tmp = tmp.resize (dim_vector (0,0)).resize (dv); |
4824 | 1240 |
4915 | 1241 if (error_state) |
1242 return retval; | |
4806 | 1243 |
6883 | 1244 int dv_len = dv.length (); |
1245 Array<octave_idx_type> ra_idx (dv_len, 0); | |
6401 | 1246 |
1247 for (int j = i; j < n_args; j++) | |
1248 { | |
6887 | 1249 if (args (j). dims (). any_zero ()) |
6883 | 1250 continue; |
1251 | |
6401 | 1252 tmp = do_cat_op (tmp, args (j), ra_idx); |
1253 | |
1254 if (error_state) | |
1255 return retval; | |
1256 | |
1257 dim_vector dv_tmp = args (j).dims (); | |
1258 | |
6883 | 1259 if (dim >= dv_len) |
1260 { | |
1261 if (j > i) | |
1262 error ("%s: indexing error", fname.c_str ()); | |
1263 break; | |
1264 } | |
1265 else | |
1266 ra_idx (dim) += (dim < dv_tmp.length () ? | |
1267 dv_tmp (dim) : 1); | |
6401 | 1268 } |
1269 | |
1270 retval = tmp; | |
4915 | 1271 } |
4806 | 1272 } |
5533 | 1273 else |
1274 error ("%s: invalid dimension argument", fname.c_str ()); | |
4806 | 1275 } |
1276 else | |
5823 | 1277 print_usage (); |
4806 | 1278 |
1279 return retval; | |
1280 } | |
1281 | |
1282 DEFUN (horzcat, args, , | |
4824 | 1283 "-*- texinfo -*-\n\ |
4806 | 1284 @deftypefn {Built-in Function} {} horzcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ |
1285 Return the horizontal concatenation of N-d array objects, @var{array1},\n\ | |
1286 @var{array2}, @dots{}, @var{arrayN} along dimension 2.\n\ | |
5642 | 1287 @seealso{cat, vertcat}\n\ |
1288 @end deftypefn") | |
4806 | 1289 { |
1290 octave_value_list args_tmp = args; | |
1291 | |
1292 int dim = 2; | |
1293 | |
1294 octave_value d (dim); | |
1295 | |
1296 args_tmp.prepend (d); | |
1297 | |
4824 | 1298 return do_cat (args_tmp, "horzcat"); |
4806 | 1299 } |
1300 | |
1301 DEFUN (vertcat, args, , | |
1302 "-*- texinfo -*-\n\ | |
1303 @deftypefn {Built-in Function} {} vertcat (@var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ | |
1304 Return the vertical concatenation of N-d array objects, @var{array1},\n\ | |
1305 @var{array2}, @dots{}, @var{arrayN} along dimension 1.\n\ | |
5642 | 1306 @seealso{cat, horzcat}\n\ |
1307 @end deftypefn") | |
4806 | 1308 { |
1309 octave_value_list args_tmp = args; | |
1310 | |
1311 int dim = 1; | |
1312 | |
1313 octave_value d (dim); | |
1314 | |
1315 args_tmp.prepend (d); | |
1316 | |
4824 | 1317 return do_cat (args_tmp, "vertcat"); |
4806 | 1318 } |
1319 | |
4758 | 1320 DEFUN (cat, args, , |
1321 "-*- texinfo -*-\n\ | |
1322 @deftypefn {Built-in Function} {} cat (@var{dim}, @var{array1}, @var{array2}, @dots{}, @var{arrayN})\n\ | |
4806 | 1323 Return the concatenation of N-d array objects, @var{array1},\n\ |
1324 @var{array2}, @dots{}, @var{arrayN} along dimension @var{dim}.\n\ | |
4758 | 1325 \n\ |
1326 @example\n\ | |
1327 @group\n\ | |
1328 A = ones (2, 2);\n\ | |
1329 B = zeros (2, 2);\n\ | |
1330 cat (2, A, B)\n\ | |
1331 @result{} ans =\n\ | |
1332 \n\ | |
1333 1 1 0 0\n\ | |
1334 1 1 0 0\n\ | |
1335 @end group\n\ | |
1336 @end example\n\ | |
1337 \n\ | |
1338 Alternatively, we can concatenate @var{A} and @var{B} along the\n\ | |
1339 second dimension the following way:\n\ | |
1340 \n\ | |
1341 @example\n\ | |
1342 @group\n\ | |
1343 [A, B].\n\ | |
1344 @end group\n\ | |
1345 @end example\n\ | |
1346 \n\ | |
1347 @var{dim} can be larger than the dimensions of the N-d array objects\n\ | |
1348 and the result will thus have @var{dim} dimensions as the\n\ | |
1349 following example shows:\n\ | |
1350 @example\n\ | |
1351 @group\n\ | |
1352 cat (4, ones(2, 2), zeros (2, 2))\n\ | |
1353 @result{} ans =\n\ | |
1354 \n\ | |
1355 ans(:,:,1,1) =\n\ | |
1356 \n\ | |
1357 1 1\n\ | |
1358 1 1\n\ | |
1359 \n\ | |
1360 ans(:,:,1,2) =\n\ | |
1361 0 0\n\ | |
1362 0 0\n\ | |
1363 @end group\n\ | |
1364 @end example\n\ | |
5642 | 1365 @seealso{horzcat, vertcat}\n\ |
1366 @end deftypefn") | |
4758 | 1367 { |
4824 | 1368 return do_cat (args, "cat"); |
4758 | 1369 } |
1370 | |
4593 | 1371 static octave_value |
6959 | 1372 do_permute (const octave_value_list& args, bool inv) |
4593 | 1373 { |
1374 octave_value retval; | |
1375 | |
5148 | 1376 if (args.length () == 2 && args(1).length () >= args(1).ndims ()) |
4593 | 1377 { |
1378 Array<int> vec = args(1).int_vector_value (); | |
1379 | |
5775 | 1380 // FIXME -- maybe we should create an idx_vector object |
5148 | 1381 // here and pass that to permute? |
1382 | |
1383 int n = vec.length (); | |
1384 | |
1385 for (int i = 0; i < n; i++) | |
1386 vec(i)--; | |
1387 | |
4593 | 1388 octave_value ret = args(0).permute (vec, inv); |
1389 | |
1390 if (! error_state) | |
1391 retval = ret; | |
1392 } | |
1393 else | |
5823 | 1394 print_usage (); |
4593 | 1395 |
1396 return retval; | |
1397 } | |
1398 | |
1399 DEFUN (permute, args, , | |
1400 "-*- texinfo -*-\n\ | |
1401 @deftypefn {Built-in Function} {} permute (@var{a}, @var{perm})\n\ | |
1402 Return the generalized transpose for an N-d array object @var{a}.\n\ | |
1403 The permutation vector @var{perm} must contain the elements\n\ | |
1404 @code{1:ndims(a)} (in any order, but each element must appear just once).\n\ | |
5642 | 1405 @seealso{ipermute}\n\ |
1406 @end deftypefn") | |
4593 | 1407 { |
6959 | 1408 return do_permute (args, false); |
4593 | 1409 } |
1410 | |
1411 DEFUN (ipermute, args, , | |
1412 "-*- texinfo -*-\n\ | |
1413 @deftypefn {Built-in Function} {} ipermute (@var{a}, @var{iperm})\n\ | |
1414 The inverse of the @code{permute} function. The expression\n\ | |
1415 \n\ | |
1416 @example\n\ | |
1417 ipermute (permute (a, perm), perm)\n\ | |
1418 @end example\n\ | |
1419 returns the original array @var{a}.\n\ | |
5642 | 1420 @seealso{permute}\n\ |
1421 @end deftypefn") | |
4593 | 1422 { |
6959 | 1423 return do_permute (args, true); |
4593 | 1424 } |
1425 | |
3195 | 1426 DEFUN (length, args, , |
3373 | 1427 "-*- texinfo -*-\n\ |
1428 @deftypefn {Built-in Function} {} length (@var{a})\n\ | |
4176 | 1429 Return the `length' of the object @var{a}. For matrix objects, the\n\ |
3373 | 1430 length is the number of rows or columns, whichever is greater (this\n\ |
6556 | 1431 odd definition is used for compatibility with @sc{Matlab}).\n\ |
3373 | 1432 @end deftypefn") |
3195 | 1433 { |
1434 octave_value retval; | |
1435 | |
1436 if (args.length () == 1) | |
1437 { | |
1438 int len = args(0).length (); | |
1439 | |
1440 if (! error_state) | |
4233 | 1441 retval = len; |
3195 | 1442 } |
1443 else | |
5823 | 1444 print_usage (); |
3195 | 1445 |
1446 return retval; | |
1447 } | |
1448 | |
4554 | 1449 DEFUN (ndims, args, , |
1450 "-*- texinfo -*-\n\ | |
1451 @deftypefn {Built-in Function} {} ndims (@var{a})\n\ | |
1452 Returns the number of dimensions of array @var{a}.\n\ | |
1453 For any array, the result will always be larger than or equal to 2.\n\ | |
1454 Trailing singleton dimensions are not counted.\n\ | |
1455 @end deftypefn") | |
1456 { | |
1457 octave_value retval; | |
1458 | |
1459 if (args.length () == 1) | |
1460 { | |
1461 int n_dims = args(0).ndims (); | |
1462 | |
1463 if (! error_state) | |
1464 retval = n_dims; | |
1465 } | |
1466 else | |
5823 | 1467 print_usage (); |
4554 | 1468 |
1469 return retval; | |
1470 } | |
1471 | |
4559 | 1472 DEFUN (numel, args, , |
1473 "-*- texinfo -*-\n\ | |
1474 @deftypefn {Built-in Function} {} numel (@var{a})\n\ | |
1475 Returns the number of elements in the object @var{a}.\n\ | |
5724 | 1476 @seealso{size}\n\ |
4559 | 1477 @end deftypefn") |
1478 { | |
1479 octave_value retval; | |
1480 | |
1481 if (args.length () == 1) | |
1482 { | |
1483 int numel = args(0).numel (); | |
1484 | |
1485 if (! error_state) | |
1486 { | |
1487 if (numel < 0) | |
1488 numel = 0; | |
1489 | |
1490 retval = numel; | |
1491 } | |
1492 } | |
1493 else | |
5823 | 1494 print_usage (); |
4559 | 1495 |
1496 return retval; | |
1497 } | |
1498 | |
1957 | 1499 DEFUN (size, args, nargout, |
3373 | 1500 "-*- texinfo -*-\n\ |
1501 @deftypefn {Built-in Function} {} size (@var{a}, @var{n})\n\ | |
1502 Return the number rows and columns of @var{a}.\n\ | |
1503 \n\ | |
1504 With one input argument and one output argument, the result is returned\n\ | |
4741 | 1505 in a row vector. If there are multiple output arguments, the number of\n\ |
1506 rows is assigned to the first, and the number of columns to the second,\n\ | |
1507 etc. For example,\n\ | |
3373 | 1508 \n\ |
1509 @example\n\ | |
1510 @group\n\ | |
1511 size ([1, 2; 3, 4; 5, 6])\n\ | |
1512 @result{} [ 3, 2 ]\n\ | |
1031 | 1513 \n\ |
3373 | 1514 [nr, nc] = size ([1, 2; 3, 4; 5, 6])\n\ |
1515 @result{} nr = 3\n\ | |
1516 @result{} nc = 2\n\ | |
1517 @end group\n\ | |
1518 @end example\n\ | |
1519 \n\ | |
4741 | 1520 If given a second argument, @code{size} will return the size of the\n\ |
1521 corresponding dimension. For example\n\ | |
1031 | 1522 \n\ |
3373 | 1523 @example\n\ |
1524 size ([1, 2; 3, 4; 5, 6], 2)\n\ | |
1525 @result{} 2\n\ | |
1526 @end example\n\ | |
1527 \n\ | |
1528 @noindent\n\ | |
1529 returns the number of columns in the given matrix.\n\ | |
5724 | 1530 @seealso{numel}\n\ |
3373 | 1531 @end deftypefn") |
523 | 1532 { |
2086 | 1533 octave_value_list retval; |
523 | 1534 |
1535 int nargin = args.length (); | |
1536 | |
4513 | 1537 if (nargin == 1) |
523 | 1538 { |
4513 | 1539 dim_vector dimensions = args(0).dims (); |
1540 | |
1541 int ndims = dimensions.length (); | |
1031 | 1542 |
4513 | 1543 Matrix m (1, ndims); |
1544 | |
1545 if (nargout > 1) | |
523 | 1546 { |
5991 | 1547 for (int i = nargout-1; i >= ndims; i--) |
1548 retval(i) = 1; | |
1549 | |
6197 | 1550 if (ndims > nargout) |
1551 { | |
1552 octave_idx_type d = 1; | |
1553 | |
1554 while (ndims >= nargout) | |
1555 d *= dimensions(--ndims); | |
1556 | |
1557 retval(ndims) = d; | |
1558 } | |
1559 | |
4513 | 1560 while (ndims--) |
1561 retval(ndims) = dimensions(ndims); | |
523 | 1562 } |
4513 | 1563 else |
712 | 1564 { |
4513 | 1565 for (int i = 0; i < ndims; i++) |
1566 m(0, i) = dimensions(i); | |
1567 | |
1568 retval(0) = m; | |
712 | 1569 } |
1031 | 1570 } |
1571 else if (nargin == 2 && nargout < 2) | |
1572 { | |
5275 | 1573 octave_idx_type nd = args(1).int_value (true); |
1031 | 1574 |
1575 if (error_state) | |
1576 error ("size: expecting scalar as second argument"); | |
712 | 1577 else |
1031 | 1578 { |
4741 | 1579 dim_vector dv = args(0).dims (); |
1580 | |
4911 | 1581 if (nd > 0) |
1582 { | |
1583 if (nd <= dv.length ()) | |
1584 retval(0) = dv(nd-1); | |
1585 else | |
1586 retval(0) = 1; | |
1587 } | |
1031 | 1588 else |
4741 | 1589 error ("size: requested dimension (= %d) out of range", nd); |
1031 | 1590 } |
523 | 1591 } |
712 | 1592 else |
5823 | 1593 print_usage (); |
523 | 1594 |
1595 return retval; | |
1596 } | |
1597 | |
6156 | 1598 DEFUN (size_equal, args, , |
1599 "-*- texinfo -*-\n\ | |
6561 | 1600 @deftypefn {Built-in Function} {} size_equal (@var{a}, @var{b}, @dots{})\n\ |
1601 Return true if the dimensions of all arguments agree.\n\ | |
6156 | 1602 Trailing singleton dimensions are ignored.\n\ |
1603 @seealso{size, numel}\n\ | |
1604 @end deftypefn") | |
1605 { | |
1606 octave_value retval; | |
1607 | |
6561 | 1608 int nargin = args.length (); |
1609 | |
1610 if (nargin >= 2) | |
6156 | 1611 { |
6561 | 1612 retval = true; |
1613 | |
6156 | 1614 dim_vector a_dims = args(0).dims (); |
1615 a_dims.chop_trailing_singletons (); | |
6561 | 1616 |
1617 for (int i = 1; i < nargin; ++i) | |
1618 { | |
1619 dim_vector b_dims = args(i).dims (); | |
1620 b_dims.chop_trailing_singletons (); | |
1621 | |
1622 if (a_dims != b_dims) | |
1623 { | |
1624 retval = false; | |
1625 break; | |
1626 } | |
1627 } | |
6156 | 1628 } |
1629 else | |
1630 print_usage (); | |
1631 | |
1632 return retval; | |
1633 } | |
1634 | |
5602 | 1635 DEFUN (nnz, args, , |
1636 "-*- texinfo -*-\n\ | |
6156 | 1637 @deftypefn {Built-in Function} {@var{scalar} =} nnz (@var{a})\n\ |
1638 Returns the number of non zero elements in @var{a}.\n\ | |
5602 | 1639 @seealso{sparse}\n\ |
1640 @end deftypefn") | |
1641 { | |
1642 octave_value retval; | |
1643 | |
1644 if (args.length () == 1) | |
1645 retval = args(0).nnz (); | |
1646 else | |
5823 | 1647 print_usage (); |
5602 | 1648 |
1649 return retval; | |
1650 } | |
1651 | |
5604 | 1652 DEFUN (nzmax, args, , |
1653 "-*- texinfo -*-\n\ | |
6156 | 1654 @deftypefn {Built-in Function} {@var{scalar} =} nzmax (@var{SM})\n\ |
5604 | 1655 Return the amount of storage allocated to the sparse matrix @var{SM}.\n\ |
7001 | 1656 Note that Octave tends to crop unused memory at the first opportunity\n\ |
5604 | 1657 for sparse objects. There are some cases of user created sparse objects\n\ |
1658 where the value returned by @dfn{nzmaz} will not be the same as @dfn{nnz},\n\ | |
1659 but in general they will give the same result.\n\ | |
1660 @seealso{sparse, spalloc}\n\ | |
1661 @end deftypefn") | |
1662 { | |
1663 octave_value retval; | |
1664 | |
1665 if (args.length() == 1) | |
1666 retval = args(0).nzmax (); | |
1667 else | |
5823 | 1668 print_usage (); |
5604 | 1669 |
1670 return retval; | |
1671 } | |
1672 | |
5677 | 1673 DEFUN (rows, args, , |
1674 "-*- texinfo -*-\n\ | |
1675 @deftypefn {Built-in Function} {} rows (@var{a})\n\ | |
1676 Return the number of rows of @var{a}.\n\ | |
5724 | 1677 @seealso{size, numel, columns, length, isscalar, isvector, ismatrix}\n\ |
5677 | 1678 @end deftypefn") |
1679 { | |
1680 octave_value retval; | |
1681 | |
1682 if (args.length () == 1) | |
1683 retval = args(0).rows (); | |
1684 else | |
5823 | 1685 print_usage (); |
5677 | 1686 |
1687 return retval; | |
1688 } | |
1689 | |
1690 DEFUN (columns, args, , | |
1691 "-*- texinfo -*-\n\ | |
1692 @deftypefn {Built-in Function} {} columns (@var{a})\n\ | |
1693 Return the number of columns of @var{a}.\n\ | |
5724 | 1694 @seealso{size, numel, rows, length, isscalar, isvector, and ismatrix}\n\ |
5677 | 1695 @end deftypefn") |
1696 { | |
1697 octave_value retval; | |
1698 | |
1699 if (args.length () == 1) | |
1700 retval = args(0).columns (); | |
1701 else | |
5823 | 1702 print_usage (); |
5677 | 1703 |
1704 return retval; | |
1705 } | |
1706 | |
1957 | 1707 DEFUN (sum, args, , |
3428 | 1708 "-*- texinfo -*-\n\ |
3723 | 1709 @deftypefn {Built-in Function} {} sum (@var{x}, @var{dim})\n\ |
7112 | 1710 @deftypefnx {Built-in Function} {} sum (@dots{}, 'native')\n\ |
3723 | 1711 Sum of elements along dimension @var{dim}. If @var{dim} is\n\ |
1712 omitted, it defaults to 1 (column-wise sum).\n\ | |
5061 | 1713 \n\ |
1714 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ | |
1715 return the sum of the elements.\n\ | |
7112 | 1716 \n\ |
1717 If the optional argument 'native' is given, then the sum is performed\n\ | |
1718 in the same type as the original argument, rather than in the default\n\ | |
1719 double type. For example\n\ | |
1720 \n\ | |
1721 @example\n\ | |
1722 sum ([true, true])\n\ | |
1723 @result{} 2\n\ | |
1724 sum ([true, true], 'native')\n\ | |
1725 @result{} true\n\ | |
1726 @end example\n\ | |
3428 | 1727 @end deftypefn") |
523 | 1728 { |
7112 | 1729 NATIVE_REDUCTION (sum); |
523 | 1730 } |
1731 | |
7112 | 1732 /* |
1733 | |
1734 %!assert (sum([true,true]), 2) | |
1735 %!assert (sum([true,true],'native'), true) | |
1736 %!assert (sum(int8([127,10,-20])), 117); | |
1737 %!assert (sum(int8([127,10,-20]),'native'), int8(107)); | |
1738 | |
1739 */ | |
1740 | |
1957 | 1741 DEFUN (sumsq, args, , |
3428 | 1742 "-*- texinfo -*-\n\ |
3723 | 1743 @deftypefn {Built-in Function} {} sumsq (@var{x}, @var{dim})\n\ |
1744 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ | |
1745 is omitted, it defaults to 1 (column-wise sum of squares).\n\ | |
3095 | 1746 \n\ |
5061 | 1747 As a special case, if @var{x} is a vector and @var{dim} is omitted,\n\ |
1748 return the sum of squares of the elements.\n\ | |
1749 \n\ | |
1750 This function is conceptually equivalent to computing\n\ | |
3723 | 1751 @example\n\ |
1752 sum (x .* conj (x), dim)\n\ | |
1753 @end example\n\ | |
1754 but it uses less memory and avoids calling conj if @var{x} is real.\n\ | |
3428 | 1755 @end deftypefn") |
523 | 1756 { |
3723 | 1757 DATA_REDUCTION (sumsq); |
523 | 1758 } |
1759 | |
6688 | 1760 DEFUN (islogical, args, , |
3428 | 1761 "-*- texinfo -*-\n\ |
7144 | 1762 @deftypefn {Built-in Function} {} islogical (@var{x})\n\ |
6688 | 1763 Return true if @var{x} is a logical object.\n\ |
3439 | 1764 @end deftypefn") |
3209 | 1765 { |
1766 octave_value retval; | |
1767 | |
1768 if (args.length () == 1) | |
3258 | 1769 retval = args(0).is_bool_type (); |
3209 | 1770 else |
5823 | 1771 print_usage (); |
3209 | 1772 |
1773 return retval; | |
1774 } | |
1775 | |
6688 | 1776 DEFALIAS (isbool, islogical); |
3209 | 1777 |
6223 | 1778 DEFUN (isinteger, args, , |
1779 "-*- texinfo -*-\n\ | |
6230 | 1780 @deftypefn {Built-in Function} {} isinteger (@var{x})\n\ |
6223 | 1781 Return true if @var{x} is an integer object (int8, uint8, int16, etc.).\n\ |
1782 Note that @code{isinteger (14)} is false because numeric constants in\n\ | |
1783 are double precision floating point values.\n\ | |
1784 @seealso{isreal, isnumeric, class, isa}\n\ | |
1785 @end deftypefn") | |
1786 { | |
1787 octave_value retval; | |
1788 | |
1789 if (args.length () == 1) | |
1790 retval = args(0).is_integer_type (); | |
1791 else | |
1792 print_usage (); | |
1793 | |
1794 return retval; | |
1795 } | |
1796 | |
4028 | 1797 DEFUN (iscomplex, args, , |
3428 | 1798 "-*- texinfo -*-\n\ |
4028 | 1799 @deftypefn {Built-in Function} {} iscomplex (@var{x})\n\ |
3428 | 1800 Return true if @var{x} is a complex-valued numeric object.\n\ |
1801 @end deftypefn") | |
3186 | 1802 { |
1803 octave_value retval; | |
1804 | |
1805 if (args.length () == 1) | |
3258 | 1806 retval = args(0).is_complex_type (); |
3186 | 1807 else |
5823 | 1808 print_usage (); |
3186 | 1809 |
1810 return retval; | |
1811 } | |
1812 | |
7576 | 1813 DEFUN (isfloat, args, , |
1814 "-*- texinfo -*-\n\ | |
1815 @deftypefn {Built-in Function} {} isfloat (@var{x})\n\ | |
1816 Return true if @var{x} is a floating-point numeric object.\n\ | |
1817 @end deftypefn") | |
1818 { | |
1819 octave_value retval; | |
1820 | |
1821 if (args.length () == 1) | |
1822 retval = args(0).is_float_type (); | |
1823 else | |
1824 print_usage (); | |
1825 | |
1826 return retval; | |
1827 } | |
1828 | |
5775 | 1829 // FIXME -- perhaps this should be implemented with an |
5476 | 1830 // octave_value member function? |
1831 | |
1832 DEFUN (complex, args, , | |
1833 "-*- texinfo -*-\n\ | |
1834 @deftypefn {Built-in Function} {} complex (@var{val})\n\ | |
1835 @deftypefnx {Built-in Function} {} complex (@var{re}, @var{im})\n\ | |
1836 Convert @var{x} to a complex value.\n\ | |
1837 @end deftypefn") | |
1838 { | |
1839 octave_value retval; | |
1840 | |
1841 int nargin = args.length (); | |
1842 | |
1843 if (nargin == 1) | |
1844 { | |
1845 octave_value arg = args(0); | |
1846 | |
1847 if (arg.is_complex_type ()) | |
1848 retval = arg; | |
1849 else | |
1850 { | |
1851 if (arg.numel () == 1) | |
1852 { | |
1853 Complex val = arg.complex_value (); | |
1854 | |
1855 if (! error_state) | |
1856 retval = octave_value (new octave_complex (val)); | |
1857 } | |
1858 else | |
1859 { | |
1860 ComplexNDArray val = arg.complex_array_value (); | |
1861 | |
1862 if (! error_state) | |
1863 retval = octave_value (new octave_complex_matrix (val)); | |
1864 } | |
1865 | |
1866 if (error_state) | |
1867 error ("complex: invalid conversion"); | |
1868 } | |
1869 } | |
1870 else if (nargin == 2) | |
1871 { | |
1872 octave_value re = args(0); | |
1873 octave_value im = args(1); | |
1874 | |
1875 if (re.numel () == 1) | |
1876 { | |
1877 double re_val = re.double_value (); | |
1878 | |
1879 if (im.numel () == 1) | |
1880 { | |
1881 double im_val = im.double_value (); | |
1882 | |
1883 if (! error_state) | |
1884 retval = octave_value (new octave_complex (Complex (re_val, im_val))); | |
1885 } | |
1886 else | |
1887 { | |
1888 const NDArray im_val = im.array_value (); | |
1889 | |
1890 if (! error_state) | |
1891 { | |
1892 ComplexNDArray result (im_val.dims (), Complex ()); | |
1893 | |
1894 for (octave_idx_type i = 0; i < im_val.numel (); i++) | |
1895 result.xelem (i) = Complex (re_val, im_val(i)); | |
1896 | |
1897 retval = octave_value (new octave_complex_matrix (result)); | |
1898 } | |
1899 } | |
1900 } | |
1901 else | |
1902 { | |
1903 const NDArray re_val = re.array_value (); | |
1904 | |
1905 if (im.numel () == 1) | |
1906 { | |
1907 double im_val = im.double_value (); | |
1908 | |
1909 if (! error_state) | |
1910 { | |
1911 ComplexNDArray result (re_val.dims (), Complex ()); | |
1912 | |
1913 for (octave_idx_type i = 0; i < re_val.numel (); i++) | |
1914 result.xelem (i) = Complex (re_val(i), im_val); | |
1915 | |
1916 retval = octave_value (new octave_complex_matrix (result)); | |
1917 } | |
1918 } | |
1919 else | |
1920 { | |
1921 const NDArray im_val = im.array_value (); | |
1922 | |
1923 if (! error_state) | |
1924 { | |
1925 if (re_val.dims () == im_val.dims ()) | |
1926 { | |
1927 ComplexNDArray result (re_val.dims (), Complex ()); | |
1928 | |
1929 for (octave_idx_type i = 0; i < re_val.numel (); i++) | |
1930 result.xelem (i) = Complex (re_val(i), im_val(i)); | |
1931 | |
1932 retval = octave_value (new octave_complex_matrix (result)); | |
1933 } | |
1934 else | |
1935 error ("complex: dimension mismatch"); | |
1936 } | |
1937 } | |
1938 } | |
1939 | |
1940 if (error_state) | |
1941 error ("complex: invalid conversion"); | |
1942 } | |
1943 else | |
5823 | 1944 print_usage (); |
5476 | 1945 |
1946 return retval; | |
1947 } | |
1948 | |
3258 | 1949 DEFUN (isreal, args, , |
3428 | 1950 "-*- texinfo -*-\n\ |
1951 @deftypefn {Built-in Function} {} isreal (@var{x})\n\ | |
1952 Return true if @var{x} is a real-valued numeric object.\n\ | |
1953 @end deftypefn") | |
3258 | 1954 { |
1955 octave_value retval; | |
1956 | |
1957 if (args.length () == 1) | |
1958 retval = args(0).is_real_type (); | |
1959 else | |
5823 | 1960 print_usage (); |
3258 | 1961 |
1962 return retval; | |
1963 } | |
1964 | |
3202 | 1965 DEFUN (isempty, args, , |
3373 | 1966 "-*- texinfo -*-\n\ |
1967 @deftypefn {Built-in Function} {} isempty (@var{a})\n\ | |
1968 Return 1 if @var{a} is an empty matrix (either the number of rows, or\n\ | |
1969 the number of columns, or both are zero). Otherwise, return 0.\n\ | |
1970 @end deftypefn") | |
3202 | 1971 { |
4233 | 1972 octave_value retval = false; |
3202 | 1973 |
1974 if (args.length () == 1) | |
4559 | 1975 retval = args(0).is_empty (); |
3202 | 1976 else |
5823 | 1977 print_usage (); |
3202 | 1978 |
1979 return retval; | |
1980 } | |
1981 | |
3206 | 1982 DEFUN (isnumeric, args, , |
3428 | 1983 "-*- texinfo -*-\n\ |
1984 @deftypefn {Built-in Function} {} isnumeric (@var{x})\n\ | |
1985 Return nonzero if @var{x} is a numeric object.\n\ | |
1986 @end deftypefn") | |
3206 | 1987 { |
1988 octave_value retval; | |
1989 | |
1990 if (args.length () == 1) | |
3258 | 1991 retval = args(0).is_numeric_type (); |
3206 | 1992 else |
5823 | 1993 print_usage (); |
3206 | 1994 |
1995 return retval; | |
1996 } | |
1997 | |
4028 | 1998 DEFUN (islist, args, , |
3526 | 1999 "-*- texinfo -*-\n\ |
4028 | 2000 @deftypefn {Built-in Function} {} islist (@var{x})\n\ |
3428 | 2001 Return nonzero if @var{x} is a list.\n\ |
2002 @end deftypefn") | |
3204 | 2003 { |
2004 octave_value retval; | |
2005 | |
2006 if (args.length () == 1) | |
3258 | 2007 retval = args(0).is_list (); |
3204 | 2008 else |
5823 | 2009 print_usage (); |
3204 | 2010 |
2011 return retval; | |
2012 } | |
2013 | |
4028 | 2014 DEFUN (ismatrix, args, , |
3321 | 2015 "-*- texinfo -*-\n\ |
4028 | 2016 @deftypefn {Built-in Function} {} ismatrix (@var{a})\n\ |
3321 | 2017 Return 1 if @var{a} is a matrix. Otherwise, return 0.\n\ |
3333 | 2018 @end deftypefn") |
3202 | 2019 { |
4233 | 2020 octave_value retval = false; |
3202 | 2021 |
2022 if (args.length () == 1) | |
2023 { | |
2024 octave_value arg = args(0); | |
2025 | |
3212 | 2026 if (arg.is_scalar_type () || arg.is_range ()) |
4233 | 2027 retval = true; |
3202 | 2028 else if (arg.is_matrix_type ()) |
4233 | 2029 retval = (arg.rows () >= 1 && arg.columns () >= 1); |
3202 | 2030 } |
2031 else | |
5823 | 2032 print_usage (); |
3202 | 2033 |
2034 return retval; | |
2035 } | |
2036 | |
3354 | 2037 static octave_value |
5747 | 2038 fill_matrix (const octave_value_list& args, int val, const char *fcn) |
523 | 2039 { |
3354 | 2040 octave_value retval; |
523 | 2041 |
2042 int nargin = args.length (); | |
2043 | |
4946 | 2044 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
4481 | 2045 |
4946 | 2046 dim_vector dims (1, 1); |
4481 | 2047 |
2048 if (nargin > 0 && args(nargin-1).is_string ()) | |
2049 { | |
4946 | 2050 std::string nm = args(nargin-1).string_value (); |
4481 | 2051 nargin--; |
2052 | |
4946 | 2053 dt = oct_data_conv::string_to_data_type (nm); |
2054 | |
2055 if (error_state) | |
2056 return retval; | |
4481 | 2057 } |
2058 | |
523 | 2059 switch (nargin) |
2060 { | |
712 | 2061 case 0: |
2062 break; | |
777 | 2063 |
610 | 2064 case 1: |
4481 | 2065 get_dimensions (args(0), fcn, dims); |
610 | 2066 break; |
777 | 2067 |
4563 | 2068 default: |
2069 { | |
2070 dims.resize (nargin); | |
4481 | 2071 |
4563 | 2072 for (int i = 0; i < nargin; i++) |
2073 { | |
6133 | 2074 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
4481 | 2075 |
4563 | 2076 if (error_state) |
2077 { | |
4732 | 2078 error ("%s: expecting scalar integer arguments", fcn); |
4563 | 2079 break; |
2080 } | |
2081 } | |
2082 } | |
2083 break; | |
4481 | 2084 } |
2085 | |
2086 if (! error_state) | |
2087 { | |
4946 | 2088 dims.chop_trailing_singletons (); |
4565 | 2089 |
4481 | 2090 check_dimensions (dims, fcn); |
3354 | 2091 |
5775 | 2092 // FIXME -- perhaps this should be made extensible by |
4946 | 2093 // using the class name to lookup a function to call to create |
2094 // the new value. | |
2095 | |
2096 // Note that automatic narrowing will handle conversion from | |
2097 // NDArray to scalar. | |
2098 | |
4481 | 2099 if (! error_state) |
2100 { | |
4946 | 2101 switch (dt) |
2102 { | |
2103 case oct_data_conv::dt_int8: | |
2104 retval = int8NDArray (dims, val); | |
2105 break; | |
4481 | 2106 |
4946 | 2107 case oct_data_conv::dt_uint8: |
2108 retval = uint8NDArray (dims, val); | |
2109 break; | |
2110 | |
2111 case oct_data_conv::dt_int16: | |
2112 retval = int16NDArray (dims, val); | |
2113 break; | |
2114 | |
2115 case oct_data_conv::dt_uint16: | |
2116 retval = uint16NDArray (dims, val); | |
2117 break; | |
2118 | |
2119 case oct_data_conv::dt_int32: | |
2120 retval = int32NDArray (dims, val); | |
2121 break; | |
777 | 2122 |
4946 | 2123 case oct_data_conv::dt_uint32: |
2124 retval = uint32NDArray (dims, val); | |
2125 break; | |
2126 | |
2127 case oct_data_conv::dt_int64: | |
2128 retval = int64NDArray (dims, val); | |
2129 break; | |
4481 | 2130 |
4946 | 2131 case oct_data_conv::dt_uint64: |
2132 retval = uint64NDArray (dims, val); | |
2133 break; | |
4481 | 2134 |
5775 | 2135 case oct_data_conv::dt_single: // FIXME |
4946 | 2136 case oct_data_conv::dt_double: |
2137 retval = NDArray (dims, val); | |
2138 break; | |
2139 | |
4986 | 2140 case oct_data_conv::dt_logical: |
2141 retval = boolNDArray (dims, val); | |
2142 break; | |
2143 | |
4946 | 2144 default: |
2145 error ("%s: invalid class name", fcn); | |
2146 break; | |
4481 | 2147 } |
2148 } | |
523 | 2149 } |
2150 | |
2151 return retval; | |
2152 } | |
2153 | |
5747 | 2154 static octave_value |
2155 fill_matrix (const octave_value_list& args, double val, const char *fcn) | |
2156 { | |
2157 octave_value retval; | |
2158 | |
2159 int nargin = args.length (); | |
2160 | |
2161 oct_data_conv::data_type dt = oct_data_conv::dt_double; | |
2162 | |
2163 dim_vector dims (1, 1); | |
2164 | |
2165 if (nargin > 0 && args(nargin-1).is_string ()) | |
2166 { | |
2167 std::string nm = args(nargin-1).string_value (); | |
2168 nargin--; | |
2169 | |
2170 dt = oct_data_conv::string_to_data_type (nm); | |
2171 | |
2172 if (error_state) | |
2173 return retval; | |
2174 } | |
2175 | |
2176 switch (nargin) | |
2177 { | |
2178 case 0: | |
2179 break; | |
2180 | |
2181 case 1: | |
2182 get_dimensions (args(0), fcn, dims); | |
2183 break; | |
2184 | |
2185 default: | |
2186 { | |
2187 dims.resize (nargin); | |
2188 | |
2189 for (int i = 0; i < nargin; i++) | |
2190 { | |
6133 | 2191 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
5747 | 2192 |
2193 if (error_state) | |
2194 { | |
2195 error ("%s: expecting scalar integer arguments", fcn); | |
2196 break; | |
2197 } | |
2198 } | |
2199 } | |
2200 break; | |
2201 } | |
2202 | |
2203 if (! error_state) | |
2204 { | |
2205 dims.chop_trailing_singletons (); | |
2206 | |
2207 check_dimensions (dims, fcn); | |
2208 | |
2209 // Note that automatic narrowing will handle conversion from | |
2210 // NDArray to scalar. | |
2211 | |
2212 if (! error_state) | |
2213 { | |
2214 switch (dt) | |
2215 { | |
5775 | 2216 case oct_data_conv::dt_single: // FIXME |
5747 | 2217 case oct_data_conv::dt_double: |
2218 retval = NDArray (dims, val); | |
2219 break; | |
2220 | |
2221 default: | |
2222 error ("%s: invalid class name", fcn); | |
2223 break; | |
2224 } | |
2225 } | |
2226 } | |
2227 | |
2228 return retval; | |
2229 } | |
2230 | |
2231 static octave_value | |
2232 fill_matrix (const octave_value_list& args, const Complex& val, | |
2233 const char *fcn) | |
2234 { | |
2235 octave_value retval; | |
2236 | |
2237 int nargin = args.length (); | |
2238 | |
2239 oct_data_conv::data_type dt = oct_data_conv::dt_double; | |
2240 | |
2241 dim_vector dims (1, 1); | |
2242 | |
2243 if (nargin > 0 && args(nargin-1).is_string ()) | |
2244 { | |
2245 std::string nm = args(nargin-1).string_value (); | |
2246 nargin--; | |
2247 | |
2248 dt = oct_data_conv::string_to_data_type (nm); | |
2249 | |
2250 if (error_state) | |
2251 return retval; | |
2252 } | |
2253 | |
2254 switch (nargin) | |
2255 { | |
2256 case 0: | |
2257 break; | |
2258 | |
2259 case 1: | |
2260 get_dimensions (args(0), fcn, dims); | |
2261 break; | |
2262 | |
2263 default: | |
2264 { | |
2265 dims.resize (nargin); | |
2266 | |
2267 for (int i = 0; i < nargin; i++) | |
2268 { | |
6133 | 2269 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
5747 | 2270 |
2271 if (error_state) | |
2272 { | |
2273 error ("%s: expecting scalar integer arguments", fcn); | |
2274 break; | |
2275 } | |
2276 } | |
2277 } | |
2278 break; | |
2279 } | |
2280 | |
2281 if (! error_state) | |
2282 { | |
2283 dims.chop_trailing_singletons (); | |
2284 | |
2285 check_dimensions (dims, fcn); | |
2286 | |
2287 // Note that automatic narrowing will handle conversion from | |
2288 // NDArray to scalar. | |
2289 | |
2290 if (! error_state) | |
2291 { | |
2292 switch (dt) | |
2293 { | |
5775 | 2294 case oct_data_conv::dt_single: // FIXME |
5747 | 2295 case oct_data_conv::dt_double: |
2296 retval = ComplexNDArray (dims, val); | |
2297 break; | |
2298 | |
2299 default: | |
2300 error ("%s: invalid class name", fcn); | |
2301 break; | |
2302 } | |
2303 } | |
2304 } | |
2305 | |
2306 return retval; | |
2307 } | |
2308 | |
2309 static octave_value | |
2310 fill_matrix (const octave_value_list& args, bool val, const char *fcn) | |
2311 { | |
2312 octave_value retval; | |
2313 | |
2314 int nargin = args.length (); | |
2315 | |
2316 dim_vector dims (1, 1); | |
2317 | |
2318 switch (nargin) | |
2319 { | |
2320 case 0: | |
2321 break; | |
2322 | |
2323 case 1: | |
2324 get_dimensions (args(0), fcn, dims); | |
2325 break; | |
2326 | |
2327 default: | |
2328 { | |
2329 dims.resize (nargin); | |
2330 | |
2331 for (int i = 0; i < nargin; i++) | |
2332 { | |
6133 | 2333 dims(i) = args(i).is_empty () ? 0 : args(i).idx_type_value (); |
5747 | 2334 |
2335 if (error_state) | |
2336 { | |
2337 error ("%s: expecting scalar integer arguments", fcn); | |
2338 break; | |
2339 } | |
2340 } | |
2341 } | |
2342 break; | |
2343 } | |
2344 | |
2345 if (! error_state) | |
2346 { | |
2347 dims.chop_trailing_singletons (); | |
2348 | |
2349 check_dimensions (dims, fcn); | |
2350 | |
2351 // Note that automatic narrowing will handle conversion from | |
2352 // NDArray to scalar. | |
2353 | |
2354 if (! error_state) | |
2355 retval = boolNDArray (dims, val); | |
2356 } | |
2357 | |
2358 return retval; | |
2359 } | |
2360 | |
3354 | 2361 DEFUN (ones, args, , |
3369 | 2362 "-*- texinfo -*-\n\ |
2363 @deftypefn {Built-in Function} {} ones (@var{x})\n\ | |
2364 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m})\n\ | |
4948 | 2365 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
2366 @deftypefnx {Built-in Function} {} ones (@dots{}, @var{class})\n\ | |
4481 | 2367 Return a matrix or N-dimensional array whose elements are all 1.\n\ |
2368 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
3369 | 2369 \n\ |
2370 If you need to create a matrix whose values are all the same, you should\n\ | |
2371 use an expression like\n\ | |
2372 \n\ | |
2373 @example\n\ | |
2374 val_matrix = val * ones (n, m)\n\ | |
2375 @end example\n\ | |
4945 | 2376 \n\ |
2377 The optional argument @var{class}, allows @code{ones} to return an array of\n\ | |
5747 | 2378 the specified type, for example\n\ |
4945 | 2379 \n\ |
2380 @example\n\ | |
2381 val = ones (n,m, \"uint8\")\n\ | |
2382 @end example\n\ | |
3369 | 2383 @end deftypefn") |
523 | 2384 { |
5747 | 2385 return fill_matrix (args, 1, "ones"); |
523 | 2386 } |
2387 | |
3354 | 2388 DEFUN (zeros, args, , |
3369 | 2389 "-*- texinfo -*-\n\ |
2390 @deftypefn {Built-in Function} {} zeros (@var{x})\n\ | |
2391 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m})\n\ | |
4948 | 2392 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}, @var{k}, @dots{})\n\ |
2393 @deftypefnx {Built-in Function} {} zeros (@dots{}, @var{class})\n\ | |
4481 | 2394 Return a matrix or N-dimensional array whose elements are all 0.\n\ |
2395 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
4945 | 2396 \n\ |
2397 The optional argument @var{class}, allows @code{zeros} to return an array of\n\ | |
5747 | 2398 the specified type, for example\n\ |
4945 | 2399 \n\ |
2400 @example\n\ | |
2401 val = zeros (n,m, \"uint8\")\n\ | |
2402 @end example\n\ | |
3369 | 2403 @end deftypefn") |
523 | 2404 { |
5747 | 2405 return fill_matrix (args, 0, "zeros"); |
2406 } | |
2407 | |
2408 DEFUN (Inf, args, , | |
2409 "-*- texinfo -*-\n\ | |
2410 @deftypefn {Built-in Function} {} Inf (@var{x})\n\ | |
2411 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m})\n\ | |
2412 @deftypefnx {Built-in Function} {} Inf (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2413 @deftypefnx {Built-in Function} {} Inf (@dots{}, @var{class})\n\ | |
2414 Return a matrix or N-dimensional array whose elements are all Infinity.\n\ | |
2415 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
2416 The optional argument @var{class} may be either @samp{\"single\"} or\n\ | |
5798 | 2417 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747 | 2418 @end deftypefn") |
2419 { | |
2420 return fill_matrix (args, lo_ieee_inf_value (), "Inf"); | |
2421 } | |
2422 | |
2423 DEFALIAS (inf, Inf); | |
2424 | |
2425 DEFUN (NaN, args, , | |
2426 "-*- texinfo -*-\n\ | |
2427 @deftypefn {Built-in Function} {} NaN (@var{x})\n\ | |
2428 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m})\n\ | |
2429 @deftypefnx {Built-in Function} {} NaN (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2430 @deftypefnx {Built-in Function} {} NaN (@dots{}, @var{class})\n\ | |
2431 Return a matrix or N-dimensional array whose elements are all NaN\n\ | |
2432 (Not a Number). The value NaN is the result of an operation like\n\ | |
2433 @iftex\n\ | |
2434 @tex\n\ | |
2435 $0/0$, or $\\infty - \\infty$,\n\ | |
2436 @end tex\n\ | |
2437 @end iftex\n\ | |
2438 @ifinfo\n\ | |
2439 0/0, or @samp{Inf - Inf},\n\ | |
2440 @end ifinfo\n\ | |
2441 or any operation with a NaN.\n\ | |
2442 \n\ | |
2443 Note that NaN always compares not equal to NaN. This behavior is\n\ | |
2444 specified by the IEEE standard for floating point arithmetic. To\n\ | |
2445 find NaN values, you must use the @code{isnan} function.\n\ | |
2446 \n\ | |
2447 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
2448 The optional argument @var{class} may be either @samp{\"single\"} or\n\ | |
5798 | 2449 @samp{\"double\"}. The default is @samp{\"double\"}.\n\ |
5747 | 2450 @end deftypefn") |
2451 { | |
2452 return fill_matrix (args, lo_ieee_nan_value (), "NaN"); | |
2453 } | |
2454 | |
2455 DEFALIAS (nan, NaN); | |
2456 | |
2457 DEFUN (e, args, , | |
2458 "-*- texinfo -*-\n\ | |
2459 @deftypefn {Built-in Function} {} e (@var{x})\n\ | |
2460 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m})\n\ | |
2461 @deftypefnx {Built-in Function} {} e (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2462 @deftypefnx {Built-in Function} {} e (@dots{}, @var{class})\n\ | |
2463 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2464 to the base of natural logarithms. The constant\n\ | |
2465 @iftex\n\ | |
2466 @tex\n\ | |
2467 $e$\n\ | |
2468 @end tex\n\ | |
2469 @end iftex\n\ | |
2470 @ifinfo\n\ | |
2471 @var{e}\n\ | |
2472 @end ifinfo\n\ | |
2473 satisfies the equation\n\ | |
2474 @iftex\n\ | |
2475 @tex\n\ | |
2476 $\\log (e) = 1$.\n\ | |
2477 @end tex\n\ | |
2478 @end iftex\n\ | |
2479 @ifinfo\n\ | |
2480 @code{log} (@var{e}) = 1.\n\ | |
2481 @end ifinfo\n\ | |
2482 @end deftypefn") | |
2483 { | |
2484 #if defined (M_E) | |
2485 double e_val = M_E; | |
2486 #else | |
2487 double e_val = exp (1.0); | |
2488 #endif | |
2489 | |
2490 return fill_matrix (args, e_val, "e"); | |
2491 } | |
2492 | |
2493 DEFUN (eps, args, , | |
2494 "-*- texinfo -*-\n\ | |
2495 @deftypefn {Built-in Function} {} eps (@var{x})\n\ | |
2496 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m})\n\ | |
2497 @deftypefnx {Built-in Function} {} eps (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2498 @deftypefnx {Built-in Function} {} eps (@dots{}, @var{class})\n\ | |
2499 Return a matrix or N-dimensional array whose elements are all eps,\n\ | |
2500 the machine precision. More precisely, @code{eps} is the largest\n\ | |
2501 relative spacing between any two adjacent numbers in the machine's\n\ | |
2502 floating point system. This number is obviously system-dependent. On\n\ | |
2503 machines that support 64 bit IEEE floating point arithmetic, @code{eps}\n\ | |
2504 is approximately\n\ | |
2505 @ifinfo\n\ | |
2506 2.2204e-16.\n\ | |
2507 @end ifinfo\n\ | |
2508 @iftex\n\ | |
2509 @tex\n\ | |
2510 $2.2204\\times10^{-16}$.\n\ | |
2511 @end tex\n\ | |
2512 @end iftex\n\ | |
2513 @end deftypefn") | |
2514 { | |
2515 return fill_matrix (args, DBL_EPSILON, "eps"); | |
2516 } | |
2517 | |
2518 DEFUN (pi, args, , | |
2519 "-*- texinfo -*-\n\ | |
2520 @deftypefn {Built-in Function} {} pi (@var{x})\n\ | |
2521 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m})\n\ | |
2522 @deftypefnx {Built-in Function} {} pi (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2523 @deftypefnx {Built-in Function} {} pi (@dots{}, @var{class})\n\ | |
2524 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2525 to the ratio of the circumference of a circle to its diameter.\n\ | |
2526 Internally, @code{pi} is computed as @samp{4.0 * atan (1.0)}.\n\ | |
2527 @end deftypefn") | |
2528 { | |
2529 #if defined (M_PI) | |
2530 double pi_val = M_PI; | |
2531 #else | |
2532 double pi_val = 4.0 * atan (1.0); | |
2533 #endif | |
2534 | |
2535 return fill_matrix (args, pi_val, "pi"); | |
2536 } | |
2537 | |
2538 DEFUN (realmax, args, , | |
2539 "-*- texinfo -*-\n\ | |
2540 @deftypefn {Built-in Function} {} realmax (@var{x})\n\ | |
2541 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m})\n\ | |
2542 @deftypefnx {Built-in Function} {} realmax (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2543 @deftypefnx {Built-in Function} {} realmax (@dots{}, @var{class})\n\ | |
2544 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2545 to the largest floating point number that is representable. The actual\n\ | |
2546 value is system-dependent. On machines that support 64-bit IEEE\n\ | |
2547 floating point arithmetic, @code{realmax} is approximately\n\ | |
2548 @ifinfo\n\ | |
2549 1.7977e+308\n\ | |
2550 @end ifinfo\n\ | |
2551 @iftex\n\ | |
2552 @tex\n\ | |
2553 $1.7977\\times10^{308}$.\n\ | |
2554 @end tex\n\ | |
2555 @end iftex\n\ | |
2556 @seealso{realmin}\n\ | |
2557 @end deftypefn") | |
2558 { | |
2559 return fill_matrix (args, DBL_MAX, "realmax"); | |
2560 } | |
2561 | |
2562 DEFUN (realmin, args, , | |
2563 "-*- texinfo -*-\n\ | |
2564 @deftypefn {Built-in Function} {} realmin (@var{x})\n\ | |
2565 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m})\n\ | |
2566 @deftypefnx {Built-in Function} {} realmin (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2567 @deftypefnx {Built-in Function} {} realmin (@dots{}, @var{class})\n\ | |
2568 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2569 to the smallest normalized floating point number that is representable.\n\ | |
2570 The actual value is system-dependent. On machines that support\n\ | |
2571 64-bit IEEE floating point arithmetic, @code{realmin} is approximately\n\ | |
2572 @ifinfo\n\ | |
2573 2.2251e-308\n\ | |
2574 @end ifinfo\n\ | |
2575 @iftex\n\ | |
2576 @tex\n\ | |
2577 $2.2251\\times10^{-308}$.\n\ | |
2578 @end tex\n\ | |
2579 @end iftex\n\ | |
2580 @seealso{realmax}\n\ | |
2581 @end deftypefn") | |
2582 { | |
2583 return fill_matrix (args, DBL_MIN, "realmin"); | |
2584 } | |
2585 | |
2586 DEFUN (I, args, , | |
2587 "-*- texinfo -*-\n\ | |
2588 @deftypefn {Built-in Function} {} I (@var{x})\n\ | |
2589 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m})\n\ | |
2590 @deftypefnx {Built-in Function} {} I (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2591 @deftypefnx {Built-in Function} {} I (@dots{}, @var{class})\n\ | |
2592 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2593 to the pure imaginary unit, defined as\n\ | |
2594 @iftex\n\ | |
2595 @tex\n\ | |
2596 $\\sqrt{-1}$.\n\ | |
2597 @end tex\n\ | |
2598 @end iftex\n\ | |
2599 @ifinfo\n\ | |
2600 @code{sqrt (-1)}.\n\ | |
2601 @end ifinfo\n\ | |
7001 | 2602 Since I (also i, J, and j) is a function, you can use the name(s) for\n\ |
5747 | 2603 other purposes.\n\ |
2604 @end deftypefn") | |
2605 { | |
2606 return fill_matrix (args, Complex (0.0, 1.0), "I"); | |
2607 } | |
2608 | |
2609 DEFALIAS (i, I); | |
2610 DEFALIAS (J, I); | |
2611 DEFALIAS (j, I); | |
2612 | |
2613 DEFUN (NA, args, , | |
2614 "-*- texinfo -*-\n\ | |
2615 @deftypefn {Built-in Function} {} NA (@var{x})\n\ | |
2616 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m})\n\ | |
2617 @deftypefnx {Built-in Function} {} NA (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2618 @deftypefnx {Built-in Function} {} NA (@dots{}, @var{class})\n\ | |
2619 Return a matrix or N-dimensional array whose elements are all equal\n\ | |
2620 to the special constant used to designate missing values.\n\ | |
2621 @end deftypefn") | |
2622 { | |
2623 return fill_matrix (args, lo_ieee_na_value (), "NA"); | |
2624 } | |
2625 | |
2626 DEFUN (false, args, , | |
2627 "-*- texinfo -*-\n\ | |
2628 @deftypefn {Built-in Function} {} false (@var{x})\n\ | |
2629 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m})\n\ | |
2630 @deftypefnx {Built-in Function} {} false (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2631 Return a matrix or N-dimensional array whose elements are all logical 0.\n\ | |
2632 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
2633 @end deftypefn") | |
2634 { | |
2635 return fill_matrix (args, false, "false"); | |
2636 } | |
2637 | |
2638 DEFUN (true, args, , | |
2639 "-*- texinfo -*-\n\ | |
2640 @deftypefn {Built-in Function} {} true (@var{x})\n\ | |
2641 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m})\n\ | |
2642 @deftypefnx {Built-in Function} {} true (@var{n}, @var{m}, @var{k}, @dots{})\n\ | |
2643 Return a matrix or N-dimensional array whose elements are all logical 1.\n\ | |
2644 The arguments are handled the same as the arguments for @code{eye}.\n\ | |
2645 @end deftypefn") | |
2646 { | |
2647 return fill_matrix (args, true, "true"); | |
3354 | 2648 } |
523 | 2649 |
4946 | 2650 template <class MT> |
2651 octave_value | |
2652 identity_matrix (int nr, int nc) | |
2653 { | |
2654 octave_value retval; | |
2655 | |
2656 typename octave_array_type_traits<MT>::element_type one (1); | |
2657 | |
2658 if (nr == 1 && nc == 1) | |
2659 retval = one; | |
2660 else | |
2661 { | |
2662 dim_vector dims (nr, nc); | |
2663 | |
2664 typename octave_array_type_traits<MT>::element_type zero (0); | |
2665 | |
2666 MT m (dims, zero); | |
2667 | |
2668 if (nr > 0 && nc > 0) | |
2669 { | |
2670 int n = std::min (nr, nc); | |
2671 | |
2672 for (int i = 0; i < n; i++) | |
2673 m(i,i) = one; | |
2674 } | |
2675 | |
2676 retval = m; | |
2677 } | |
2678 | |
2679 return retval; | |
2680 } | |
2681 | |
5058 | 2682 #define INSTANTIATE_EYE(T) \ |
2683 template octave_value identity_matrix<T> (int, int) | |
2684 | |
2685 INSTANTIATE_EYE (int8NDArray); | |
2686 INSTANTIATE_EYE (uint8NDArray); | |
2687 INSTANTIATE_EYE (int16NDArray); | |
2688 INSTANTIATE_EYE (uint16NDArray); | |
2689 INSTANTIATE_EYE (int32NDArray); | |
2690 INSTANTIATE_EYE (uint32NDArray); | |
2691 INSTANTIATE_EYE (int64NDArray); | |
2692 INSTANTIATE_EYE (uint64NDArray); | |
2693 INSTANTIATE_EYE (NDArray); | |
2694 INSTANTIATE_EYE (boolNDArray); | |
2695 | |
4945 | 2696 static octave_value |
4948 | 2697 identity_matrix (int nr, int nc, oct_data_conv::data_type dt) |
4945 | 2698 { |
2699 octave_value retval; | |
2700 | |
5775 | 2701 // FIXME -- perhaps this should be made extensible by using |
4946 | 2702 // the class name to lookup a function to call to create the new |
2703 // value. | |
2704 | |
2705 if (! error_state) | |
2706 { | |
2707 switch (dt) | |
2708 { | |
2709 case oct_data_conv::dt_int8: | |
2710 retval = identity_matrix<int8NDArray> (nr, nc); | |
2711 break; | |
2712 | |
2713 case oct_data_conv::dt_uint8: | |
2714 retval = identity_matrix<uint8NDArray> (nr, nc); | |
2715 break; | |
2716 | |
2717 case oct_data_conv::dt_int16: | |
2718 retval = identity_matrix<int16NDArray> (nr, nc); | |
2719 break; | |
4945 | 2720 |
4946 | 2721 case oct_data_conv::dt_uint16: |
2722 retval = identity_matrix<uint16NDArray> (nr, nc); | |
2723 break; | |
2724 | |
2725 case oct_data_conv::dt_int32: | |
2726 retval = identity_matrix<int32NDArray> (nr, nc); | |
2727 break; | |
2728 | |
2729 case oct_data_conv::dt_uint32: | |
2730 retval = identity_matrix<uint32NDArray> (nr, nc); | |
2731 break; | |
4945 | 2732 |
4946 | 2733 case oct_data_conv::dt_int64: |
2734 retval = identity_matrix<int64NDArray> (nr, nc); | |
2735 break; | |
2736 | |
2737 case oct_data_conv::dt_uint64: | |
2738 retval = identity_matrix<uint64NDArray> (nr, nc); | |
2739 break; | |
4945 | 2740 |
5775 | 2741 case oct_data_conv::dt_single: // FIXME |
4946 | 2742 case oct_data_conv::dt_double: |
2743 retval = identity_matrix<NDArray> (nr, nc); | |
2744 break; | |
4945 | 2745 |
4986 | 2746 case oct_data_conv::dt_logical: |
2747 retval = identity_matrix<boolNDArray> (nr, nc); | |
2748 break; | |
2749 | |
4946 | 2750 default: |
2751 error ("eye: invalid class name"); | |
2752 break; | |
4945 | 2753 } |
2754 } | |
2755 | |
2756 return retval; | |
2757 } | |
2758 | |
4946 | 2759 #undef INT_EYE_MATRIX |
2760 | |
1957 | 2761 DEFUN (eye, args, , |
3369 | 2762 "-*- texinfo -*-\n\ |
2763 @deftypefn {Built-in Function} {} eye (@var{x})\n\ | |
2764 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m})\n\ | |
4948 | 2765 @deftypefnx {Built-in Function} {} eye (@dots{}, @var{class})\n\ |
3369 | 2766 Return an identity matrix. If invoked with a single scalar argument,\n\ |
2767 @code{eye} returns a square matrix with the dimension specified. If you\n\ | |
2768 supply two scalar arguments, @code{eye} takes them to be the number of\n\ | |
2769 rows and columns. If given a vector with two elements, @code{eye} uses\n\ | |
2770 the values of the elements as the number of rows and columns,\n\ | |
2771 respectively. For example,\n\ | |
2772 \n\ | |
2773 @example\n\ | |
2774 @group\n\ | |
2775 eye (3)\n\ | |
2776 @result{} 1 0 0\n\ | |
2777 0 1 0\n\ | |
2778 0 0 1\n\ | |
2779 @end group\n\ | |
2780 @end example\n\ | |
2781 \n\ | |
2782 The following expressions all produce the same result:\n\ | |
2783 \n\ | |
2784 @example\n\ | |
2785 @group\n\ | |
2786 eye (2)\n\ | |
2787 @equiv{}\n\ | |
2788 eye (2, 2)\n\ | |
2789 @equiv{}\n\ | |
2790 eye (size ([1, 2; 3, 4])\n\ | |
2791 @end group\n\ | |
2792 @end example\n\ | |
2793 \n\ | |
4945 | 2794 The optional argument @var{class}, allows @code{eye} to return an array of\n\ |
2795 the specified type, like\n\ | |
2796 \n\ | |
2797 @example\n\ | |
2798 val = zeros (n,m, \"uint8\")\n\ | |
2799 @end example\n\ | |
2800 \n\ | |
6556 | 2801 Calling @code{eye} with no arguments is equivalent to calling it\n\ |
2802 with an argument of 1. This odd definition is for compatibility\n\ | |
2803 with @sc{Matlab}.\n\ | |
3369 | 2804 @end deftypefn") |
523 | 2805 { |
3354 | 2806 octave_value retval; |
523 | 2807 |
4948 | 2808 int nargin = args.length (); |
4945 | 2809 |
4948 | 2810 oct_data_conv::data_type dt = oct_data_conv::dt_double; |
523 | 2811 |
4945 | 2812 // Check for type information. |
2813 | |
2814 if (nargin > 0 && args(nargin-1).is_string ()) | |
2815 { | |
4948 | 2816 std::string nm = args(nargin-1).string_value (); |
4945 | 2817 nargin--; |
4948 | 2818 |
2819 dt = oct_data_conv::string_to_data_type (nm); | |
2820 | |
2821 if (error_state) | |
2822 return retval; | |
4945 | 2823 } |
2824 | |
523 | 2825 switch (nargin) |
2826 { | |
712 | 2827 case 0: |
4948 | 2828 retval = identity_matrix (1, 1, dt); |
712 | 2829 break; |
777 | 2830 |
610 | 2831 case 1: |
3354 | 2832 { |
5275 | 2833 octave_idx_type nr, nc; |
3354 | 2834 get_dimensions (args(0), "eye", nr, nc); |
2835 | |
2836 if (! error_state) | |
4948 | 2837 retval = identity_matrix (nr, nc, dt); |
3354 | 2838 } |
610 | 2839 break; |
777 | 2840 |
523 | 2841 case 2: |
3354 | 2842 { |
5275 | 2843 octave_idx_type nr, nc; |
3354 | 2844 get_dimensions (args(0), args(1), "eye", nr, nc); |
2845 | |
2846 if (! error_state) | |
4948 | 2847 retval = identity_matrix (nr, nc, dt); |
3354 | 2848 } |
523 | 2849 break; |
777 | 2850 |
523 | 2851 default: |
5823 | 2852 print_usage (); |
523 | 2853 break; |
2854 } | |
2855 | |
2856 return retval; | |
2857 } | |
2858 | |
1957 | 2859 DEFUN (linspace, args, , |
3369 | 2860 "-*- texinfo -*-\n\ |
2861 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ | |
2862 Return a row vector with @var{n} linearly spaced elements between\n\ | |
6630 | 2863 @var{base} and @var{limit}. If the number of elements is greater than one,\n\ |
2864 then the @var{base} and @var{limit} are always included in\n\ | |
3369 | 2865 the range. If @var{base} is greater than @var{limit}, the elements are\n\ |
2866 stored in decreasing order. If the number of points is not specified, a\n\ | |
2867 value of 100 is used.\n\ | |
1100 | 2868 \n\ |
4455 | 2869 The @code{linspace} function always returns a row vector.\n\ |
6630 | 2870 \n\ |
2871 For compatibility with @sc{Matlab}, return the second argument if\n\ | |
2872 fewer than two values are requested.\n\ | |
3369 | 2873 @end deftypefn") |
1100 | 2874 { |
3418 | 2875 octave_value retval; |
1100 | 2876 |
2877 int nargin = args.length (); | |
2878 | |
6133 | 2879 octave_idx_type npoints = 100; |
1100 | 2880 |
1940 | 2881 if (nargin != 2 && nargin != 3) |
2882 { | |
5823 | 2883 print_usage (); |
1940 | 2884 return retval; |
2885 } | |
2886 | |
1100 | 2887 if (nargin == 3) |
6133 | 2888 npoints = args(2).idx_type_value (); |
1100 | 2889 |
2890 if (! error_state) | |
2891 { | |
3322 | 2892 octave_value arg_1 = args(0); |
2893 octave_value arg_2 = args(1); | |
1100 | 2894 |
3322 | 2895 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) |
2896 { | |
2897 Complex x1 = arg_1.complex_value (); | |
2898 Complex x2 = arg_2.complex_value (); | |
2899 | |
2900 if (! error_state) | |
1100 | 2901 { |
3322 | 2902 ComplexRowVector rv = linspace (x1, x2, npoints); |
1100 | 2903 |
2904 if (! error_state) | |
3418 | 2905 retval = rv; |
1100 | 2906 } |
2907 } | |
2908 else | |
3322 | 2909 { |
2910 double x1 = arg_1.double_value (); | |
2911 double x2 = arg_2.double_value (); | |
2912 | |
2913 if (! error_state) | |
2914 { | |
2915 RowVector rv = linspace (x1, x2, npoints); | |
2916 | |
2917 if (! error_state) | |
3418 | 2918 retval = rv; |
3322 | 2919 } |
2920 } | |
1100 | 2921 } |
4732 | 2922 else |
2923 error ("linspace: expecting third argument to be an integer"); | |
1100 | 2924 |
2925 return retval; | |
2926 } | |
2927 | |
5775 | 2928 // FIXME -- should accept dimensions as separate args for N-d |
5734 | 2929 // arrays as well as 1-d and 2-d arrays. |
2930 | |
5731 | 2931 DEFUN (resize, args, , |
2932 "-*- texinfo -*-\n\ | |
2933 @deftypefn {Built-in Function} {} resize (@var{x}, @var{m})\n\ | |
2934 @deftypefnx {Built-in Function} {} resize (@var{x}, @var{m}, @var{n})\n\ | |
6174 | 2935 Destructively resize @var{x}.\n\ |
2936 \n\ | |
2937 @strong{Values in @var{x} are not preserved as they are with\n\ | |
6175 | 2938 @code{reshape}.}\n\ |
6174 | 2939 \n\ |
2940 If only @var{m} is supplied and it is a scalar, the dimension of the\n\ | |
2941 result is @var{m}-by-@var{m}. If @var{m} is a vector, then the\n\ | |
2942 dimensions of the result are given by the elements of @var{m}.\n\ | |
2943 If both @var{m} and @var{n} are scalars, then the dimensions of\n\ | |
2944 the result are @var{m}-by-@var{n}.\n\ | |
2945 @seealso{reshape}\n\ | |
5731 | 2946 @end deftypefn") |
2947 { | |
2948 octave_value retval; | |
2949 int nargin = args.length (); | |
2950 | |
2951 if (nargin == 2) | |
2952 { | |
2953 Array<double> vec = args(1).vector_value (); | |
2954 int ndim = vec.length (); | |
2955 if (ndim == 1) | |
2956 { | |
2957 octave_idx_type m = static_cast<octave_idx_type> (vec(0)); | |
2958 retval = args(0); | |
2959 retval = retval.resize (dim_vector (m, m), true); | |
2960 } | |
2961 else | |
2962 { | |
2963 dim_vector dv; | |
2964 dv.resize (ndim); | |
2965 for (int i = 0; i < ndim; i++) | |
2966 dv(i) = static_cast<octave_idx_type> (vec(i)); | |
2967 retval = args(0); | |
2968 retval = retval.resize (dv, true); | |
2969 } | |
2970 } | |
2971 else if (nargin == 3) | |
2972 { | |
2973 octave_idx_type m = static_cast<octave_idx_type> | |
2974 (args(1).scalar_value()); | |
2975 octave_idx_type n = static_cast<octave_idx_type> | |
2976 (args(2).scalar_value()); | |
2977 if (!error_state) | |
2978 { | |
2979 retval = args(0); | |
2980 retval = retval.resize (dim_vector (m, n), true); | |
2981 } | |
2982 } | |
2983 else | |
5823 | 2984 print_usage (); |
5731 | 2985 return retval; |
2986 } | |
2987 | |
5775 | 2988 // FIXME -- should use octave_idx_type for dimensions. |
5734 | 2989 |
4567 | 2990 DEFUN (reshape, args, , |
2991 "-*- texinfo -*-\n\ | |
6671 | 2992 @deftypefn {Built-in Function} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ |
2993 @deftypefnx {Built-in Function} {} reshape (@var{a}, @var{siz})\n\ | |
4567 | 2994 Return a matrix with the given dimensions whose elements are taken\n\ |
6671 | 2995 from the matrix @var{a}. The elements of the matrix are accessed in\n\ |
4567 | 2996 column-major order (like Fortran arrays are stored).\n\ |
2997 \n\ | |
2998 For example,\n\ | |
2999 \n\ | |
3000 @example\n\ | |
3001 @group\n\ | |
3002 reshape ([1, 2, 3, 4], 2, 2)\n\ | |
3003 @result{} 1 3\n\ | |
3004 2 4\n\ | |
3005 @end group\n\ | |
3006 @end example\n\ | |
3007 \n\ | |
3008 @noindent\n\ | |
3009 Note that the total number of elements in the original\n\ | |
3010 matrix must match the total number of elements in the new matrix.\n\ | |
5013 | 3011 \n\ |
3012 A single dimension of the return matrix can be unknown and is flagged\n\ | |
3013 by an empty argument.\n\ | |
4567 | 3014 @end deftypefn") |
3015 { | |
3016 octave_value retval; | |
3017 | |
3018 int nargin = args.length (); | |
3019 | |
3020 Array<int> new_size; | |
3021 | |
3022 if (nargin == 2) | |
3023 new_size = args(1).int_vector_value (); | |
3024 else if (nargin > 2) | |
3025 { | |
3026 new_size.resize (nargin-1); | |
5013 | 3027 int empty_dim = -1; |
3028 | |
4567 | 3029 for (int i = 1; i < nargin; i++) |
3030 { | |
5013 | 3031 if (args(i).is_empty ()) |
3032 if (empty_dim > 0) | |
3033 { | |
3034 error ("reshape: only a single dimension can be unknown"); | |
3035 break; | |
3036 } | |
3037 else | |
3038 { | |
3039 empty_dim = i; | |
3040 new_size(i-1) = 1; | |
3041 } | |
3042 else | |
3043 { | |
6133 | 3044 new_size(i-1) = args(i).idx_type_value (); |
4567 | 3045 |
5013 | 3046 if (error_state) |
3047 break; | |
3048 } | |
3049 } | |
3050 | |
3051 if (! error_state && (empty_dim > 0)) | |
3052 { | |
3053 int nel = 1; | |
3054 for (int i = 0; i < nargin - 1; i++) | |
3055 nel *= new_size(i); | |
3056 | |
3057 if (nel == 0) | |
3058 new_size(empty_dim-1) = 0; | |
3059 else | |
3060 { | |
3061 int size_empty_dim = args(0).numel () / nel; | |
3062 | |
3063 if (args(0).numel () != size_empty_dim * nel) | |
3064 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); | |
3065 else | |
3066 new_size(empty_dim-1) = size_empty_dim; | |
3067 } | |
4567 | 3068 } |
3069 } | |
3070 else | |
3071 { | |
5823 | 3072 print_usage (); |
4567 | 3073 return retval; |
3074 } | |
3075 | |
3076 if (error_state) | |
3077 { | |
3078 error ("reshape: invalid arguments"); | |
3079 return retval; | |
3080 } | |
3081 | |
4739 | 3082 // Remove trailing singletons in new_size, but leave at least 2 |
3083 // elements. | |
3084 | |
4567 | 3085 int n = new_size.length (); |
3086 | |
4739 | 3087 while (n > 2) |
3088 { | |
3089 if (new_size(n-1) == 1) | |
3090 n--; | |
3091 else | |
3092 break; | |
3093 } | |
3094 | |
3095 new_size.resize (n); | |
3096 | |
4567 | 3097 if (n < 2) |
3098 { | |
3099 error ("reshape: expecting size to be vector with at least 2 elements"); | |
3100 return retval; | |
3101 } | |
3102 | |
3103 dim_vector new_dims; | |
3104 | |
3105 new_dims.resize (n); | |
3106 | |
5275 | 3107 for (octave_idx_type i = 0; i < n; i++) |
4567 | 3108 new_dims(i) = new_size(i); |
3109 | |
3110 octave_value arg = args(0); | |
3111 | |
3112 if (new_dims.numel () == arg.numel ()) | |
3113 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); | |
3114 else | |
3115 error ("reshape: size mismatch"); | |
3116 | |
3117 return retval; | |
3118 } | |
3119 | |
4532 | 3120 DEFUN (squeeze, args, , |
3121 "-*- texinfo -*-\n\ | |
3122 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ | |
3123 Remove singleton dimensions from @var{x} and return the result.\n\ | |
6999 | 3124 Note that for compatibility with @sc{Matlab}, all objects have\n\ |
7007 | 3125 a minimum of two dimensions and row vectors are left unchanged.\n\ |
4532 | 3126 @end deftypefn") |
3127 { | |
3128 octave_value retval; | |
3129 | |
3130 if (args.length () == 1) | |
4545 | 3131 retval = args(0).squeeze (); |
4532 | 3132 else |
5823 | 3133 print_usage (); |
4532 | 3134 |
3135 return retval; | |
3136 } | |
3137 | |
6953 | 3138 /* |
3139 %!shared x | |
3140 %! x = [1, -3, 4, 5, -7]; | |
3141 %!assert(norm(x,1), 20); | |
3142 %!assert(norm(x,2), 10); | |
3143 %!assert(norm(x,3), 8.24257059961711, -4*eps); | |
3144 %!assert(norm(x,Inf), 7); | |
3145 %!assert(norm(x,-Inf), 1); | |
3146 %!assert(norm(x,"inf"), 7); | |
7103 | 3147 %!assert(norm(x,"fro"), 10, -eps); |
6953 | 3148 %!assert(norm(x), 10); |
3149 %!assert(norm([1e200, 1]), 1e200); | |
3150 %!assert(norm([3+4i, 3-4i, sqrt(31)]), 9, -4*eps); | |
3151 %!shared m | |
3152 %! m = magic (4); | |
3153 %!assert(norm(m,1), 34); | |
7026 | 3154 %!assert(norm(m,2), 34, -eps); |
6953 | 3155 %!assert(norm(m,Inf), 34); |
3156 %!assert(norm(m,"inf"), 34); | |
7103 | 3157 %!shared m2, flo, fhi |
7102 | 3158 %! m2 = [1,2;3,4]; |
3159 %! flo = 1e-300; | |
3160 %! fhi = 1e+300; | |
7103 | 3161 %!assert (norm(flo*m2,"fro"), sqrt(30)*flo, -eps) |
3162 %!assert (norm(fhi*m2,"fro"), sqrt(30)*fhi, -eps) | |
6953 | 3163 */ |
3164 | |
6945 | 3165 // Compute various norms of the vector X. |
3166 | |
6953 | 3167 DEFUN (norm, args, , |
6508 | 3168 "-*- texinfo -*-\n\ |
6953 | 3169 @deftypefn {Function File} {} norm (@var{a}, @var{p})\n\ |
3170 Compute the p-norm of the matrix @var{a}. If the second argument is\n\ | |
3171 missing, @code{p = 2} is assumed.\n\ | |
3172 \n\ | |
3173 If @var{a} is a matrix:\n\ | |
3174 \n\ | |
3175 @table @asis\n\ | |
3176 @item @var{p} = @code{1}\n\ | |
3177 1-norm, the largest column sum of the absolute values of @var{a}.\n\ | |
3178 \n\ | |
3179 @item @var{p} = @code{2}\n\ | |
3180 Largest singular value of @var{a}.\n\ | |
3181 \n\ | |
7189 | 3182 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953 | 3183 @cindex infinity norm\n\ |
3184 Infinity norm, the largest row sum of the absolute values of @var{a}.\n\ | |
3185 \n\ | |
3186 @item @var{p} = @code{\"fro\"}\n\ | |
3187 @cindex Frobenius norm\n\ | |
3188 Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}.\n\ | |
3189 @end table\n\ | |
3190 \n\ | |
3191 If @var{a} is a vector or a scalar:\n\ | |
3192 \n\ | |
3193 @table @asis\n\ | |
7189 | 3194 @item @var{p} = @code{Inf} or @code{\"inf\"}\n\ |
6953 | 3195 @code{max (abs (@var{a}))}.\n\ |
3196 \n\ | |
3197 @item @var{p} = @code{-Inf}\n\ | |
3198 @code{min (abs (@var{a}))}.\n\ | |
3199 \n\ | |
7189 | 3200 @item @var{p} = @code{\"fro\"}\n\ |
3201 Frobenius norm of @var{a}, @code{sqrt (sumsq (abs (a)))}.\n\ | |
3202 \n\ | |
6953 | 3203 @item other\n\ |
3204 p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}.\n\ | |
3205 @end table\n\ | |
3206 @seealso{cond, svd}\n\ | |
6508 | 3207 @end deftypefn") |
3208 { | |
6953 | 3209 // Currently only handles vector norms for full double/complex |
3210 // vectors internally. Other cases are handled by __norm__.m. | |
3211 | |
3212 octave_value_list retval; | |
6508 | 3213 |
3214 int nargin = args.length (); | |
3215 | |
3216 if (nargin == 1 || nargin == 2) | |
3217 { | |
6953 | 3218 octave_value x_arg = args(0); |
3219 | |
3220 if (x_arg.is_empty ()) | |
3221 retval(0) = 0.0; | |
3222 else if (x_arg.ndims () == 2) | |
6508 | 3223 { |
6953 | 3224 if ((x_arg.rows () == 1 || x_arg.columns () == 1) |
3225 && ! (x_arg.is_sparse_type () || x_arg.is_integer_type ())) | |
3226 { | |
7093 | 3227 double p_val = 2; |
3228 | |
3229 if (nargin == 2) | |
6953 | 3230 { |
7093 | 3231 octave_value p_arg = args(1); |
3232 | |
3233 if (p_arg.is_string ()) | |
3234 { | |
3235 std::string p = args(1).string_value (); | |
3236 | |
3237 if (p == "inf") | |
3238 p_val = octave_Inf; | |
3239 else if (p == "fro") | |
3240 p_val = -1; | |
3241 else | |
3242 error ("norm: unrecognized norm `%s'", p.c_str ()); | |
3243 } | |
6953 | 3244 else |
7093 | 3245 { |
3246 p_val = p_arg.double_value (); | |
3247 | |
3248 if (error_state) | |
3249 error ("norm: unrecognized norm value"); | |
3250 } | |
6953 | 3251 } |
3252 | |
3253 if (! error_state) | |
3254 { | |
3255 if (x_arg.is_real_type ()) | |
3256 { | |
3257 MArray<double> x (x_arg.array_value ()); | |
3258 | |
3259 if (! error_state) | |
3260 retval(0) = x.norm (p_val); | |
3261 else | |
3262 error ("norm: expecting real vector"); | |
3263 } | |
3264 else | |
3265 { | |
3266 MArray<Complex> x (x_arg.complex_array_value ()); | |
3267 | |
3268 if (! error_state) | |
3269 retval(0) = x.norm (p_val); | |
3270 else | |
3271 error ("norm: expecting complex vector"); | |
3272 } | |
3273 } | |
3274 } | |
6508 | 3275 else |
6953 | 3276 retval = feval ("__norm__", args); |
6508 | 3277 } |
3278 else | |
6953 | 3279 error ("norm: only valid for 2-D objects"); |
6508 | 3280 } |
3281 else | |
3282 print_usage (); | |
3283 | |
7269 | 3284 // Should not return a sparse type |
3285 if (retval(0).is_sparse_type ()) | |
3286 { | |
3287 if (retval(0).type_name () == "sparse matrix") | |
3288 retval(0) = retval(0).matrix_value (); | |
3289 else if (retval(0).type_name () == "sparse complex matrix") | |
3290 retval(0) = retval(0).complex_matrix_value (); | |
3291 else if (retval(0).type_name () == "sparse bool matrix") | |
3292 retval(0) = retval(0).bool_matrix_value (); | |
3293 } | |
3294 | |
6508 | 3295 return retval; |
3296 } | |
3297 | |
6518 | 3298 #define UNARY_OP_DEFUN_BODY(F) \ |
3299 \ | |
3300 octave_value retval; \ | |
3301 \ | |
3302 if (args.length () == 1) \ | |
3303 retval = F (args(0)); \ | |
3304 else \ | |
3305 print_usage (); \ | |
3306 \ | |
3307 return retval | |
3308 | |
3309 DEFUN (not, args, , | |
3310 "-*- texinfo -*-\n\ | |
3311 @deftypefn {Built-in Function} {} not (@var{x})\n\ | |
3312 This function is equivalent to @code{! x}.\n\ | |
3313 @end deftypefn") | |
3314 { | |
3315 UNARY_OP_DEFUN_BODY (op_not); | |
3316 } | |
3317 | |
3318 DEFUN (uplus, args, , | |
3319 "-*- texinfo -*-\n\ | |
3320 @deftypefn {Built-in Function} {} uplus (@var{x})\n\ | |
3321 This function is equivalent to @code{+ x}.\n\ | |
3322 @end deftypefn") | |
3323 { | |
3324 UNARY_OP_DEFUN_BODY (op_uplus); | |
3325 } | |
3326 | |
3327 DEFUN (uminus, args, , | |
3328 "-*- texinfo -*-\n\ | |
3329 @deftypefn {Built-in Function} {} uminus (@var{x})\n\ | |
3330 This function is equivalent to @code{- x}.\n\ | |
3331 @end deftypefn") | |
3332 { | |
3333 UNARY_OP_DEFUN_BODY (op_uminus); | |
3334 } | |
3335 | |
3336 DEFUN (transpose, args, , | |
3337 "-*- texinfo -*-\n\ | |
3338 @deftypefn {Built-in Function} {} transpose (@var{x})\n\ | |
3339 This function is equivalent to @code{x.'}.\n\ | |
3340 @end deftypefn") | |
3341 { | |
3342 UNARY_OP_DEFUN_BODY (op_transpose); | |
3343 } | |
3344 | |
3345 DEFUN (ctranspose, args, , | |
3346 "-*- texinfo -*-\n\ | |
3347 @deftypefn {Built-in Function} {} ctranspose (@var{x})\n\ | |
3348 This function is equivalent to @code{x'}.\n\ | |
3349 @end deftypefn") | |
3350 { | |
3351 UNARY_OP_DEFUN_BODY (op_hermitian); | |
3352 } | |
3353 | |
3354 #define BINARY_OP_DEFUN_BODY(F) \ | |
3355 \ | |
3356 octave_value retval; \ | |
3357 \ | |
3358 if (args.length () == 2) \ | |
3359 retval = F (args(0), args(1)); \ | |
3360 else \ | |
3361 print_usage (); \ | |
3362 \ | |
3363 return retval | |
3364 | |
3365 DEFUN (plus, args, , | |
3366 "-*- texinfo -*-\n\ | |
3367 @deftypefn {Built-in Function} {} plus (@var{x}, @var{y})\n\ | |
3368 This function is equivalent to @code{x + y}.\n\ | |
3369 @end deftypefn") | |
3370 { | |
3371 BINARY_OP_DEFUN_BODY (op_add); | |
3372 } | |
3373 | |
3374 DEFUN (minus, args, , | |
3375 "-*- texinfo -*-\n\ | |
3376 @deftypefn {Built-in Function} {} minus (@var{x}, @var{y})\n\ | |
3377 This function is equivalent to @code{x - y}.\n\ | |
3378 @end deftypefn") | |
3379 { | |
3380 BINARY_OP_DEFUN_BODY (op_sub); | |
3381 } | |
3382 | |
3383 DEFUN (mtimes, args, , | |
3384 "-*- texinfo -*-\n\ | |
3385 @deftypefn {Built-in Function} {} mtimes (@var{x}, @var{y})\n\ | |
3386 This function is equivalent to @code{x * y}.\n\ | |
3387 @end deftypefn") | |
3388 { | |
3389 BINARY_OP_DEFUN_BODY (op_mul); | |
3390 } | |
3391 | |
3392 DEFUN (mrdivide, args, , | |
3393 "-*- texinfo -*-\n\ | |
3394 @deftypefn {Built-in Function} {} mrdivide (@var{x}, @var{y})\n\ | |
3395 This function is equivalent to @code{x / y}.\n\ | |
3396 @end deftypefn") | |
3397 { | |
3398 BINARY_OP_DEFUN_BODY (op_div); | |
3399 } | |
3400 | |
3401 DEFUN (mpower, args, , | |
3402 "-*- texinfo -*-\n\ | |
3403 @deftypefn {Built-in Function} {} mpower (@var{x}, @var{y})\n\ | |
3404 This function is equivalent to @code{x ^ y}.\n\ | |
3405 @end deftypefn") | |
3406 { | |
3407 BINARY_OP_DEFUN_BODY (op_pow); | |
3408 } | |
3409 | |
3410 DEFUN (mldivide, args, , | |
3411 "-*- texinfo -*-\n\ | |
3412 @deftypefn {Built-in Function} {} mldivide (@var{x}, @var{y})\n\ | |
3413 This function is equivalent to @code{x \\ y}.\n\ | |
3414 @end deftypefn") | |
3415 { | |
3416 BINARY_OP_DEFUN_BODY (op_ldiv); | |
3417 } | |
3418 | |
3419 DEFUN (lt, args, , | |
3420 "-*- texinfo -*-\n\ | |
3421 @deftypefn {Built-in Function} {} lt (@var{x}, @var{y})\n\ | |
3422 This function is equivalent to @code{x < y}.\n\ | |
3423 @end deftypefn") | |
3424 { | |
3425 BINARY_OP_DEFUN_BODY (op_lt); | |
3426 } | |
3427 | |
3428 DEFUN (le, args, , | |
3429 "-*- texinfo -*-\n\ | |
3430 @deftypefn {Built-in Function} {} le (@var{x}, @var{y})\n\ | |
3431 This function is equivalent to @code{x <= y}.\n\ | |
3432 @end deftypefn") | |
3433 { | |
3434 BINARY_OP_DEFUN_BODY (op_le); | |
3435 } | |
3436 | |
3437 DEFUN (eq, args, , | |
3438 "-*- texinfo -*-\n\ | |
3439 @deftypefn {Built-in Function} {} eq (@var{x}, @var{y})\n\ | |
3440 This function is equivalent to @code{x == y}.\n\ | |
3441 @end deftypefn") | |
3442 { | |
3443 BINARY_OP_DEFUN_BODY (op_eq); | |
3444 } | |
3445 | |
3446 DEFUN (ge, args, , | |
3447 "-*- texinfo -*-\n\ | |
3448 @deftypefn {Built-in Function} {} ge (@var{x}, @var{y})\n\ | |
3449 This function is equivalent to @code{x >= y}.\n\ | |
3450 @end deftypefn") | |
3451 { | |
3452 BINARY_OP_DEFUN_BODY (op_ge); | |
3453 } | |
3454 | |
3455 DEFUN (gt, args, , | |
3456 "-*- texinfo -*-\n\ | |
3457 @deftypefn {Built-in Function} {} gt (@var{x}, @var{y})\n\ | |
3458 This function is equivalent to @code{x > y}.\n\ | |
3459 @end deftypefn") | |
3460 { | |
3461 BINARY_OP_DEFUN_BODY (op_gt); | |
3462 } | |
3463 | |
3464 DEFUN (ne, args, , | |
3465 "-*- texinfo -*-\n\ | |
3466 @deftypefn {Built-in Function} {} ne (@var{x}, @var{y})\n\ | |
3467 This function is equivalent to @code{x != y}.\n\ | |
3468 @end deftypefn") | |
3469 { | |
3470 BINARY_OP_DEFUN_BODY (op_ne); | |
3471 } | |
3472 | |
3473 DEFUN (times, args, , | |
3474 "-*- texinfo -*-\n\ | |
3475 @deftypefn {Built-in Function} {} times (@var{x}, @var{y})\n\ | |
3476 This function is equivalent to @code{x .* y}.\n\ | |
3477 @end deftypefn") | |
3478 { | |
3479 BINARY_OP_DEFUN_BODY (op_el_mul); | |
3480 } | |
3481 | |
3482 DEFUN (rdivide, args, , | |
3483 "-*- texinfo -*-\n\ | |
3484 @deftypefn {Built-in Function} {} rdivide (@var{x}, @var{y})\n\ | |
3485 This function is equivalent to @code{x ./ y}.\n\ | |
3486 @end deftypefn") | |
3487 { | |
3488 BINARY_OP_DEFUN_BODY (op_el_div); | |
3489 } | |
3490 | |
3491 DEFUN (power, args, , | |
3492 "-*- texinfo -*-\n\ | |
3493 @deftypefn {Built-in Function} {} power (@var{x}, @var{y})\n\ | |
7623 | 3494 This function is equivalent to @code{x .^ y}.\n\ |
6518 | 3495 @end deftypefn") |
3496 { | |
3497 BINARY_OP_DEFUN_BODY (op_el_pow); | |
3498 } | |
3499 | |
3500 DEFUN (ldivide, args, , | |
3501 "-*- texinfo -*-\n\ | |
3502 @deftypefn {Built-in Function} {} ldivide (@var{x}, @var{y})\n\ | |
7623 | 3503 This function is equivalent to @code{x .\\ y}.\n\ |
6518 | 3504 @end deftypefn") |
3505 { | |
3506 BINARY_OP_DEFUN_BODY (op_el_ldiv); | |
3507 } | |
3508 | |
3509 DEFUN (and, args, , | |
3510 "-*- texinfo -*-\n\ | |
3511 @deftypefn {Built-in Function} {} and (@var{x}, @var{y})\n\ | |
3512 This function is equivalent to @code{x & y}.\n\ | |
3513 @end deftypefn") | |
3514 { | |
3515 BINARY_OP_DEFUN_BODY (op_el_and); | |
3516 } | |
3517 | |
3518 DEFUN (or, args, , | |
3519 "-*- texinfo -*-\n\ | |
3520 @deftypefn {Built-in Function} {} or (@var{x}, @var{y})\n\ | |
3521 This function is equivalent to @code{x | y}.\n\ | |
3522 @end deftypefn") | |
3523 { | |
3524 BINARY_OP_DEFUN_BODY (op_el_or); | |
3525 } | |
3526 | |
7065 | 3527 static double tic_toc_timestamp = -1.0; |
7045 | 3528 |
3529 DEFUN (tic, args, nargout, | |
3530 "-*- texinfo -*-\n\ | |
3531 @deftypefn {Built-in Function} {} tic ()\n\ | |
3532 @deftypefnx {Built-in Function} {} toc ()\n\ | |
3533 Set or check a wall-clock timer. Calling @code{tic} without an\n\ | |
3534 output argument sets the timer. Subsequent calls to @code{toc}\n\ | |
3535 return the number of seconds since the timer was set. For example,\n\ | |
3536 \n\ | |
3537 @example\n\ | |
3538 tic ();\n\ | |
3539 # many computations later...\n\ | |
3540 elapsed_time = toc ();\n\ | |
3541 @end example\n\ | |
3542 \n\ | |
3543 @noindent\n\ | |
3544 will set the variable @code{elapsed_time} to the number of seconds since\n\ | |
3545 the most recent call to the function @code{tic}.\n\ | |
3546 \n\ | |
3547 If called with one output argument then this function returns a scalar\n\ | |
3548 of type @code{uint64} and the wall-clock timer is not started.\n\ | |
3549 \n\ | |
3550 @example\n\ | |
3551 @group\n\ | |
3552 t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6\n\ | |
3553 @result{} 5\n\ | |
3554 @end group\n\ | |
3555 @end example\n\ | |
3556 \n\ | |
3557 Nested timing with @code{tic} and @code{toc} is not supported.\n\ | |
3558 Therefore @code{toc} will always return the elapsed time from the most\n\ | |
3559 recent call to @code{tic}.\n\ | |
3560 \n\ | |
3561 If you are more interested in the CPU time that your process used, you\n\ | |
3562 should use the @code{cputime} function instead. The @code{tic} and\n\ | |
3563 @code{toc} functions report the actual wall clock time that elapsed\n\ | |
3564 between the calls. This may include time spent processing other jobs or\n\ | |
3565 doing nothing at all. For example,\n\ | |
3566 \n\ | |
3567 @example\n\ | |
3568 @group\n\ | |
3569 tic (); sleep (5); toc ()\n\ | |
3570 @result{} 5\n\ | |
3571 t = cputime (); sleep (5); cputime () - t\n\ | |
3572 @result{} 0\n\ | |
3573 @end group\n\ | |
3574 @end example\n\ | |
3575 \n\ | |
3576 @noindent\n\ | |
3577 (This example also illustrates that the CPU timer may have a fairly\n\ | |
3578 coarse resolution.)\n\ | |
3579 @end deftypefn") | |
3580 { | |
3581 octave_value retval; | |
7065 | 3582 |
7045 | 3583 int nargin = args.length (); |
3584 | |
3585 if (nargin != 0) | |
3586 warning ("tic: ignoring extra arguments"); | |
3587 | |
7065 | 3588 octave_time now; |
3589 | |
3590 double tmp = now.double_value (); | |
3591 | |
7045 | 3592 if (nargout > 0) |
7065 | 3593 retval = static_cast<octave_uint64> (1e6 * tmp); |
7045 | 3594 else |
7065 | 3595 tic_toc_timestamp = tmp; |
7045 | 3596 |
3597 return retval; | |
3598 } | |
3599 | |
3600 DEFUN (toc, args, nargout, | |
3601 "-*- texinfo -*-\n\ | |
3602 @deftypefn {Built-in Function} {} toc ()\n\ | |
3603 See tic.\n\ | |
3604 @end deftypefn") | |
3605 { | |
3606 octave_value retval; | |
7065 | 3607 |
7045 | 3608 int nargin = args.length (); |
3609 | |
3610 if (nargin != 0) | |
3611 warning ("tic: ignoring extra arguments"); | |
3612 | |
7065 | 3613 if (tic_toc_timestamp < 0) |
7045 | 3614 { |
3615 warning ("toc called before timer set"); | |
3616 if (nargout > 0) | |
7065 | 3617 retval = Matrix (); |
7045 | 3618 } |
3619 else | |
7065 | 3620 { |
3621 octave_time now; | |
3622 | |
3623 double tmp = now.double_value () - tic_toc_timestamp; | |
3624 | |
3625 if (nargout > 0) | |
3626 retval = tmp; | |
3627 else | |
3628 octave_stdout << "Elapsed time is " << tmp << " seconds.\n"; | |
3629 } | |
7045 | 3630 |
3631 return retval; | |
3632 } | |
3633 | |
3634 DEFUN (cputime, args, , | |
3635 "-*- texinfo -*-\n\ | |
3636 @deftypefn {Built-in Function} {[@var{total}, @var{user}, @var{system}] =} cputime ();\n\ | |
3637 Return the CPU time used by your Octave session. The first output is\n\ | |
3638 the total time spent executing your process and is equal to the sum of\n\ | |
3639 second and third outputs, which are the number of CPU seconds spent\n\ | |
3640 executing in user mode and the number of CPU seconds spent executing in\n\ | |
3641 system mode, respectively. If your system does not have a way to report\n\ | |
3642 CPU time usage, @code{cputime} returns 0 for each of its output values.\n\ | |
3643 Note that because Octave used some CPU time to start, it is reasonable\n\ | |
3644 to check to see if @code{cputime} works by checking to see if the total\n\ | |
3645 CPU time used is nonzero.\n\ | |
3646 @end deftypefn") | |
3647 { | |
3648 octave_value_list retval; | |
3649 int nargin = args.length (); | |
3650 double usr = 0.0; | |
3651 double sys = 0.0; | |
3652 | |
3653 if (nargin != 0) | |
3654 warning ("tic: ignoring extra arguments"); | |
3655 | |
3656 #if defined (HAVE_GETRUSAGE) | |
3657 | |
3658 struct rusage ru; | |
3659 | |
3660 getrusage (RUSAGE_SELF, &ru); | |
3661 | |
3662 usr = static_cast<double> (ru.ru_utime.tv_sec) + | |
3663 static_cast<double> (ru.ru_utime.tv_usec) * 1e-6; | |
3664 | |
3665 sys = static_cast<double> (ru.ru_stime.tv_sec) + | |
3666 static_cast<double> (ru.ru_stime.tv_usec) * 1e-6; | |
3667 | |
3668 #elif defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) | |
3669 | |
3670 struct tms t; | |
3671 | |
3672 times (&t); | |
3673 | |
3674 unsigned long ticks; | |
3675 unsigned long seconds; | |
3676 unsigned long fraction; | |
3677 | |
3678 ticks = t.tms_utime + t.tms_cutime; | |
3679 fraction = ticks % HZ; | |
3680 seconds = ticks / HZ; | |
3681 | |
3682 usr = static_cast<double> (seconds) + static_cast<double>(fraction) / | |
3683 static_cast<double>(HZ); | |
3684 | |
3685 ticks = t.tms_stime + t.tms_cstime; | |
3686 fraction = ticks % HZ; | |
3687 seconds = ticks / HZ; | |
3688 | |
3689 sys = static_cast<double> (seconds) + static_cast<double>(fraction) / | |
3690 static_cast<double>(HZ); | |
3691 | |
3692 #elif defined (__WIN32__) | |
7145 | 3693 |
7045 | 3694 HANDLE hProcess = GetCurrentProcess (); |
3695 FILETIME ftCreation, ftExit, ftUser, ftKernel; | |
3696 GetProcessTimes (hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser); | |
3697 | |
3698 int64_t itmp = *(reinterpret_cast<int64_t *> (&ftUser)); | |
7145 | 3699 usr = static_cast<double> (itmp) * 1e-7; |
7045 | 3700 |
3701 itmp = *(reinterpret_cast<int64_t *> (&ftKernel)); | |
7145 | 3702 sys = static_cast<double> (itmp) * 1e-7; |
7045 | 3703 |
3704 #endif | |
3705 | |
3706 retval (2) = sys; | |
3707 retval (1) = usr; | |
3708 retval (0) = sys + usr; | |
3709 | |
3710 return retval; | |
3711 } | |
3712 | |
7433 | 3713 DEFUN (sort, args, nargout, |
3714 "-*- texinfo -*-\n\ | |
3715 @deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})\n\ | |
3716 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim})\n\ | |
3717 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{mode})\n\ | |
3718 @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim}, @var{mode})\n\ | |
3719 Return a copy of @var{x} with the elements arranged in increasing\n\ | |
3720 order. For matrices, @code{sort} orders the elements in each column.\n\ | |
3721 \n\ | |
3722 For example,\n\ | |
3723 \n\ | |
3724 @example\n\ | |
3725 @group\n\ | |
3726 sort ([1, 2; 2, 3; 3, 1])\n\ | |
3727 @result{} 1 1\n\ | |
3728 2 2\n\ | |
3729 3 3\n\ | |
3730 @end group\n\ | |
3731 @end example\n\ | |
3732 \n\ | |
3733 The @code{sort} function may also be used to produce a matrix\n\ | |
3734 containing the original row indices of the elements in the sorted\n\ | |
3735 matrix. For example,\n\ | |
3736 \n\ | |
3737 @example\n\ | |
3738 @group\n\ | |
3739 [s, i] = sort ([1, 2; 2, 3; 3, 1])\n\ | |
3740 @result{} s = 1 1\n\ | |
3741 2 2\n\ | |
3742 3 3\n\ | |
3743 @result{} i = 1 3\n\ | |
3744 2 1\n\ | |
3745 3 2\n\ | |
3746 @end group\n\ | |
3747 @end example\n\ | |
3748 \n\ | |
3749 If the optional argument @var{dim} is given, then the matrix is sorted\n\ | |
3750 along the dimension defined by @var{dim}. The optional argument @code{mode}\n\ | |
3751 defines the order in which the values will be sorted. Valid values of\n\ | |
3752 @code{mode} are `ascend' or `descend'.\n\ | |
3753 \n\ | |
3754 For equal elements, the indices are such that the equal elements are listed\n\ | |
3755 in the order that appeared in the original list.\n\ | |
3756 \n\ | |
3757 The @code{sort} function may also be used to sort strings and cell arrays\n\ | |
3758 of strings, in which case the dictionary order of the strings is used.\n\ | |
3759 \n\ | |
3760 The algorithm used in @code{sort} is optimized for the sorting of partially\n\ | |
3761 ordered lists.\n\ | |
3762 @end deftypefn") | |
3763 { | |
3764 octave_value_list retval; | |
3765 | |
3766 int nargin = args.length (); | |
3767 sortmode smode = ASCENDING; | |
3768 | |
3769 if (nargin < 1 || nargin > 3) | |
3770 { | |
3771 print_usage (); | |
3772 return retval; | |
3773 } | |
3774 | |
3775 bool return_idx = nargout > 1; | |
3776 | |
3777 octave_value arg = args(0); | |
3778 | |
3779 int dim = 0; | |
3780 if (nargin > 1) | |
3781 { | |
3782 if (args(1).is_string ()) | |
3783 { | |
3784 std::string mode = args(1).string_value(); | |
3785 if (mode == "ascend") | |
3786 smode = ASCENDING; | |
3787 else if (mode == "descend") | |
3788 smode = DESCENDING; | |
3789 else | |
3790 { | |
3791 error ("sort: mode must be either \"ascend\" or \"descend\""); | |
3792 return retval; | |
3793 } | |
3794 } | |
3795 else | |
3796 dim = args(1).nint_value () - 1; | |
3797 } | |
3798 | |
3799 if (nargin > 2) | |
3800 { | |
3801 if (args(1).is_string ()) | |
3802 { | |
3803 print_usage (); | |
3804 return retval; | |
3805 } | |
3806 | |
3807 if (! args(2).is_string ()) | |
3808 { | |
3809 error ("sort: mode must be a string"); | |
3810 return retval; | |
3811 } | |
3812 std::string mode = args(2).string_value(); | |
3813 if (mode == "ascend") | |
3814 smode = ASCENDING; | |
3815 else if (mode == "descend") | |
3816 smode = DESCENDING; | |
3817 else | |
3818 { | |
3819 error ("sort: mode must be either \"ascend\" or \"descend\""); | |
3820 return retval; | |
3821 } | |
3822 } | |
3823 | |
3824 dim_vector dv = arg.dims (); | |
3825 if (error_state) | |
3826 { | |
3827 gripe_wrong_type_arg ("sort", arg); | |
3828 return retval; | |
3829 } | |
3830 if (nargin == 1 || args(1).is_string ()) | |
3831 { | |
3832 // Find first non singleton dimension | |
3833 for (int i = 0; i < dv.length (); i++) | |
3834 if (dv(i) > 1) | |
3835 { | |
3836 dim = i; | |
3837 break; | |
3838 } | |
3839 } | |
3840 else | |
3841 { | |
3842 if (dim < 0 || dim > dv.length () - 1) | |
3843 { | |
3844 error ("sort: dim must be a valid dimension"); | |
3845 return retval; | |
3846 } | |
3847 } | |
3848 | |
3849 if (return_idx) | |
3850 { | |
3851 Array<octave_idx_type> sidx; | |
3852 | |
3853 retval (0) = arg.sort (sidx, dim, smode); | |
3854 | |
3855 octave_idx_type *ps = sidx.fortran_vec (); | |
3856 NDArray midx (sidx.dims ()); | |
3857 double *pm = midx.fortran_vec (); | |
3858 | |
3859 for (octave_idx_type i = 0; i < sidx.numel (); i++) | |
3860 pm [i] = static_cast<double> | |
3861 (ps [i] + static_cast<octave_idx_type> (1)); | |
3862 | |
3863 retval (1) = midx; | |
3864 } | |
3865 else | |
3866 retval(0) = arg.sort (dim, smode); | |
3867 | |
3868 return retval; | |
3869 } | |
3870 | |
3871 /* | |
3872 | |
3873 %% Double | |
3874 %!assert (sort ([NaN, 1, -1, 2, Inf]), [-1, 1, 2, Inf, NaN]) | |
3875 %!assert (sort ([NaN, 1, -1, 2, Inf], 1), [NaN, 1, -1, 2, Inf]) | |
3876 %!assert (sort ([NaN, 1, -1, 2, Inf], 2), [-1, 1, 2, Inf, NaN]) | |
3877 %!error (sort ([NaN, 1, -1, 2, Inf], 3)) | |
3878 %!assert (sort ([NaN, 1, -1, 2, Inf], "ascend"), [-1, 1, 2, Inf, NaN]) | |
3879 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "ascend"), [-1, 1, 2, Inf, NaN]) | |
3880 %!assert (sort ([NaN, 1, -1, 2, Inf], "descend"), [NaN, Inf, 2, 1, -1]) | |
3881 %!assert (sort ([NaN, 1, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, 1, -1]) | |
3882 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4]), [3, 1, 6, 4; 8, 2, 7, 5]) | |
3883 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 1), [3, 1, 6, 4; 8, 2, 7, 5]) | |
3884 %!assert (sort ([3, 1, 7, 5; 8, 2, 6, 4], 2), [1, 3, 5, 7; 2, 4, 6, 8]) | |
3885 %!assert (sort (1), 1) | |
3886 | |
3887 %!test | |
3888 %! [v, i] = sort ([NaN, 1, -1, Inf, 1]); | |
3889 %! assert (v, [-1, 1, 1, Inf, NaN]) | |
3890 %! assert (i, [3, 2, 5, 4, 1]) | |
3891 | |
3892 %% Complex | |
3893 %!assert (sort ([NaN, 1i, -1, 2, Inf]), [1i, -1, 2, Inf, NaN]) | |
3894 %!assert (sort ([NaN, 1i, -1, 2, Inf], 1), [NaN, 1i, -1, 2, Inf]) | |
3895 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2), [1i, -1, 2, Inf, NaN]) | |
3896 %!error (sort ([NaN, 1i, -1, 2, Inf], 3)) | |
3897 %!assert (sort ([NaN, 1i, -1, 2, Inf], "ascend"), [1i, -1, 2, Inf, NaN]) | |
3898 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "ascend"), [1i, -1, 2, Inf, NaN]) | |
3899 %!assert (sort ([NaN, 1i, -1, 2, Inf], "descend"), [NaN, Inf, 2, -1, 1i]) | |
3900 %!assert (sort ([NaN, 1i, -1, 2, Inf], 2, "descend"), [NaN, Inf, 2, -1, 1i]) | |
3901 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4]), [3, 1i, 6, 4; 8, 2, 7, 5]) | |
3902 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 1), [3, 1i, 6, 4; 8, 2, 7, 5]) | |
3903 %!assert (sort ([3, 1i, 7, 5; 8, 2, 6, 4], 2), [1i, 3, 5, 7; 2, 4, 6, 8]) | |
3904 %!assert (sort (1i), 1i) | |
3905 | |
3906 %!test | |
3907 %! [v, i] = sort ([NaN, 1i, -1, Inf, 1, 1i]); | |
3908 %! assert (v, [1, 1i, 1i, -1, Inf, NaN]) | |
3909 %! assert (i, [5, 2, 6, 3, 4, 1]) | |
3910 | |
3911 %% Bool | |
3912 %!assert (sort ([true, false, true, false]), [false, false, true, true]) | |
3913 %!assert (sort ([true, false, true, false], 1), [true, false, true, false]) | |
3914 %!assert (sort ([true, false, true, false], 2), [false, false, true, true]) | |
3915 %!error (sort ([true, false, true, false], 3)) | |
3916 %!assert (sort ([true, false, true, false], "ascend"), [false, false, true, true]) | |
3917 %!assert (sort ([true, false, true, false], 2, "ascend"), [false, false, true, true]) | |
3918 %!assert (sort ([true, false, true, false], "descend"), [true, true, false, false]) | |
3919 %!assert (sort ([true, false, true, false], 2, "descend"), [true, true, false, false]) | |
3920 %!assert (sort (true), true) | |
3921 | |
3922 %!test | |
3923 %! [v, i] = sort ([true, false, true, false]); | |
3924 %! assert (v, [false, false, true, true]) | |
3925 %! assert (i, [2, 4, 1, 3]) | |
3926 | |
3927 %% Sparse Double | |
3928 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf])), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
3929 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1, 0, -1, 2, Inf])) | |
3930 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
3931 %!error (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 3)) | |
3932 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
3933 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "ascend"), sparse ([-1, 0, 0, 1, 2, Inf, NaN])) | |
3934 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) | |
3935 %!assert (sort (sparse ([0, NaN, 1, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, 1, 0, 0, -1])) | |
3936 | |
3937 %!shared a | |
3938 %! a = randn (10, 10); | |
3939 %! a (a < 0) = 0; | |
3940 %!assert (sort (sparse (a)), sparse (sort (a))) | |
3941 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) | |
3942 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) | |
3943 %!test | |
3944 %! [v, i] = sort (a); | |
3945 %! [vs, is] = sort (sparse (a)); | |
3946 %! assert (vs, sparse (v)) | |
3947 %! assert (is, i) | |
3948 | |
3949 %% Sparse Complex | |
3950 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf])), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
3951 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 1), sparse ([0, NaN, 1i, 0, -1, 2, Inf])) | |
3952 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
3953 %!error (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 3)) | |
3954 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
3955 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "ascend"), sparse ([0, 0, 1i, -1, 2, Inf, NaN])) | |
3956 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) | |
3957 %!assert (sort (sparse ([0, NaN, 1i, 0, -1, 2, Inf]), 2, "descend"), sparse ([NaN, Inf, 2, -1, 1i, 0, 0])) | |
3958 | |
3959 %!shared a | |
3960 %! a = randn (10, 10); | |
3961 %! a (a < 0) = 0; | |
3962 %! a = 1i * a; | |
3963 %!assert (sort (sparse (a)), sparse (sort (a))) | |
3964 %!assert (sort (sparse (a), 1), sparse (sort (a, 1))) | |
3965 %!assert (sort (sparse (a), 2), sparse (sort (a, 2))) | |
3966 %!test | |
3967 %! [v, i] = sort (a); | |
3968 %! [vs, is] = sort (sparse (a)); | |
3969 %! assert (vs, sparse (v)) | |
3970 %! assert (is, i) | |
3971 | |
3972 %% Sparse Bool | |
3973 %!assert (sort (sparse ([true, false, true, false])), sparse ([false, false, true, true])) | |
3974 %!assert (sort (sparse([true, false, true, false]), 1), sparse ([true, false, true, false])) | |
3975 %!assert (sort (sparse ([true, false, true, false]), 2), sparse ([false, false, true, true])) | |
3976 %!error (sort (sparse ([true, false, true, false], 3))) | |
3977 %!assert (sort (sparse ([true, false, true, false]), "ascend"), sparse([false, false, true, true])) | |
3978 %!assert (sort (sparse ([true, false, true, false]), 2, "ascend"), sparse([false, false, true, true])) | |
3979 %!assert (sort (sparse ([true, false, true, false]), "descend"), sparse ([true, true, false, false])) | |
3980 %!assert (sort (sparse ([true, false, true, false]), 2, "descend"), sparse([true, true, false, false])) | |
3981 | |
3982 %!test | |
3983 %! [v, i] = sort (sparse([true, false, true, false])); | |
3984 %! assert (v, sparse([false, false, true, true])) | |
3985 %! assert (i, [2, 4, 1, 3]) | |
3986 | |
3987 %% Cell string array | |
3988 %!shared a, b, c | |
3989 %! a = {"Alice", "Cecile", "Eric", "Barry", "David"}; | |
3990 %! b = {"Alice", "Barry", "Cecile", "David", "Eric"}; | |
3991 %! c = {"Eric", "David", "Cecile", "Barry", "Alice"}; | |
3992 %!assert (sort (a), b); | |
3993 %!assert (sort (a, 1), a) | |
3994 %!assert (sort (a, 2), b) | |
3995 %!error (sort (a, 3)) | |
3996 %!assert (sort (a, "ascend"), b) | |
3997 %!assert (sort (a, 2, "ascend"), b) | |
3998 %!assert (sort (a, "descend"), c) | |
3999 %!assert (sort (a, 2, "descend"), c) | |
4000 | |
4001 %!test | |
4002 %! [v, i] = sort (a); | |
4003 %! assert (i, [1, 4, 2, 5, 3]) | |
4004 | |
4005 */ | |
4006 | |
523 | 4007 /* |
4008 ;;; Local Variables: *** | |
4009 ;;; mode: C++ *** | |
4010 ;;; End: *** | |
4011 */ |