Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logprob.h « src « mgizapp - github.com/moses-smt/mgiza.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f275498d57f09e3890e6a4ba904b22f29566ff10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*

EGYPT Toolkit for Statistical Machine Translation
Written by Yaser Al-Onaizan, Jan Curin, Michael Jahr, Kevin Knight, John Lafferty, Dan Melamed, David Purdy, Franz Och, Noah Smith, and David Yarowsky.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

*/
#ifndef _LOGPROB_H
#define _LOGPROB_H

// Routines to perform integer exponential arithmetic.
// A number x is represented as n, where x = b**n
// It is assumed that b > 1, something like b = 1.001

#include <iostream>
#include <cmath>
#include <algorithm>

//#define  MAX(A,B) ((A) > (B) ? (A) : (B))
//#define  MIN(A,B) ((A) > (B) ? (B) : (A))

#ifdef WIN32
#define round(x) floor(x+0.5)
#endif
class LogProb
{
public:
  // mj for cross entropy
  double base2() const {
    return (logr * logb2 / log((double)2));
  }

  // Constructors
  LogProb() : logr(zeron) {}
  LogProb(const LogProb &obj) : logr(obj.logr) {}
  LogProb(double x) : logr(x == 0.0 ? zeron : round(log(x)/logb2)) {}
  // destructor
  ~LogProb() {}                  // default destructor

  operator double() const {  // converts logr to (double) b**logr
    if (logr < nmin) return ntof[0];
    if (logr > nmax) return ntof[nmax-nmin];
    return ntof[logr-nmin];
  }

  LogProb &operator=(const LogProb &obj) {
    logr = obj.logr;
    return *this;
  }
  int operator!() const {
    return logr == zeron;
  }

  // iostream friend specifications
  friend std::ostream& operator<<(std::ostream& os, const LogProb &obj);
  friend std::istream& operator>>(std::istream& is, LogProb &obj);
  friend std::ostream& operator<<=(std::ostream& os, const LogProb &obj);
  friend std::istream& operator>>=(std::istream& is, LogProb &obj);

  // arithmetic operators
  LogProb &operator+=(const LogProb &add)   // logr2 = logb ( b**logr2 + b**logr1 )
  // Add two numbers represented as logarithms. Use the following method:
  //   b**n + b**m = b**n(1 + b**(m-n)), assuming n >= m.
  {
    if (add.logr == zeron)
      return *this;
    if (logr == zeron) {
      logr = add.logr;
      return *this;
    }
    int a = add.logr - logr;
    if (a > 0) {
      a = -a;
      logr = add.logr;
    }
    if (a < tblbnd)
      return *this;
    logr += addtbl[a-tblbnd];
    return *this;
  }

  LogProb &operator-=(const LogProb &);   // logr2 = logb ( b**logr2 + b**logr1 )
  LogProb operator*(const LogProb &mul) const { // logr3 = logr2 + logr1
    LogProb result;		// start out with result == 0
    if ((logr != zeron) && (mul.logr != zeron))
      result.logr = std::max(logr+mul.logr, zeron);
    return result;
  }
  LogProb operator*(double x) const { // logr3 = logr2 + logr1
    return (*this)*(LogProb)x;
  }
  LogProb operator^(const int i) const { // logr2 = logr1 * i
    LogProb result;		// start out with result == 0
    //      if ((logr != zeron) && (mul.logr != zeron))
    result.logr = logr * i ;
    return result;
  }
  LogProb &operator*=(const LogProb &mul) { // logr2 += logr1
    if ((logr == zeron) || (mul.logr == zeron))
      logr = zeron;
    else
      logr = std::max(logr+mul.logr, zeron);
    return *this;
  }
  LogProb operator/(const LogProb &div) const { // logr3 = logr2 -logr1
    LogProb result;
    if (logr != zeron)
      result.logr = std::max(logr - div.logr, zeron);
    return result;
  }
  LogProb &operator/=(const LogProb &div) { // logr2 -= logr1
    if (logr != zeron)
      logr = std::max(logr - div.logr, zeron);
    return *this;
  }
  LogProb operator+(const LogProb &l) const { // logr3 = logb ( b**logr2 + b**logr1 )
    LogProb result(*this);
    result += l;
    return result;
  }
  LogProb operator-(const LogProb &l) const { // logr3 = logb ( b**logr2 - b**logr1 )
    LogProb result(*this);
    result -= l;
    return result;
  }
  LogProb power(const int n) const { // logr2 = logr1 * int
    LogProb result(*this);
    result.logr *= n;
    return result;
  }

  // Conditional operators
  int operator<(const LogProb &obj)  const {
    return logr <  obj.logr;
  }
  int operator<=(const LogProb &obj) const {
    return logr <= obj.logr;
  }
  int operator>(const LogProb &obj)  const {
    return logr >  obj.logr;
  }
  int operator>=(const LogProb &obj) const {
    return logr >= obj.logr;
  }
  int operator==(const LogProb &obj) const {
    return logr == obj.logr;
  }
  int operator!=(const LogProb &obj) const {
    return logr != obj.logr;
  }
  int operator<(double d)  const {
    return ((double)*this) < d;
  }
  int operator<=(double d) const {
    return ((double)*this) <= d;
  }
  int operator>(double d)  const {
    return ((double)*this) >  d;
  }
  int operator>=(double d) const {
    return ((double)*this) >= d;
  }
  int operator==(double d) const {
    return ((double)*this) == d;
  }
  int operator!=(double d) const {
    return ((double)*this) != d;
  }


  LogProb &SetZero() {
    logr = zeron;  // representation of 0,
    return *this;
  }
  LogProb &SetOne() {
    logr = onen;  // 1, and
    return *this;
  }
  LogProb &SetInf() {
    logr = infn;  // inf in logarithm domain
    return *this;
  }

private:
  int logr;			// a representation of logarithm
  // static constants
  static const  int    initialized; // initialization flag
  static const  double b;
  static const  double logb2;
  static const  int    nmin, nmax;
  static const  int    tblbnd;
  static const  int    zeron, onen, infn;  // zero, one, and inf in log domain
  static const  int    max_2byte_integer, min_2byte_integer;

  // Arithmetic computation Tables
  static double *ntof;
  static int   *addtbl;
  static int   *subtbl;

  static int Initialize();

public:
  static void FreeTables();
  // constants for initializing LogProbs to 0 or 1
  static const LogProb zero;
  static const LogProb one;
  static const LogProb minus2;
  static const LogProb minus4;
  static const LogProb minus6;
  static const LogProb minus8;
  static const LogProb minus10;
  static const LogProb minus12;
  static const LogProb minus14;
  static const LogProb minus16;
};

// iostream friend operators
inline std::ostream &operator<<(std::ostream& os, const LogProb &obj)
{
  return os << (double) obj;         // output in linear domain, b**logr
}

inline std::istream &operator>>(std::istream& is, LogProb &obj)
{
  double d;
  is >> d;
  obj = d;
  return is;
}

inline std::ostream &operator<<=(std::ostream& os, const LogProb &obj) // write binary
{
  os.write((const char *)&obj.logr, sizeof(obj.logr));
  return os;
}

inline std::istream &operator>>=(std::istream& is, LogProb &obj)
{
  is.read((char *)&obj.logr, sizeof(obj.logr));
  return is;
}

#endif