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