1993
|
1 // RowVector manipulations. |
458
|
2 /* |
|
3 |
7017
|
4 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, |
|
5 2004, 2005, 2006, 2007 John W. Eaton |
458
|
6 |
|
7 This file is part of Octave. |
|
8 |
|
9 Octave is free software; you can redistribute it and/or modify it |
|
10 under the terms of the GNU General Public License as published by the |
7016
|
11 Free Software Foundation; either version 3 of the License, or (at your |
|
12 option) any later version. |
458
|
13 |
|
14 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
17 for more details. |
|
18 |
|
19 You should have received a copy of the GNU General Public License |
7016
|
20 along with Octave; see the file COPYING. If not, see |
|
21 <http://www.gnu.org/licenses/>. |
458
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
458
|
27 #endif |
|
28 |
3503
|
29 #include <iostream> |
458
|
30 |
4669
|
31 #include "Array-util.h" |
1847
|
32 #include "f77-fcn.h" |
1368
|
33 #include "lo-error.h" |
458
|
34 #include "mx-base.h" |
|
35 #include "mx-inlines.cc" |
1650
|
36 #include "oct-cmplx.h" |
458
|
37 |
|
38 // Fortran functions we call. |
|
39 |
|
40 extern "C" |
|
41 { |
4552
|
42 F77_RET_T |
|
43 F77_FUNC (zgemv, ZGEMV) (F77_CONST_CHAR_ARG_DECL, |
5275
|
44 const octave_idx_type&, const octave_idx_type&, const Complex&, |
|
45 const Complex*, const octave_idx_type&, const Complex*, |
|
46 const octave_idx_type&, const Complex&, Complex*, const octave_idx_type& |
4552
|
47 F77_CHAR_ARG_LEN_DECL); |
5983
|
48 |
|
49 F77_RET_T |
|
50 F77_FUNC (xzdotu, XZDOTU) (const octave_idx_type&, const Complex*, const octave_idx_type&, |
|
51 const Complex*, const octave_idx_type&, Complex&); |
458
|
52 } |
|
53 |
1360
|
54 // Complex Row Vector class |
458
|
55 |
|
56 ComplexRowVector::ComplexRowVector (const RowVector& a) |
1214
|
57 : MArray<Complex> (a.length ()) |
458
|
58 { |
5275
|
59 for (octave_idx_type i = 0; i < length (); i++) |
458
|
60 elem (i) = a.elem (i); |
|
61 } |
|
62 |
2386
|
63 bool |
458
|
64 ComplexRowVector::operator == (const ComplexRowVector& a) const |
|
65 { |
5275
|
66 octave_idx_type len = length (); |
458
|
67 if (len != a.length ()) |
|
68 return 0; |
3769
|
69 return mx_inline_equal (data (), a.data (), len); |
458
|
70 } |
|
71 |
2386
|
72 bool |
458
|
73 ComplexRowVector::operator != (const ComplexRowVector& a) const |
|
74 { |
|
75 return !(*this == a); |
|
76 } |
|
77 |
|
78 // destructive insert/delete/reorder operations |
|
79 |
|
80 ComplexRowVector& |
5275
|
81 ComplexRowVector::insert (const RowVector& a, octave_idx_type c) |
458
|
82 { |
5275
|
83 octave_idx_type a_len = a.length (); |
4316
|
84 |
1699
|
85 if (c < 0 || c + a_len > length ()) |
458
|
86 { |
|
87 (*current_liboctave_error_handler) ("range error for insert"); |
|
88 return *this; |
|
89 } |
|
90 |
4316
|
91 if (a_len > 0) |
|
92 { |
|
93 make_unique (); |
|
94 |
5275
|
95 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
96 xelem (c+i) = a.elem (i); |
|
97 } |
458
|
98 |
|
99 return *this; |
|
100 } |
|
101 |
|
102 ComplexRowVector& |
5275
|
103 ComplexRowVector::insert (const ComplexRowVector& a, octave_idx_type c) |
458
|
104 { |
5275
|
105 octave_idx_type a_len = a.length (); |
4316
|
106 |
1699
|
107 if (c < 0 || c + a_len > length ()) |
458
|
108 { |
|
109 (*current_liboctave_error_handler) ("range error for insert"); |
|
110 return *this; |
|
111 } |
|
112 |
4316
|
113 if (a_len > 0) |
|
114 { |
|
115 make_unique (); |
|
116 |
5275
|
117 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
118 xelem (c+i) = a.elem (i); |
|
119 } |
458
|
120 |
|
121 return *this; |
|
122 } |
|
123 |
|
124 ComplexRowVector& |
|
125 ComplexRowVector::fill (double val) |
|
126 { |
5275
|
127 octave_idx_type len = length (); |
4316
|
128 |
458
|
129 if (len > 0) |
4316
|
130 { |
|
131 make_unique (); |
|
132 |
5275
|
133 for (octave_idx_type i = 0; i < len; i++) |
4316
|
134 xelem (i) = val; |
|
135 } |
|
136 |
458
|
137 return *this; |
|
138 } |
|
139 |
|
140 ComplexRowVector& |
|
141 ComplexRowVector::fill (const Complex& val) |
|
142 { |
5275
|
143 octave_idx_type len = length (); |
4316
|
144 |
458
|
145 if (len > 0) |
4316
|
146 { |
|
147 make_unique (); |
|
148 |
5275
|
149 for (octave_idx_type i = 0; i < len; i++) |
4316
|
150 xelem (i) = val; |
|
151 } |
|
152 |
458
|
153 return *this; |
|
154 } |
|
155 |
|
156 ComplexRowVector& |
5275
|
157 ComplexRowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458
|
158 { |
5275
|
159 octave_idx_type len = length (); |
4316
|
160 |
458
|
161 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
162 { |
|
163 (*current_liboctave_error_handler) ("range error for fill"); |
|
164 return *this; |
|
165 } |
|
166 |
5275
|
167 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
168 |
4316
|
169 if (c2 >= c1) |
|
170 { |
|
171 make_unique (); |
|
172 |
5275
|
173 for (octave_idx_type i = c1; i <= c2; i++) |
4316
|
174 xelem (i) = val; |
|
175 } |
458
|
176 |
|
177 return *this; |
|
178 } |
|
179 |
|
180 ComplexRowVector& |
5275
|
181 ComplexRowVector::fill (const Complex& val, octave_idx_type c1, octave_idx_type c2) |
458
|
182 { |
5275
|
183 octave_idx_type len = length (); |
4316
|
184 |
458
|
185 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
186 { |
|
187 (*current_liboctave_error_handler) ("range error for fill"); |
|
188 return *this; |
|
189 } |
|
190 |
5275
|
191 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
192 |
4316
|
193 if (c2 >= c1) |
|
194 { |
|
195 make_unique (); |
|
196 |
5275
|
197 for (octave_idx_type i = c1; i <= c2; i++) |
4316
|
198 xelem (i) = val; |
|
199 } |
458
|
200 |
|
201 return *this; |
|
202 } |
|
203 |
|
204 ComplexRowVector |
|
205 ComplexRowVector::append (const RowVector& a) const |
|
206 { |
5275
|
207 octave_idx_type len = length (); |
|
208 octave_idx_type nc_insert = len; |
458
|
209 ComplexRowVector retval (len + a.length ()); |
|
210 retval.insert (*this, 0); |
|
211 retval.insert (a, nc_insert); |
|
212 return retval; |
|
213 } |
|
214 |
|
215 ComplexRowVector |
|
216 ComplexRowVector::append (const ComplexRowVector& a) const |
|
217 { |
5275
|
218 octave_idx_type len = length (); |
|
219 octave_idx_type nc_insert = len; |
458
|
220 ComplexRowVector retval (len + a.length ()); |
|
221 retval.insert (*this, 0); |
|
222 retval.insert (a, nc_insert); |
|
223 return retval; |
|
224 } |
|
225 |
|
226 ComplexColumnVector |
|
227 ComplexRowVector::hermitian (void) const |
|
228 { |
5275
|
229 octave_idx_type len = length (); |
3769
|
230 return ComplexColumnVector (mx_inline_conj_dup (data (), len), len); |
458
|
231 } |
|
232 |
|
233 ComplexColumnVector |
|
234 ComplexRowVector::transpose (void) const |
|
235 { |
1858
|
236 return ComplexColumnVector (*this); |
458
|
237 } |
|
238 |
|
239 ComplexRowVector |
|
240 conj (const ComplexRowVector& a) |
|
241 { |
5275
|
242 octave_idx_type a_len = a.length (); |
458
|
243 ComplexRowVector retval; |
|
244 if (a_len > 0) |
3769
|
245 retval = ComplexRowVector (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 ComplexRowVector |
5275
|
252 ComplexRowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458
|
253 { |
5275
|
254 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
255 |
5275
|
256 octave_idx_type new_c = c2 - c1 + 1; |
458
|
257 |
|
258 ComplexRowVector result (new_c); |
|
259 |
5275
|
260 for (octave_idx_type i = 0; i < new_c; i++) |
458
|
261 result.elem (i) = elem (c1+i); |
|
262 |
|
263 return result; |
|
264 } |
|
265 |
4316
|
266 ComplexRowVector |
5275
|
267 ComplexRowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316
|
268 { |
|
269 ComplexRowVector result (n); |
|
270 |
5275
|
271 for (octave_idx_type i = 0; i < n; i++) |
4316
|
272 result.elem (i) = elem (r1+i); |
|
273 |
|
274 return result; |
|
275 } |
|
276 |
458
|
277 // row vector by row vector -> row vector operations |
|
278 |
|
279 ComplexRowVector& |
|
280 ComplexRowVector::operator += (const RowVector& a) |
|
281 { |
5275
|
282 octave_idx_type len = length (); |
2386
|
283 |
5275
|
284 octave_idx_type a_len = a.length (); |
2386
|
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 ComplexRowVector& |
|
302 ComplexRowVector::operator -= (const RowVector& a) |
|
303 { |
5275
|
304 octave_idx_type len = length (); |
2386
|
305 |
5275
|
306 octave_idx_type a_len = a.length (); |
2386
|
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 |
|
323 // row vector by matrix -> row vector |
|
324 |
|
325 ComplexRowVector |
|
326 operator * (const ComplexRowVector& v, const ComplexMatrix& a) |
|
327 { |
1947
|
328 ComplexRowVector retval; |
|
329 |
5275
|
330 octave_idx_type len = v.length (); |
1947
|
331 |
5275
|
332 octave_idx_type a_nr = a.rows (); |
|
333 octave_idx_type a_nc = a.cols (); |
2386
|
334 |
|
335 if (a_nr != len) |
|
336 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); |
1947
|
337 else |
458
|
338 { |
1947
|
339 if (len == 0) |
|
340 retval.resize (a_nc, 0.0); |
|
341 else |
|
342 { |
|
343 // Transpose A to form A'*x == (x'*A)' |
|
344 |
5275
|
345 octave_idx_type ld = a_nr; |
1947
|
346 |
|
347 retval.resize (a_nc); |
|
348 Complex *y = retval.fortran_vec (); |
|
349 |
4552
|
350 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
|
351 a_nr, a_nc, 1.0, a.data (), |
|
352 ld, v.data (), 1, 0.0, y, 1 |
|
353 F77_CHAR_ARG_LEN (1))); |
1947
|
354 |
|
355 if (f77_exception_encountered) |
|
356 (*current_liboctave_error_handler) |
|
357 ("unrecoverable error in zgemv"); |
|
358 } |
458
|
359 } |
|
360 |
1947
|
361 return retval; |
458
|
362 } |
|
363 |
1205
|
364 ComplexRowVector |
|
365 operator * (const RowVector& v, const ComplexMatrix& a) |
|
366 { |
|
367 ComplexRowVector tmp (v); |
|
368 return tmp * a; |
|
369 } |
|
370 |
458
|
371 // other operations |
|
372 |
|
373 ComplexRowVector |
2676
|
374 ComplexRowVector::map (c_c_Mapper f) const |
458
|
375 { |
2676
|
376 ComplexRowVector b (*this); |
|
377 return b.apply (f); |
458
|
378 } |
|
379 |
2676
|
380 RowVector |
|
381 ComplexRowVector::map (d_c_Mapper f) const |
458
|
382 { |
2676
|
383 const Complex *d = data (); |
|
384 |
5275
|
385 octave_idx_type len = length (); |
2676
|
386 |
|
387 RowVector retval (len); |
|
388 |
|
389 double *r = retval.fortran_vec (); |
|
390 |
5275
|
391 for (octave_idx_type i = 0; i < len; i++) |
2676
|
392 r[i] = f (d[i]); |
|
393 |
|
394 return retval; |
|
395 } |
|
396 |
|
397 ComplexRowVector& |
|
398 ComplexRowVector::apply (c_c_Mapper f) |
|
399 { |
|
400 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
401 |
5275
|
402 for (octave_idx_type i = 0; i < length (); i++) |
2676
|
403 d[i] = f (d[i]); |
|
404 |
|
405 return *this; |
458
|
406 } |
|
407 |
|
408 Complex |
|
409 ComplexRowVector::min (void) const |
|
410 { |
5275
|
411 octave_idx_type len = length (); |
458
|
412 if (len == 0) |
|
413 return Complex (0.0); |
|
414 |
|
415 Complex res = elem (0); |
5260
|
416 double absres = std::abs (res); |
458
|
417 |
5275
|
418 for (octave_idx_type i = 1; i < len; i++) |
5260
|
419 if (std::abs (elem (i)) < absres) |
458
|
420 { |
|
421 res = elem (i); |
5260
|
422 absres = std::abs (res); |
458
|
423 } |
|
424 |
|
425 return res; |
|
426 } |
|
427 |
|
428 Complex |
|
429 ComplexRowVector::max (void) const |
|
430 { |
5275
|
431 octave_idx_type len = length (); |
458
|
432 if (len == 0) |
|
433 return Complex (0.0); |
|
434 |
|
435 Complex res = elem (0); |
5260
|
436 double absres = std::abs (res); |
458
|
437 |
5275
|
438 for (octave_idx_type i = 1; i < len; i++) |
5260
|
439 if (std::abs (elem (i)) > absres) |
458
|
440 { |
|
441 res = elem (i); |
5260
|
442 absres = std::abs (res); |
458
|
443 } |
|
444 |
|
445 return res; |
|
446 } |
|
447 |
|
448 // i/o |
|
449 |
3504
|
450 std::ostream& |
|
451 operator << (std::ostream& os, const ComplexRowVector& a) |
458
|
452 { |
|
453 // int field_width = os.precision () + 7; |
5275
|
454 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
455 os << " " /* setw (field_width) */ << a.elem (i); |
|
456 return os; |
|
457 } |
|
458 |
3504
|
459 std::istream& |
|
460 operator >> (std::istream& is, ComplexRowVector& a) |
458
|
461 { |
5275
|
462 octave_idx_type len = a.length(); |
458
|
463 |
|
464 if (len < 1) |
3504
|
465 is.clear (std::ios::badbit); |
458
|
466 else |
|
467 { |
|
468 Complex tmp; |
5275
|
469 for (octave_idx_type i = 0; i < len; i++) |
458
|
470 { |
|
471 is >> tmp; |
|
472 if (is) |
|
473 a.elem (i) = tmp; |
|
474 else |
|
475 break; |
|
476 } |
|
477 } |
532
|
478 return is; |
458
|
479 } |
|
480 |
1205
|
481 // row vector by column vector -> scalar |
|
482 |
|
483 // row vector by column vector -> scalar |
|
484 |
|
485 Complex |
|
486 operator * (const ComplexRowVector& v, const ColumnVector& a) |
|
487 { |
|
488 ComplexColumnVector tmp (a); |
|
489 return v * tmp; |
|
490 } |
|
491 |
|
492 Complex |
|
493 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) |
|
494 { |
5983
|
495 Complex retval (0.0, 0.0); |
|
496 |
5275
|
497 octave_idx_type len = v.length (); |
2386
|
498 |
5275
|
499 octave_idx_type a_len = a.length (); |
2386
|
500 |
|
501 if (len != a_len) |
5983
|
502 gripe_nonconformant ("operator *", len, a_len); |
|
503 else if (len != 0) |
|
504 F77_FUNC (xzdotu, XZDOTU) (len, v.data (), 1, a.data (), 1, retval); |
5275
|
505 for (octave_idx_type i = 0; i < len; i++) |
1205
|
506 retval += v.elem (i) * a.elem (i); |
|
507 |
|
508 return retval; |
|
509 } |
|
510 |
|
511 // other operations |
|
512 |
|
513 ComplexRowVector |
5275
|
514 linspace (const Complex& x1, const Complex& x2, octave_idx_type n) |
1205
|
515 { |
|
516 ComplexRowVector retval; |
|
517 |
|
518 if (n > 0) |
|
519 { |
|
520 retval.resize (n); |
3092
|
521 Complex delta = (x2 - x1) / (n - 1.0); |
1205
|
522 retval.elem (0) = x1; |
5275
|
523 for (octave_idx_type i = 1; i < n-1; i++) |
3092
|
524 retval.elem (i) = x1 + 1.0 * i * delta; |
1205
|
525 retval.elem (n-1) = x2; |
|
526 } |
6630
|
527 else |
3322
|
528 { |
6629
|
529 retval.resize (1); |
|
530 retval.elem (0) = x2; |
3322
|
531 } |
1205
|
532 |
|
533 return retval; |
|
534 } |
|
535 |
458
|
536 /* |
|
537 ;;; Local Variables: *** |
|
538 ;;; mode: C++ *** |
|
539 ;;; End: *** |
|
540 */ |