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 "oct-obj.h" |
2979
|
39 #include "oct-lvalue.h" |
2410
|
40 #include "ops.h" |
|
41 #include "ov-scalar.h" |
2376
|
42 #include "ov-re-mat.h" |
|
43 #include "pr-output.h" |
2948
|
44 #include "variables.h" |
2376
|
45 |
2477
|
46 octave_allocator |
|
47 octave_matrix::allocator (sizeof (octave_matrix)); |
2376
|
48 |
2477
|
49 int |
|
50 octave_matrix::t_id (-1); |
|
51 |
|
52 const string |
|
53 octave_matrix::t_name ("matrix"); |
2376
|
54 |
|
55 octave_matrix::octave_matrix (const RowVector& v, int pcv) |
|
56 : octave_base_value (), |
|
57 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
58 ? Matrix (v.transpose ()) : Matrix (v)) { } |
|
59 |
|
60 octave_matrix::octave_matrix (const ColumnVector& v, int pcv) |
|
61 : octave_base_value (), |
|
62 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
63 ? Matrix (v) : Matrix (v.transpose ())) { } |
|
64 |
2410
|
65 octave_value * |
|
66 octave_matrix::try_narrowing_conversion (void) |
|
67 { |
|
68 octave_value *retval = 0; |
|
69 |
|
70 int nr = matrix.rows (); |
|
71 int nc = matrix.cols (); |
|
72 |
|
73 if (nr == 1 && nc == 1) |
|
74 retval = new octave_scalar (matrix (0, 0)); |
|
75 |
|
76 return retval; |
|
77 } |
|
78 |
2376
|
79 octave_value |
2974
|
80 octave_matrix::do_index_op (const octave_value_list& idx) |
2376
|
81 { |
|
82 octave_value retval; |
|
83 |
|
84 int len = idx.length (); |
|
85 |
|
86 switch (len) |
|
87 { |
|
88 case 2: |
|
89 { |
|
90 idx_vector i = idx (0).index_vector (); |
|
91 idx_vector j = idx (1).index_vector (); |
2407
|
92 |
2376
|
93 retval = Matrix (matrix.index (i, j)); |
|
94 } |
|
95 break; |
|
96 |
|
97 case 1: |
|
98 { |
|
99 idx_vector i = idx (0).index_vector (); |
2407
|
100 |
2376
|
101 retval = Matrix (matrix.index (i)); |
|
102 } |
|
103 break; |
|
104 |
|
105 default: |
|
106 error ("invalid number of indices (%d) for matrix value", len); |
|
107 break; |
|
108 } |
|
109 |
|
110 return retval; |
|
111 } |
|
112 |
|
113 extern void assign (Array2<double>&, const Array2<double>&); |
|
114 |
|
115 void |
|
116 octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs) |
|
117 { |
|
118 int len = idx.length (); |
|
119 |
|
120 switch (len) |
|
121 { |
|
122 case 2: |
|
123 { |
|
124 idx_vector i = idx (0).index_vector (); |
|
125 idx_vector j = idx (1).index_vector (); |
|
126 |
|
127 matrix.set_index (i); |
|
128 matrix.set_index (j); |
|
129 |
|
130 ::assign (matrix, rhs); |
|
131 } |
|
132 break; |
|
133 |
|
134 case 1: |
|
135 { |
|
136 idx_vector i = idx (0).index_vector (); |
|
137 |
|
138 matrix.set_index (i); |
|
139 |
|
140 ::assign (matrix, rhs); |
|
141 } |
|
142 break; |
|
143 |
|
144 default: |
|
145 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
146 len); |
|
147 break; |
|
148 } |
|
149 } |
|
150 |
2948
|
151 void |
|
152 octave_matrix::assign_struct_elt (assign_op, const string& nm, |
|
153 const octave_value& rhs) |
|
154 { |
|
155 octave_value retval; |
|
156 |
|
157 Matrix m = rhs.matrix_value (); |
|
158 |
|
159 if (! error_state) |
|
160 { |
|
161 int nr = -1; |
|
162 int nc = -1; |
|
163 |
|
164 int dim = -1; |
|
165 |
|
166 if (m.rows () == 1 && m.cols () == 2) |
|
167 { |
|
168 nr = NINT (m (0, 0)); |
|
169 nc = NINT (m (0, 1)); |
|
170 } |
|
171 else if (m.rows () == 2 && m.cols () == 1) |
|
172 { |
|
173 nr = NINT (m (0, 0)); |
|
174 nc = NINT (m (1, 0)); |
|
175 } |
|
176 else if (m.rows () == 1 && m.cols () == 1) |
|
177 { |
|
178 dim = NINT (m (0, 0)); |
|
179 |
|
180 nr = matrix.rows (); |
|
181 nc = matrix.cols (); |
|
182 } |
|
183 |
|
184 if (nm == "size") |
|
185 { |
|
186 if (nr >= 0 && nc >= 0) |
|
187 matrix.resize (nr, nc, 0.0); |
|
188 else |
|
189 error ("invalid size specification = [%d, %d] specified", |
|
190 nr, nc); |
|
191 } |
|
192 else if (nm == "rows") |
|
193 { |
|
194 if (dim >= 0) |
|
195 matrix.resize (dim, nc, 0.0); |
|
196 else |
|
197 error ("invalid row dimension = %d specified", dim); |
|
198 } |
|
199 else if (nm == "cols" || nm == "columns") |
|
200 { |
|
201 if (dim >= 0) |
|
202 matrix.resize (nr, dim, 0.0); |
|
203 else |
|
204 error ("invalid column dimension = %d specified", dim); |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 void |
|
210 octave_matrix::assign_struct_elt (assign_op, const string&, |
|
211 const octave_value_list&, |
|
212 const octave_value&) |
|
213 { |
|
214 error ("indexed assignment for matrix properties is not implemented"); |
|
215 } |
|
216 |
|
217 octave_value |
2962
|
218 octave_matrix::do_struct_elt_index_op (const string& nm, |
|
219 const octave_value_list& idx, |
|
220 bool silent) |
|
221 { |
|
222 // XXX DO_ME XXX |
|
223 } |
|
224 |
|
225 octave_value |
|
226 octave_matrix::do_struct_elt_index_op (const string& nm, bool silent) |
2948
|
227 { |
|
228 octave_value retval; |
|
229 |
|
230 double nr = static_cast<double> (matrix.rows ()); |
|
231 double nc = static_cast<double> (matrix.cols ()); |
|
232 |
|
233 if (nm == "rows") |
|
234 retval = nr; |
|
235 else if (nm == "cols" || nm == "columns") |
|
236 retval = nc; |
|
237 else if (nm == "size") |
|
238 { |
|
239 Matrix tmp (1, 2); |
|
240 |
|
241 tmp.elem (0, 0) = nr; |
|
242 tmp.elem (0, 1) = nc; |
|
243 |
|
244 retval = tmp; |
|
245 } |
|
246 else if (! silent) |
|
247 error ("structure has no member `%s'", nm.c_str ()); |
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
2979
|
252 octave_lvalue |
2948
|
253 octave_matrix::struct_elt_ref (octave_value *parent, const string& nm) |
|
254 { |
2979
|
255 return octave_lvalue (parent, nm); |
2948
|
256 } |
|
257 |
2376
|
258 bool |
|
259 octave_matrix::valid_as_scalar_index (void) const |
|
260 { |
|
261 // XXX FIXME XXX |
|
262 return false; |
|
263 } |
|
264 |
|
265 bool |
|
266 octave_matrix::is_true (void) const |
|
267 { |
|
268 bool retval = false; |
|
269 |
|
270 if (rows () == 0 || columns () == 0) |
|
271 { |
|
272 int flag = Vpropagate_empty_matrices; |
|
273 |
|
274 if (flag < 0) |
|
275 warning ("empty matrix used in conditional expression"); |
|
276 else if (flag == 0) |
|
277 error ("empty matrix used in conditional expression"); |
|
278 } |
|
279 else |
|
280 { |
|
281 Matrix m = (matrix.all ()) . all (); |
|
282 |
|
283 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
284 } |
|
285 |
|
286 return retval; |
|
287 } |
|
288 |
|
289 double |
|
290 octave_matrix::double_value (bool) const |
|
291 { |
|
292 double retval = octave_NaN; |
|
293 |
|
294 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
295 if ((rows () == 1 && columns () == 1) |
|
296 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
297 retval = matrix (0, 0); |
|
298 else |
|
299 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
|
304 Complex |
|
305 octave_matrix::complex_value (bool) const |
|
306 { |
|
307 Complex retval (octave_NaN, octave_NaN); |
|
308 |
|
309 if ((rows () == 1 && columns () == 1) |
|
310 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
311 retval = matrix (0, 0); |
|
312 else |
|
313 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 octave_value |
|
319 octave_matrix::convert_to_str (void) const |
|
320 { |
|
321 octave_value retval; |
|
322 |
|
323 int nr = matrix.rows (); |
|
324 int nc = matrix.columns (); |
|
325 |
|
326 if (nr == 0 && nc == 0) |
|
327 { |
|
328 char s = '\0'; |
|
329 retval = octave_value (&s); |
|
330 } |
|
331 else |
|
332 { |
|
333 if (nr == 0 || nc == 0) |
|
334 { |
|
335 char s = '\0'; |
|
336 retval = octave_value (&s); |
|
337 } |
|
338 else |
|
339 { |
|
340 charMatrix chm (nr, nc); |
|
341 |
|
342 for (int j = 0; j < nc; j++) |
|
343 { |
|
344 for (int i = 0; i < nr; i++) |
|
345 { |
|
346 double d = matrix (i, j); |
|
347 |
|
348 if (xisnan (d)) |
|
349 { |
|
350 ::error ("invalid conversion from NaN to character"); |
|
351 return retval; |
|
352 } |
|
353 else |
|
354 { |
|
355 // XXX FIXME XXX -- warn about out of range |
|
356 // conversions? |
|
357 |
|
358 int ival = NINT (d); |
|
359 chm (i, j) = (char) ival; |
|
360 } |
|
361 } |
|
362 } |
|
363 |
|
364 retval = octave_value (chm, 1); |
|
365 } |
|
366 } |
|
367 |
|
368 return retval; |
|
369 } |
|
370 |
|
371 void |
2901
|
372 octave_matrix::print (ostream& os, bool pr_as_read_syntax) const |
|
373 { |
|
374 print_raw (os, pr_as_read_syntax); |
|
375 newline (os); |
|
376 } |
|
377 |
|
378 void |
|
379 octave_matrix::print_raw (ostream& os, bool pr_as_read_syntax) const |
|
380 { |
|
381 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
382 current_print_indent_level ()); |
|
383 } |
|
384 |
|
385 bool |
|
386 octave_matrix::print_name_tag (ostream& os, const string& name) const |
2376
|
387 { |
2901
|
388 bool retval = false; |
|
389 |
|
390 int nr = rows (); |
|
391 int nc = columns (); |
|
392 |
|
393 indent (os); |
|
394 |
|
395 if (nr == 1 && nc == 1 || (nr == 0 || nc == 0)) |
|
396 os << name << " = "; |
|
397 else |
|
398 { |
|
399 os << name << " ="; |
|
400 newline (os); |
|
401 newline (os); |
|
402 retval = true; |
|
403 } |
|
404 |
|
405 return retval; |
2376
|
406 } |
|
407 |
|
408 /* |
|
409 ;;; Local Variables: *** |
|
410 ;;; mode: C++ *** |
|
411 ;;; End: *** |
|
412 */ |