458
|
1 // RowVector 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 "lo-error.h" |
|
35 #include "f77-uscore.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 |
1253
|
47 double F77_FCN (ddot, DDOT) (const int&, const double*, const int&, |
|
48 const double*, const int&); |
458
|
49 } |
|
50 |
|
51 /* |
|
52 * Row Vector class. |
|
53 */ |
|
54 |
|
55 int |
|
56 RowVector::operator == (const RowVector& a) const |
|
57 { |
|
58 int len = length (); |
|
59 if (len != a.length ()) |
|
60 return 0; |
|
61 return equal (data (), a.data (), len); |
|
62 } |
|
63 |
|
64 int |
|
65 RowVector::operator != (const RowVector& a) const |
|
66 { |
|
67 return !(*this == a); |
|
68 } |
|
69 |
|
70 RowVector& |
|
71 RowVector::insert (const RowVector& a, int c) |
|
72 { |
|
73 int a_len = a.length (); |
|
74 if (c < 0 || c + a_len - 1 > length ()) |
|
75 { |
|
76 (*current_liboctave_error_handler) ("range error for insert"); |
|
77 return *this; |
|
78 } |
|
79 |
|
80 for (int i = 0; i < a_len; i++) |
|
81 elem (c+i) = a.elem (i); |
|
82 |
|
83 return *this; |
|
84 } |
|
85 |
|
86 RowVector& |
|
87 RowVector::fill (double val) |
|
88 { |
|
89 int len = length (); |
|
90 if (len > 0) |
|
91 for (int i = 0; i < len; i++) |
|
92 elem (i) = val; |
|
93 return *this; |
|
94 } |
|
95 |
|
96 RowVector& |
|
97 RowVector::fill (double val, int c1, int c2) |
|
98 { |
|
99 int len = length (); |
|
100 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
101 { |
|
102 (*current_liboctave_error_handler) ("range error for fill"); |
|
103 return *this; |
|
104 } |
|
105 |
|
106 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
107 |
|
108 for (int i = c1; i <= c2; i++) |
|
109 elem (i) = val; |
|
110 |
|
111 return *this; |
|
112 } |
|
113 |
|
114 RowVector |
|
115 RowVector::append (const RowVector& a) const |
|
116 { |
|
117 int len = length (); |
|
118 int nc_insert = len; |
|
119 RowVector retval (len + a.length ()); |
|
120 retval.insert (*this, 0); |
|
121 retval.insert (a, nc_insert); |
|
122 return retval; |
|
123 } |
|
124 |
|
125 ColumnVector |
|
126 RowVector::transpose (void) const |
|
127 { |
|
128 int len = length (); |
|
129 return ColumnVector (dup (data (), len), len); |
|
130 } |
|
131 |
|
132 RowVector |
1205
|
133 real (const ComplexRowVector& a) |
|
134 { |
|
135 int a_len = a.length (); |
|
136 RowVector retval; |
|
137 if (a_len > 0) |
|
138 retval = RowVector (real_dup (a.data (), a_len), a_len); |
|
139 return retval; |
|
140 } |
|
141 |
|
142 RowVector |
|
143 imag (const ComplexRowVector& a) |
|
144 { |
|
145 int a_len = a.length (); |
|
146 RowVector retval; |
|
147 if (a_len > 0) |
|
148 retval = RowVector (imag_dup (a.data (), a_len), a_len); |
|
149 return retval; |
|
150 } |
|
151 |
|
152 RowVector |
458
|
153 RowVector::extract (int c1, int c2) const |
|
154 { |
|
155 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
156 |
|
157 int new_c = c2 - c1 + 1; |
|
158 |
|
159 RowVector result (new_c); |
|
160 |
|
161 for (int i = 0; i < new_c; i++) |
|
162 result.elem (i) = elem (c1+i); |
|
163 |
|
164 return result; |
|
165 } |
|
166 |
|
167 // row vector by row vector -> row vector operations |
|
168 |
|
169 RowVector& |
|
170 RowVector::operator += (const RowVector& a) |
|
171 { |
|
172 int len = length (); |
|
173 if (len != a.length ()) |
|
174 { |
|
175 (*current_liboctave_error_handler) |
|
176 ("nonconformant vector += operation attempted"); |
|
177 return *this; |
|
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 RowVector& |
|
190 RowVector::operator -= (const RowVector& a) |
|
191 { |
|
192 int len = length (); |
|
193 if (len != a.length ()) |
|
194 { |
|
195 (*current_liboctave_error_handler) |
|
196 ("nonconformant vector -= operation attempted"); |
|
197 return *this; |
|
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 |
|
209 // row vector by matrix -> row vector |
|
210 |
|
211 RowVector |
|
212 operator * (const RowVector& v, const Matrix& a) |
|
213 { |
|
214 int len = v.length (); |
|
215 if (a.rows () != len) |
|
216 { |
|
217 (*current_liboctave_error_handler) |
|
218 ("nonconformant vector multiplication attempted"); |
|
219 return RowVector (); |
|
220 } |
|
221 |
|
222 if (len == 0 || a.cols () == 0) |
|
223 return RowVector (0); |
|
224 |
|
225 // Transpose A to form A'*x == (x'*A)' |
|
226 |
|
227 int a_nr = a.rows (); |
|
228 int a_nc = a.cols (); |
|
229 |
|
230 int ld = a_nr; |
|
231 |
|
232 double *y = new double [len]; |
|
233 |
1253
|
234 F77_FCN (dgemv, DGEMV) ("T", a_nc, a_nr, 1.0, a.data (), ld, |
|
235 v.data (), 1, 0.0, y, 1, 1L); |
458
|
236 |
|
237 return RowVector (y, len); |
|
238 } |
|
239 |
|
240 // other operations |
|
241 |
|
242 RowVector |
|
243 map (d_d_Mapper f, const RowVector& a) |
|
244 { |
|
245 RowVector b (a); |
|
246 b.map (f); |
|
247 return b; |
|
248 } |
|
249 |
1205
|
250 RowVector |
|
251 map (d_c_Mapper f, const ComplexRowVector& a) |
|
252 { |
|
253 int a_len = a.length (); |
|
254 RowVector b (a_len); |
|
255 for (int i = 0; i < a_len; i++) |
|
256 b.elem (i) = f (a.elem (i)); |
|
257 return b; |
|
258 } |
|
259 |
458
|
260 void |
|
261 RowVector::map (d_d_Mapper f) |
|
262 { |
|
263 for (int i = 0; i < length (); i++) |
|
264 elem (i) = f (elem (i)); |
|
265 } |
|
266 |
|
267 double |
|
268 RowVector::min (void) const |
|
269 { |
|
270 int len = length (); |
|
271 if (len == 0) |
|
272 return 0; |
|
273 |
|
274 double res = elem (0); |
|
275 |
|
276 for (int i = 1; i < len; i++) |
|
277 if (elem (i) < res) |
|
278 res = elem (i); |
|
279 |
|
280 return res; |
|
281 } |
|
282 |
|
283 double |
|
284 RowVector::max (void) const |
|
285 { |
|
286 int len = length (); |
|
287 if (len == 0) |
|
288 return 0; |
|
289 |
|
290 double res = elem (0); |
|
291 |
|
292 for (int i = 1; i < len; i++) |
|
293 if (elem (i) > res) |
|
294 res = elem (i); |
|
295 |
|
296 return res; |
|
297 } |
|
298 |
|
299 ostream& |
|
300 operator << (ostream& os, const RowVector& a) |
|
301 { |
|
302 // int field_width = os.precision () + 7; |
|
303 for (int i = 0; i < a.length (); i++) |
|
304 os << " " /* setw (field_width) */ << a.elem (i); |
|
305 return os; |
|
306 } |
|
307 |
|
308 istream& |
|
309 operator >> (istream& is, RowVector& a) |
|
310 { |
|
311 int len = a.length(); |
|
312 |
|
313 if (len < 1) |
|
314 is.clear (ios::badbit); |
|
315 else |
|
316 { |
|
317 double tmp; |
|
318 for (int i = 0; i < len; i++) |
|
319 { |
|
320 is >> tmp; |
|
321 if (is) |
|
322 a.elem (i) = tmp; |
|
323 else |
|
324 break; |
|
325 } |
|
326 } |
532
|
327 return is; |
458
|
328 } |
|
329 |
1205
|
330 // other operations |
|
331 |
|
332 RowVector |
|
333 linspace (double x1, double x2, int n) |
|
334 { |
|
335 RowVector retval; |
|
336 |
|
337 if (n > 0) |
|
338 { |
|
339 retval.resize (n); |
|
340 double delta = (x2 - x1) / (n - 1); |
|
341 retval.elem (0) = x1; |
|
342 for (int i = 1; i < n-1; i++) |
|
343 retval.elem (i) = x1 + i * delta; |
|
344 retval.elem (n-1) = x2; |
|
345 } |
|
346 |
|
347 return retval; |
|
348 } |
|
349 |
|
350 // row vector by column vector -> scalar |
|
351 |
|
352 double |
|
353 operator * (const RowVector& v, const ColumnVector& a) |
|
354 { |
|
355 int len = v.length (); |
|
356 if (len != a.length ()) |
|
357 { |
|
358 (*current_liboctave_error_handler) |
|
359 ("nonconformant vector multiplication attempted"); |
|
360 return 0.0; |
|
361 } |
|
362 |
1253
|
363 return F77_FCN (ddot, DDOT) (len, v.data (), 1, a.data (), 1); |
1205
|
364 } |
|
365 |
|
366 Complex |
|
367 operator * (const RowVector& v, const ComplexColumnVector& a) |
|
368 { |
|
369 ComplexRowVector tmp (v); |
|
370 return tmp * a; |
|
371 } |
|
372 |
458
|
373 /* |
|
374 ;;; Local Variables: *** |
|
375 ;;; mode: C++ *** |
|
376 ;;; page-delimiter: "^/\\*" *** |
|
377 ;;; End: *** |
|
378 */ |