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