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