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 { |
1253
|
41 int F77_FCN (dgemv, DGEMV) (const char*, const int&, const int&, |
|
42 const double&, const double*, |
|
43 const int&, const double*, const int&, |
|
44 const double&, double*, const int&, |
|
45 long); |
458
|
46 } |
|
47 |
|
48 /* |
|
49 * Column Vector class. |
|
50 */ |
|
51 |
|
52 int |
|
53 ColumnVector::operator == (const ColumnVector& a) const |
|
54 { |
|
55 int len = length (); |
|
56 if (len != a.length ()) |
|
57 return 0; |
|
58 return equal (data (), a.data (), len); |
|
59 } |
|
60 |
|
61 int |
|
62 ColumnVector::operator != (const ColumnVector& a) const |
|
63 { |
|
64 return !(*this == a); |
|
65 } |
|
66 |
|
67 ColumnVector& |
|
68 ColumnVector::insert (const ColumnVector& a, int r) |
|
69 { |
|
70 int a_len = a.length (); |
|
71 if (r < 0 || r + a_len - 1 > length ()) |
|
72 { |
|
73 (*current_liboctave_error_handler) ("range error for insert"); |
|
74 return *this; |
|
75 } |
|
76 |
|
77 for (int i = 0; i < a_len; i++) |
|
78 elem (r+i) = a.elem (i); |
|
79 |
|
80 return *this; |
|
81 } |
|
82 |
|
83 ColumnVector& |
|
84 ColumnVector::fill (double val) |
|
85 { |
|
86 int len = length (); |
|
87 if (len > 0) |
|
88 for (int i = 0; i < len; i++) |
|
89 elem (i) = val; |
|
90 return *this; |
|
91 } |
|
92 |
|
93 ColumnVector& |
|
94 ColumnVector::fill (double val, int r1, int r2) |
|
95 { |
|
96 int len = length (); |
|
97 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
98 { |
|
99 (*current_liboctave_error_handler) ("range error for fill"); |
|
100 return *this; |
|
101 } |
|
102 |
|
103 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
104 |
|
105 for (int i = r1; i <= r2; i++) |
|
106 elem (i) = val; |
|
107 |
|
108 return *this; |
|
109 } |
|
110 |
|
111 ColumnVector |
|
112 ColumnVector::stack (const ColumnVector& a) const |
|
113 { |
|
114 int len = length (); |
|
115 int nr_insert = len; |
|
116 ColumnVector retval (len + a.length ()); |
|
117 retval.insert (*this, 0); |
|
118 retval.insert (a, nr_insert); |
|
119 return retval; |
|
120 } |
|
121 |
|
122 RowVector |
|
123 ColumnVector::transpose (void) const |
|
124 { |
|
125 int len = length (); |
|
126 return RowVector (dup (data (), len), len); |
|
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 { |
1205
|
213 int nr = m.rows (); |
|
214 int nc = m.cols (); |
|
215 if (nc != a.length ()) |
458
|
216 { |
|
217 (*current_liboctave_error_handler) |
1205
|
218 ("nonconformant matrix multiplication attempted"); |
458
|
219 return ColumnVector (); |
|
220 } |
|
221 |
1205
|
222 if (nr == 0 || nc == 0) |
|
223 return ColumnVector (0); |
458
|
224 |
1205
|
225 int ld = nr; |
|
226 |
|
227 double *y = new double [nr]; |
|
228 |
1253
|
229 F77_FCN (dgemv, DGEMV) ("N", nr, nc, 1.0, m.data (), ld, a.data (), |
|
230 1, 0.0, y, 1, 1L); |
1205
|
231 |
|
232 return ColumnVector (y, nr); |
458
|
233 } |
|
234 |
1205
|
235 // diagonal matrix by column vector -> column vector operations |
|
236 |
|
237 ColumnVector |
|
238 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
239 { |
1205
|
240 int nr = m.rows (); |
|
241 int nc = m.cols (); |
|
242 int a_len = a.length (); |
|
243 if (nc != a_len) |
458
|
244 { |
|
245 (*current_liboctave_error_handler) |
1205
|
246 ("nonconformant matrix multiplication attempted"); |
458
|
247 return ColumnVector (); |
|
248 } |
|
249 |
1205
|
250 if (nc == 0 || nr == 0) |
|
251 return ColumnVector (0); |
|
252 |
|
253 ColumnVector result (nr); |
458
|
254 |
1205
|
255 for (int i = 0; i < a_len; i++) |
|
256 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
257 |
|
258 for (i = a_len; i < nr; i++) |
|
259 result.elem (i) = 0.0; |
|
260 |
|
261 return result; |
458
|
262 } |
|
263 |
|
264 // other operations |
|
265 |
|
266 ColumnVector |
|
267 map (d_d_Mapper f, const ColumnVector& a) |
|
268 { |
|
269 ColumnVector b (a); |
|
270 b.map (f); |
|
271 return b; |
|
272 } |
|
273 |
1205
|
274 ColumnVector |
|
275 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
276 { |
|
277 int a_len = a.length (); |
|
278 ColumnVector b (a_len); |
|
279 for (int i = 0; i < a_len; i++) |
|
280 b.elem (i) = f (a.elem (i)); |
|
281 return b; |
|
282 } |
|
283 |
458
|
284 void |
|
285 ColumnVector::map (d_d_Mapper f) |
|
286 { |
|
287 for (int i = 0; i < length (); i++) |
|
288 elem (i) = f (elem (i)); |
|
289 } |
|
290 |
|
291 double |
|
292 ColumnVector::min (void) const |
|
293 { |
|
294 int len = length (); |
|
295 if (len == 0) |
|
296 return 0.0; |
|
297 |
|
298 double res = elem (0); |
|
299 |
|
300 for (int i = 1; i < len; i++) |
|
301 if (elem (i) < res) |
|
302 res = elem (i); |
|
303 |
|
304 return res; |
|
305 } |
|
306 |
|
307 double |
|
308 ColumnVector::max (void) const |
|
309 { |
|
310 int len = length (); |
|
311 if (len == 0) |
|
312 return 0.0; |
|
313 |
|
314 double res = elem (0); |
|
315 |
|
316 for (int i = 1; i < len; i++) |
|
317 if (elem (i) > res) |
|
318 res = elem (i); |
|
319 |
|
320 return res; |
|
321 } |
|
322 |
|
323 ostream& |
|
324 operator << (ostream& os, const ColumnVector& a) |
|
325 { |
|
326 // int field_width = os.precision () + 7; |
|
327 for (int i = 0; i < a.length (); i++) |
|
328 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
329 return os; |
|
330 } |
|
331 |
|
332 istream& |
|
333 operator >> (istream& is, ColumnVector& a) |
|
334 { |
|
335 int len = a.length(); |
|
336 |
|
337 if (len < 1) |
|
338 is.clear (ios::badbit); |
|
339 else |
|
340 { |
|
341 double tmp; |
|
342 for (int i = 0; i < len; i++) |
|
343 { |
|
344 is >> tmp; |
|
345 if (is) |
|
346 a.elem (i) = tmp; |
|
347 else |
|
348 break; |
|
349 } |
|
350 } |
532
|
351 return is; |
458
|
352 } |
|
353 |
|
354 /* |
|
355 ;;; Local Variables: *** |
|
356 ;;; mode: C++ *** |
|
357 ;;; page-delimiter: "^/\\*" *** |
|
358 ;;; End: *** |
|
359 */ |