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 |
|
24 // I\'m not sure how this is supposed to work if the .h file declares |
|
25 // several classes, each of which is defined in a separate file... |
|
26 // |
|
27 // #ifdef __GNUG__ |
|
28 // #pragma implementation "Matrix.h" |
|
29 // #endif |
|
30 |
|
31 #include "Matrix.h" |
|
32 #include "mx-inlines.cc" |
227
|
33 #include "lo-error.h" |
3
|
34 |
|
35 /* |
|
36 * Row Vector class. |
|
37 */ |
|
38 |
|
39 RowVector::RowVector (int n) |
|
40 { |
|
41 if (n < 0) |
227
|
42 { |
|
43 (*current_liboctave_error_handler) |
|
44 ("can't create vector with negative dimension"); |
|
45 len = 0; |
|
46 data = (double *) NULL; |
|
47 return; |
|
48 } |
3
|
49 |
|
50 len = n; |
|
51 if (len > 0) |
|
52 data = new double [len]; |
|
53 else |
|
54 data = (double *) NULL; |
|
55 } |
|
56 |
|
57 RowVector::RowVector (int n, double val) |
|
58 { |
|
59 if (n < 0) |
227
|
60 { |
|
61 (*current_liboctave_error_handler) |
|
62 ("can't create vector with negative dimension"); |
|
63 len = 0; |
|
64 data = (double *) NULL; |
|
65 return; |
|
66 } |
3
|
67 |
|
68 len = n; |
|
69 if (len > 0) |
|
70 { |
|
71 data = new double [len]; |
|
72 copy (data, len, val); |
|
73 } |
|
74 else |
|
75 data = (double *) NULL; |
|
76 } |
|
77 |
|
78 RowVector::RowVector (const RowVector& a) |
|
79 { |
|
80 len = a.len; |
|
81 if (len > 0) |
|
82 { |
|
83 data = new double [len]; |
|
84 copy (data, a.data, len); |
|
85 } |
|
86 else |
|
87 data = (double *) NULL; |
|
88 } |
|
89 |
|
90 RowVector::RowVector (double a) |
|
91 { |
|
92 len = 1; |
|
93 data = new double [1]; |
|
94 data[0] = a; |
|
95 } |
|
96 |
|
97 RowVector& |
|
98 RowVector::operator = (const RowVector& a) |
|
99 { |
|
100 if (this != &a) |
|
101 { |
|
102 delete [] data; |
|
103 len = a.len; |
|
104 if (len > 0) |
|
105 { |
|
106 data = new double [len]; |
|
107 copy (data, a.data, len); |
|
108 } |
|
109 else |
|
110 data = (double *) NULL; |
|
111 } |
|
112 return *this; |
|
113 } |
|
114 |
227
|
115 double& |
|
116 RowVector::checkelem (int n) |
|
117 { |
|
118 #ifndef NO_RANGE_CHECK |
|
119 if (n < 0 || n >= len) |
|
120 { |
|
121 (*current_liboctave_error_handler) ("range error"); |
|
122 static double foo = 0.0; |
|
123 return foo; |
|
124 } |
|
125 #endif |
|
126 |
|
127 return elem (n); |
|
128 } |
|
129 |
|
130 double |
|
131 RowVector::checkelem (int n) const |
|
132 { |
|
133 #ifndef NO_RANGE_CHECK |
|
134 if (n < 0 || n >= len) |
|
135 { |
|
136 (*current_liboctave_error_handler) ("range error"); |
|
137 return 0.0; |
|
138 } |
|
139 #endif |
|
140 |
|
141 return elem (n); |
|
142 } |
|
143 |
3
|
144 RowVector& |
|
145 RowVector::resize (int n) |
|
146 { |
|
147 if (n < 0) |
227
|
148 { |
|
149 (*current_liboctave_error_handler) |
|
150 ("can't resize to negative dimension"); |
|
151 return *this; |
|
152 } |
3
|
153 |
|
154 double *new_data = (double *) NULL; |
|
155 if (n > 0) |
|
156 { |
|
157 new_data = new double [n]; |
|
158 int min_len = len < n ? len : n; |
|
159 |
|
160 for (int i = 0; i < min_len; i++) |
|
161 new_data[i] = data[i]; |
|
162 } |
|
163 |
|
164 delete [] data; |
|
165 len = n; |
|
166 data = new_data; |
|
167 |
|
168 return *this; |
|
169 } |
|
170 |
|
171 RowVector& |
|
172 RowVector::resize (int n, double val) |
|
173 { |
|
174 int old_len = len; |
|
175 resize (n); |
|
176 for (int i = old_len; i < len; i++) |
|
177 data[i] = val; |
|
178 |
|
179 return *this; |
|
180 } |
|
181 |
|
182 int |
|
183 RowVector::operator == (const RowVector& a) const |
|
184 { |
|
185 if (len != a.len) |
|
186 return 0; |
|
187 return equal (data, a.data, len); |
|
188 } |
|
189 |
|
190 int |
|
191 RowVector::operator != (const RowVector& a) const |
|
192 { |
|
193 if (len != a.len) |
|
194 return 1; |
|
195 return !equal (data, a.data, len); |
|
196 } |
|
197 |
|
198 RowVector& |
|
199 RowVector::insert (const RowVector& a, int c) |
|
200 { |
|
201 if (c < 0 || c + a.len - 1 > len) |
227
|
202 { |
|
203 (*current_liboctave_error_handler) ("range error for insert"); |
|
204 return *this; |
|
205 } |
3
|
206 |
|
207 for (int i = 0; i < a.len; i++) |
|
208 data[c+i] = a.data[i]; |
|
209 |
|
210 return *this; |
|
211 } |
|
212 |
|
213 RowVector& |
|
214 RowVector::fill (double val) |
|
215 { |
|
216 if (len > 0) |
|
217 copy (data, len, val); |
|
218 return *this; |
|
219 } |
|
220 |
|
221 RowVector& |
|
222 RowVector::fill (double val, int c1, int c2) |
|
223 { |
|
224 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
227
|
225 { |
|
226 (*current_liboctave_error_handler) ("range error for fill"); |
|
227 return *this; |
|
228 } |
3
|
229 |
|
230 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
231 |
|
232 for (int i = c1; i <= c2; i++) |
|
233 data[i] = val; |
|
234 |
|
235 return *this; |
|
236 } |
|
237 |
|
238 RowVector |
|
239 RowVector::append (const RowVector& a) const |
|
240 { |
|
241 int nc_insert = len; |
|
242 RowVector retval (len + a.len); |
|
243 retval.insert (*this, 0); |
|
244 retval.insert (a, nc_insert); |
|
245 return retval; |
|
246 } |
|
247 |
|
248 ColumnVector |
|
249 RowVector::transpose (void) const |
|
250 { |
|
251 return ColumnVector (dup (data, len), len); |
|
252 } |
|
253 |
|
254 RowVector |
|
255 RowVector::extract (int c1, int c2) const |
|
256 { |
|
257 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
258 |
|
259 int new_c = c2 - c1 + 1; |
|
260 |
|
261 RowVector result (new_c); |
|
262 |
|
263 for (int i = 0; i < new_c; i++) |
|
264 result.data[i] = elem (c1+i); |
|
265 |
|
266 return result; |
|
267 } |
|
268 |
|
269 // row vector by scalar -> row vector operations |
|
270 |
|
271 RowVector |
|
272 RowVector::operator + (double s) const |
|
273 { |
|
274 return RowVector (add (data, len, s), len); |
|
275 } |
|
276 |
|
277 RowVector |
|
278 RowVector::operator - (double s) const |
|
279 { |
|
280 return RowVector (subtract (data, len, s), len); |
|
281 } |
|
282 |
|
283 RowVector |
|
284 RowVector::operator * (double s) const |
|
285 { |
|
286 return RowVector (multiply (data, len, s), len); |
|
287 } |
|
288 |
|
289 RowVector |
|
290 RowVector::operator / (double s) const |
|
291 { |
|
292 return RowVector (divide (data, len, s), len); |
|
293 } |
|
294 |
|
295 ComplexRowVector |
161
|
296 RowVector::operator + (const Complex& s) const |
3
|
297 { |
|
298 return ComplexRowVector (add (data, len, s), len); |
|
299 } |
|
300 |
|
301 ComplexRowVector |
161
|
302 RowVector::operator - (const Complex& s) const |
3
|
303 { |
|
304 return ComplexRowVector (subtract (data, len, s), len); |
|
305 } |
|
306 |
|
307 ComplexRowVector |
161
|
308 RowVector::operator * (const Complex& s) const |
3
|
309 { |
|
310 return ComplexRowVector (multiply (data, len, s), len); |
|
311 } |
|
312 |
|
313 ComplexRowVector |
161
|
314 RowVector::operator / (const Complex& s) const |
3
|
315 { |
|
316 return ComplexRowVector (divide (data, len, s), len); |
|
317 } |
|
318 |
|
319 // scalar by row vector -> row vector operations |
|
320 |
|
321 RowVector |
|
322 operator + (double s, const RowVector& a) |
|
323 { |
|
324 return RowVector (add (a.data, a.len, s), a.len); |
|
325 } |
|
326 |
|
327 RowVector |
|
328 operator - (double s, const RowVector& a) |
|
329 { |
|
330 return RowVector (subtract (s, a.data, a.len), a.len); |
|
331 } |
|
332 |
|
333 RowVector |
|
334 operator * (double s, const RowVector& a) |
|
335 { |
|
336 return RowVector (multiply (a.data, a.len, s), a.len); |
|
337 } |
|
338 |
|
339 RowVector |
|
340 operator / (double s, const RowVector& a) |
|
341 { |
|
342 return RowVector (divide (s, a.data, a.len), a.len); |
|
343 } |
|
344 |
|
345 // row vector by column vector -> scalar |
|
346 |
|
347 double |
|
348 RowVector::operator * (const ColumnVector& a) const |
|
349 { |
|
350 if (len != a.len) |
227
|
351 { |
|
352 (*current_liboctave_error_handler) |
|
353 ("nonconformant vector multiplication attempted"); |
|
354 return 0.0; |
|
355 } |
3
|
356 |
|
357 int i_one = 1; |
|
358 return F77_FCN (ddot) (&len, data, &i_one, a.data, &i_one); |
|
359 } |
|
360 |
|
361 Complex |
|
362 RowVector::operator * (const ComplexColumnVector& a) const |
|
363 { |
|
364 ComplexRowVector tmp (*this); |
|
365 return tmp * a; |
|
366 } |
|
367 |
|
368 // row vector by matrix -> row vector |
|
369 |
|
370 RowVector |
|
371 RowVector::operator * (const Matrix& a) const |
|
372 { |
|
373 if (a.nr != len) |
227
|
374 { |
|
375 (*current_liboctave_error_handler) |
|
376 ("nonconformant vector multiplication attempted"); |
|
377 return RowVector (); |
|
378 } |
3
|
379 |
|
380 if (len == 0 || a.nc == 0) |
|
381 return RowVector (0); |
|
382 |
|
383 // Transpose A to form A'*x == (x'*A)' |
|
384 |
|
385 int anr = a.nr; |
|
386 int anc = a.nc; |
|
387 |
|
388 char trans = 'T'; |
|
389 int ld = anr; |
|
390 double alpha = 1.0; |
|
391 double beta = 0.0; |
|
392 int i_one = 1; |
|
393 |
|
394 double *y = new double [len]; |
|
395 |
|
396 F77_FCN (dgemv) (&trans, &anc, &anr, &alpha, a.data, &ld, data, |
|
397 &i_one, &beta, y, &i_one, 1L); |
|
398 |
|
399 return RowVector (y, len); |
|
400 } |
|
401 |
|
402 ComplexRowVector |
|
403 RowVector::operator * (const ComplexMatrix& a) const |
|
404 { |
|
405 ComplexRowVector tmp (*this); |
|
406 return tmp * a; |
|
407 } |
|
408 |
|
409 // row vector by row vector -> row vector operations |
|
410 |
|
411 RowVector |
|
412 RowVector::operator + (const RowVector& a) const |
|
413 { |
|
414 if (len != a.len) |
227
|
415 { |
|
416 (*current_liboctave_error_handler) |
|
417 ("nonconformant vector addition attempted"); |
|
418 return RowVector (); |
|
419 } |
3
|
420 |
|
421 if (len == 0) |
|
422 return RowVector (0); |
|
423 |
|
424 return RowVector (add (data, a.data, len), len); |
|
425 } |
|
426 |
|
427 RowVector |
|
428 RowVector::operator - (const RowVector& a) const |
|
429 { |
|
430 if (len != a.len) |
227
|
431 { |
|
432 (*current_liboctave_error_handler) |
|
433 ("nonconformant vector subtraction attempted"); |
|
434 return RowVector (); |
|
435 } |
3
|
436 |
|
437 if (len == 0) |
|
438 return RowVector (0); |
|
439 |
|
440 return RowVector (subtract (data, a.data, len), len); |
|
441 } |
|
442 |
|
443 ComplexRowVector |
|
444 RowVector::operator + (const ComplexRowVector& a) const |
|
445 { |
|
446 if (len != a.len) |
227
|
447 { |
|
448 (*current_liboctave_error_handler) |
|
449 ("nonconformant vector addition attempted"); |
|
450 return ComplexRowVector (); |
|
451 } |
3
|
452 |
|
453 if (len == 0) |
|
454 return ComplexRowVector (0); |
|
455 |
|
456 return ComplexRowVector (add (data, a.data, len), len); |
|
457 } |
|
458 |
|
459 ComplexRowVector |
|
460 RowVector::operator - (const ComplexRowVector& a) const |
|
461 { |
|
462 if (len != a.len) |
227
|
463 { |
|
464 (*current_liboctave_error_handler) |
|
465 ("nonconformant vector subtraction attempted"); |
|
466 return ComplexRowVector (); |
|
467 } |
3
|
468 |
|
469 if (len == 0) |
|
470 return ComplexRowVector (0); |
|
471 |
|
472 return ComplexRowVector (subtract (data, a.data, len), len); |
|
473 } |
|
474 |
|
475 RowVector |
|
476 RowVector::product (const RowVector& a) const |
|
477 { |
|
478 if (len != a.len) |
227
|
479 { |
|
480 (*current_liboctave_error_handler) |
|
481 ("nonconformant vector product attempted"); |
|
482 return RowVector (); |
|
483 } |
3
|
484 |
|
485 if (len == 0) |
|
486 return RowVector (0); |
|
487 |
|
488 return RowVector (multiply (data, a.data, len), len); |
|
489 } |
|
490 |
|
491 RowVector |
|
492 RowVector::quotient (const RowVector& a) const |
|
493 { |
|
494 if (len != a.len) |
227
|
495 { |
|
496 (*current_liboctave_error_handler) |
|
497 ("nonconformant vector quotient attempted"); |
|
498 return RowVector (); |
|
499 } |
3
|
500 |
|
501 if (len == 0) |
|
502 return RowVector (0); |
|
503 |
|
504 return RowVector (divide (data, a.data, len), len); |
|
505 } |
|
506 |
|
507 ComplexRowVector |
|
508 RowVector::product (const ComplexRowVector& a) const |
|
509 { |
|
510 if (len != a.len) |
227
|
511 { |
|
512 (*current_liboctave_error_handler) |
|
513 ("nonconformant vector product attempted"); |
|
514 return ComplexRowVector (); |
|
515 } |
3
|
516 |
|
517 if (len == 0) |
|
518 return ComplexRowVector (0); |
|
519 |
|
520 return ComplexRowVector (multiply (data, a.data, len), len); |
|
521 } |
|
522 |
|
523 ComplexRowVector |
|
524 RowVector::quotient (const ComplexRowVector& a) const |
|
525 { |
|
526 if (len != a.len) |
227
|
527 { |
|
528 (*current_liboctave_error_handler) |
|
529 ("nonconformant vector quotient attempted"); |
|
530 return ComplexRowVector (); |
|
531 } |
3
|
532 |
|
533 if (len == 0) |
|
534 return ComplexRowVector (0); |
|
535 |
|
536 return ComplexRowVector (divide (data, a.data, len), len); |
|
537 } |
|
538 |
|
539 RowVector& |
|
540 RowVector::operator += (const RowVector& a) |
|
541 { |
|
542 if (len != a.len) |
227
|
543 { |
|
544 (*current_liboctave_error_handler) |
|
545 ("nonconformant vector += operation attempted"); |
|
546 return *this; |
|
547 } |
3
|
548 |
|
549 if (len == 0) |
|
550 return *this; |
|
551 |
|
552 add2 (data, a.data, len); |
|
553 return *this; |
|
554 } |
|
555 |
|
556 RowVector& |
|
557 RowVector::operator -= (const RowVector& a) |
|
558 { |
|
559 if (len != a.len) |
227
|
560 { |
|
561 (*current_liboctave_error_handler) |
|
562 ("nonconformant vector -= operation attempted"); |
|
563 return *this; |
|
564 } |
3
|
565 |
|
566 if (len == 0) |
|
567 return *this; |
|
568 |
|
569 subtract2 (data, a.data, len); |
|
570 return *this; |
|
571 } |
|
572 |
|
573 // unary operations |
|
574 |
|
575 RowVector |
|
576 RowVector::operator - (void) const |
|
577 { |
|
578 if (len == 0) |
|
579 return RowVector (0); |
|
580 |
|
581 return RowVector (negate (data, len), len); |
|
582 } |
|
583 |
|
584 RowVector |
|
585 map (d_d_Mapper f, const RowVector& a) |
|
586 { |
|
587 RowVector b (a); |
|
588 b.map (f); |
|
589 return b; |
|
590 } |
|
591 |
|
592 void |
|
593 RowVector::map (d_d_Mapper f) |
|
594 { |
|
595 for (int i = 0; i < len; i++) |
|
596 data[i] = f (data[i]); |
|
597 } |
|
598 |
|
599 double |
|
600 RowVector::min (void) const |
|
601 { |
|
602 if (len == 0) |
|
603 return 0; |
|
604 |
|
605 double res = data[0]; |
|
606 |
|
607 for (int i = 1; i < len; i++) |
|
608 if (data[i] < res) |
|
609 res = data[i]; |
|
610 |
|
611 return res; |
|
612 } |
|
613 |
|
614 double |
|
615 RowVector::max (void) const |
|
616 { |
|
617 if (len == 0) |
|
618 return 0; |
|
619 |
|
620 double res = data[0]; |
|
621 |
|
622 for (int i = 1; i < len; i++) |
|
623 if (data[i] > res) |
|
624 res = data[i]; |
|
625 |
|
626 return res; |
|
627 } |
|
628 |
|
629 ostream& |
|
630 operator << (ostream& os, const RowVector& a) |
|
631 { |
|
632 // int field_width = os.precision () + 7; |
|
633 for (int i = 0; i < a.len; i++) |
|
634 os << " " /* setw (field_width) */ << a.data[i]; |
|
635 return os; |
|
636 } |
|
637 |
|
638 /* |
|
639 * Complex Row Vector class |
|
640 */ |
|
641 |
|
642 ComplexRowVector::ComplexRowVector (int n) |
|
643 { |
|
644 if (n < 0) |
227
|
645 { |
|
646 (*current_liboctave_error_handler) |
|
647 ("can't create vector with negative dimension"); |
|
648 len = 0; |
|
649 data = (Complex *) NULL; |
|
650 return; |
|
651 } |
3
|
652 |
|
653 len = n; |
|
654 if (len > 0) |
|
655 data = new Complex [len]; |
|
656 else |
|
657 data = (Complex *) NULL; |
|
658 } |
|
659 |
|
660 ComplexRowVector::ComplexRowVector (int n, double val) |
|
661 { |
|
662 if (n < 0) |
227
|
663 { |
|
664 (*current_liboctave_error_handler) |
|
665 ("can't create vector with negative dimension"); |
|
666 len = 0; |
|
667 data = (Complex *) NULL; |
|
668 return; |
|
669 } |
3
|
670 |
|
671 len = n; |
|
672 if (len > 0) |
|
673 { |
|
674 data = new Complex [len]; |
|
675 copy (data, len, val); |
|
676 } |
|
677 else |
|
678 data = (Complex *) NULL; |
|
679 } |
|
680 |
161
|
681 ComplexRowVector::ComplexRowVector (int n, const Complex& val) |
3
|
682 { |
|
683 if (n < 0) |
227
|
684 { |
|
685 (*current_liboctave_error_handler) |
|
686 ("can't create vector with negative dimension"); |
|
687 len = 0; |
|
688 data = (Complex *) NULL; |
|
689 return; |
|
690 } |
3
|
691 |
|
692 len = n; |
|
693 if (len > 0) |
|
694 { |
|
695 data = new Complex [len]; |
|
696 copy (data, len, val); |
|
697 } |
|
698 else |
|
699 data = (Complex *) NULL; |
|
700 } |
|
701 |
|
702 ComplexRowVector::ComplexRowVector (const RowVector& a) |
|
703 { |
|
704 len = a.len; |
|
705 if (len > 0) |
|
706 { |
|
707 data = new Complex [len]; |
|
708 copy (data, a.data, len); |
|
709 } |
|
710 else |
|
711 data = (Complex *) NULL; |
|
712 } |
|
713 |
|
714 ComplexRowVector::ComplexRowVector (const ComplexRowVector& a) |
|
715 { |
|
716 len = a.len; |
|
717 if (len > 0) |
|
718 { |
|
719 data = new Complex [len]; |
|
720 copy (data, a.data, len); |
|
721 } |
|
722 else |
|
723 data = (Complex *) NULL; |
|
724 } |
|
725 |
|
726 ComplexRowVector::ComplexRowVector (double a) |
|
727 { |
|
728 len = 1; |
|
729 data = new Complex [1]; |
|
730 data[0] = a; |
|
731 } |
|
732 |
161
|
733 ComplexRowVector::ComplexRowVector (const Complex& a) |
3
|
734 { |
|
735 len = 1; |
|
736 data = new Complex [1]; |
|
737 data[0] = Complex (a); |
|
738 } |
|
739 |
|
740 ComplexRowVector& |
|
741 ComplexRowVector::operator = (const RowVector& a) |
|
742 { |
|
743 delete [] data; |
|
744 len = a.len; |
|
745 if (len > 0) |
|
746 { |
|
747 data = new Complex [len]; |
|
748 copy (data, a.data, len); |
|
749 } |
|
750 else |
|
751 data = (Complex *) NULL; |
|
752 |
|
753 return *this; |
|
754 } |
|
755 |
|
756 ComplexRowVector& |
|
757 ComplexRowVector::operator = (const ComplexRowVector& a) |
|
758 { |
|
759 if (this != &a) |
|
760 { |
|
761 delete [] data; |
|
762 len = a.len; |
|
763 if (len > 0) |
|
764 { |
|
765 data = new Complex [len]; |
|
766 copy (data, a.data, len); |
|
767 } |
|
768 else |
|
769 data = (Complex *) NULL; |
|
770 } |
|
771 return *this; |
|
772 } |
|
773 |
227
|
774 Complex& |
|
775 ComplexRowVector::checkelem (int n) |
|
776 { |
|
777 #ifndef NO_RANGE_CHECK |
|
778 if (n < 0 || n >= len) |
|
779 { |
|
780 (*current_liboctave_error_handler) ("range error"); |
|
781 static Complex foo (0.0); |
|
782 return foo; |
|
783 } |
|
784 #endif |
|
785 |
|
786 return elem (n); |
|
787 } |
|
788 |
|
789 Complex |
|
790 ComplexRowVector::checkelem (int n) const |
|
791 { |
|
792 #ifndef NO_RANGE_CHECK |
|
793 if (n < 0 || n >= len) |
|
794 { |
|
795 (*current_liboctave_error_handler) ("range error"); |
|
796 return Complex (0.0); |
|
797 } |
|
798 #endif |
|
799 |
|
800 return elem (n); |
|
801 } |
|
802 |
3
|
803 ComplexRowVector& |
|
804 ComplexRowVector::resize (int n) |
|
805 { |
|
806 if (n < 0) |
227
|
807 { |
|
808 (*current_liboctave_error_handler) |
|
809 ("can't resize to negative dimension"); |
|
810 return *this; |
|
811 } |
3
|
812 |
|
813 Complex *new_data = (Complex *) NULL; |
|
814 if (n > 0) |
|
815 { |
|
816 new_data = new Complex [n]; |
|
817 int min_len = len < n ? len : n; |
|
818 |
|
819 for (int i = 0; i < min_len; i++) |
|
820 new_data[i] = data[i]; |
|
821 } |
|
822 |
|
823 delete [] data; |
|
824 len = n; |
|
825 data = new_data; |
|
826 |
|
827 return *this; |
|
828 } |
|
829 |
|
830 ComplexRowVector& |
|
831 ComplexRowVector::resize (int n, double val) |
|
832 { |
|
833 int old_len = len; |
|
834 resize (n); |
|
835 for (int i = old_len; i < len; i++) |
|
836 data[i] = val; |
|
837 |
|
838 return *this; |
|
839 } |
|
840 |
|
841 ComplexRowVector& |
161
|
842 ComplexRowVector::resize (int n, const Complex& val) |
3
|
843 { |
|
844 int old_len = len; |
|
845 resize (n); |
|
846 for (int i = old_len; i < len; i++) |
|
847 data[i] = val; |
|
848 |
|
849 return *this; |
|
850 } |
|
851 |
|
852 int |
|
853 ComplexRowVector::operator == (const ComplexRowVector& a) const |
|
854 { |
|
855 if (len != a.len) |
|
856 return 0; |
|
857 return equal (data, a.data, len); |
|
858 } |
|
859 |
|
860 int |
|
861 ComplexRowVector::operator != (const ComplexRowVector& a) const |
|
862 { |
|
863 if (len != a.len) |
|
864 return 1; |
|
865 return !equal (data, a.data, len); |
|
866 } |
|
867 |
|
868 // destructive insert/delete/reorder operations |
|
869 |
|
870 ComplexRowVector& |
|
871 ComplexRowVector::insert (const RowVector& a, int c) |
|
872 { |
|
873 if (c < 0 || c + a.len - 1 > len) |
227
|
874 { |
|
875 (*current_liboctave_error_handler) ("range error for insert"); |
|
876 return *this; |
|
877 } |
3
|
878 |
|
879 for (int i = 0; i < a.len; i++) |
|
880 data[c+i] = a.data[i]; |
|
881 |
|
882 return *this; |
|
883 } |
|
884 |
|
885 ComplexRowVector& |
|
886 ComplexRowVector::insert (const ComplexRowVector& a, int c) |
|
887 { |
|
888 if (c < 0 || c + a.len - 1 > len) |
227
|
889 { |
|
890 (*current_liboctave_error_handler) ("range error for insert"); |
|
891 return *this; |
|
892 } |
3
|
893 |
|
894 for (int i = 0; i < a.len; i++) |
|
895 data[c+i] = a.data[i]; |
|
896 |
|
897 return *this; |
|
898 } |
|
899 |
|
900 ComplexRowVector& |
|
901 ComplexRowVector::fill (double val) |
|
902 { |
|
903 if (len > 0) |
|
904 copy (data, len, val); |
|
905 return *this; |
|
906 } |
|
907 |
|
908 ComplexRowVector& |
161
|
909 ComplexRowVector::fill (const Complex& val) |
3
|
910 { |
|
911 if (len > 0) |
|
912 copy (data, len, val); |
|
913 return *this; |
|
914 } |
|
915 |
|
916 ComplexRowVector& |
|
917 ComplexRowVector::fill (double val, int c1, int c2) |
|
918 { |
|
919 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
227
|
920 { |
|
921 (*current_liboctave_error_handler) ("range error for fill"); |
|
922 return *this; |
|
923 } |
3
|
924 |
|
925 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
926 |
|
927 for (int i = c1; i <= c2; i++) |
|
928 data[i] = val; |
|
929 |
|
930 return *this; |
|
931 } |
|
932 |
|
933 ComplexRowVector& |
161
|
934 ComplexRowVector::fill (const Complex& val, int c1, int c2) |
3
|
935 { |
|
936 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
227
|
937 { |
|
938 (*current_liboctave_error_handler) ("range error for fill"); |
|
939 return *this; |
|
940 } |
3
|
941 |
|
942 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
943 |
|
944 for (int i = c1; i <= c2; i++) |
|
945 data[i] = val; |
|
946 |
|
947 return *this; |
|
948 } |
|
949 |
|
950 ComplexRowVector |
|
951 ComplexRowVector::append (const RowVector& a) const |
|
952 { |
|
953 int nc_insert = len; |
|
954 ComplexRowVector retval (len + a.len); |
|
955 retval.insert (*this, 0); |
|
956 retval.insert (a, nc_insert); |
|
957 return retval; |
|
958 } |
|
959 |
|
960 ComplexRowVector |
|
961 ComplexRowVector::append (const ComplexRowVector& a) const |
|
962 { |
|
963 int nc_insert = len; |
|
964 ComplexRowVector retval (len + a.len); |
|
965 retval.insert (*this, 0); |
|
966 retval.insert (a, nc_insert); |
|
967 return retval; |
|
968 } |
|
969 |
|
970 ComplexColumnVector |
|
971 ComplexRowVector::hermitian (void) const |
|
972 { |
|
973 return ComplexColumnVector (conj_dup (data, len), len); |
|
974 } |
|
975 |
|
976 ComplexColumnVector |
|
977 ComplexRowVector::transpose (void) const |
|
978 { |
|
979 return ComplexColumnVector (dup (data, len), len); |
|
980 } |
|
981 |
|
982 RowVector |
|
983 real (const ComplexRowVector& a) |
|
984 { |
|
985 RowVector retval; |
|
986 if (a.len > 0) |
|
987 retval = RowVector (real_dup (a.data, a.len), a.len); |
|
988 return retval; |
|
989 } |
|
990 |
|
991 RowVector |
|
992 imag (const ComplexRowVector& a) |
|
993 { |
|
994 RowVector retval; |
|
995 if (a.len > 0) |
|
996 retval = RowVector (imag_dup (a.data, a.len), a.len); |
|
997 return retval; |
|
998 } |
|
999 |
|
1000 ComplexRowVector |
|
1001 conj (const ComplexRowVector& a) |
|
1002 { |
|
1003 ComplexRowVector retval; |
|
1004 if (a.len > 0) |
|
1005 retval = ComplexRowVector (conj_dup (a.data, a.len), a.len); |
|
1006 return retval; |
|
1007 } |
|
1008 |
|
1009 // resize is the destructive equivalent for this one |
|
1010 |
|
1011 ComplexRowVector |
|
1012 ComplexRowVector::extract (int c1, int c2) const |
|
1013 { |
|
1014 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
1015 |
|
1016 int new_c = c2 - c1 + 1; |
|
1017 |
|
1018 ComplexRowVector result (new_c); |
|
1019 |
|
1020 for (int i = 0; i < new_c; i++) |
|
1021 result.data[i] = elem (c1+i); |
|
1022 |
|
1023 return result; |
|
1024 } |
|
1025 |
|
1026 // row vector by scalar -> row vector operations |
|
1027 |
|
1028 ComplexRowVector |
|
1029 ComplexRowVector::operator + (double s) const |
|
1030 { |
|
1031 return ComplexRowVector (add (data, len, s), len); |
|
1032 } |
|
1033 |
|
1034 ComplexRowVector |
|
1035 ComplexRowVector::operator - (double s) const |
|
1036 { |
|
1037 return ComplexRowVector (subtract (data, len, s), len); |
|
1038 } |
|
1039 |
|
1040 ComplexRowVector |
|
1041 ComplexRowVector::operator * (double s) const |
|
1042 { |
|
1043 return ComplexRowVector (multiply (data, len, s), len); |
|
1044 } |
|
1045 |
|
1046 ComplexRowVector |
|
1047 ComplexRowVector::operator / (double s) const |
|
1048 { |
|
1049 return ComplexRowVector (divide (data, len, s), len); |
|
1050 } |
|
1051 |
|
1052 ComplexRowVector |
161
|
1053 ComplexRowVector::operator + (const Complex& s) const |
3
|
1054 { |
|
1055 return ComplexRowVector (add (data, len, s), len); |
|
1056 } |
|
1057 |
|
1058 ComplexRowVector |
161
|
1059 ComplexRowVector::operator - (const Complex& s) const |
3
|
1060 { |
|
1061 return ComplexRowVector (subtract (data, len, s), len); |
|
1062 } |
|
1063 |
|
1064 ComplexRowVector |
161
|
1065 ComplexRowVector::operator * (const Complex& s) const |
3
|
1066 { |
|
1067 return ComplexRowVector (multiply (data, len, s), len); |
|
1068 } |
|
1069 |
|
1070 ComplexRowVector |
161
|
1071 ComplexRowVector::operator / (const Complex& s) const |
3
|
1072 { |
|
1073 return ComplexRowVector (divide (data, len, s), len); |
|
1074 } |
|
1075 |
|
1076 // scalar by row vector -> row vector operations |
|
1077 |
|
1078 ComplexRowVector |
|
1079 operator + (double s, const ComplexRowVector& a) |
|
1080 { |
|
1081 return ComplexRowVector (add (a.data, a.len, s), a.len); |
|
1082 } |
|
1083 |
|
1084 ComplexRowVector |
|
1085 operator - (double s, const ComplexRowVector& a) |
|
1086 { |
|
1087 return ComplexRowVector (subtract (s, a.data, a.len), a.len); |
|
1088 } |
|
1089 |
|
1090 ComplexRowVector |
|
1091 operator * (double s, const ComplexRowVector& a) |
|
1092 { |
|
1093 return ComplexRowVector (multiply (a.data, a.len, s), a.len); |
|
1094 } |
|
1095 |
|
1096 ComplexRowVector |
|
1097 operator / (double s, const ComplexRowVector& a) |
|
1098 { |
|
1099 return ComplexRowVector (divide (s, a.data, a.len), a.len); |
|
1100 } |
|
1101 |
|
1102 ComplexRowVector |
161
|
1103 operator + (const Complex& s, const ComplexRowVector& a) |
3
|
1104 { |
|
1105 return ComplexRowVector (add (a.data, a.len, s), a.len); |
|
1106 } |
|
1107 |
|
1108 ComplexRowVector |
161
|
1109 operator - (const Complex& s, const ComplexRowVector& a) |
3
|
1110 { |
|
1111 return ComplexRowVector (subtract (s, a.data, a.len), a.len); |
|
1112 } |
|
1113 |
|
1114 ComplexRowVector |
161
|
1115 operator * (const Complex& s, const ComplexRowVector& a) |
3
|
1116 { |
|
1117 return ComplexRowVector (multiply (a.data, a.len, s), a.len); |
|
1118 } |
|
1119 |
|
1120 ComplexRowVector |
161
|
1121 operator / (const Complex& s, const ComplexRowVector& a) |
3
|
1122 { |
|
1123 return ComplexRowVector (divide (s, a.data, a.len), a.len); |
|
1124 } |
|
1125 |
|
1126 // row vector by column vector -> scalar |
|
1127 |
|
1128 Complex |
|
1129 ComplexRowVector::operator * (const ColumnVector& a) const |
|
1130 { |
|
1131 ComplexColumnVector tmp (a); |
|
1132 return *this * tmp; |
|
1133 } |
|
1134 |
|
1135 Complex |
|
1136 ComplexRowVector::operator * (const ComplexColumnVector& a) const |
|
1137 { |
|
1138 // XXX FIXME XXX -- need function body |
|
1139 assert (0); |
|
1140 return Complex (0.0, 0.0); |
|
1141 } |
|
1142 |
|
1143 // row vector by matrix -> row vector |
|
1144 |
|
1145 ComplexRowVector |
|
1146 ComplexRowVector::operator * (const Matrix& a) const |
|
1147 { |
|
1148 ComplexMatrix tmp (a); |
|
1149 return *this * tmp; |
|
1150 } |
|
1151 |
|
1152 ComplexRowVector |
|
1153 ComplexRowVector::operator * (const ComplexMatrix& a) const |
|
1154 { |
|
1155 if (a.nr != len) |
227
|
1156 { |
|
1157 (*current_liboctave_error_handler) |
|
1158 ("nonconformant vector multiplication attempted"); |
|
1159 return ComplexRowVector (); |
|
1160 } |
3
|
1161 |
|
1162 if (len == 0 || a.nc == 0) |
|
1163 return ComplexRowVector (0); |
|
1164 |
|
1165 // Transpose A to form A'*x == (x'*A)' |
|
1166 |
|
1167 int anr = a.nr; |
|
1168 int anc = a.nc; |
|
1169 |
|
1170 char trans = 'T'; |
|
1171 int ld = anr; |
|
1172 Complex alpha (1.0); |
|
1173 Complex beta (0.0); |
|
1174 int i_one = 1; |
|
1175 |
|
1176 Complex *y = new Complex [len]; |
|
1177 |
|
1178 F77_FCN (zgemv) (&trans, &anc, &anr, &alpha, a.data, &ld, data, |
|
1179 &i_one, &beta, y, &i_one, 1L); |
|
1180 |
|
1181 return ComplexRowVector (y, len); |
|
1182 } |
|
1183 |
|
1184 // row vector by row vector -> row vector operations |
|
1185 |
|
1186 ComplexRowVector |
|
1187 ComplexRowVector::operator + (const RowVector& a) const |
|
1188 { |
|
1189 if (len != a.len) |
227
|
1190 { |
|
1191 (*current_liboctave_error_handler) |
|
1192 ("nonconformant vector addition attempted"); |
|
1193 return ComplexRowVector (); |
|
1194 } |
3
|
1195 |
|
1196 if (len == 0) |
|
1197 return ComplexRowVector (0); |
|
1198 |
|
1199 return ComplexRowVector (add (data, a.data, len), len); |
|
1200 } |
|
1201 |
|
1202 ComplexRowVector |
|
1203 ComplexRowVector::operator - (const RowVector& a) const |
|
1204 { |
|
1205 if (len != a.len) |
227
|
1206 { |
|
1207 (*current_liboctave_error_handler) |
|
1208 ("nonconformant vector subtraction attempted"); |
|
1209 return ComplexRowVector (); |
|
1210 } |
3
|
1211 |
|
1212 if (len == 0) |
|
1213 return ComplexRowVector (0); |
|
1214 |
|
1215 return ComplexRowVector (subtract (data, a.data, len), len); |
|
1216 } |
|
1217 |
|
1218 ComplexRowVector |
|
1219 ComplexRowVector::operator + (const ComplexRowVector& a) const |
|
1220 { |
|
1221 if (len != a.len) |
227
|
1222 { |
|
1223 (*current_liboctave_error_handler) |
|
1224 ("nonconformant vector addition attempted"); |
|
1225 return ComplexRowVector (); |
|
1226 } |
3
|
1227 |
|
1228 if (len == 0) |
|
1229 return ComplexRowVector (0); |
|
1230 |
|
1231 return ComplexRowVector (add (data, a.data, len), len); |
|
1232 } |
|
1233 |
|
1234 ComplexRowVector |
|
1235 ComplexRowVector::operator - (const ComplexRowVector& a) const |
|
1236 { |
|
1237 if (len != a.len) |
227
|
1238 { |
|
1239 (*current_liboctave_error_handler) |
|
1240 ("nonconformant vector subtraction attempted"); |
|
1241 return ComplexRowVector (); |
|
1242 } |
3
|
1243 |
|
1244 if (len == 0) |
|
1245 return ComplexRowVector (0); |
|
1246 |
|
1247 return ComplexRowVector (subtract (data, a.data, len), len); |
|
1248 } |
|
1249 |
|
1250 ComplexRowVector |
|
1251 ComplexRowVector::product (const RowVector& a) const |
|
1252 { |
|
1253 if (len != a.len) |
227
|
1254 { |
|
1255 (*current_liboctave_error_handler) |
|
1256 ("nonconformant vector product attempted"); |
|
1257 return ComplexRowVector (); |
|
1258 } |
3
|
1259 |
|
1260 if (len == 0) |
|
1261 return ComplexRowVector (0); |
|
1262 |
|
1263 return ComplexRowVector (multiply (data, a.data, len), len); |
|
1264 } |
|
1265 |
|
1266 ComplexRowVector |
|
1267 ComplexRowVector::quotient (const RowVector& a) const |
|
1268 { |
|
1269 if (len != a.len) |
227
|
1270 { |
|
1271 (*current_liboctave_error_handler) |
|
1272 ("nonconformant vector quotient attempted"); |
|
1273 return ComplexRowVector (); |
|
1274 } |
3
|
1275 |
|
1276 if (len == 0) |
|
1277 return ComplexRowVector (0); |
|
1278 |
|
1279 return ComplexRowVector (divide (data, a.data, len), len); |
|
1280 } |
|
1281 |
|
1282 ComplexRowVector |
|
1283 ComplexRowVector::product (const ComplexRowVector& a) const |
|
1284 { |
|
1285 if (len != a.len) |
227
|
1286 { |
|
1287 (*current_liboctave_error_handler) |
|
1288 ("nonconformant vector product attempted"); |
|
1289 return ComplexRowVector (); |
|
1290 } |
3
|
1291 |
|
1292 if (len == 0) |
|
1293 return ComplexRowVector (0); |
|
1294 |
|
1295 return ComplexRowVector (multiply (data, a.data, len), len); |
|
1296 } |
|
1297 |
|
1298 ComplexRowVector |
|
1299 ComplexRowVector::quotient (const ComplexRowVector& a) const |
|
1300 { |
|
1301 if (len != a.len) |
227
|
1302 { |
|
1303 (*current_liboctave_error_handler) |
|
1304 ("nonconformant vector quotient attempted"); |
|
1305 return ComplexRowVector (); |
|
1306 } |
3
|
1307 |
|
1308 if (len == 0) |
|
1309 return ComplexRowVector (0); |
|
1310 |
|
1311 return ComplexRowVector (divide (data, a.data, len), len); |
|
1312 } |
|
1313 |
|
1314 ComplexRowVector& |
|
1315 ComplexRowVector::operator += (const RowVector& a) |
|
1316 { |
|
1317 if (len != a.len) |
227
|
1318 { |
|
1319 (*current_liboctave_error_handler) |
|
1320 ("nonconformant vector += operation attempted"); |
|
1321 return *this; |
|
1322 } |
3
|
1323 |
|
1324 if (len == 0) |
|
1325 return *this; |
|
1326 |
|
1327 add2 (data, a.data, len); |
|
1328 return *this; |
|
1329 } |
|
1330 |
|
1331 ComplexRowVector& |
|
1332 ComplexRowVector::operator -= (const RowVector& a) |
|
1333 { |
|
1334 if (len != a.len) |
227
|
1335 { |
|
1336 (*current_liboctave_error_handler) |
|
1337 ("nonconformant vector -= operation attempted"); |
|
1338 return *this; |
|
1339 } |
3
|
1340 |
|
1341 if (len == 0) |
|
1342 return *this; |
|
1343 |
|
1344 subtract2 (data, a.data, len); |
|
1345 return *this; |
|
1346 } |
|
1347 |
|
1348 ComplexRowVector& |
|
1349 ComplexRowVector::operator += (const ComplexRowVector& a) |
|
1350 { |
|
1351 if (len != a.len) |
227
|
1352 { |
|
1353 (*current_liboctave_error_handler) |
|
1354 ("nonconformant vector += operation attempted"); |
|
1355 return *this; |
|
1356 } |
3
|
1357 |
|
1358 if (len == 0) |
|
1359 return *this; |
|
1360 |
|
1361 add2 (data, a.data, len); |
|
1362 return *this; |
|
1363 } |
|
1364 |
|
1365 ComplexRowVector& |
|
1366 ComplexRowVector::operator -= (const ComplexRowVector& a) |
|
1367 { |
|
1368 if (len != a.len) |
227
|
1369 { |
|
1370 (*current_liboctave_error_handler) |
|
1371 ("nonconformant vector -= operation attempted"); |
|
1372 return *this; |
|
1373 } |
3
|
1374 |
|
1375 if (len == 0) |
|
1376 return *this; |
|
1377 |
|
1378 subtract2 (data, a.data, len); |
|
1379 return *this; |
|
1380 } |
|
1381 |
|
1382 // unary operations |
|
1383 |
|
1384 ComplexRowVector |
|
1385 ComplexRowVector::operator - (void) const |
|
1386 { |
|
1387 if (len == 0) |
|
1388 return ComplexRowVector (0); |
|
1389 |
|
1390 return ComplexRowVector (negate (data, len), len); |
|
1391 } |
|
1392 |
|
1393 ComplexRowVector |
|
1394 map (c_c_Mapper f, const ComplexRowVector& a) |
|
1395 { |
|
1396 ComplexRowVector b (a); |
|
1397 b.map (f); |
|
1398 return b; |
|
1399 } |
|
1400 |
|
1401 RowVector |
|
1402 map (d_c_Mapper f, const ComplexRowVector& a) |
|
1403 { |
|
1404 RowVector b (a.len); |
|
1405 for (int i = 0; i < a.len; i++) |
|
1406 b.elem (i) = f (a.elem (i)); |
|
1407 return b; |
|
1408 } |
|
1409 |
|
1410 void |
|
1411 ComplexRowVector::map (c_c_Mapper f) |
|
1412 { |
|
1413 for (int i = 0; i < len; i++) |
|
1414 data[i] = f (data[i]); |
|
1415 } |
|
1416 |
|
1417 Complex |
|
1418 ComplexRowVector::min (void) const |
|
1419 { |
|
1420 if (len == 0) |
|
1421 return Complex (0.0); |
|
1422 |
|
1423 Complex res = data[0]; |
|
1424 double absres = abs (res); |
|
1425 |
|
1426 for (int i = 1; i < len; i++) |
|
1427 if (abs (data[i]) < absres) |
|
1428 { |
|
1429 res = data[i]; |
|
1430 absres = abs (res); |
|
1431 } |
|
1432 |
|
1433 return res; |
|
1434 } |
|
1435 |
|
1436 Complex |
|
1437 ComplexRowVector::max (void) const |
|
1438 { |
|
1439 if (len == 0) |
|
1440 return Complex (0.0); |
|
1441 |
|
1442 Complex res = data[0]; |
|
1443 double absres = abs (res); |
|
1444 |
|
1445 for (int i = 1; i < len; i++) |
|
1446 if (abs (data[i]) > absres) |
|
1447 { |
|
1448 res = data[i]; |
|
1449 absres = abs (res); |
|
1450 } |
|
1451 |
|
1452 return res; |
|
1453 } |
|
1454 |
|
1455 // i/o |
|
1456 |
|
1457 ostream& |
|
1458 operator << (ostream& os, const ComplexRowVector& a) |
|
1459 { |
|
1460 // int field_width = os.precision () + 7; |
|
1461 for (int i = 0; i < a.len; i++) |
|
1462 os << " " /* setw (field_width) */ << a.data[i]; |
|
1463 return os; |
|
1464 } |
|
1465 |
|
1466 |
|
1467 /* |
|
1468 ;;; Local Variables: *** |
|
1469 ;;; mode: C++ *** |
|
1470 ;;; page-delimiter: "^/\\*" *** |
|
1471 ;;; End: *** |
|
1472 */ |