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