2376
|
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 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "defun.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "ov-scalar.h" |
2423
|
35 #include "ov-re-mat.h" |
2376
|
36 #include "ov-typeinfo.h" |
|
37 #include "pr-output.h" |
|
38 #include "xdiv.h" |
|
39 #include "xpow.h" |
|
40 |
|
41 int octave_scalar::t_id = -1; |
|
42 |
|
43 const string octave_scalar::t_name ("scalar"); |
|
44 |
|
45 static inline bool |
|
46 valid_scalar_indices (const octave_value_list& args) |
|
47 { |
|
48 int nargin = args.length (); |
|
49 |
|
50 for (int i = 0; i < nargin; i++) |
|
51 if (! args(i).valid_as_scalar_index ()) |
|
52 return false; |
|
53 |
|
54 return true; |
|
55 } |
|
56 |
|
57 octave_value |
|
58 octave_scalar::index (const octave_value_list& idx) const |
|
59 { |
|
60 octave_value retval; |
|
61 |
|
62 if (valid_scalar_indices (idx)) |
|
63 retval = scalar; |
|
64 else |
|
65 { |
|
66 // XXX FIXME XXX -- this doesn't solve the problem of |
|
67 // |
|
68 // a = 1; a([1,1], [1,1], [1,1]) |
|
69 // |
|
70 // and similar constructions. Hmm... |
|
71 |
2423
|
72 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
73 // 1x1 matrix back to a scalar value. Need a better solution |
|
74 // to this problem. |
|
75 |
|
76 octave_value tmp (new octave_matrix (matrix_value ())); |
2376
|
77 |
|
78 retval = tmp.index (idx); |
|
79 } |
|
80 |
|
81 return retval; |
|
82 } |
|
83 |
|
84 octave_value |
|
85 octave_scalar::convert_to_str (void) const |
|
86 { |
|
87 octave_value retval; |
|
88 |
|
89 if (xisnan (scalar)) |
|
90 ::error ("invalid conversion from NaN to character"); |
|
91 else |
|
92 { |
|
93 // XXX FIXME XXX -- warn about out of range conversions? |
|
94 |
|
95 int i = NINT (scalar); |
|
96 char s[2]; |
|
97 s[0] = (char) i; |
|
98 s[1] = '\0'; |
|
99 retval = octave_value (s); |
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 void |
|
106 octave_scalar::print (ostream& os) |
|
107 { |
|
108 octave_print_internal (os, scalar, false); |
|
109 } |
|
110 |
|
111 /* |
|
112 ;;; Local Variables: *** |
|
113 ;;; mode: C++ *** |
|
114 ;;; End: *** |
|
115 */ |