458
|
1 // RowVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 #include "f77-uscore.h" |
|
40 |
|
41 // Fortran functions we call. |
|
42 |
|
43 extern "C" |
|
44 { |
|
45 int F77_FCN (zgemv) (const char*, const int*, const int*, |
|
46 const Complex*, const Complex*, const int*, |
|
47 const Complex*, const int*, const Complex*, |
|
48 Complex*, const int*, long); |
|
49 } |
|
50 |
|
51 /* |
|
52 * Complex Row Vector class |
|
53 */ |
|
54 |
|
55 #define KLUDGE_VECTORS |
|
56 #define TYPE Complex |
|
57 #define KL_VEC_TYPE ComplexRowVector |
|
58 #include "mx-kludge.cc" |
|
59 #undef KLUDGE_VECTORS |
|
60 #undef TYPE |
|
61 #undef KL_VEC_TYPE |
|
62 |
|
63 ComplexRowVector::ComplexRowVector (const RowVector& a) |
|
64 : Array<Complex> (a.length ()) |
|
65 { |
|
66 for (int i = 0; i < length (); i++) |
|
67 elem (i) = a.elem (i); |
|
68 } |
|
69 |
|
70 #if 0 |
|
71 ComplexRowVector& |
|
72 ComplexRowVector::resize (int n) |
|
73 { |
|
74 if (n < 0) |
|
75 { |
|
76 (*current_liboctave_error_handler) |
|
77 ("can't resize to negative dimension"); |
|
78 return *this; |
|
79 } |
|
80 |
|
81 Complex *new_data = (Complex *) NULL; |
|
82 if (n > 0) |
|
83 { |
|
84 new_data = new Complex [n]; |
|
85 int min_len = len < n ? len : n; |
|
86 |
|
87 for (int i = 0; i < min_len; i++) |
|
88 new_data[i] = data[i]; |
|
89 } |
|
90 |
|
91 delete [] data; |
|
92 len = n; |
|
93 data = new_data; |
|
94 |
|
95 return *this; |
|
96 } |
|
97 |
|
98 ComplexRowVector& |
|
99 ComplexRowVector::resize (int n, double val) |
|
100 { |
|
101 int old_len = len; |
|
102 resize (n); |
|
103 for (int i = old_len; i < len; i++) |
|
104 data[i] = val; |
|
105 |
|
106 return *this; |
|
107 } |
|
108 |
|
109 ComplexRowVector& |
|
110 ComplexRowVector::resize (int n, const Complex& val) |
|
111 { |
|
112 int old_len = len; |
|
113 resize (n); |
|
114 for (int i = old_len; i < len; i++) |
|
115 data[i] = val; |
|
116 |
|
117 return *this; |
|
118 } |
|
119 #endif |
|
120 |
|
121 int |
|
122 ComplexRowVector::operator == (const ComplexRowVector& a) const |
|
123 { |
|
124 int len = length (); |
|
125 if (len != a.length ()) |
|
126 return 0; |
|
127 return equal (data (), a.data (), len); |
|
128 } |
|
129 |
|
130 int |
|
131 ComplexRowVector::operator != (const ComplexRowVector& a) const |
|
132 { |
|
133 return !(*this == a); |
|
134 } |
|
135 |
|
136 // destructive insert/delete/reorder operations |
|
137 |
|
138 ComplexRowVector& |
|
139 ComplexRowVector::insert (const RowVector& a, int c) |
|
140 { |
|
141 int a_len = a.length (); |
|
142 if (c < 0 || c + a_len - 1 > length ()) |
|
143 { |
|
144 (*current_liboctave_error_handler) ("range error for insert"); |
|
145 return *this; |
|
146 } |
|
147 |
|
148 for (int i = 0; i < a_len; i++) |
|
149 elem (c+i) = a.elem (i); |
|
150 |
|
151 return *this; |
|
152 } |
|
153 |
|
154 ComplexRowVector& |
|
155 ComplexRowVector::insert (const ComplexRowVector& a, int c) |
|
156 { |
|
157 int a_len = a.length (); |
|
158 if (c < 0 || c + a_len - 1 > length ()) |
|
159 { |
|
160 (*current_liboctave_error_handler) ("range error for insert"); |
|
161 return *this; |
|
162 } |
|
163 |
|
164 for (int i = 0; i < a_len; i++) |
|
165 elem (c+i) = a.elem (i); |
|
166 |
|
167 return *this; |
|
168 } |
|
169 |
|
170 ComplexRowVector& |
|
171 ComplexRowVector::fill (double val) |
|
172 { |
|
173 int len = length (); |
|
174 if (len > 0) |
|
175 for (int i = 0; i < len; i++) |
|
176 elem (i) = val; |
|
177 return *this; |
|
178 } |
|
179 |
|
180 ComplexRowVector& |
|
181 ComplexRowVector::fill (const Complex& val) |
|
182 { |
|
183 int len = length (); |
|
184 if (len > 0) |
|
185 for (int i = 0; i < len; i++) |
|
186 elem (i) = val; |
|
187 return *this; |
|
188 } |
|
189 |
|
190 ComplexRowVector& |
|
191 ComplexRowVector::fill (double val, int c1, int c2) |
|
192 { |
|
193 int len = length (); |
|
194 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
195 { |
|
196 (*current_liboctave_error_handler) ("range error for fill"); |
|
197 return *this; |
|
198 } |
|
199 |
|
200 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
201 |
|
202 for (int i = c1; i <= c2; i++) |
|
203 elem (i) = val; |
|
204 |
|
205 return *this; |
|
206 } |
|
207 |
|
208 ComplexRowVector& |
|
209 ComplexRowVector::fill (const Complex& val, int c1, int c2) |
|
210 { |
|
211 int len = length (); |
|
212 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
213 { |
|
214 (*current_liboctave_error_handler) ("range error for fill"); |
|
215 return *this; |
|
216 } |
|
217 |
|
218 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
219 |
|
220 for (int i = c1; i <= c2; i++) |
|
221 elem (i) = val; |
|
222 |
|
223 return *this; |
|
224 } |
|
225 |
|
226 ComplexRowVector |
|
227 ComplexRowVector::append (const RowVector& a) const |
|
228 { |
|
229 int len = length (); |
|
230 int nc_insert = len; |
|
231 ComplexRowVector retval (len + a.length ()); |
|
232 retval.insert (*this, 0); |
|
233 retval.insert (a, nc_insert); |
|
234 return retval; |
|
235 } |
|
236 |
|
237 ComplexRowVector |
|
238 ComplexRowVector::append (const ComplexRowVector& a) const |
|
239 { |
|
240 int len = length (); |
|
241 int nc_insert = len; |
|
242 ComplexRowVector retval (len + a.length ()); |
|
243 retval.insert (*this, 0); |
|
244 retval.insert (a, nc_insert); |
|
245 return retval; |
|
246 } |
|
247 |
|
248 ComplexColumnVector |
|
249 ComplexRowVector::hermitian (void) const |
|
250 { |
|
251 int len = length (); |
|
252 return ComplexColumnVector (conj_dup (data (), len), len); |
|
253 } |
|
254 |
|
255 ComplexColumnVector |
|
256 ComplexRowVector::transpose (void) const |
|
257 { |
|
258 int len = length (); |
|
259 return ComplexColumnVector (dup (data (), len), len); |
|
260 } |
|
261 |
|
262 RowVector |
|
263 real (const ComplexRowVector& a) |
|
264 { |
|
265 int a_len = a.length (); |
|
266 RowVector retval; |
|
267 if (a_len > 0) |
|
268 retval = RowVector (real_dup (a.data (), a_len), a_len); |
|
269 return retval; |
|
270 } |
|
271 |
|
272 RowVector |
|
273 imag (const ComplexRowVector& a) |
|
274 { |
|
275 int a_len = a.length (); |
|
276 RowVector retval; |
|
277 if (a_len > 0) |
|
278 retval = RowVector (imag_dup (a.data (), a_len), a_len); |
|
279 return retval; |
|
280 } |
|
281 |
|
282 ComplexRowVector |
|
283 conj (const ComplexRowVector& a) |
|
284 { |
|
285 int a_len = a.length (); |
|
286 ComplexRowVector retval; |
|
287 if (a_len > 0) |
|
288 retval = ComplexRowVector (conj_dup (a.data (), a_len), a_len); |
|
289 return retval; |
|
290 } |
|
291 |
|
292 // resize is the destructive equivalent for this one |
|
293 |
|
294 ComplexRowVector |
|
295 ComplexRowVector::extract (int c1, int c2) const |
|
296 { |
|
297 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
298 |
|
299 int new_c = c2 - c1 + 1; |
|
300 |
|
301 ComplexRowVector result (new_c); |
|
302 |
|
303 for (int i = 0; i < new_c; i++) |
|
304 result.elem (i) = elem (c1+i); |
|
305 |
|
306 return result; |
|
307 } |
|
308 |
|
309 // row vector by row vector -> row vector operations |
|
310 |
|
311 ComplexRowVector& |
|
312 ComplexRowVector::operator += (const RowVector& a) |
|
313 { |
|
314 int len = length (); |
|
315 if (len != a.length ()) |
|
316 { |
|
317 (*current_liboctave_error_handler) |
|
318 ("nonconformant vector += operation attempted"); |
|
319 return *this; |
|
320 } |
|
321 |
|
322 if (len == 0) |
|
323 return *this; |
|
324 |
|
325 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
326 |
|
327 add2 (d, a.data (), len); |
|
328 return *this; |
|
329 } |
|
330 |
|
331 ComplexRowVector& |
|
332 ComplexRowVector::operator -= (const RowVector& a) |
|
333 { |
|
334 int len = length (); |
|
335 if (len != a.length ()) |
|
336 { |
|
337 (*current_liboctave_error_handler) |
|
338 ("nonconformant vector -= operation attempted"); |
|
339 return *this; |
|
340 } |
|
341 |
|
342 if (len == 0) |
|
343 return *this; |
|
344 |
|
345 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
346 |
|
347 subtract2 (d, a.data (), len); |
|
348 return *this; |
|
349 } |
|
350 |
|
351 ComplexRowVector& |
|
352 ComplexRowVector::operator += (const ComplexRowVector& a) |
|
353 { |
|
354 int len = length (); |
|
355 if (len != a.length ()) |
|
356 { |
|
357 (*current_liboctave_error_handler) |
|
358 ("nonconformant vector += operation attempted"); |
|
359 return *this; |
|
360 } |
|
361 |
|
362 if (len == 0) |
|
363 return *this; |
|
364 |
|
365 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
366 |
|
367 add2 (d, a.data (), len); |
|
368 return *this; |
|
369 } |
|
370 |
|
371 ComplexRowVector& |
|
372 ComplexRowVector::operator -= (const ComplexRowVector& a) |
|
373 { |
|
374 int len = length (); |
|
375 if (len != a.length ()) |
|
376 { |
|
377 (*current_liboctave_error_handler) |
|
378 ("nonconformant vector -= operation attempted"); |
|
379 return *this; |
|
380 } |
|
381 |
|
382 if (len == 0) |
|
383 return *this; |
|
384 |
|
385 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
386 |
|
387 subtract2 (d, a.data (), len); |
|
388 return *this; |
|
389 } |
|
390 |
|
391 // row vector by scalar -> row vector operations |
|
392 |
|
393 ComplexRowVector |
|
394 operator + (const ComplexRowVector& v, double s) |
|
395 { |
|
396 int len = v.length (); |
|
397 return ComplexRowVector (add (v.data (), len, s), len); |
|
398 } |
|
399 |
|
400 ComplexRowVector |
|
401 operator - (const ComplexRowVector& v, double s) |
|
402 { |
|
403 int len = v.length (); |
|
404 return ComplexRowVector (subtract (v.data (), len, s), len); |
|
405 } |
|
406 |
|
407 ComplexRowVector |
|
408 operator * (const ComplexRowVector& v, double s) |
|
409 { |
|
410 int len = v.length (); |
|
411 return ComplexRowVector (multiply (v.data (), len, s), len); |
|
412 } |
|
413 |
|
414 ComplexRowVector |
|
415 operator / (const ComplexRowVector& v, double s) |
|
416 { |
|
417 int len = v.length (); |
|
418 return ComplexRowVector (divide (v.data (), len, s), len); |
|
419 } |
|
420 |
|
421 // scalar by row vector -> row vector operations |
|
422 |
|
423 ComplexRowVector |
|
424 operator + (double s, const ComplexRowVector& a) |
|
425 { |
|
426 int a_len = a.length (); |
|
427 return ComplexRowVector (add (a.data (), a_len, s), a_len); |
|
428 } |
|
429 |
|
430 ComplexRowVector |
|
431 operator - (double s, const ComplexRowVector& a) |
|
432 { |
|
433 int a_len = a.length (); |
|
434 return ComplexRowVector (subtract (s, a.data (), a_len), a_len); |
|
435 } |
|
436 |
|
437 ComplexRowVector |
|
438 operator * (double s, const ComplexRowVector& a) |
|
439 { |
|
440 int a_len = a.length (); |
|
441 return ComplexRowVector (multiply (a.data (), a_len, s), a_len); |
|
442 } |
|
443 |
|
444 ComplexRowVector |
|
445 operator / (double s, const ComplexRowVector& a) |
|
446 { |
|
447 int a_len = a.length (); |
|
448 return ComplexRowVector (divide (s, a.data (), a_len), a_len); |
|
449 } |
|
450 |
|
451 // row vector by column vector -> scalar |
|
452 |
|
453 Complex |
|
454 operator * (const ComplexRowVector& v, const ColumnVector& a) |
|
455 { |
|
456 ComplexColumnVector tmp (a); |
|
457 return v * tmp; |
|
458 } |
|
459 |
|
460 Complex |
|
461 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) |
|
462 { |
|
463 int len = v.length (); |
|
464 if (len != a.length ()) |
|
465 { |
|
466 (*current_liboctave_error_handler) |
|
467 ("nonconformant vector multiplication attempted"); |
|
468 return 0.0; |
|
469 } |
|
470 |
|
471 Complex retval (0.0, 0.0); |
|
472 |
|
473 for (int i = 0; i < len; i++) |
|
474 retval += v.elem (i) * a.elem (i); |
|
475 |
|
476 return retval; |
|
477 } |
|
478 |
|
479 // row vector by matrix -> row vector |
|
480 |
|
481 ComplexRowVector |
|
482 operator * (const ComplexRowVector& v, const ComplexMatrix& a) |
|
483 { |
|
484 int len = v.length (); |
|
485 if (a.rows () != len) |
|
486 { |
|
487 (*current_liboctave_error_handler) |
|
488 ("nonconformant vector multiplication attempted"); |
|
489 return ComplexRowVector (); |
|
490 } |
|
491 |
|
492 if (len == 0 || a.cols () == 0) |
|
493 return ComplexRowVector (0); |
|
494 |
|
495 // Transpose A to form A'*x == (x'*A)' |
|
496 |
|
497 int a_nr = a.rows (); |
|
498 int a_nc = a.cols (); |
|
499 |
|
500 char trans = 'T'; |
|
501 int ld = a_nr; |
|
502 Complex alpha (1.0); |
|
503 Complex beta (0.0); |
|
504 int i_one = 1; |
|
505 |
|
506 Complex *y = new Complex [len]; |
|
507 |
|
508 F77_FCN (zgemv) (&trans, &a_nc, &a_nr, &alpha, a.data (), &ld, |
|
509 v.data (), &i_one, &beta, y, &i_one, 1L); |
|
510 |
|
511 return ComplexRowVector (y, len); |
|
512 } |
|
513 |
|
514 // row vector by row vector -> row vector operations |
|
515 |
|
516 ComplexRowVector |
|
517 operator + (const ComplexRowVector& v, const RowVector& a) |
|
518 { |
|
519 int len = v.length (); |
|
520 if (len != a.length ()) |
|
521 { |
|
522 (*current_liboctave_error_handler) |
|
523 ("nonconformant vector addition attempted"); |
|
524 return ComplexRowVector (); |
|
525 } |
|
526 |
|
527 if (len == 0) |
|
528 return ComplexRowVector (0); |
|
529 |
|
530 return ComplexRowVector (add (v.data (), a.data (), len), len); |
|
531 } |
|
532 |
|
533 ComplexRowVector |
|
534 operator - (const ComplexRowVector& v, const RowVector& a) |
|
535 { |
|
536 int len = v.length (); |
|
537 if (len != a.length ()) |
|
538 { |
|
539 (*current_liboctave_error_handler) |
|
540 ("nonconformant vector subtraction attempted"); |
|
541 return ComplexRowVector (); |
|
542 } |
|
543 |
|
544 if (len == 0) |
|
545 return ComplexRowVector (0); |
|
546 |
|
547 return ComplexRowVector (subtract (v.data (), a.data (), len), len); |
|
548 } |
|
549 |
|
550 ComplexRowVector |
|
551 product (const ComplexRowVector& v, const RowVector& a) |
|
552 { |
|
553 int len = v.length (); |
|
554 if (len != a.length ()) |
|
555 { |
|
556 (*current_liboctave_error_handler) |
|
557 ("nonconformant vector product attempted"); |
|
558 return ComplexRowVector (); |
|
559 } |
|
560 |
|
561 if (len == 0) |
|
562 return ComplexRowVector (0); |
|
563 |
|
564 return ComplexRowVector (multiply (v.data (), a.data (), len), len); |
|
565 } |
|
566 |
|
567 ComplexRowVector |
|
568 quotient (const ComplexRowVector& v, const RowVector& a) |
|
569 { |
|
570 int len = v.length (); |
|
571 if (len != a.length ()) |
|
572 { |
|
573 (*current_liboctave_error_handler) |
|
574 ("nonconformant vector quotient attempted"); |
|
575 return ComplexRowVector (); |
|
576 } |
|
577 |
|
578 if (len == 0) |
|
579 return ComplexRowVector (0); |
|
580 |
|
581 return ComplexRowVector (divide (v.data (), a.data (), len), len); |
|
582 } |
|
583 |
|
584 // other operations |
|
585 |
|
586 ComplexRowVector |
|
587 map (c_c_Mapper f, const ComplexRowVector& a) |
|
588 { |
|
589 ComplexRowVector b (a); |
|
590 b.map (f); |
|
591 return b; |
|
592 } |
|
593 |
|
594 RowVector |
|
595 map (d_c_Mapper f, const ComplexRowVector& a) |
|
596 { |
|
597 int a_len = a.length (); |
|
598 RowVector b (a_len); |
|
599 for (int i = 0; i < a_len; i++) |
|
600 b.elem (i) = f (a.elem (i)); |
|
601 return b; |
|
602 } |
|
603 |
|
604 void |
|
605 ComplexRowVector::map (c_c_Mapper f) |
|
606 { |
|
607 for (int i = 0; i < length (); i++) |
|
608 elem (i) = f (elem (i)); |
|
609 } |
|
610 |
|
611 Complex |
|
612 ComplexRowVector::min (void) const |
|
613 { |
|
614 int len = length (); |
|
615 if (len == 0) |
|
616 return Complex (0.0); |
|
617 |
|
618 Complex res = elem (0); |
|
619 double absres = abs (res); |
|
620 |
|
621 for (int i = 1; i < len; i++) |
|
622 if (abs (elem (i)) < absres) |
|
623 { |
|
624 res = elem (i); |
|
625 absres = abs (res); |
|
626 } |
|
627 |
|
628 return res; |
|
629 } |
|
630 |
|
631 Complex |
|
632 ComplexRowVector::max (void) const |
|
633 { |
|
634 int len = length (); |
|
635 if (len == 0) |
|
636 return Complex (0.0); |
|
637 |
|
638 Complex res = elem (0); |
|
639 double absres = abs (res); |
|
640 |
|
641 for (int i = 1; i < len; i++) |
|
642 if (abs (elem (i)) > absres) |
|
643 { |
|
644 res = elem (i); |
|
645 absres = abs (res); |
|
646 } |
|
647 |
|
648 return res; |
|
649 } |
|
650 |
|
651 // i/o |
|
652 |
|
653 ostream& |
|
654 operator << (ostream& os, const ComplexRowVector& a) |
|
655 { |
|
656 // int field_width = os.precision () + 7; |
|
657 for (int i = 0; i < a.length (); i++) |
|
658 os << " " /* setw (field_width) */ << a.elem (i); |
|
659 return os; |
|
660 } |
|
661 |
|
662 istream& |
|
663 operator >> (istream& is, ComplexRowVector& a) |
|
664 { |
|
665 int len = a.length(); |
|
666 |
|
667 if (len < 1) |
|
668 is.clear (ios::badbit); |
|
669 else |
|
670 { |
|
671 Complex tmp; |
|
672 for (int i = 0; i < len; i++) |
|
673 { |
|
674 is >> tmp; |
|
675 if (is) |
|
676 a.elem (i) = tmp; |
|
677 else |
|
678 break; |
|
679 } |
|
680 } |
|
681 } |
|
682 |
|
683 /* |
|
684 ;;; Local Variables: *** |
|
685 ;;; mode: C++ *** |
|
686 ;;; page-delimiter: "^/\\*" *** |
|
687 ;;; End: *** |
|
688 */ |