Mercurial > hg > octave-lyh
annotate liboctave/CRowVector.cc @ 10493:2f8bacc2a57d
fix order of func defs in Sparse.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 07 Apr 2010 12:07:06 +0200 |
parents | a0728e81ed25 |
children | 141b3fb5cef7 |
rev | line source |
---|---|
1993 | 1 // RowVector manipulations. |
458 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, |
8920 | 5 2004, 2005, 2006, 2007, 2008 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, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
45 const octave_idx_type&, const octave_idx_type&, const Complex&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
46 const Complex*, const octave_idx_type&, const Complex*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 const octave_idx_type&, const Complex&, Complex*, const octave_idx_type& |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
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&, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
52 const Complex*, const octave_idx_type&, Complex&); |
458 | 53 } |
54 | |
1360 | 55 // Complex Row Vector class |
458 | 56 |
2386 | 57 bool |
458 | 58 ComplexRowVector::operator == (const ComplexRowVector& a) const |
59 { | |
5275 | 60 octave_idx_type len = length (); |
458 | 61 if (len != a.length ()) |
62 return 0; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
63 return mx_inline_equal (len, data (), a.data ()); |
458 | 64 } |
65 | |
2386 | 66 bool |
458 | 67 ComplexRowVector::operator != (const ComplexRowVector& a) const |
68 { | |
69 return !(*this == a); | |
70 } | |
71 | |
72 // destructive insert/delete/reorder operations | |
73 | |
74 ComplexRowVector& | |
5275 | 75 ComplexRowVector::insert (const RowVector& a, octave_idx_type c) |
458 | 76 { |
5275 | 77 octave_idx_type a_len = a.length (); |
4316 | 78 |
1699 | 79 if (c < 0 || c + a_len > length ()) |
458 | 80 { |
81 (*current_liboctave_error_handler) ("range error for insert"); | |
82 return *this; | |
83 } | |
84 | |
4316 | 85 if (a_len > 0) |
86 { | |
87 make_unique (); | |
88 | |
5275 | 89 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
|
90 xelem (c+i) = a.elem (i); |
4316 | 91 } |
458 | 92 |
93 return *this; | |
94 } | |
95 | |
96 ComplexRowVector& | |
5275 | 97 ComplexRowVector::insert (const ComplexRowVector& a, octave_idx_type c) |
458 | 98 { |
5275 | 99 octave_idx_type a_len = a.length (); |
4316 | 100 |
1699 | 101 if (c < 0 || c + a_len > length ()) |
458 | 102 { |
103 (*current_liboctave_error_handler) ("range error for insert"); | |
104 return *this; | |
105 } | |
106 | |
4316 | 107 if (a_len > 0) |
108 { | |
109 make_unique (); | |
110 | |
5275 | 111 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
|
112 xelem (c+i) = a.elem (i); |
4316 | 113 } |
458 | 114 |
115 return *this; | |
116 } | |
117 | |
118 ComplexRowVector& | |
119 ComplexRowVector::fill (double val) | |
120 { | |
5275 | 121 octave_idx_type len = length (); |
4316 | 122 |
458 | 123 if (len > 0) |
4316 | 124 { |
125 make_unique (); | |
126 | |
5275 | 127 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
|
128 xelem (i) = val; |
4316 | 129 } |
130 | |
458 | 131 return *this; |
132 } | |
133 | |
134 ComplexRowVector& | |
135 ComplexRowVector::fill (const Complex& val) | |
136 { | |
5275 | 137 octave_idx_type len = length (); |
4316 | 138 |
458 | 139 if (len > 0) |
4316 | 140 { |
141 make_unique (); | |
142 | |
5275 | 143 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
|
144 xelem (i) = val; |
4316 | 145 } |
146 | |
458 | 147 return *this; |
148 } | |
149 | |
150 ComplexRowVector& | |
5275 | 151 ComplexRowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458 | 152 { |
5275 | 153 octave_idx_type len = length (); |
4316 | 154 |
458 | 155 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
156 { | |
157 (*current_liboctave_error_handler) ("range error for fill"); | |
158 return *this; | |
159 } | |
160 | |
5275 | 161 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 162 |
4316 | 163 if (c2 >= c1) |
164 { | |
165 make_unique (); | |
166 | |
5275 | 167 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
|
168 xelem (i) = val; |
4316 | 169 } |
458 | 170 |
171 return *this; | |
172 } | |
173 | |
174 ComplexRowVector& | |
5275 | 175 ComplexRowVector::fill (const Complex& val, octave_idx_type c1, octave_idx_type c2) |
458 | 176 { |
5275 | 177 octave_idx_type len = length (); |
4316 | 178 |
458 | 179 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
180 { | |
181 (*current_liboctave_error_handler) ("range error for fill"); | |
182 return *this; | |
183 } | |
184 | |
5275 | 185 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 186 |
4316 | 187 if (c2 >= c1) |
188 { | |
189 make_unique (); | |
190 | |
5275 | 191 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
|
192 xelem (i) = val; |
4316 | 193 } |
458 | 194 |
195 return *this; | |
196 } | |
197 | |
198 ComplexRowVector | |
199 ComplexRowVector::append (const RowVector& a) const | |
200 { | |
5275 | 201 octave_idx_type len = length (); |
202 octave_idx_type nc_insert = len; | |
458 | 203 ComplexRowVector retval (len + a.length ()); |
204 retval.insert (*this, 0); | |
205 retval.insert (a, nc_insert); | |
206 return retval; | |
207 } | |
208 | |
209 ComplexRowVector | |
210 ComplexRowVector::append (const ComplexRowVector& a) const | |
211 { | |
5275 | 212 octave_idx_type len = length (); |
213 octave_idx_type nc_insert = len; | |
458 | 214 ComplexRowVector retval (len + a.length ()); |
215 retval.insert (*this, 0); | |
216 retval.insert (a, nc_insert); | |
217 return retval; | |
218 } | |
219 | |
220 ComplexColumnVector | |
221 ComplexRowVector::hermitian (void) const | |
222 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
223 return MArray<Complex>::hermitian (std::conj); |
458 | 224 } |
225 | |
226 ComplexColumnVector | |
227 ComplexRowVector::transpose (void) const | |
228 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
229 return MArray<Complex>::transpose (); |
458 | 230 } |
231 | |
232 ComplexRowVector | |
233 conj (const ComplexRowVector& a) | |
234 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
235 return do_mx_unary_map<Complex, Complex, std::conj> (a); |
458 | 236 } |
237 | |
238 // resize is the destructive equivalent for this one | |
239 | |
240 ComplexRowVector | |
5275 | 241 ComplexRowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 242 { |
5275 | 243 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 244 |
5275 | 245 octave_idx_type new_c = c2 - c1 + 1; |
458 | 246 |
247 ComplexRowVector result (new_c); | |
248 | |
5275 | 249 for (octave_idx_type i = 0; i < new_c; i++) |
458 | 250 result.elem (i) = elem (c1+i); |
251 | |
252 return result; | |
253 } | |
254 | |
4316 | 255 ComplexRowVector |
5275 | 256 ComplexRowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 257 { |
258 ComplexRowVector result (n); | |
259 | |
5275 | 260 for (octave_idx_type i = 0; i < n; i++) |
4316 | 261 result.elem (i) = elem (r1+i); |
262 | |
263 return result; | |
264 } | |
265 | |
458 | 266 // row vector by row vector -> row vector operations |
267 | |
268 ComplexRowVector& | |
269 ComplexRowVector::operator += (const RowVector& a) | |
270 { | |
5275 | 271 octave_idx_type len = length (); |
2386 | 272 |
5275 | 273 octave_idx_type a_len = a.length (); |
2386 | 274 |
275 if (len != a_len) | |
458 | 276 { |
2386 | 277 gripe_nonconformant ("operator +=", len, a_len); |
458 | 278 return *this; |
279 } | |
280 | |
281 if (len == 0) | |
282 return *this; | |
283 | |
284 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
285 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
286 mx_inline_add2 (len, d, a.data ()); |
458 | 287 return *this; |
288 } | |
289 | |
290 ComplexRowVector& | |
291 ComplexRowVector::operator -= (const RowVector& a) | |
292 { | |
5275 | 293 octave_idx_type len = length (); |
2386 | 294 |
5275 | 295 octave_idx_type a_len = a.length (); |
2386 | 296 |
297 if (len != a_len) | |
458 | 298 { |
2386 | 299 gripe_nonconformant ("operator -=", len, a_len); |
458 | 300 return *this; |
301 } | |
302 | |
303 if (len == 0) | |
304 return *this; | |
305 | |
306 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
307 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
308 mx_inline_sub2 (len, d, a.data ()); |
458 | 309 return *this; |
310 } | |
311 | |
312 // row vector by matrix -> row vector | |
313 | |
314 ComplexRowVector | |
315 operator * (const ComplexRowVector& v, const ComplexMatrix& a) | |
316 { | |
1947 | 317 ComplexRowVector retval; |
318 | |
5275 | 319 octave_idx_type len = v.length (); |
1947 | 320 |
5275 | 321 octave_idx_type a_nr = a.rows (); |
322 octave_idx_type a_nc = a.cols (); | |
2386 | 323 |
324 if (a_nr != len) | |
325 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 326 else |
458 | 327 { |
1947 | 328 if (len == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
329 retval.resize (a_nc, 0.0); |
1947 | 330 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
331 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
332 // Transpose A to form A'*x == (x'*A)' |
1947 | 333 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 octave_idx_type ld = a_nr; |
1947 | 335 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
336 retval.resize (a_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
337 Complex *y = retval.fortran_vec (); |
1947 | 338 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
339 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
|
340 a_nr, a_nc, 1.0, a.data (), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
341 ld, v.data (), 1, 0.0, y, 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
342 F77_CHAR_ARG_LEN (1))); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
343 } |
458 | 344 } |
345 | |
1947 | 346 return retval; |
458 | 347 } |
348 | |
1205 | 349 ComplexRowVector |
350 operator * (const RowVector& v, const ComplexMatrix& a) | |
351 { | |
352 ComplexRowVector tmp (v); | |
353 return tmp * a; | |
354 } | |
355 | |
458 | 356 // other operations |
357 | |
358 Complex | |
359 ComplexRowVector::min (void) const | |
360 { | |
5275 | 361 octave_idx_type len = length (); |
458 | 362 if (len == 0) |
363 return Complex (0.0); | |
364 | |
365 Complex res = elem (0); | |
5260 | 366 double absres = std::abs (res); |
458 | 367 |
5275 | 368 for (octave_idx_type i = 1; i < len; i++) |
5260 | 369 if (std::abs (elem (i)) < absres) |
458 | 370 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
371 res = elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
372 absres = std::abs (res); |
458 | 373 } |
374 | |
375 return res; | |
376 } | |
377 | |
378 Complex | |
379 ComplexRowVector::max (void) const | |
380 { | |
5275 | 381 octave_idx_type len = length (); |
458 | 382 if (len == 0) |
383 return Complex (0.0); | |
384 | |
385 Complex res = elem (0); | |
5260 | 386 double absres = std::abs (res); |
458 | 387 |
5275 | 388 for (octave_idx_type i = 1; i < len; i++) |
5260 | 389 if (std::abs (elem (i)) > absres) |
458 | 390 { |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
391 res = elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
392 absres = std::abs (res); |
458 | 393 } |
394 | |
395 return res; | |
396 } | |
397 | |
398 // i/o | |
399 | |
3504 | 400 std::ostream& |
401 operator << (std::ostream& os, const ComplexRowVector& a) | |
458 | 402 { |
403 // int field_width = os.precision () + 7; | |
5275 | 404 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 405 os << " " /* setw (field_width) */ << a.elem (i); |
406 return os; | |
407 } | |
408 | |
3504 | 409 std::istream& |
410 operator >> (std::istream& is, ComplexRowVector& a) | |
458 | 411 { |
5275 | 412 octave_idx_type len = a.length(); |
458 | 413 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
414 if (len > 0) |
458 | 415 { |
416 Complex tmp; | |
5275 | 417 for (octave_idx_type i = 0; i < len; i++) |
458 | 418 { |
419 is >> tmp; | |
420 if (is) | |
421 a.elem (i) = tmp; | |
422 else | |
423 break; | |
424 } | |
425 } | |
532 | 426 return is; |
458 | 427 } |
428 | |
1205 | 429 // row vector by column vector -> scalar |
430 | |
431 // row vector by column vector -> scalar | |
432 | |
433 Complex | |
434 operator * (const ComplexRowVector& v, const ColumnVector& a) | |
435 { | |
436 ComplexColumnVector tmp (a); | |
437 return v * tmp; | |
438 } | |
439 | |
440 Complex | |
441 operator * (const ComplexRowVector& v, const ComplexColumnVector& a) | |
442 { | |
5983 | 443 Complex retval (0.0, 0.0); |
444 | |
5275 | 445 octave_idx_type len = v.length (); |
2386 | 446 |
5275 | 447 octave_idx_type a_len = a.length (); |
2386 | 448 |
449 if (len != a_len) | |
5983 | 450 gripe_nonconformant ("operator *", len, a_len); |
451 else if (len != 0) | |
452 F77_FUNC (xzdotu, XZDOTU) (len, v.data (), 1, a.data (), 1, retval); | |
1205 | 453 |
454 return retval; | |
455 } | |
456 | |
457 // other operations | |
458 | |
459 ComplexRowVector | |
5275 | 460 linspace (const Complex& x1, const Complex& x2, octave_idx_type n) |
1205 | 461 { |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
462 if (n < 1) n = 1; |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
463 |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
464 NoAlias<ComplexRowVector> retval (n); |
1205 | 465 |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
466 Complex delta = (x2 - x1) / (n - 1.0); |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
467 retval(0) = x1; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
468 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
|
469 retval(i) = x1 + static_cast<double> (i)*delta; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
470 retval(n-1) = x2; |
1205 | 471 |
472 return retval; | |
473 } |