Mercurial > hg > octave-lyh
annotate liboctave/dColVector.cc @ 10396:a0b51ac0f88a
optimize accumdim with summation
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 05 Mar 2010 12:31:30 +0100 |
parents | a0728e81ed25 |
children | 4d1fc073fbb7 |
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 |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
149 ColumnVector::abs (void) const |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
150 { |
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
151 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
|
152 } |
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 ColumnVector |
1205 | 155 real (const ComplexColumnVector& a) |
156 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
157 return do_mx_unary_op<double, Complex> (a, mx_inline_real); |
1205 | 158 } |
159 | |
160 ColumnVector | |
161 imag (const ComplexColumnVector& a) | |
162 { | |
10363
a0728e81ed25
improve diag matrix interface & implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
163 return do_mx_unary_op<double, Complex> (a, mx_inline_imag); |
1205 | 164 } |
165 | |
458 | 166 // resize is the destructive equivalent for this one |
167 | |
168 ColumnVector | |
5275 | 169 ColumnVector::extract (octave_idx_type r1, octave_idx_type r2) const |
458 | 170 { |
5275 | 171 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
458 | 172 |
5275 | 173 octave_idx_type new_r = r2 - r1 + 1; |
458 | 174 |
175 ColumnVector result (new_r); | |
176 | |
5275 | 177 for (octave_idx_type i = 0; i < new_r; i++) |
4316 | 178 result.xelem (i) = elem (r1+i); |
179 | |
180 return result; | |
181 } | |
182 | |
183 ColumnVector | |
5275 | 184 ColumnVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 185 { |
186 ColumnVector result (n); | |
187 | |
5275 | 188 for (octave_idx_type i = 0; i < n; i++) |
4316 | 189 result.xelem (i) = elem (r1+i); |
458 | 190 |
191 return result; | |
192 } | |
193 | |
1205 | 194 // matrix by column vector -> column vector operations |
458 | 195 |
1205 | 196 ColumnVector |
197 operator * (const Matrix& m, const ColumnVector& a) | |
458 | 198 { |
1947 | 199 ColumnVector retval; |
200 | |
5275 | 201 octave_idx_type nr = m.rows (); |
202 octave_idx_type nc = m.cols (); | |
1947 | 203 |
5275 | 204 octave_idx_type a_len = a.length (); |
2386 | 205 |
206 if (nc != a_len) | |
207 gripe_nonconformant ("operator *", nr, nc, a_len, 1); | |
1947 | 208 else |
458 | 209 { |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
210 retval.clear (nr); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
211 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
212 if (nr != 0) |
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 double *y = retval.fortran_vec (); |
1947 | 215 |
9625
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
216 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
217 nr, nc, 1.0, m.data (), nr, |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
218 a.data (), 1, 0.0, y, 1 |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
219 F77_CHAR_ARG_LEN (1))); |
cbabf50315ca
optimize Matrix*ColumnVector
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
220 } |
458 | 221 } |
222 | |
1947 | 223 return retval; |
458 | 224 } |
225 | |
1205 | 226 // diagonal matrix by column vector -> column vector operations |
227 | |
228 ColumnVector | |
229 operator * (const DiagMatrix& m, const ColumnVector& a) | |
458 | 230 { |
1947 | 231 ColumnVector retval; |
232 | |
5275 | 233 octave_idx_type nr = m.rows (); |
234 octave_idx_type nc = m.cols (); | |
1947 | 235 |
5275 | 236 octave_idx_type a_len = a.length (); |
1947 | 237 |
1205 | 238 if (nc != a_len) |
2386 | 239 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947 | 240 else |
458 | 241 { |
1947 | 242 if (nr == 0 || nc == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
243 retval.resize (nr, 0.0); |
1947 | 244 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
245 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
246 retval.resize (nr); |
1947 | 247 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
248 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
|
249 retval.elem (i) = a.elem (i) * m.elem (i, i); |
1947 | 250 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
251 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
|
252 retval.elem (i) = 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
253 } |
458 | 254 } |
255 | |
1947 | 256 return retval; |
458 | 257 } |
258 | |
259 // other operations | |
260 | |
261 double | |
262 ColumnVector::min (void) const | |
263 { | |
5275 | 264 octave_idx_type len = length (); |
458 | 265 if (len == 0) |
266 return 0.0; | |
267 | |
268 double res = elem (0); | |
269 | |
5275 | 270 for (octave_idx_type i = 1; i < len; i++) |
458 | 271 if (elem (i) < res) |
272 res = elem (i); | |
273 | |
274 return res; | |
275 } | |
276 | |
277 double | |
278 ColumnVector::max (void) const | |
279 { | |
5275 | 280 octave_idx_type len = length (); |
458 | 281 if (len == 0) |
282 return 0.0; | |
283 | |
284 double res = elem (0); | |
285 | |
5275 | 286 for (octave_idx_type i = 1; i < len; i++) |
458 | 287 if (elem (i) > res) |
288 res = elem (i); | |
289 | |
290 return res; | |
291 } | |
292 | |
3504 | 293 std::ostream& |
294 operator << (std::ostream& os, const ColumnVector& a) | |
458 | 295 { |
296 // int field_width = os.precision () + 7; | |
5275 | 297 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 298 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
299 return os; | |
300 } | |
301 | |
3504 | 302 std::istream& |
303 operator >> (std::istream& is, ColumnVector& a) | |
458 | 304 { |
5275 | 305 octave_idx_type len = a.length(); |
458 | 306 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
307 if (len > 0) |
458 | 308 { |
309 double tmp; | |
5275 | 310 for (octave_idx_type i = 0; i < len; i++) |
458 | 311 { |
312 is >> tmp; | |
313 if (is) | |
314 a.elem (i) = tmp; | |
315 else | |
316 break; | |
317 } | |
318 } | |
532 | 319 return is; |
458 | 320 } |