Mercurial > hg > octave-lyh
annotate liboctave/CRowVector.cc @ 14685:4460c4fb20e6 stable rc-3-6-2-2
3.6.2-rc2 release candidate
* configure.ac (AC_INIT): Version is now 3.6.2-rc2.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 24 May 2012 15:35:50 -0400 |
parents | 72c96de7a403 |
children | 460a3c6d8bf1 |
rev | line source |
---|---|
1993 | 1 // RowVector manipulations. |
458 | 2 /* |
3 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13107
diff
changeset
|
4 Copyright (C) 1994-2012 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
458 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
458 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
458 | 26 #endif |
27 | |
3503 | 28 #include <iostream> |
458 | 29 |
4669 | 30 #include "Array-util.h" |
1847 | 31 #include "f77-fcn.h" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
32 #include "functor.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, | |
11518 | 44 const octave_idx_type&, const octave_idx_type&, |
45 const Complex&, const Complex*, | |
46 const octave_idx_type&, const Complex*, | |
47 const octave_idx_type&, const Complex&, Complex*, | |
48 const octave_idx_type& | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
49 F77_CHAR_ARG_LEN_DECL); |
5983 | 50 |
51 F77_RET_T | |
11518 | 52 F77_FUNC (xzdotu, XZDOTU) (const octave_idx_type&, const Complex*, |
53 const octave_idx_type&, const Complex*, | |
54 const octave_idx_type&, Complex&); | |
458 | 55 } |
56 | |
1360 | 57 // Complex Row Vector class |
458 | 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; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
65 return mx_inline_equal (len, data (), a.data ()); |
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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
92 xelem (c+i) = a.elem (i); |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
114 xelem (c+i) = a.elem (i); |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
130 xelem (i) = val; |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
146 xelem (i) = val; |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
170 xelem (i) = val; |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
194 xelem (i) = val; |
4316 | 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 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
225 return MArray<Complex>::hermitian (std::conj); |
458 | 226 } |
227 | |
228 ComplexColumnVector | |
229 ComplexRowVector::transpose (void) const | |
230 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
231 return MArray<Complex>::transpose (); |
458 | 232 } |
233 | |
234 ComplexRowVector | |
235 conj (const ComplexRowVector& a) | |
236 { | |
13107
353c71c76f22
maint: fix compilation problem with g++ -std=c++0x option
Júlio Hoffimann <julio.hoffimann@gmail.com>
parents:
11523
diff
changeset
|
237 return do_mx_unary_map<Complex, Complex, std::conj<double> > (a); |
458 | 238 } |
239 | |
240 // resize is the destructive equivalent for this one | |
241 | |
242 ComplexRowVector | |
5275 | 243 ComplexRowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 244 { |
5275 | 245 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 246 |
5275 | 247 octave_idx_type new_c = c2 - c1 + 1; |
458 | 248 |
249 ComplexRowVector result (new_c); | |
250 | |
5275 | 251 for (octave_idx_type i = 0; i < new_c; i++) |
458 | 252 result.elem (i) = elem (c1+i); |
253 | |
254 return result; | |
255 } | |
256 | |
4316 | 257 ComplexRowVector |
5275 | 258 ComplexRowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 259 { |
260 ComplexRowVector result (n); | |
261 | |
5275 | 262 for (octave_idx_type i = 0; i < n; i++) |
4316 | 263 result.elem (i) = elem (r1+i); |
264 | |
265 return result; | |
266 } | |
267 | |
458 | 268 // row vector by row vector -> row vector operations |
269 | |
270 ComplexRowVector& | |
271 ComplexRowVector::operator += (const RowVector& a) | |
272 { | |
5275 | 273 octave_idx_type len = length (); |
2386 | 274 |
5275 | 275 octave_idx_type a_len = a.length (); |
2386 | 276 |
277 if (len != a_len) | |
458 | 278 { |
2386 | 279 gripe_nonconformant ("operator +=", len, a_len); |
458 | 280 return *this; |
281 } | |
282 | |
283 if (len == 0) | |
284 return *this; | |
285 | |
286 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
287 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
288 mx_inline_add2 (len, d, a.data ()); |
458 | 289 return *this; |
290 } | |
291 | |
292 ComplexRowVector& | |
293 ComplexRowVector::operator -= (const RowVector& a) | |
294 { | |
5275 | 295 octave_idx_type len = length (); |
2386 | 296 |
5275 | 297 octave_idx_type a_len = a.length (); |
2386 | 298 |
299 if (len != a_len) | |
458 | 300 { |
2386 | 301 gripe_nonconformant ("operator -=", len, a_len); |
458 | 302 return *this; |
303 } | |
304 | |
305 if (len == 0) | |
306 return *this; | |
307 | |
308 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
309 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
310 mx_inline_sub2 (len, d, a.data ()); |
458 | 311 return *this; |
312 } | |
313 | |
314 // row vector by matrix -> row vector | |
315 | |
316 ComplexRowVector | |
317 operator * (const ComplexRowVector& v, const ComplexMatrix& a) | |
318 { | |
1947 | 319 ComplexRowVector retval; |
320 | |
5275 | 321 octave_idx_type len = v.length (); |
1947 | 322 |
5275 | 323 octave_idx_type a_nr = a.rows (); |
324 octave_idx_type a_nc = a.cols (); | |
2386 | 325 |
326 if (a_nr != len) | |
327 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 328 else |
458 | 329 { |
1947 | 330 if (len == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
331 retval.resize (a_nc, 0.0); |
1947 | 332 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
333 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 // Transpose A to form A'*x == (x'*A)' |
1947 | 335 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
336 octave_idx_type ld = a_nr; |
1947 | 337 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
338 retval.resize (a_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
339 Complex *y = retval.fortran_vec (); |
1947 | 340 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
341 F77_XFCN (zgemv, ZGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
342 a_nr, a_nc, 1.0, a.data (), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
343 ld, v.data (), 1, 0.0, y, 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
344 F77_CHAR_ARG_LEN (1))); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
345 } |
458 | 346 } |
347 | |
1947 | 348 return retval; |
458 | 349 } |
350 | |
1205 | 351 ComplexRowVector |
352 operator * (const RowVector& v, const ComplexMatrix& a) | |
353 { | |
354 ComplexRowVector tmp (v); | |
355 return tmp * a; | |
356 } | |
357 | |
458 | 358 // other operations |
359 | |
360 Complex | |
361 ComplexRowVector::min (void) const | |
362 { | |
5275 | 363 octave_idx_type len = length (); |
458 | 364 if (len == 0) |
365 return Complex (0.0); | |
366 | |
367 Complex res = elem (0); | |
5260 | 368 double absres = std::abs (res); |
458 | 369 |
5275 | 370 for (octave_idx_type i = 1; i < len; i++) |
5260 | 371 if (std::abs (elem (i)) < absres) |
458 | 372 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 res = elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 absres = std::abs (res); |
458 | 375 } |
376 | |
377 return res; | |
378 } | |
379 | |
380 Complex | |
381 ComplexRowVector::max (void) const | |
382 { | |
5275 | 383 octave_idx_type len = length (); |
458 | 384 if (len == 0) |
385 return Complex (0.0); | |
386 | |
387 Complex res = elem (0); | |
5260 | 388 double absres = std::abs (res); |
458 | 389 |
5275 | 390 for (octave_idx_type i = 1; i < len; i++) |
5260 | 391 if (std::abs (elem (i)) > absres) |
458 | 392 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
393 res = elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
394 absres = std::abs (res); |
458 | 395 } |
396 | |
397 return res; | |
398 } | |
399 | |
400 // i/o | |
401 | |
3504 | 402 std::ostream& |
403 operator << (std::ostream& os, const ComplexRowVector& a) | |
458 | 404 { |
405 // int field_width = os.precision () + 7; | |
5275 | 406 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 407 os << " " /* setw (field_width) */ << a.elem (i); |
408 return os; | |
409 } | |
410 | |
3504 | 411 std::istream& |
412 operator >> (std::istream& is, ComplexRowVector& a) | |
458 | 413 { |
5275 | 414 octave_idx_type len = a.length(); |
458 | 415 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
416 if (len > 0) |
458 | 417 { |
418 Complex tmp; | |
5275 | 419 for (octave_idx_type i = 0; i < len; i++) |
458 | 420 { |
421 is >> tmp; | |
422 if (is) | |
423 a.elem (i) = tmp; | |
424 else | |
425 break; | |
426 } | |
427 } | |
532 | 428 return is; |
458 | 429 } |
430 | |
1205 | 431 // row vector by column vector -> scalar |
432 | |
433 // row vector by column vector -> scalar | |
434 | |
435 Complex | |
436 operator * (const ComplexRowVector& v, const ColumnVector& a) | |
437 { | |
438 ComplexColumnVector tmp (a); | |
439 return v * tmp; | |
440 } | |
441 | |
442 Complex | |
443 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) | |
444 { | |
5983 | 445 Complex retval (0.0, 0.0); |
446 | |
5275 | 447 octave_idx_type len = v.length (); |
2386 | 448 |
5275 | 449 octave_idx_type a_len = a.length (); |
2386 | 450 |
451 if (len != a_len) | |
5983 | 452 gripe_nonconformant ("operator *", len, a_len); |
453 else if (len != 0) | |
454 F77_FUNC (xzdotu, XZDOTU) (len, v.data (), 1, a.data (), 1, retval); | |
1205 | 455 |
456 return retval; | |
457 } | |
458 | |
459 // other operations | |
460 | |
461 ComplexRowVector | |
5275 | 462 linspace (const Complex& x1, const Complex& x2, octave_idx_type n) |
1205 | 463 { |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
464 if (n < 1) n = 1; |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
465 |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
466 NoAlias<ComplexRowVector> retval (n); |
1205 | 467 |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
468 Complex delta = (x2 - x1) / (n - 1.0); |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
469 retval(0) = x1; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
470 for (octave_idx_type i = 1; i < n-1; i++) |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
471 retval(i) = x1 + static_cast<double> (i)*delta; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
472 retval(n-1) = x2; |
1205 | 473 |
474 return retval; | |
475 } |