diff options
Diffstat (limited to 'src/jtutil')
-rw-r--r-- | src/jtutil/README | 15 | ||||
-rw-r--r-- | src/jtutil/miscfp.cc | 83 | ||||
-rw-r--r-- | src/jtutil/util.hh | 24 |
3 files changed, 113 insertions, 9 deletions
diff --git a/src/jtutil/README b/src/jtutil/README index 102e1b1..1b6ea5e 100644 --- a/src/jtutil/README +++ b/src/jtutil/README @@ -13,12 +13,15 @@ linear_map<fp> is a template class (templated on the floating-point type) representing a linear map between a contiguous interval of integers, and floating-point numbers. + cpm_map<fp> is a template class (templated on the floating-point type) representing a mapping from the integers to the integers, of the form i --> constant +/- i . (The name abbreviates "Constant Plus or Minus MAP".) + fuzzy<fp> + (declarations in "util.hh") is a template class (templated on the floating-point type) providing fuzzy arithmetic, to try to paper over some of the effects of floating-point rounding errors. For example, @@ -30,8 +33,20 @@ fuzzy<fp> and have the loop execute as one would naievly expect, even if rounding errors cause the final value of \code{x} to be 0.9999999999999999 or 1.000000000000001 or suchlike. + round<fp> + (declarations in "util.hh") is a template class (templated on the floating-point type) to do machine-independent rounding of floating point values +norm<fp> + (declarations in "util.hh") + is a template class (templated on the floating-point type) + to the compute 2-, rms-, and/or infinity-norms of a set of + numbers + +miscfp.cc + (declarations in "util.hh") + contains various misc floating-point routines + There are also a number of test drivers in the files test_*.cc . diff --git a/src/jtutil/miscfp.cc b/src/jtutil/miscfp.cc index 5ccda53..4263d2c 100644 --- a/src/jtutil/miscfp.cc +++ b/src/jtutil/miscfp.cc @@ -6,12 +6,20 @@ // jtutil::arctan_xy - 4-quadrant arc tangent // jtutil::modulo_reduce - reduce an angle modulo 2*pi radians (360 degrees) // +// jtutil::array_zero - zero an array +// jtutil::array_copy - copy one array to another +// jtutil::array_scale - scale an array by a scalar +// +// ***** template instantiations ***** +// #include <math.h> #include "stdc.h" #include "util.hh" //****************************************************************************** +//****************************************************************************** +//****************************************************************************** // // This function computes the floating point "signum" function (as in APL), @@ -113,3 +121,78 @@ if (! (fuzzy<double>::GE(xx, xmin) && fuzzy<double>::LE(xx, xmax)) ) return xx; } } // namespace jtutil:: + +//****************************************************************************** +//****************************************************************************** +//****************************************************************************** + +// +// This function zeros the size-N array dst[]. +// +namespace jtutil + { +template <typename fp> + void array_zero(int N, fp dst[]) +{ + for (int i = 0 ; i < N ; ++i) + { + dst[i] = 0.0; + } +} + } // namespace jtutil:: + +//****************************************************************************** + +// +// This function copies the size-N array src[] to the size-N array dst[], +// analogously to the BLAS routines xCOPY(). +// +namespace jtutil + { +template <typename fp> + void array_copy(int N, const fp src[], fp dst[]) +{ + for (int i = 0 ; i < N ; ++i) + { + dst[i] = src[i]; + } +} + } // namespace jtutil:: + +//****************************************************************************** + +// +// This function multiplies the size-N array dst[] by the scalar alpha, +// analogously to the BLAS routines xSCAL(). +// +namespace jtutil + { +template <typename fp> + void array_scale(int N, fp alpha, fp dst[]) +{ + for (int i = 0 ; i < N ; ++i) + { + dst[i] *= alpha; + } +} + } // namespace jtutil:: + +//****************************************************************************** +//****************************************************************************** +//****************************************************************************** + +// +// ***** template instantiations ***** +// + +namespace jtutil + { +template void array_zero<float >(int, float []); +template void array_zero<double>(int, double[]); + +template void array_copy<float >(int, const float [], float []); +template void array_copy<double>(int, const double[], double[]); + +template void array_scale<float >(int, float , float []); +template void array_scale<double>(int, double, double[]); + } // namespace jtutil:: diff --git a/src/jtutil/util.hh b/src/jtutil/util.hh index 6873e62..89d6bde 100644 --- a/src/jtutil/util.hh +++ b/src/jtutil/util.hh @@ -1,13 +1,5 @@ // util.hh -- stuff for my C++ utility library // $Id$ -// -// jtutil::{how_many_in_range,is_even,is_odd} - misc integer manipulation -// jtutil::abs - absolute value template -// jtutil::pow* - raise floating point value to small-integer power -// jtutil::norm<fp> - compute 2-, rms-, and infinity-norms -// jtutil::fuzzy::<fp> - fuzzy floating point comparisons and other operations -// jtutil::round::<fp> - floating point rounding -// // // prerequisites: @@ -27,11 +19,15 @@ inline int is_even(int i) { return !(i & 0x1); } inline int is_odd (int i) { return (i & 0x1); } // -// absolute value template +// min/max/absolute value template // FIXME: <cmath> puts this in std::, but older g++ versions // (which don't fully implement namespaces) get confused // template <typename fp> + inline fp min(fp x, fp y) { return (x < y) ? x : y; } +template <typename fp> + inline fp max(fp x, fp y) { return (x > y) ? x : y; } +template <typename fp> inline fp abs(fp x) { return (x > 0.0) ? x : -x; } // @@ -66,6 +62,16 @@ double arctan_xy(double x, double y); double modulo_reduce(double x, double xmod, double xmin, double xmax); // +// simple BLAS-like array arithmetic routines +// +template <typename fp> + void array_zero(int N, fp dst[]); // dst[] = 0 +template <typename fp> + void array_copy(int N, const fp src[], fp dst[]); // dst[] = src[] +template <typename fp> + void array_scale(int N, fp alpha, fp dst[]); // dst[] *= alpha + +// // more misc math stuff, valid only if <math.h> has been #included // #ifdef PI // from "jt/stdc.h" |