1993
|
1 // ColumnVector 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 { |
3887
|
44 int F77_FUNC (dgemv, DGEMV) (const char*, const int&, const int&, |
1253
|
45 const double&, const double*, |
|
46 const int&, const double*, const int&, |
|
47 const double&, double*, const int&, |
|
48 long); |
458
|
49 } |
|
50 |
1360
|
51 // Column Vector class. |
458
|
52 |
2386
|
53 bool |
458
|
54 ColumnVector::operator == (const ColumnVector& a) const |
|
55 { |
|
56 int len = length (); |
|
57 if (len != a.length ()) |
|
58 return 0; |
3769
|
59 return mx_inline_equal (data (), a.data (), len); |
458
|
60 } |
|
61 |
2386
|
62 bool |
458
|
63 ColumnVector::operator != (const ColumnVector& a) const |
|
64 { |
|
65 return !(*this == a); |
|
66 } |
|
67 |
|
68 ColumnVector& |
|
69 ColumnVector::insert (const ColumnVector& a, int r) |
|
70 { |
|
71 int a_len = a.length (); |
1699
|
72 if (r < 0 || r + a_len > length ()) |
458
|
73 { |
|
74 (*current_liboctave_error_handler) ("range error for insert"); |
|
75 return *this; |
|
76 } |
|
77 |
|
78 for (int i = 0; i < a_len; i++) |
|
79 elem (r+i) = a.elem (i); |
|
80 |
|
81 return *this; |
|
82 } |
|
83 |
|
84 ColumnVector& |
|
85 ColumnVector::fill (double val) |
|
86 { |
|
87 int len = length (); |
|
88 if (len > 0) |
|
89 for (int i = 0; i < len; i++) |
|
90 elem (i) = val; |
|
91 return *this; |
|
92 } |
|
93 |
|
94 ColumnVector& |
|
95 ColumnVector::fill (double val, int r1, int r2) |
|
96 { |
|
97 int len = length (); |
|
98 if (r1 < 0 || r2 < 0 || r1 >= len || r2 >= len) |
|
99 { |
|
100 (*current_liboctave_error_handler) ("range error for fill"); |
|
101 return *this; |
|
102 } |
|
103 |
|
104 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
105 |
|
106 for (int i = r1; i <= r2; i++) |
|
107 elem (i) = val; |
|
108 |
|
109 return *this; |
|
110 } |
|
111 |
|
112 ColumnVector |
|
113 ColumnVector::stack (const ColumnVector& a) const |
|
114 { |
|
115 int len = length (); |
|
116 int nr_insert = len; |
|
117 ColumnVector retval (len + a.length ()); |
|
118 retval.insert (*this, 0); |
|
119 retval.insert (a, nr_insert); |
|
120 return retval; |
|
121 } |
|
122 |
|
123 RowVector |
|
124 ColumnVector::transpose (void) const |
|
125 { |
1858
|
126 return RowVector (*this); |
458
|
127 } |
|
128 |
1205
|
129 ColumnVector |
|
130 real (const ComplexColumnVector& a) |
|
131 { |
|
132 int a_len = a.length (); |
|
133 ColumnVector retval; |
|
134 if (a_len > 0) |
3769
|
135 retval = ColumnVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205
|
136 return retval; |
|
137 } |
|
138 |
|
139 ColumnVector |
|
140 imag (const ComplexColumnVector& a) |
|
141 { |
|
142 int a_len = a.length (); |
|
143 ColumnVector retval; |
|
144 if (a_len > 0) |
3769
|
145 retval = ColumnVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205
|
146 return retval; |
|
147 } |
|
148 |
458
|
149 // resize is the destructive equivalent for this one |
|
150 |
|
151 ColumnVector |
|
152 ColumnVector::extract (int r1, int r2) const |
|
153 { |
|
154 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
155 |
|
156 int new_r = r2 - r1 + 1; |
|
157 |
|
158 ColumnVector result (new_r); |
|
159 |
|
160 for (int i = 0; i < new_r; i++) |
|
161 result.elem (i) = elem (r1+i); |
|
162 |
|
163 return result; |
|
164 } |
|
165 |
1205
|
166 // matrix by column vector -> column vector operations |
458
|
167 |
1205
|
168 ColumnVector |
|
169 operator * (const Matrix& m, const ColumnVector& a) |
458
|
170 { |
1947
|
171 ColumnVector retval; |
|
172 |
1205
|
173 int nr = m.rows (); |
|
174 int nc = m.cols (); |
1947
|
175 |
2386
|
176 int a_len = a.length (); |
|
177 |
|
178 if (nc != a_len) |
|
179 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
180 else |
458
|
181 { |
1947
|
182 if (nr == 0 || nc == 0) |
|
183 retval.resize (nr, 0.0); |
|
184 else |
|
185 { |
|
186 int ld = nr; |
|
187 |
|
188 retval.resize (nr); |
|
189 double *y = retval.fortran_vec (); |
|
190 |
|
191 F77_XFCN (dgemv, DGEMV, ("N", nr, nc, 1.0, m.data (), ld, |
|
192 a.data (), 1, 0.0, y, 1, 1L)); |
|
193 |
|
194 if (f77_exception_encountered) |
|
195 (*current_liboctave_error_handler) |
|
196 ("unrecoverable error in dgemv"); |
|
197 } |
458
|
198 } |
|
199 |
1947
|
200 return retval; |
458
|
201 } |
|
202 |
1205
|
203 // diagonal matrix by column vector -> column vector operations |
|
204 |
|
205 ColumnVector |
|
206 operator * (const DiagMatrix& m, const ColumnVector& a) |
458
|
207 { |
1947
|
208 ColumnVector retval; |
|
209 |
1205
|
210 int nr = m.rows (); |
|
211 int nc = m.cols (); |
1947
|
212 |
1205
|
213 int a_len = a.length (); |
1947
|
214 |
1205
|
215 if (nc != a_len) |
2386
|
216 gripe_nonconformant ("operator *", nr, nc, a_len, 1); |
1947
|
217 else |
458
|
218 { |
1947
|
219 if (nr == 0 || nc == 0) |
|
220 retval.resize (nr, 0.0); |
|
221 else |
|
222 { |
|
223 retval.resize (nr); |
|
224 |
|
225 for (int i = 0; i < a_len; i++) |
|
226 retval.elem (i) = a.elem (i) * m.elem (i, i); |
|
227 |
|
228 for (int i = a_len; i < nr; i++) |
|
229 retval.elem (i) = 0.0; |
|
230 } |
458
|
231 } |
|
232 |
1947
|
233 return retval; |
458
|
234 } |
|
235 |
|
236 // other operations |
|
237 |
|
238 ColumnVector |
2676
|
239 ColumnVector::map (d_d_Mapper f) const |
458
|
240 { |
2676
|
241 ColumnVector b (*this); |
|
242 return b.apply (f); |
458
|
243 } |
|
244 |
2676
|
245 ColumnVector& |
|
246 ColumnVector::apply (d_d_Mapper f) |
1205
|
247 { |
2676
|
248 double *d = fortran_vec (); // Ensures only one reference to my privates! |
1205
|
249 |
458
|
250 for (int i = 0; i < length (); i++) |
2676
|
251 d[i] = f (d[i]); |
|
252 |
|
253 return *this; |
458
|
254 } |
|
255 |
|
256 double |
|
257 ColumnVector::min (void) const |
|
258 { |
|
259 int len = length (); |
|
260 if (len == 0) |
|
261 return 0.0; |
|
262 |
|
263 double res = elem (0); |
|
264 |
|
265 for (int i = 1; i < len; i++) |
|
266 if (elem (i) < res) |
|
267 res = elem (i); |
|
268 |
|
269 return res; |
|
270 } |
|
271 |
|
272 double |
|
273 ColumnVector::max (void) const |
|
274 { |
|
275 int len = length (); |
|
276 if (len == 0) |
|
277 return 0.0; |
|
278 |
|
279 double res = elem (0); |
|
280 |
|
281 for (int i = 1; i < len; i++) |
|
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 ColumnVector& a) |
458
|
290 { |
|
291 // int field_width = os.precision () + 7; |
|
292 for (int i = 0; i < a.length (); i++) |
|
293 os << /* setw (field_width) << */ a.elem (i) << "\n"; |
|
294 return os; |
|
295 } |
|
296 |
3504
|
297 std::istream& |
|
298 operator >> (std::istream& is, ColumnVector& 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 |
|
319 /* |
|
320 ;;; Local Variables: *** |
|
321 ;;; mode: C++ *** |
|
322 ;;; End: *** |
|
323 */ |