Clipper
clipper_util.h
1
4//C Copyright (C) 2000-2006 Kevin Cowtan and University of York
5//L
6//L This library is free software and is distributed under the terms
7//L and conditions of version 2.1 of the GNU Lesser General Public
8//L Licence (LGPL) with the following additional clause:
9//L
10//L `You may also combine or link a "work that uses the Library" to
11//L produce a work containing portions of the Library, and distribute
12//L that work under terms of your choice, provided that you give
13//L prominent notice with each copy of the work that the specified
14//L version of the Library is used in it, and that you include or
15//L provide public access to the complete corresponding
16//L machine-readable source code for the Library including whatever
17//L changes were used in the work. (i.e. If you make changes to the
18//L Library you must distribute those, but you do not need to
19//L distribute source or object code to those portions of the work
20//L not covered by this licence.)'
21//L
22//L Note that this clause grants an additional right and does not impose
23//L any additional restriction, and so does not affect compatibility
24//L with the GNU General Public Licence (GPL). If you wish to negotiate
25//L other terms, please contact the maintainer.
26//L
27//L You can redistribute it and/or modify the library under the terms of
28//L the GNU Lesser General Public License as published by the Free Software
29//L Foundation; either version 2.1 of the License, or (at your option) any
30//L later version.
31//L
32//L This library is distributed in the hope that it will be useful, but
33//L WITHOUT ANY WARRANTY; without even the implied warranty of
34//L MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35//L Lesser General Public License for more details.
36//L
37//L You should have received a copy of the CCP4 licence and/or GNU
38//L Lesser General Public License along with this library; if not, write
39//L to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK.
40//L The GNU Lesser General Public can also be obtained by writing to the
41//L Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42//L MA 02111-1307 USA
43
44
45#ifndef CLIPPER_UTIL
46#define CLIPPER_UTIL
47
48
49#include "clipper_precision.h"
50
51
52namespace clipper
53{
54
56
59 class Util
60 {
61 private:
62 typedef union { uitype32 i; ftype32 f; } U32;
63 typedef union { uitype64 i; ftype64 f; } U64;
64 public:
65 Util();
67 static const ftype& nan() { return nan_; }
69 static const float& nanf() { return nanf_; }
71 static const double& nand() { return nand_; }
73 template<class T> inline static void set_null( T& t ) { t = T(-2147483648); }
75 inline static void set_null( ftype32& f ) { U32* const u1=(U32* const)&f; const U32* const u2=(const U32* const)&nanf_; u1->i = u2->i; }
77 inline static void set_null( ftype64& f ) { U64* const u1=(U64* const)&f; const U64* const u2=(const U64* const)&nand_; u1->i = u2->i; }
79 inline static bool is_null( const ftype32& f ) { U32 u1,u2; u1.f = f; u2.f = nanf_; return ( u1.i == u2.i ); }
81 inline static bool is_null( const ftype64& f ) { U64 u1,u2; u1.f = f; u2.f = nand_; return ( u1.i == u2.i ); }
83
84 inline static bool is_nan( const ftype32 f ) { U32 u; u.f = f; return ((u.i&CLIPPER_NAN_MASK_A_32)==CLIPPER_NAN_MASK_A_32); }
86
87 inline static bool is_nan( const ftype64 f ) { U64 u; u.f = f; return ((u.i&CLIPPER_NAN_MASK_A_64)==CLIPPER_NAN_MASK_A_64); }
89
90 inline static bool isnan(const ftype32 f) { U32 u; u.f = f; return ((u.i&CLIPPER_NAN_MASK_A_32)==CLIPPER_NAN_MASK_A_32)&&((u.i&CLIPPER_NAN_MASK_B_32)!=0U); }
92
93 inline static bool isnan(const ftype64 f) { U64 u; u.f = f; return ((u.i&CLIPPER_NAN_MASK_A_64)==CLIPPER_NAN_MASK_A_64)&&((u.i&CLIPPER_NAN_MASK_B_64)!=0U); }
95 static ftype sim( const ftype& x );
97 static ftype invsim( const ftype& x );
99 static ftype sim_integ( const ftype& x );
101 static ftype sim_deriv( const ftype& x );
103 static ftype sim_deriv_recur( const ftype& x );
105 static ftype atanh( const ftype& x ) { return log((1.0+x)/(1.0-x))/2.0; }
107 static ftype bessel_i0( const ftype& x );
109 static ftype u2b( const ftype& x ) { return x * eightpi2_; }
111 static ftype b2u( const ftype& x ) { return x / eightpi2_; }
113 template<class T> inline static T mean( const T& pl, const T& mi )
114 {
115 if ( Util::is_nan(pl) ) return mi;
116 else if (Util::is_nan(mi) ) return pl;
117 else return 0.5*(pl+mi);
118 }
120 template<class T> inline static T sig_mean( const T& pl, const T& mi, const T& cov )
121 {
122 if ( Util::is_nan(pl) ) return mi;
123 else if (Util::is_nan(mi) ) return pl;
124 else if (Util::is_nan(cov) ) return 0.5*sqrt(pl*pl+mi*mi);
125 else return 0.5*sqrt(pl*pl+mi*mi+2*cov);
126 }
127
129 inline static int intf( const ftype& a ) { return int( floor( a ) ); }
131 inline static int intc( const ftype& a ) { return int( ceil( a ) ); }
133 inline static int intr( const ftype& a ) { return int( rint( a ) ); }
134
136 inline static ftype mod( const ftype& a, const ftype& b )
137 { ftype c = fmod(a, b); if (c < 0) c+=b; return c;}
139 inline static int mod( const int& a, const int& b )
140 { int c = a%b; if (c < 0) c+=b; return c; }
142 template<class T> inline static T max(const T& a, const T& b)
143 { return (a > b) ? a : b; }
145 template<class T> inline static T min(const T& a, const T& b)
146 { return (a < b) ? a : b; }
148 template<class T> inline static T bound( const T& min, const T& val, const T& max ) { return ( (val < max) ? ( (val > min ) ? val : min ) : max ); }
150 template<class T> inline static void swap( T& a, T& b )
151 { T c = a; a = b; b = c; }
153 template<class T> inline static void swap( T& a, T& b, T& c )
154 { c = a; a = b; b = c; }
156 template<class T> inline static T sqr( const T& a ) { return a*a; }
158 template<class T> inline static T isqrt( const T& n )
159 { return T(floor(sqrt(ftype(n)))); }
160
162 inline static const ftype& pi() { return onepi_; }
164 inline static const ftype& twopi() { return twopi_; }
166 inline static const ftype& twopi2() { return twopi2_; }
168 inline static const ftype& eightpi2() { return eightpi2_; }
170 static ftype d2rad( const ftype& x );
172 static ftype rad2d( const ftype& x );
173
174 private:
175 static float nanf_;
176 static double nand_;
177 static ftype nan_;
178 static ftype onepi_;
179 static ftype twopi_;
180 static ftype twopi2_;
181 static ftype eightpi2_;
182 static ftype d2rad_;
183 static ftype sim_a;
184 static ftype sim_b;
185 static ftype sim_c;
186 static ftype sim_d;
187 static ftype sim_e;
188 static ftype sim_A;
189 static ftype sim_B;
190 static ftype sim_C;
191 static ftype sim_g;
192 static ftype sim_p;
193 static ftype sim_q;
194 static ftype sim_r;
195 };
196
197} // namespace clipper
198
199#endif
Utility class.
Definition: clipper_util.h:60
static int mod(const int &a, const int &b)
Corrected mod.
Definition: clipper_util.h:139
static T max(const T &a, const T &b)
max
Definition: clipper_util.h:142
static bool is_null(const ftype64 &f)
fast test for null floating value - only works if set from Util::null()
Definition: clipper_util.h:81
static T mean(const T &pl, const T &mi)
Convert F+/F- to mean F, with NaN checks.
Definition: clipper_util.h:113
static const float & nanf()
fast Util::nan() value
Definition: clipper_util.h:69
static ftype sim_integ(const ftype &x)
Integral of Sim function: log(I0(X))
Definition: clipper_util.cpp:112
static void set_null(ftype32 &f)
set null floating value - a specific value of NaN used for missings
Definition: clipper_util.h:75
static bool isnan(const ftype64 f)
slow general NaN test for compatibility
Definition: clipper_util.h:93
static bool isnan(const ftype32 f)
slow general NaN test for compatibility
Definition: clipper_util.h:90
static ftype sim_deriv(const ftype &x)
Derivative of Sim function: d/dx( I1(X)/I0(x) )
Definition: clipper_util.cpp:119
static const ftype & eightpi2()
8 pi squared
Definition: clipper_util.h:168
static T sqr(const T &a)
square
Definition: clipper_util.h:156
static int intr(const ftype &a)
Round-to-integer: int(round(a))
Definition: clipper_util.h:133
Util()
null constructor
Definition: clipper_util.cpp:70
static const ftype & twopi()
2 pi
Definition: clipper_util.h:164
static ftype b2u(const ftype &x)
Convert isotropic B-factor to U-value.
Definition: clipper_util.h:111
static ftype invsim(const ftype &x)
Inverse Sim function: I1(X)/I0(X)
Definition: clipper_util.cpp:93
static ftype u2b(const ftype &x)
Convert isotropic U-value to B-factor.
Definition: clipper_util.h:109
static const ftype & nan()
fast Util::nan() value
Definition: clipper_util.h:67
static ftype sim_deriv_recur(const ftype &x)
Derivative of Sim function using recurrance: -sim(x)/x + (1 - sim(x)^2)
Definition: clipper_util.cpp:125
static T sig_mean(const T &pl, const T &mi, const T &cov)
Convert sigF+/sigF-/cov to sig F, with NaN checks.
Definition: clipper_util.h:120
static const double & nand()
fast Util::nan() value
Definition: clipper_util.h:71
static T isqrt(const T &n)
Integer square root (returns floor of sqrt)
Definition: clipper_util.h:158
static int intc(const ftype &a)
Truncate-to-integer above: int(ceil(a))
Definition: clipper_util.h:131
static const ftype & pi()
pi
Definition: clipper_util.h:162
static int intf(const ftype &a)
Truncate-to-integer: int(floor(a))
Definition: clipper_util.h:129
static bool is_null(const ftype32 &f)
fast test for null floating value - only works if set from Util::null()
Definition: clipper_util.h:79
static void swap(T &a, T &b)
swap the contents of two objects
Definition: clipper_util.h:150
static ftype mod(const ftype &a, const ftype &b)
Corrected mod.
Definition: clipper_util.h:136
static const ftype & twopi2()
2 pi squared
Definition: clipper_util.h:166
static void set_null(ftype64 &f)
set null floating value - a specific value of NaN used for missings
Definition: clipper_util.h:77
static ftype rad2d(const ftype &x)
degree-to-radian conversion
Definition: clipper_util.cpp:157
static void swap(T &a, T &b, T &c)
swap the contents of two objects, using third as store (for speed)
Definition: clipper_util.h:153
static bool is_nan(const ftype64 f)
fast Util::nan() test
Definition: clipper_util.h:87
static void set_null(T &t)
set null generic value
Definition: clipper_util.h:73
static ftype sim(const ftype &x)
Sim function: I1(X)/I0(X)
Definition: clipper_util.cpp:84
static bool is_nan(const ftype32 f)
fast Util::nan() test
Definition: clipper_util.h:84
static ftype atanh(const ftype &x)
Arc hyperbolic tangent.
Definition: clipper_util.h:105
static T min(const T &a, const T &b)
min
Definition: clipper_util.h:145
static ftype d2rad(const ftype &x)
degree-to-radian conversion
Definition: clipper_util.cpp:153
static ftype bessel_i0(const ftype &x)
Modified Bessel function of the first kind.
Definition: clipper_util.cpp:135
static T bound(const T &min, const T &val, const T &max)
bound a value by limits
Definition: clipper_util.h:148
ftype64 ftype
ftype definition for floating point representation
Definition: clipper_precision.h:58