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