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