1993
|
1 // RowVector 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 |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
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); |
458
|
48 } |
|
49 |
1360
|
50 // Complex Row Vector class |
458
|
51 |
|
52 ComplexRowVector::ComplexRowVector (const RowVector& a) |
1214
|
53 : MArray<Complex> (a.length ()) |
458
|
54 { |
5275
|
55 for (octave_idx_type i = 0; i < length (); i++) |
458
|
56 elem (i) = a.elem (i); |
|
57 } |
|
58 |
2386
|
59 bool |
458
|
60 ComplexRowVector::operator == (const ComplexRowVector& a) const |
|
61 { |
5275
|
62 octave_idx_type len = length (); |
458
|
63 if (len != a.length ()) |
|
64 return 0; |
3769
|
65 return mx_inline_equal (data (), a.data (), len); |
458
|
66 } |
|
67 |
2386
|
68 bool |
458
|
69 ComplexRowVector::operator != (const ComplexRowVector& a) const |
|
70 { |
|
71 return !(*this == a); |
|
72 } |
|
73 |
|
74 // destructive insert/delete/reorder operations |
|
75 |
|
76 ComplexRowVector& |
5275
|
77 ComplexRowVector::insert (const RowVector& a, octave_idx_type c) |
458
|
78 { |
5275
|
79 octave_idx_type a_len = a.length (); |
4316
|
80 |
1699
|
81 if (c < 0 || c + a_len > length ()) |
458
|
82 { |
|
83 (*current_liboctave_error_handler) ("range error for insert"); |
|
84 return *this; |
|
85 } |
|
86 |
4316
|
87 if (a_len > 0) |
|
88 { |
|
89 make_unique (); |
|
90 |
5275
|
91 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
92 xelem (c+i) = a.elem (i); |
|
93 } |
458
|
94 |
|
95 return *this; |
|
96 } |
|
97 |
|
98 ComplexRowVector& |
5275
|
99 ComplexRowVector::insert (const ComplexRowVector& a, octave_idx_type c) |
458
|
100 { |
5275
|
101 octave_idx_type a_len = a.length (); |
4316
|
102 |
1699
|
103 if (c < 0 || c + a_len > length ()) |
458
|
104 { |
|
105 (*current_liboctave_error_handler) ("range error for insert"); |
|
106 return *this; |
|
107 } |
|
108 |
4316
|
109 if (a_len > 0) |
|
110 { |
|
111 make_unique (); |
|
112 |
5275
|
113 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
114 xelem (c+i) = a.elem (i); |
|
115 } |
458
|
116 |
|
117 return *this; |
|
118 } |
|
119 |
|
120 ComplexRowVector& |
|
121 ComplexRowVector::fill (double val) |
|
122 { |
5275
|
123 octave_idx_type len = length (); |
4316
|
124 |
458
|
125 if (len > 0) |
4316
|
126 { |
|
127 make_unique (); |
|
128 |
5275
|
129 for (octave_idx_type i = 0; i < len; i++) |
4316
|
130 xelem (i) = val; |
|
131 } |
|
132 |
458
|
133 return *this; |
|
134 } |
|
135 |
|
136 ComplexRowVector& |
|
137 ComplexRowVector::fill (const Complex& val) |
|
138 { |
5275
|
139 octave_idx_type len = length (); |
4316
|
140 |
458
|
141 if (len > 0) |
4316
|
142 { |
|
143 make_unique (); |
|
144 |
5275
|
145 for (octave_idx_type i = 0; i < len; i++) |
4316
|
146 xelem (i) = val; |
|
147 } |
|
148 |
458
|
149 return *this; |
|
150 } |
|
151 |
|
152 ComplexRowVector& |
5275
|
153 ComplexRowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458
|
154 { |
5275
|
155 octave_idx_type len = length (); |
4316
|
156 |
458
|
157 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
158 { |
|
159 (*current_liboctave_error_handler) ("range error for fill"); |
|
160 return *this; |
|
161 } |
|
162 |
5275
|
163 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
164 |
4316
|
165 if (c2 >= c1) |
|
166 { |
|
167 make_unique (); |
|
168 |
5275
|
169 for (octave_idx_type i = c1; i <= c2; i++) |
4316
|
170 xelem (i) = val; |
|
171 } |
458
|
172 |
|
173 return *this; |
|
174 } |
|
175 |
|
176 ComplexRowVector& |
5275
|
177 ComplexRowVector::fill (const Complex& val, octave_idx_type c1, octave_idx_type c2) |
458
|
178 { |
5275
|
179 octave_idx_type len = length (); |
4316
|
180 |
458
|
181 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
182 { |
|
183 (*current_liboctave_error_handler) ("range error for fill"); |
|
184 return *this; |
|
185 } |
|
186 |
5275
|
187 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
188 |
4316
|
189 if (c2 >= c1) |
|
190 { |
|
191 make_unique (); |
|
192 |
5275
|
193 for (octave_idx_type i = c1; i <= c2; i++) |
4316
|
194 xelem (i) = val; |
|
195 } |
458
|
196 |
|
197 return *this; |
|
198 } |
|
199 |
|
200 ComplexRowVector |
|
201 ComplexRowVector::append (const RowVector& a) const |
|
202 { |
5275
|
203 octave_idx_type len = length (); |
|
204 octave_idx_type nc_insert = len; |
458
|
205 ComplexRowVector retval (len + a.length ()); |
|
206 retval.insert (*this, 0); |
|
207 retval.insert (a, nc_insert); |
|
208 return retval; |
|
209 } |
|
210 |
|
211 ComplexRowVector |
|
212 ComplexRowVector::append (const ComplexRowVector& a) const |
|
213 { |
5275
|
214 octave_idx_type len = length (); |
|
215 octave_idx_type nc_insert = len; |
458
|
216 ComplexRowVector retval (len + a.length ()); |
|
217 retval.insert (*this, 0); |
|
218 retval.insert (a, nc_insert); |
|
219 return retval; |
|
220 } |
|
221 |
|
222 ComplexColumnVector |
|
223 ComplexRowVector::hermitian (void) const |
|
224 { |
5275
|
225 octave_idx_type len = length (); |
3769
|
226 return ComplexColumnVector (mx_inline_conj_dup (data (), len), len); |
458
|
227 } |
|
228 |
|
229 ComplexColumnVector |
|
230 ComplexRowVector::transpose (void) const |
|
231 { |
1858
|
232 return ComplexColumnVector (*this); |
458
|
233 } |
|
234 |
|
235 ComplexRowVector |
|
236 conj (const ComplexRowVector& a) |
|
237 { |
5275
|
238 octave_idx_type a_len = a.length (); |
458
|
239 ComplexRowVector retval; |
|
240 if (a_len > 0) |
3769
|
241 retval = ComplexRowVector (mx_inline_conj_dup (a.data (), a_len), a_len); |
458
|
242 return retval; |
|
243 } |
|
244 |
|
245 // resize is the destructive equivalent for this one |
|
246 |
|
247 ComplexRowVector |
5275
|
248 ComplexRowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458
|
249 { |
5275
|
250 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
251 |
5275
|
252 octave_idx_type new_c = c2 - c1 + 1; |
458
|
253 |
|
254 ComplexRowVector result (new_c); |
|
255 |
5275
|
256 for (octave_idx_type i = 0; i < new_c; i++) |
458
|
257 result.elem (i) = elem (c1+i); |
|
258 |
|
259 return result; |
|
260 } |
|
261 |
4316
|
262 ComplexRowVector |
5275
|
263 ComplexRowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316
|
264 { |
|
265 ComplexRowVector result (n); |
|
266 |
5275
|
267 for (octave_idx_type i = 0; i < n; i++) |
4316
|
268 result.elem (i) = elem (r1+i); |
|
269 |
|
270 return result; |
|
271 } |
|
272 |
458
|
273 // row vector by row vector -> row vector operations |
|
274 |
|
275 ComplexRowVector& |
|
276 ComplexRowVector::operator += (const RowVector& a) |
|
277 { |
5275
|
278 octave_idx_type len = length (); |
2386
|
279 |
5275
|
280 octave_idx_type a_len = a.length (); |
2386
|
281 |
|
282 if (len != a_len) |
458
|
283 { |
2386
|
284 gripe_nonconformant ("operator +=", len, a_len); |
458
|
285 return *this; |
|
286 } |
|
287 |
|
288 if (len == 0) |
|
289 return *this; |
|
290 |
|
291 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
292 |
3769
|
293 mx_inline_add2 (d, a.data (), len); |
458
|
294 return *this; |
|
295 } |
|
296 |
|
297 ComplexRowVector& |
|
298 ComplexRowVector::operator -= (const RowVector& a) |
|
299 { |
5275
|
300 octave_idx_type len = length (); |
2386
|
301 |
5275
|
302 octave_idx_type a_len = a.length (); |
2386
|
303 |
|
304 if (len != a_len) |
458
|
305 { |
2386
|
306 gripe_nonconformant ("operator -=", len, a_len); |
458
|
307 return *this; |
|
308 } |
|
309 |
|
310 if (len == 0) |
|
311 return *this; |
|
312 |
|
313 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
314 |
3769
|
315 mx_inline_subtract2 (d, a.data (), len); |
458
|
316 return *this; |
|
317 } |
|
318 |
|
319 // row vector by matrix -> row vector |
|
320 |
|
321 ComplexRowVector |
|
322 operator * (const ComplexRowVector& v, const ComplexMatrix& a) |
|
323 { |
1947
|
324 ComplexRowVector retval; |
|
325 |
5275
|
326 octave_idx_type len = v.length (); |
1947
|
327 |
5275
|
328 octave_idx_type a_nr = a.rows (); |
|
329 octave_idx_type a_nc = a.cols (); |
2386
|
330 |
|
331 if (a_nr != len) |
|
332 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); |
1947
|
333 else |
458
|
334 { |
1947
|
335 if (len == 0) |
|
336 retval.resize (a_nc, 0.0); |
|
337 else |
|
338 { |
|
339 // Transpose A to form A'*x == (x'*A)' |
|
340 |
5275
|
341 octave_idx_type ld = a_nr; |
1947
|
342 |
|
343 retval.resize (a_nc); |
|
344 Complex *y = retval.fortran_vec (); |
|
345 |
4552
|
346 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
|
347 a_nr, a_nc, 1.0, a.data (), |
|
348 ld, v.data (), 1, 0.0, y, 1 |
|
349 F77_CHAR_ARG_LEN (1))); |
1947
|
350 |
|
351 if (f77_exception_encountered) |
|
352 (*current_liboctave_error_handler) |
|
353 ("unrecoverable error in zgemv"); |
|
354 } |
458
|
355 } |
|
356 |
1947
|
357 return retval; |
458
|
358 } |
|
359 |
1205
|
360 ComplexRowVector |
|
361 operator * (const RowVector& v, const ComplexMatrix& a) |
|
362 { |
|
363 ComplexRowVector tmp (v); |
|
364 return tmp * a; |
|
365 } |
|
366 |
458
|
367 // other operations |
|
368 |
|
369 ComplexRowVector |
2676
|
370 ComplexRowVector::map (c_c_Mapper f) const |
458
|
371 { |
2676
|
372 ComplexRowVector b (*this); |
|
373 return b.apply (f); |
458
|
374 } |
|
375 |
2676
|
376 RowVector |
|
377 ComplexRowVector::map (d_c_Mapper f) const |
458
|
378 { |
2676
|
379 const Complex *d = data (); |
|
380 |
5275
|
381 octave_idx_type len = length (); |
2676
|
382 |
|
383 RowVector retval (len); |
|
384 |
|
385 double *r = retval.fortran_vec (); |
|
386 |
5275
|
387 for (octave_idx_type i = 0; i < len; i++) |
2676
|
388 r[i] = f (d[i]); |
|
389 |
|
390 return retval; |
|
391 } |
|
392 |
|
393 ComplexRowVector& |
|
394 ComplexRowVector::apply (c_c_Mapper f) |
|
395 { |
|
396 Complex *d = fortran_vec (); // Ensures only one reference to my privates! |
|
397 |
5275
|
398 for (octave_idx_type i = 0; i < length (); i++) |
2676
|
399 d[i] = f (d[i]); |
|
400 |
|
401 return *this; |
458
|
402 } |
|
403 |
|
404 Complex |
|
405 ComplexRowVector::min (void) const |
|
406 { |
5275
|
407 octave_idx_type len = length (); |
458
|
408 if (len == 0) |
|
409 return Complex (0.0); |
|
410 |
|
411 Complex res = elem (0); |
5260
|
412 double absres = std::abs (res); |
458
|
413 |
5275
|
414 for (octave_idx_type i = 1; i < len; i++) |
5260
|
415 if (std::abs (elem (i)) < absres) |
458
|
416 { |
|
417 res = elem (i); |
5260
|
418 absres = std::abs (res); |
458
|
419 } |
|
420 |
|
421 return res; |
|
422 } |
|
423 |
|
424 Complex |
|
425 ComplexRowVector::max (void) const |
|
426 { |
5275
|
427 octave_idx_type len = length (); |
458
|
428 if (len == 0) |
|
429 return Complex (0.0); |
|
430 |
|
431 Complex res = elem (0); |
5260
|
432 double absres = std::abs (res); |
458
|
433 |
5275
|
434 for (octave_idx_type i = 1; i < len; i++) |
5260
|
435 if (std::abs (elem (i)) > absres) |
458
|
436 { |
|
437 res = elem (i); |
5260
|
438 absres = std::abs (res); |
458
|
439 } |
|
440 |
|
441 return res; |
|
442 } |
|
443 |
|
444 // i/o |
|
445 |
3504
|
446 std::ostream& |
|
447 operator << (std::ostream& os, const ComplexRowVector& a) |
458
|
448 { |
|
449 // int field_width = os.precision () + 7; |
5275
|
450 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
451 os << " " /* setw (field_width) */ << a.elem (i); |
|
452 return os; |
|
453 } |
|
454 |
3504
|
455 std::istream& |
|
456 operator >> (std::istream& is, ComplexRowVector& a) |
458
|
457 { |
5275
|
458 octave_idx_type len = a.length(); |
458
|
459 |
|
460 if (len < 1) |
3504
|
461 is.clear (std::ios::badbit); |
458
|
462 else |
|
463 { |
|
464 Complex tmp; |
5275
|
465 for (octave_idx_type i = 0; i < len; i++) |
458
|
466 { |
|
467 is >> tmp; |
|
468 if (is) |
|
469 a.elem (i) = tmp; |
|
470 else |
|
471 break; |
|
472 } |
|
473 } |
532
|
474 return is; |
458
|
475 } |
|
476 |
1205
|
477 // row vector by column vector -> scalar |
|
478 |
|
479 // row vector by column vector -> scalar |
|
480 |
|
481 Complex |
|
482 operator * (const ComplexRowVector& v, const ColumnVector& a) |
|
483 { |
|
484 ComplexColumnVector tmp (a); |
|
485 return v * tmp; |
|
486 } |
|
487 |
|
488 Complex |
|
489 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) |
|
490 { |
5275
|
491 octave_idx_type len = v.length (); |
2386
|
492 |
5275
|
493 octave_idx_type a_len = a.length (); |
2386
|
494 |
|
495 if (len != a_len) |
1205
|
496 { |
2386
|
497 gripe_nonconformant ("operator *", len, a_len); |
1205
|
498 return 0.0; |
|
499 } |
|
500 |
|
501 Complex retval (0.0, 0.0); |
|
502 |
5275
|
503 for (octave_idx_type i = 0; i < len; i++) |
1205
|
504 retval += v.elem (i) * a.elem (i); |
|
505 |
|
506 return retval; |
|
507 } |
|
508 |
|
509 // other operations |
|
510 |
|
511 ComplexRowVector |
5275
|
512 linspace (const Complex& x1, const Complex& x2, octave_idx_type n) |
1205
|
513 { |
|
514 ComplexRowVector retval; |
|
515 |
|
516 if (n > 0) |
|
517 { |
|
518 retval.resize (n); |
3092
|
519 Complex delta = (x2 - x1) / (n - 1.0); |
1205
|
520 retval.elem (0) = x1; |
5275
|
521 for (octave_idx_type i = 1; i < n-1; i++) |
3092
|
522 retval.elem (i) = x1 + 1.0 * i * delta; |
1205
|
523 retval.elem (n-1) = x2; |
|
524 } |
3322
|
525 else if (n == 1) |
|
526 { |
|
527 if (x1 == x2) |
|
528 { |
|
529 retval.resize (1); |
|
530 retval.elem (0) = x1; |
|
531 } |
|
532 else |
|
533 (*current_liboctave_error_handler) |
|
534 ("linspace: npoints is 1, but x1 != x2"); |
|
535 } |
|
536 else |
|
537 (*current_liboctave_error_handler) |
|
538 ("linspace: npoints must be greater than 0"); |
1205
|
539 |
|
540 return retval; |
|
541 } |
|
542 |
458
|
543 /* |
|
544 ;;; Local Variables: *** |
|
545 ;;; mode: C++ *** |
|
546 ;;; End: *** |
|
547 */ |