458
|
1 // ColumnVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
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 { |
1253
|
44 int F77_FCN (dgemv, DGEMV) (const char*, const int&, const int&, |
|
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 |
|
53 int |
|
54 ColumnVector::operator == (const ColumnVector& a) const |
|
55 { |
|
56 int len = length (); |
|
57 if (len != a.length ()) |
|
58 return 0; |
|
59 return equal (data (), a.data (), len); |
|
60 } |
|
61 |
|
62 int |
|
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 (); |
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 |
|
78 for (int i = 0; i < a_len; i++) |
|
79 elem (r+i) = a.elem (i); |
|
80 |
|
81 return *this; |
|
82 } |
|
83 |
|
84 ColumnVector& |
|
85 ColumnVector::fill (double val) |
|
86 { |
|
87 int len = length (); |
|
88 if (len > 0) |
|
89 for (int i = 0; i < len; i++) |
|
90 elem (i) = val; |
|
91 return *this; |
|
92 } |
|
93 |
|
94 ColumnVector& |
|
95 ColumnVector::fill (double val, int r1, int r2) |
|
96 { |
|
97 int len = length (); |
|
98 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
99 { |
|
100 (*current_liboctave_error_handler) ("range error for fill"); |
|
101 return *this; |
|
102 } |
|
103 |
|
104 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
105 |
|
106 for (int i = r1; i <= r2; i++) |
|
107 elem (i) = val; |
|
108 |
|
109 return *this; |
|
110 } |
|
111 |
|
112 ColumnVector |
|
113 ColumnVector::stack (const ColumnVector& a) const |
|
114 { |
|
115 int len = length (); |
|
116 int nr_insert = len; |
|
117 ColumnVector retval (len + a.length ()); |
|
118 retval.insert (*this, 0); |
|
119 retval.insert (a, nr_insert); |
|
120 return retval; |
|
121 } |
|
122 |
|
123 RowVector |
|
124 ColumnVector::transpose (void) const |
|
125 { |
1858
|
126 return RowVector (*this); |
458
|
127 } |
|
128 |
1205
|
129 ColumnVector |
|
130 real (const ComplexColumnVector& a) |
|
131 { |
|
132 int a_len = a.length (); |
|
133 ColumnVector retval; |
|
134 if (a_len > 0) |
|
135 retval = ColumnVector (real_dup (a.data (), a_len), a_len); |
|
136 return retval; |
|
137 } |
|
138 |
|
139 ColumnVector |
|
140 imag (const ComplexColumnVector& a) |
|
141 { |
|
142 int a_len = a.length (); |
|
143 ColumnVector retval; |
|
144 if (a_len > 0) |
|
145 retval = ColumnVector (imag_dup (a.data (), a_len), a_len); |
|
146 return retval; |
|
147 } |
|
148 |
458
|
149 // resize is the destructive equivalent for this one |
|
150 |
|
151 ColumnVector |
|
152 ColumnVector::extract (int r1, int r2) const |
|
153 { |
|
154 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
155 |
|
156 int new_r = r2 - r1 + 1; |
|
157 |
|
158 ColumnVector result (new_r); |
|
159 |
|
160 for (int i = 0; i < new_r; i++) |
|
161 result.elem (i) = elem (r1+i); |
|
162 |
|
163 return result; |
|
164 } |
|
165 |
|
166 // column vector by column vector -> column vector operations |
|
167 |
|
168 ColumnVector& |
|
169 ColumnVector::operator += (const ColumnVector& a) |
|
170 { |
|
171 int len = length (); |
|
172 if (len != a.length ()) |
|
173 { |
|
174 (*current_liboctave_error_handler) |
|
175 ("nonconformant vector += operation attempted"); |
890
|
176 return *this; |
458
|
177 } |
|
178 |
|
179 if (len == 0) |
|
180 return *this; |
|
181 |
|
182 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
183 |
|
184 add2 (d, a.data (), len); |
|
185 return *this; |
|
186 } |
|
187 |
|
188 ColumnVector& |
|
189 ColumnVector::operator -= (const ColumnVector& a) |
|
190 { |
|
191 int len = length (); |
|
192 if (len != a.length ()) |
|
193 { |
|
194 (*current_liboctave_error_handler) |
|
195 ("nonconformant vector -= operation attempted"); |
889
|
196 return *this; |
458
|
197 } |
|
198 |
|
199 if (len == 0) |
|
200 return *this; |
|
201 |
|
202 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
203 |
|
204 subtract2 (d, a.data (), len); |
|
205 return *this; |
|
206 } |
|
207 |
1205
|
208 // matrix by column vector -> column vector operations |
458
|
209 |
1205
|
210 ColumnVector |
|
211 operator * (const Matrix& m, const ColumnVector& a) |
458
|
212 { |
1947
|
213 ColumnVector retval; |
|
214 |
1205
|
215 int nr = m.rows (); |
|
216 int nc = m.cols (); |
1947
|
217 |
1205
|
218 if (nc != a.length ()) |
1947
|
219 (*current_liboctave_error_handler) |
|
220 ("nonconformant matrix multiplication attempted"); |
|
221 else |
458
|
222 { |
1947
|
223 if (nr == 0 || nc == 0) |
|
224 retval.resize (nr, 0.0); |
|
225 else |
|
226 { |
|
227 int ld = nr; |
|
228 |
|
229 retval.resize (nr); |
|
230 double *y = retval.fortran_vec (); |
|
231 |
|
232 F77_XFCN (dgemv, DGEMV, ("N", nr, nc, 1.0, m.data (), ld, |
|
233 a.data (), 1, 0.0, y, 1, 1L)); |
|
234 |
|
235 if (f77_exception_encountered) |
|
236 (*current_liboctave_error_handler) |
|
237 ("unrecoverable error in dgemv"); |
|
238 } |
458
|
239 } |
|
240 |
1947
|
241 return retval; |
458
|
242 } |
|
243 |
1205
|
244 // diagonal matrix by column vector -> column vector operations |
|
245 |
|
246 ColumnVector |
|
247 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
248 { |
1947
|
249 ColumnVector retval; |
|
250 |
1205
|
251 int nr = m.rows (); |
|
252 int nc = m.cols (); |
1947
|
253 |
1205
|
254 int a_len = a.length (); |
1947
|
255 |
1205
|
256 if (nc != a_len) |
1947
|
257 (*current_liboctave_error_handler) |
|
258 ("nonconformant matrix multiplication attempted"); |
|
259 else |
458
|
260 { |
1947
|
261 if (nr == 0 || nc == 0) |
|
262 retval.resize (nr, 0.0); |
|
263 else |
|
264 { |
|
265 retval.resize (nr); |
|
266 |
|
267 for (int i = 0; i < a_len; i++) |
|
268 retval.elem (i) = a.elem (i) * m.elem (i, i); |
|
269 |
|
270 for (int i = a_len; i < nr; i++) |
|
271 retval.elem (i) = 0.0; |
|
272 } |
458
|
273 } |
|
274 |
1947
|
275 return retval; |
458
|
276 } |
|
277 |
|
278 // other operations |
|
279 |
|
280 ColumnVector |
|
281 map (d_d_Mapper f, const ColumnVector& a) |
|
282 { |
|
283 ColumnVector b (a); |
|
284 b.map (f); |
|
285 return b; |
|
286 } |
|
287 |
1205
|
288 ColumnVector |
|
289 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
290 { |
|
291 int a_len = a.length (); |
|
292 ColumnVector b (a_len); |
|
293 for (int i = 0; i < a_len; i++) |
|
294 b.elem (i) = f (a.elem (i)); |
|
295 return b; |
|
296 } |
|
297 |
458
|
298 void |
|
299 ColumnVector::map (d_d_Mapper f) |
|
300 { |
|
301 for (int i = 0; i < length (); i++) |
|
302 elem (i) = f (elem (i)); |
|
303 } |
|
304 |
|
305 double |
|
306 ColumnVector::min (void) const |
|
307 { |
|
308 int len = length (); |
|
309 if (len == 0) |
|
310 return 0.0; |
|
311 |
|
312 double res = elem (0); |
|
313 |
|
314 for (int i = 1; i < len; i++) |
|
315 if (elem (i) < res) |
|
316 res = elem (i); |
|
317 |
|
318 return res; |
|
319 } |
|
320 |
|
321 double |
|
322 ColumnVector::max (void) const |
|
323 { |
|
324 int len = length (); |
|
325 if (len == 0) |
|
326 return 0.0; |
|
327 |
|
328 double res = elem (0); |
|
329 |
|
330 for (int i = 1; i < len; i++) |
|
331 if (elem (i) > res) |
|
332 res = elem (i); |
|
333 |
|
334 return res; |
|
335 } |
|
336 |
|
337 ostream& |
|
338 operator << (ostream& os, const ColumnVector& a) |
|
339 { |
|
340 // int field_width = os.precision () + 7; |
|
341 for (int i = 0; i < a.length (); i++) |
|
342 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
343 return os; |
|
344 } |
|
345 |
|
346 istream& |
|
347 operator >> (istream& is, ColumnVector& a) |
|
348 { |
|
349 int len = a.length(); |
|
350 |
|
351 if (len < 1) |
|
352 is.clear (ios::badbit); |
|
353 else |
|
354 { |
|
355 double tmp; |
|
356 for (int i = 0; i < len; i++) |
|
357 { |
|
358 is >> tmp; |
|
359 if (is) |
|
360 a.elem (i) = tmp; |
|
361 else |
|
362 break; |
|
363 } |
|
364 } |
532
|
365 return is; |
458
|
366 } |
|
367 |
|
368 /* |
|
369 ;;; Local Variables: *** |
|
370 ;;; mode: C++ *** |
|
371 ;;; page-delimiter: "^/\\*" *** |
|
372 ;;; End: *** |
|
373 */ |