1993
|
1 // ColumnVector manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
33 |
1847
|
34 #include "f77-fcn.h" |
1368
|
35 #include "lo-error.h" |
458
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
1650
|
38 #include "oct-cmplx.h" |
458
|
39 |
|
40 // Fortran functions we call. |
|
41 |
|
42 extern "C" |
|
43 { |
4552
|
44 F77_RET_T |
|
45 F77_FUNC (zgemv, ZGEMV) (F77_CONST_CHAR_ARG_DECL, |
|
46 const int&, const int&, const Complex&, |
|
47 const Complex*, const int&, const Complex*, |
|
48 const int&, const Complex&, Complex*, const int& |
|
49 F77_CHAR_ARG_LEN_DECL); |
458
|
50 } |
|
51 |
1360
|
52 // Complex Column Vector class |
458
|
53 |
|
54 ComplexColumnVector::ComplexColumnVector (const ColumnVector& a) |
1214
|
55 : MArray<Complex> (a.length ()) |
458
|
56 { |
|
57 for (int i = 0; i < length (); i++) |
|
58 elem (i) = a.elem (i); |
|
59 } |
|
60 |
2386
|
61 bool |
458
|
62 ComplexColumnVector::operator == (const ComplexColumnVector& a) const |
|
63 { |
|
64 int len = length (); |
|
65 if (len != a.length ()) |
|
66 return 0; |
3769
|
67 return mx_inline_equal (data (), a.data (), len); |
458
|
68 } |
|
69 |
2386
|
70 bool |
458
|
71 ComplexColumnVector::operator != (const ComplexColumnVector& a) const |
|
72 { |
|
73 return !(*this == a); |
|
74 } |
|
75 |
|
76 // destructive insert/delete/reorder operations |
|
77 |
|
78 ComplexColumnVector& |
|
79 ComplexColumnVector::insert (const ColumnVector& a, int r) |
|
80 { |
|
81 int a_len = a.length (); |
4316
|
82 |
1699
|
83 if (r < 0 || r + a_len > length ()) |
458
|
84 { |
|
85 (*current_liboctave_error_handler) ("range error for insert"); |
|
86 return *this; |
|
87 } |
|
88 |
4316
|
89 if (a_len > 0) |
|
90 { |
|
91 make_unique (); |
|
92 |
|
93 for (int i = 0; i < a_len; i++) |
|
94 xelem (r+i) = a.elem (i); |
|
95 } |
458
|
96 |
|
97 return *this; |
|
98 } |
|
99 |
|
100 ComplexColumnVector& |
|
101 ComplexColumnVector::insert (const ComplexColumnVector& a, int r) |
|
102 { |
|
103 int a_len = a.length (); |
4316
|
104 |
1699
|
105 if (r < 0 || r + a_len > length ()) |
458
|
106 { |
|
107 (*current_liboctave_error_handler) ("range error for insert"); |
|
108 return *this; |
|
109 } |
|
110 |
4316
|
111 if (a_len > 0) |
|
112 { |
|
113 make_unique (); |
|
114 |
|
115 for (int i = 0; i < a_len; i++) |
|
116 xelem (r+i) = a.elem (i); |
|
117 } |
458
|
118 |
|
119 return *this; |
|
120 } |
|
121 |
|
122 ComplexColumnVector& |
|
123 ComplexColumnVector::fill (double val) |
|
124 { |
|
125 int len = length (); |
4316
|
126 |
458
|
127 if (len > 0) |
4316
|
128 { |
|
129 make_unique (); |
|
130 |
|
131 for (int i = 0; i < len; i++) |
|
132 xelem (i) = val; |
|
133 } |
|
134 |
458
|
135 return *this; |
|
136 } |
|
137 |
|
138 ComplexColumnVector& |
|
139 ComplexColumnVector::fill (const Complex& val) |
|
140 { |
|
141 int len = length (); |
4316
|
142 |
458
|
143 if (len > 0) |
4316
|
144 { |
|
145 make_unique (); |
|
146 |
|
147 for (int i = 0; i < len; i++) |
|
148 xelem (i) = val; |
|
149 } |
|
150 |
|
151 |
458
|
152 return *this; |
|
153 } |
|
154 |
|
155 ComplexColumnVector& |
|
156 ComplexColumnVector::fill (double val, int r1, int r2) |
|
157 { |
|
158 int len = length (); |
4316
|
159 |
458
|
160 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
161 { |
|
162 (*current_liboctave_error_handler) ("range error for fill"); |
|
163 return *this; |
|
164 } |
|
165 |
|
166 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
167 |
4316
|
168 if (r2 >= r1) |
|
169 { |
|
170 make_unique (); |
|
171 |
|
172 for (int i = r1; i <= r2; i++) |
|
173 xelem (i) = val; |
|
174 } |
458
|
175 |
|
176 return *this; |
|
177 } |
|
178 |
|
179 ComplexColumnVector& |
|
180 ComplexColumnVector::fill (const Complex& val, int r1, int r2) |
|
181 { |
|
182 int len = length (); |
4316
|
183 |
458
|
184 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
185 { |
|
186 (*current_liboctave_error_handler) ("range error for fill"); |
|
187 return *this; |
|
188 } |
|
189 |
|
190 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
191 |
4316
|
192 if (r2 >= r1) |
|
193 { |
|
194 make_unique (); |
|
195 |
|
196 for (int i = r1; i <= r2; i++) |
|
197 xelem (i) = val; |
|
198 } |
458
|
199 |
|
200 return *this; |
|
201 } |
|
202 |
|
203 ComplexColumnVector |
|
204 ComplexColumnVector::stack (const ColumnVector& a) const |
|
205 { |
|
206 int len = length (); |
|
207 int nr_insert = len; |
|
208 ComplexColumnVector retval (len + a.length ()); |
|
209 retval.insert (*this, 0); |
|
210 retval.insert (a, nr_insert); |
|
211 return retval; |
|
212 } |
|
213 |
|
214 ComplexColumnVector |
|
215 ComplexColumnVector::stack (const ComplexColumnVector& a) const |
|
216 { |
|
217 int len = length (); |
|
218 int nr_insert = len; |
|
219 ComplexColumnVector retval (len + a.length ()); |
|
220 retval.insert (*this, 0); |
|
221 retval.insert (a, nr_insert); |
|
222 return retval; |
|
223 } |
|
224 |
|
225 ComplexRowVector |
|
226 ComplexColumnVector::hermitian (void) const |
|
227 { |
|
228 int len = length (); |
3769
|
229 return ComplexRowVector (mx_inline_conj_dup (data (), len), len); |
458
|
230 } |
|
231 |
|
232 ComplexRowVector |
|
233 ComplexColumnVector::transpose (void) const |
|
234 { |
1858
|
235 return ComplexRowVector (*this); |
458
|
236 } |
|
237 |
|
238 ComplexColumnVector |
|
239 conj (const ComplexColumnVector& a) |
|
240 { |
|
241 int a_len = a.length (); |
|
242 ComplexColumnVector retval; |
|
243 if (a_len > 0) |
3769
|
244 retval = ComplexColumnVector (mx_inline_conj_dup (a.data (), a_len), a_len); |
458
|
245 return retval; |
|
246 } |
|
247 |
|
248 // resize is the destructive equivalent for this one |
|
249 |
|
250 ComplexColumnVector |
|
251 ComplexColumnVector::extract (int r1, int r2) const |
|
252 { |
|
253 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
254 |
|
255 int new_r = r2 - r1 + 1; |
|
256 |
|
257 ComplexColumnVector result (new_r); |
|
258 |
|
259 for (int i = 0; i < new_r; i++) |
|
260 result.elem (i) = elem (r1+i); |
|
261 |
|
262 return result; |
|
263 } |
|
264 |
4316
|
265 ComplexColumnVector |
|
266 ComplexColumnVector::extract_n (int r1, int n) const |
|
267 { |
|
268 ComplexColumnVector result (n); |
|
269 |
|
270 for (int i = 0; i < n; i++) |
|
271 result.elem (i) = elem (r1+i); |
|
272 |
|
273 return result; |
|
274 } |
|
275 |
458
|
276 // column vector by column vector -> column vector operations |
|
277 |
|
278 ComplexColumnVector& |
|
279 ComplexColumnVector::operator += (const ColumnVector& a) |
|
280 { |
|
281 int len = length (); |
2386
|
282 |
|
283 int a_len = a.length (); |
|
284 |
|
285 if (len != a_len) |
458
|
286 { |
2386
|
287 gripe_nonconformant ("operator +=", len, a_len); |
458
|
288 return *this; |
|
289 } |
|
290 |
|
291 if (len == 0) |
|
292 return *this; |
|
293 |
|
294 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
295 |
3769
|
296 mx_inline_add2 (d, a.data (), len); |
458
|
297 return *this; |
|
298 } |
|
299 |
|
300 ComplexColumnVector& |
|
301 ComplexColumnVector::operator -= (const ColumnVector& a) |
|
302 { |
|
303 int len = length (); |
2386
|
304 |
|
305 int a_len = a.length (); |
|
306 |
|
307 if (len != a_len) |
458
|
308 { |
2386
|
309 gripe_nonconformant ("operator -=", len, a_len); |
458
|
310 return *this; |
|
311 } |
|
312 |
|
313 if (len == 0) |
|
314 return *this; |
|
315 |
|
316 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
317 |
3769
|
318 mx_inline_subtract2 (d, a.data (), len); |
458
|
319 return *this; |
|
320 } |
|
321 |
1205
|
322 // matrix by column vector -> column vector operations |
|
323 |
|
324 ComplexColumnVector |
|
325 operator * (const ComplexMatrix& m, const ColumnVector& a) |
|
326 { |
|
327 ComplexColumnVector tmp (a); |
|
328 return m * tmp; |
|
329 } |
|
330 |
|
331 ComplexColumnVector |
|
332 operator * (const ComplexMatrix& m, const ComplexColumnVector& a) |
|
333 { |
1947
|
334 ComplexColumnVector retval; |
|
335 |
1205
|
336 int nr = m.rows (); |
|
337 int nc = m.cols (); |
1947
|
338 |
2386
|
339 int a_len = a.length (); |
|
340 |
|
341 if (nc != a_len) |
|
342 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
343 else |
458
|
344 { |
1947
|
345 if (nc == 0 || nr == 0) |
|
346 retval.resize (nr, 0.0); |
|
347 else |
|
348 { |
|
349 int ld = nr; |
|
350 |
|
351 retval.resize (nr); |
|
352 Complex *y = retval.fortran_vec (); |
|
353 |
4552
|
354 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
355 nr, nc, 1.0, m.data (), ld, |
|
356 a.data (), 1, 0.0, y, 1 |
|
357 F77_CHAR_ARG_LEN (1))); |
1947
|
358 |
|
359 if (f77_exception_encountered) |
|
360 (*current_liboctave_error_handler) |
|
361 ("unrecoverable error in zgemv"); |
|
362 } |
458
|
363 } |
|
364 |
1947
|
365 return retval; |
458
|
366 } |
|
367 |
1205
|
368 // matrix by column vector -> column vector operations |
|
369 |
|
370 ComplexColumnVector |
|
371 operator * (const Matrix& m, const ComplexColumnVector& a) |
|
372 { |
|
373 ComplexMatrix tmp (m); |
|
374 return tmp * a; |
|
375 } |
|
376 |
|
377 // diagonal matrix by column vector -> column vector operations |
|
378 |
|
379 ComplexColumnVector |
|
380 operator * (const DiagMatrix& m, const ComplexColumnVector& a) |
|
381 { |
|
382 int nr = m.rows (); |
|
383 int nc = m.cols (); |
2386
|
384 |
1205
|
385 int a_len = a.length (); |
2386
|
386 |
1205
|
387 if (nc != a_len) |
|
388 { |
2386
|
389 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
3585
|
390 return ComplexColumnVector (); |
1205
|
391 } |
|
392 |
|
393 if (nc == 0 || nr == 0) |
|
394 return ComplexColumnVector (0); |
|
395 |
|
396 ComplexColumnVector result (nr); |
|
397 |
|
398 for (int i = 0; i < a_len; i++) |
|
399 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
400 |
1321
|
401 for (int i = a_len; i < nr; i++) |
1205
|
402 result.elem (i) = 0.0; |
|
403 |
|
404 return result; |
|
405 } |
|
406 |
|
407 ComplexColumnVector |
|
408 operator * (const ComplexDiagMatrix& m, const ColumnVector& a) |
|
409 { |
|
410 int nr = m.rows (); |
|
411 int nc = m.cols (); |
2386
|
412 |
1205
|
413 int a_len = a.length (); |
2386
|
414 |
1205
|
415 if (nc != a_len) |
|
416 { |
2386
|
417 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1205
|
418 return ComplexColumnVector (); |
|
419 } |
|
420 |
|
421 if (nc == 0 || nr == 0) |
|
422 return ComplexColumnVector (0); |
|
423 |
|
424 ComplexColumnVector result (nr); |
|
425 |
|
426 for (int i = 0; i < a_len; i++) |
|
427 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
428 |
1321
|
429 for (int i = a_len; i < nr; i++) |
1205
|
430 result.elem (i) = 0.0; |
|
431 |
|
432 return result; |
|
433 } |
|
434 |
|
435 ComplexColumnVector |
|
436 operator * (const ComplexDiagMatrix& m, const ComplexColumnVector& a) |
|
437 { |
|
438 int nr = m.rows (); |
|
439 int nc = m.cols (); |
2386
|
440 |
1205
|
441 int a_len = a.length (); |
2386
|
442 |
1205
|
443 if (nc != a_len) |
|
444 { |
2386
|
445 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1205
|
446 return ComplexColumnVector (); |
|
447 } |
|
448 |
|
449 if (nc == 0 || nr == 0) |
|
450 return ComplexColumnVector (0); |
|
451 |
|
452 ComplexColumnVector result (nr); |
|
453 |
|
454 for (int i = 0; i < a_len; i++) |
|
455 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
456 |
1321
|
457 for (int i = a_len; i < nr; i++) |
1205
|
458 result.elem (i) = 0.0; |
|
459 |
|
460 return result; |
|
461 } |
|
462 |
458
|
463 // other operations |
|
464 |
|
465 ComplexColumnVector |
2676
|
466 ComplexColumnVector::map (c_c_Mapper f) const |
458
|
467 { |
2676
|
468 ComplexColumnVector b (*this); |
|
469 return b.apply (f); |
458
|
470 } |
|
471 |
2676
|
472 ColumnVector |
|
473 ComplexColumnVector::map (d_c_Mapper f) const |
458
|
474 { |
2676
|
475 const Complex *d = data (); |
|
476 |
|
477 int len = length (); |
|
478 |
|
479 ColumnVector retval (len); |
|
480 |
|
481 double *r = retval.fortran_vec (); |
|
482 |
|
483 for (int i = 0; i < len; i++) |
|
484 r[i] = f (d[i]); |
|
485 |
|
486 return retval; |
|
487 } |
|
488 |
|
489 ComplexColumnVector& |
|
490 ComplexColumnVector::apply (c_c_Mapper f) |
|
491 { |
|
492 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
493 |
458
|
494 for (int i = 0; i < length (); i++) |
2676
|
495 d[i] = f (d[i]); |
|
496 |
|
497 return *this; |
458
|
498 } |
|
499 |
|
500 Complex |
|
501 ComplexColumnVector::min (void) const |
|
502 { |
|
503 int len = length (); |
|
504 if (len == 0) |
|
505 return 0.0; |
|
506 |
|
507 Complex res = elem (0); |
|
508 double absres = abs (res); |
|
509 |
|
510 for (int i = 1; i < len; i++) |
|
511 if (abs (elem (i)) < absres) |
|
512 { |
|
513 res = elem (i); |
|
514 absres = abs (res); |
|
515 } |
|
516 |
|
517 return res; |
|
518 } |
|
519 |
|
520 Complex |
|
521 ComplexColumnVector::max (void) const |
|
522 { |
|
523 int len = length (); |
|
524 if (len == 0) |
|
525 return 0.0; |
|
526 |
|
527 Complex res = elem (0); |
|
528 double absres = abs (res); |
|
529 |
|
530 for (int i = 1; i < len; i++) |
|
531 if (abs (elem (i)) > absres) |
|
532 { |
|
533 res = elem (i); |
|
534 absres = abs (res); |
|
535 } |
|
536 |
|
537 return res; |
|
538 } |
|
539 |
|
540 // i/o |
|
541 |
3504
|
542 std::ostream& |
|
543 operator << (std::ostream& os, const ComplexColumnVector& a) |
458
|
544 { |
|
545 // int field_width = os.precision () + 7; |
|
546 for (int i = 0; i < a.length (); i++) |
|
547 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
548 return os; |
|
549 } |
|
550 |
3504
|
551 std::istream& |
|
552 operator >> (std::istream& is, ComplexColumnVector& a) |
458
|
553 { |
|
554 int len = a.length(); |
|
555 |
|
556 if (len < 1) |
3504
|
557 is.clear (std::ios::badbit); |
458
|
558 else |
|
559 { |
|
560 double tmp; |
|
561 for (int i = 0; i < len; i++) |
|
562 { |
|
563 is >> tmp; |
|
564 if (is) |
|
565 a.elem (i) = tmp; |
|
566 else |
|
567 break; |
|
568 } |
|
569 } |
532
|
570 return is; |
458
|
571 } |
|
572 |
|
573 /* |
|
574 ;;; Local Variables: *** |
|
575 ;;; mode: C++ *** |
|
576 ;;; End: *** |
|
577 */ |