IMP  2.1.0
The Integrative Modeling Platform
exp.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/exp.h
3  * \brief An approximation of the exponential function.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM_EXP_H
10 #define IMPEM_EXP_H
11 
12 #include <IMP/em/em_config.h>
13 
14 /* An approximation of the exponential function.
15  Schraudolph, Nicol N. "A Fast, Compact Approximation of the exponential
16  function" Technical Report IDSIA-07-98
17  (Neural Computation 11(4))
18  The function returns values within 1-2% deviation of the correct ones in
19  the range (-700,700)
20 */
21 
22 #include <IMP/em/em_config.h>
23 #include <math.h>
24 
25 IMPEM_BEGIN_NAMESPACE
26 
27 /* Unions are similar to structs, but they differ in one aspect:
28  the fields of a union share the same position in memory.
29  The size of the union is the size of its largest field
30  (or larger if alignment so requires, for example on a SPARC machine
31  a union contains a double and a char [17] so its size is likely to be
32  24 because it needs 64-bit alignment).
33  What is the point of this? Unions provide multiple ways of viewing
34  the same memory location, allowing for \more efficient use of memory.
35  Most of the uses of unions are covered by object-oriented features of C++,
36  so it is more common in C. However, sometimes it is convenient to avoid
37  the formalities of object-oriented programming when performance is
38  important or when one knows that the item in question will not be extended.
39  http://en.wikibooks.org/wiki/C++_Programming/Union
40 */
41 typedef union
42 {
43  double d;
44  struct{
45 #ifdef BOOST_LITTLE_ENDIAN
46  int j,i;
47 #else
48  int i,j;
49 #endif
50  } n;
51 } _eco;
52 
53 /* Originally was (1048576/M_LN2); M_LN2 expanded out so that this header
54  can be correctly #include'd in other code on Windows machines without having
55  to #define the non-standard _USE_MATH_DEFINES macro */
56 #define IMP_EXP_A (1048576/0.693147180559945309417232121458176568)
57 #define IMP_EXP_C 60801
58 
59 
60 
61 
62 inline double EXP(float y) {
63  static _eco eco;
64  eco.n.i = (int) (IMP_EXP_A*(y) + (1072693248-IMP_EXP_C));
65  return eco.d;
66 }
67 
68 IMPEM_END_NAMESPACE
69 
70 #endif /* IMPEM_EXP_H */