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 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
458
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 |
|
30 #include <Complex.h> |
|
31 |
|
32 #include "mx-base.h" |
|
33 #include "mx-inlines.cc" |
|
34 #include "f77-uscore.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 // Fortran functions we call. |
|
38 |
|
39 extern "C" |
|
40 { |
1251
|
41 int F77_FCN (dgemv) (const char*, const int&, const int&, |
|
42 const double&, const double*, const int&, |
|
43 const double*, const int&, const double&, |
|
44 double*, const int&, long); |
458
|
45 } |
|
46 |
|
47 /* |
|
48 * Column Vector class. |
|
49 */ |
|
50 |
|
51 int |
|
52 ColumnVector::operator == (const ColumnVector& a) const |
|
53 { |
|
54 int len = length (); |
|
55 if (len != a.length ()) |
|
56 return 0; |
|
57 return equal (data (), a.data (), len); |
|
58 } |
|
59 |
|
60 int |
|
61 ColumnVector::operator != (const ColumnVector& a) const |
|
62 { |
|
63 return !(*this == a); |
|
64 } |
|
65 |
|
66 ColumnVector& |
|
67 ColumnVector::insert (const ColumnVector& a, int r) |
|
68 { |
|
69 int a_len = a.length (); |
|
70 if (r < 0 || r + a_len - 1 > length ()) |
|
71 { |
|
72 (*current_liboctave_error_handler) ("range error for insert"); |
|
73 return *this; |
|
74 } |
|
75 |
|
76 for (int i = 0; i < a_len; i++) |
|
77 elem (r+i) = a.elem (i); |
|
78 |
|
79 return *this; |
|
80 } |
|
81 |
|
82 ColumnVector& |
|
83 ColumnVector::fill (double val) |
|
84 { |
|
85 int len = length (); |
|
86 if (len > 0) |
|
87 for (int i = 0; i < len; i++) |
|
88 elem (i) = val; |
|
89 return *this; |
|
90 } |
|
91 |
|
92 ColumnVector& |
|
93 ColumnVector::fill (double val, int r1, int r2) |
|
94 { |
|
95 int len = length (); |
|
96 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
97 { |
|
98 (*current_liboctave_error_handler) ("range error for fill"); |
|
99 return *this; |
|
100 } |
|
101 |
|
102 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
103 |
|
104 for (int i = r1; i <= r2; i++) |
|
105 elem (i) = val; |
|
106 |
|
107 return *this; |
|
108 } |
|
109 |
|
110 ColumnVector |
|
111 ColumnVector::stack (const ColumnVector& a) const |
|
112 { |
|
113 int len = length (); |
|
114 int nr_insert = len; |
|
115 ColumnVector retval (len + a.length ()); |
|
116 retval.insert (*this, 0); |
|
117 retval.insert (a, nr_insert); |
|
118 return retval; |
|
119 } |
|
120 |
|
121 RowVector |
|
122 ColumnVector::transpose (void) const |
|
123 { |
|
124 int len = length (); |
|
125 return RowVector (dup (data (), len), len); |
|
126 } |
|
127 |
1205
|
128 ColumnVector |
|
129 real (const ComplexColumnVector& a) |
|
130 { |
|
131 int a_len = a.length (); |
|
132 ColumnVector retval; |
|
133 if (a_len > 0) |
|
134 retval = ColumnVector (real_dup (a.data (), a_len), a_len); |
|
135 return retval; |
|
136 } |
|
137 |
|
138 ColumnVector |
|
139 imag (const ComplexColumnVector& a) |
|
140 { |
|
141 int a_len = a.length (); |
|
142 ColumnVector retval; |
|
143 if (a_len > 0) |
|
144 retval = ColumnVector (imag_dup (a.data (), a_len), a_len); |
|
145 return retval; |
|
146 } |
|
147 |
458
|
148 // resize is the destructive equivalent for this one |
|
149 |
|
150 ColumnVector |
|
151 ColumnVector::extract (int r1, int r2) const |
|
152 { |
|
153 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
154 |
|
155 int new_r = r2 - r1 + 1; |
|
156 |
|
157 ColumnVector result (new_r); |
|
158 |
|
159 for (int i = 0; i < new_r; i++) |
|
160 result.elem (i) = elem (r1+i); |
|
161 |
|
162 return result; |
|
163 } |
|
164 |
|
165 // column vector by column vector -> column vector operations |
|
166 |
|
167 ColumnVector& |
|
168 ColumnVector::operator += (const ColumnVector& a) |
|
169 { |
|
170 int len = length (); |
|
171 if (len != a.length ()) |
|
172 { |
|
173 (*current_liboctave_error_handler) |
|
174 ("nonconformant vector += operation attempted"); |
890
|
175 return *this; |
458
|
176 } |
|
177 |
|
178 if (len == 0) |
|
179 return *this; |
|
180 |
|
181 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
182 |
|
183 add2 (d, a.data (), len); |
|
184 return *this; |
|
185 } |
|
186 |
|
187 ColumnVector& |
|
188 ColumnVector::operator -= (const ColumnVector& a) |
|
189 { |
|
190 int len = length (); |
|
191 if (len != a.length ()) |
|
192 { |
|
193 (*current_liboctave_error_handler) |
|
194 ("nonconformant vector -= operation attempted"); |
889
|
195 return *this; |
458
|
196 } |
|
197 |
|
198 if (len == 0) |
|
199 return *this; |
|
200 |
|
201 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
202 |
|
203 subtract2 (d, a.data (), len); |
|
204 return *this; |
|
205 } |
|
206 |
1205
|
207 // matrix by column vector -> column vector operations |
458
|
208 |
1205
|
209 ColumnVector |
|
210 operator * (const Matrix& m, const ColumnVector& a) |
458
|
211 { |
1205
|
212 int nr = m.rows (); |
|
213 int nc = m.cols (); |
|
214 if (nc != a.length ()) |
458
|
215 { |
|
216 (*current_liboctave_error_handler) |
1205
|
217 ("nonconformant matrix multiplication attempted"); |
458
|
218 return ColumnVector (); |
|
219 } |
|
220 |
1205
|
221 if (nr == 0 || nc == 0) |
|
222 return ColumnVector (0); |
458
|
223 |
1205
|
224 int ld = nr; |
|
225 |
|
226 double *y = new double [nr]; |
|
227 |
1251
|
228 F77_FCN (dgemv) ("N", nr, nc, 1.0, m.data (), ld, a.data (), 1, 0.0, |
|
229 y, 1, 1L); |
1205
|
230 |
|
231 return ColumnVector (y, nr); |
458
|
232 } |
|
233 |
1205
|
234 // diagonal matrix by column vector -> column vector operations |
|
235 |
|
236 ColumnVector |
|
237 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
238 { |
1205
|
239 int nr = m.rows (); |
|
240 int nc = m.cols (); |
|
241 int a_len = a.length (); |
|
242 if (nc != a_len) |
458
|
243 { |
|
244 (*current_liboctave_error_handler) |
1205
|
245 ("nonconformant matrix multiplication attempted"); |
458
|
246 return ColumnVector (); |
|
247 } |
|
248 |
1205
|
249 if (nc == 0 || nr == 0) |
|
250 return ColumnVector (0); |
|
251 |
|
252 ColumnVector result (nr); |
458
|
253 |
1205
|
254 for (int i = 0; i < a_len; i++) |
|
255 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
256 |
|
257 for (i = a_len; i < nr; i++) |
|
258 result.elem (i) = 0.0; |
|
259 |
|
260 return result; |
458
|
261 } |
|
262 |
|
263 // other operations |
|
264 |
|
265 ColumnVector |
|
266 map (d_d_Mapper f, const ColumnVector& a) |
|
267 { |
|
268 ColumnVector b (a); |
|
269 b.map (f); |
|
270 return b; |
|
271 } |
|
272 |
1205
|
273 ColumnVector |
|
274 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
275 { |
|
276 int a_len = a.length (); |
|
277 ColumnVector b (a_len); |
|
278 for (int i = 0; i < a_len; i++) |
|
279 b.elem (i) = f (a.elem (i)); |
|
280 return b; |
|
281 } |
|
282 |
458
|
283 void |
|
284 ColumnVector::map (d_d_Mapper f) |
|
285 { |
|
286 for (int i = 0; i < length (); i++) |
|
287 elem (i) = f (elem (i)); |
|
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 |
|
322 ostream& |
|
323 operator << (ostream& os, const ColumnVector& a) |
|
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 |
|
331 istream& |
|
332 operator >> (istream& is, ColumnVector& a) |
|
333 { |
|
334 int len = a.length(); |
|
335 |
|
336 if (len < 1) |
|
337 is.clear (ios::badbit); |
|
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 ;;; page-delimiter: "^/\\*" *** |
|
357 ;;; End: *** |
|
358 */ |