25 #ifndef FST_SIGNED_LOG_WEIGHT_H_ 26 #define FST_SIGNED_LOG_WEIGHT_H_ 80 static const std::string &
Type() {
81 static const std::string *
const type =
110 size_t h2 =
Value2().Hash();
111 static constexpr
int lshift = 5;
112 static constexpr
int rshift = CHAR_BIT *
sizeof(size_t) - 5;
113 return h1 << lshift ^ h1 >> rshift ^ h2;
125 const bool equal = (s1 == s2);
126 const auto f1 = w1.
Value2().Value();
127 const auto f2 = w2.
Value2().Value();
132 }
else if (f1 == f2) {
138 }
else if (f1 > f2) {
161 return Plus(w1, minus_w2);
171 const auto f1 = w1.
Value2().Value();
172 const auto f2 = w2.
Value2().Value();
188 const auto f1 = w1.
Value2().Value();
189 const auto f2 = w2.
Value2().Value();
196 }
else if (s1 == s2) {
328 template <
class W1,
class W2>
330 if (weight.Value1().Value() < 0.0) {
331 FSTERROR() <<
"WeightConvert: Can't convert weight " << weight <<
" from " 351 const auto f = w.
Value2().Value();
352 const bool equal = (ssum_ == sw);
360 }
else if (f == sum_) {
368 }
else if (f > sum_) {
388 ssum_ = w.IsPositive();
389 sum_ = w.Value2().Value();
403 if (!SignedLogConvertCheck<SignedLogWeight, TropicalWeight>(weight)) {
413 if (!SignedLogConvertCheck<SignedLog64Weight, TropicalWeight>(weight)) {
424 if (!SignedLogConvertCheck<SignedLogWeight, LogWeight>(weight)) {
434 if (!SignedLogConvertCheck<SignedLog64Weight, LogWeight>(weight)) {
445 if (!SignedLogConvertCheck<SignedLogWeight, Log64Weight>(weight)) {
455 if (!SignedLogConvertCheck<SignedLog64Weight, Log64Weight>(weight)) {
482 exp(-weight.
Value2().Value()));
490 exp(-weight.
Value2().Value()));
520 -log(std::abs(weight.
Value())));
528 -log(std::abs(weight.
Value())));
565 -log(std::abs(weight.
Value())));
573 -log(std::abs(weight.
Value())));
595 bool allow_zero =
true,
598 allow_zero_(allow_zero),
599 num_random_weights_(num_random_weights) {}
602 static constexpr
W1 negative(-1.0);
603 static constexpr
W1 positive(+1.0);
604 const bool sign = std::bernoulli_distribution(.5)(rand_);
605 const int sample = std::uniform_int_distribution<>(
606 0, num_random_weights_ + allow_zero_ - 1)(rand_);
607 if (allow_zero_ && sample == num_random_weights_) {
610 return Weight(sign ? positive : negative,
W2(sample));
614 mutable std::mt19937_64 rand_;
615 const bool allow_zero_;
616 const size_t num_random_weights_;
621 #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