Mercurial > hg > octave-lyh
annotate liboctave/CRowVector.cc @ 7875:bff8dbc1be11
mlock: doc fix
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 06 Jun 2008 11:35:10 -0400 |
parents | 82be108cc558 |
children | eb63fbe60fab |
rev | line source |
---|---|
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" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
33 #include "functor.h" |
1368 | 34 #include "lo-error.h" |
458 | 35 #include "mx-base.h" |
36 #include "mx-inlines.cc" | |
1650 | 37 #include "oct-cmplx.h" |
458 | 38 |
39 // Fortran functions we call. | |
40 | |
41 extern "C" | |
42 { | |
4552 | 43 F77_RET_T |
44 F77_FUNC (zgemv, ZGEMV) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 45 const octave_idx_type&, const octave_idx_type&, const Complex&, |
46 const Complex*, const octave_idx_type&, const Complex*, | |
47 const octave_idx_type&, const Complex&, Complex*, const octave_idx_type& | |
4552 | 48 F77_CHAR_ARG_LEN_DECL); |
5983 | 49 |
50 F77_RET_T | |
51 F77_FUNC (xzdotu, XZDOTU) (const octave_idx_type&, const Complex*, const octave_idx_type&, | |
52 const Complex*, const octave_idx_type&, Complex&); | |
458 | 53 } |
54 | |
1360 | 55 // Complex Row Vector class |
458 | 56 |
57 ComplexRowVector::ComplexRowVector (const RowVector& a) | |
1214 | 58 : MArray<Complex> (a.length ()) |
458 | 59 { |
5275 | 60 for (octave_idx_type i = 0; i < length (); i++) |
458 | 61 elem (i) = a.elem (i); |
62 } | |
63 | |
2386 | 64 bool |
458 | 65 ComplexRowVector::operator == (const ComplexRowVector& a) const |
66 { | |
5275 | 67 octave_idx_type len = length (); |
458 | 68 if (len != a.length ()) |
69 return 0; | |
3769 | 70 return mx_inline_equal (data (), a.data (), len); |
458 | 71 } |
72 | |
2386 | 73 bool |
458 | 74 ComplexRowVector::operator != (const ComplexRowVector& a) const |
75 { | |
76 return !(*this == a); | |
77 } | |
78 | |
79 // destructive insert/delete/reorder operations | |
80 | |
81 ComplexRowVector& | |
5275 | 82 ComplexRowVector::insert (const RowVector& a, octave_idx_type c) |
458 | 83 { |
5275 | 84 octave_idx_type a_len = a.length (); |
4316 | 85 |
1699 | 86 if (c < 0 || c + a_len > length ()) |
458 | 87 { |
88 (*current_liboctave_error_handler) ("range error for insert"); | |
89 return *this; | |
90 } | |
91 | |
4316 | 92 if (a_len > 0) |
93 { | |
94 make_unique (); | |
95 | |
5275 | 96 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 97 xelem (c+i) = a.elem (i); |
98 } | |
458 | 99 |
100 return *this; | |
101 } | |
102 | |
103 ComplexRowVector& | |
5275 | 104 ComplexRowVector::insert (const ComplexRowVector& a, octave_idx_type c) |
458 | 105 { |
5275 | 106 octave_idx_type a_len = a.length (); |
4316 | 107 |
1699 | 108 if (c < 0 || c + a_len > length ()) |
458 | 109 { |
110 (*current_liboctave_error_handler) ("range error for insert"); | |
111 return *this; | |
112 } | |
113 | |
4316 | 114 if (a_len > 0) |
115 { | |
116 make_unique (); | |
117 | |
5275 | 118 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 119 xelem (c+i) = a.elem (i); |
120 } | |
458 | 121 |
122 return *this; | |
123 } | |
124 | |
125 ComplexRowVector& | |
126 ComplexRowVector::fill (double val) | |
127 { | |
5275 | 128 octave_idx_type len = length (); |
4316 | 129 |
458 | 130 if (len > 0) |
4316 | 131 { |
132 make_unique (); | |
133 | |
5275 | 134 for (octave_idx_type i = 0; i < len; i++) |
4316 | 135 xelem (i) = val; |
136 } | |
137 | |
458 | 138 return *this; |
139 } | |
140 | |
141 ComplexRowVector& | |
142 ComplexRowVector::fill (const Complex& val) | |
143 { | |
5275 | 144 octave_idx_type len = length (); |
4316 | 145 |
458 | 146 if (len > 0) |
4316 | 147 { |
148 make_unique (); | |
149 | |
5275 | 150 for (octave_idx_type i = 0; i < len; i++) |
4316 | 151 xelem (i) = val; |
152 } | |
153 | |
458 | 154 return *this; |
155 } | |
156 | |
157 ComplexRowVector& | |
5275 | 158 ComplexRowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458 | 159 { |
5275 | 160 octave_idx_type len = length (); |
4316 | 161 |
458 | 162 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
163 { | |
164 (*current_liboctave_error_handler) ("range error for fill"); | |
165 return *this; | |
166 } | |
167 | |
5275 | 168 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 169 |
4316 | 170 if (c2 >= c1) |
171 { | |
172 make_unique (); | |
173 | |
5275 | 174 for (octave_idx_type i = c1; i <= c2; i++) |
4316 | 175 xelem (i) = val; |
176 } | |
458 | 177 |
178 return *this; | |
179 } | |
180 | |
181 ComplexRowVector& | |
5275 | 182 ComplexRowVector::fill (const Complex& val, octave_idx_type c1, octave_idx_type c2) |
458 | 183 { |
5275 | 184 octave_idx_type len = length (); |
4316 | 185 |
458 | 186 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
187 { | |
188 (*current_liboctave_error_handler) ("range error for fill"); | |
189 return *this; | |
190 } | |
191 | |
5275 | 192 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 193 |
4316 | 194 if (c2 >= c1) |
195 { | |
196 make_unique (); | |
197 | |
5275 | 198 for (octave_idx_type i = c1; i <= c2; i++) |
4316 | 199 xelem (i) = val; |
200 } | |
458 | 201 |
202 return *this; | |
203 } | |
204 | |
205 ComplexRowVector | |
206 ComplexRowVector::append (const RowVector& a) const | |
207 { | |
5275 | 208 octave_idx_type len = length (); |
209 octave_idx_type nc_insert = len; | |
458 | 210 ComplexRowVector retval (len + a.length ()); |
211 retval.insert (*this, 0); | |
212 retval.insert (a, nc_insert); | |
213 return retval; | |
214 } | |
215 | |
216 ComplexRowVector | |
217 ComplexRowVector::append (const ComplexRowVector& a) const | |
218 { | |
5275 | 219 octave_idx_type len = length (); |
220 octave_idx_type nc_insert = len; | |
458 | 221 ComplexRowVector retval (len + a.length ()); |
222 retval.insert (*this, 0); | |
223 retval.insert (a, nc_insert); | |
224 return retval; | |
225 } | |
226 | |
227 ComplexColumnVector | |
228 ComplexRowVector::hermitian (void) const | |
229 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
230 return MArray<Complex>::hermitian (std::conj); |
458 | 231 } |
232 | |
233 ComplexColumnVector | |
234 ComplexRowVector::transpose (void) const | |
235 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
236 return MArray<Complex>::transpose (); |
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 } |
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 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
369 RowVector |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
370 ComplexRowVector::map (dmapper fcn) const |
458 | 371 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
372 return MArray<Complex>::map<double> (func_ptr (fcn)); |
458 | 373 } |
374 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
375 ComplexRowVector |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
376 ComplexRowVector::map (cmapper fcn) const |
458 | 377 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
378 return MArray<Complex>::map<Complex> (func_ptr (fcn)); |
458 | 379 } |
380 | |
381 Complex | |
382 ComplexRowVector::min (void) const | |
383 { | |
5275 | 384 octave_idx_type len = length (); |
458 | 385 if (len == 0) |
386 return Complex (0.0); | |
387 | |
388 Complex res = elem (0); | |
5260 | 389 double absres = std::abs (res); |
458 | 390 |
5275 | 391 for (octave_idx_type i = 1; i < len; i++) |
5260 | 392 if (std::abs (elem (i)) < absres) |
458 | 393 { |
394 res = elem (i); | |
5260 | 395 absres = std::abs (res); |
458 | 396 } |
397 | |
398 return res; | |
399 } | |
400 | |
401 Complex | |
402 ComplexRowVector::max (void) const | |
403 { | |
5275 | 404 octave_idx_type len = length (); |
458 | 405 if (len == 0) |
406 return Complex (0.0); | |
407 | |
408 Complex res = elem (0); | |
5260 | 409 double absres = std::abs (res); |
458 | 410 |
5275 | 411 for (octave_idx_type i = 1; i < len; i++) |
5260 | 412 if (std::abs (elem (i)) > absres) |
458 | 413 { |
414 res = elem (i); | |
5260 | 415 absres = std::abs (res); |
458 | 416 } |
417 | |
418 return res; | |
419 } | |
420 | |
421 // i/o | |
422 | |
3504 | 423 std::ostream& |
424 operator << (std::ostream& os, const ComplexRowVector& a) | |
458 | 425 { |
426 // int field_width = os.precision () + 7; | |
5275 | 427 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 428 os << " " /* setw (field_width) */ << a.elem (i); |
429 return os; | |
430 } | |
431 | |
3504 | 432 std::istream& |
433 operator >> (std::istream& is, ComplexRowVector& a) | |
458 | 434 { |
5275 | 435 octave_idx_type len = a.length(); |
458 | 436 |
437 if (len < 1) | |
3504 | 438 is.clear (std::ios::badbit); |
458 | 439 else |
440 { | |
441 Complex tmp; | |
5275 | 442 for (octave_idx_type i = 0; i < len; i++) |
458 | 443 { |
444 is >> tmp; | |
445 if (is) | |
446 a.elem (i) = tmp; | |
447 else | |
448 break; | |
449 } | |
450 } | |
532 | 451 return is; |
458 | 452 } |
453 | |
1205 | 454 // row vector by column vector -> scalar |
455 | |
456 // row vector by column vector -> scalar | |
457 | |
458 Complex | |
459 operator * (const ComplexRowVector& v, const ColumnVector& a) | |
460 { | |
461 ComplexColumnVector tmp (a); | |
462 return v * tmp; | |
463 } | |
464 | |
465 Complex | |
466 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) | |
467 { | |
5983 | 468 Complex retval (0.0, 0.0); |
469 | |
5275 | 470 octave_idx_type len = v.length (); |
2386 | 471 |
5275 | 472 octave_idx_type a_len = a.length (); |
2386 | 473 |
474 if (len != a_len) | |
5983 | 475 gripe_nonconformant ("operator *", len, a_len); |
476 else if (len != 0) | |
477 F77_FUNC (xzdotu, XZDOTU) (len, v.data (), 1, a.data (), 1, retval); | |
1205 | 478 |
479 return retval; | |
480 } | |
481 | |
482 // other operations | |
483 | |
484 ComplexRowVector | |
5275 | 485 linspace (const Complex& x1, const Complex& x2, octave_idx_type n) |
1205 | 486 { |
487 ComplexRowVector retval; | |
488 | |
489 if (n > 0) | |
490 { | |
491 retval.resize (n); | |
3092 | 492 Complex delta = (x2 - x1) / (n - 1.0); |
1205 | 493 retval.elem (0) = x1; |
5275 | 494 for (octave_idx_type i = 1; i < n-1; i++) |
3092 | 495 retval.elem (i) = x1 + 1.0 * i * delta; |
1205 | 496 retval.elem (n-1) = x2; |
497 } | |
6630 | 498 else |
3322 | 499 { |
6629 | 500 retval.resize (1); |
501 retval.elem (0) = x2; | |
3322 | 502 } |
1205 | 503 |
504 return retval; | |
505 } | |
506 | |
458 | 507 /* |
508 ;;; Local Variables: *** | |
509 ;;; mode: C++ *** | |
510 ;;; End: *** | |
511 */ |