25 #ifndef FST_SIGNED_LOG_WEIGHT_H_ 26 #define FST_SIGNED_LOG_WEIGHT_H_ 74 static const std::string &
Type() {
75 static const std::string *
const type =
104 size_t h2 =
Value2().Hash();
105 static constexpr
int lshift = 5;
106 static constexpr
int rshift = CHAR_BIT *
sizeof(size_t) - 5;
107 return h1 << lshift ^ h1 >> rshift ^ h2;
119 const bool equal = (s1 == s2);
120 const auto f1 = w1.
Value2().Value();
121 const auto f2 = w2.
Value2().Value();
126 }
else if (f1 == f2) {
132 }
else if (f1 > f2) {
155 return Plus(w1, minus_w2);
165 const auto f1 = w1.
Value2().Value();
166 const auto f2 = w2.
Value2().Value();
182 const auto f1 = w1.
Value2().Value();
183 const auto f2 = w2.
Value2().Value();
190 }
else if (s1 == s2) {
322 template <
class W1,
class W2>
324 if (weight.Value1().Value() < 0.0) {
325 FSTERROR() <<
"WeightConvert: Can't convert weight " << weight <<
" from " 345 const auto f = w.
Value2().Value();
346 const bool equal = (ssum_ == sw);
354 }
else if (f == sum_) {
362 }
else if (f > sum_) {
382 ssum_ = w.IsPositive();
383 sum_ = w.Value2().Value();
397 if (!SignedLogConvertCheck<SignedLogWeight, TropicalWeight>(weight)) {
407 if (!SignedLogConvertCheck<SignedLog64Weight, TropicalWeight>(weight)) {
418 if (!SignedLogConvertCheck<SignedLogWeight, LogWeight>(weight)) {
428 if (!SignedLogConvertCheck<SignedLog64Weight, LogWeight>(weight)) {
439 if (!SignedLogConvertCheck<SignedLogWeight, Log64Weight>(weight)) {
449 if (!SignedLogConvertCheck<SignedLog64Weight, Log64Weight>(weight)) {
476 exp(-weight.
Value2().Value()));
484 exp(-weight.
Value2().Value()));
514 -log(std::abs(weight.
Value())));
522 -log(std::abs(weight.
Value())));
559 -log(std::abs(weight.
Value())));
567 -log(std::abs(weight.
Value())));
589 bool allow_zero =
true,
592 allow_zero_(allow_zero),
593 num_random_weights_(num_random_weights) {}
596 static constexpr
W1 negative(-1.0);
597 static constexpr
W1 positive(+1.0);
598 const bool sign = std::bernoulli_distribution(.5)(rand_);
599 const int sample = std::uniform_int_distribution<>(
600 0, num_random_weights_ + allow_zero_ - 1)(rand_);
601 if (allow_zero_ && sample == num_random_weights_) {
604 return Weight(sign ? positive : negative,
W2(sample));
608 mutable std::mt19937_64 rand_;
609 const bool allow_zero_;
610 const size_t num_random_weights_;
615 #endif // FST_SIGNED_LOG_WEIGHT_H_ static const SignedLogWeightTpl & NoWeight()
Real64Weight operator()(const SignedLogWeight &weight) const
TropicalWeight operator()(const SignedLog64Weight &weight) const
Real64Weight operator()(const SignedLog64Weight &weight) const
RealWeightTpl< float > RealWeight
static const SignedLogWeightTpl & Zero()
LogWeight operator()(const SignedLogWeight &weight) const
SignedLog64Weight operator()(const RealWeight &weight) const
RealWeight operator()(const SignedLogWeight &weight) const
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
SignedLogWeightTpl(const PairWeight< W1, W2 > &weight)
void Reset(Weight w=Weight::Zero())
SignedLog64Weight operator()(const TropicalWeight &weight) const
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
Weight operator()() const
SignedLogWeight operator()(const TropicalWeight &weight) const
const LogWeightTpl< T > & Value2() const
static constexpr uint64_t Properties()
static constexpr LogWeightTpl Zero()
SignedLogWeightTpl Quantize(float delta=kDelta) const
Adder(Weight w=Weight::Zero())
SignedLogWeight operator()(const Log64Weight &weight) const
static constexpr T PosInfinity()
constexpr uint64_t kRightSemiring
static constexpr LogWeightTpl One()
LogWeightTpl< double > Log64Weight
Log64Weight operator()(const SignedLog64Weight &weight) const
LogWeightTpl< float > LogWeight
WeightGenerate(uint64_t seed=std::random_device()(), bool allow_zero=true, size_t num_random_weights=kNumRandomWeights)
TropicalWeightTpl< float > TropicalWeight
bool operator!=(const ErrorWeight &, const ErrorWeight &)
static const SignedLogWeightTpl & One()
static constexpr LogWeightTpl NoWeight()
constexpr uint64_t kCommutative
SignedLogWeightTpl(const W1 &w1, const W2 &w2)
Weight Add(const Weight &w)
SignedLog64Weight operator()(const SignedLogWeight &weight) const
static constexpr TropicalWeightTpl< T > NoWeight()
double LogPosExp(double x)
double KahanLogSum(double a, double b, double *c)
RealWeight operator()(const SignedLog64Weight &weight) const
SignedLogWeightTpl< double > SignedLog64Weight
static const std::string & Type()
SignedLog64Weight operator()(const Real64Weight &weight) const
SignedLogWeight operator()(const RealWeight &weight) const
SignedLogWeightTpl() noexcept
double LogNegExp(double x)
LogWeightTpl< T > Minus(const LogWeightTpl< T > &w1, const LogWeightTpl< T > &w2)
SignedLogWeightTpl(const W2 &w2)
static const std::string & Type()
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
bool operator==(const ErrorWeight &, const ErrorWeight &)
constexpr size_t kNumRandomWeights
Log64Weight operator()(const SignedLogWeight &weight) const
bool SignedLogConvertCheck(W1 weight)
RealWeightTpl< double > Real64Weight
SignedLog64Weight operator()(const Log64Weight &weight) const
SignedLogWeight operator()(const SignedLog64Weight &weight) const
ReverseWeight Reverse() const
SignedLogWeight operator()(const Real64Weight &weight) const
SignedLog64Weight operator()(const LogWeight &weight) const
SignedLogWeight operator()(const LogWeight &weight) const
LogWeight operator()(const SignedLog64Weight &weight) const
constexpr uint64_t kLeftSemiring
SignedLogWeightTpl< float > SignedLogWeight
static const std::string & Type()
constexpr const T & Value() const
bool ApproxEqual(const ErrorWeight &, const ErrorWeight &, float)
TropicalWeight operator()(const SignedLogWeight &weight) const
double KahanLogDiff(double a, double b, double *c)
const TropicalWeight & Value1() const