Mercurial > hg > octave-lyh
annotate liboctave/dColVector.cc @ 10314:07ebe522dac2
untabify liboctave C++ sources
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 11 Feb 2010 12:23:32 -0500 |
parents | 4c0cdbe0acca |
children | a0728e81ed25 |
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 |
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*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 const octave_idx_type& |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
49 F77_CHAR_ARG_LEN_DECL); |
458 | 50 } |
51 | |
1360 | 52 // Column Vector class. |
458 | 53 |
2386 | 54 bool |
458 | 55 ColumnVector::operator == (const ColumnVector& a) const |
56 { | |
5275 | 57 octave_idx_type len = length (); |
458 | 58 if (len != a.length ()) |
59 return 0; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
60 return mx_inline_equal (len, data (), a.data ()); |
458 | 61 } |
62 | |
2386 | 63 bool |
458 | 64 ColumnVector::operator != (const ColumnVector& a) const |
65 { | |
66 return !(*this == a); | |
67 } | |
68 | |
69 ColumnVector& | |
5275 | 70 ColumnVector::insert (const ColumnVector& a, octave_idx_type r) |
458 | 71 { |
5275 | 72 octave_idx_type a_len = a.length (); |
4316 | 73 |
1699 | 74 if (r < 0 || r + a_len > length ()) |
458 | 75 { |
76 (*current_liboctave_error_handler) ("range error for insert"); | |
77 return *this; | |
78 } | |
79 | |
4316 | 80 if (a_len > 0) |
81 { | |
82 make_unique (); | |
83 | |
5275 | 84 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
|
85 xelem (r+i) = a.elem (i); |
4316 | 86 } |
458 | 87 |
88 return *this; | |
89 } | |
90 | |
91 ColumnVector& | |
92 ColumnVector::fill (double val) | |
93 { | |
5275 | 94 octave_idx_type len = length (); |
4316 | 95 |
458 | 96 if (len > 0) |
4316 | 97 { |
98 make_unique (); | |
99 | |
5275 | 100 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
|
101 xelem (i) = val; |
4316 | 102 } |
103 | |
458 | 104 return *this; |
105 } | |
106 | |
107 ColumnVector& | |
5275 | 108 ColumnVector::fill (double val, octave_idx_type r1, octave_idx_type r2) |
458 | 109 { |
5275 | 110 octave_idx_type len = length (); |
4316 | 111 |
458 | 112 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
113 { | |
114 (*current_liboctave_error_handler) ("range error for fill"); | |
115 return *this; | |
116 } | |
117 | |
5275 | 118 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
458 | 119 |
4316 | 120 if (r2 >= r1) |
121 { | |
122 make_unique (); | |
123 | |
5275 | 124 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
|
125 xelem (i) = val; |
4316 | 126 } |
458 | 127 |
128 return *this; | |
129 } | |
130 | |
131 ColumnVector | |
132 ColumnVector::stack (const ColumnVector& a) const | |
133 { | |
5275 | 134 octave_idx_type len = length (); |
135 octave_idx_type nr_insert = len; | |
458 | 136 ColumnVector retval (len + a.length ()); |
137 retval.insert (*this, 0); | |
138 retval.insert (a, nr_insert); | |
139 return retval; | |
140 } | |
141 | |
142 RowVector | |
143 ColumnVector::transpose (void) const | |
144 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
145 return MArray<double>::transpose(); |
458 | 146 } |
147 | |
1205 | 148 ColumnVector |
149 real (const ComplexColumnVector& a) | |
150 { | |
5275 | 151 octave_idx_type a_len = a.length (); |
1205 | 152 ColumnVector retval; |
153 if (a_len > 0) | |
3769 | 154 retval = ColumnVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205 | 155 return retval; |
156 } | |
157 | |
158 ColumnVector | |
159 imag (const ComplexColumnVector& a) | |
160 { | |
5275 | 161 octave_idx_type a_len = a.length (); |
1205 | 162 ColumnVector retval; |
163 if (a_len > 0) | |
3769 | 164 retval = ColumnVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205 | 165 return retval; |
166 } | |
167 | |
458 | 168 // resize is the destructive equivalent for this one |
169 | |
170 ColumnVector | |
5275 | 171 ColumnVector::extract (octave_idx_type r1, octave_idx_type r2) const |
458 | 172 { |
5275 | 173 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
458 | 174 |
5275 | 175 octave_idx_type new_r = r2 - r1 + 1; |
458 | 176 |
177 ColumnVector result (new_r); | |
178 | |
5275 | 179 for (octave_idx_type i = 0; i < new_r; i++) |
4316 | 180 result.xelem (i) = elem (r1+i); |
181 | |
182 return result; | |
183 } | |
184 | |
185 ColumnVector | |
5275 | 186 ColumnVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 187 { |
188 ColumnVector 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 | |
1205 | 196 // matrix by column vector -> column vector operations |
458 | 197 |
1205 | 198 ColumnVector |
199 operator * (const Matrix& m, const ColumnVector& a) | |
458 | 200 { |
1947 | 201 ColumnVector retval; |
202 | |
5275 | 203 octave_idx_type nr = m.rows (); |
204 octave_idx_type nc = m.cols (); | |
1947 | 205 |
5275 | 206 octave_idx_type a_len = a.length (); |
2386 | 207 |
208 if (nc != a_len) | |
209 gripe_nonconformant ("operator *", nr, nc, a_len, 1); | |
1947 | 210 else |
458 | 211 { |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
212 retval.clear (nr); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
213 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
214 if (nr != 0) |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
215 { |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
216 double *y = retval.fortran_vec (); |
1947 | 217 |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
218 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
219 nr, nc, 1.0, m.data (), nr, |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
220 a.data (), 1, 0.0, y, 1 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
221 F77_CHAR_ARG_LEN (1))); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
222 } |
458 | 223 } |
224 | |
1947 | 225 return retval; |
458 | 226 } |
227 | |
1205 | 228 // diagonal matrix by column vector -> column vector operations |
229 | |
230 ColumnVector | |
231 operator * (const DiagMatrix& m, const ColumnVector& a) | |
458 | 232 { |
1947 | 233 ColumnVector retval; |
234 | |
5275 | 235 octave_idx_type nr = m.rows (); |
236 octave_idx_type nc = m.cols (); | |
1947 | 237 |
5275 | 238 octave_idx_type a_len = a.length (); |
1947 | 239 |
1205 | 240 if (nc != a_len) |
2386 | 241 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947 | 242 else |
458 | 243 { |
1947 | 244 if (nr == 0 || nc == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
245 retval.resize (nr, 0.0); |
1947 | 246 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
247 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
248 retval.resize (nr); |
1947 | 249 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
250 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
|
251 retval.elem (i) = a.elem (i) * m.elem (i, i); |
1947 | 252 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
253 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
|
254 retval.elem (i) = 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
255 } |
458 | 256 } |
257 | |
1947 | 258 return retval; |
458 | 259 } |
260 | |
261 // other operations | |
262 | |
263 double | |
264 ColumnVector::min (void) const | |
265 { | |
5275 | 266 octave_idx_type len = length (); |
458 | 267 if (len == 0) |
268 return 0.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 | |
279 double | |
280 ColumnVector::max (void) const | |
281 { | |
5275 | 282 octave_idx_type len = length (); |
458 | 283 if (len == 0) |
284 return 0.0; | |
285 | |
286 double res = elem (0); | |
287 | |
5275 | 288 for (octave_idx_type i = 1; i < len; i++) |
458 | 289 if (elem (i) > res) |
290 res = elem (i); | |
291 | |
292 return res; | |
293 } | |
294 | |
3504 | 295 std::ostream& |
296 operator << (std::ostream& os, const ColumnVector& a) | |
458 | 297 { |
298 // int field_width = os.precision () + 7; | |
5275 | 299 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 300 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
301 return os; | |
302 } | |
303 | |
3504 | 304 std::istream& |
305 operator >> (std::istream& is, ColumnVector& a) | |
458 | 306 { |
5275 | 307 octave_idx_type len = a.length(); |
458 | 308 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
309 if (len > 0) |
458 | 310 { |
311 double tmp; | |
5275 | 312 for (octave_idx_type i = 0; i < len; i++) |
458 | 313 { |
314 is >> tmp; | |
315 if (is) | |
316 a.elem (i) = tmp; | |
317 else | |
318 break; | |
319 } | |
320 } | |
532 | 321 return is; |
458 | 322 } |