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