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" |
3219
|
41 #include "ov-base.h" |
|
42 #include "ov-base-mat.h" |
|
43 #include "ov-base-mat.cc" |
2410
|
44 #include "ov-scalar.h" |
2376
|
45 #include "ov-re-mat.h" |
|
46 #include "pr-output.h" |
2948
|
47 #include "variables.h" |
2376
|
48 |
3219
|
49 template class octave_base_matrix<Matrix>; |
2376
|
50 |
3219
|
51 DEFINE_OCTAVE_ALLOCATOR (octave_matrix); |
2477
|
52 |
3219
|
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_matrix, "matrix"); |
2376
|
54 |
|
55 octave_matrix::octave_matrix (const RowVector& v, int pcv) |
3219
|
56 : octave_base_matrix<Matrix> ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
57 ? Matrix (v.transpose ()) : Matrix (v)) { } |
2376
|
58 |
|
59 octave_matrix::octave_matrix (const ColumnVector& v, int pcv) |
3219
|
60 : octave_base_matrix<Matrix> ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
61 ? Matrix (v) : Matrix (v.transpose ())) { } |
2376
|
62 |
2410
|
63 octave_value * |
|
64 octave_matrix::try_narrowing_conversion (void) |
|
65 { |
|
66 octave_value *retval = 0; |
|
67 |
|
68 int nr = matrix.rows (); |
|
69 int nc = matrix.cols (); |
|
70 |
|
71 if (nr == 1 && nc == 1) |
|
72 retval = new octave_scalar (matrix (0, 0)); |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
2376
|
77 octave_value |
2974
|
78 octave_matrix::do_index_op (const octave_value_list& idx) |
2376
|
79 { |
|
80 octave_value retval; |
|
81 |
|
82 int len = idx.length (); |
|
83 |
|
84 switch (len) |
|
85 { |
|
86 case 2: |
|
87 { |
|
88 idx_vector i = idx (0).index_vector (); |
|
89 idx_vector j = idx (1).index_vector (); |
2407
|
90 |
2376
|
91 retval = Matrix (matrix.index (i, j)); |
|
92 } |
|
93 break; |
|
94 |
|
95 case 1: |
|
96 { |
|
97 idx_vector i = idx (0).index_vector (); |
2407
|
98 |
2376
|
99 retval = Matrix (matrix.index (i)); |
|
100 } |
|
101 break; |
|
102 |
|
103 default: |
|
104 error ("invalid number of indices (%d) for matrix value", len); |
|
105 break; |
|
106 } |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
3109
|
111 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
2376
|
112 extern void assign (Array2<double>&, const Array2<double>&); |
3107
|
113 #endif |
2376
|
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 /* |
|
372 ;;; Local Variables: *** |
|
373 ;;; mode: C++ *** |
|
374 ;;; End: *** |
|
375 */ |