aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/util.hh
blob: fa46f7016147cb378198c25d315b95f90bf6db2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// util.hh -- stuff for my C++ utility library
// $Header$

//
// prerequisites:
//	"stdc.h"
//

namespace jtutil
	  {

//******************************************************************************

// how many integers are in the closed interval [low,high]
inline int how_many_in_range(int low, int high) { return high - low + 1; }

// is an integer even/odd
inline int is_even(int i) { return !(i & 0x1); }
inline int is_odd (int i) { return  (i & 0x1); }

//
// 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; }

//
// These functions raise their arguments (presumably floating-point)
// to various small-integer powers.
// FIXME: do we ever use these?
//
template <typename fp>
  inline fp pow2(fp x) { return x*x; }
template <typename fp>
  inline fp pow3(fp x) { return x*x*x; }
template <typename fp>
  inline fp pow4(fp x) { return pow2(pow2(x)); }
template <typename fp>
  inline fp pow5(fp x) { return x * pow4(x); }
template <typename fp>
  inline fp pow6(fp x) { return pow3(pow2(x)); }
template <typename fp>
  inline fp pow7(fp x) { return x * pow6(x); }
template <typename fp>
  inline fp pow8(fp x) { return pow2(pow2(pow2(x))); }

//
// misc math stuff
//
double signum(double x);
double hypot3(double x, double y, double z);
double arctan_xy(double x, double y);

// reduce x modulo xmod to be in the interval [xmin,xmax],
// or error_exit() if no such value exists
double modulo_reduce(double x, double xmod, double xmin, double xmax);

//
// more misc math stuff, valid only if <math.h> has been #included
//
#ifdef PI	// from "jt/stdc.h"
  // convert degrees <--> radians
  template <typename fp>
    inline fp degrees_of_radians(fp radians) { return (180.0/PI)*radians; }
  template <typename fp>
    inline fp radians_of_degrees(fp degrees) { return (PI/180.0)*degrees; }
#endif

//******************************************************************************

//
// This template class computes means, 2-norms, rms-norms, and infinity-norms.
//
template <typename fp>
class	norm
	{
public:
	// get norms etc
	fp mean() const;
	fp two_norm() const;		// sqrt(sum x_i^2)
	fp rms_norm() const;		// sqrt(average of x_i^2)
	fp infinity_norm() const;	// max(|x_i|)

	// specify data point
	void data(fp x);

	// have any data points been specified?
	bool is_empty()    const { return N_ == 0; }
	bool is_nonempty() const { return N_ > 0; }

	// reset ==> just like newly-constructed object
	void reset() { N_ = 0; sum2_ = 0.0; infinity_norm_ = 0.0; }

	// constructor, destructor
	// ... compiler-generated no-op destructor is ok
	norm()
		: N_(0), sum_(0.0), sum2_(0.0), infinity_norm_(0.0)
		{ }

private:
        // we forbid copying and passing by value
        // by declaring the copy constructor and assignment operator
        // private, but never defining them
        norm(const norm &rhs);
	norm& operator=(const norm &rhs);

private:
	int N_;			// # of data points
	fp sum_;		// sum(data)
	fp sum2_;		// sum(data^2)
	fp infinity_norm_;	// max |data|
	};

//******************************************************************************

//
// This template does fuzzy comparisons and related operations on
// floating point values, parameterized by the floating point type.
//
// The fuzzy comparison semantics are based on those of APL, but are
// modified to use an absolute error tolerance for values close to 0.
//

// this template class has only static members
// ... it's a class, not a namespace, because we want to express the
//     semantics that the entire class is a single template, rather
//     than the individual members being conceptually-unrelated templates
// ... moreover, we need the *data* member (template)  tolerance , and
//     it seems C++ doesn't grok data templates which aren't in a class
template <typename fp>
class	fuzzy
	{
public:
	// comparison tolerance
	// ... must be explicitly initialized when instantiating
	//     for a new <fp> type, see "fuzzy.cc" for details/examples
	// ... may be modified by user code if needed
	static fp tolerance;

	// fuzzy commparisons
	static bool EQ(fp x, fp y);
	static bool NE(fp x, fp y) { return ! EQ(x,y); }
	static bool LT(fp x, fp y) { return EQ(x,y) ? false : (x < y); }
	static bool LE(fp x, fp y) { return EQ(x,y) ? true  : (x < y); }
	static bool GT(fp x, fp y) { return EQ(x,y) ? false : (x > y); }
	static bool GE(fp x, fp y) { return EQ(x,y) ? true  : (x > y); }

	static bool is_integer(fp x);	// is x fuzzily an integer?
	static int floor(fp x);	// round x fuzzily down to integer
	static int ceiling(fp x);	// round x fuzzily up to integer
	};

//******************************************************************************

//
// This template does machine-independent rounding of floating point
// values, parameterized by the floating point type.
//

// this template class has only static members
// ... it's a class, not a namespace, because we want to express the
//     semantics that the entire class is a single template, rather
//     than the individual members being conceptually-unrelated templates
template <typename fp>
class	round
	{
public:
	static int to_integer(fp x);	// round to nearest integer

	static int floor(fp x);		// round down to integer
	static int ceiling(fp x);	// round up to integer
	};

//******************************************************************************

	  }	// namespace jtutil