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