3
|
1 // ColumnVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
377
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
3
|
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 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
3
|
29 |
|
30 #include "Matrix.h" |
|
31 #include "mx-inlines.cc" |
238
|
32 #include "f77-uscore.h" |
227
|
33 #include "lo-error.h" |
230
|
34 |
|
35 // Fortran functions we call. |
|
36 |
|
37 extern "C" |
|
38 { |
|
39 int F77_FCN (dgemm) (const char*, const char*, const int*, |
|
40 const int*, const int*, const double*, |
|
41 const double*, const int*, const double*, |
|
42 const int*, const double*, double*, const int*, |
|
43 long, long); |
|
44 |
|
45 /* |
|
46 * f2c translates complex*16 as |
|
47 * |
|
48 * typedef struct { doublereal re, im; } doublecomplex; |
|
49 * |
|
50 * and Complex.h from libg++ uses |
|
51 * |
|
52 * protected: |
|
53 * double re; |
|
54 * double im; |
|
55 * |
|
56 * as the only data members, so this should work (fingers crossed that |
|
57 * things don't change). |
|
58 */ |
|
59 |
|
60 int F77_FCN (zgemm) (const char*, const char*, const int*, |
|
61 const int*, const int*, const Complex*, |
|
62 const Complex*, const int*, const Complex*, |
|
63 const int*, const Complex*, Complex*, const int*, |
|
64 long, long); |
|
65 } |
3
|
66 |
|
67 /* |
|
68 * Column Vector class. |
|
69 */ |
|
70 |
238
|
71 #if 0 |
3
|
72 ColumnVector& |
|
73 ColumnVector::resize (int n) |
|
74 { |
|
75 if (n < 0) |
227
|
76 { |
|
77 (*current_liboctave_error_handler) |
|
78 ("can't resize to negative dimension"); |
|
79 return *this; |
|
80 } |
3
|
81 |
|
82 double *new_data = (double *) NULL; |
|
83 if (n > 0) |
|
84 { |
|
85 new_data = new double [n]; |
|
86 int min_len = len < n ? len : n; |
|
87 |
|
88 for (int i = 0; i < min_len; i++) |
|
89 new_data[i] = data[i]; |
|
90 } |
|
91 |
|
92 delete [] data; |
|
93 len = n; |
|
94 data = new_data; |
|
95 |
|
96 return *this; |
|
97 } |
|
98 |
|
99 ColumnVector& |
|
100 ColumnVector::resize (int n, double val) |
|
101 { |
|
102 int old_len = len; |
|
103 resize (n); |
|
104 for (int i = old_len; i < len; i++) |
|
105 data[i] = val; |
|
106 |
|
107 return *this; |
|
108 } |
238
|
109 #endif |
3
|
110 |
|
111 int |
|
112 ColumnVector::operator == (const ColumnVector& a) const |
|
113 { |
238
|
114 int len = length (); |
|
115 if (len != a.length ()) |
3
|
116 return 0; |
238
|
117 return equal (data (), a.data (), len); |
3
|
118 } |
|
119 |
|
120 int |
|
121 ColumnVector::operator != (const ColumnVector& a) const |
|
122 { |
238
|
123 return !(*this == a); |
3
|
124 } |
|
125 |
|
126 ColumnVector& |
|
127 ColumnVector::insert (const ColumnVector& a, int r) |
|
128 { |
238
|
129 int a_len = a.length (); |
|
130 if (r < 0 || r + a_len - 1 > length ()) |
227
|
131 { |
|
132 (*current_liboctave_error_handler) ("range error for insert"); |
|
133 return *this; |
|
134 } |
3
|
135 |
238
|
136 for (int i = 0; i < a_len; i++) |
|
137 elem (r+i) = a.elem (i); |
3
|
138 |
|
139 return *this; |
|
140 } |
|
141 |
|
142 ColumnVector& |
|
143 ColumnVector::fill (double val) |
|
144 { |
238
|
145 int len = length (); |
3
|
146 if (len > 0) |
238
|
147 for (int i = 0; i < len; i++) |
|
148 elem (i) = val; |
3
|
149 return *this; |
|
150 } |
|
151 |
|
152 ColumnVector& |
|
153 ColumnVector::fill (double val, int r1, int r2) |
|
154 { |
238
|
155 int len = length (); |
3
|
156 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
227
|
157 { |
|
158 (*current_liboctave_error_handler) ("range error for fill"); |
|
159 return *this; |
|
160 } |
3
|
161 |
|
162 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
163 |
|
164 for (int i = r1; i <= r2; i++) |
238
|
165 elem (i) = val; |
3
|
166 |
|
167 return *this; |
|
168 } |
|
169 |
|
170 ColumnVector |
|
171 ColumnVector::stack (const ColumnVector& a) const |
|
172 { |
238
|
173 int len = length (); |
3
|
174 int nr_insert = len; |
238
|
175 ColumnVector retval (len + a.length ()); |
3
|
176 retval.insert (*this, 0); |
|
177 retval.insert (a, nr_insert); |
|
178 return retval; |
|
179 } |
|
180 |
|
181 RowVector |
|
182 ColumnVector::transpose (void) const |
|
183 { |
238
|
184 int len = length (); |
|
185 return RowVector (dup (data (), len), len); |
3
|
186 } |
|
187 |
|
188 // resize is the destructive equivalent for this one |
|
189 |
|
190 ColumnVector |
|
191 ColumnVector::extract (int r1, int r2) const |
|
192 { |
|
193 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
194 |
|
195 int new_r = r2 - r1 + 1; |
|
196 |
|
197 ColumnVector result (new_r); |
|
198 |
|
199 for (int i = 0; i < new_r; i++) |
238
|
200 result.elem (i) = elem (r1+i); |
3
|
201 |
|
202 return result; |
|
203 } |
|
204 |
238
|
205 // column vector by column vector -> column vector operations |
3
|
206 |
238
|
207 ColumnVector& |
|
208 ColumnVector::operator += (const ColumnVector& a) |
3
|
209 { |
238
|
210 int len = length (); |
|
211 if (len != a.length ()) |
|
212 { |
|
213 (*current_liboctave_error_handler) |
|
214 ("nonconformant vector += operation attempted"); |
|
215 return ColumnVector (); |
|
216 } |
|
217 |
|
218 if (len == 0) |
|
219 return *this; |
|
220 |
|
221 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
222 |
|
223 add2 (d, a.data (), len); |
|
224 return *this; |
3
|
225 } |
|
226 |
238
|
227 ColumnVector& |
|
228 ColumnVector::operator -= (const ColumnVector& a) |
3
|
229 { |
238
|
230 int len = length (); |
|
231 if (len != a.length ()) |
|
232 { |
|
233 (*current_liboctave_error_handler) |
|
234 ("nonconformant vector -= operation attempted"); |
|
235 return ColumnVector (); |
|
236 } |
3
|
237 |
238
|
238 if (len == 0) |
|
239 return *this; |
3
|
240 |
238
|
241 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
242 |
|
243 subtract2 (d, a.data (), len); |
|
244 return *this; |
3
|
245 } |
|
246 |
|
247 // scalar by column vector -> column vector operations |
|
248 |
238
|
249 ComplexColumnVector |
|
250 operator + (const ColumnVector& a, const Complex& s) |
3
|
251 { |
238
|
252 int len = a.length (); |
|
253 return ComplexColumnVector (add (a.data (), len, s), len); |
3
|
254 } |
|
255 |
238
|
256 ComplexColumnVector |
|
257 operator - (const ColumnVector& a, const Complex& s) |
3
|
258 { |
238
|
259 int len = a.length (); |
|
260 return ComplexColumnVector (subtract (a.data (), len, s), len); |
3
|
261 } |
|
262 |
238
|
263 ComplexColumnVector |
|
264 operator * (const ColumnVector& a, const Complex& s) |
3
|
265 { |
238
|
266 int len = a.length (); |
|
267 return ComplexColumnVector (multiply (a.data (), len, s), len); |
3
|
268 } |
|
269 |
|
270 ComplexColumnVector |
238
|
271 operator / (const ColumnVector& a, const Complex& s) |
3
|
272 { |
238
|
273 int len = a.length (); |
|
274 return ComplexColumnVector (divide (a.data (), len, s), len); |
|
275 } |
|
276 |
|
277 // scalar by column vector -> column vector operations |
|
278 |
|
279 ComplexColumnVector |
|
280 operator + (const Complex& s, const ColumnVector& a) |
|
281 { |
|
282 int a_len = a.length (); |
|
283 return ComplexColumnVector (add (a.data (), a_len, s), a_len); |
3
|
284 } |
|
285 |
|
286 ComplexColumnVector |
238
|
287 operator - (const Complex& s, const ColumnVector& a) |
3
|
288 { |
238
|
289 int a_len = a.length (); |
|
290 return ComplexColumnVector (subtract (s, a.data (), a_len), a_len); |
3
|
291 } |
|
292 |
|
293 ComplexColumnVector |
238
|
294 operator * (const Complex& s, const ColumnVector& a) |
3
|
295 { |
238
|
296 int a_len = a.length (); |
|
297 return ComplexColumnVector (multiply (a.data (), a_len, s), a_len); |
3
|
298 } |
|
299 |
|
300 ComplexColumnVector |
238
|
301 operator / (const Complex& s, const ColumnVector& a) |
3
|
302 { |
238
|
303 int a_len = a.length (); |
|
304 return ComplexColumnVector (divide (s, a.data (), a_len), a_len); |
3
|
305 } |
|
306 |
|
307 // column vector by row vector -> matrix operations |
|
308 |
|
309 Matrix |
238
|
310 operator * (const ColumnVector& v, const RowVector& a) |
3
|
311 { |
238
|
312 int len = v.length (); |
|
313 int a_len = a.length (); |
|
314 if (len != a_len) |
227
|
315 { |
|
316 (*current_liboctave_error_handler) |
|
317 ("nonconformant vector multiplication attempted"); |
|
318 return Matrix (); |
|
319 } |
3
|
320 |
|
321 if (len == 0) |
|
322 return Matrix (len, len, 0.0); |
|
323 |
|
324 char transa = 'N'; |
|
325 char transb = 'N'; |
|
326 double alpha = 1.0; |
|
327 double beta = 0.0; |
|
328 int anr = 1; |
|
329 |
238
|
330 double *c = new double [len * a_len]; |
3
|
331 |
238
|
332 F77_FCN (dgemm) (&transa, &transb, &len, &a_len, &anr, &alpha, |
|
333 v.data (), &len, a.data (), &anr, &beta, c, &len, |
|
334 1L, 1L); |
3
|
335 |
238
|
336 return Matrix (c, len, a_len); |
3
|
337 } |
|
338 |
|
339 ComplexMatrix |
238
|
340 operator * (const ColumnVector& v, const ComplexRowVector& a) |
3
|
341 { |
238
|
342 ComplexColumnVector tmp (v); |
3
|
343 return tmp * a; |
|
344 } |
|
345 |
238
|
346 ComplexColumnVector |
|
347 operator + (const ColumnVector& v, const ComplexColumnVector& a) |
3
|
348 { |
238
|
349 int len = v.length (); |
|
350 if (len != a.length ()) |
227
|
351 { |
|
352 (*current_liboctave_error_handler) |
|
353 ("nonconformant vector subtraction attempted"); |
|
354 return ComplexColumnVector (); |
|
355 } |
3
|
356 |
|
357 if (len == 0) |
|
358 return ComplexColumnVector (0); |
|
359 |
238
|
360 return ComplexColumnVector (add (v.data (), a.data (), len), len); |
3
|
361 } |
|
362 |
238
|
363 ComplexColumnVector |
|
364 operator - (const ColumnVector& v, const ComplexColumnVector& a) |
3
|
365 { |
238
|
366 int len = v.length (); |
|
367 if (len != a.length ()) |
227
|
368 { |
|
369 (*current_liboctave_error_handler) |
238
|
370 ("nonconformant vector subtraction attempted"); |
|
371 return ComplexColumnVector (); |
227
|
372 } |
3
|
373 |
|
374 if (len == 0) |
238
|
375 return ComplexColumnVector (0); |
3
|
376 |
238
|
377 return ComplexColumnVector (subtract (v.data (), a.data (), len), len); |
3
|
378 } |
|
379 |
|
380 ComplexColumnVector |
238
|
381 product (const ColumnVector& v, const ComplexColumnVector& a) |
3
|
382 { |
238
|
383 int len = v.length (); |
|
384 if (len != a.length ()) |
227
|
385 { |
|
386 (*current_liboctave_error_handler) |
|
387 ("nonconformant vector product attempted"); |
|
388 return ColumnVector (); |
|
389 } |
3
|
390 |
|
391 if (len == 0) |
|
392 return ComplexColumnVector (0); |
|
393 |
238
|
394 return ComplexColumnVector (multiply (v.data (), a.data (), len), len); |
3
|
395 } |
|
396 |
|
397 ComplexColumnVector |
238
|
398 quotient (const ColumnVector& v, const ComplexColumnVector& a) |
3
|
399 { |
238
|
400 int len = v.length (); |
|
401 if (len != a.length ()) |
227
|
402 { |
|
403 (*current_liboctave_error_handler) |
|
404 ("nonconformant vector quotient attempted"); |
|
405 return ColumnVector (); |
|
406 } |
3
|
407 |
|
408 if (len == 0) |
|
409 return ComplexColumnVector (0); |
|
410 |
238
|
411 return ComplexColumnVector (divide (v.data (), a.data (), len), len); |
3
|
412 } |
|
413 |
238
|
414 // other operations |
3
|
415 |
|
416 ColumnVector |
|
417 map (d_d_Mapper f, const ColumnVector& a) |
|
418 { |
|
419 ColumnVector b (a); |
|
420 b.map (f); |
|
421 return b; |
|
422 } |
|
423 |
|
424 void |
|
425 ColumnVector::map (d_d_Mapper f) |
|
426 { |
238
|
427 for (int i = 0; i < length (); i++) |
|
428 elem (i) = f (elem (i)); |
3
|
429 } |
|
430 |
|
431 double |
|
432 ColumnVector::min (void) const |
|
433 { |
238
|
434 int len = length (); |
3
|
435 if (len == 0) |
|
436 return 0.0; |
|
437 |
238
|
438 double res = elem (0); |
3
|
439 |
|
440 for (int i = 1; i < len; i++) |
238
|
441 if (elem (i) < res) |
|
442 res = elem (i); |
3
|
443 |
|
444 return res; |
|
445 } |
|
446 |
|
447 double |
|
448 ColumnVector::max (void) const |
|
449 { |
238
|
450 int len = length (); |
3
|
451 if (len == 0) |
|
452 return 0.0; |
|
453 |
238
|
454 double res = elem (0); |
3
|
455 |
|
456 for (int i = 1; i < len; i++) |
238
|
457 if (elem (i) > res) |
|
458 res = elem (i); |
3
|
459 |
|
460 return res; |
|
461 } |
|
462 |
|
463 ostream& |
|
464 operator << (ostream& os, const ColumnVector& a) |
|
465 { |
|
466 // int field_width = os.precision () + 7; |
238
|
467 for (int i = 0; i < a.length (); i++) |
|
468 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
3
|
469 return os; |
|
470 } |
|
471 |
377
|
472 istream& |
|
473 operator >> (istream& is, ColumnVector& a) |
|
474 { |
|
475 int len = a.length(); |
|
476 |
|
477 if (len < 1) |
|
478 is.clear (ios::badbit); |
|
479 else |
|
480 { |
|
481 double tmp; |
|
482 for (int i = 0; i < len; i++) |
|
483 { |
|
484 is >> tmp; |
|
485 if (is) |
|
486 a.elem (i) = tmp; |
|
487 else |
|
488 break; |
|
489 } |
|
490 } |
|
491 } |
|
492 |
3
|
493 /* |
|
494 * Complex Column Vector class |
|
495 */ |
|
496 |
238
|
497 ComplexColumnVector::ComplexColumnVector (const ColumnVector& a) |
|
498 : Array<Complex> (a.length ()) |
3
|
499 { |
238
|
500 for (int i = 0; i < length (); i++) |
|
501 elem (i) = a.elem (i); |
3
|
502 } |
|
503 |
238
|
504 #if 0 |
3
|
505 ComplexColumnVector& |
|
506 ComplexColumnVector::resize (int n) |
|
507 { |
|
508 if (n < 0) |
227
|
509 { |
|
510 (*current_liboctave_error_handler) |
|
511 ("can't resize to negative dimension"); |
|
512 return *this; |
|
513 } |
3
|
514 |
|
515 Complex *new_data = (Complex *) NULL; |
|
516 if (n > 0) |
|
517 { |
|
518 new_data = new Complex [n]; |
|
519 int min_len = len < n ? len : n; |
|
520 |
|
521 for (int i = 0; i < min_len; i++) |
|
522 new_data[i] = data[i]; |
|
523 } |
|
524 |
|
525 delete [] data; |
|
526 len = n; |
|
527 data = new_data; |
|
528 |
|
529 return *this; |
|
530 } |
|
531 |
|
532 ComplexColumnVector& |
|
533 ComplexColumnVector::resize (int n, double val) |
|
534 { |
|
535 int old_len = len; |
|
536 resize (n); |
|
537 for (int i = old_len; i < len; i++) |
|
538 data[i] = val; |
|
539 |
|
540 return *this; |
|
541 } |
|
542 |
|
543 ComplexColumnVector& |
161
|
544 ComplexColumnVector::resize (int n, const Complex& val) |
3
|
545 { |
|
546 int old_len = len; |
|
547 resize (n); |
|
548 for (int i = old_len; i < len; i++) |
|
549 data[i] = val; |
|
550 |
|
551 return *this; |
|
552 } |
238
|
553 #endif |
3
|
554 |
|
555 int |
|
556 ComplexColumnVector::operator == (const ComplexColumnVector& a) const |
|
557 { |
238
|
558 int len = length (); |
|
559 if (len != a.length ()) |
3
|
560 return 0; |
238
|
561 return equal (data (), a.data (), len); |
3
|
562 } |
|
563 |
|
564 int |
|
565 ComplexColumnVector::operator != (const ComplexColumnVector& a) const |
|
566 { |
238
|
567 return !(*this == a); |
3
|
568 } |
|
569 |
|
570 // destructive insert/delete/reorder operations |
|
571 |
|
572 ComplexColumnVector& |
|
573 ComplexColumnVector::insert (const ColumnVector& a, int r) |
|
574 { |
238
|
575 int a_len = a.length (); |
|
576 if (r < 0 || r + a_len - 1 > length ()) |
227
|
577 { |
|
578 (*current_liboctave_error_handler) ("range error for insert"); |
|
579 return *this; |
|
580 } |
3
|
581 |
238
|
582 for (int i = 0; i < a_len; i++) |
|
583 elem (r+i) = a.elem (i); |
3
|
584 |
|
585 return *this; |
|
586 } |
|
587 |
|
588 ComplexColumnVector& |
|
589 ComplexColumnVector::insert (const ComplexColumnVector& a, int r) |
|
590 { |
238
|
591 int a_len = a.length (); |
|
592 if (r < 0 || r + a_len - 1 > length ()) |
227
|
593 { |
|
594 (*current_liboctave_error_handler) ("range error for insert"); |
|
595 return *this; |
|
596 } |
3
|
597 |
238
|
598 for (int i = 0; i < a_len; i++) |
|
599 elem (r+i) = a.elem (i); |
3
|
600 |
|
601 return *this; |
|
602 } |
|
603 |
|
604 ComplexColumnVector& |
|
605 ComplexColumnVector::fill (double val) |
|
606 { |
238
|
607 int len = length (); |
3
|
608 if (len > 0) |
238
|
609 for (int i = 0; i < len; i++) |
|
610 elem (i) = val; |
3
|
611 return *this; |
|
612 } |
|
613 |
|
614 ComplexColumnVector& |
161
|
615 ComplexColumnVector::fill (const Complex& val) |
3
|
616 { |
238
|
617 int len = length (); |
3
|
618 if (len > 0) |
238
|
619 for (int i = 0; i < len; i++) |
|
620 elem (i) = val; |
3
|
621 return *this; |
|
622 } |
|
623 |
|
624 ComplexColumnVector& |
|
625 ComplexColumnVector::fill (double val, int r1, int r2) |
|
626 { |
238
|
627 int len = length (); |
3
|
628 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
227
|
629 { |
|
630 (*current_liboctave_error_handler) ("range error for fill"); |
|
631 return *this; |
|
632 } |
3
|
633 |
|
634 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
635 |
|
636 for (int i = r1; i <= r2; i++) |
238
|
637 elem (i) = val; |
3
|
638 |
|
639 return *this; |
|
640 } |
|
641 |
|
642 ComplexColumnVector& |
161
|
643 ComplexColumnVector::fill (const Complex& val, int r1, int r2) |
3
|
644 { |
238
|
645 int len = length (); |
3
|
646 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
227
|
647 { |
|
648 (*current_liboctave_error_handler) ("range error for fill"); |
|
649 return *this; |
|
650 } |
3
|
651 |
|
652 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
653 |
|
654 for (int i = r1; i <= r2; i++) |
238
|
655 elem (i) = val; |
3
|
656 |
|
657 return *this; |
|
658 } |
|
659 |
|
660 ComplexColumnVector |
|
661 ComplexColumnVector::stack (const ColumnVector& a) const |
|
662 { |
238
|
663 int len = length (); |
3
|
664 int nr_insert = len; |
238
|
665 ComplexColumnVector retval (len + a.length ()); |
3
|
666 retval.insert (*this, 0); |
|
667 retval.insert (a, nr_insert); |
|
668 return retval; |
|
669 } |
|
670 |
|
671 ComplexColumnVector |
|
672 ComplexColumnVector::stack (const ComplexColumnVector& a) const |
|
673 { |
238
|
674 int len = length (); |
3
|
675 int nr_insert = len; |
238
|
676 ComplexColumnVector retval (len + a.length ()); |
3
|
677 retval.insert (*this, 0); |
|
678 retval.insert (a, nr_insert); |
|
679 return retval; |
|
680 } |
|
681 |
|
682 ComplexRowVector |
|
683 ComplexColumnVector::hermitian (void) const |
|
684 { |
238
|
685 int len = length (); |
|
686 return ComplexRowVector (conj_dup (data (), len), len); |
3
|
687 } |
|
688 |
|
689 ComplexRowVector |
|
690 ComplexColumnVector::transpose (void) const |
|
691 { |
238
|
692 int len = length (); |
|
693 return ComplexRowVector (dup (data (), len), len); |
3
|
694 } |
|
695 |
|
696 ColumnVector |
|
697 real (const ComplexColumnVector& a) |
|
698 { |
238
|
699 int a_len = a.length (); |
3
|
700 ColumnVector retval; |
238
|
701 if (a_len > 0) |
|
702 retval = ColumnVector (real_dup (a.data (), a_len), a_len); |
3
|
703 return retval; |
|
704 } |
|
705 |
|
706 ColumnVector |
|
707 imag (const ComplexColumnVector& a) |
|
708 { |
238
|
709 int a_len = a.length (); |
3
|
710 ColumnVector retval; |
238
|
711 if (a_len > 0) |
|
712 retval = ColumnVector (imag_dup (a.data (), a_len), a_len); |
3
|
713 return retval; |
|
714 } |
|
715 |
|
716 ComplexColumnVector |
|
717 conj (const ComplexColumnVector& a) |
|
718 { |
238
|
719 int a_len = a.length (); |
3
|
720 ComplexColumnVector retval; |
238
|
721 if (a_len > 0) |
|
722 retval = ComplexColumnVector (conj_dup (a.data (), a_len), a_len); |
3
|
723 return retval; |
|
724 } |
|
725 |
|
726 // resize is the destructive equivalent for this one |
|
727 |
|
728 ComplexColumnVector |
|
729 ComplexColumnVector::extract (int r1, int r2) const |
|
730 { |
|
731 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
732 |
|
733 int new_r = r2 - r1 + 1; |
|
734 |
|
735 ComplexColumnVector result (new_r); |
|
736 |
|
737 for (int i = 0; i < new_r; i++) |
238
|
738 result.elem (i) = elem (r1+i); |
3
|
739 |
|
740 return result; |
|
741 } |
|
742 |
238
|
743 // column vector by column vector -> column vector operations |
|
744 |
|
745 ComplexColumnVector& |
|
746 ComplexColumnVector::operator += (const ColumnVector& a) |
|
747 { |
|
748 int len = length (); |
|
749 if (len != a.length ()) |
|
750 { |
|
751 (*current_liboctave_error_handler) |
|
752 ("nonconformant vector += operation attempted"); |
|
753 return *this; |
|
754 } |
|
755 |
|
756 if (len == 0) |
|
757 return *this; |
|
758 |
|
759 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
760 |
|
761 add2 (d, a.data (), len); |
|
762 return *this; |
|
763 } |
|
764 |
|
765 ComplexColumnVector& |
|
766 ComplexColumnVector::operator -= (const ColumnVector& a) |
|
767 { |
|
768 int len = length (); |
|
769 if (len != a.length ()) |
|
770 { |
|
771 (*current_liboctave_error_handler) |
|
772 ("nonconformant vector -= operation attempted"); |
|
773 return *this; |
|
774 } |
|
775 |
|
776 if (len == 0) |
|
777 return *this; |
|
778 |
|
779 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
780 |
|
781 subtract2 (d, a.data (), len); |
|
782 return *this; |
|
783 } |
|
784 |
|
785 ComplexColumnVector& |
|
786 ComplexColumnVector::operator += (const ComplexColumnVector& a) |
|
787 { |
|
788 int len = length (); |
|
789 |
|
790 if (len != a.length ()) |
|
791 { |
|
792 (*current_liboctave_error_handler) |
|
793 ("nonconformant vector += operation attempted"); |
|
794 return *this; |
|
795 } |
|
796 |
|
797 if (len == 0) |
|
798 return *this; |
|
799 |
|
800 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
801 |
|
802 add2 (d, a.data (), len); |
|
803 return *this; |
|
804 } |
|
805 |
|
806 ComplexColumnVector& |
|
807 ComplexColumnVector::operator -= (const ComplexColumnVector& a) |
|
808 { |
|
809 int len = length (); |
|
810 if (len != a.length ()) |
|
811 { |
|
812 (*current_liboctave_error_handler) |
|
813 ("nonconformant vector -= operation attempted"); |
|
814 return *this; |
|
815 } |
|
816 |
|
817 if (len == 0) |
|
818 return *this; |
|
819 |
|
820 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
821 |
|
822 subtract2 (d, a.data (), len); |
|
823 return *this; |
|
824 } |
|
825 |
3
|
826 // column vector by scalar -> column vector operations |
|
827 |
|
828 ComplexColumnVector |
238
|
829 operator + (const ComplexColumnVector& v, double s) |
3
|
830 { |
238
|
831 int len = v.length (); |
|
832 return ComplexColumnVector (add (v.data (), len, s), len); |
3
|
833 } |
|
834 |
|
835 ComplexColumnVector |
238
|
836 operator - (const ComplexColumnVector& v, double s) |
3
|
837 { |
238
|
838 int len = v.length (); |
|
839 return ComplexColumnVector (subtract (v.data (), len, s), len); |
3
|
840 } |
|
841 |
|
842 ComplexColumnVector |
238
|
843 operator * (const ComplexColumnVector& v, double s) |
3
|
844 { |
238
|
845 int len = v.length (); |
|
846 return ComplexColumnVector (multiply (v.data (), len, s), len); |
3
|
847 } |
|
848 |
|
849 ComplexColumnVector |
238
|
850 operator / (const ComplexColumnVector& v, double s) |
3
|
851 { |
238
|
852 int len = v.length (); |
|
853 return ComplexColumnVector (divide (v.data (), len, s), len); |
3
|
854 } |
|
855 |
|
856 // scalar by column vector -> column vector operations |
|
857 |
|
858 ComplexColumnVector |
|
859 operator + (double s, const ComplexColumnVector& a) |
|
860 { |
238
|
861 int a_len = a.length (); |
|
862 return ComplexColumnVector (add (a.data (), a_len, s), a_len); |
3
|
863 } |
|
864 |
|
865 ComplexColumnVector |
|
866 operator - (double s, const ComplexColumnVector& a) |
|
867 { |
238
|
868 int a_len = a.length (); |
|
869 return ComplexColumnVector (subtract (s, a.data (), a_len), a_len); |
3
|
870 } |
|
871 |
|
872 ComplexColumnVector |
|
873 operator * (double s, const ComplexColumnVector& a) |
|
874 { |
238
|
875 int a_len = a.length (); |
|
876 return ComplexColumnVector (multiply (a.data (), a_len, s), a_len); |
3
|
877 } |
|
878 |
|
879 ComplexColumnVector |
|
880 operator / (double s, const ComplexColumnVector& a) |
|
881 { |
238
|
882 int a_len = a.length (); |
|
883 return ComplexColumnVector (divide (s, a.data (), a_len), a_len); |
3
|
884 } |
|
885 |
|
886 // column vector by row vector -> matrix operations |
|
887 |
|
888 ComplexMatrix |
238
|
889 operator * (const ComplexColumnVector& v, const ComplexRowVector& a) |
3
|
890 { |
238
|
891 int len = v.length (); |
|
892 int a_len = a.length (); |
|
893 if (len != a_len) |
227
|
894 { |
|
895 (*current_liboctave_error_handler) |
|
896 ("nonconformant vector multiplication attempted"); |
|
897 return ComplexMatrix (); |
|
898 } |
3
|
899 |
|
900 if (len == 0) |
|
901 return ComplexMatrix (len, len, 0.0); |
|
902 |
|
903 char transa = 'N'; |
|
904 char transb = 'N'; |
|
905 Complex alpha (1.0); |
|
906 Complex beta (0.0); |
|
907 int anr = 1; |
|
908 |
238
|
909 Complex *c = new Complex [len * a_len]; |
3
|
910 |
238
|
911 F77_FCN (zgemm) (&transa, &transb, &len, &a_len, &anr, &alpha, |
|
912 v.data (), &len, a.data (), &anr, &beta, c, &len, |
|
913 1L, 1L); |
3
|
914 |
238
|
915 return ComplexMatrix (c, len, a_len); |
3
|
916 } |
|
917 |
|
918 // column vector by column vector -> column vector operations |
|
919 |
|
920 ComplexColumnVector |
238
|
921 operator + (const ComplexColumnVector& v, const ColumnVector& a) |
3
|
922 { |
238
|
923 int len = v.length (); |
|
924 if (len != a.length ()) |
227
|
925 { |
|
926 (*current_liboctave_error_handler) |
|
927 ("nonconformant vector addition attempted"); |
|
928 return ComplexColumnVector (); |
|
929 } |
3
|
930 |
|
931 if (len == 0) |
|
932 return ComplexColumnVector (0); |
|
933 |
238
|
934 return ComplexColumnVector (add (v.data (), a.data (), len), len); |
3
|
935 } |
|
936 |
|
937 ComplexColumnVector |
238
|
938 operator - (const ComplexColumnVector& v, const ColumnVector& a) |
3
|
939 { |
238
|
940 int len = v.length (); |
|
941 if (len != a.length ()) |
227
|
942 { |
|
943 (*current_liboctave_error_handler) |
|
944 ("nonconformant vector subtraction attempted"); |
|
945 return ComplexColumnVector (); |
|
946 } |
3
|
947 |
|
948 if (len == 0) |
|
949 return ComplexColumnVector (0); |
|
950 |
238
|
951 return ComplexColumnVector (subtract (v.data (), a.data (), len), len); |
3
|
952 } |
|
953 |
|
954 ComplexColumnVector |
238
|
955 product (const ComplexColumnVector& v, const ColumnVector& a) |
3
|
956 { |
238
|
957 int len = v.length (); |
|
958 if (len != a.length ()) |
227
|
959 { |
|
960 (*current_liboctave_error_handler) |
|
961 ("nonconformant vector product attempted"); |
|
962 return ComplexColumnVector (); |
|
963 } |
3
|
964 |
|
965 if (len == 0) |
|
966 return ComplexColumnVector (0); |
|
967 |
238
|
968 return ComplexColumnVector (multiply (v.data (), a.data (), len), len); |
3
|
969 } |
|
970 |
|
971 ComplexColumnVector |
238
|
972 quotient (const ComplexColumnVector& v, const ColumnVector& a) |
3
|
973 { |
238
|
974 int len = v.length (); |
|
975 if (len != a.length ()) |
227
|
976 { |
|
977 (*current_liboctave_error_handler) |
|
978 ("nonconformant vector quotient attempted"); |
|
979 return ComplexColumnVector (); |
|
980 } |
3
|
981 |
|
982 if (len == 0) |
|
983 return ComplexColumnVector (0); |
|
984 |
238
|
985 return ComplexColumnVector (divide (v.data (), a.data (), len), len); |
3
|
986 } |
|
987 |
238
|
988 // other operations |
3
|
989 |
|
990 ComplexColumnVector |
|
991 map (c_c_Mapper f, const ComplexColumnVector& a) |
|
992 { |
|
993 ComplexColumnVector b (a); |
|
994 b.map (f); |
|
995 return b; |
|
996 } |
|
997 |
|
998 ColumnVector |
|
999 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
1000 { |
238
|
1001 int a_len = a.length (); |
|
1002 ColumnVector b (a_len); |
|
1003 for (int i = 0; i < a_len; i++) |
3
|
1004 b.elem (i) = f (a.elem (i)); |
|
1005 return b; |
|
1006 } |
|
1007 |
|
1008 void |
|
1009 ComplexColumnVector::map (c_c_Mapper f) |
|
1010 { |
238
|
1011 for (int i = 0; i < length (); i++) |
|
1012 elem (i) = f (elem (i)); |
3
|
1013 } |
|
1014 |
|
1015 Complex |
|
1016 ComplexColumnVector::min (void) const |
|
1017 { |
238
|
1018 int len = length (); |
3
|
1019 if (len == 0) |
|
1020 return 0.0; |
|
1021 |
238
|
1022 Complex res = elem (0); |
3
|
1023 double absres = abs (res); |
|
1024 |
|
1025 for (int i = 1; i < len; i++) |
238
|
1026 if (abs (elem (i)) < absres) |
3
|
1027 { |
238
|
1028 res = elem (i); |
3
|
1029 absres = abs (res); |
|
1030 } |
|
1031 |
|
1032 return res; |
|
1033 } |
|
1034 |
|
1035 Complex |
|
1036 ComplexColumnVector::max (void) const |
|
1037 { |
238
|
1038 int len = length (); |
3
|
1039 if (len == 0) |
|
1040 return 0.0; |
|
1041 |
238
|
1042 Complex res = elem (0); |
3
|
1043 double absres = abs (res); |
|
1044 |
|
1045 for (int i = 1; i < len; i++) |
238
|
1046 if (abs (elem (i)) > absres) |
3
|
1047 { |
238
|
1048 res = elem (i); |
3
|
1049 absres = abs (res); |
|
1050 } |
|
1051 |
|
1052 return res; |
|
1053 } |
|
1054 |
|
1055 // i/o |
|
1056 |
|
1057 ostream& |
|
1058 operator << (ostream& os, const ComplexColumnVector& a) |
|
1059 { |
|
1060 // int field_width = os.precision () + 7; |
238
|
1061 for (int i = 0; i < a.length (); i++) |
|
1062 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
3
|
1063 return os; |
|
1064 } |
|
1065 |
377
|
1066 istream& |
|
1067 operator >> (istream& is, ComplexColumnVector& a) |
|
1068 { |
|
1069 int len = a.length(); |
|
1070 |
|
1071 if (len < 1) |
|
1072 is.clear (ios::badbit); |
|
1073 else |
|
1074 { |
|
1075 double tmp; |
|
1076 for (int i = 0; i < len; i++) |
|
1077 { |
|
1078 is >> tmp; |
|
1079 if (is) |
|
1080 a.elem (i) = tmp; |
|
1081 else |
|
1082 break; |
|
1083 } |
|
1084 } |
|
1085 } |
|
1086 |
3
|
1087 /* |
|
1088 ;;; Local Variables: *** |
|
1089 ;;; mode: C++ *** |
|
1090 ;;; page-delimiter: "^/\\*" *** |
|
1091 ;;; End: *** |
|
1092 */ |