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 
52 namespace 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();
66  static const ftype& nan() { return nan_; }
69  static const float& nanf() { return nanf_; }
71  static const double& nand() { return nand_; }
73  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; }
75  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; }
77  inline static bool is_null( const ftype32& f ) { U32 u1,u2; u1.f = f; u2.f = nanf_; return ( u1.i == u2.i ); }
79  inline static bool is_null( const ftype64& f ) { U64 u1,u2; u1.f = f; u2.f = nand_; return ( u1.i == u2.i ); }
81 
82  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); }
84 
85  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); }
87 
88  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); }
90 
91  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); }
93  static ftype sim( const ftype& x );
95  static ftype invsim( const ftype& x );
97  static ftype sim_integ( const ftype& x );
99  static ftype sim_deriv( const ftype& x );
101  static ftype sim_deriv_recur( const ftype& x );
103  static ftype atanh( const ftype& x ) { return log((1.0+x)/(1.0-x))/2.0; }
105  static ftype bessel_i0( const ftype& x );
107  static ftype u2b( const ftype& x ) { return x * eightpi2_; }
109  static ftype b2u( const ftype& x ) { return x / eightpi2_; }
111  template<class T> inline static T mean( const T& pl, const T& mi )
112  {
113  if ( Util::is_nan(pl) ) return mi;
114  else if (Util::is_nan(mi) ) return pl;
115  else return 0.5*(pl+mi);
116  }
118  template<class T> inline static T sig_mean( const T& pl, const T& mi, const T& cov )
119  {
120  if ( Util::is_nan(pl) ) return mi;
121  else if (Util::is_nan(mi) ) return pl;
122  else if (Util::is_nan(cov) ) return 0.5*sqrt(pl*pl+mi*mi);
123  else return 0.5*sqrt(pl*pl+mi*mi+2*cov);
124  }
125 
127  inline static int intf( const ftype& a ) { return int( floor( a ) ); }
129  inline static int intc( const ftype& a ) { return int( ceil( a ) ); }
131  inline static int intr( const ftype& a ) { return int( rint( a ) ); }
132 
134  inline static ftype mod( const ftype& a, const ftype& b )
135  { ftype c = fmod(a, b); if (c < 0) c+=b; return c;}
137  inline static int mod( const int& a, const int& b )
138  { int c = a%b; if (c < 0) c+=b; return c; }
140  template<class T> inline static T max(const T& a, const T& b)
141  { return (a > b) ? a : b; }
143  template<class T> inline static T min(const T& a, const T& b)
144  { return (a < b) ? a : b; }
146  template<class T> inline static T bound( const T& min, const T& val, const T& max ) { return ( (val < max) ? ( (val > min ) ? val : min ) : max ); }
148  template<class T> inline static void swap( T& a, T& b )
149  { T c = a; a = b; b = c; }
151  template<class T> inline static void swap( T& a, T& b, T& c )
152  { c = a; a = b; b = c; }
154  template<class T> inline static T sqr( const T& a ) { return a*a; }
156  template<class T> inline static T isqrt( const T& n )
157  { return T(floor(sqrt(ftype(n)))); }
158 
160  inline static const ftype& pi() { return onepi_; }
162  inline static const ftype& twopi() { return twopi_; }
164  inline static const ftype& twopi2() { return twopi2_; }
166  inline static const ftype& eightpi2() { return eightpi2_; }
168  static ftype d2rad( const ftype& x );
170  static ftype rad2d( const ftype& x );
171 
172  private:
173  static float nanf_;
174  static double nand_;
175  static ftype nan_;
176  static ftype onepi_;
177  static ftype twopi_;
178  static ftype twopi2_;
179  static ftype eightpi2_;
180  static ftype d2rad_;
181  static ftype sim_a;
182  static ftype sim_b;
183  static ftype sim_c;
184  static ftype sim_d;
185  static ftype sim_e;
186  static ftype sim_A;
187  static ftype sim_B;
188  static ftype sim_C;
189  static ftype sim_g;
190  static ftype sim_p;
191  static ftype sim_q;
192  static ftype sim_r;
193  };
194 
195 } // namespace clipper
196 
197 #endif
static ftype invsim(const ftype &x)
Inverse Sim function: I1(X)/I0(X)
Definition: clipper_util.cpp:93
static ftype mod(const ftype &a, const ftype &b)
Corrected mod.
Definition: clipper_util.h:134
static const ftype & eightpi2()
8 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:75
static ftype u2b(const ftype &x)
Convert isotropic U-value to B-factor.
Definition: clipper_util.h:107
Utility class.
Definition: clipper_util.h:59
static T min(const T &a, const T &b)
min
Definition: clipper_util.h:143
static T sqr(const T &a)
square
Definition: clipper_util.h:154
Util()
null constructor
Definition: clipper_util.cpp:70
static bool is_null(const ftype32 &f)
fast test for null floating value - only works if set from Util::null()
Definition: clipper_util.h:77
static ftype bessel_i0(const ftype &x)
Modified Bessel function of the first kind.
Definition: clipper_util.cpp:135
static const ftype & twopi()
2 pi
Definition: clipper_util.h:162
static bool is_null(const ftype64 &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, T &c)
swap the contents of two objects, using third as store (for speed)
Definition: clipper_util.h:151
static ftype b2u(const ftype &x)
Convert isotropic B-factor to U-value.
Definition: clipper_util.h:109
static ftype sim(const ftype &x)
Sim function: I1(X)/I0(X)
Definition: clipper_util.cpp:84
static const ftype & nan()
fast Util::nan() value
Definition: clipper_util.h:67
static ftype sim_integ(const ftype &x)
Integral of Sim function: log(I0(X))
Definition: clipper_util.cpp:112
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
ftype64 ftype
ftype definition for floating point representation
Definition: clipper_precision.h:58
static ftype rad2d(const ftype &x)
degree-to-radian conversion
Definition: clipper_util.cpp:157
static bool isnan(const ftype32 f)
slow general NaN test for compatibility
Definition: clipper_util.h:88
static ftype atanh(const ftype &x)
Arc hyperbolic tangent.
Definition: clipper_util.h:103
static int intf(const ftype &a)
Truncate-to-integer: int(floor(a))
Definition: clipper_util.h:127
static void swap(T &a, T &b)
swap the contents of two objects
Definition: clipper_util.h:148
static int intr(const ftype &a)
Round-to-integer: int(round(a))
Definition: clipper_util.h:131
static T isqrt(const T &n)
Integer square root (returns floor of sqrt)
Definition: clipper_util.h:156
static bool is_nan(const ftype32 f)
fast Util::nan() test
Definition: clipper_util.h:82
static ftype sim_deriv(const ftype &x)
Derivative of Sim function: d/dx( I1(X)/I0(x) )
Definition: clipper_util.cpp:119
static T max(const T &a, const T &b)
max
Definition: clipper_util.h:140
static const double & nand()
fast Util::nan() value
Definition: clipper_util.h:71
static T mean(const T &pl, const T &mi)
Convert F+/F- to mean F, with NaN checks.
Definition: clipper_util.h:111
static bool is_nan(const ftype64 f)
fast Util::nan() test
Definition: clipper_util.h:85
static void set_null(ftype32 &f)
set null floating value - a specific value of NaN used for missings
Definition: clipper_util.h:73
static const ftype & twopi2()
2 pi squared
Definition: clipper_util.h:164
static ftype d2rad(const ftype &x)
degree-to-radian conversion
Definition: clipper_util.cpp:153
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:118
static int mod(const int &a, const int &b)
Corrected mod.
Definition: clipper_util.h:137
static bool isnan(const ftype64 f)
slow general NaN test for compatibility
Definition: clipper_util.h:91
static const ftype & pi()
pi
Definition: clipper_util.h:160
static const float & nanf()
fast Util::nan() value
Definition: clipper_util.h:69
static T bound(const T &min, const T &val, const T &max)
bound a value by limits
Definition: clipper_util.h:146
static int intc(const ftype &a)
Truncate-to-integer above: int(ceil(a))
Definition: clipper_util.h:129