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