1993
|
1 // ColumnVector manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
458
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
458
|
29 |
4669
|
30 #include "Array-util.h" |
1847
|
31 #include "f77-fcn.h" |
1368
|
32 #include "lo-error.h" |
458
|
33 #include "mx-base.h" |
|
34 #include "mx-inlines.cc" |
1650
|
35 #include "oct-cmplx.h" |
458
|
36 |
|
37 // Fortran functions we call. |
|
38 |
|
39 extern "C" |
|
40 { |
4552
|
41 F77_RET_T |
|
42 F77_FUNC (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL, |
|
43 const int&, const int&, const double&, |
|
44 const double*, const int&, const double*, |
|
45 const int&, const double&, double*, |
|
46 const int& |
|
47 F77_CHAR_ARG_LEN_DECL); |
458
|
48 } |
|
49 |
1360
|
50 // Column Vector class. |
458
|
51 |
2386
|
52 bool |
458
|
53 ColumnVector::operator == (const ColumnVector& a) const |
|
54 { |
|
55 int len = length (); |
|
56 if (len != a.length ()) |
|
57 return 0; |
3769
|
58 return mx_inline_equal (data (), a.data (), len); |
458
|
59 } |
|
60 |
2386
|
61 bool |
458
|
62 ColumnVector::operator != (const ColumnVector& a) const |
|
63 { |
|
64 return !(*this == a); |
|
65 } |
|
66 |
|
67 ColumnVector& |
|
68 ColumnVector::insert (const ColumnVector& a, int r) |
|
69 { |
|
70 int a_len = a.length (); |
4316
|
71 |
1699
|
72 if (r < 0 || r + a_len > length ()) |
458
|
73 { |
|
74 (*current_liboctave_error_handler) ("range error for insert"); |
|
75 return *this; |
|
76 } |
|
77 |
4316
|
78 if (a_len > 0) |
|
79 { |
|
80 make_unique (); |
|
81 |
|
82 for (int i = 0; i < a_len; i++) |
|
83 xelem (r+i) = a.elem (i); |
|
84 } |
458
|
85 |
|
86 return *this; |
|
87 } |
|
88 |
|
89 ColumnVector& |
|
90 ColumnVector::fill (double val) |
|
91 { |
|
92 int len = length (); |
4316
|
93 |
458
|
94 if (len > 0) |
4316
|
95 { |
|
96 make_unique (); |
|
97 |
|
98 for (int i = 0; i < len; i++) |
|
99 xelem (i) = val; |
|
100 } |
|
101 |
458
|
102 return *this; |
|
103 } |
|
104 |
|
105 ColumnVector& |
|
106 ColumnVector::fill (double val, int r1, int r2) |
|
107 { |
|
108 int len = length (); |
4316
|
109 |
458
|
110 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
111 { |
|
112 (*current_liboctave_error_handler) ("range error for fill"); |
|
113 return *this; |
|
114 } |
|
115 |
|
116 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
117 |
4316
|
118 if (r2 >= r1) |
|
119 { |
|
120 make_unique (); |
|
121 |
|
122 for (int i = r1; i <= r2; i++) |
|
123 xelem (i) = val; |
|
124 } |
458
|
125 |
|
126 return *this; |
|
127 } |
|
128 |
|
129 ColumnVector |
|
130 ColumnVector::stack (const ColumnVector& a) const |
|
131 { |
|
132 int len = length (); |
|
133 int nr_insert = len; |
|
134 ColumnVector retval (len + a.length ()); |
|
135 retval.insert (*this, 0); |
|
136 retval.insert (a, nr_insert); |
|
137 return retval; |
|
138 } |
|
139 |
|
140 RowVector |
|
141 ColumnVector::transpose (void) const |
|
142 { |
1858
|
143 return RowVector (*this); |
458
|
144 } |
|
145 |
1205
|
146 ColumnVector |
|
147 real (const ComplexColumnVector& a) |
|
148 { |
|
149 int a_len = a.length (); |
|
150 ColumnVector retval; |
|
151 if (a_len > 0) |
3769
|
152 retval = ColumnVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205
|
153 return retval; |
|
154 } |
|
155 |
|
156 ColumnVector |
|
157 imag (const ComplexColumnVector& a) |
|
158 { |
|
159 int a_len = a.length (); |
|
160 ColumnVector retval; |
|
161 if (a_len > 0) |
3769
|
162 retval = ColumnVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205
|
163 return retval; |
|
164 } |
|
165 |
458
|
166 // resize is the destructive equivalent for this one |
|
167 |
|
168 ColumnVector |
|
169 ColumnVector::extract (int r1, int r2) const |
|
170 { |
|
171 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
172 |
|
173 int new_r = r2 - r1 + 1; |
|
174 |
|
175 ColumnVector result (new_r); |
|
176 |
|
177 for (int i = 0; i < new_r; i++) |
4316
|
178 result.xelem (i) = elem (r1+i); |
|
179 |
|
180 return result; |
|
181 } |
|
182 |
|
183 ColumnVector |
|
184 ColumnVector::extract_n (int r1, int n) const |
|
185 { |
|
186 ColumnVector result (n); |
|
187 |
|
188 for (int i = 0; i < n; i++) |
|
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 |
1205
|
201 int nr = m.rows (); |
|
202 int nc = m.cols (); |
1947
|
203 |
2386
|
204 int a_len = a.length (); |
|
205 |
|
206 if (nc != a_len) |
|
207 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
208 else |
458
|
209 { |
1947
|
210 if (nr == 0 || nc == 0) |
|
211 retval.resize (nr, 0.0); |
|
212 else |
|
213 { |
|
214 int ld = nr; |
|
215 |
|
216 retval.resize (nr); |
|
217 double *y = retval.fortran_vec (); |
|
218 |
4552
|
219 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
220 nr, nc, 1.0, m.data (), ld, |
|
221 a.data (), 1, 0.0, y, 1 |
|
222 F77_CHAR_ARG_LEN (1))); |
1947
|
223 |
|
224 if (f77_exception_encountered) |
|
225 (*current_liboctave_error_handler) |
|
226 ("unrecoverable error in dgemv"); |
|
227 } |
458
|
228 } |
|
229 |
1947
|
230 return retval; |
458
|
231 } |
|
232 |
1205
|
233 // diagonal matrix by column vector -> column vector operations |
|
234 |
|
235 ColumnVector |
|
236 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
237 { |
1947
|
238 ColumnVector retval; |
|
239 |
1205
|
240 int nr = m.rows (); |
|
241 int nc = m.cols (); |
1947
|
242 |
1205
|
243 int a_len = a.length (); |
1947
|
244 |
1205
|
245 if (nc != a_len) |
2386
|
246 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
247 else |
458
|
248 { |
1947
|
249 if (nr == 0 || nc == 0) |
|
250 retval.resize (nr, 0.0); |
|
251 else |
|
252 { |
|
253 retval.resize (nr); |
|
254 |
|
255 for (int i = 0; i < a_len; i++) |
|
256 retval.elem (i) = a.elem (i) * m.elem (i, i); |
|
257 |
|
258 for (int i = a_len; i < nr; i++) |
|
259 retval.elem (i) = 0.0; |
|
260 } |
458
|
261 } |
|
262 |
1947
|
263 return retval; |
458
|
264 } |
|
265 |
|
266 // other operations |
|
267 |
|
268 ColumnVector |
2676
|
269 ColumnVector::map (d_d_Mapper f) const |
458
|
270 { |
2676
|
271 ColumnVector b (*this); |
|
272 return b.apply (f); |
458
|
273 } |
|
274 |
2676
|
275 ColumnVector& |
|
276 ColumnVector::apply (d_d_Mapper f) |
1205
|
277 { |
2676
|
278 double *d = fortran_vec (); // Ensures only one reference to my privates! |
1205
|
279 |
458
|
280 for (int i = 0; i < length (); i++) |
2676
|
281 d[i] = f (d[i]); |
|
282 |
|
283 return *this; |
458
|
284 } |
|
285 |
|
286 double |
|
287 ColumnVector::min (void) const |
|
288 { |
|
289 int len = length (); |
|
290 if (len == 0) |
|
291 return 0.0; |
|
292 |
|
293 double res = elem (0); |
|
294 |
|
295 for (int i = 1; i < len; i++) |
|
296 if (elem (i) < res) |
|
297 res = elem (i); |
|
298 |
|
299 return res; |
|
300 } |
|
301 |
|
302 double |
|
303 ColumnVector::max (void) const |
|
304 { |
|
305 int len = length (); |
|
306 if (len == 0) |
|
307 return 0.0; |
|
308 |
|
309 double res = elem (0); |
|
310 |
|
311 for (int i = 1; i < len; i++) |
|
312 if (elem (i) > res) |
|
313 res = elem (i); |
|
314 |
|
315 return res; |
|
316 } |
|
317 |
3504
|
318 std::ostream& |
|
319 operator << (std::ostream& os, const ColumnVector& a) |
458
|
320 { |
|
321 // int field_width = os.precision () + 7; |
|
322 for (int i = 0; i < a.length (); i++) |
|
323 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
324 return os; |
|
325 } |
|
326 |
3504
|
327 std::istream& |
|
328 operator >> (std::istream& is, ColumnVector& a) |
458
|
329 { |
|
330 int len = a.length(); |
|
331 |
|
332 if (len < 1) |
3504
|
333 is.clear (std::ios::badbit); |
458
|
334 else |
|
335 { |
|
336 double tmp; |
|
337 for (int i = 0; i < len; i++) |
|
338 { |
|
339 is >> tmp; |
|
340 if (is) |
|
341 a.elem (i) = tmp; |
|
342 else |
|
343 break; |
|
344 } |
|
345 } |
532
|
346 return is; |
458
|
347 } |
|
348 |
|
349 /* |
|
350 ;;; Local Variables: *** |
|
351 ;;; mode: C++ *** |
|
352 ;;; End: *** |
|
353 */ |