458
|
1 // RowVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 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 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 #include "f77-uscore.h" |
|
40 |
|
41 // Fortran functions we call. |
|
42 |
|
43 extern "C" |
|
44 { |
|
45 int F77_FCN (dgemv) (const char*, const int*, const int*, |
|
46 const double*, const double*, const int*, |
|
47 const double*, const int*, const double*, |
|
48 double*, const int*, long); |
|
49 |
|
50 double F77_FCN (ddot) (const int*, const double*, const int*, |
|
51 const double*, const int*); |
|
52 } |
|
53 |
|
54 /* |
|
55 * Row Vector class. |
|
56 */ |
|
57 |
|
58 #define KLUDGE_VECTORS |
|
59 #define TYPE double |
|
60 #define KL_VEC_TYPE RowVector |
|
61 #include "mx-kludge.cc" |
|
62 #undef KLUDGE_VECTORS |
|
63 #undef TYPE |
|
64 #undef KL_VEC_TYPE |
|
65 |
|
66 #if 0 |
|
67 RowVector& |
|
68 RowVector::resize (int n) |
|
69 { |
|
70 if (n < 0) |
|
71 { |
|
72 (*current_liboctave_error_handler) |
|
73 ("can't resize to negative dimension"); |
|
74 return *this; |
|
75 } |
|
76 |
|
77 double *new_data = (double *) NULL; |
|
78 if (n > 0) |
|
79 { |
|
80 new_data = new double [n]; |
|
81 int min_len = len < n ? len : n; |
|
82 |
|
83 for (int i = 0; i < min_len; i++) |
|
84 new_data[i] = data[i]; |
|
85 } |
|
86 |
|
87 delete [] data; |
|
88 len = n; |
|
89 data = new_data; |
|
90 |
|
91 return *this; |
|
92 } |
|
93 |
|
94 RowVector& |
|
95 RowVector::resize (int n, double val) |
|
96 { |
|
97 int old_len = len; |
|
98 resize (n); |
|
99 for (int i = old_len; i < len; i++) |
|
100 data[i] = val; |
|
101 |
|
102 return *this; |
|
103 } |
|
104 #endif |
|
105 |
|
106 int |
|
107 RowVector::operator == (const RowVector& a) const |
|
108 { |
|
109 int len = length (); |
|
110 if (len != a.length ()) |
|
111 return 0; |
|
112 return equal (data (), a.data (), len); |
|
113 } |
|
114 |
|
115 int |
|
116 RowVector::operator != (const RowVector& a) const |
|
117 { |
|
118 return !(*this == a); |
|
119 } |
|
120 |
|
121 RowVector& |
|
122 RowVector::insert (const RowVector& a, int c) |
|
123 { |
|
124 int a_len = a.length (); |
|
125 if (c < 0 || c + a_len - 1 > length ()) |
|
126 { |
|
127 (*current_liboctave_error_handler) ("range error for insert"); |
|
128 return *this; |
|
129 } |
|
130 |
|
131 for (int i = 0; i < a_len; i++) |
|
132 elem (c+i) = a.elem (i); |
|
133 |
|
134 return *this; |
|
135 } |
|
136 |
|
137 RowVector& |
|
138 RowVector::fill (double val) |
|
139 { |
|
140 int len = length (); |
|
141 if (len > 0) |
|
142 for (int i = 0; i < len; i++) |
|
143 elem (i) = val; |
|
144 return *this; |
|
145 } |
|
146 |
|
147 RowVector& |
|
148 RowVector::fill (double 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 RowVector |
|
166 RowVector::append (const RowVector& a) const |
|
167 { |
|
168 int len = length (); |
|
169 int nc_insert = len; |
|
170 RowVector retval (len + a.length ()); |
|
171 retval.insert (*this, 0); |
|
172 retval.insert (a, nc_insert); |
|
173 return retval; |
|
174 } |
|
175 |
|
176 ColumnVector |
|
177 RowVector::transpose (void) const |
|
178 { |
|
179 int len = length (); |
|
180 return ColumnVector (dup (data (), len), len); |
|
181 } |
|
182 |
|
183 RowVector |
|
184 RowVector::extract (int c1, int c2) const |
|
185 { |
|
186 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
187 |
|
188 int new_c = c2 - c1 + 1; |
|
189 |
|
190 RowVector result (new_c); |
|
191 |
|
192 for (int i = 0; i < new_c; i++) |
|
193 result.elem (i) = elem (c1+i); |
|
194 |
|
195 return result; |
|
196 } |
|
197 |
|
198 // row vector by row vector -> row vector operations |
|
199 |
|
200 RowVector& |
|
201 RowVector::operator += (const RowVector& a) |
|
202 { |
|
203 int len = length (); |
|
204 if (len != a.length ()) |
|
205 { |
|
206 (*current_liboctave_error_handler) |
|
207 ("nonconformant vector += operation attempted"); |
|
208 return *this; |
|
209 } |
|
210 |
|
211 if (len == 0) |
|
212 return *this; |
|
213 |
|
214 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
215 |
|
216 add2 (d, a.data (), len); |
|
217 return *this; |
|
218 } |
|
219 |
|
220 RowVector& |
|
221 RowVector::operator -= (const RowVector& a) |
|
222 { |
|
223 int len = length (); |
|
224 if (len != a.length ()) |
|
225 { |
|
226 (*current_liboctave_error_handler) |
|
227 ("nonconformant vector -= operation attempted"); |
|
228 return *this; |
|
229 } |
|
230 |
|
231 if (len == 0) |
|
232 return *this; |
|
233 |
|
234 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
235 |
|
236 subtract2 (d, a.data (), len); |
|
237 return *this; |
|
238 } |
|
239 |
|
240 // row vector by scalar -> row vector operations |
|
241 |
|
242 ComplexRowVector |
|
243 operator + (const RowVector& v, const Complex& s) |
|
244 { |
|
245 int len = v.length (); |
|
246 return ComplexRowVector (add (v.data (), len, s), len); |
|
247 } |
|
248 |
|
249 ComplexRowVector |
|
250 operator - (const RowVector& v, const Complex& s) |
|
251 { |
|
252 int len = v.length (); |
|
253 return ComplexRowVector (subtract (v.data (), len, s), len); |
|
254 } |
|
255 |
|
256 ComplexRowVector |
|
257 operator * (const RowVector& v, const Complex& s) |
|
258 { |
|
259 int len = v.length (); |
|
260 return ComplexRowVector (multiply (v.data (), len, s), len); |
|
261 } |
|
262 |
|
263 ComplexRowVector |
|
264 operator / (const RowVector& v, const Complex& s) |
|
265 { |
|
266 int len = v.length (); |
|
267 return ComplexRowVector (divide (v.data (), len, s), len); |
|
268 } |
|
269 |
|
270 // scalar by row vector -> row vector operations |
|
271 |
|
272 ComplexRowVector |
|
273 operator + (const Complex& s, const RowVector& a) |
|
274 { |
|
275 return ComplexRowVector (); |
|
276 } |
|
277 |
|
278 ComplexRowVector |
|
279 operator - (const Complex& s, const RowVector& a) |
|
280 { |
|
281 return ComplexRowVector (); |
|
282 } |
|
283 |
|
284 ComplexRowVector |
|
285 operator * (const Complex& s, const RowVector& a) |
|
286 { |
|
287 return ComplexRowVector (); |
|
288 } |
|
289 |
|
290 ComplexRowVector |
|
291 operator / (const Complex& s, const RowVector& a) |
|
292 { |
|
293 return ComplexRowVector (); |
|
294 } |
|
295 |
|
296 // row vector by column vector -> scalar |
|
297 |
|
298 double |
|
299 operator * (const RowVector& v, const ColumnVector& a) |
|
300 { |
|
301 int len = v.length (); |
|
302 if (len != a.length ()) |
|
303 { |
|
304 (*current_liboctave_error_handler) |
|
305 ("nonconformant vector multiplication attempted"); |
|
306 return 0.0; |
|
307 } |
|
308 |
|
309 int i_one = 1; |
|
310 return F77_FCN (ddot) (&len, v.data (), &i_one, a.data (), &i_one); |
|
311 } |
|
312 |
|
313 Complex |
|
314 operator * (const RowVector& v, const ComplexColumnVector& a) |
|
315 { |
|
316 ComplexRowVector tmp (v); |
|
317 return tmp * a; |
|
318 } |
|
319 |
|
320 // row vector by matrix -> row vector |
|
321 |
|
322 RowVector |
|
323 operator * (const RowVector& v, const Matrix& a) |
|
324 { |
|
325 int len = v.length (); |
|
326 if (a.rows () != len) |
|
327 { |
|
328 (*current_liboctave_error_handler) |
|
329 ("nonconformant vector multiplication attempted"); |
|
330 return RowVector (); |
|
331 } |
|
332 |
|
333 if (len == 0 || a.cols () == 0) |
|
334 return RowVector (0); |
|
335 |
|
336 // Transpose A to form A'*x == (x'*A)' |
|
337 |
|
338 int a_nr = a.rows (); |
|
339 int a_nc = a.cols (); |
|
340 |
|
341 char trans = 'T'; |
|
342 int ld = a_nr; |
|
343 double alpha = 1.0; |
|
344 double beta = 0.0; |
|
345 int i_one = 1; |
|
346 |
|
347 double *y = new double [len]; |
|
348 |
|
349 F77_FCN (dgemv) (&trans, &a_nc, &a_nr, &alpha, a.data (), &ld, |
|
350 v.data (), &i_one, &beta, y, &i_one, 1L); |
|
351 |
|
352 return RowVector (y, len); |
|
353 } |
|
354 |
|
355 ComplexRowVector |
|
356 operator * (const RowVector& v, const ComplexMatrix& a) |
|
357 { |
|
358 ComplexRowVector tmp (v); |
|
359 return tmp * a; |
|
360 } |
|
361 |
|
362 // row vector by row vector -> row vector operations |
|
363 |
|
364 ComplexRowVector |
|
365 operator + (const RowVector& v, const ComplexRowVector& a) |
|
366 { |
|
367 int len = v.length (); |
|
368 if (len != a.length ()) |
|
369 { |
|
370 (*current_liboctave_error_handler) |
|
371 ("nonconformant vector addition attempted"); |
|
372 return ComplexRowVector (); |
|
373 } |
|
374 |
|
375 if (len == 0) |
|
376 return ComplexRowVector (0); |
|
377 |
|
378 return ComplexRowVector (add (v.data (), a.data (), len), len); |
|
379 } |
|
380 |
|
381 ComplexRowVector |
|
382 operator - (const RowVector& v, const ComplexRowVector& a) |
|
383 { |
|
384 int len = v.length (); |
|
385 if (len != a.length ()) |
|
386 { |
|
387 (*current_liboctave_error_handler) |
|
388 ("nonconformant vector subtraction attempted"); |
|
389 return ComplexRowVector (); |
|
390 } |
|
391 |
|
392 if (len == 0) |
|
393 return ComplexRowVector (0); |
|
394 |
|
395 return ComplexRowVector (subtract (v.data (), a.data (), len), len); |
|
396 } |
|
397 |
|
398 ComplexRowVector |
|
399 product (const RowVector& v, const ComplexRowVector& a) |
|
400 { |
|
401 int len = v.length (); |
|
402 if (len != a.length ()) |
|
403 { |
|
404 (*current_liboctave_error_handler) |
|
405 ("nonconformant vector product attempted"); |
|
406 return ComplexRowVector (); |
|
407 } |
|
408 |
|
409 if (len == 0) |
|
410 return ComplexRowVector (0); |
|
411 |
|
412 return ComplexRowVector (multiply (v.data (), a.data (), len), len); |
|
413 } |
|
414 |
|
415 ComplexRowVector |
|
416 quotient (const RowVector& v, const ComplexRowVector& a) |
|
417 { |
|
418 int len = v.length (); |
|
419 if (len != a.length ()) |
|
420 { |
|
421 (*current_liboctave_error_handler) |
|
422 ("nonconformant vector quotient attempted"); |
|
423 return ComplexRowVector (); |
|
424 } |
|
425 |
|
426 if (len == 0) |
|
427 return ComplexRowVector (0); |
|
428 |
|
429 return ComplexRowVector (divide (v.data (), a.data (), len), len); |
|
430 } |
|
431 |
|
432 // other operations |
|
433 |
|
434 RowVector |
|
435 map (d_d_Mapper f, const RowVector& a) |
|
436 { |
|
437 RowVector b (a); |
|
438 b.map (f); |
|
439 return b; |
|
440 } |
|
441 |
|
442 void |
|
443 RowVector::map (d_d_Mapper f) |
|
444 { |
|
445 for (int i = 0; i < length (); i++) |
|
446 elem (i) = f (elem (i)); |
|
447 } |
|
448 |
|
449 double |
|
450 RowVector::min (void) const |
|
451 { |
|
452 int len = length (); |
|
453 if (len == 0) |
|
454 return 0; |
|
455 |
|
456 double res = elem (0); |
|
457 |
|
458 for (int i = 1; i < len; i++) |
|
459 if (elem (i) < res) |
|
460 res = elem (i); |
|
461 |
|
462 return res; |
|
463 } |
|
464 |
|
465 double |
|
466 RowVector::max (void) const |
|
467 { |
|
468 int len = length (); |
|
469 if (len == 0) |
|
470 return 0; |
|
471 |
|
472 double res = elem (0); |
|
473 |
|
474 for (int i = 1; i < len; i++) |
|
475 if (elem (i) > res) |
|
476 res = elem (i); |
|
477 |
|
478 return res; |
|
479 } |
|
480 |
|
481 ostream& |
|
482 operator << (ostream& os, const RowVector& a) |
|
483 { |
|
484 // int field_width = os.precision () + 7; |
|
485 for (int i = 0; i < a.length (); i++) |
|
486 os << " " /* setw (field_width) */ << a.elem (i); |
|
487 return os; |
|
488 } |
|
489 |
|
490 istream& |
|
491 operator >> (istream& is, RowVector& a) |
|
492 { |
|
493 int len = a.length(); |
|
494 |
|
495 if (len < 1) |
|
496 is.clear (ios::badbit); |
|
497 else |
|
498 { |
|
499 double tmp; |
|
500 for (int i = 0; i < len; i++) |
|
501 { |
|
502 is >> tmp; |
|
503 if (is) |
|
504 a.elem (i) = tmp; |
|
505 else |
|
506 break; |
|
507 } |
|
508 } |
|
509 } |
|
510 |
|
511 /* |
|
512 ;;; Local Variables: *** |
|
513 ;;; mode: C++ *** |
|
514 ;;; page-delimiter: "^/\\*" *** |
|
515 ;;; End: *** |
|
516 */ |