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