/usr/include/fst/signed-log-weight.h is in libfst-dev 1.5.3+r3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | // See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// LogWeight along with sign information that represents the value X in the
// linear domain as <sign(X), -ln(|X|)>
//
// The sign is a TropicalWeight:
// positive, TropicalWeight.Value() > 0.0, recommended value 1.0
// negative, TropicalWeight.Value() <= 0.0, recommended value -1.0
#ifndef FST_LIB_SIGNED_LOG_WEIGHT_H_
#define FST_LIB_SIGNED_LOG_WEIGHT_H_
#include <fst/float-weight.h>
#include <fst/pair-weight.h>
#include <fst/product-weight.h>
namespace fst {
template <class T>
class SignedLogWeightTpl : public PairWeight<TropicalWeight, LogWeightTpl<T>> {
public:
typedef TropicalWeight X1;
typedef LogWeightTpl<T> X2;
using PairWeight<X1, X2>::Value1;
using PairWeight<X1, X2>::Value2;
using PairWeight<X1, X2>::Reverse;
using PairWeight<X1, X2>::Quantize;
using PairWeight<X1, X2>::Member;
typedef SignedLogWeightTpl<T> ReverseWeight;
SignedLogWeightTpl() : PairWeight<X1, X2>() {}
SignedLogWeightTpl(const SignedLogWeightTpl<T> &w) : PairWeight<X1, X2>(w) {}
SignedLogWeightTpl(const PairWeight<X1, X2> &w) : PairWeight<X1, X2>(w) {}
SignedLogWeightTpl(const X1 &x1, const X2 &x2) : PairWeight<X1, X2>(x1, x2) {}
static const SignedLogWeightTpl<T> &Zero() {
static const SignedLogWeightTpl<T> zero(X1(1.0), X2::Zero());
return zero;
}
static const SignedLogWeightTpl<T> &One() {
static const SignedLogWeightTpl<T> one(X1(1.0), X2::One());
return one;
}
static const SignedLogWeightTpl<T> &NoWeight() {
static const SignedLogWeightTpl<T> no_weight(X1(1.0), X2::NoWeight());
return no_weight;
}
static const string &Type() {
static const string type = "signed_log_" + X1::Type() + "_" + X2::Type();
return type;
}
ProductWeight<X1, X2> Quantize(float delta = kDelta) const {
return PairWeight<X1, X2>::Quantize();
}
ReverseWeight Reverse() const { return PairWeight<X1, X2>::Reverse(); }
bool Member() const { return PairWeight<X1, X2>::Member(); }
static uint64 Properties() {
// not idempotent nor path
return kLeftSemiring | kRightSemiring | kCommutative;
}
size_t Hash() const {
size_t h1;
if (Value2() == X2::Zero() || Value1().Value() > 0.0)
h1 = TropicalWeight(1.0).Hash();
else
h1 = TropicalWeight(-1.0).Hash();
size_t h2 = Value2().Hash();
const int lshift = 5;
const int rshift = CHAR_BIT * sizeof(size_t) - 5;
return h1 << lshift ^ h1 >> rshift ^ h2;
}
};
template <class T>
inline SignedLogWeightTpl<T> Plus(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2) {
if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl<T>::NoWeight();
bool s1 = w1.Value1().Value() > 0.0;
bool s2 = w2.Value1().Value() > 0.0;
T f1 = w1.Value2().Value();
T f2 = w2.Value2().Value();
if (f1 == FloatLimits<T>::PosInfinity())
return w2;
else if (f2 == FloatLimits<T>::PosInfinity())
return w1;
else if (f1 == f2) {
if (s1 == s2)
return SignedLogWeightTpl<T>(w1.Value1(), (f2 - log(2.0F)));
else
return SignedLogWeightTpl<T>::Zero();
} else if (f1 > f2) {
if (s1 == s2) {
return SignedLogWeightTpl<T>(w1.Value1(),
(f2 - log(1.0F + exp(f2 - f1))));
} else {
return SignedLogWeightTpl<T>(w2.Value1(),
(f2 - log(1.0F - exp(f2 - f1))));
}
} else {
if (s2 == s1) {
return SignedLogWeightTpl<T>(w2.Value1(),
(f1 - log(1.0F + exp(f1 - f2))));
} else {
return SignedLogWeightTpl<T>(w1.Value1(),
(f1 - log(1.0F - exp(f1 - f2))));
}
}
}
template <class T>
inline SignedLogWeightTpl<T> Minus(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2) {
SignedLogWeightTpl<T> minus_w2(-w2.Value1().Value(), w2.Value2());
return Plus(w1, minus_w2);
}
template <class T>
inline SignedLogWeightTpl<T> Times(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2) {
if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl<T>::NoWeight();
bool s1 = w1.Value1().Value() > 0.0;
bool s2 = w2.Value1().Value() > 0.0;
T f1 = w1.Value2().Value();
T f2 = w2.Value2().Value();
if (s1 == s2)
return SignedLogWeightTpl<T>(TropicalWeight(1.0), (f1 + f2));
else
return SignedLogWeightTpl<T>(TropicalWeight(-1.0), (f1 + f2));
}
template <class T>
inline SignedLogWeightTpl<T> Divide(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2,
DivideType typ = DIVIDE_ANY) {
if (!w1.Member() || !w2.Member()) return SignedLogWeightTpl<T>::NoWeight();
bool s1 = w1.Value1().Value() > 0.0;
bool s2 = w2.Value1().Value() > 0.0;
T f1 = w1.Value2().Value();
T f2 = w2.Value2().Value();
if (f2 == FloatLimits<T>::PosInfinity())
return SignedLogWeightTpl<T>(TropicalWeight(1.0),
FloatLimits<T>::NumberBad());
else if (f1 == FloatLimits<T>::PosInfinity())
return SignedLogWeightTpl<T>(TropicalWeight(1.0),
FloatLimits<T>::PosInfinity());
else if (s1 == s2)
return SignedLogWeightTpl<T>(TropicalWeight(1.0), (f1 - f2));
else
return SignedLogWeightTpl<T>(TropicalWeight(-1.0), (f1 - f2));
}
template <class T>
inline bool ApproxEqual(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2, float delta = kDelta) {
bool s1 = w1.Value1().Value() > 0.0;
bool s2 = w2.Value1().Value() > 0.0;
if (s1 == s2) {
return ApproxEqual(w1.Value2(), w2.Value2(), delta);
} else {
return w1.Value2() == LogWeightTpl<T>::Zero() &&
w2.Value2() == LogWeightTpl<T>::Zero();
}
}
template <class T>
inline bool operator==(const SignedLogWeightTpl<T> &w1,
const SignedLogWeightTpl<T> &w2) {
bool s1 = w1.Value1().Value() > 0.0;
bool s2 = w2.Value1().Value() > 0.0;
if (s1 == s2)
return w1.Value2() == w2.Value2();
else
return (w1.Value2() == LogWeightTpl<T>::Zero()) &&
(w2.Value2() == LogWeightTpl<T>::Zero());
}
// Single-precision signed-log weight
typedef SignedLogWeightTpl<float> SignedLogWeight;
// Double-precision signed-log weight
typedef SignedLogWeightTpl<double> SignedLog64Weight;
//
// WEIGHT CONVERTER SPECIALIZATIONS.
//
template <class W1, class W2>
bool SignedLogConvertCheck(W1 w) {
if (w.Value1().Value() < 0.0) {
FSTERROR() << "WeightConvert: Can't convert weight from " << W1::Type()
<< " to " << W2::Type();
return false;
}
return true;
}
// Convert to tropical
template <>
struct WeightConvert<SignedLogWeight, TropicalWeight> {
TropicalWeight operator()(SignedLogWeight w) const {
if (!SignedLogConvertCheck<SignedLogWeight, TropicalWeight>(w))
return TropicalWeight::NoWeight();
return w.Value2().Value();
}
};
template <>
struct WeightConvert<SignedLog64Weight, TropicalWeight> {
TropicalWeight operator()(SignedLog64Weight w) const {
if (!SignedLogConvertCheck<SignedLog64Weight, TropicalWeight>(w))
return TropicalWeight::NoWeight();
return w.Value2().Value();
}
};
// Convert to log
template <>
struct WeightConvert<SignedLogWeight, LogWeight> {
LogWeight operator()(SignedLogWeight w) const {
if (!SignedLogConvertCheck<SignedLogWeight, LogWeight>(w))
return LogWeight::NoWeight();
return w.Value2().Value();
}
};
template <>
struct WeightConvert<SignedLog64Weight, LogWeight> {
LogWeight operator()(SignedLog64Weight w) const {
if (!SignedLogConvertCheck<SignedLog64Weight, LogWeight>(w))
return LogWeight::NoWeight();
return w.Value2().Value();
}
};
// Convert to log64
template <>
struct WeightConvert<SignedLogWeight, Log64Weight> {
Log64Weight operator()(SignedLogWeight w) const {
if (!SignedLogConvertCheck<SignedLogWeight, Log64Weight>(w))
return Log64Weight::NoWeight();
return w.Value2().Value();
}
};
template <>
struct WeightConvert<SignedLog64Weight, Log64Weight> {
Log64Weight operator()(SignedLog64Weight w) const {
if (!SignedLogConvertCheck<SignedLog64Weight, Log64Weight>(w))
return Log64Weight::NoWeight();
return w.Value2().Value();
}
};
// Convert to signed log
template <>
struct WeightConvert<TropicalWeight, SignedLogWeight> {
SignedLogWeight operator()(TropicalWeight w) const {
TropicalWeight x1 = 1.0;
LogWeight x2 = w.Value();
return SignedLogWeight(x1, x2);
}
};
template <>
struct WeightConvert<LogWeight, SignedLogWeight> {
SignedLogWeight operator()(LogWeight w) const {
TropicalWeight x1 = 1.0;
LogWeight x2 = w.Value();
return SignedLogWeight(x1, x2);
}
};
template <>
struct WeightConvert<Log64Weight, SignedLogWeight> {
SignedLogWeight operator()(Log64Weight w) const {
TropicalWeight x1 = 1.0;
LogWeight x2 = w.Value();
return SignedLogWeight(x1, x2);
}
};
template <>
struct WeightConvert<SignedLog64Weight, SignedLogWeight> {
SignedLogWeight operator()(SignedLog64Weight w) const {
TropicalWeight x1 = w.Value1();
LogWeight x2 = w.Value2().Value();
return SignedLogWeight(x1, x2);
}
};
// Convert to signed log64
template <>
struct WeightConvert<TropicalWeight, SignedLog64Weight> {
SignedLog64Weight operator()(TropicalWeight w) const {
TropicalWeight x1 = 1.0;
Log64Weight x2 = w.Value();
return SignedLog64Weight(x1, x2);
}
};
template <>
struct WeightConvert<LogWeight, SignedLog64Weight> {
SignedLog64Weight operator()(LogWeight w) const {
TropicalWeight x1 = 1.0;
Log64Weight x2 = w.Value();
return SignedLog64Weight(x1, x2);
}
};
template <>
struct WeightConvert<Log64Weight, SignedLog64Weight> {
SignedLog64Weight operator()(Log64Weight w) const {
TropicalWeight x1 = 1.0;
Log64Weight x2 = w.Value();
return SignedLog64Weight(x1, x2);
}
};
template <>
struct WeightConvert<SignedLogWeight, SignedLog64Weight> {
SignedLog64Weight operator()(SignedLogWeight w) const {
TropicalWeight x1 = w.Value1();
Log64Weight x2 = w.Value2().Value();
return SignedLog64Weight(x1, x2);
}
};
} // namespace fst
#endif // FST_LIB_SIGNED_LOG_WEIGHT_H_
|