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" |
2954
|
39 #include "oct-var-ref.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 |
|
80 octave_matrix::index (const octave_value_list& idx) const |
|
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 |
|
218 octave_matrix::struct_elt_val (const string& nm, bool silent) const |
|
219 { |
|
220 octave_value retval; |
|
221 |
|
222 double nr = static_cast<double> (matrix.rows ()); |
|
223 double nc = static_cast<double> (matrix.cols ()); |
|
224 |
|
225 if (nm == "rows") |
|
226 retval = nr; |
|
227 else if (nm == "cols" || nm == "columns") |
|
228 retval = nc; |
|
229 else if (nm == "size") |
|
230 { |
|
231 Matrix tmp (1, 2); |
|
232 |
|
233 tmp.elem (0, 0) = nr; |
|
234 tmp.elem (0, 1) = nc; |
|
235 |
|
236 retval = tmp; |
|
237 } |
|
238 else if (! silent) |
|
239 error ("structure has no member `%s'", nm.c_str ()); |
|
240 |
|
241 return retval; |
|
242 } |
|
243 |
|
244 octave_variable_reference |
|
245 octave_matrix::struct_elt_ref (octave_value *parent, const string& nm) |
|
246 { |
|
247 return octave_variable_reference (parent, nm); |
|
248 } |
|
249 |
2376
|
250 bool |
|
251 octave_matrix::valid_as_scalar_index (void) const |
|
252 { |
|
253 // XXX FIXME XXX |
|
254 return false; |
|
255 } |
|
256 |
|
257 bool |
|
258 octave_matrix::is_true (void) const |
|
259 { |
|
260 bool retval = false; |
|
261 |
|
262 if (rows () == 0 || columns () == 0) |
|
263 { |
|
264 int flag = Vpropagate_empty_matrices; |
|
265 |
|
266 if (flag < 0) |
|
267 warning ("empty matrix used in conditional expression"); |
|
268 else if (flag == 0) |
|
269 error ("empty matrix used in conditional expression"); |
|
270 } |
|
271 else |
|
272 { |
|
273 Matrix m = (matrix.all ()) . all (); |
|
274 |
|
275 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
276 } |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 double |
|
282 octave_matrix::double_value (bool) const |
|
283 { |
|
284 double retval = octave_NaN; |
|
285 |
|
286 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
287 if ((rows () == 1 && columns () == 1) |
|
288 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
289 retval = matrix (0, 0); |
|
290 else |
|
291 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
292 |
|
293 return retval; |
|
294 } |
|
295 |
|
296 Complex |
|
297 octave_matrix::complex_value (bool) const |
|
298 { |
|
299 Complex retval (octave_NaN, octave_NaN); |
|
300 |
|
301 if ((rows () == 1 && columns () == 1) |
|
302 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
303 retval = matrix (0, 0); |
|
304 else |
|
305 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
306 |
|
307 return retval; |
|
308 } |
|
309 |
|
310 octave_value |
|
311 octave_matrix::convert_to_str (void) const |
|
312 { |
|
313 octave_value retval; |
|
314 |
|
315 int nr = matrix.rows (); |
|
316 int nc = matrix.columns (); |
|
317 |
|
318 if (nr == 0 && nc == 0) |
|
319 { |
|
320 char s = '\0'; |
|
321 retval = octave_value (&s); |
|
322 } |
|
323 else |
|
324 { |
|
325 if (nr == 0 || nc == 0) |
|
326 { |
|
327 char s = '\0'; |
|
328 retval = octave_value (&s); |
|
329 } |
|
330 else |
|
331 { |
|
332 charMatrix chm (nr, nc); |
|
333 |
|
334 for (int j = 0; j < nc; j++) |
|
335 { |
|
336 for (int i = 0; i < nr; i++) |
|
337 { |
|
338 double d = matrix (i, j); |
|
339 |
|
340 if (xisnan (d)) |
|
341 { |
|
342 ::error ("invalid conversion from NaN to character"); |
|
343 return retval; |
|
344 } |
|
345 else |
|
346 { |
|
347 // XXX FIXME XXX -- warn about out of range |
|
348 // conversions? |
|
349 |
|
350 int ival = NINT (d); |
|
351 chm (i, j) = (char) ival; |
|
352 } |
|
353 } |
|
354 } |
|
355 |
|
356 retval = octave_value (chm, 1); |
|
357 } |
|
358 } |
|
359 |
|
360 return retval; |
|
361 } |
|
362 |
|
363 void |
2901
|
364 octave_matrix::print (ostream& os, bool pr_as_read_syntax) const |
|
365 { |
|
366 print_raw (os, pr_as_read_syntax); |
|
367 newline (os); |
|
368 } |
|
369 |
|
370 void |
|
371 octave_matrix::print_raw (ostream& os, bool pr_as_read_syntax) const |
|
372 { |
|
373 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
374 current_print_indent_level ()); |
|
375 } |
|
376 |
|
377 bool |
|
378 octave_matrix::print_name_tag (ostream& os, const string& name) const |
2376
|
379 { |
2901
|
380 bool retval = false; |
|
381 |
|
382 int nr = rows (); |
|
383 int nc = columns (); |
|
384 |
|
385 indent (os); |
|
386 |
|
387 if (nr == 1 && nc == 1 || (nr == 0 || nc == 0)) |
|
388 os << name << " = "; |
|
389 else |
|
390 { |
|
391 os << name << " ="; |
|
392 newline (os); |
|
393 newline (os); |
|
394 retval = true; |
|
395 } |
|
396 |
|
397 return retval; |
2376
|
398 } |
|
399 |
|
400 /* |
|
401 ;;; Local Variables: *** |
|
402 ;;; mode: C++ *** |
|
403 ;;; End: *** |
|
404 */ |