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 |
1368
|
34 #include "f77-uscore.h" |
|
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 (); |
|
75 if (c < 0 || c + a_len - 1 > length ()) |
|
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 { |
|
129 int len = length (); |
|
130 return ColumnVector (dup (data (), len), len); |
|
131 } |
|
132 |
|
133 RowVector |
1205
|
134 real (const ComplexRowVector& a) |
|
135 { |
|
136 int a_len = a.length (); |
|
137 RowVector retval; |
|
138 if (a_len > 0) |
|
139 retval = RowVector (real_dup (a.data (), a_len), a_len); |
|
140 return retval; |
|
141 } |
|
142 |
|
143 RowVector |
|
144 imag (const ComplexRowVector& a) |
|
145 { |
|
146 int a_len = a.length (); |
|
147 RowVector retval; |
|
148 if (a_len > 0) |
|
149 retval = RowVector (imag_dup (a.data (), a_len), a_len); |
|
150 return retval; |
|
151 } |
|
152 |
|
153 RowVector |
458
|
154 RowVector::extract (int c1, int c2) const |
|
155 { |
|
156 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
157 |
|
158 int new_c = c2 - c1 + 1; |
|
159 |
|
160 RowVector result (new_c); |
|
161 |
|
162 for (int i = 0; i < new_c; i++) |
|
163 result.elem (i) = elem (c1+i); |
|
164 |
|
165 return result; |
|
166 } |
|
167 |
|
168 // row vector by row vector -> row vector operations |
|
169 |
|
170 RowVector& |
|
171 RowVector::operator += (const RowVector& a) |
|
172 { |
|
173 int len = length (); |
|
174 if (len != a.length ()) |
|
175 { |
|
176 (*current_liboctave_error_handler) |
|
177 ("nonconformant vector += operation attempted"); |
|
178 return *this; |
|
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 RowVector& |
|
191 RowVector::operator -= (const RowVector& a) |
|
192 { |
|
193 int len = length (); |
|
194 if (len != a.length ()) |
|
195 { |
|
196 (*current_liboctave_error_handler) |
|
197 ("nonconformant vector -= operation attempted"); |
|
198 return *this; |
|
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 |
|
210 // row vector by matrix -> row vector |
|
211 |
|
212 RowVector |
|
213 operator * (const RowVector& v, const Matrix& a) |
|
214 { |
|
215 int len = v.length (); |
|
216 if (a.rows () != len) |
|
217 { |
|
218 (*current_liboctave_error_handler) |
|
219 ("nonconformant vector multiplication attempted"); |
|
220 return RowVector (); |
|
221 } |
|
222 |
1677
|
223 if (len == 0) |
|
224 return RowVector (a.cols (), 0.0); |
458
|
225 |
1360
|
226 // Transpose A to form A'*x == (x'*A)' |
458
|
227 |
|
228 int a_nr = a.rows (); |
|
229 int a_nc = a.cols (); |
|
230 |
|
231 int ld = a_nr; |
|
232 |
1677
|
233 double *y = new double [a_nc]; |
458
|
234 |
1678
|
235 F77_FCN (dgemv, DGEMV) ("T", a_nr, a_nc, 1.0, a.data (), ld, |
1253
|
236 v.data (), 1, 0.0, y, 1, 1L); |
458
|
237 |
1677
|
238 return RowVector (y, a_nc); |
458
|
239 } |
|
240 |
|
241 // other operations |
|
242 |
|
243 RowVector |
|
244 map (d_d_Mapper f, const RowVector& a) |
|
245 { |
|
246 RowVector b (a); |
|
247 b.map (f); |
|
248 return b; |
|
249 } |
|
250 |
1205
|
251 RowVector |
|
252 map (d_c_Mapper f, const ComplexRowVector& a) |
|
253 { |
|
254 int a_len = a.length (); |
|
255 RowVector b (a_len); |
|
256 for (int i = 0; i < a_len; i++) |
|
257 b.elem (i) = f (a.elem (i)); |
|
258 return b; |
|
259 } |
|
260 |
458
|
261 void |
|
262 RowVector::map (d_d_Mapper f) |
|
263 { |
|
264 for (int i = 0; i < length (); i++) |
|
265 elem (i) = f (elem (i)); |
|
266 } |
|
267 |
|
268 double |
|
269 RowVector::min (void) const |
|
270 { |
|
271 int len = length (); |
|
272 if (len == 0) |
|
273 return 0; |
|
274 |
|
275 double res = elem (0); |
|
276 |
|
277 for (int i = 1; i < len; i++) |
|
278 if (elem (i) < res) |
|
279 res = elem (i); |
|
280 |
|
281 return res; |
|
282 } |
|
283 |
|
284 double |
|
285 RowVector::max (void) const |
|
286 { |
|
287 int len = length (); |
|
288 if (len == 0) |
|
289 return 0; |
|
290 |
|
291 double res = elem (0); |
|
292 |
|
293 for (int i = 1; i < len; i++) |
|
294 if (elem (i) > res) |
|
295 res = elem (i); |
|
296 |
|
297 return res; |
|
298 } |
|
299 |
|
300 ostream& |
|
301 operator << (ostream& os, const RowVector& a) |
|
302 { |
|
303 // int field_width = os.precision () + 7; |
1360
|
304 |
458
|
305 for (int i = 0; i < a.length (); i++) |
|
306 os << " " /* setw (field_width) */ << a.elem (i); |
|
307 return os; |
|
308 } |
|
309 |
|
310 istream& |
|
311 operator >> (istream& is, RowVector& a) |
|
312 { |
|
313 int len = a.length(); |
|
314 |
|
315 if (len < 1) |
|
316 is.clear (ios::badbit); |
|
317 else |
|
318 { |
|
319 double tmp; |
|
320 for (int i = 0; i < len; i++) |
|
321 { |
|
322 is >> tmp; |
|
323 if (is) |
|
324 a.elem (i) = tmp; |
|
325 else |
|
326 break; |
|
327 } |
|
328 } |
532
|
329 return is; |
458
|
330 } |
|
331 |
1205
|
332 // other operations |
|
333 |
|
334 RowVector |
|
335 linspace (double x1, double x2, int n) |
|
336 { |
|
337 RowVector retval; |
|
338 |
|
339 if (n > 0) |
|
340 { |
|
341 retval.resize (n); |
|
342 double delta = (x2 - x1) / (n - 1); |
|
343 retval.elem (0) = x1; |
|
344 for (int i = 1; i < n-1; i++) |
|
345 retval.elem (i) = x1 + i * delta; |
|
346 retval.elem (n-1) = x2; |
|
347 } |
|
348 |
|
349 return retval; |
|
350 } |
|
351 |
|
352 // row vector by column vector -> scalar |
|
353 |
|
354 double |
|
355 operator * (const RowVector& v, const ColumnVector& a) |
|
356 { |
|
357 int len = v.length (); |
|
358 if (len != a.length ()) |
|
359 { |
|
360 (*current_liboctave_error_handler) |
|
361 ("nonconformant vector multiplication attempted"); |
|
362 return 0.0; |
|
363 } |
|
364 |
1253
|
365 return F77_FCN (ddot, DDOT) (len, v.data (), 1, a.data (), 1); |
1205
|
366 } |
|
367 |
|
368 Complex |
|
369 operator * (const RowVector& v, const ComplexColumnVector& a) |
|
370 { |
|
371 ComplexRowVector tmp (v); |
|
372 return tmp * a; |
|
373 } |
|
374 |
458
|
375 /* |
|
376 ;;; Local Variables: *** |
|
377 ;;; mode: C++ *** |
|
378 ;;; page-delimiter: "^/\\*" *** |
|
379 ;;; End: *** |
|
380 */ |