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 #include "mx-base.h" |
|
36 |
|
37 #include "gripes.h" |
|
38 #include "mappers.h" |
|
39 #include "oct-obj.h" |
2410
|
40 #include "ops.h" |
|
41 #include "ov-scalar.h" |
2376
|
42 #include "ov-re-mat.h" |
|
43 #include "pr-output.h" |
|
44 |
2477
|
45 octave_allocator |
|
46 octave_matrix::allocator (sizeof (octave_matrix)); |
2376
|
47 |
2477
|
48 int |
|
49 octave_matrix::t_id (-1); |
|
50 |
|
51 const string |
|
52 octave_matrix::t_name ("matrix"); |
2376
|
53 |
|
54 octave_matrix::octave_matrix (const RowVector& v, int pcv) |
|
55 : octave_base_value (), |
|
56 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
57 ? Matrix (v.transpose ()) : Matrix (v)) { } |
|
58 |
|
59 octave_matrix::octave_matrix (const ColumnVector& v, int pcv) |
|
60 : octave_base_value (), |
|
61 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
62 ? Matrix (v) : Matrix (v.transpose ())) { } |
|
63 |
2410
|
64 octave_value * |
|
65 octave_matrix::try_narrowing_conversion (void) |
|
66 { |
|
67 octave_value *retval = 0; |
|
68 |
|
69 int nr = matrix.rows (); |
|
70 int nc = matrix.cols (); |
|
71 |
|
72 if (nr == 1 && nc == 1) |
|
73 retval = new octave_scalar (matrix (0, 0)); |
|
74 |
|
75 return retval; |
|
76 } |
|
77 |
2376
|
78 octave_value |
|
79 octave_matrix::index (const octave_value_list& idx) const |
|
80 { |
|
81 octave_value retval; |
|
82 |
|
83 int len = idx.length (); |
|
84 |
|
85 switch (len) |
|
86 { |
|
87 case 2: |
|
88 { |
|
89 idx_vector i = idx (0).index_vector (); |
|
90 idx_vector j = idx (1).index_vector (); |
2407
|
91 |
2376
|
92 retval = Matrix (matrix.index (i, j)); |
|
93 } |
|
94 break; |
|
95 |
|
96 case 1: |
|
97 { |
|
98 idx_vector i = idx (0).index_vector (); |
2407
|
99 |
2376
|
100 retval = Matrix (matrix.index (i)); |
|
101 } |
|
102 break; |
|
103 |
|
104 default: |
|
105 error ("invalid number of indices (%d) for matrix value", len); |
|
106 break; |
|
107 } |
|
108 |
|
109 return retval; |
|
110 } |
|
111 |
|
112 extern void assign (Array2<double>&, const Array2<double>&); |
|
113 |
|
114 void |
|
115 octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs) |
|
116 { |
|
117 int len = idx.length (); |
|
118 |
|
119 switch (len) |
|
120 { |
|
121 case 2: |
|
122 { |
|
123 idx_vector i = idx (0).index_vector (); |
|
124 idx_vector j = idx (1).index_vector (); |
|
125 |
|
126 matrix.set_index (i); |
|
127 matrix.set_index (j); |
|
128 |
|
129 ::assign (matrix, rhs); |
|
130 } |
|
131 break; |
|
132 |
|
133 case 1: |
|
134 { |
|
135 idx_vector i = idx (0).index_vector (); |
|
136 |
|
137 matrix.set_index (i); |
|
138 |
|
139 ::assign (matrix, rhs); |
|
140 } |
|
141 break; |
|
142 |
|
143 default: |
|
144 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
145 len); |
|
146 break; |
|
147 } |
|
148 } |
|
149 |
|
150 bool |
|
151 octave_matrix::valid_as_scalar_index (void) const |
|
152 { |
|
153 // XXX FIXME XXX |
|
154 return false; |
|
155 } |
|
156 |
|
157 bool |
|
158 octave_matrix::is_true (void) const |
|
159 { |
|
160 bool retval = false; |
|
161 |
|
162 if (rows () == 0 || columns () == 0) |
|
163 { |
|
164 int flag = Vpropagate_empty_matrices; |
|
165 |
|
166 if (flag < 0) |
|
167 warning ("empty matrix used in conditional expression"); |
|
168 else if (flag == 0) |
|
169 error ("empty matrix used in conditional expression"); |
|
170 } |
|
171 else |
|
172 { |
|
173 Matrix m = (matrix.all ()) . all (); |
|
174 |
|
175 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
176 } |
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
|
181 double |
|
182 octave_matrix::double_value (bool) const |
|
183 { |
|
184 double retval = octave_NaN; |
|
185 |
|
186 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
187 if ((rows () == 1 && columns () == 1) |
|
188 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
189 retval = matrix (0, 0); |
|
190 else |
|
191 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
192 |
|
193 return retval; |
|
194 } |
|
195 |
|
196 Complex |
|
197 octave_matrix::complex_value (bool) const |
|
198 { |
|
199 Complex retval (octave_NaN, octave_NaN); |
|
200 |
|
201 if ((rows () == 1 && columns () == 1) |
|
202 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
203 retval = matrix (0, 0); |
|
204 else |
|
205 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
206 |
|
207 return retval; |
|
208 } |
|
209 |
|
210 octave_value |
|
211 octave_matrix::convert_to_str (void) const |
|
212 { |
|
213 octave_value retval; |
|
214 |
|
215 int nr = matrix.rows (); |
|
216 int nc = matrix.columns (); |
|
217 |
|
218 if (nr == 0 && nc == 0) |
|
219 { |
|
220 char s = '\0'; |
|
221 retval = octave_value (&s); |
|
222 } |
|
223 else |
|
224 { |
|
225 if (nr == 0 || nc == 0) |
|
226 { |
|
227 char s = '\0'; |
|
228 retval = octave_value (&s); |
|
229 } |
|
230 else |
|
231 { |
|
232 charMatrix chm (nr, nc); |
|
233 |
|
234 for (int j = 0; j < nc; j++) |
|
235 { |
|
236 for (int i = 0; i < nr; i++) |
|
237 { |
|
238 double d = matrix (i, j); |
|
239 |
|
240 if (xisnan (d)) |
|
241 { |
|
242 ::error ("invalid conversion from NaN to character"); |
|
243 return retval; |
|
244 } |
|
245 else |
|
246 { |
|
247 // XXX FIXME XXX -- warn about out of range |
|
248 // conversions? |
|
249 |
|
250 int ival = NINT (d); |
|
251 chm (i, j) = (char) ival; |
|
252 } |
|
253 } |
|
254 } |
|
255 |
|
256 retval = octave_value (chm, 1); |
|
257 } |
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 void |
2901
|
264 octave_matrix::print (ostream& os, bool pr_as_read_syntax) const |
|
265 { |
|
266 indent (os); |
|
267 print_raw (os, pr_as_read_syntax); |
|
268 newline (os); |
|
269 } |
|
270 |
|
271 void |
|
272 octave_matrix::print_raw (ostream& os, bool pr_as_read_syntax) const |
|
273 { |
|
274 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
275 current_print_indent_level ()); |
|
276 } |
|
277 |
|
278 bool |
|
279 octave_matrix::print_name_tag (ostream& os, const string& name) const |
2376
|
280 { |
2901
|
281 bool retval = false; |
|
282 |
|
283 int nr = rows (); |
|
284 int nc = columns (); |
|
285 |
|
286 indent (os); |
|
287 |
|
288 if (nr == 1 && nc == 1 || (nr == 0 || nc == 0)) |
|
289 os << name << " = "; |
|
290 else |
|
291 { |
|
292 os << name << " ="; |
|
293 newline (os); |
|
294 newline (os); |
|
295 retval = true; |
|
296 } |
|
297 |
|
298 return retval; |
2376
|
299 } |
|
300 |
|
301 /* |
|
302 ;;; Local Variables: *** |
|
303 ;;; mode: C++ *** |
|
304 ;;; End: *** |
|
305 */ |