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 { |
1205
|
41 int F77_FCN (dgemv) (const char*, const int*, const int*, |
|
42 const double*, const double*, const int*, |
458
|
43 const double*, const int*, const double*, |
1205
|
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 char trans = 'N'; |
|
225 int ld = nr; |
|
226 double alpha = 1.0; |
|
227 double beta = 0.0; |
|
228 int i_one = 1; |
|
229 |
|
230 double *y = new double [nr]; |
|
231 |
|
232 F77_FCN (dgemv) (&trans, &nr, &nc, &alpha, m.data (), &ld, a.data (), |
|
233 &i_one, &beta, y, &i_one, 1L); |
|
234 |
|
235 return ColumnVector (y, nr); |
458
|
236 } |
|
237 |
1205
|
238 // diagonal matrix by column vector -> column vector operations |
|
239 |
|
240 ColumnVector |
|
241 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
242 { |
1205
|
243 int nr = m.rows (); |
|
244 int nc = m.cols (); |
|
245 int a_len = a.length (); |
|
246 if (nc != a_len) |
458
|
247 { |
|
248 (*current_liboctave_error_handler) |
1205
|
249 ("nonconformant matrix multiplication attempted"); |
458
|
250 return ColumnVector (); |
|
251 } |
|
252 |
1205
|
253 if (nc == 0 || nr == 0) |
|
254 return ColumnVector (0); |
|
255 |
|
256 ColumnVector result (nr); |
458
|
257 |
1205
|
258 for (int i = 0; i < a_len; i++) |
|
259 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
260 |
|
261 for (i = a_len; i < nr; i++) |
|
262 result.elem (i) = 0.0; |
|
263 |
|
264 return result; |
458
|
265 } |
|
266 |
|
267 // other operations |
|
268 |
|
269 ColumnVector |
|
270 map (d_d_Mapper f, const ColumnVector& a) |
|
271 { |
|
272 ColumnVector b (a); |
|
273 b.map (f); |
|
274 return b; |
|
275 } |
|
276 |
1205
|
277 ColumnVector |
|
278 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
279 { |
|
280 int a_len = a.length (); |
|
281 ColumnVector b (a_len); |
|
282 for (int i = 0; i < a_len; i++) |
|
283 b.elem (i) = f (a.elem (i)); |
|
284 return b; |
|
285 } |
|
286 |
458
|
287 void |
|
288 ColumnVector::map (d_d_Mapper f) |
|
289 { |
|
290 for (int i = 0; i < length (); i++) |
|
291 elem (i) = f (elem (i)); |
|
292 } |
|
293 |
|
294 double |
|
295 ColumnVector::min (void) const |
|
296 { |
|
297 int len = length (); |
|
298 if (len == 0) |
|
299 return 0.0; |
|
300 |
|
301 double res = elem (0); |
|
302 |
|
303 for (int i = 1; i < len; i++) |
|
304 if (elem (i) < res) |
|
305 res = elem (i); |
|
306 |
|
307 return res; |
|
308 } |
|
309 |
|
310 double |
|
311 ColumnVector::max (void) const |
|
312 { |
|
313 int len = length (); |
|
314 if (len == 0) |
|
315 return 0.0; |
|
316 |
|
317 double res = elem (0); |
|
318 |
|
319 for (int i = 1; i < len; i++) |
|
320 if (elem (i) > res) |
|
321 res = elem (i); |
|
322 |
|
323 return res; |
|
324 } |
|
325 |
|
326 ostream& |
|
327 operator << (ostream& os, const ColumnVector& a) |
|
328 { |
|
329 // int field_width = os.precision () + 7; |
|
330 for (int i = 0; i < a.length (); i++) |
|
331 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
332 return os; |
|
333 } |
|
334 |
|
335 istream& |
|
336 operator >> (istream& is, ColumnVector& a) |
|
337 { |
|
338 int len = a.length(); |
|
339 |
|
340 if (len < 1) |
|
341 is.clear (ios::badbit); |
|
342 else |
|
343 { |
|
344 double tmp; |
|
345 for (int i = 0; i < len; i++) |
|
346 { |
|
347 is >> tmp; |
|
348 if (is) |
|
349 a.elem (i) = tmp; |
|
350 else |
|
351 break; |
|
352 } |
|
353 } |
532
|
354 return is; |
458
|
355 } |
|
356 |
|
357 /* |
|
358 ;;; Local Variables: *** |
|
359 ;;; mode: C++ *** |
|
360 ;;; page-delimiter: "^/\\*" *** |
|
361 ;;; End: *** |
|
362 */ |