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