2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 |
2901
|
31 #include <iostream.h> |
|
32 |
2376
|
33 #include "lo-ieee.h" |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "gripes.h" |
|
37 #include "ops.h" |
|
38 #include "ov-range.h" |
|
39 #include "ov-re-mat.h" |
2410
|
40 #include "ov-scalar.h" |
2376
|
41 #include "pr-output.h" |
|
42 |
2477
|
43 octave_allocator |
|
44 octave_range::allocator (sizeof (octave_range)); |
2376
|
45 |
2477
|
46 int |
|
47 octave_range::t_id (-1); |
|
48 |
|
49 const string |
|
50 octave_range::t_name ("range"); |
2376
|
51 |
|
52 static octave_value * |
|
53 default_numeric_conversion_function (const octave_value& a) |
|
54 { |
|
55 CAST_CONV_ARG (const octave_range&); |
|
56 |
|
57 return new octave_matrix (v.matrix_value ()); |
|
58 } |
|
59 |
2427
|
60 type_conv_fcn |
2376
|
61 octave_range::numeric_conversion_function (void) const |
|
62 { |
|
63 return default_numeric_conversion_function; |
|
64 } |
|
65 |
2410
|
66 octave_value * |
|
67 octave_range::try_narrowing_conversion (void) |
|
68 { |
|
69 octave_value *retval = 0; |
|
70 |
|
71 switch (range.nelem ()) |
|
72 { |
|
73 case 1: |
|
74 retval = new octave_scalar (range.base ()); |
|
75 break; |
|
76 |
|
77 case 0: |
|
78 retval = new octave_matrix (Matrix ()); |
|
79 break; |
|
80 |
|
81 default: |
|
82 break; |
|
83 } |
|
84 |
|
85 return retval; |
|
86 } |
|
87 |
2436
|
88 octave_value |
2962
|
89 octave_range::do_index_op (const octave_value_list& idx) const |
2436
|
90 { |
|
91 // XXX FIXME XXX -- this doesn't solve the problem of |
|
92 // |
|
93 // a = 1:5; a(1, 1, 1) |
|
94 // |
|
95 // and similar constructions. Hmm... |
|
96 |
|
97 // XXX FIXME XXX -- using this constructor avoids possibly narrowing |
|
98 // the range to a scalar value. Need a better solution to this |
|
99 // problem. |
|
100 |
|
101 octave_value tmp (new octave_matrix (range.matrix_value ())); |
|
102 |
2962
|
103 return tmp.do_index_op (idx); |
2436
|
104 } |
|
105 |
2376
|
106 double |
|
107 octave_range::double_value (bool) const |
|
108 { |
|
109 double retval = octave_NaN; |
|
110 |
|
111 int nel = range.nelem (); |
|
112 |
|
113 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
114 retval = range.base (); |
|
115 else |
|
116 gripe_invalid_conversion ("range", "real scalar"); |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
|
121 octave_value |
|
122 octave_range::all (void) const |
|
123 { |
2436
|
124 // XXX FIXME XXX -- this is a potential waste of memory. |
|
125 |
|
126 Matrix m = range.matrix_value (); |
|
127 |
|
128 return m.all (); |
2376
|
129 } |
|
130 |
|
131 octave_value |
|
132 octave_range::any (void) const |
|
133 { |
2800
|
134 return static_cast<double> (range.base () != 0.0 || range.nelem () > 1); |
2376
|
135 } |
|
136 |
|
137 bool |
|
138 octave_range::is_true (void) const |
|
139 { |
|
140 bool retval = false; |
2436
|
141 |
|
142 if (range.nelem () == 0) |
|
143 { |
|
144 int flag = Vpropagate_empty_matrices; |
|
145 |
|
146 if (flag < 0) |
|
147 warning ("empty range used in conditional expression"); |
|
148 else if (flag == 0) |
|
149 error ("empty range used in conditional expression"); |
|
150 } |
|
151 else |
|
152 { |
|
153 // XXX FIXME XXX -- this is a potential waste of memory. |
|
154 |
|
155 Matrix m ((range.matrix_value () . all ()) . all ()); |
|
156 |
|
157 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
158 } |
|
159 |
2376
|
160 return retval; |
|
161 } |
|
162 |
|
163 Complex |
|
164 octave_range::complex_value (bool) const |
|
165 { |
|
166 Complex retval (octave_NaN, octave_NaN); |
|
167 |
|
168 int nel = range.nelem (); |
|
169 |
|
170 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
171 retval = range.base (); |
|
172 else |
|
173 gripe_invalid_conversion ("range", "complex scalar"); |
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
|
178 octave_value |
2599
|
179 octave_range::not (void) const |
|
180 { |
|
181 Matrix tmp (range.matrix_value ()); |
|
182 return (! tmp); |
|
183 } |
|
184 |
|
185 octave_value |
2376
|
186 octave_range::transpose (void) const |
|
187 { |
2449
|
188 Matrix tmp (range.matrix_value ()); |
2376
|
189 return tmp.transpose (); |
|
190 } |
|
191 |
|
192 octave_value |
|
193 octave_range::hermitian (void) const |
|
194 { |
2449
|
195 Matrix tmp (range.matrix_value ()); |
2376
|
196 return tmp.transpose (); |
|
197 } |
|
198 |
2449
|
199 octave_value |
|
200 octave_range::convert_to_str (void) const |
|
201 { |
|
202 octave_value tmp (range.matrix_value ()); |
|
203 return tmp.convert_to_str (); |
|
204 } |
|
205 |
2376
|
206 void |
2901
|
207 octave_range::print (ostream& os, bool pr_as_read_syntax) const |
|
208 { |
|
209 print_raw (os, pr_as_read_syntax); |
|
210 newline (os); |
|
211 } |
|
212 |
|
213 void |
|
214 octave_range::print_raw (ostream& os, bool pr_as_read_syntax) const |
|
215 { |
|
216 octave_print_internal (os, range, pr_as_read_syntax, |
|
217 current_print_indent_level ()); |
|
218 } |
|
219 |
|
220 bool |
|
221 octave_range::print_name_tag (ostream& os, const string& name) const |
2376
|
222 { |
2901
|
223 bool retval = false; |
|
224 |
|
225 int n = range.nelem (); |
|
226 |
|
227 indent (os); |
|
228 |
|
229 if (n == 0 || n == 1) |
|
230 os << name << " = "; |
|
231 else |
|
232 { |
|
233 os << name << " ="; |
|
234 newline (os); |
|
235 newline (os); |
|
236 retval = true; |
|
237 } |
|
238 |
|
239 return retval; |
2376
|
240 } |
|
241 |
|
242 /* |
|
243 ;;; Local Variables: *** |
|
244 ;;; mode: C++ *** |
|
245 ;;; End: *** |
|
246 */ |