Skip to content
Snippets Groups Projects
Fast_Exponential.h 1.07 KiB
Newer Older
Laurent Chapon's avatar
Laurent Chapon committed
#ifndef MANTID_KERNEL_FAST_EXPONENTIAL_H_
#define MANTID_KERNEL_FAST_EXPONENTIAL_H_

namespace Mantid {
namespace Kernel {
Laurent Chapon's avatar
Laurent Chapon committed

/** Based on the article: A Fast, Compact Approximation of the Exponential
*Function
Laurent Chapon's avatar
Laurent Chapon committed
* by Nicol N. Schraudolph and on the slighly modified version:
* On a Fast, Compact Approximation of the Exponential Function
* Neural Computation archive
* Volume 12 ,  Issue 9  (September 2000)
* Pages 2009-2012
* Author: Gavin C. Cawley 	 University of East Anglia, Norwich, Norfolk
*NR4 7TJ, England
* This is actually a bit faster than a LookupTable with linear interpolation,
*however it seems less accurate
Laurent Chapon's avatar
Laurent Chapon committed
* with error as big as a few % for some values of y.
 **/

#define EXP_A (1048576 / M_LN2)
Laurent Chapon's avatar
Laurent Chapon committed
#define EXP_C 60801

inline double fast_exp(double y) {
  static union {
    double d;
Laurent Chapon's avatar
Laurent Chapon committed
#ifdef LITTLE_ENDIAN
    struct {
      int j, i;
    } n;
Laurent Chapon's avatar
Laurent Chapon committed
#else
    struct {
      int i, j;
    } n;
Laurent Chapon's avatar
Laurent Chapon committed
#endif
Laurent Chapon's avatar
Laurent Chapon committed

  _eco.n.i = (int)(EXP_A * (y)) + (1072693248 - EXP_C);
  _eco.n.j = 0;
Laurent Chapon's avatar
Laurent Chapon committed

  return _eco.d;
Laurent Chapon's avatar
Laurent Chapon committed
}

} // Namespace Kernel
} // Namespace Mantid

#endif /* FAST_EXPOENTIAL_H_ */