Mercurial > hg > octave-lyh
annotate liboctave/array/dColVector.cc @ 16660:cbb1bb7a5c3d
Added tag ss-3-7-5 for changeset 608e307b4914
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 14 May 2013 05:23:53 -0400 |
parents | 648dabbb4c6b |
children |
rev | line source |
---|---|
1993 | 1 // ColumnVector manipulations. |
458 | 2 /* |
3 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
4 Copyright (C) 1994-2012 John W. Eaton |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10363
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
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*, | |
10314
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 { | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14394
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 { |
14394
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
215 if (nc == 0) |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
216 retval.fill (0.0); |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
217 else |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
218 { |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
219 double *y = retval.fortran_vec (); |
1947 | 220 |
14394
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
221 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
222 nr, nc, 1.0, m.data (), nr, |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
223 a.data (), 1, 0.0, y, 1 |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
224 F77_CHAR_ARG_LEN (1))); |
ed8c4921bf61
correctly fill result for M * v for Nx0 * 0x1 operations
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
225 } |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
226 } |
458 | 227 } |
228 | |
1947 | 229 return retval; |
458 | 230 } |
231 | |
1205 | 232 // diagonal matrix by column vector -> column vector operations |
233 | |
234 ColumnVector | |
235 operator * (const DiagMatrix& m, const ColumnVector& a) | |
458 | 236 { |
1947 | 237 ColumnVector retval; |
238 | |
5275 | 239 octave_idx_type nr = m.rows (); |
240 octave_idx_type nc = m.cols (); | |
1947 | 241 |
5275 | 242 octave_idx_type a_len = a.length (); |
1947 | 243 |
1205 | 244 if (nc != a_len) |
2386 | 245 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947 | 246 else |
458 | 247 { |
1947 | 248 if (nr == 0 || nc == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
249 retval.resize (nr, 0.0); |
1947 | 250 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
251 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
252 retval.resize (nr); |
1947 | 253 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
254 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
|
255 retval.elem (i) = a.elem (i) * m.elem (i, i); |
1947 | 256 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
257 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
|
258 retval.elem (i) = 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
259 } |
458 | 260 } |
261 | |
1947 | 262 return retval; |
458 | 263 } |
264 | |
265 // other operations | |
266 | |
267 double | |
268 ColumnVector::min (void) const | |
269 { | |
5275 | 270 octave_idx_type len = length (); |
458 | 271 if (len == 0) |
272 return 0.0; | |
273 | |
274 double res = elem (0); | |
275 | |
5275 | 276 for (octave_idx_type i = 1; i < len; i++) |
458 | 277 if (elem (i) < res) |
278 res = elem (i); | |
279 | |
280 return res; | |
281 } | |
282 | |
283 double | |
284 ColumnVector::max (void) const | |
285 { | |
5275 | 286 octave_idx_type len = length (); |
458 | 287 if (len == 0) |
288 return 0.0; | |
289 | |
290 double res = elem (0); | |
291 | |
5275 | 292 for (octave_idx_type i = 1; i < len; i++) |
458 | 293 if (elem (i) > res) |
294 res = elem (i); | |
295 | |
296 return res; | |
297 } | |
298 | |
3504 | 299 std::ostream& |
300 operator << (std::ostream& os, const ColumnVector& a) | |
458 | 301 { |
302 // int field_width = os.precision () + 7; | |
5275 | 303 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 304 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
305 return os; | |
306 } | |
307 | |
3504 | 308 std::istream& |
309 operator >> (std::istream& is, ColumnVector& a) | |
458 | 310 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14394
diff
changeset
|
311 octave_idx_type len = a.length (); |
458 | 312 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
313 if (len > 0) |
458 | 314 { |
315 double tmp; | |
5275 | 316 for (octave_idx_type i = 0; i < len; i++) |
458 | 317 { |
318 is >> tmp; | |
319 if (is) | |
320 a.elem (i) = tmp; | |
321 else | |
322 break; | |
323 } | |
324 } | |
532 | 325 return is; |
458 | 326 } |