CMS 3D CMS Logo

liblogintpack.h
Go to the documentation of this file.
1 #ifndef liblogintpack_h
2 #define liblogintpack_h
3 
4 #include <cmath>
5 
6 namespace logintpack
7 {
9  // note that abs(unpack(smallestNegative)) == unpack(1), i.e. there
10  // is no "x" such that "unpack(x) == -unpack(0)"
12  inline int16_t pack16logCeil(double x,double lmin, double lmax, uint16_t base=32768)
13  {
14  if(base>32768) base=32768;
15  const double l = std::log(std::abs(x));
16  const double centered = (l-lmin)/(lmax-lmin)*base;
17  int16_t r=std::ceil(centered);
18  if(centered >= base-1) r=base-1;
19  if(centered < 0) r=0;
20  if(x<0) r = r==0 ? -1 : -r;
21  return r;
22  }
23 
24 
25  inline int16_t pack16log(double x,double lmin, double lmax, uint16_t base=32768)
26  {
27  if(base>32768) base=32768;
28  const float delta=(log(1.+exp((lmax-lmin)/base))-log(2.))*base/(lmax-lmin);
29  const double l = std::log(std::abs(x));
30  const double centered = (l-lmin)/(lmax-lmin)*base;
31  int16_t r=std::floor(centered);
32  if(centered-r>delta) r+=1;
33  if(centered >= base-1) r=base-1;
34  if(centered < 0) r=0;
35  if(x<0) r = r==0 ? -1 : -r;
36  return r;
37  }
38 
41  inline int16_t pack16logClosed(double x,double lmin, double lmax, uint16_t base=32768)
42  {
43  if(base>32768) base=32768;
44  const double l = std::log(std::abs(x));
45  const double centered = (l-lmin)/(lmax-lmin)*(base-1);
46  int16_t r=round(centered);
47  if(centered >= base-1) r=base-1;
48  if(centered < 0) r=0;
49  if(x<0) r = r==0 ? -1 : -r;
50  return r;
51  }
52 
53 
54  inline double unpack16log(int16_t i,double lmin, double lmax, uint16_t base=32768)
55  {
56  if(base>32768) base=32768;
57  const double basef=base;
58  const double l=lmin+std::abs(i)/basef*(lmax-lmin);
59  const double val=std::exp(l);
60  if(i<0) return -val; else return val;
61  }
62 
64  inline double unpack16logClosed(int16_t i,double lmin, double lmax, uint16_t base=32768)
65  {
66  if(base>32768) base=32768;
67  const double basef=base-1;
68  double l=lmin+std::abs(i)/basef*(lmax-lmin);
69  if (std::abs(i) == base-1) l = lmax;
70  const double val=std::exp(l);
71  if(i<0) return -val; else return val;
72  }
73 
74 
75 
76  inline
77  int8_t pack8logCeil(double x,double lmin, double lmax, uint8_t base=128)
78  {
79  if(base>128) base=128;
80  const double l = std::log(std::abs(x));
81  const double centered = (l-lmin)/(lmax-lmin)*base;
82  int8_t r=std::ceil(centered);
83  if(centered >= base-1) r=base-1;
84  if(centered < 0) r=0;
85  if(x<0) r = r==0 ? -1 : -r;
86  return r;
87  }
88 
89  inline
90  int8_t pack8log(double x,double lmin, double lmax, uint8_t base=128)
91  {
92  if(base>128) base=128;
93  const double l = std::log(std::abs(x));
94  const double centered = (l-lmin)/(lmax-lmin)*base;
95  int8_t r=centered;
96  if(centered >= base-1) r=base-1;
97  if(centered < 0) r=0;
98  if(x<0) r = r==0 ? -1 : -r;
99  return r;
100  }
101 
104  inline
105  int8_t pack8logClosed(double x,double lmin, double lmax, uint8_t base=128)
106  {
107  if(base>128) base=128;
108  const double l = std::log(std::abs(x));
109  const double centered = (l-lmin)/(lmax-lmin)*(base-1);
110  int8_t r=round(centered);
111  if(centered >= base-1) r=base-1;
112  if(centered < 0) r=0;
113  if(x<0) r = r==0 ? -1 : -r;
114  return r;
115  }
116 
117  inline
118  double unpack8log(int8_t i,double lmin, double lmax, uint8_t base=128)
119  {
120  if(base>128) base=128;
121  const double basef=base;
122  const double l=lmin+std::abs(i)/basef*(lmax-lmin);
123  const double val=std::exp(l);
124  if(i<0) return -val; else return val;
125  }
126 
128  inline
129  double unpack8logClosed(int8_t i,double lmin, double lmax, uint8_t base=128)
130  {
131  if(base>128) base=128;
132  const double basef=base-1;
133  double l=lmin+std::abs(i)/basef*(lmax-lmin);
134  if (std::abs(i) == base-1) l = lmax;
135  const double val=std::exp(l);
136  if(i<0) return -val; else return val;
137  }
138 
139 }
140 #endif
dbl * delta
Definition: mlp_gen.cc:36
double unpack8log(int8_t i, double lmin, double lmax, uint8_t base=128)
int8_t pack8log(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:90
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:54
constexpr int8_t smallestNegative
Definition: liblogintpack.h:11
#define constexpr
int8_t pack8logCeil(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:77
double unpack16logClosed(int16_t i, double lmin, double lmax, uint16_t base=32768)
reverse of pack8logClosed
Definition: liblogintpack.h:64
int8_t pack8logClosed(double x, double lmin, double lmax, uint8_t base=128)
int16_t pack16logClosed(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:41
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
base
Make Sure CMSSW is Setup ##.
constexpr int8_t smallestPositive
Definition: liblogintpack.h:8
double unpack8logClosed(int8_t i, double lmin, double lmax, uint8_t base=128)
reverse of pack8logClosed
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:25
int16_t pack16logCeil(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:12