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