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