Mercurial > octave-nkf
comparison liboctave/mx-inlines.cc @ 11586:12df7854fa7c
strip trailing whitespace from source files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:24:59 -0500 |
parents | fd0a3ac60b0e |
children | d9d65c3017c3 |
comparison
equal
deleted
inserted
replaced
11585:1473d0cf86d2 | 11586:12df7854fa7c |
---|---|
112 DEFMXCMPOP (mx_inline_eq, ==) | 112 DEFMXCMPOP (mx_inline_eq, ==) |
113 DEFMXCMPOP (mx_inline_ne, !=) | 113 DEFMXCMPOP (mx_inline_ne, !=) |
114 | 114 |
115 // Convert to logical value, for logical op purposes. | 115 // Convert to logical value, for logical op purposes. |
116 template <class T> inline bool logical_value (T x) { return x; } | 116 template <class T> inline bool logical_value (T x) { return x; } |
117 template <class T> inline bool logical_value (const std::complex<T>& x) | 117 template <class T> inline bool logical_value (const std::complex<T>& x) |
118 { return x.real () != 0 || x.imag () != 0; } | 118 { return x.real () != 0 || x.imag () != 0; } |
119 template <class T> inline bool logical_value (const octave_int<T>& x) | 119 template <class T> inline bool logical_value (const octave_int<T>& x) |
120 { return x.value (); } | 120 { return x.value (); } |
121 | 121 |
122 template <class X> | 122 template <class X> |
123 void mx_inline_not (size_t n, bool *r, const X* x) throw () | 123 void mx_inline_not (size_t n, bool *r, const X* x) throw () |
124 { | 124 { |
169 } \ | 169 } \ |
170 | 170 |
171 DEFMXBOOLOPEQ (mx_inline_and2, &=) | 171 DEFMXBOOLOPEQ (mx_inline_and2, &=) |
172 DEFMXBOOLOPEQ (mx_inline_or2, |=) | 172 DEFMXBOOLOPEQ (mx_inline_or2, |=) |
173 | 173 |
174 template <class T> | 174 template <class T> |
175 inline bool | 175 inline bool |
176 mx_inline_any_nan (size_t n, const T* x) throw () | 176 mx_inline_any_nan (size_t n, const T* x) throw () |
177 { | 177 { |
178 for (size_t i = 0; i < n; i++) | 178 for (size_t i = 0; i < n; i++) |
179 { | 179 { |
180 if (xisnan (x[i])) | 180 if (xisnan (x[i])) |
195 } | 195 } |
196 | 196 |
197 return true; | 197 return true; |
198 } | 198 } |
199 | 199 |
200 template <class T> | 200 template <class T> |
201 inline bool | 201 inline bool |
202 mx_inline_any_negative (size_t n, const T* x) throw () | 202 mx_inline_any_negative (size_t n, const T* x) throw () |
203 { | 203 { |
204 for (size_t i = 0; i < n; i++) | 204 for (size_t i = 0; i < n; i++) |
205 { | 205 { |
206 if (x[i] < 0) | 206 if (x[i] < 0) |
207 return true; | 207 return true; |
209 | 209 |
210 return false; | 210 return false; |
211 } | 211 } |
212 | 212 |
213 template<class T> | 213 template<class T> |
214 inline bool | 214 inline bool |
215 mx_inline_all_real (size_t n, const std::complex<T>* x) throw () | 215 mx_inline_all_real (size_t n, const std::complex<T>* x) throw () |
216 { | 216 { |
217 for (size_t i = 0; i < n; i++) | 217 for (size_t i = 0; i < n; i++) |
218 { | 218 { |
219 if (x[i].imag () != 0) | 219 if (x[i].imag () != 0) |
267 std::memcpy (r, y, n * sizeof (T)); \ | 267 std::memcpy (r, y, n * sizeof (T)); \ |
268 else \ | 268 else \ |
269 for (size_t i = 0; i < n; i++) r[i] = (y[i] OP x) ? y[i] : x; \ | 269 for (size_t i = 0; i < n; i++) r[i] = (y[i] OP x) ? y[i] : x; \ |
270 } | 270 } |
271 | 271 |
272 DEFMINMAXSPEC (double, mx_inline_xmin, <=) | 272 DEFMINMAXSPEC (double, mx_inline_xmin, <=) |
273 DEFMINMAXSPEC (double, mx_inline_xmax, >=) | 273 DEFMINMAXSPEC (double, mx_inline_xmax, >=) |
274 DEFMINMAXSPEC (float, mx_inline_xmin, <=) | 274 DEFMINMAXSPEC (float, mx_inline_xmin, <=) |
275 DEFMINMAXSPEC (float, mx_inline_xmax, >=) | 275 DEFMINMAXSPEC (float, mx_inline_xmax, >=) |
276 | 276 |
277 // Pairwise power | 277 // Pairwise power |
278 #define DEFMXMAPPER2X(F, FUN) \ | 278 #define DEFMXMAPPER2X(F, FUN) \ |
279 template <class R, class X, class Y> \ | 279 template <class R, class X, class Y> \ |
280 inline void F (size_t n, R *r, const X *x, const Y *y) throw () \ | 280 inline void F (size_t n, R *r, const X *x, const Y *y) throw () \ |
300 | 300 |
301 // Appliers. Since these call the operation just once, we pass it as | 301 // Appliers. Since these call the operation just once, we pass it as |
302 // a pointer, to allow the compiler reduce number of instances. | 302 // a pointer, to allow the compiler reduce number of instances. |
303 | 303 |
304 template <class R, class X> | 304 template <class R, class X> |
305 inline Array<R> | 305 inline Array<R> |
306 do_mx_unary_op (const Array<X>& x, | 306 do_mx_unary_op (const Array<X>& x, |
307 void (*op) (size_t, R *, const X *) throw ()) | 307 void (*op) (size_t, R *, const X *) throw ()) |
308 { | 308 { |
309 Array<R> r (x.dims ()); | 309 Array<R> r (x.dims ()); |
310 op (r.numel (), r.fortran_vec (), x.data ()); | 310 op (r.numel (), r.fortran_vec (), x.data ()); |
312 } | 312 } |
313 | 313 |
314 // Shortcuts for applying mx_inline_map. | 314 // Shortcuts for applying mx_inline_map. |
315 | 315 |
316 template <class R, class X, R fun (X)> | 316 template <class R, class X, R fun (X)> |
317 inline Array<R> | 317 inline Array<R> |
318 do_mx_unary_map (const Array<X>& x) | 318 do_mx_unary_map (const Array<X>& x) |
319 { | 319 { |
320 return do_mx_unary_op<R, X> (x, mx_inline_map<R, X, fun>); | 320 return do_mx_unary_op<R, X> (x, mx_inline_map<R, X, fun>); |
321 } | 321 } |
322 | 322 |
323 template <class R, class X, R fun (const X&)> | 323 template <class R, class X, R fun (const X&)> |
324 inline Array<R> | 324 inline Array<R> |
325 do_mx_unary_map (const Array<X>& x) | 325 do_mx_unary_map (const Array<X>& x) |
326 { | 326 { |
327 return do_mx_unary_op<R, X> (x, mx_inline_map<R, X, fun>); | 327 return do_mx_unary_op<R, X> (x, mx_inline_map<R, X, fun>); |
328 } | 328 } |
329 | 329 |
336 return r; | 336 return r; |
337 } | 337 } |
338 | 338 |
339 | 339 |
340 template <class R, class X, class Y> | 340 template <class R, class X, class Y> |
341 inline Array<R> | 341 inline Array<R> |
342 do_mm_binary_op (const Array<X>& x, const Array<Y>& y, | 342 do_mm_binary_op (const Array<X>& x, const Array<Y>& y, |
343 void (*op) (size_t, R *, const X *, const Y *) throw (), | 343 void (*op) (size_t, R *, const X *, const Y *) throw (), |
344 const char *opname) | 344 const char *opname) |
345 { | 345 { |
346 dim_vector dx = x.dims (), dy = y.dims (); | 346 dim_vector dx = x.dims (), dy = y.dims (); |
356 return Array<R> (); | 356 return Array<R> (); |
357 } | 357 } |
358 } | 358 } |
359 | 359 |
360 template <class R, class X, class Y> | 360 template <class R, class X, class Y> |
361 inline Array<R> | 361 inline Array<R> |
362 do_ms_binary_op (const Array<X>& x, const Y& y, | 362 do_ms_binary_op (const Array<X>& x, const Y& y, |
363 void (*op) (size_t, R *, const X *, Y) throw ()) | 363 void (*op) (size_t, R *, const X *, Y) throw ()) |
364 { | 364 { |
365 Array<R> r (x.dims ()); | 365 Array<R> r (x.dims ()); |
366 op (r.length (), r.fortran_vec (), x.data (), y); | 366 op (r.length (), r.fortran_vec (), x.data (), y); |
367 return r; | 367 return r; |
368 } | 368 } |
369 | 369 |
370 template <class R, class X, class Y> | 370 template <class R, class X, class Y> |
371 inline Array<R> | 371 inline Array<R> |
372 do_sm_binary_op (const X& x, const Array<Y>& y, | 372 do_sm_binary_op (const X& x, const Array<Y>& y, |
373 void (*op) (size_t, R *, X, const Y *) throw ()) | 373 void (*op) (size_t, R *, X, const Y *) throw ()) |
374 { | 374 { |
375 Array<R> r (y.dims ()); | 375 Array<R> r (y.dims ()); |
376 op (r.length (), r.fortran_vec (), x, y.data ()); | 376 op (r.length (), r.fortran_vec (), x, y.data ()); |
377 return r; | 377 return r; |
378 } | 378 } |
379 | 379 |
380 template <class R, class X> | 380 template <class R, class X> |
381 inline Array<R>& | 381 inline Array<R>& |
382 do_mm_inplace_op (Array<R>& r, const Array<X>& x, | 382 do_mm_inplace_op (Array<R>& r, const Array<X>& x, |
383 void (*op) (size_t, R *, const X *) throw (), | 383 void (*op) (size_t, R *, const X *) throw (), |
384 const char *opname) | 384 const char *opname) |
385 { | 385 { |
386 dim_vector dr = r.dims (), dx = x.dims (); | 386 dim_vector dr = r.dims (), dx = x.dims (); |
390 gripe_nonconformant (opname, dr, dx); | 390 gripe_nonconformant (opname, dr, dx); |
391 return r; | 391 return r; |
392 } | 392 } |
393 | 393 |
394 template <class R, class X> | 394 template <class R, class X> |
395 inline Array<R>& | 395 inline Array<R>& |
396 do_ms_inplace_op (Array<R>& r, const X& x, | 396 do_ms_inplace_op (Array<R>& r, const X& x, |
397 void (*op) (size_t, R *, X) throw ()) | 397 void (*op) (size_t, R *, X) throw ()) |
398 { | 398 { |
399 op (r.length (), r.fortran_vec (), x); | 399 op (r.length (), r.fortran_vec (), x); |
400 return r; | 400 return r; |
410 return true; | 410 return true; |
411 } | 411 } |
412 | 412 |
413 template <class T> | 413 template <class T> |
414 inline bool | 414 inline bool |
415 do_mx_check (const Array<T>& a, | 415 do_mx_check (const Array<T>& a, |
416 bool (*op) (size_t, const T *) throw ()) | 416 bool (*op) (size_t, const T *) throw ()) |
417 { | 417 { |
418 return op (a.numel (), a.data ()); | 418 return op (a.numel (), a.data ()); |
419 } | 419 } |
420 | 420 |
421 // NOTE: we don't use std::norm because it typically does some heavyweight | 421 // NOTE: we don't use std::norm because it typically does some heavyweight |
422 // magic to avoid underflows, which we don't need here. | 422 // magic to avoid underflows, which we don't need here. |
423 template <class T> | 423 template <class T> |
424 inline T cabsq (const std::complex<T>& c) | 424 inline T cabsq (const std::complex<T>& c) |
425 { return c.real () * c.real () + c.imag () * c.imag (); } | 425 { return c.real () * c.real () + c.imag () * c.imag (); } |
426 | 426 |
427 // default. works for integers and bool. | 427 // default. works for integers and bool. |
428 template <class T> | 428 template <class T> |
429 inline bool xis_true (T x) { return x; } | 429 inline bool xis_true (T x) { return x; } |
1037 } | 1037 } |
1038 } | 1038 } |
1039 } | 1039 } |
1040 | 1040 |
1041 template <class T> | 1041 template <class T> |
1042 void mx_inline_diff (const T *v, T *r, | 1042 void mx_inline_diff (const T *v, T *r, |
1043 octave_idx_type m, octave_idx_type n, | 1043 octave_idx_type m, octave_idx_type n, |
1044 octave_idx_type order) | 1044 octave_idx_type order) |
1045 { | 1045 { |
1046 switch (order) | 1046 switch (order) |
1047 { | 1047 { |
1123 if (dim < 0) | 1123 if (dim < 0) |
1124 dim = dims.first_non_singleton (); | 1124 dim = dims.first_non_singleton (); |
1125 | 1125 |
1126 // calculate extent triplet. | 1126 // calculate extent triplet. |
1127 l = 1, n = dims(dim), u = 1; | 1127 l = 1, n = dims(dim), u = 1; |
1128 for (octave_idx_type i = 0; i < dim; i++) | 1128 for (octave_idx_type i = 0; i < dim; i++) |
1129 l *= dims (i); | 1129 l *= dims (i); |
1130 for (octave_idx_type i = dim + 1; i < ndims; i++) | 1130 for (octave_idx_type i = dim + 1; i < ndims; i++) |
1131 u *= dims (i); | 1131 u *= dims (i); |
1132 } | 1132 } |
1133 } | 1133 } |
1137 // maybe it can be done without an explicit parameter? | 1137 // maybe it can be done without an explicit parameter? |
1138 | 1138 |
1139 template <class R, class T> | 1139 template <class R, class T> |
1140 inline Array<R> | 1140 inline Array<R> |
1141 do_mx_red_op (const Array<T>& src, int dim, | 1141 do_mx_red_op (const Array<T>& src, int dim, |
1142 void (*mx_red_op) (const T *, R *, octave_idx_type, | 1142 void (*mx_red_op) (const T *, R *, octave_idx_type, |
1143 octave_idx_type, octave_idx_type)) | 1143 octave_idx_type, octave_idx_type)) |
1144 { | 1144 { |
1145 octave_idx_type l, n, u; | 1145 octave_idx_type l, n, u; |
1146 dim_vector dims = src.dims (); | 1146 dim_vector dims = src.dims (); |
1147 // M*b inconsistency: sum([]) = 0 etc. | 1147 // M*b inconsistency: sum([]) = 0 etc. |
1161 } | 1161 } |
1162 | 1162 |
1163 template <class R, class T> | 1163 template <class R, class T> |
1164 inline Array<R> | 1164 inline Array<R> |
1165 do_mx_cum_op (const Array<T>& src, int dim, | 1165 do_mx_cum_op (const Array<T>& src, int dim, |
1166 void (*mx_cum_op) (const T *, R *, octave_idx_type, | 1166 void (*mx_cum_op) (const T *, R *, octave_idx_type, |
1167 octave_idx_type, octave_idx_type)) | 1167 octave_idx_type, octave_idx_type)) |
1168 { | 1168 { |
1169 octave_idx_type l, n, u; | 1169 octave_idx_type l, n, u; |
1170 dim_vector dims = src.dims (); | 1170 dim_vector dims = src.dims (); |
1171 get_extent_triplet (dims, dim, l, n, u); | 1171 get_extent_triplet (dims, dim, l, n, u); |
1178 } | 1178 } |
1179 | 1179 |
1180 template <class R> | 1180 template <class R> |
1181 inline Array<R> | 1181 inline Array<R> |
1182 do_mx_minmax_op (const Array<R>& src, int dim, | 1182 do_mx_minmax_op (const Array<R>& src, int dim, |
1183 void (*mx_minmax_op) (const R *, R *, octave_idx_type, | 1183 void (*mx_minmax_op) (const R *, R *, octave_idx_type, |
1184 octave_idx_type, octave_idx_type)) | 1184 octave_idx_type, octave_idx_type)) |
1185 { | 1185 { |
1186 octave_idx_type l, n, u; | 1186 octave_idx_type l, n, u; |
1187 dim_vector dims = src.dims (); | 1187 dim_vector dims = src.dims (); |
1188 get_extent_triplet (dims, dim, l, n, u); | 1188 get_extent_triplet (dims, dim, l, n, u); |
1221 } | 1221 } |
1222 | 1222 |
1223 template <class R> | 1223 template <class R> |
1224 inline Array<R> | 1224 inline Array<R> |
1225 do_mx_cumminmax_op (const Array<R>& src, int dim, | 1225 do_mx_cumminmax_op (const Array<R>& src, int dim, |
1226 void (*mx_cumminmax_op) (const R *, R *, octave_idx_type, | 1226 void (*mx_cumminmax_op) (const R *, R *, octave_idx_type, |
1227 octave_idx_type, octave_idx_type)) | 1227 octave_idx_type, octave_idx_type)) |
1228 { | 1228 { |
1229 octave_idx_type l, n, u; | 1229 octave_idx_type l, n, u; |
1230 dim_vector dims = src.dims (); | 1230 dim_vector dims = src.dims (); |
1231 get_extent_triplet (dims, dim, l, n, u); | 1231 get_extent_triplet (dims, dim, l, n, u); |
1292 // T. Ogita, S. M. Rump, S. Oishi: | 1292 // T. Ogita, S. M. Rump, S. Oishi: |
1293 // Accurate Sum And Dot Product, | 1293 // Accurate Sum And Dot Product, |
1294 // SIAM J. Sci. Computing, Vol. 26, 2005 | 1294 // SIAM J. Sci. Computing, Vol. 26, 2005 |
1295 | 1295 |
1296 template <class T> | 1296 template <class T> |
1297 inline void twosum_accum (T& s, T& e, | 1297 inline void twosum_accum (T& s, T& e, |
1298 const T& x) | 1298 const T& x) |
1299 { | 1299 { |
1300 T s1 = s + x, t = s1 - s, e1 = (s - (s1 - t)) + (x - t); | 1300 T s1 = s + x, t = s1 - s, e1 = (s - (s1 - t)) + (x - t); |
1301 s = s1; | 1301 s = s1; |
1302 e += e1; | 1302 e += e1; |
1303 } | 1303 } |
1304 | 1304 |
1305 template <class T> | 1305 template <class T> |
1306 inline T | 1306 inline T |
1307 mx_inline_xsum (const T *v, octave_idx_type n) | 1307 mx_inline_xsum (const T *v, octave_idx_type n) |
1308 { | 1308 { |
1309 T s = 0, e = 0; | 1309 T s = 0, e = 0; |
1310 for (octave_idx_type i = 0; i < n; i++) | 1310 for (octave_idx_type i = 0; i < n; i++) |
1311 twosum_accum (s, e, v[i]); | 1311 twosum_accum (s, e, v[i]); |
1312 | 1312 |
1313 return s + e; | 1313 return s + e; |
1314 } | 1314 } |
1315 | 1315 |
1316 template <class T> | 1316 template <class T> |
1317 inline void | 1317 inline void |
1318 mx_inline_xsum (const T *v, T *r, | 1318 mx_inline_xsum (const T *v, T *r, |
1319 octave_idx_type m, octave_idx_type n) | 1319 octave_idx_type m, octave_idx_type n) |
1320 { | 1320 { |
1321 OCTAVE_LOCAL_BUFFER (T, e, m); | 1321 OCTAVE_LOCAL_BUFFER (T, e, m); |
1322 for (octave_idx_type i = 0; i < m; i++) | 1322 for (octave_idx_type i = 0; i < m; i++) |
1323 e[i] = r[i] = T (); | 1323 e[i] = r[i] = T (); |