458
|
1 // RowVector manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
1253
|
50 double F77_FCN (ddot, DDOT) (const int&, const double*, const int&, |
|
51 const double*, const int&); |
458
|
52 } |
|
53 |
1360
|
54 // Row Vector class. |
458
|
55 |
|
56 int |
|
57 RowVector::operator == (const RowVector& a) const |
|
58 { |
|
59 int len = length (); |
|
60 if (len != a.length ()) |
|
61 return 0; |
|
62 return equal (data (), a.data (), len); |
|
63 } |
|
64 |
|
65 int |
|
66 RowVector::operator != (const RowVector& a) const |
|
67 { |
|
68 return !(*this == a); |
|
69 } |
|
70 |
|
71 RowVector& |
|
72 RowVector::insert (const RowVector& a, int c) |
|
73 { |
|
74 int a_len = a.length (); |
1699
|
75 if (c < 0 || c + a_len > length ()) |
458
|
76 { |
|
77 (*current_liboctave_error_handler) ("range error for insert"); |
|
78 return *this; |
|
79 } |
|
80 |
|
81 for (int i = 0; i < a_len; i++) |
|
82 elem (c+i) = a.elem (i); |
|
83 |
|
84 return *this; |
|
85 } |
|
86 |
|
87 RowVector& |
|
88 RowVector::fill (double val) |
|
89 { |
|
90 int len = length (); |
|
91 if (len > 0) |
|
92 for (int i = 0; i < len; i++) |
|
93 elem (i) = val; |
|
94 return *this; |
|
95 } |
|
96 |
|
97 RowVector& |
|
98 RowVector::fill (double val, int c1, int c2) |
|
99 { |
|
100 int len = length (); |
|
101 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
102 { |
|
103 (*current_liboctave_error_handler) ("range error for fill"); |
|
104 return *this; |
|
105 } |
|
106 |
|
107 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
108 |
|
109 for (int i = c1; i <= c2; i++) |
|
110 elem (i) = val; |
|
111 |
|
112 return *this; |
|
113 } |
|
114 |
|
115 RowVector |
|
116 RowVector::append (const RowVector& a) const |
|
117 { |
|
118 int len = length (); |
|
119 int nc_insert = len; |
|
120 RowVector retval (len + a.length ()); |
|
121 retval.insert (*this, 0); |
|
122 retval.insert (a, nc_insert); |
|
123 return retval; |
|
124 } |
|
125 |
|
126 ColumnVector |
|
127 RowVector::transpose (void) const |
|
128 { |
1858
|
129 return ColumnVector (*this); |
458
|
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 |
1677
|
222 if (len == 0) |
|
223 return RowVector (a.cols (), 0.0); |
458
|
224 |
1360
|
225 // Transpose A to form A'*x == (x'*A)' |
458
|
226 |
|
227 int a_nr = a.rows (); |
|
228 int a_nc = a.cols (); |
|
229 |
|
230 int ld = a_nr; |
|
231 |
1677
|
232 double *y = new double [a_nc]; |
458
|
233 |
1678
|
234 F77_FCN (dgemv, DGEMV) ("T", a_nr, a_nc, 1.0, a.data (), ld, |
1253
|
235 v.data (), 1, 0.0, y, 1, 1L); |
458
|
236 |
1677
|
237 return RowVector (y, a_nc); |
458
|
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; |
1360
|
303 |
458
|
304 for (int i = 0; i < a.length (); i++) |
|
305 os << " " /* setw (field_width) */ << a.elem (i); |
|
306 return os; |
|
307 } |
|
308 |
|
309 istream& |
|
310 operator >> (istream& is, RowVector& a) |
|
311 { |
|
312 int len = a.length(); |
|
313 |
|
314 if (len < 1) |
|
315 is.clear (ios::badbit); |
|
316 else |
|
317 { |
|
318 double tmp; |
|
319 for (int i = 0; i < len; i++) |
|
320 { |
|
321 is >> tmp; |
|
322 if (is) |
|
323 a.elem (i) = tmp; |
|
324 else |
|
325 break; |
|
326 } |
|
327 } |
532
|
328 return is; |
458
|
329 } |
|
330 |
1205
|
331 // other operations |
|
332 |
|
333 RowVector |
|
334 linspace (double x1, double x2, int n) |
|
335 { |
|
336 RowVector retval; |
|
337 |
|
338 if (n > 0) |
|
339 { |
|
340 retval.resize (n); |
|
341 double delta = (x2 - x1) / (n - 1); |
|
342 retval.elem (0) = x1; |
|
343 for (int i = 1; i < n-1; i++) |
|
344 retval.elem (i) = x1 + i * delta; |
|
345 retval.elem (n-1) = x2; |
|
346 } |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
|
351 // row vector by column vector -> scalar |
|
352 |
|
353 double |
|
354 operator * (const RowVector& v, const ColumnVector& a) |
|
355 { |
|
356 int len = v.length (); |
|
357 if (len != a.length ()) |
|
358 { |
|
359 (*current_liboctave_error_handler) |
|
360 ("nonconformant vector multiplication attempted"); |
|
361 return 0.0; |
|
362 } |
|
363 |
1253
|
364 return F77_FCN (ddot, DDOT) (len, v.data (), 1, a.data (), 1); |
1205
|
365 } |
|
366 |
|
367 Complex |
|
368 operator * (const RowVector& v, const ComplexColumnVector& a) |
|
369 { |
|
370 ComplexRowVector tmp (v); |
|
371 return tmp * a; |
|
372 } |
|
373 |
458
|
374 /* |
|
375 ;;; Local Variables: *** |
|
376 ;;; mode: C++ *** |
|
377 ;;; page-delimiter: "^/\\*" *** |
|
378 ;;; End: *** |
|
379 */ |