Mercurial > hg > octave-lyh
annotate liboctave/dRowVector.cc @ 10363:a0728e81ed25
improve diag matrix interface & implementation
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 11:44:38 +0100 |
parents | 07ebe522dac2 |
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 (dgemv, DGEMV) (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 double&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
46 const double*, const octave_idx_type&, const double*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 const octave_idx_type&, const double&, double*, 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 F77_RET_T |
50 F77_FUNC (xddot, XDDOT) (const octave_idx_type&, const double*, const octave_idx_type&, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
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; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
62 return mx_inline_equal (len, data (), a.data ()); |
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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
87 xelem (c+i) = a.elem (i); |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
103 xelem (i) = val; |
4316 | 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++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
127 xelem (i) = val; |
4316 | 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 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
153 return do_mx_unary_op<double, Complex> (a, mx_inline_real); |
1205 | 154 } |
155 | |
156 RowVector | |
157 imag (const ComplexRowVector& a) | |
158 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
159 return do_mx_unary_op<double, Complex> (a, mx_inline_imag); |
1205 | 160 } |
161 | |
162 RowVector | |
5275 | 163 RowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 164 { |
5275 | 165 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 166 |
5275 | 167 octave_idx_type new_c = c2 - c1 + 1; |
458 | 168 |
169 RowVector result (new_c); | |
170 | |
5275 | 171 for (octave_idx_type i = 0; i < new_c; i++) |
4316 | 172 result.xelem (i) = elem (c1+i); |
173 | |
174 return result; | |
175 } | |
176 | |
177 RowVector | |
5275 | 178 RowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 179 { |
180 RowVector result (n); | |
181 | |
5275 | 182 for (octave_idx_type i = 0; i < n; i++) |
4316 | 183 result.xelem (i) = elem (r1+i); |
458 | 184 |
185 return result; | |
186 } | |
187 | |
188 // row vector by matrix -> row vector | |
189 | |
190 RowVector | |
191 operator * (const RowVector& v, const Matrix& a) | |
192 { | |
1947 | 193 RowVector retval; |
194 | |
5275 | 195 octave_idx_type len = v.length (); |
1947 | 196 |
5275 | 197 octave_idx_type a_nr = a.rows (); |
198 octave_idx_type a_nc = a.cols (); | |
2386 | 199 |
200 if (a_nr != len) | |
201 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 202 else |
458 | 203 { |
1947 | 204 if (len == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
205 retval.resize (a_nc, 0.0); |
1947 | 206 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
207 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
208 // Transpose A to form A'*x == (x'*A)' |
1947 | 209 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
210 octave_idx_type ld = a_nr; |
1947 | 211 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
212 retval.resize (a_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
213 double *y = retval.fortran_vec (); |
1947 | 214 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
215 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
|
216 a_nr, a_nc, 1.0, a.data (), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
217 ld, v.data (), 1, 0.0, y, 1 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
218 F77_CHAR_ARG_LEN (1))); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
219 } |
458 | 220 } |
221 | |
1947 | 222 return retval; |
458 | 223 } |
224 | |
225 // other operations | |
226 | |
227 double | |
228 RowVector::min (void) const | |
229 { | |
5275 | 230 octave_idx_type len = length (); |
458 | 231 if (len == 0) |
232 return 0; | |
233 | |
234 double res = elem (0); | |
235 | |
5275 | 236 for (octave_idx_type i = 1; i < len; i++) |
458 | 237 if (elem (i) < res) |
238 res = elem (i); | |
239 | |
240 return res; | |
241 } | |
242 | |
243 double | |
244 RowVector::max (void) const | |
245 { | |
5275 | 246 octave_idx_type len = length (); |
458 | 247 if (len == 0) |
248 return 0; | |
249 | |
250 double res = elem (0); | |
251 | |
5275 | 252 for (octave_idx_type i = 1; i < len; i++) |
458 | 253 if (elem (i) > res) |
254 res = elem (i); | |
255 | |
256 return res; | |
257 } | |
258 | |
3504 | 259 std::ostream& |
260 operator << (std::ostream& os, const RowVector& a) | |
458 | 261 { |
262 // int field_width = os.precision () + 7; | |
1360 | 263 |
5275 | 264 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 265 os << " " /* setw (field_width) */ << a.elem (i); |
266 return os; | |
267 } | |
268 | |
3504 | 269 std::istream& |
270 operator >> (std::istream& is, RowVector& a) | |
458 | 271 { |
5275 | 272 octave_idx_type len = a.length(); |
458 | 273 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
274 if (len > 0) |
458 | 275 { |
276 double tmp; | |
5275 | 277 for (octave_idx_type i = 0; i < len; i++) |
458 | 278 { |
279 is >> tmp; | |
280 if (is) | |
281 a.elem (i) = tmp; | |
282 else | |
283 break; | |
284 } | |
285 } | |
532 | 286 return is; |
458 | 287 } |
288 | |
1205 | 289 // other operations |
290 | |
291 RowVector | |
5275 | 292 linspace (double x1, double x2, octave_idx_type n) |
1205 | 293 { |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
294 if (n < 1) n = 1; |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
295 |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
296 NoAlias<RowVector> retval (n); |
1205 | 297 |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
298 double delta = (x2 - x1) / (n - 1); |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
299 retval(0) = x1; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
300 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
|
301 retval(i) = x1 + i*delta; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
302 retval(n-1) = x2; |
1205 | 303 |
304 return retval; | |
305 } | |
306 | |
307 // row vector by column vector -> scalar | |
308 | |
309 double | |
310 operator * (const RowVector& v, const ColumnVector& a) | |
311 { | |
1947 | 312 double retval = 0.0; |
313 | |
5275 | 314 octave_idx_type len = v.length (); |
1947 | 315 |
5275 | 316 octave_idx_type a_len = a.length (); |
2386 | 317 |
318 if (len != a_len) | |
319 gripe_nonconformant ("operator *", len, a_len); | |
1947 | 320 else if (len != 0) |
5983 | 321 F77_FUNC (xddot, XDDOT) (len, v.data (), 1, a.data (), 1, retval); |
1205 | 322 |
1947 | 323 return retval; |
1205 | 324 } |
325 | |
326 Complex | |
327 operator * (const RowVector& v, const ComplexColumnVector& a) | |
328 { | |
329 ComplexRowVector tmp (v); | |
330 return tmp * a; | |
331 } |