Mercurial > hg > octave-lyh
annotate liboctave/dRowVector.cc @ 11518:141b3fb5cef7
style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 13 Jan 2011 16:52:30 -0500 |
parents | a0728e81ed25 |
children | fd0a3ac60b0e |
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 (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL, | |
11518 | 45 const octave_idx_type&, const octave_idx_type&, |
46 const double&, const double*, | |
47 const octave_idx_type&, const double*, | |
48 const octave_idx_type&, const double&, | |
49 double*, const octave_idx_type& | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
50 F77_CHAR_ARG_LEN_DECL); |
5983 | 51 F77_RET_T |
11518 | 52 F77_FUNC (xddot, XDDOT) (const octave_idx_type&, const double*, |
53 const octave_idx_type&, const double*, | |
54 const octave_idx_type&, double&); | |
458 | 55 } |
56 | |
1360 | 57 // Row Vector class. |
458 | 58 |
2386 | 59 bool |
458 | 60 RowVector::operator == (const RowVector& 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 RowVector::operator != (const RowVector& a) const |
70 { | |
71 return !(*this == a); | |
72 } | |
73 | |
74 RowVector& | |
5275 | 75 RowVector::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 RowVector& | |
97 RowVector::fill (double val) | |
98 { | |
5275 | 99 octave_idx_type len = length (); |
4316 | 100 |
458 | 101 if (len > 0) |
4316 | 102 { |
103 make_unique (); | |
104 | |
5275 | 105 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
|
106 xelem (i) = val; |
4316 | 107 } |
108 | |
458 | 109 return *this; |
110 } | |
111 | |
112 RowVector& | |
5275 | 113 RowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458 | 114 { |
5275 | 115 octave_idx_type len = length (); |
4316 | 116 |
458 | 117 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
118 { | |
119 (*current_liboctave_error_handler) ("range error for fill"); | |
120 return *this; | |
121 } | |
122 | |
5275 | 123 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 124 |
4316 | 125 if (c2 >= c1) |
126 { | |
127 make_unique (); | |
128 | |
5275 | 129 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
|
130 xelem (i) = val; |
4316 | 131 } |
458 | 132 |
133 return *this; | |
134 } | |
135 | |
136 RowVector | |
137 RowVector::append (const RowVector& a) const | |
138 { | |
5275 | 139 octave_idx_type len = length (); |
140 octave_idx_type nc_insert = len; | |
458 | 141 RowVector retval (len + a.length ()); |
142 retval.insert (*this, 0); | |
143 retval.insert (a, nc_insert); | |
144 return retval; | |
145 } | |
146 | |
147 ColumnVector | |
148 RowVector::transpose (void) const | |
149 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
150 return MArray<double>::transpose(); |
458 | 151 } |
152 | |
153 RowVector | |
1205 | 154 real (const ComplexRowVector& a) |
155 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
156 return do_mx_unary_op<double, Complex> (a, mx_inline_real); |
1205 | 157 } |
158 | |
159 RowVector | |
160 imag (const ComplexRowVector& a) | |
161 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
162 return do_mx_unary_op<double, Complex> (a, mx_inline_imag); |
1205 | 163 } |
164 | |
165 RowVector | |
5275 | 166 RowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 167 { |
5275 | 168 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 169 |
5275 | 170 octave_idx_type new_c = c2 - c1 + 1; |
458 | 171 |
172 RowVector result (new_c); | |
173 | |
5275 | 174 for (octave_idx_type i = 0; i < new_c; i++) |
4316 | 175 result.xelem (i) = elem (c1+i); |
176 | |
177 return result; | |
178 } | |
179 | |
180 RowVector | |
5275 | 181 RowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 182 { |
183 RowVector result (n); | |
184 | |
5275 | 185 for (octave_idx_type i = 0; i < n; i++) |
4316 | 186 result.xelem (i) = elem (r1+i); |
458 | 187 |
188 return result; | |
189 } | |
190 | |
191 // row vector by matrix -> row vector | |
192 | |
193 RowVector | |
194 operator * (const RowVector& v, const Matrix& a) | |
195 { | |
1947 | 196 RowVector retval; |
197 | |
5275 | 198 octave_idx_type len = v.length (); |
1947 | 199 |
5275 | 200 octave_idx_type a_nr = a.rows (); |
201 octave_idx_type a_nc = a.cols (); | |
2386 | 202 |
203 if (a_nr != len) | |
204 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 205 else |
458 | 206 { |
1947 | 207 if (len == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
208 retval.resize (a_nc, 0.0); |
1947 | 209 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
210 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
211 // Transpose A to form A'*x == (x'*A)' |
1947 | 212 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
213 octave_idx_type ld = a_nr; |
1947 | 214 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
215 retval.resize (a_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
216 double *y = retval.fortran_vec (); |
1947 | 217 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
218 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
219 a_nr, a_nc, 1.0, a.data (), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
220 ld, v.data (), 1, 0.0, y, 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
221 F77_CHAR_ARG_LEN (1))); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
222 } |
458 | 223 } |
224 | |
1947 | 225 return retval; |
458 | 226 } |
227 | |
228 // other operations | |
229 | |
230 double | |
231 RowVector::min (void) const | |
232 { | |
5275 | 233 octave_idx_type len = length (); |
458 | 234 if (len == 0) |
235 return 0; | |
236 | |
237 double res = elem (0); | |
238 | |
5275 | 239 for (octave_idx_type i = 1; i < len; i++) |
458 | 240 if (elem (i) < res) |
241 res = elem (i); | |
242 | |
243 return res; | |
244 } | |
245 | |
246 double | |
247 RowVector::max (void) const | |
248 { | |
5275 | 249 octave_idx_type len = length (); |
458 | 250 if (len == 0) |
251 return 0; | |
252 | |
253 double res = elem (0); | |
254 | |
5275 | 255 for (octave_idx_type i = 1; i < len; i++) |
458 | 256 if (elem (i) > res) |
257 res = elem (i); | |
258 | |
259 return res; | |
260 } | |
261 | |
3504 | 262 std::ostream& |
263 operator << (std::ostream& os, const RowVector& a) | |
458 | 264 { |
265 // int field_width = os.precision () + 7; | |
1360 | 266 |
5275 | 267 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 268 os << " " /* setw (field_width) */ << a.elem (i); |
269 return os; | |
270 } | |
271 | |
3504 | 272 std::istream& |
273 operator >> (std::istream& is, RowVector& a) | |
458 | 274 { |
5275 | 275 octave_idx_type len = a.length(); |
458 | 276 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
277 if (len > 0) |
458 | 278 { |
279 double tmp; | |
5275 | 280 for (octave_idx_type i = 0; i < len; i++) |
458 | 281 { |
282 is >> tmp; | |
283 if (is) | |
284 a.elem (i) = tmp; | |
285 else | |
286 break; | |
287 } | |
288 } | |
532 | 289 return is; |
458 | 290 } |
291 | |
1205 | 292 // other operations |
293 | |
294 RowVector | |
5275 | 295 linspace (double x1, double x2, octave_idx_type n) |
1205 | 296 { |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
297 if (n < 1) n = 1; |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
298 |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
299 NoAlias<RowVector> retval (n); |
1205 | 300 |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
301 double delta = (x2 - x1) / (n - 1); |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
302 retval(0) = x1; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
303 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
|
304 retval(i) = x1 + i*delta; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
305 retval(n-1) = x2; |
1205 | 306 |
307 return retval; | |
308 } | |
309 | |
310 // row vector by column vector -> scalar | |
311 | |
312 double | |
313 operator * (const RowVector& v, const ColumnVector& a) | |
314 { | |
1947 | 315 double retval = 0.0; |
316 | |
5275 | 317 octave_idx_type len = v.length (); |
1947 | 318 |
5275 | 319 octave_idx_type a_len = a.length (); |
2386 | 320 |
321 if (len != a_len) | |
322 gripe_nonconformant ("operator *", len, a_len); | |
1947 | 323 else if (len != 0) |
5983 | 324 F77_FUNC (xddot, XDDOT) (len, v.data (), 1, a.data (), 1, retval); |
1205 | 325 |
1947 | 326 return retval; |
1205 | 327 } |
328 | |
329 Complex | |
330 operator * (const RowVector& v, const ComplexColumnVector& a) | |
331 { | |
332 ComplexRowVector tmp (v); | |
333 return tmp * a; | |
334 } |