Mercurial > hg > octave-nkf
annotate liboctave/dColVector.cc @ 10687:a8ce6bdecce5
Improve documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 20:22:38 -0700 |
parents | 4d1fc073fbb7 |
children | 141b3fb5cef7 |
rev | line source |
---|---|
1993 | 1 // ColumnVector manipulations. |
458 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004, |
8920 | 5 2005, 2007, 2008 John W. Eaton |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10363
diff
changeset
|
6 Copyright (C) 2010 VZLU Prague |
458 | 7 |
8 This file is part of Octave. | |
9 | |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
458 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
7016 | 21 along with Octave; see the file COPYING. If not, see |
22 <http://www.gnu.org/licenses/>. | |
458 | 23 |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
1192 | 27 #include <config.h> |
458 | 28 #endif |
29 | |
3503 | 30 #include <iostream> |
458 | 31 |
4669 | 32 #include "Array-util.h" |
1847 | 33 #include "f77-fcn.h" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
34 #include "functor.h" |
1368 | 35 #include "lo-error.h" |
458 | 36 #include "mx-base.h" |
37 #include "mx-inlines.cc" | |
1650 | 38 #include "oct-cmplx.h" |
458 | 39 |
40 // Fortran functions we call. | |
41 | |
42 extern "C" | |
43 { | |
4552 | 44 F77_RET_T |
45 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
|
46 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
|
47 const double*, const octave_idx_type&, const double*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 const octave_idx_type&, const double&, double*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
49 const octave_idx_type& |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
50 F77_CHAR_ARG_LEN_DECL); |
458 | 51 } |
52 | |
1360 | 53 // Column Vector class. |
458 | 54 |
2386 | 55 bool |
458 | 56 ColumnVector::operator == (const ColumnVector& a) const |
57 { | |
5275 | 58 octave_idx_type len = length (); |
458 | 59 if (len != a.length ()) |
60 return 0; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
61 return mx_inline_equal (len, data (), a.data ()); |
458 | 62 } |
63 | |
2386 | 64 bool |
458 | 65 ColumnVector::operator != (const ColumnVector& a) const |
66 { | |
67 return !(*this == a); | |
68 } | |
69 | |
70 ColumnVector& | |
5275 | 71 ColumnVector::insert (const ColumnVector& a, octave_idx_type r) |
458 | 72 { |
5275 | 73 octave_idx_type a_len = a.length (); |
4316 | 74 |
1699 | 75 if (r < 0 || r + a_len > length ()) |
458 | 76 { |
77 (*current_liboctave_error_handler) ("range error for insert"); | |
78 return *this; | |
79 } | |
80 | |
4316 | 81 if (a_len > 0) |
82 { | |
83 make_unique (); | |
84 | |
5275 | 85 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
|
86 xelem (r+i) = a.elem (i); |
4316 | 87 } |
458 | 88 |
89 return *this; | |
90 } | |
91 | |
92 ColumnVector& | |
93 ColumnVector::fill (double val) | |
94 { | |
5275 | 95 octave_idx_type len = length (); |
4316 | 96 |
458 | 97 if (len > 0) |
4316 | 98 { |
99 make_unique (); | |
100 | |
5275 | 101 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
|
102 xelem (i) = val; |
4316 | 103 } |
104 | |
458 | 105 return *this; |
106 } | |
107 | |
108 ColumnVector& | |
5275 | 109 ColumnVector::fill (double val, octave_idx_type r1, octave_idx_type r2) |
458 | 110 { |
5275 | 111 octave_idx_type len = length (); |
4316 | 112 |
458 | 113 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
114 { | |
115 (*current_liboctave_error_handler) ("range error for fill"); | |
116 return *this; | |
117 } | |
118 | |
5275 | 119 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
458 | 120 |
4316 | 121 if (r2 >= r1) |
122 { | |
123 make_unique (); | |
124 | |
5275 | 125 for (octave_idx_type i = r1; i <= r2; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
126 xelem (i) = val; |
4316 | 127 } |
458 | 128 |
129 return *this; | |
130 } | |
131 | |
132 ColumnVector | |
133 ColumnVector::stack (const ColumnVector& a) const | |
134 { | |
5275 | 135 octave_idx_type len = length (); |
136 octave_idx_type nr_insert = len; | |
458 | 137 ColumnVector retval (len + a.length ()); |
138 retval.insert (*this, 0); | |
139 retval.insert (a, nr_insert); | |
140 return retval; | |
141 } | |
142 | |
143 RowVector | |
144 ColumnVector::transpose (void) const | |
145 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
146 return MArray<double>::transpose(); |
458 | 147 } |
148 | |
1205 | 149 ColumnVector |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
150 ColumnVector::abs (void) const |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
151 { |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
152 return do_mx_unary_map<double, double, std::abs> (*this); |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
153 } |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
154 |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
155 ColumnVector |
1205 | 156 real (const ComplexColumnVector& a) |
157 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
158 return do_mx_unary_op<double, Complex> (a, mx_inline_real); |
1205 | 159 } |
160 | |
161 ColumnVector | |
162 imag (const ComplexColumnVector& a) | |
163 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
164 return do_mx_unary_op<double, Complex> (a, mx_inline_imag); |
1205 | 165 } |
166 | |
458 | 167 // resize is the destructive equivalent for this one |
168 | |
169 ColumnVector | |
5275 | 170 ColumnVector::extract (octave_idx_type r1, octave_idx_type r2) const |
458 | 171 { |
5275 | 172 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
458 | 173 |
5275 | 174 octave_idx_type new_r = r2 - r1 + 1; |
458 | 175 |
176 ColumnVector result (new_r); | |
177 | |
5275 | 178 for (octave_idx_type i = 0; i < new_r; i++) |
4316 | 179 result.xelem (i) = elem (r1+i); |
180 | |
181 return result; | |
182 } | |
183 | |
184 ColumnVector | |
5275 | 185 ColumnVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 186 { |
187 ColumnVector result (n); | |
188 | |
5275 | 189 for (octave_idx_type i = 0; i < n; i++) |
4316 | 190 result.xelem (i) = elem (r1+i); |
458 | 191 |
192 return result; | |
193 } | |
194 | |
1205 | 195 // matrix by column vector -> column vector operations |
458 | 196 |
1205 | 197 ColumnVector |
198 operator * (const Matrix& m, const ColumnVector& a) | |
458 | 199 { |
1947 | 200 ColumnVector retval; |
201 | |
5275 | 202 octave_idx_type nr = m.rows (); |
203 octave_idx_type nc = m.cols (); | |
1947 | 204 |
5275 | 205 octave_idx_type a_len = a.length (); |
2386 | 206 |
207 if (nc != a_len) | |
208 gripe_nonconformant ("operator *", nr, nc, a_len, 1); | |
1947 | 209 else |
458 | 210 { |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
211 retval.clear (nr); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
212 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
213 if (nr != 0) |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
214 { |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
215 double *y = retval.fortran_vec (); |
1947 | 216 |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
217 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
218 nr, nc, 1.0, m.data (), nr, |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
219 a.data (), 1, 0.0, y, 1 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
220 F77_CHAR_ARG_LEN (1))); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
221 } |
458 | 222 } |
223 | |
1947 | 224 return retval; |
458 | 225 } |
226 | |
1205 | 227 // diagonal matrix by column vector -> column vector operations |
228 | |
229 ColumnVector | |
230 operator * (const DiagMatrix& m, const ColumnVector& a) | |
458 | 231 { |
1947 | 232 ColumnVector retval; |
233 | |
5275 | 234 octave_idx_type nr = m.rows (); |
235 octave_idx_type nc = m.cols (); | |
1947 | 236 |
5275 | 237 octave_idx_type a_len = a.length (); |
1947 | 238 |
1205 | 239 if (nc != a_len) |
2386 | 240 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947 | 241 else |
458 | 242 { |
1947 | 243 if (nr == 0 || nc == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
244 retval.resize (nr, 0.0); |
1947 | 245 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
246 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
247 retval.resize (nr); |
1947 | 248 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
249 for (octave_idx_type i = 0; i < a_len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
250 retval.elem (i) = a.elem (i) * m.elem (i, i); |
1947 | 251 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
252 for (octave_idx_type i = a_len; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
253 retval.elem (i) = 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
254 } |
458 | 255 } |
256 | |
1947 | 257 return retval; |
458 | 258 } |
259 | |
260 // other operations | |
261 | |
262 double | |
263 ColumnVector::min (void) const | |
264 { | |
5275 | 265 octave_idx_type len = length (); |
458 | 266 if (len == 0) |
267 return 0.0; | |
268 | |
269 double res = elem (0); | |
270 | |
5275 | 271 for (octave_idx_type i = 1; i < len; i++) |
458 | 272 if (elem (i) < res) |
273 res = elem (i); | |
274 | |
275 return res; | |
276 } | |
277 | |
278 double | |
279 ColumnVector::max (void) const | |
280 { | |
5275 | 281 octave_idx_type len = length (); |
458 | 282 if (len == 0) |
283 return 0.0; | |
284 | |
285 double res = elem (0); | |
286 | |
5275 | 287 for (octave_idx_type i = 1; i < len; i++) |
458 | 288 if (elem (i) > res) |
289 res = elem (i); | |
290 | |
291 return res; | |
292 } | |
293 | |
3504 | 294 std::ostream& |
295 operator << (std::ostream& os, const ColumnVector& a) | |
458 | 296 { |
297 // int field_width = os.precision () + 7; | |
5275 | 298 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 299 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
300 return os; | |
301 } | |
302 | |
3504 | 303 std::istream& |
304 operator >> (std::istream& is, ColumnVector& a) | |
458 | 305 { |
5275 | 306 octave_idx_type len = a.length(); |
458 | 307 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
308 if (len > 0) |
458 | 309 { |
310 double tmp; | |
5275 | 311 for (octave_idx_type i = 0; i < len; i++) |
458 | 312 { |
313 is >> tmp; | |
314 if (is) | |
315 a.elem (i) = tmp; | |
316 else | |
317 break; | |
318 } | |
319 } | |
532 | 320 return is; |
458 | 321 } |