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