523
|
1 // data.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
523
|
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 |
1192
|
33 #include <config.h> |
523
|
34 #endif |
|
35 |
|
36 #include "tree-const.h" |
|
37 #include "user-prefs.h" |
1216
|
38 #include "oct-map.h" |
565
|
39 #include "help.h" |
523
|
40 #include "utils.h" |
|
41 #include "error.h" |
777
|
42 #include "gripes.h" |
523
|
43 #include "defun.h" |
|
44 |
|
45 #ifndef MIN |
|
46 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
47 #endif |
|
48 |
767
|
49 #ifndef ABS |
|
50 #define ABS(x) (((x) < 0) ? (-x) : (x)) |
|
51 #endif |
|
52 |
712
|
53 DEFUN ("all", Fall, Sall, 1, 1, |
523
|
54 "all (X): are all elements of X nonzero?") |
|
55 { |
|
56 Octave_object retval; |
|
57 |
|
58 int nargin = args.length (); |
|
59 |
712
|
60 if (nargin == 1 && args(0).is_defined ()) |
|
61 retval = args(0).all (); |
|
62 else |
523
|
63 print_usage ("all"); |
|
64 |
|
65 return retval; |
|
66 } |
|
67 |
712
|
68 DEFUN ("any", Fany, Sany, 1, 1, |
523
|
69 "any (X): are any elements of X nonzero?") |
|
70 { |
|
71 Octave_object retval; |
|
72 |
|
73 int nargin = args.length (); |
|
74 |
712
|
75 if (nargin == 1 && args(0).is_defined ()) |
|
76 retval = args(0).any (); |
|
77 else |
523
|
78 print_usage ("any"); |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
649
|
83 // These mapping functions may also be useful in other places, eh? |
|
84 |
|
85 typedef double (*d_dd_fcn) (double, double); |
|
86 |
|
87 static Matrix |
|
88 map (d_dd_fcn f, double x, const Matrix& y) |
|
89 { |
|
90 int nr = y.rows (); |
|
91 int nc = y.columns (); |
|
92 |
|
93 Matrix retval (nr, nc); |
|
94 |
|
95 for (int j = 0; j < nc; j++) |
|
96 for (int i = 0; i < nr; i++) |
|
97 retval.elem (i, j) = f (x, y.elem (i, j)); |
|
98 |
|
99 return retval; |
|
100 } |
|
101 |
|
102 static Matrix |
|
103 map (d_dd_fcn f, const Matrix& x, double y) |
|
104 { |
|
105 int nr = x.rows (); |
|
106 int nc = x.columns (); |
|
107 |
|
108 Matrix retval (nr, nc); |
|
109 |
|
110 for (int j = 0; j < nc; j++) |
|
111 for (int i = 0; i < nr; i++) |
|
112 retval.elem (i, j) = f (x.elem (i, j), y); |
|
113 |
|
114 return retval; |
|
115 } |
|
116 |
|
117 static Matrix |
|
118 map (d_dd_fcn f, const Matrix& x, const Matrix& y) |
|
119 { |
|
120 int x_nr = x.rows (); |
|
121 int x_nc = x.columns (); |
|
122 |
|
123 int y_nr = y.rows (); |
|
124 int y_nc = y.columns (); |
|
125 |
719
|
126 assert (x_nr == y_nr && x_nc == y_nc); |
649
|
127 |
|
128 Matrix retval (x_nr, x_nc); |
|
129 |
|
130 for (int j = 0; j < x_nc; j++) |
|
131 for (int i = 0; i < x_nr; i++) |
|
132 retval.elem (i, j) = f (x.elem (i, j), y.elem (i, j)); |
|
133 |
|
134 return retval; |
|
135 } |
|
136 |
712
|
137 DEFUN ("atan2", Fatan2, Satan2, 2, 1, |
649
|
138 "atan2 (Y, X): atan (Y / X) in range -pi to pi") |
|
139 { |
|
140 Octave_object retval; |
|
141 |
712
|
142 int nargin = args.length (); |
|
143 |
|
144 if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
649
|
145 { |
712
|
146 tree_constant arg_y = args(0); |
|
147 tree_constant arg_x = args(1); |
649
|
148 |
|
149 int y_nr = arg_y.rows (); |
|
150 int y_nc = arg_y.columns (); |
|
151 |
|
152 int x_nr = arg_x.rows (); |
|
153 int x_nc = arg_x.columns (); |
|
154 |
|
155 int arg_y_empty = empty_arg ("atan2", y_nr, y_nc); |
|
156 int arg_x_empty = empty_arg ("atan2", x_nr, x_nc); |
|
157 |
719
|
158 if (arg_y_empty > 0 && arg_x_empty > 0) |
|
159 return Matrix (); |
|
160 else if (arg_y_empty || arg_x_empty) |
649
|
161 return retval; |
|
162 |
|
163 int y_is_scalar = (y_nr == 1 && y_nc == 1); |
|
164 int x_is_scalar = (x_nr == 1 && x_nc == 1); |
|
165 |
|
166 if (y_is_scalar && x_is_scalar) |
|
167 { |
|
168 double y = arg_y.double_value (); |
|
169 |
|
170 if (! error_state) |
|
171 { |
|
172 double x = arg_x.double_value (); |
|
173 |
|
174 if (! error_state) |
|
175 retval = atan2 (y, x); |
|
176 } |
|
177 } |
|
178 else if (y_is_scalar) |
|
|