Mercurial > hg > octave-nkf
annotate liboctave/dRowVector.cc @ 9300:fddb9f9f724b
Correct documentation for keyboard function
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Wed, 03 Jun 2009 13:21:37 -0700 |
parents | dc07bc4157b8 |
children | 3d6a9aea2aea |
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, | |
5275 | 45 const octave_idx_type&, const octave_idx_type&, const double&, |
46 const double*, const octave_idx_type&, const double*, | |
47 const octave_idx_type&, const double&, double*, const octave_idx_type& | |
4552 | 48 F77_CHAR_ARG_LEN_DECL); |
5983 | 49 F77_RET_T |
50 F77_FUNC (xddot, XDDOT) (const octave_idx_type&, const double*, const octave_idx_type&, | |
51 const double*, const octave_idx_type&, double&); | |
458 | 52 } |
53 | |
1360 | 54 // Row Vector class. |
458 | 55 |
2386 | 56 bool |
458 | 57 RowVector::operator == (const RowVector& a) const |
58 { | |
5275 | 59 octave_idx_type len = length (); |
458 | 60 if (len != a.length ()) |
61 return 0; | |
3769 | 62 return mx_inline_equal (data (), a.data (), len); |
458 | 63 } |
64 | |
2386 | 65 bool |
458 | 66 RowVector::operator != (const RowVector& a) const |
67 { | |
68 return !(*this == a); | |
69 } | |
70 | |
71 RowVector& | |
5275 | 72 RowVector::insert (const RowVector& a, octave_idx_type c) |
458 | 73 { |
5275 | 74 octave_idx_type a_len = a.length (); |
4316 | 75 |
1699 | 76 if (c < 0 || c + a_len > length ()) |
458 | 77 { |
78 (*current_liboctave_error_handler) ("range error for insert"); | |
79 return *this; | |
80 } | |
81 | |
4316 | 82 if (a_len > 0) |
83 { | |
84 make_unique (); | |
85 | |
5275 | 86 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 87 xelem (c+i) = a.elem (i); |
88 } | |
458 | 89 |
90 return *this; | |
91 } | |
92 | |
93 RowVector& | |
94 RowVector::fill (double val) | |
95 { | |
5275 | 96 octave_idx_type len = length (); |
4316 | 97 |
458 | 98 if (len > 0) |
4316 | 99 { |
100 make_unique (); | |
101 | |
5275 | 102 for (octave_idx_type i = 0; i < len; i++) |
4316 | 103 xelem (i) = val; |
104 } | |
105 | |
458 | 106 return *this; |
107 } | |
108 | |
109 RowVector& | |
5275 | 110 RowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458 | 111 { |
5275 | 112 octave_idx_type len = length (); |
4316 | 113 |
458 | 114 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
115 { | |
116 (*current_liboctave_error_handler) ("range error for fill"); | |
117 return *this; | |
118 } | |
119 | |
5275 | 120 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 121 |
4316 | 122 if (c2 >= c1) |
123 { | |
124 make_unique (); | |
125 | |
5275 | 126 for (octave_idx_type i = c1; i <= c2; i++) |
4316 | 127 xelem (i) = val; |
128 } | |
458 | 129 |
130 return *this; | |
131 } | |
132 | |
133 RowVector | |
134 RowVector::append (const RowVector& a) const | |
135 { | |
5275 | 136 octave_idx_type len = length (); |
137 octave_idx_type nc_insert = len; | |
458 | 138 RowVector retval (len + a.length ()); |
139 retval.insert (*this, 0); | |
140 retval.insert (a, nc_insert); | |
141 return retval; | |
142 } | |
143 | |
144 ColumnVector | |
145 RowVector::transpose (void) const | |
146 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
147 return MArray<double>::transpose(); |
458 | 148 } |
149 | |
150 RowVector | |
1205 | 151 real (const ComplexRowVector& a) |
152 { | |
5275 | 153 octave_idx_type a_len = a.length (); |
1205 | 154 RowVector retval; |
155 if (a_len > 0) | |
3769 | 156 retval = RowVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205 | 157 return retval; |
158 } | |
159 | |
160 RowVector | |
161 imag (const ComplexRowVector& a) | |
162 { | |
5275 | 163 octave_idx_type a_len = a.length (); |
1205 | 164 RowVector retval; |
165 if (a_len > 0) | |
3769 | 166 retval = RowVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205 | 167 return retval; |
168 } | |
169 | |
170 RowVector | |
5275 | 171 RowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 172 { |
5275 | 173 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 174 |
5275 | 175 octave_idx_type new_c = c2 - c1 + 1; |
458 | 176 |
177 RowVector result (new_c); | |
178 | |
5275 | 179 for (octave_idx_type i = 0; i < new_c; i++) |
4316 | 180 result.xelem (i) = elem (c1+i); |
181 | |
182 return result; | |
183 } | |
184 | |
185 RowVector | |
5275 | 186 RowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 187 { |
188 RowVector result (n); | |
189 | |
5275 | 190 for (octave_idx_type i = 0; i < n; i++) |
4316 | 191 result.xelem (i) = elem (r1+i); |
458 | 192 |
193 return result; | |
194 } | |
195 | |
196 // row vector by matrix -> row vector | |
197 | |
198 RowVector | |
199 operator * (const RowVector& v, const Matrix& a) | |
200 { | |
1947 | 201 RowVector retval; |
202 | |
5275 | 203 octave_idx_type len = v.length (); |
1947 | 204 |
5275 | 205 octave_idx_type a_nr = a.rows (); |
206 octave_idx_type a_nc = a.cols (); | |
2386 | 207 |
208 if (a_nr != len) | |
209 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 210 else |
458 | 211 { |
1947 | 212 if (len == 0) |
213 retval.resize (a_nc, 0.0); | |
214 else | |
215 { | |
216 // Transpose A to form A'*x == (x'*A)' | |
217 | |
5275 | 218 octave_idx_type ld = a_nr; |
1947 | 219 |
220 retval.resize (a_nc); | |
221 double *y = retval.fortran_vec (); | |
222 | |
4552 | 223 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
224 a_nr, a_nc, 1.0, a.data (), | |
225 ld, v.data (), 1, 0.0, y, 1 | |
226 F77_CHAR_ARG_LEN (1))); | |
1947 | 227 } |
458 | 228 } |
229 | |
1947 | 230 return retval; |
458 | 231 } |
232 | |
233 // other operations | |
234 | |
235 RowVector | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
236 RowVector::map (dmapper fcn) const |
458 | 237 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
238 return MArray<double>::map<double> (func_ptr (fcn)); |
458 | 239 } |
240 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
241 ComplexRowVector |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
242 RowVector::map (cmapper fcn) const |
1205 | 243 { |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
244 return MArray<double>::map<Complex> (func_ptr (fcn)); |
458 | 245 } |
246 | |
247 double | |
248 RowVector::min (void) const | |
249 { | |
5275 | 250 octave_idx_type len = length (); |
458 | 251 if (len == 0) |
252 return 0; | |
253 | |
254 double res = elem (0); | |
255 | |
5275 | 256 for (octave_idx_type i = 1; i < len; i++) |
458 | 257 if (elem (i) < res) |
258 res = elem (i); | |
259 | |
260 return res; | |
261 } | |
262 | |
263 double | |
264 RowVector::max (void) const | |
265 { | |
5275 | 266 octave_idx_type len = length (); |
458 | 267 if (len == 0) |
268 return 0; | |
269 | |
270 double res = elem (0); | |
271 | |
5275 | 272 for (octave_idx_type i = 1; i < len; i++) |
458 | 273 if (elem (i) > res) |
274 res = elem (i); | |
275 | |
276 return res; | |
277 } | |
278 | |
3504 | 279 std::ostream& |
280 operator << (std::ostream& os, const RowVector& a) | |
458 | 281 { |
282 // int field_width = os.precision () + 7; | |
1360 | 283 |
5275 | 284 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 285 os << " " /* setw (field_width) */ << a.elem (i); |
286 return os; | |
287 } | |
288 | |
3504 | 289 std::istream& |
290 operator >> (std::istream& is, RowVector& a) | |
458 | 291 { |
5275 | 292 octave_idx_type len = a.length(); |
458 | 293 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
294 if (len > 0) |
458 | 295 { |
296 double tmp; | |
5275 | 297 for (octave_idx_type i = 0; i < len; i++) |
458 | 298 { |
299 is >> tmp; | |
300 if (is) | |
301 a.elem (i) = tmp; | |
302 else | |
303 break; | |
304 } | |
305 } | |
532 | 306 return is; |
458 | 307 } |
308 | |
1205 | 309 // other operations |
310 | |
311 RowVector | |
5275 | 312 linspace (double x1, double x2, octave_idx_type n) |
1205 | 313 { |
314 RowVector retval; | |
315 | |
3322 | 316 if (n > 1) |
1205 | 317 { |
318 retval.resize (n); | |
319 double delta = (x2 - x1) / (n - 1); | |
320 retval.elem (0) = x1; | |
5275 | 321 for (octave_idx_type i = 1; i < n-1; i++) |
1205 | 322 retval.elem (i) = x1 + i * delta; |
323 retval.elem (n-1) = x2; | |
324 } | |
6630 | 325 else |
3322 | 326 { |
6629 | 327 retval.resize (1); |
328 retval.elem (0) = x2; | |
3322 | 329 } |
1205 | 330 |
331 return retval; | |
332 } | |
333 | |
334 // row vector by column vector -> scalar | |
335 | |
336 double | |
337 operator * (const RowVector& v, const ColumnVector& a) | |
338 { | |
1947 | 339 double retval = 0.0; |
340 | |
5275 | 341 octave_idx_type len = v.length (); |
1947 | 342 |
5275 | 343 octave_idx_type a_len = a.length (); |
2386 | 344 |
345 if (len != a_len) | |
346 gripe_nonconformant ("operator *", len, a_len); | |
1947 | 347 else if (len != 0) |
5983 | 348 F77_FUNC (xddot, XDDOT) (len, v.data (), 1, a.data (), 1, retval); |
1205 | 349 |
1947 | 350 return retval; |
1205 | 351 } |
352 | |
353 Complex | |
354 operator * (const RowVector& v, const ComplexColumnVector& a) | |
355 { | |
356 ComplexRowVector tmp (v); | |
357 return tmp * a; | |
358 } | |
359 | |
458 | 360 /* |
361 ;;; Local Variables: *** | |
362 ;;; mode: C++ *** | |
363 ;;; End: *** | |
364 */ |