21 #ifndef FST_SPARSE_POWER_WEIGHT_H_ 22 #define FST_SPARSE_POWER_WEIGHT_H_ 48 template <
class W,
class K =
int>
58 template <
class Iterator>
64 const W &default_weight = W::Zero())
65 :
Base(key, weight, default_weight) {}
84 static const std::string &
Type() {
85 static const std::string *
const type = [] {
86 std::string type = W::Type() +
"_^n";
87 if (
sizeof(K) !=
sizeof(uint32_t)) {
88 type +=
"_" + std::to_string(CHAR_BIT *
sizeof(K));
90 return new std::string(type);
96 return W::Properties() &
107 template <
class W,
class K,
class M>
110 const M &operator_mapper) {
117 template <
class W,
class K>
126 template <
class W,
class K>
130 return Minus(v1, v2);
135 template <
class W,
class K>
139 return Times(v1, v2);
144 template <
class W,
class K>
149 [type](
const K &k,
const W &v1,
const W &v2) {
150 return Divide(v1, v2, type);
155 template <
class W,
class K>
161 result =
Plus(result, it.Value().second);
166 template <
class W,
class K>
171 w1, w2, [delta](
const K &k,
const W &v1,
const W &v2) {
172 return ApproxEqual(v1, v2, delta) ? W::One() : W::Zero();
177 template <
class W,
class K>
182 return Times(w1, w2);
185 template <
class W,
class K>
190 return Times(w1, w2);
193 template <
class W,
class K>
199 return Divide(w1, w2, divide_type);
204 template <
class W,
class K>
211 bool allow_zero =
true,
size_t sparse_power_rank = 3)
212 : generate_(seed, allow_zero), sparse_power_rank_(sparse_power_rank) {}
216 for (
size_t i = 1; i <= sparse_power_rank_; ++i) {
217 weight.
PushBack(i, generate_(),
true);
224 const size_t sparse_power_rank_;
229 #endif // FST_SPARSE_POWER_WEIGHT_H_ void PushBack(const K &key, const W &weight, bool default_value_check=true)
SparsePowerWeight(const K &key, const W &weight, const W &default_weight=W::Zero())
static const SparsePowerWeight & NoWeight()
static const SparsePowerWeight & One()
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
void SparseTupleWeightMap(SparseTupleWeight< W, K > *result, const SparseTupleWeight< W, K > &w1, const SparseTupleWeight< W, K > &w2, const M &operator_mapper)
static const SparseTupleWeight & Zero()
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
static const SparseTupleWeight & One()
constexpr uint64_t kIdempotent
SparsePowerWeight< W, K > SparsePowerWeightMap(const SparsePowerWeight< W, K > &w1, const SparsePowerWeight< W, K > &w2, const M &operator_mapper)
static const SparseTupleWeight & NoWeight()
constexpr uint64_t kRightSemiring
SparsePowerWeight(Iterator begin, Iterator end)
SparsePowerWeight Quantize(float delta=kDelta) const
W DotProduct(const PowerWeight< W, n > &w1, const PowerWeight< W, n > &w2)
static const SparsePowerWeight & Zero()
Weight operator()() const
constexpr uint64_t kCommutative
SparsePowerWeight< typename W::ReverseWeight, K > ReverseWeight
WeightGenerate(uint64_t seed=std::random_device()(), bool allow_zero=true, size_t sparse_power_rank=3)
static const std::string & Type()
LogWeightTpl< T > Minus(const LogWeightTpl< T > &w1, const LogWeightTpl< T > &w2)
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
SparseTupleWeight Quantize(float delta=kDelta) const
SparsePowerWeight(const Base &weight)
static constexpr uint64_t Properties()
constexpr uint64_t kLeftSemiring
ReverseWeight Reverse() const
ReverseWeight Reverse() const
bool ApproxEqual(const ErrorWeight &, const ErrorWeight &, float)