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