3
|
1 // RowVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 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 |
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 |
|
494 /* |
|
495 * Complex Row Vector class |
|
496 */ |
|
497 |
238
|
498 ComplexRowVector::ComplexRowVector (const RowVector& a) |
|
499 : Array<Complex> (a.length ()) |
3
|
500 { |
238
|
501 for (int i = 0; i < length (); i++) |
|
502 elem (i) = a.elem (i); |
3
|
503 } |
|
504 |
238
|
505 #if 0 |
3
|
506 ComplexRowVector& |
|
507 ComplexRowVector::resize (int n) |
|
508 { |
|
509 if (n < 0) |
227
|
510 { |
|
511 (*current_liboctave_error_handler) |
|
512 ("can't resize to negative dimension"); |
|
513 return *this; |
|
514 } |
3
|
515 |
|
516 Complex *new_data = (Complex *) NULL; |
|
517 if (n > 0) |
|
518 { |
|
519 new_data = new Complex [n]; |
|
520 int min_len = len < n ? len : n; |
|
521 |
|
522 for (int i = 0; i < min_len; i++) |
|
523 new_data[i] = data[i]; |
|
524 } |
|
525 |
|
526 delete [] data; |
|
527 len = n; |
|
528 data = new_data; |
|
529 |
|
530 return *this; |
|
531 } |
|
532 |
|
533 ComplexRowVector& |
|
534 ComplexRowVector::resize (int n, double val) |
|
535 { |
|
536 int old_len = len; |
|
537 resize (n); |
|
538 for (int i = old_len; i < len; i++) |
|
539 data[i] = val; |
|
540 |
|
541 return *this; |
|
542 } |
|
543 |
|
544 ComplexRowVector& |
161
|
545 ComplexRowVector::resize (int n, const Complex& val) |
3
|
546 { |
|
547 int old_len = len; |
|
548 resize (n); |
|
549 for (int i = old_len; i < len; i++) |
|
550 data[i] = val; |
|
551 |
|
552 return *this; |
|
553 } |
238
|
554 #endif |
3
|
555 |
|
556 int |
|
557 ComplexRowVector::operator == (const ComplexRowVector& a) const |
|
558 { |
238
|
559 int len = length (); |
|
560 if (len != a.length ()) |
3
|
561 return 0; |
238
|
562 return equal (data (), a.data (), len); |
3
|
563 } |
|
564 |
|
565 int |
|
566 ComplexRowVector::operator != (const ComplexRowVector& a) const |
|
567 { |
238
|
568 return !(*this == a); |
3
|
569 } |
|
570 |
|
571 // destructive insert/delete/reorder operations |
|
572 |
|
573 ComplexRowVector& |
|
574 ComplexRowVector::insert (const RowVector& a, int c) |
|
575 { |
238
|
576 int a_len = a.length (); |
|
577 if (c < 0 || c + a_len - 1 > length ()) |
227
|
578 { |
|
579 (*current_liboctave_error_handler) ("range error for insert"); |
|
580 return *this; |
|
581 } |
3
|
582 |
238
|
583 for (int i = 0; i < a_len; i++) |
|
584 elem (c+i) = a.elem (i); |
3
|
585 |
|
586 return *this; |
|
587 } |
|
588 |
|
589 ComplexRowVector& |
|
590 ComplexRowVector::insert (const ComplexRowVector& a, int c) |
|
591 { |
238
|
592 int a_len = a.length (); |
|
593 if (c < 0 || c + a_len - 1 > length ()) |
227
|
594 { |
|
595 (*current_liboctave_error_handler) ("range error for insert"); |
|
596 return *this; |
|
597 } |
3
|
598 |
238
|
599 for (int i = 0; i < a_len; i++) |
|
600 elem (c+i) = a.elem (i); |
3
|
601 |
|
602 return *this; |
|
603 } |
|
604 |
|
605 ComplexRowVector& |
|
606 ComplexRowVector::fill (double val) |
|
607 { |
238
|
608 int len = length (); |
3
|
609 if (len > 0) |
238
|
610 for (int i = 0; i < len; i++) |
|
611 elem (i) = val; |
3
|
612 return *this; |
|
613 } |
|
614 |
|
615 ComplexRowVector& |
161
|
616 ComplexRowVector::fill (const Complex& val) |
3
|
617 { |
238
|
618 int len = length (); |
3
|
619 if (len > 0) |
238
|
620 for (int i = 0; i < len; i++) |
|
621 elem (i) = val; |
3
|
622 return *this; |
|
623 } |
|
624 |
|
625 ComplexRowVector& |
|
626 ComplexRowVector::fill (double val, int c1, int c2) |
|
627 { |
238
|
628 int len = length (); |
3
|
629 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
227
|
630 { |
|
631 (*current_liboctave_error_handler) ("range error for fill"); |
|
632 return *this; |
|
633 } |
3
|
634 |
|
635 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
636 |
|
637 for (int i = c1; i <= c2; i++) |
238
|
638 elem (i) = val; |
3
|
639 |
|
640 return *this; |
|
641 } |
|
642 |
|
643 ComplexRowVector& |
161
|
644 ComplexRowVector::fill (const Complex& val, int c1, int c2) |
3
|
645 { |
238
|
646 int len = length (); |
3
|
647 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
227
|
648 { |
|
649 (*current_liboctave_error_handler) ("range error for fill"); |
|
650 return *this; |
|
651 } |
3
|
652 |
|
653 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
654 |
|
655 for (int i = c1; i <= c2; i++) |
238
|
656 elem (i) = val; |
3
|
657 |
|
658 return *this; |
|
659 } |
|
660 |
|
661 ComplexRowVector |
|
662 ComplexRowVector::append (const RowVector& a) const |
|
663 { |
238
|
664 int len = length (); |
3
|
665 int nc_insert = len; |
238
|
666 ComplexRowVector retval (len + a.length ()); |
3
|
667 retval.insert (*this, 0); |
|
668 retval.insert (a, nc_insert); |
|
669 return retval; |
|
670 } |
|
671 |
|
672 ComplexRowVector |
|
673 ComplexRowVector::append (const ComplexRowVector& a) const |
|
674 { |
238
|
675 int len = length (); |
3
|
676 int nc_insert = len; |
238
|
677 ComplexRowVector retval (len + a.length ()); |
3
|
678 retval.insert (*this, 0); |
|
679 retval.insert (a, nc_insert); |
|
680 return retval; |
|
681 } |
|
682 |
|
683 ComplexColumnVector |
|
684 ComplexRowVector::hermitian (void) const |
|
685 { |
238
|
686 int len = length (); |
|
687 return ComplexColumnVector (conj_dup (data (), len), len); |
3
|
688 } |
|
689 |
|
690 ComplexColumnVector |
|
691 ComplexRowVector::transpose (void) const |
|
692 { |
238
|
693 int len = length (); |
|
694 return ComplexColumnVector (dup (data (), len), len); |
3
|
695 } |
|
696 |
|
697 RowVector |
|
698 real (const ComplexRowVector& a) |
|
699 { |
238
|
700 int a_len = a.length (); |
3
|
701 RowVector retval; |
238
|
702 if (a_len > 0) |
|
703 retval = RowVector (real_dup (a.data (), a_len), a_len); |
3
|
704 return retval; |
|
705 } |
|
706 |
|
707 RowVector |
|
708 imag (const ComplexRowVector& a) |
|
709 { |
238
|
710 int a_len = a.length (); |
3
|
711 RowVector retval; |
238
|
712 if (a_len > 0) |
|
713 retval = RowVector (imag_dup (a.data (), a_len), a_len); |
3
|
714 return retval; |
|
715 } |
|
716 |
|
717 ComplexRowVector |
|
718 conj (const ComplexRowVector& a) |
|
719 { |
238
|
720 int a_len = a.length (); |
3
|
721 ComplexRowVector retval; |
238
|
722 if (a_len > 0) |
|
723 retval = ComplexRowVector (conj_dup (a.data (), a_len), a_len); |
3
|
724 return retval; |
|
725 } |
|
726 |
|
727 // resize is the destructive equivalent for this one |
|
728 |
|
729 ComplexRowVector |
|
730 ComplexRowVector::extract (int c1, int c2) const |
|
731 { |
|
732 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
733 |
|
734 int new_c = c2 - c1 + 1; |
|
735 |
|
736 ComplexRowVector result (new_c); |
|
737 |
|
738 for (int i = 0; i < new_c; i++) |
238
|
739 result.elem (i) = elem (c1+i); |
3
|
740 |
|
741 return result; |
|
742 } |
|
743 |
238
|
744 // row vector by row vector -> row vector operations |
|
745 |
|
746 ComplexRowVector& |
|
747 ComplexRowVector::operator += (const RowVector& a) |
|
748 { |
|
749 int len = length (); |
|
750 if (len != a.length ()) |
|
751 { |
|
752 (*current_liboctave_error_handler) |
|
753 ("nonconformant vector += operation attempted"); |
|
754 return *this; |
|
755 } |
|
756 |
|
757 if (len == 0) |
|
758 return *this; |
|
759 |
|
760 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
761 |
|
762 add2 (d, a.data (), len); |
|
763 return *this; |
|
764 } |
|
765 |
|
766 ComplexRowVector& |
|
767 ComplexRowVector::operator -= (const RowVector& a) |
|
768 { |
|
769 int len = length (); |
|
770 if (len != a.length ()) |
|
771 { |
|
772 (*current_liboctave_error_handler) |
|
773 ("nonconformant vector -= operation attempted"); |
|
774 return *this; |
|
775 } |
|
776 |
|
777 if (len == 0) |
|
778 return *this; |
|
779 |
|
780 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
781 |
|
782 subtract2 (d, a.data (), len); |
|
783 return *this; |
|
784 } |
|
785 |
|
786 ComplexRowVector& |
|
787 ComplexRowVector::operator += (const ComplexRowVector& a) |
|
788 { |
|
789 int len = length (); |
|
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 ComplexRowVector& |
|
807 ComplexRowVector::operator -= (const ComplexRowVector& 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 // row vector by scalar -> row vector operations |
|
827 |
|
828 ComplexRowVector |
238
|
829 operator + (const ComplexRowVector& v, double s) |
3
|
830 { |
238
|
831 int len = v.length (); |
|
832 return ComplexRowVector (add (v.data (), len, s), len); |
3
|
833 } |
|
834 |
|
835 ComplexRowVector |
238
|
836 operator - (const ComplexRowVector& v, double s) |
3
|
837 { |
238
|
838 int len = v.length (); |
|
839 return ComplexRowVector (subtract (v.data (), len, s), len); |
3
|
840 } |
|
841 |
|
842 ComplexRowVector |
238
|
843 operator * (const ComplexRowVector& v, double s) |
3
|
844 { |
238
|
845 int len = v.length (); |
|
846 return ComplexRowVector (multiply (v.data (), len, s), len); |
3
|
847 } |
|
848 |
|
849 ComplexRowVector |
238
|
850 operator / (const ComplexRowVector& v, double s) |
3
|
851 { |
238
|
852 int len = v.length (); |
|
853 return ComplexRowVector (divide (v.data (), len, s), len); |
3
|
854 } |
|
855 |
|
856 // scalar by row vector -> row vector operations |
|
857 |
|
858 ComplexRowVector |
|
859 operator + (double s, const ComplexRowVector& a) |
|
860 { |
238
|
861 int a_len = a.length (); |
|
862 return ComplexRowVector (add (a.data (), a_len, s), a_len); |
3
|
863 } |
|
864 |
|
865 ComplexRowVector |
|
866 operator - (double s, const ComplexRowVector& a) |
|
867 { |
238
|
868 int a_len = a.length (); |
|
869 return ComplexRowVector (subtract (s, a.data (), a_len), a_len); |
3
|
870 } |
|
871 |
|
872 ComplexRowVector |
|
873 operator * (double s, const ComplexRowVector& a) |
|
874 { |
238
|
875 int a_len = a.length (); |
|
876 return ComplexRowVector (multiply (a.data (), a_len, s), a_len); |
3
|
877 } |
|
878 |
|
879 ComplexRowVector |
|
880 operator / (double s, const ComplexRowVector& a) |
|
881 { |
238
|
882 int a_len = a.length (); |
|
883 return ComplexRowVector (divide (s, a.data (), a_len), a_len); |
3
|
884 } |
|
885 |
|
886 // row vector by column vector -> scalar |
|
887 |
|
888 Complex |
238
|
889 operator * (const ComplexRowVector& v, const ColumnVector& a) |
3
|
890 { |
|
891 ComplexColumnVector tmp (a); |
238
|
892 return v * tmp; |
3
|
893 } |
|
894 |
|
895 Complex |
238
|
896 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) |
3
|
897 { |
|
898 // XXX FIXME XXX -- need function body |
|
899 assert (0); |
238
|
900 return Complex (); |
3
|
901 } |
|
902 |
|
903 // row vector by matrix -> row vector |
|
904 |
|
905 ComplexRowVector |
238
|
906 operator * (const ComplexRowVector& v, const ComplexMatrix& a) |
3
|
907 { |
238
|
908 int len = v.length (); |
|
909 if (a.rows () != len) |
227
|
910 { |
|
911 (*current_liboctave_error_handler) |
|
912 ("nonconformant vector multiplication attempted"); |
|
913 return ComplexRowVector (); |
|
914 } |
3
|
915 |
238
|
916 if (len == 0 || a.cols () == 0) |
3
|
917 return ComplexRowVector (0); |
|
918 |
|
919 // Transpose A to form A'*x == (x'*A)' |
|
920 |
238
|
921 int a_nr = a.rows (); |
|
922 int a_nc = a.cols (); |
3
|
923 |
|
924 char trans = 'T'; |
238
|
925 int ld = a_nr; |
3
|
926 Complex alpha (1.0); |
|
927 Complex beta (0.0); |
|
928 int i_one = 1; |
|
929 |
|
930 Complex *y = new Complex [len]; |
|
931 |
238
|
932 F77_FCN (zgemv) (&trans, &a_nc, &a_nr, &alpha, a.data (), &ld, |
|
933 v.data (), &i_one, &beta, y, &i_one, 1L); |
3
|
934 |
|
935 return ComplexRowVector (y, len); |
|
936 } |
|
937 |
|
938 // row vector by row vector -> row vector operations |
|
939 |
|
940 ComplexRowVector |
238
|
941 operator + (const ComplexRowVector& v, const RowVector& a) |
3
|
942 { |
238
|
943 int len = v.length (); |
|
944 if (len != a.length ()) |
227
|
945 { |
|
946 (*current_liboctave_error_handler) |
|
947 ("nonconformant vector addition attempted"); |
|
948 return ComplexRowVector (); |
|
949 } |
3
|
950 |
|
951 if (len == 0) |
|
952 return ComplexRowVector (0); |
|
953 |
238
|
954 return ComplexRowVector (add (v.data (), a.data (), len), len); |
3
|
955 } |
|
956 |
|
957 ComplexRowVector |
238
|
958 operator - (const ComplexRowVector& v, const RowVector& a) |
3
|
959 { |
238
|
960 int len = v.length (); |
|
961 if (len != a.length ()) |
227
|
962 { |
|
963 (*current_liboctave_error_handler) |
|
964 ("nonconformant vector subtraction attempted"); |
|
965 return ComplexRowVector (); |
|
966 } |
3
|
967 |
|
968 if (len == 0) |
|
969 return ComplexRowVector (0); |
|
970 |
238
|
971 return ComplexRowVector (subtract (v.data (), a.data (), len), len); |
3
|
972 } |
|
973 |
|
974 ComplexRowVector |
238
|
975 product (const ComplexRowVector& v, const RowVector& a) |
3
|
976 { |
238
|
977 int len = v.length (); |
|
978 if (len != a.length ()) |
227
|
979 { |
|
980 (*current_liboctave_error_handler) |
|
981 ("nonconformant vector product attempted"); |
|
982 return ComplexRowVector (); |
|
983 } |
3
|
984 |
|
985 if (len == 0) |
|
986 return ComplexRowVector (0); |
|
987 |
238
|
988 return ComplexRowVector (multiply (v.data (), a.data (), len), len); |
3
|
989 } |
|
990 |
|
991 ComplexRowVector |
238
|
992 quotient (const ComplexRowVector& v, const RowVector& a) |
3
|
993 { |
238
|
994 int len = v.length (); |
|
995 if (len != a.length ()) |
227
|
996 { |
|
997 (*current_liboctave_error_handler) |
|
998 ("nonconformant vector quotient attempted"); |
|
999 return ComplexRowVector (); |
|
1000 } |
3
|
1001 |
|
1002 if (len == 0) |
|
1003 return ComplexRowVector (0); |
|
1004 |
238
|
1005 return ComplexRowVector (divide (v.data (), a.data (), len), len); |
3
|
1006 } |
|
1007 |
238
|
1008 // other operations |
3
|
1009 |
|
1010 ComplexRowVector |
|
1011 map (c_c_Mapper f, const ComplexRowVector& a) |
|
1012 { |
|
1013 ComplexRowVector b (a); |
|
1014 b.map (f); |
|
1015 return b; |
|
1016 } |
|
1017 |
|
1018 RowVector |
|
1019 map (d_c_Mapper f, const ComplexRowVector& a) |
|
1020 { |
238
|
1021 int a_len = a.length (); |
|
1022 RowVector b (a_len); |
|
1023 for (int i = 0; i < a_len; i++) |
3
|
1024 b.elem (i) = f (a.elem (i)); |
|
1025 return b; |
|
1026 } |
|
1027 |
|
1028 void |
|
1029 ComplexRowVector::map (c_c_Mapper f) |
|
1030 { |
238
|
1031 for (int i = 0; i < length (); i++) |
|
1032 elem (i) = f (elem (i)); |
3
|
1033 } |
|
1034 |
|
1035 Complex |
|
1036 ComplexRowVector::min (void) const |
|
1037 { |
238
|
1038 int len = length (); |
3
|
1039 if (len == 0) |
|
1040 return Complex (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 Complex |
|
1056 ComplexRowVector::max (void) const |
|
1057 { |
238
|
1058 int len = length (); |
3
|
1059 if (len == 0) |
|
1060 return Complex (0.0); |
|
1061 |
238
|
1062 Complex res = elem (0); |
3
|
1063 double absres = abs (res); |
|
1064 |
|
1065 for (int i = 1; i < len; i++) |
238
|
1066 if (abs (elem (i)) > absres) |
3
|
1067 { |
238
|
1068 res = elem (i); |
3
|
1069 absres = abs (res); |
|
1070 } |
|
1071 |
|
1072 return res; |
|
1073 } |
|
1074 |
|
1075 // i/o |
|
1076 |
|
1077 ostream& |
|
1078 operator << (ostream& os, const ComplexRowVector& a) |
|
1079 { |
|
1080 // int field_width = os.precision () + 7; |
238
|
1081 for (int i = 0; i < a.length (); i++) |
|
1082 os << " " /* setw (field_width) */ << a.elem (i); |
3
|
1083 return os; |
|
1084 } |
|
1085 |
|
1086 /* |
|
1087 ;;; Local Variables: *** |
|
1088 ;;; mode: C++ *** |
|
1089 ;;; page-delimiter: "^/\\*" *** |
|
1090 ;;; End: *** |
|
1091 */ |