Mercurial > hg > octave-lyh
comparison examples/make_int.cc @ 2391:b12625d6fbcd
[project @ 1996-10-12 19:35:37 by jwe]
author | jwe |
---|---|
date | Sat, 12 Oct 1996 19:43:12 +0000 |
parents | |
children | 8b262e771614 |
comparison
equal
deleted
inserted
replaced
2390:c2c1482c34c8 | 2391:b12625d6fbcd |
---|---|
1 /* | |
2 | |
3 Copyright (C) 1996 John W. Eaton | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, write to the Free | |
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | |
21 */ | |
22 | |
23 #include <octave/config.h> | |
24 | |
25 #include <cstdlib> | |
26 | |
27 #include <string> | |
28 | |
29 class ostream; | |
30 | |
31 #include <octave/lo-utils.h> | |
32 #include <octave/mx-base.h> | |
33 #include <octave/str-vec.h> | |
34 | |
35 #include <octave/defun-dld.h> | |
36 #include <octave/error.h> | |
37 #include <octave/gripes.h> | |
38 #include <octave/mappers.h> | |
39 #include <octave/oct-obj.h> | |
40 #include <octave/ops.h> | |
41 #include <octave/ov-base.h> | |
42 #include <octave/ov-typeinfo.h> | |
43 #include <octave/ov.h> | |
44 #include <octave/ov-scalar.h> | |
45 #include <octave/pager.h> | |
46 #include <octave/pr-output.h> | |
47 #include <octave/symtab.h> | |
48 #include <octave/variables.h> | |
49 | |
50 class Octave_map; | |
51 class octave_value_list; | |
52 | |
53 class tree_walker; | |
54 | |
55 // Integer values. | |
56 | |
57 class | |
58 octave_integer : public octave_base_value | |
59 { | |
60 public: | |
61 | |
62 octave_integer (void) | |
63 : octave_base_value (), scalar (0) { } | |
64 | |
65 octave_integer (int i) | |
66 : octave_base_value (), scalar (i) { } | |
67 | |
68 octave_integer (const octave_integer& s) | |
69 : octave_base_value (), scalar (s.scalar) { } | |
70 | |
71 ~octave_integer (void) { } | |
72 | |
73 octave_value *clone (void) { return new octave_integer (*this); } | |
74 | |
75 #if 0 | |
76 void *operator new (size_t size); | |
77 void operator delete (void *p, size_t size); | |
78 #endif | |
79 | |
80 idx_vector index_vector (void) const { return idx_vector ((double) scalar); } | |
81 | |
82 int rows (void) const { return 1; } | |
83 int columns (void) const { return 1; } | |
84 | |
85 bool is_defined (void) const { return true; } | |
86 bool is_real_scalar (void) const { return true; } | |
87 | |
88 octave_value all (void) const { return (scalar != 0); } | |
89 octave_value any (void) const { return (scalar != 0); } | |
90 | |
91 bool is_real_type (void) const { return true; } | |
92 bool is_scalar_type (void) const { return true; } | |
93 bool is_numeric_type (void) const { return true; } | |
94 | |
95 bool valid_as_scalar_index (void) const | |
96 { return scalar == 1; } | |
97 | |
98 bool valid_as_zero_index (void) const | |
99 { return scalar == 0; } | |
100 | |
101 bool is_true (void) const { return (scalar != 0); } | |
102 | |
103 double double_value (bool = false) const { return (double) scalar; } | |
104 | |
105 int integer_value (bool = false) const { return scalar; } | |
106 | |
107 Matrix matrix_value (bool = false) const { return Matrix (1, 1, scalar); } | |
108 | |
109 Complex complex_value (bool = false) const { return scalar; } | |
110 | |
111 ComplexMatrix complex_matrix_value (bool = false) const | |
112 { return ComplexMatrix (1, 1, Complex (scalar)); } | |
113 | |
114 octave_value not (void) const { return octave_value (! scalar); } | |
115 | |
116 octave_value uminus (void) const { return octave_value (- scalar); } | |
117 | |
118 octave_value transpose (void) const { return octave_value (scalar); } | |
119 | |
120 octave_value hermitian (void) const { return octave_value (scalar); } | |
121 | |
122 void increment (void) { ++scalar; } | |
123 | |
124 void decrement (void) { --scalar; } | |
125 | |
126 void print (ostream& os); | |
127 | |
128 int type_id (void) const { return t_id; } | |
129 | |
130 string type_name (void) const { return t_name; } | |
131 | |
132 static int static_type_id (void) { return t_id; } | |
133 | |
134 static void register_type (void) | |
135 { t_id = octave_value_typeinfo::register_type (t_name); } | |
136 | |
137 private: | |
138 | |
139 int scalar; | |
140 | |
141 static int t_id; | |
142 | |
143 static const string t_name; | |
144 }; | |
145 | |
146 int octave_integer::t_id = -1; | |
147 | |
148 const string octave_integer::t_name ("integer"); | |
149 | |
150 void | |
151 octave_integer::print (ostream& os) | |
152 { | |
153 octave_print_internal (os, scalar, false); | |
154 } | |
155 | |
156 // integer by integer ops. | |
157 | |
158 static octave_value | |
159 add (const octave_value& a1, const octave_value& a2) | |
160 { | |
161 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
162 | |
163 return new octave_integer (v1.integer_value () + v2.integer_value ()); | |
164 } | |
165 | |
166 static octave_value | |
167 sub (const octave_value& a1, const octave_value& a2) | |
168 { | |
169 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
170 | |
171 return new octave_integer (v1.integer_value () - v2.integer_value ()); | |
172 } | |
173 | |
174 static octave_value | |
175 mul (const octave_value& a1, const octave_value& a2) | |
176 { | |
177 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
178 | |
179 return new octave_integer (v1.integer_value () * v2.integer_value ()); | |
180 } | |
181 | |
182 static octave_value | |
183 div (const octave_value& a1, const octave_value& a2) | |
184 { | |
185 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
186 | |
187 int d = v2.integer_value (); | |
188 | |
189 if (d == 0) | |
190 gripe_divide_by_zero (); | |
191 | |
192 return new octave_integer (v1.integer_value () / d); | |
193 } | |
194 | |
195 static octave_value | |
196 i_s_div (const octave_value& a1, const octave_value& a2) | |
197 { | |
198 CAST_BINOP_ARGS (const octave_integer&, const octave_scalar&); | |
199 | |
200 double d = v2.double_value (); | |
201 | |
202 if (d == 0.0) | |
203 gripe_divide_by_zero (); | |
204 | |
205 return new octave_scalar (v1.double_value () / d); | |
206 } | |
207 | |
208 static octave_value | |
209 ldiv (const octave_value& a1, const octave_value& a2) | |
210 { | |
211 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
212 | |
213 int d = v1.integer_value (); | |
214 | |
215 if (d == 0) | |
216 gripe_divide_by_zero (); | |
217 | |
218 return new octave_integer (v2.integer_value () / d); | |
219 } | |
220 | |
221 static octave_value | |
222 lt (const octave_value& a1, const octave_value& a2) | |
223 { | |
224 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
225 | |
226 return new octave_integer (v1.integer_value () < v2.integer_value ()); | |
227 } | |
228 | |
229 static octave_value | |
230 le (const octave_value& a1, const octave_value& a2) | |
231 { | |
232 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
233 | |
234 return new octave_integer (v1.integer_value () <= v2.integer_value ()); | |
235 } | |
236 | |
237 static octave_value | |
238 eq (const octave_value& a1, const octave_value& a2) | |
239 { | |
240 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
241 | |
242 return new octave_integer (v1.integer_value () == v2.integer_value ()); | |
243 } | |
244 | |
245 static octave_value | |
246 ge (const octave_value& a1, const octave_value& a2) | |
247 { | |
248 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
249 | |
250 return new octave_integer (v1.integer_value () >= v2.integer_value ()); | |
251 } | |
252 | |
253 static octave_value | |
254 gt (const octave_value& a1, const octave_value& a2) | |
255 { | |
256 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
257 | |
258 return new octave_integer (v1.integer_value () > v2.integer_value ()); | |
259 } | |
260 | |
261 static octave_value | |
262 ne (const octave_value& a1, const octave_value& a2) | |
263 { | |
264 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
265 | |
266 return new octave_integer (v1.integer_value () != v2.integer_value ()); | |
267 } | |
268 | |
269 static octave_value | |
270 el_mul (const octave_value& a1, const octave_value& a2) | |
271 { | |
272 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
273 | |
274 return new octave_integer (v1.integer_value () * v2.integer_value ()); | |
275 } | |
276 | |
277 static octave_value | |
278 el_div (const octave_value& a1, const octave_value& a2) | |
279 { | |
280 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
281 | |
282 int d = v2.integer_value (); | |
283 | |
284 if (d == 0) | |
285 gripe_divide_by_zero (); | |
286 | |
287 return new octave_integer (v1.integer_value () / d); | |
288 } | |
289 | |
290 static octave_value | |
291 el_ldiv (const octave_value& a1, const octave_value& a2) | |
292 { | |
293 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
294 | |
295 int d = v1.integer_value (); | |
296 | |
297 if (d == 0) | |
298 gripe_divide_by_zero (); | |
299 | |
300 return new octave_integer (v2.integer_value () / d); | |
301 } | |
302 | |
303 static octave_value | |
304 el_and (const octave_value& a1, const octave_value& a2) | |
305 { | |
306 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
307 | |
308 return new octave_integer (v1.integer_value () && v2.integer_value ()); | |
309 } | |
310 | |
311 static octave_value | |
312 el_or (const octave_value& a1, const octave_value& a2) | |
313 { | |
314 CAST_BINOP_ARGS (const octave_integer&, const octave_integer&); | |
315 | |
316 return new octave_integer (v1.integer_value () || v2.integer_value ()); | |
317 } | |
318 | |
319 DEFUN_DLD (make_int, args, , | |
320 "int_val = make_int (val)\n\ | |
321 \n\ | |
322 Creates an integer variable from VAL.") | |
323 { | |
324 static bool type_loaded = false; | |
325 | |
326 if (! type_loaded) | |
327 { | |
328 octave_integer::register_type (); | |
329 | |
330 cerr << "installing integer type at type-id = " | |
331 << octave_integer::static_type_id () << "\n"; | |
332 | |
333 INSTALL_BINOP (add, octave_integer, octave_integer, add); | |
334 INSTALL_BINOP (sub, octave_integer, octave_integer, sub); | |
335 INSTALL_BINOP (mul, octave_integer, octave_integer, mul); | |
336 INSTALL_BINOP (div, octave_integer, octave_integer, div); | |
337 INSTALL_BINOP (ldiv, octave_integer, octave_integer, ldiv); | |
338 INSTALL_BINOP (lt, octave_integer, octave_integer, lt); | |
339 INSTALL_BINOP (le, octave_integer, octave_integer, le); | |
340 INSTALL_BINOP (eq, octave_integer, octave_integer, eq); | |
341 INSTALL_BINOP (ge, octave_integer, octave_integer, ge); | |
342 INSTALL_BINOP (gt, octave_integer, octave_integer, gt); | |
343 INSTALL_BINOP (ne, octave_integer, octave_integer, ne); | |
344 INSTALL_BINOP (el_mul, octave_integer, octave_integer, el_mul); | |
345 INSTALL_BINOP (el_div, octave_integer, octave_integer, el_div); | |
346 INSTALL_BINOP (el_ldiv, octave_integer, octave_integer, el_ldiv); | |
347 INSTALL_BINOP (el_and, octave_integer, octave_integer, el_and); | |
348 INSTALL_BINOP (el_or, octave_integer, octave_integer, el_or); | |
349 | |
350 INSTALL_BINOP (div, octave_integer, octave_scalar, i_s_div); | |
351 } | |
352 | |
353 octave_value retval; | |
354 | |
355 if (args.length () == 1) | |
356 { | |
357 double d = args(0).double_value (); | |
358 | |
359 if (! error_state) | |
360 retval = octave_value (new octave_integer (NINT (d))); | |
361 } | |
362 else | |
363 usage ("make_int"); | |
364 | |
365 return retval; | |
366 } | |
367 | |
368 /* | |
369 ;;; Local Variables: *** | |
370 ;;; mode: C++ *** | |
371 ;;; End: *** | |
372 */ |