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