1993
|
1 // ColumnVector manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
33 |
4669
|
34 #include "Array-util.h" |
1847
|
35 #include "f77-fcn.h" |
1368
|
36 #include "lo-error.h" |
458
|
37 #include "mx-base.h" |
|
38 #include "mx-inlines.cc" |
1650
|
39 #include "oct-cmplx.h" |
458
|
40 |
|
41 // Fortran functions we call. |
|
42 |
|
43 extern "C" |
|
44 { |
4552
|
45 F77_RET_T |
|
46 F77_FUNC (zgemv, ZGEMV) (F77_CONST_CHAR_ARG_DECL, |
|
47 const int&, const int&, const Complex&, |
|
48 const Complex*, const int&, const Complex*, |
|
49 const int&, const Complex&, Complex*, const int& |
|
50 F77_CHAR_ARG_LEN_DECL); |
458
|
51 } |
|
52 |
1360
|
53 // Complex Column Vector class |
458
|
54 |
|
55 ComplexColumnVector::ComplexColumnVector (const ColumnVector& a) |
1214
|
56 : MArray<Complex> (a.length ()) |
458
|
57 { |
|
58 for (int i = 0; i < length (); i++) |
|
59 elem (i) = a.elem (i); |
|
60 } |
|
61 |
2386
|
62 bool |
458
|
63 ComplexColumnVector::operator == (const ComplexColumnVector& a) const |
|
64 { |
|
65 int len = length (); |
|
66 if (len != a.length ()) |
|
67 return 0; |
3769
|
68 return mx_inline_equal (data (), a.data (), len); |
458
|
69 } |
|
70 |
2386
|
71 bool |
458
|
72 ComplexColumnVector::operator != (const ComplexColumnVector& a) const |
|
73 { |
|
74 return !(*this == a); |
|
75 } |
|
76 |
|
77 // destructive insert/delete/reorder operations |
|
78 |
|
79 ComplexColumnVector& |
|
80 ComplexColumnVector::insert (const ColumnVector& a, int r) |
|
81 { |
|
82 int a_len = a.length (); |
4316
|
83 |
1699
|
84 if (r < 0 || r + a_len > length ()) |
458
|
85 { |
|
86 (*current_liboctave_error_handler) ("range error for insert"); |
|
87 return *this; |
|
88 } |
|
89 |
4316
|
90 if (a_len > 0) |
|
91 { |
|
92 make_unique (); |
|
93 |
|
94 for (int i = 0; i < a_len; i++) |
|
95 xelem (r+i) = a.elem (i); |
|
96 } |
458
|
97 |
|
98 return *this; |
|
99 } |
|
100 |
|
101 ComplexColumnVector& |
|
102 ComplexColumnVector::insert (const ComplexColumnVector& a, int r) |
|
103 { |
|
104 int a_len = a.length (); |
4316
|
105 |
1699
|
106 if (r < 0 || r + a_len > length ()) |
458
|
107 { |
|
108 (*current_liboctave_error_handler) ("range error for insert"); |
|
109 return *this; |
|
110 } |
|
111 |
4316
|
112 if (a_len > 0) |
|
113 { |
|
114 make_unique (); |
|
115 |
|
116 for (int i = 0; i < a_len; i++) |
|
117 xelem (r+i) = a.elem (i); |
|
118 } |
458
|
119 |
|
120 return *this; |
|
121 } |
|
122 |
|
123 ComplexColumnVector& |
|
124 ComplexColumnVector::fill (double val) |
|
125 { |
|
126 int len = length (); |
4316
|
127 |
458
|
128 if (len > 0) |
4316
|
129 { |
|
130 make_unique (); |
|
131 |
|
132 for (int i = 0; i < len; i++) |
|
133 xelem (i) = val; |
|
134 } |
|
135 |
458
|
136 return *this; |
|
137 } |
|
138 |
|
139 ComplexColumnVector& |
|
140 ComplexColumnVector::fill (const Complex& val) |
|
141 { |
|
142 int len = length (); |
4316
|
143 |
458
|
144 if (len > 0) |
4316
|
145 { |
|
146 make_unique (); |
|
147 |
|
148 for (int i = 0; i < len; i++) |
|
149 xelem (i) = val; |
|
150 } |
|
151 |
|
152 |
458
|
153 return *this; |
|
154 } |
|
155 |
|
156 ComplexColumnVector& |
|
157 ComplexColumnVector::fill (double val, int r1, int r2) |
|
158 { |
|
159 int len = length (); |
4316
|
160 |
458
|
161 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
162 { |
|
163 (*current_liboctave_error_handler) ("range error for fill"); |
|
164 return *this; |
|
165 } |
|
166 |
|
167 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
168 |
4316
|
169 if (r2 >= r1) |
|
170 { |
|
171 make_unique (); |
|
172 |
|
173 for (int i = r1; i <= r2; i++) |
|
174 xelem (i) = val; |
|
175 } |
458
|
176 |
|
177 return *this; |
|
178 } |
|
179 |
|
180 ComplexColumnVector& |
|
181 ComplexColumnVector::fill (const Complex& val, int r1, int r2) |
|
182 { |
|
183 int len = length (); |
4316
|
184 |
458
|
185 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
186 { |
|
187 (*current_liboctave_error_handler) ("range error for fill"); |
|
188 return *this; |
|
189 } |
|
190 |
|
191 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
192 |
4316
|
193 if (r2 >= r1) |
|
194 { |
|
195 make_unique (); |
|
196 |
|
197 for (int i = r1; i <= r2; i++) |
|
198 xelem (i) = val; |
|
199 } |
458
|
200 |
|
201 return *this; |
|
202 } |
|
203 |
|
204 ComplexColumnVector |
|
205 ComplexColumnVector::stack (const ColumnVector& a) const |
|
206 { |
|
207 int len = length (); |
|
208 int nr_insert = len; |
|
209 ComplexColumnVector retval (len + a.length ()); |
|
210 retval.insert (*this, 0); |
|
211 retval.insert (a, nr_insert); |
|
212 return retval; |
|
213 } |
|
214 |
|
215 ComplexColumnVector |
|
216 ComplexColumnVector::stack (const ComplexColumnVector& a) const |
|
217 { |
|
218 int len = length (); |
|
219 int nr_insert = len; |
|
220 ComplexColumnVector retval (len + a.length ()); |
|
221 retval.insert (*this, 0); |
|
222 retval.insert (a, nr_insert); |
|
223 return retval; |
|
224 } |
|
225 |
|
226 ComplexRowVector |
|
227 ComplexColumnVector::hermitian (void) const |
|
228 { |
|
229 int len = length (); |
3769
|
230 return ComplexRowVector (mx_inline_conj_dup (data (), len), len); |
458
|
231 } |
|
232 |
|
233 ComplexRowVector |
|
234 ComplexColumnVector::transpose (void) const |
|
235 { |
1858
|
236 return ComplexRowVector (*this); |
458
|
237 } |
|
238 |
|
239 ComplexColumnVector |
|
240 conj (const ComplexColumnVector& a) |
|
241 { |
|
242 int a_len = a.length (); |
|
243 ComplexColumnVector retval; |
|
244 if (a_len > 0) |
3769
|
245 retval = ComplexColumnVector (mx_inline_conj_dup (a.data (), a_len), a_len); |
458
|
246 return retval; |
|
247 } |
|
248 |
|
249 // resize is the destructive equivalent for this one |
|
250 |
|
251 ComplexColumnVector |
|
252 ComplexColumnVector::extract (int r1, int r2) const |
|
253 { |
|
254 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
255 |
|
256 int new_r = r2 - r1 + 1; |
|
257 |
|
258 ComplexColumnVector result (new_r); |
|
259 |
|
260 for (int i = 0; i < new_r; i++) |
|
261 result.elem (i) = elem (r1+i); |
|
262 |
|
263 return result; |
|
264 } |
|
265 |
4316
|
266 ComplexColumnVector |
|
267 ComplexColumnVector::extract_n (int r1, int n) const |
|
268 { |
|
269 ComplexColumnVector result (n); |
|
270 |
|
271 for (int i = 0; i < n; i++) |
|
272 result.elem (i) = elem (r1+i); |
|
273 |
|
274 return result; |
|
275 } |
|
276 |
458
|
277 // column vector by column vector -> column vector operations |
|
278 |
|
279 ComplexColumnVector& |
|
280 ComplexColumnVector::operator += (const ColumnVector& a) |
|
281 { |
|
282 int len = length (); |
2386
|
283 |
|
284 int a_len = a.length (); |
|
285 |
|
286 if (len != a_len) |
458
|
287 { |
2386
|
288 gripe_nonconformant ("operator +=", len, a_len); |
458
|
289 return *this; |
|
290 } |
|
291 |
|
292 if (len == 0) |
|
293 return *this; |
|
294 |
|
295 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
296 |
3769
|
297 mx_inline_add2 (d, a.data (), len); |
458
|
298 return *this; |
|
299 } |
|
300 |
|
301 ComplexColumnVector& |
|
302 ComplexColumnVector::operator -= (const ColumnVector& a) |
|
303 { |
|
304 int len = length (); |
2386
|
305 |
|
306 int a_len = a.length (); |
|
307 |
|
308 if (len != a_len) |
458
|
309 { |
2386
|
310 gripe_nonconformant ("operator -=", len, a_len); |
458
|
311 return *this; |
|
312 } |
|
313 |
|
314 if (len == 0) |
|
315 return *this; |
|
316 |
|
317 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
318 |
3769
|
319 mx_inline_subtract2 (d, a.data (), len); |
458
|
320 return *this; |
|
321 } |
|
322 |
1205
|
323 // matrix by column vector -> column vector operations |
|
324 |
|
325 ComplexColumnVector |
|
326 operator * (const ComplexMatrix& m, const ColumnVector& a) |
|
327 { |
|
328 ComplexColumnVector tmp (a); |
|
329 return m * tmp; |
|
330 } |
|
331 |
|
332 ComplexColumnVector |
|
333 operator * (const ComplexMatrix& m, const ComplexColumnVector& a) |
|
334 { |
1947
|
335 ComplexColumnVector retval; |
|
336 |
1205
|
337 int nr = m.rows (); |
|
338 int nc = m.cols (); |
1947
|
339 |
2386
|
340 int a_len = a.length (); |
|
341 |
|
342 if (nc != a_len) |
|
343 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
344 else |
458
|
345 { |
1947
|
346 if (nc == 0 || nr == 0) |
|
347 retval.resize (nr, 0.0); |
|
348 else |
|
349 { |
|
350 int ld = nr; |
|
351 |
|
352 retval.resize (nr); |
|
353 Complex *y = retval.fortran_vec (); |
|
354 |
4552
|
355 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
356 nr, nc, 1.0, m.data (), ld, |
|
357 a.data (), 1, 0.0, y, 1 |
|
358 F77_CHAR_ARG_LEN (1))); |
1947
|
359 |
|
360 if (f77_exception_encountered) |
|
361 (*current_liboctave_error_handler) |
|
362 ("unrecoverable error in zgemv"); |
|
363 } |
458
|
364 } |
|
365 |
1947
|
366 return retval; |
458
|
367 } |
|
368 |
1205
|
369 // matrix by column vector -> column vector operations |
|
370 |
|
371 ComplexColumnVector |
|
372 operator * (const Matrix& m, const ComplexColumnVector& a) |
|
373 { |
|
374 ComplexMatrix tmp (m); |
|
375 return tmp * a; |
|
376 } |
|
377 |
|
378 // diagonal matrix by column vector -> column vector operations |
|
379 |
|
380 ComplexColumnVector |
|
381 operator * (const DiagMatrix& m, const ComplexColumnVector& a) |
|
382 { |
|
383 int nr = m.rows (); |
|
384 int nc = m.cols (); |
2386
|
385 |
1205
|
386 int a_len = a.length (); |
2386
|
387 |
1205
|
388 if (nc != a_len) |
|
389 { |
2386
|
390 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
3585
|
391 return ComplexColumnVector (); |
1205
|
392 } |
|
393 |
|
394 if (nc == 0 || nr == 0) |
|
395 return ComplexColumnVector (0); |
|
396 |
|
397 ComplexColumnVector result (nr); |
|
398 |
|
399 for (int i = 0; i < a_len; i++) |
|
400 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
401 |
1321
|
402 for (int i = a_len; i < nr; i++) |
1205
|
403 result.elem (i) = 0.0; |
|
404 |
|
405 return result; |
|
406 } |
|
407 |
|
408 ComplexColumnVector |
|
409 operator * (const ComplexDiagMatrix& m, const ColumnVector& a) |
|
410 { |
|
411 int nr = m.rows (); |
|
412 int nc = m.cols (); |
2386
|
413 |
1205
|
414 int a_len = a.length (); |
2386
|
415 |
1205
|
416 if (nc != a_len) |
|
417 { |
2386
|
418 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1205
|
419 return ComplexColumnVector (); |
|
420 } |
|
421 |
|
422 if (nc == 0 || nr == 0) |
|
423 return ComplexColumnVector (0); |
|
424 |
|
425 ComplexColumnVector result (nr); |
|
426 |
|
427 for (int i = 0; i < a_len; i++) |
|
428 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
429 |
1321
|
430 for (int i = a_len; i < nr; i++) |
1205
|
431 result.elem (i) = 0.0; |
|
432 |
|
433 return result; |
|
434 } |
|
435 |
|
436 ComplexColumnVector |
|
437 operator * (const ComplexDiagMatrix& m, const ComplexColumnVector& a) |
|
438 { |
|
439 int nr = m.rows (); |
|
440 int nc = m.cols (); |
2386
|
441 |
1205
|
442 int a_len = a.length (); |
2386
|
443 |
1205
|
444 if (nc != a_len) |
|
445 { |
2386
|
446 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1205
|
447 return ComplexColumnVector (); |
|
448 } |
|
449 |
|
450 if (nc == 0 || nr == 0) |
|
451 return ComplexColumnVector (0); |
|
452 |
|
453 ComplexColumnVector result (nr); |
|
454 |
|
455 for (int i = 0; i < a_len; i++) |
|
456 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
457 |
1321
|
458 for (int i = a_len; i < nr; i++) |
1205
|
459 result.elem (i) = 0.0; |
|
460 |
|
461 return result; |
|
462 } |
|
463 |
458
|
464 // other operations |
|
465 |
|
466 ComplexColumnVector |
2676
|
467 ComplexColumnVector::map (c_c_Mapper f) const |
458
|
468 { |
2676
|
469 ComplexColumnVector b (*this); |
|
470 return b.apply (f); |
458
|
471 } |
|
472 |
2676
|
473 ColumnVector |
|
474 ComplexColumnVector::map (d_c_Mapper f) const |
458
|
475 { |
2676
|
476 const Complex *d = data (); |
|
477 |
|
478 int len = length (); |
|
479 |
|
480 ColumnVector retval (len); |
|
481 |
|
482 double *r = retval.fortran_vec (); |
|
483 |
|
484 for (int i = 0; i < len; i++) |
|
485 r[i] = f (d[i]); |
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
|
490 ComplexColumnVector& |
|
491 ComplexColumnVector::apply (c_c_Mapper f) |
|
492 { |
|
493 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
494 |
458
|
495 for (int i = 0; i < length (); i++) |
2676
|
496 d[i] = f (d[i]); |
|
497 |
|
498 return *this; |
458
|
499 } |
|
500 |
|
501 Complex |
|
502 ComplexColumnVector::min (void) const |
|
503 { |
|
504 int len = length (); |
|
505 if (len == 0) |
|
506 return 0.0; |
|
507 |
|
508 Complex res = elem (0); |
|
509 double absres = abs (res); |
|
510 |
|
511 for (int i = 1; i < len; i++) |
|
512 if (abs (elem (i)) < absres) |
|
513 { |
|
514 res = elem (i); |
|
515 absres = abs (res); |
|
516 } |
|
517 |
|
518 return res; |
|
519 } |
|
520 |
|
521 Complex |
|
522 ComplexColumnVector::max (void) const |
|
523 { |
|
524 int len = length (); |
|
525 if (len == 0) |
|
526 return 0.0; |
|
527 |
|
528 Complex res = elem (0); |
|
529 double absres = abs (res); |
|
530 |
|
531 for (int i = 1; i < len; i++) |
|
532 if (abs (elem (i)) > absres) |
|
533 { |
|
534 res = elem (i); |
|
535 absres = abs (res); |
|
536 } |
|
537 |
|
538 return res; |
|
539 } |
|
540 |
|
541 // i/o |
|
542 |
3504
|
543 std::ostream& |
|
544 operator << (std::ostream& os, const ComplexColumnVector& a) |
458
|
545 { |
|
546 // int field_width = os.precision () + 7; |
|
547 for (int i = 0; i < a.length (); i++) |
|
548 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
549 return os; |
|
550 } |
|
551 |
3504
|
552 std::istream& |
|
553 operator >> (std::istream& is, ComplexColumnVector& a) |
458
|
554 { |
|
555 int len = a.length(); |
|
556 |
|
557 if (len < 1) |
3504
|
558 is.clear (std::ios::badbit); |
458
|
559 else |
|
560 { |
|
561 double tmp; |
|
562 for (int i = 0; i < len; i++) |
|
563 { |
|
564 is >> tmp; |
|
565 if (is) |
|
566 a.elem (i) = tmp; |
|
567 else |
|
568 break; |
|
569 } |
|
570 } |
532
|
571 return is; |
458
|
572 } |
|
573 |
|
574 /* |
|
575 ;;; Local Variables: *** |
|
576 ;;; mode: C++ *** |
|
577 ;;; End: *** |
|
578 */ |