1993
|
1 // RowVector manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
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 { |
4552
|
44 F77_RET_T |
|
45 F77_FUNC (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL, |
|
46 const int&, const int&, const double&, |
|
47 const double*, const int&, const double*, |
|
48 const int&, const double&, double*, const int& |
|
49 F77_CHAR_ARG_LEN_DECL); |
458
|
50 |
3887
|
51 double F77_FUNC (ddot, DDOT) (const int&, const double*, const int&, |
4552
|
52 const double*, const int&); |
458
|
53 } |
|
54 |
1360
|
55 // Row Vector class. |
458
|
56 |
2386
|
57 bool |
458
|
58 RowVector::operator == (const RowVector& a) const |
|
59 { |
|
60 int len = length (); |
|
61 if (len != a.length ()) |
|
62 return 0; |
3769
|
63 return mx_inline_equal (data (), a.data (), len); |
458
|
64 } |
|
65 |
2386
|
66 bool |
458
|
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 (); |
4316
|
76 |
1699
|
77 if (c < 0 || c + a_len > length ()) |
458
|
78 { |
|
79 (*current_liboctave_error_handler) ("range error for insert"); |
|
80 return *this; |
|
81 } |
|
82 |
4316
|
83 if (a_len > 0) |
|
84 { |
|
85 make_unique (); |
|
86 |
|
87 for (int i = 0; i < a_len; i++) |
|
88 xelem (c+i) = a.elem (i); |
|
89 } |
458
|
90 |
|
91 return *this; |
|
92 } |
|
93 |
|
94 RowVector& |
|
95 RowVector::fill (double val) |
|
96 { |
|
97 int len = length (); |
4316
|
98 |
458
|
99 if (len > 0) |
4316
|
100 { |
|
101 make_unique (); |
|
102 |
|
103 for (int i = 0; i < len; i++) |
|
104 xelem (i) = val; |
|
105 } |
|
106 |
458
|
107 return *this; |
|
108 } |
|
109 |
|
110 RowVector& |
|
111 RowVector::fill (double val, int c1, int c2) |
|
112 { |
|
113 int len = length (); |
4316
|
114 |
458
|
115 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
|
116 { |
|
117 (*current_liboctave_error_handler) ("range error for fill"); |
|
118 return *this; |
|
119 } |
|
120 |
|
121 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
122 |
4316
|
123 if (c2 >= c1) |
|
124 { |
|
125 make_unique (); |
|
126 |
|
127 for (int i = c1; i <= c2; i++) |
|
128 xelem (i) = val; |
|
129 } |
458
|
130 |
|
131 return *this; |
|
132 } |
|
133 |
|
134 RowVector |
|
135 RowVector::append (const RowVector& a) const |
|
136 { |
|
137 int len = length (); |
|
138 int nc_insert = len; |
|
139 RowVector retval (len + a.length ()); |
|
140 retval.insert (*this, 0); |
|
141 retval.insert (a, nc_insert); |
|
142 return retval; |
|
143 } |
|
144 |
|
145 ColumnVector |
|
146 RowVector::transpose (void) const |
|
147 { |
1858
|
148 return ColumnVector (*this); |
458
|
149 } |
|
150 |
|
151 RowVector |
1205
|
152 real (const ComplexRowVector& a) |
|
153 { |
|
154 int a_len = a.length (); |
|
155 RowVector retval; |
|
156 if (a_len > 0) |
3769
|
157 retval = RowVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205
|
158 return retval; |
|
159 } |
|
160 |
|
161 RowVector |
|
162 imag (const ComplexRowVector& a) |
|
163 { |
|
164 int a_len = a.length (); |
|
165 RowVector retval; |
|
166 if (a_len > 0) |
3769
|
167 retval = RowVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205
|
168 return retval; |
|
169 } |
|
170 |
|
171 RowVector |
458
|
172 RowVector::extract (int c1, int c2) const |
|
173 { |
|
174 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
175 |
|
176 int new_c = c2 - c1 + 1; |
|
177 |
|
178 RowVector result (new_c); |
|
179 |
|
180 for (int i = 0; i < new_c; i++) |
4316
|
181 result.xelem (i) = elem (c1+i); |
|
182 |
|
183 return result; |
|
184 } |
|
185 |
|
186 RowVector |
|
187 RowVector::extract_n (int r1, int n) const |
|
188 { |
|
189 RowVector result (n); |
|
190 |
|
191 for (int i = 0; i < n; i++) |
|
192 result.xelem (i) = elem (r1+i); |
458
|
193 |
|
194 return result; |
|
195 } |
|
196 |
|
197 // row vector by matrix -> row vector |
|
198 |
|
199 RowVector |
|
200 operator * (const RowVector& v, const Matrix& a) |
|
201 { |
1947
|
202 RowVector retval; |
|
203 |
458
|
204 int len = v.length (); |
1947
|
205 |
2386
|
206 int a_nr = a.rows (); |
|
207 int a_nc = a.cols (); |
|
208 |
|
209 if (a_nr != len) |
|
210 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); |
1947
|
211 else |
458
|
212 { |
1947
|
213 if (len == 0) |
|
214 retval.resize (a_nc, 0.0); |
|
215 else |
|
216 { |
|
217 // Transpose A to form A'*x == (x'*A)' |
|
218 |
|
219 int ld = a_nr; |
|
220 |
|
221 retval.resize (a_nc); |
|
222 double *y = retval.fortran_vec (); |
|
223 |
4552
|
224 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
|
225 a_nr, a_nc, 1.0, a.data (), |
|
226 ld, v.data (), 1, 0.0, y, 1 |
|
227 F77_CHAR_ARG_LEN (1))); |
1947
|
228 |
|
229 if (f77_exception_encountered) |
|
230 (*current_liboctave_error_handler) |
|
231 ("unrecoverable error in dgemv"); |
|
232 } |
458
|
233 } |
|
234 |
1947
|
235 return retval; |
458
|
236 } |
|
237 |
|
238 // other operations |
|
239 |
|
240 RowVector |
2676
|
241 RowVector::map (d_d_Mapper f) const |
458
|
242 { |
2676
|
243 RowVector b (*this); |
|
244 return b.apply (f); |
458
|
245 } |
|
246 |
2676
|
247 RowVector& |
|
248 RowVector::apply (d_d_Mapper f) |
1205
|
249 { |
2676
|
250 double *d = fortran_vec (); // Ensures only one reference to my privates! |
1205
|
251 |
458
|
252 for (int i = 0; i < length (); i++) |
2676
|
253 d[i] = f (d[i]); |
|
254 |
|
255 return *this; |
458
|
256 } |
|
257 |
|
258 double |
|
259 RowVector::min (void) const |
|
260 { |
|
261 int len = length (); |
|
262 if (len == 0) |
|
263 return 0; |
|
264 |
|
265 double res = elem (0); |
|
266 |
|
267 for (int i = 1; i < len; i++) |
|
268 if (elem (i) < res) |
|
269 res = elem (i); |
|
270 |
|
271 return res; |
|
272 } |
|
273 |
|
274 double |
|
275 RowVector::max (void) const |
|
276 { |
|
277 int len = length (); |
|
278 if (len == 0) |
|
279 return 0; |
|
280 |
|
281 double res = elem (0); |
|
282 |
|
283 for (int i = 1; i < len; i++) |
|
284 if (elem (i) > res) |
|
285 res = elem (i); |
|
286 |
|
287 return res; |
|
288 } |
|
289 |
3504
|
290 std::ostream& |
|
291 operator << (std::ostream& os, const RowVector& a) |
458
|
292 { |
|
293 // int field_width = os.precision () + 7; |
1360
|
294 |
458
|
295 for (int i = 0; i < a.length (); i++) |
|
296 os << " " /* setw (field_width) */ << a.elem (i); |
|
297 return os; |
|
298 } |
|
299 |
3504
|
300 std::istream& |
|
301 operator >> (std::istream& is, RowVector& a) |
458
|
302 { |
|
303 int len = a.length(); |
|
304 |
|
305 if (len < 1) |
3504
|
306 is.clear (std::ios::badbit); |
458
|
307 else |
|
308 { |
|
309 double tmp; |
|
310 for (int i = 0; i < len; i++) |
|
311 { |
|
312 is >> tmp; |
|
313 if (is) |
|
314 a.elem (i) = tmp; |
|
315 else |
|
316 break; |
|
317 } |
|
318 } |
532
|
319 return is; |
458
|
320 } |
|
321 |
1205
|
322 // other operations |
|
323 |
|
324 RowVector |
|
325 linspace (double x1, double x2, int n) |
|
326 { |
|
327 RowVector retval; |
|
328 |
3322
|
329 if (n > 1) |
1205
|
330 { |
|
331 retval.resize (n); |
|
332 double delta = (x2 - x1) / (n - 1); |
|
333 retval.elem (0) = x1; |
|
334 for (int i = 1; i < n-1; i++) |
|
335 retval.elem (i) = x1 + i * delta; |
|
336 retval.elem (n-1) = x2; |
|
337 } |
3322
|
338 else if (n == 1) |
|
339 { |
|
340 if (x1 == x2) |
|
341 { |
|
342 retval.resize (1); |
|
343 retval.elem (0) = x1; |
|
344 } |
|
345 else |
|
346 (*current_liboctave_error_handler) |
|
347 ("linspace: npoints is 1, but x1 != x2"); |
|
348 } |
|
349 else |
|
350 (*current_liboctave_error_handler) |
|
351 ("linspace: npoints must be greater than 0"); |
1205
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
|
356 // row vector by column vector -> scalar |
|
357 |
|
358 double |
|
359 operator * (const RowVector& v, const ColumnVector& a) |
|
360 { |
1947
|
361 double retval = 0.0; |
|
362 |
1205
|
363 int len = v.length (); |
1947
|
364 |
2386
|
365 int a_len = a.length (); |
|
366 |
|
367 if (len != a_len) |
|
368 gripe_nonconformant ("operator *", len, a_len); |
1947
|
369 else if (len != 0) |
3887
|
370 retval = F77_FUNC (ddot, DDOT) (len, v.data (), 1, a.data (), 1); |
1205
|
371 |
1947
|
372 return retval; |
1205
|
373 } |
|
374 |
|
375 Complex |
|
376 operator * (const RowVector& v, const ComplexColumnVector& a) |
|
377 { |
|
378 ComplexRowVector tmp (v); |
|
379 return tmp * a; |
|
380 } |
|
381 |
458
|
382 /* |
|
383 ;;; Local Variables: *** |
|
384 ;;; mode: C++ *** |
|
385 ;;; End: *** |
|
386 */ |