523
|
1 // data.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 /* |
|
25 |
|
26 The function builtin_pwd adapted from a similar function from GNU |
|
27 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
28 Software Foundation, Inc. |
|
29 |
|
30 */ |
|
31 |
|
32 #ifdef HAVE_CONFIG_H |
|
33 #include "config.h" |
|
34 #endif |
|
35 |
|
36 #include "tree-const.h" |
|
37 #include "user-prefs.h" |
565
|
38 #include "help.h" |
523
|
39 #include "utils.h" |
|
40 #include "error.h" |
|
41 #include "defun.h" |
|
42 |
|
43 #ifndef MIN |
|
44 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
45 #endif |
|
46 |
|
47 DEFUN ("all", Fall, Sall, 2, 1, |
|
48 "all (X): are all elements of X nonzero?") |
|
49 { |
|
50 Octave_object retval; |
|
51 |
|
52 int nargin = args.length (); |
|
53 |
|
54 if (nargin != 2) |
|
55 print_usage ("all"); |
|
56 else if (nargin > 0 && args(1).is_defined ()) |
|
57 retval = args(1).all (); |
|
58 |
|
59 return retval; |
|
60 } |
|
61 |
|
62 DEFUN ("any", Fany, Sany, 2, 1, |
|
63 "any (X): are any elements of X nonzero?") |
|
64 { |
|
65 Octave_object retval; |
|
66 |
|
67 int nargin = args.length (); |
|
68 |
|
69 if (nargin != 2) |
|
70 print_usage ("any"); |
|
71 else if (nargin > 0 && args(1).is_defined ()) |
|
72 retval = args(1).any (); |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
|
77 DEFUN ("cumprod", Fcumprod, Scumprod, 2, 1, |
|
78 "cumprod (X): cumulative products") |
|
79 { |
|
80 Octave_object retval; |
|
81 |
|
82 int nargin = args.length (); |
|
83 |
|
84 if (nargin != 2) |
|
85 print_usage ("cumprod"); |
|
86 else if (nargin > 0 && args(1).is_defined ()) |
|
87 retval = args(1).cumprod (); |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
|
92 DEFUN ("cumsum", Fcumsum, Scumsum, 2, 1, |
|
93 "cumsum (X): cumulative sums") |
|
94 { |
|
95 Octave_object retval; |
|
96 |
|
97 int nargin = args.length (); |
|
98 |
|
99 if (nargin != 2) |
|
100 print_usage ("cumsum"); |
|
101 else if (nargin > 0 && args(1).is_defined ()) |
|
102 retval = args(1).cumsum (); |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
|
107 DEFUN ("diag", Fdiag, Sdiag, 3, 1, |
|
108 "diag (X [,k]): form/extract diagonals") |
|
109 { |
|
110 Octave_object retval; |
|
111 |
|
112 int nargin = args.length (); |
|
113 |
|
114 if (nargin == 2) |
|
115 retval = args(1).diag (); |
|
116 else if (nargin == 3) |
|
117 retval = args(1).diag (args(2)); |
|
118 else |
|
119 print_usage ("diag"); |
|
120 |
|
121 return retval; |
|
122 } |
|
123 |
|
124 DEFUN ("isstr", Fisstr, Sisstr, 2, 1, |
|
125 "isstr (X): return 1 if X is a string, 0 otherwise") |
|
126 { |
|
127 Octave_object retval; |
|
128 |
|
129 int nargin = args.length (); |
|
130 |
|
131 if (nargin != 2) |
|
132 print_usage ("isstr"); |
|
133 else |
|
134 { |
|
135 if (nargin > 0 && args(1).is_defined ()) |
|
136 retval = args(1).isstr (); |
|
137 } |
|
138 |
|
139 return retval; |
|
140 } |
|
141 |
|
142 DEFUN ("prod", Fprod, Sprod, 2, 1, |
|
143 "prod (X): products") |
|
144 { |
|
145 Octave_object retval; |
|
146 |
|
147 int nargin = args.length (); |
|
148 |
|
149 if (nargin != 2) |
|
150 print_usage ("prod"); |
|
151 else if (nargin > 0 && args(1).is_defined ()) |
|
152 retval = args(1).prod (); |
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 DEFUN ("setstr", Fsetstr, Ssetstr, 2, 1, |
|
158 "setstr (V): convert a vector to a string") |
|
159 { |
|
160 Octave_object retval; |
|
161 |
|
162 int nargin = args.length (); |
|
163 |
|
164 if (nargin == 2) |
|
165 retval = args(1).convert_to_str (); |
|
166 else |
|
167 print_usage ("setstr"); |
|
168 |
|
169 return retval; |
|
170 } |
|
171 |
|
172 DEFUN ("size", Fsize, Ssize, 2, 1, |
|
173 "[m, n] = size (x): return rows and columns of X") |
|
174 { |
|
175 Octave_object retval; |
|
176 |
|
177 int nargin = args.length (); |
|
178 |
|
179 if (nargin != 2) |
|
180 print_usage ("size"); |
|
181 else |
|
182 { |
|
183 if (nargin > 0 && args(1).is_defined ()) |
|
184 { |
|
185 int nr = args(1).rows (); |
|
186 int nc = args(1).columns (); |
|
187 if (nargout == 0 || nargout == 1) |
|
188 { |
|
189 Matrix m (1, 2); |
|
190 m.elem (0, 0) = nr; |
|
191 m.elem (0, 1) = nc; |
|
192 retval = m; |
|
193 } |
|
194 else if (nargout == 2) |
|
195 { |
|
196 retval(1) = (double) nc; |
|
197 retval(0) = (double) nr; |
|
198 } |
|
199 else |
|
200 print_usage ("size"); |
|
201 } |
|
202 } |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 DEFUN ("sum", Fsum, Ssum, 2, 1, |
|
208 "sum (X): sum of elements") |
|
209 { |
|
210 Octave_object retval; |
|
211 |
|
212 int nargin = args.length (); |
|
213 |
|
214 if (nargin != 2) |
|
215 print_usage ("sum"); |
|
216 else |
|
217 { |
|
218 if (nargin > 0 && args(1).is_defined ()) |
|
219 retval = args(1).sum (); |
|
220 } |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
|
225 DEFUN ("sumsq", Fsumsq, Ssumsq, 2, 1, |
|
226 "sumsq (X): sum of squares of elements") |
|
227 { |
|
228 Octave_object retval; |
|
229 |
|
230 int nargin = args.length (); |
|
231 |
|
232 if (nargin != 2) |
|
233 print_usage ("sumsq"); |
|
234 else if (nargin > 0 && args(1).is_defined ()) |
|
235 retval = args(1).sumsq (); |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
|
240 static void |
|
241 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
242 { |
|
243 if (nr < 0 || nc < 0) |
|
244 { |
|
245 if (user_pref.treat_neg_dim_as_zero) |
597
|
246 { |
|
247 nr = (nr < 0) ? 0 : nr; |
|
248 nc = (nc < 0) ? 0 : nc; |
|
249 } |
523
|
250 else |
|
251 error ("%s: can't create a matrix with negative dimensions", |
|
252 warnfor); |
|
253 } |
|
254 } |
|
255 |
|
256 static void |
|
257 get_dimensions (const tree_constant& a, const char *warn_for, |
|
258 int& nr, int& nc) |
|
259 { |
|
260 tree_constant tmpa = a.make_numeric (); |
|
261 |
|
262 if (tmpa.is_scalar_type ()) |
|
263 { |
|
264 double tmp = tmpa.double_value (); |
|
265 nr = nc = NINT (tmp); |
|
266 } |
|
267 else |
|
268 { |
|
269 nr = tmpa.rows (); |
|
270 nc = tmpa.columns (); |
|
271 |
|
272 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
273 { |
|
274 ColumnVector v = tmpa.to_vector (); |
|
275 |
|
276 nr = NINT (v.elem (0)); |
|
277 nc = NINT (v.elem (1)); |
|
278 } |
|
279 else |
|
280 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
281 } |
|
282 |
|
283 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
284 } |
|
285 |
|
286 static void |
|
287 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
288 const char *warn_for, int& nr, int& nc) |
|
289 { |
|
290 tree_constant tmpa = a.make_numeric (); |
|
291 tree_constant tmpb = b.make_numeric (); |
|
292 |
|
293 if (tmpa.is_scalar_type () && tmpb.is_scalar_type ()) |
|
294 { |
|
295 nr = NINT (tmpa.double_value ()); |
|
296 nc = NINT (tmpb.double_value ()); |
|
297 |
|
298 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
299 } |
|
300 else |
|
301 error ("%s: expecting two scalar arguments", warn_for); |
|
302 } |
|
303 |
|
304 static tree_constant |
|
305 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
306 { |
|
307 int nr, nc; |
|
308 get_dimensions (a, warn_for, nr, nc); |
|
309 |
|
310 if (error_state) |
|
311 return tree_constant (); |
|
312 |
|
313 Matrix m (nr, nc, val); |
|
314 |
|
315 return m; |
|
316 } |
|
317 |
|
318 static tree_constant |
|
319 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
320 double val, const char *warn_for) |
|
321 { |
|
322 int nr, nc; |
|
323 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
324 |
|
325 if (error_state) |
|
326 return tree_constant (); |
|
327 |
|
328 Matrix m (nr, nc, val); |
|
329 |
|
330 return m; |
|
331 } |
|
332 |
|
333 DEFUN ("ones", Fones, Sones, 3, 1, |
|
334 "ones (N), ones (N, M), ones (X): create a matrix of all ones") |
|
335 { |
|
336 Octave_object retval; |
|
337 |
|
338 int nargin = args.length (); |
|
339 |
|
340 switch (nargin) |
|
341 { |
|
342 case 2: |
|
343 retval = fill_matrix (args(1), 1.0, "ones"); |
|
344 break; |
|
345 case 3: |
|
346 retval = fill_matrix (args(1), args(2), 1.0, "ones"); |
|
347 break; |
|
348 default: |
|
349 print_usage ("ones"); |
|
350 break; |
|
351 } |
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
|
356 DEFUN ("zeros", Fzeros, Szeros, 3, 1, |
|
357 "zeros (N), zeros (N, M), zeros (X): create a matrix of all zeros") |
|
358 { |
|
359 Octave_object retval; |
|
360 |
|
361 int nargin = args.length (); |
|
362 |
|
363 switch (nargin) |
|
364 { |
|
365 case 2: |
|
366 retval = fill_matrix (args(1), 0.0, "zeros"); |
|
367 break; |
|
368 case 3: |
|
369 retval = fill_matrix (args(1), args(2), 0.0, "zeros"); |
|
370 break; |
|
371 default: |
|
372 print_usage ("zeros"); |
|
373 break; |
|
374 } |
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
|
379 static tree_constant |
|
380 identity_matrix (const tree_constant& a) |
|
381 { |
|
382 int nr, nc; |
|
383 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
384 |
|
385 if (error_state) |
|
386 return tree_constant (); |
|
387 |
|
388 Matrix m (nr, nc, 0.0); |
|
389 |
|
390 if (nr > 0 && nc > 0) |
|
391 { |
|
392 int n = MIN (nr, nc); |
|
393 for (int i = 0; i < n; i++) |
|
394 m.elem (i, i) = 1.0; |
|
395 } |
|
396 |
|
397 return m; |
|
398 } |
|
399 |
|
400 static tree_constant |
|
401 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
402 { |
|
403 int nr, nc; |
|
404 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
405 |
|
406 if (error_state) |
|
407 return tree_constant (); |
|
408 |
|
409 Matrix m (nr, nc, 0.0); |
|
410 |
|
411 if (nr > 0 && nc > 0) |
|
412 { |
|
413 int n = MIN (nr, nc); |
|
414 for (int i = 0; i < n; i++) |
|
415 m.elem (i, i) = 1.0; |
|
416 } |
|
417 |
|
418 return m; |
|
419 } |
|
420 |
|
421 DEFUN ("eye", Feye, Seye, 3, 1, |
|
422 "eye (N), eye (N, M), eye (X): create an identity matrix") |
|
423 { |
|
424 Octave_object retval; |
|
425 |
|
426 int nargin = args.length (); |
|
427 |
|
428 switch (nargin) |
|
429 { |
|
430 case 2: |
|
431 retval = identity_matrix (args(1)); |
|
432 break; |
|
433 case 3: |
|
434 retval = identity_matrix (args(1), args(2)); |
|
435 break; |
|
436 default: |
|
437 print_usage ("eye"); |
|
438 break; |
|
439 } |
|
440 |
|
441 return retval; |
|
442 } |
|
443 |
|
444 /* |
|
445 ;;; Local Variables: *** |
|
446 ;;; mode: C++ *** |
|
447 ;;; page-delimiter: "^/\\*" *** |
|
448 ;;; End: *** |
|
449 */ |