Mercurial > hg > octave-lyh
annotate liboctave/dRowVector.cc @ 10010:c5e9931c7ba7
Correctly produce postcript output for geometryimages when QHULL library is not present
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 20 Dec 2009 17:02:57 -0800 |
parents | f80c566bc751 |
children | 4c0cdbe0acca |
rev | line source |
---|---|
1993 | 1 // RowVector manipulations. |
458 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003, |
8920 | 5 2004, 2005, 2006, 2007, 2008 John W. Eaton |
458 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
458 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
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" |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7482
diff
changeset
|
33 #include "functor.h" |
1368 | 34 #include "lo-error.h" |
458 | 35 #include "mx-base.h" |
36 #include "mx-inlines.cc" | |
1650 | 37 #include "oct-cmplx.h" |
458 | 38 |
39 // Fortran functions we call. | |
40 | |
41 extern "C" | |
42 { | |
4552 | 43 F77_RET_T |
44 F77_FUNC (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL, | |
5275 | 45 const octave_idx_type&, const octave_idx_type&, const double&, |
46 const double*, const octave_idx_type&, const double*, | |
47 const octave_idx_type&, const double&, double*, const octave_idx_type& | |
4552 | 48 F77_CHAR_ARG_LEN_DECL); |
5983 | 49 F77_RET_T |
50 F77_FUNC (xddot, XDDOT) (const octave_idx_type&, const double*, const octave_idx_type&, | |
51 const double*, const octave_idx_type&, double&); | |
458 | 52 } |
53 | |
1360 | 54 // Row Vector class. |
458 | 55 |
2386 | 56 bool |
458 | 57 RowVector::operator == (const RowVector& a) const |
58 { | |
5275 | 59 octave_idx_type len = length (); |
458 | 60 if (len != a.length ()) |
61 return 0; | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8999
diff
changeset
|
62 return mx_inline_equal (len, data (), a.data ()); |
458 | 63 } |
64 | |
2386 | 65 bool |
458 | 66 RowVector::operator != (const RowVector& a) const |
67 { | |
68 return !(*this == a); | |
69 } | |
70 | |
71 RowVector& | |
5275 | 72 RowVector::insert (const RowVector& a, octave_idx_type c) |
458 | 73 { |
5275 | 74 octave_idx_type a_len = a.length (); |
4316 | 75 |
1699 | 76 if (c < 0 || c + a_len > length ()) |
458 | 77 { |
78 (*current_liboctave_error_handler) ("range error for insert"); | |
79 return *this; | |
80 } | |
81 | |
4316 | 82 if (a_len > 0) |
83 { | |
84 make_unique (); | |
85 | |
5275 | 86 for (octave_idx_type i = 0; i < a_len; i++) |
4316 | 87 xelem (c+i) = a.elem (i); |
88 } | |
458 | 89 |
90 return *this; | |
91 } | |
92 | |
93 RowVector& | |
94 RowVector::fill (double val) | |
95 { | |
5275 | 96 octave_idx_type len = length (); |
4316 | 97 |
458 | 98 if (len > 0) |
4316 | 99 { |
100 make_unique (); | |
101 | |
5275 | 102 for (octave_idx_type i = 0; i < len; i++) |
4316 | 103 xelem (i) = val; |
104 } | |
105 | |
458 | 106 return *this; |
107 } | |
108 | |
109 RowVector& | |
5275 | 110 RowVector::fill (double val, octave_idx_type c1, octave_idx_type c2) |
458 | 111 { |
5275 | 112 octave_idx_type len = length (); |
4316 | 113 |
458 | 114 if (c1 < 0 || c2 < 0 || c1 >= len || c2 >= len) |
115 { | |
116 (*current_liboctave_error_handler) ("range error for fill"); | |
117 return *this; | |
118 } | |
119 | |
5275 | 120 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 121 |
4316 | 122 if (c2 >= c1) |
123 { | |
124 make_unique (); | |
125 | |
5275 | 126 for (octave_idx_type i = c1; i <= c2; i++) |
4316 | 127 xelem (i) = val; |
128 } | |
458 | 129 |
130 return *this; | |
131 } | |
132 | |
133 RowVector | |
134 RowVector::append (const RowVector& a) const | |
135 { | |
5275 | 136 octave_idx_type len = length (); |
137 octave_idx_type nc_insert = len; | |
458 | 138 RowVector retval (len + a.length ()); |
139 retval.insert (*this, 0); | |
140 retval.insert (a, nc_insert); | |
141 return retval; | |
142 } | |
143 | |
144 ColumnVector | |
145 RowVector::transpose (void) const | |
146 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7503
diff
changeset
|
147 return MArray<double>::transpose(); |
458 | 148 } |
149 | |
150 RowVector | |
1205 | 151 real (const ComplexRowVector& a) |
152 { | |
5275 | 153 octave_idx_type a_len = a.length (); |
1205 | 154 RowVector retval; |
155 if (a_len > 0) | |
3769 | 156 retval = RowVector (mx_inline_real_dup (a.data (), a_len), a_len); |
1205 | 157 return retval; |
158 } | |
159 | |
160 RowVector | |
161 imag (const ComplexRowVector& a) | |
162 { | |
5275 | 163 octave_idx_type a_len = a.length (); |
1205 | 164 RowVector retval; |
165 if (a_len > 0) | |
3769 | 166 retval = RowVector (mx_inline_imag_dup (a.data (), a_len), a_len); |
1205 | 167 return retval; |
168 } | |
169 | |
170 RowVector | |
5275 | 171 RowVector::extract (octave_idx_type c1, octave_idx_type c2) const |
458 | 172 { |
5275 | 173 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458 | 174 |
5275 | 175 octave_idx_type new_c = c2 - c1 + 1; |
458 | 176 |
177 RowVector result (new_c); | |
178 | |
5275 | 179 for (octave_idx_type i = 0; i < new_c; i++) |
4316 | 180 result.xelem (i) = elem (c1+i); |
181 | |
182 return result; | |
183 } | |
184 | |
185 RowVector | |
5275 | 186 RowVector::extract_n (octave_idx_type r1, octave_idx_type n) const |
4316 | 187 { |
188 RowVector result (n); | |
189 | |
5275 | 190 for (octave_idx_type i = 0; i < n; i++) |
4316 | 191 result.xelem (i) = elem (r1+i); |
458 | 192 |
193 return result; | |
194 } | |
195 | |
196 // row vector by matrix -> row vector | |
197 | |
198 RowVector | |
199 operator * (const RowVector& v, const Matrix& a) | |
200 { | |
1947 | 201 RowVector retval; |
202 | |
5275 | 203 octave_idx_type len = v.length (); |
1947 | 204 |
5275 | 205 octave_idx_type a_nr = a.rows (); |
206 octave_idx_type a_nc = a.cols (); | |
2386 | 207 |
208 if (a_nr != len) | |
209 gripe_nonconformant ("operator *", 1, len, a_nr, a_nc); | |
1947 | 210 else |
458 | 211 { |
1947 | 212 if (len == 0) |
213 retval.resize (a_nc, 0.0); | |
214 else | |
215 { | |
216 // Transpose A to form A'*x == (x'*A)' | |
217 | |
5275 | 218 octave_idx_type ld = a_nr; |
1947 | 219 |
220 retval.resize (a_nc); | |
221 double *y = retval.fortran_vec (); | |
222 | |
4552 | 223 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("T", 1), |
224 a_nr, a_nc, 1.0, a.data (), | |
225 ld, v.data (), 1, 0.0, y, 1 | |
226 F77_CHAR_ARG_LEN (1))); | |
1947 | 227 } |
458 | 228 } |
229 | |
1947 | 230 return retval; |
458 | 231 } |
232 | |
233 // other operations | |
234 | |
235 double | |
236 RowVector::min (void) const | |
237 { | |
5275 | 238 octave_idx_type len = length (); |
458 | 239 if (len == 0) |
240 return 0; | |
241 | |
242 double res = elem (0); | |
243 | |
5275 | 244 for (octave_idx_type i = 1; i < len; i++) |
458 | 245 if (elem (i) < res) |
246 res = elem (i); | |
247 | |
248 return res; | |
249 } | |
250 | |
251 double | |
252 RowVector::max (void) const | |
253 { | |
5275 | 254 octave_idx_type len = length (); |
458 | 255 if (len == 0) |
256 return 0; | |
257 | |
258 double res = elem (0); | |
259 | |
5275 | 260 for (octave_idx_type i = 1; i < len; i++) |
458 | 261 if (elem (i) > res) |
262 res = elem (i); | |
263 | |
264 return res; | |
265 } | |
266 | |
3504 | 267 std::ostream& |
268 operator << (std::ostream& os, const RowVector& a) | |
458 | 269 { |
270 // int field_width = os.precision () + 7; | |
1360 | 271 |
5275 | 272 for (octave_idx_type i = 0; i < a.length (); i++) |
458 | 273 os << " " /* setw (field_width) */ << a.elem (i); |
274 return os; | |
275 } | |
276 | |
3504 | 277 std::istream& |
278 operator >> (std::istream& is, RowVector& a) | |
458 | 279 { |
5275 | 280 octave_idx_type len = a.length(); |
458 | 281 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
282 if (len > 0) |
458 | 283 { |
284 double tmp; | |
5275 | 285 for (octave_idx_type i = 0; i < len; i++) |
458 | 286 { |
287 is >> tmp; | |
288 if (is) | |
289 a.elem (i) = tmp; | |
290 else | |
291 break; | |
292 } | |
293 } | |
532 | 294 return is; |
458 | 295 } |
296 | |
1205 | 297 // other operations |
298 | |
299 RowVector | |
5275 | 300 linspace (double x1, double x2, octave_idx_type n) |
1205 | 301 { |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
302 if (n < 1) n = 1; |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
303 |
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
304 NoAlias<RowVector> retval (n); |
1205 | 305 |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
306 double delta = (x2 - x1) / (n - 1); |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
307 retval(0) = x1; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
308 for (octave_idx_type i = 1; i < n-1; i++) |
9658
3429c956de6f
extend linspace & fix up liboctave rewrite
Jaroslav Hajek <highegg@gmail.com>
parents:
9653
diff
changeset
|
309 retval(i) = x1 + i*delta; |
9653
e087d7c77ff9
improve linspace in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
310 retval(n-1) = x2; |
1205 | 311 |
312 return retval; | |
313 } | |
314 | |
315 // row vector by column vector -> scalar | |
316 | |
317 double | |
318 operator * (const RowVector& v, const ColumnVector& a) | |
319 { | |
1947 | 320 double retval = 0.0; |
321 | |
5275 | 322 octave_idx_type len = v.length (); |
1947 | 323 |
5275 | 324 octave_idx_type a_len = a.length (); |
2386 | 325 |
326 if (len != a_len) | |
327 gripe_nonconformant ("operator *", len, a_len); | |
1947 | 328 else if (len != 0) |
5983 | 329 F77_FUNC (xddot, XDDOT) (len, v.data (), 1, a.data (), 1, retval); |
1205 | 330 |
1947 | 331 return retval; |
1205 | 332 } |
333 | |
334 Complex | |
335 operator * (const RowVector& v, const ComplexColumnVector& a) | |
336 { | |
337 ComplexRowVector tmp (v); | |
338 return tmp * a; | |
339 } | |
340 | |
458 | 341 /* |
342 ;;; Local Variables: *** | |
343 ;;; mode: C++ *** | |
344 ;;; End: *** | |
345 */ |