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 |
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 { |
|
126 int len = length (); |
|
127 return RowVector (dup (data (), len), len); |
|
128 } |
|
129 |
1205
|
130 ColumnVector |
|
131 real (const ComplexColumnVector& a) |
|
132 { |
|
133 int a_len = a.length (); |
|
134 ColumnVector retval; |
|
135 if (a_len > 0) |
|
136 retval = ColumnVector (real_dup (a.data (), a_len), a_len); |
|
137 return retval; |
|
138 } |
|
139 |
|
140 ColumnVector |
|
141 imag (const ComplexColumnVector& a) |
|
142 { |
|
143 int a_len = a.length (); |
|
144 ColumnVector retval; |
|
145 if (a_len > 0) |
|
146 retval = ColumnVector (imag_dup (a.data (), a_len), a_len); |
|
147 return retval; |
|
148 } |
|
149 |
458
|
150 // resize is the destructive equivalent for this one |
|
151 |
|
152 ColumnVector |
|
153 ColumnVector::extract (int r1, int r2) const |
|
154 { |
|
155 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
156 |
|
157 int new_r = r2 - r1 + 1; |
|
158 |
|
159 ColumnVector result (new_r); |
|
160 |
|
161 for (int i = 0; i < new_r; i++) |
|
162 result.elem (i) = elem (r1+i); |
|
163 |
|
164 return result; |
|
165 } |
|
166 |
|
167 // column vector by column vector -> column vector operations |
|
168 |
|
169 ColumnVector& |
|
170 ColumnVector::operator += (const ColumnVector& a) |
|
171 { |
|
172 int len = length (); |
|
173 if (len != a.length ()) |
|
174 { |
|
175 (*current_liboctave_error_handler) |
|
176 ("nonconformant vector += operation attempted"); |
890
|
177 return *this; |
458
|
178 } |
|
179 |
|
180 if (len == 0) |
|
181 return *this; |
|
182 |
|
183 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
184 |
|
185 add2 (d, a.data (), len); |
|
186 return *this; |
|
187 } |
|
188 |
|
189 ColumnVector& |
|
190 ColumnVector::operator -= (const ColumnVector& a) |
|
191 { |
|
192 int len = length (); |
|
193 if (len != a.length ()) |
|
194 { |
|
195 (*current_liboctave_error_handler) |
|
196 ("nonconformant vector -= operation attempted"); |
889
|
197 return *this; |
458
|
198 } |
|
199 |
|
200 if (len == 0) |
|
201 return *this; |
|
202 |
|
203 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
204 |
|
205 subtract2 (d, a.data (), len); |
|
206 return *this; |
|
207 } |
|
208 |
1205
|
209 // matrix by column vector -> column vector operations |
458
|
210 |
1205
|
211 ColumnVector |
|
212 operator * (const Matrix& m, const ColumnVector& a) |
458
|
213 { |
1205
|
214 int nr = m.rows (); |
|
215 int nc = m.cols (); |
|
216 if (nc != a.length ()) |
458
|
217 { |
|
218 (*current_liboctave_error_handler) |
1205
|
219 ("nonconformant matrix multiplication attempted"); |
458
|
220 return ColumnVector (); |
|
221 } |
|
222 |
1205
|
223 if (nr == 0 || nc == 0) |
|
224 return ColumnVector (0); |
458
|
225 |
1205
|
226 int ld = nr; |
|
227 |
|
228 double *y = new double [nr]; |
|
229 |
1253
|
230 F77_FCN (dgemv, DGEMV) ("N", nr, nc, 1.0, m.data (), ld, a.data (), |
|
231 1, 0.0, y, 1, 1L); |
1205
|
232 |
|
233 return ColumnVector (y, nr); |
458
|
234 } |
|
235 |
1205
|
236 // diagonal matrix by column vector -> column vector operations |
|
237 |
|
238 ColumnVector |
|
239 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
240 { |
1205
|
241 int nr = m.rows (); |
|
242 int nc = m.cols (); |
|
243 int a_len = a.length (); |
|
244 if (nc != a_len) |
458
|
245 { |
|
246 (*current_liboctave_error_handler) |
1205
|
247 ("nonconformant matrix multiplication attempted"); |
458
|
248 return ColumnVector (); |
|
249 } |
|
250 |
1205
|
251 if (nc == 0 || nr == 0) |
|
252 return ColumnVector (0); |
|
253 |
|
254 ColumnVector result (nr); |
458
|
255 |
1205
|
256 for (int i = 0; i < a_len; i++) |
|
257 result.elem (i) = a.elem (i) * m.elem (i, i); |
|
258 |
1321
|
259 for (int i = a_len; i < nr; i++) |
1205
|
260 result.elem (i) = 0.0; |
|
261 |
|
262 return result; |
458
|
263 } |
|
264 |
|
265 // other operations |
|
266 |
|
267 ColumnVector |
|
268 map (d_d_Mapper f, const ColumnVector& a) |
|
269 { |
|
270 ColumnVector b (a); |
|
271 b.map (f); |
|
272 return b; |
|
273 } |
|
274 |
1205
|
275 ColumnVector |
|
276 map (d_c_Mapper f, const ComplexColumnVector& a) |
|
277 { |
|
278 int a_len = a.length (); |
|
279 ColumnVector b (a_len); |
|
280 for (int i = 0; i < a_len; i++) |
|
281 b.elem (i) = f (a.elem (i)); |
|
282 return b; |
|
283 } |
|
284 |
458
|
285 void |
|
286 ColumnVector::map (d_d_Mapper f) |
|
287 { |
|
288 for (int i = 0; i < length (); i++) |
|
289 elem (i) = f (elem (i)); |
|
290 } |
|
291 |
|
292 double |
|
293 ColumnVector::min (void) const |
|
294 { |
|
295 int len = length (); |
|
296 if (len == 0) |
|
297 return 0.0; |
|
298 |
|
299 double res = elem (0); |
|
300 |
|
301 for (int i = 1; i < len; i++) |
|
302 if (elem (i) < res) |
|
303 res = elem (i); |
|
304 |
|
305 return res; |
|
306 } |
|
307 |
|
308 double |
|
309 ColumnVector::max (void) const |
|
310 { |
|
311 int len = length (); |
|
312 if (len == 0) |
|
313 return 0.0; |
|
314 |
|
315 double res = elem (0); |
|
316 |
|
317 for (int i = 1; i < len; i++) |
|
318 if (elem (i) > res) |
|
319 res = elem (i); |
|
320 |
|
321 return res; |
|
322 } |
|
323 |
|
324 ostream& |
|
325 operator << (ostream& os, const ColumnVector& a) |
|
326 { |
|
327 // int field_width = os.precision () + 7; |
|
328 for (int i = 0; i < a.length (); i++) |
|
329 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
330 return os; |
|
331 } |
|
332 |
|
333 istream& |
|
334 operator >> (istream& is, ColumnVector& a) |
|
335 { |
|
336 int len = a.length(); |
|
337 |
|
338 if (len < 1) |
|
339 is.clear (ios::badbit); |
|
340 else |
|
341 { |
|
342 double tmp; |
|
343 for (int i = 0; i < len; i++) |
|
344 { |
|
345 is >> tmp; |
|
346 if (is) |
|
347 a.elem (i) = tmp; |
|
348 else |
|
349 break; |
|
350 } |
|
351 } |
532
|
352 return is; |
458
|
353 } |
|
354 |
|
355 /* |
|
356 ;;; Local Variables: *** |
|
357 ;;; mode: C++ *** |
|
358 ;;; page-delimiter: "^/\\*" *** |
|
359 ;;; End: *** |
|
360 */ |