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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
33 |
1847
|
34 #include "f77-fcn.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 { |
3887
|
44 int F77_FUNC (dgemv, DGEMV) (const char*, const int&, const int&, |
1253
|
45 const double&, const double*, |
|
46 const int&, const double*, const int&, |
|
47 const double&, double*, const int&, |
|
48 long); |
458
|
49 } |
|
50 |
1360
|
51 // Column Vector class. |
458
|
52 |
2386
|
53 bool |
458
|
54 ColumnVector::operator == (const ColumnVector& a) const |
|
55 { |
|
56 int len = length (); |
|
57 if (len != a.length ()) |
|
58 return 0; |
3769
|
59 return mx_inline_equal (data (), a.data (), len); |
458
|
60 } |
|
61 |
2386
|
62 bool |
458
|
63 ColumnVector::operator != (const ColumnVector& a) const |
|
64 { |
|
65 return !(*this == a); |
|
66 } |
|
67 |
|
68 ColumnVector& |
|
69 ColumnVector::insert (const ColumnVector& a, int r) |
|
70 { |
|
71 int a_len = a.length (); |
4316
|
72 |
1699
|
73 if (r < 0 || r + a_len > length ()) |
458
|
74 { |
|
75 (*current_liboctave_error_handler) ("range error for insert"); |
|
76 return *this; |
|
77 } |
|
78 |
4316
|
79 if (a_len > 0) |
|
80 { |
|
81 make_unique (); |
|
82 |
|
83 for (int i = 0; i < a_len; i++) |
|
84 xelem (r+i) = a.elem (i); |
|
85 } |
458
|
86 |
|
87 return *this; |
|
88 } |
|
89 |
|
90 ColumnVector& |
|
91 ColumnVector::fill (double val) |
|
92 { |
|
93 int len = length (); |
4316
|
94 |
458
|
95 if (len > 0) |
4316
|
96 { |
|
97 make_unique (); |
|
98 |
|
99 for (int i = 0; i < len; i++) |
|
100 xelem (i) = val; |
|
101 } |
|
102 |
458
|
103 return *this; |
|
104 } |
|
105 |
|
106 ColumnVector& |
|
107 ColumnVector::fill (double val, int r1, int r2) |
|
108 { |
|
109 int len = length (); |
4316
|
110 |
458
|
111 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
112 { |
|
113 (*current_liboctave_error_handler) ("range error for fill"); |
|
114 return *this; |
|
115 } |
|
116 |
|
117 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
118 |
4316
|
119 if (r2 >= r1) |
|
120 { |
|
121 make_unique (); |
|
122 |
|
123 for (int i = r1; i <= r2; i++) |
|
124 xelem (i) = val; |
|
125 } |
458
|
126 |
|
127 return *this; |
|
128 } |
|
129 |
|
130 ColumnVector |
|
131 ColumnVector::stack (const ColumnVector& a) const |
|
132 { |
|
133 int len = length (); |
|
134 int nr_insert = len; |
|
135 ColumnVector retval (len + a.length ()); |
|
136 retval.insert (*this, 0); |
|
137 retval.insert (a, nr_insert); |
|
138 return retval; |
|
139 } |
|
140 |
|
141 RowVector |
|
142 ColumnVector::transpose (void) const |
|
143 { |
1858
|
144 return RowVector (*this); |
458
|
145 } |
|
146 |
1205
|
147 ColumnVector |
|
148 real (const ComplexColumnVector& a) |
|
149 { |
|
150 int a_len = a.length (); |
|
151 ColumnVector retval; |
|
152 if (a_len > 0) |
3769
|
153 retval = ColumnVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205
|
154 return retval; |
|
155 } |
|
156 |
|
157 ColumnVector |
|
158 imag (const ComplexColumnVector& a) |
|
159 { |
|
160 int a_len = a.length (); |
|
161 ColumnVector retval; |
|
162 if (a_len > 0) |
3769
|
163 retval = ColumnVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205
|
164 return retval; |
|
165 } |
|
166 |
458
|
167 // resize is the destructive equivalent for this one |
|
168 |
|
169 ColumnVector |
|
170 ColumnVector::extract (int r1, int r2) const |
|
171 { |
|
172 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
173 |
|
174 int new_r = r2 - r1 + 1; |
|
175 |
|
176 ColumnVector result (new_r); |
|
177 |
|
178 for (int i = 0; i < new_r; i++) |
4316
|
179 result.xelem (i) = elem (r1+i); |
|
180 |
|
181 return result; |
|
182 } |
|
183 |
|
184 ColumnVector |
|
185 ColumnVector::extract_n (int r1, int n) const |
|
186 { |
|
187 ColumnVector result (n); |
|
188 |
|
189 for (int i = 0; i < n; i++) |
|
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 |
1205
|
202 int nr = m.rows (); |
|
203 int nc = m.cols (); |
1947
|
204 |
2386
|
205 int a_len = a.length (); |
|
206 |
|
207 if (nc != a_len) |
|
208 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
209 else |
458
|
210 { |
1947
|
211 if (nr == 0 || nc == 0) |
|
212 retval.resize (nr, 0.0); |
|
213 else |
|
214 { |
|
215 int ld = nr; |
|
216 |
|
217 retval.resize (nr); |
|
218 double *y = retval.fortran_vec (); |
|
219 |
|
220 F77_XFCN (dgemv, DGEMV, ("N", nr, nc, 1.0, m.data (), ld, |
|
221 a.data (), 1, 0.0, y, 1, 1L)); |
|
222 |
|
223 if (f77_exception_encountered) |
|
224 (*current_liboctave_error_handler) |
|
225 ("unrecoverable error in dgemv"); |
|
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 |
1205
|
239 int nr = m.rows (); |
|
240 int nc = m.cols (); |
1947
|
241 |
1205
|
242 int 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) |
|
249 retval.resize (nr, 0.0); |
|
250 else |
|
251 { |
|
252 retval.resize (nr); |
|
253 |
|
254 for (int i = 0; i < a_len; i++) |
|
255 retval.elem (i) = a.elem (i) * m.elem (i, i); |
|
256 |
|
257 for (int i = a_len; i < nr; i++) |
|
258 retval.elem (i) = 0.0; |
|
259 } |
458
|
260 } |
|
261 |
1947
|
262 return retval; |
458
|
263 } |
|
264 |
|
265 // other operations |
|
266 |
|
267 ColumnVector |
2676
|
268 ColumnVector::map (d_d_Mapper f) const |
458
|
269 { |
2676
|
270 ColumnVector b (*this); |
|
271 return b.apply (f); |
458
|
272 } |
|
273 |
2676
|
274 ColumnVector& |
|
275 ColumnVector::apply (d_d_Mapper f) |
1205
|
276 { |
2676
|
277 double *d = fortran_vec (); // Ensures only one reference to my privates! |
1205
|
278 |
458
|
279 for (int i = 0; i < length (); i++) |
2676
|
280 d[i] = f (d[i]); |
|
281 |
|
282 return *this; |
458
|
283 } |
|
284 |
|
285 double |
|
286 ColumnVector::min (void) const |
|
287 { |
|
288 int len = length (); |
|
289 if (len == 0) |
|
290 return 0.0; |
|
291 |
|
292 double res = elem (0); |
|
293 |
|
294 for (int i = 1; i < len; i++) |
|
295 if (elem (i) < res) |
|
296 res = elem (i); |
|
297 |
|
298 return res; |
|
299 } |
|
300 |
|
301 double |
|
302 ColumnVector::max (void) const |
|
303 { |
|
304 int len = length (); |
|
305 if (len == 0) |
|
306 return 0.0; |
|
307 |
|
308 double res = elem (0); |
|
309 |
|
310 for (int i = 1; i < len; i++) |
|
311 if (elem (i) > res) |
|
312 res = elem (i); |
|
313 |
|
314 return res; |
|
315 } |
|
316 |
3504
|
317 std::ostream& |
|
318 operator << (std::ostream& os, const ColumnVector& a) |
458
|
319 { |
|
320 // int field_width = os.precision () + 7; |
|
321 for (int i = 0; i < a.length (); i++) |
|
322 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
323 return os; |
|
324 } |
|
325 |
3504
|
326 std::istream& |
|
327 operator >> (std::istream& is, ColumnVector& a) |
458
|
328 { |
|
329 int len = a.length(); |
|
330 |
|
331 if (len < 1) |
3504
|
332 is.clear (std::ios::badbit); |
458
|
333 else |
|
334 { |
|
335 double tmp; |
|
336 for (int i = 0; i < len; i++) |
|
337 { |
|
338 is >> tmp; |
|
339 if (is) |
|
340 a.elem (i) = tmp; |
|
341 else |
|
342 break; |
|
343 } |
|
344 } |
532
|
345 return is; |
458
|
346 } |
|
347 |
|
348 /* |
|
349 ;;; Local Variables: *** |
|
350 ;;; mode: C++ *** |
|
351 ;;; End: *** |
|
352 */ |