FST  openfst-1.7.3
OpenFst Library
power-weight.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Cartesian power weight semiring operation definitions.
5 
6 #ifndef FST_POWER_WEIGHT_H_
7 #define FST_POWER_WEIGHT_H_
8 
9 #include <string>
10 
11 #include <fst/tuple-weight.h>
12 #include <fst/weight.h>
13 
14 
15 namespace fst {
16 
17 // Cartesian power semiring: W ^ n
18 //
19 // Forms:
20 // - a left semimodule when W is a left semiring,
21 // - a right semimodule when W is a right semiring,
22 // - a bisemimodule when W is a semiring,
23 // the free semimodule of rank n over W
24 // The Times operation is overloaded to provide the left and right scalar
25 // products.
26 template <class W, size_t n>
27 class PowerWeight : public TupleWeight<W, n> {
28  public:
30 
32 
33  explicit PowerWeight(const TupleWeight<W, n> &weight)
34  : TupleWeight<W, n>(weight) {}
35 
36  template <class Iterator>
37  PowerWeight(Iterator begin, Iterator end) : TupleWeight<W, n>(begin, end) {}
38 
39  // Initialize component `index` to `weight`; initialize all other components
40  // to `default_weight`
41  PowerWeight(size_t index, const W &weight,
42  const W &default_weight = W::Zero())
43  : TupleWeight<W, n>(index, weight, default_weight) {}
44 
45  static const PowerWeight &Zero() {
46  static const PowerWeight zero(TupleWeight<W, n>::Zero());
47  return zero;
48  }
49 
50  static const PowerWeight &One() {
51  static const PowerWeight one(TupleWeight<W, n>::One());
52  return one;
53  }
54 
55  static const PowerWeight &NoWeight() {
56  static const PowerWeight no_weight(TupleWeight<W, n>::NoWeight());
57  return no_weight;
58  }
59 
60  static const std::string &Type() {
61  static const std::string *const type =
62  new std::string(W::Type() + "_^" + std::to_string(n));
63  return *type;
64  }
65 
66  static constexpr uint64 Properties() {
67  return W::Properties() &
69  }
70 
71  PowerWeight Quantize(float delta = kDelta) const {
73  }
74 
77  }
78 };
79 
80 // Semiring plus operation.
81 template <class W, size_t n>
83  const PowerWeight<W, n> &w2) {
84  PowerWeight<W, n> result;
85  for (size_t i = 0; i < n; ++i) {
86  result.SetValue(i, Plus(w1.Value(i), w2.Value(i)));
87  }
88  return result;
89 }
90 
91 // Semiring times operation.
92 template <class W, size_t n>
94  const PowerWeight<W, n> &w2) {
95  PowerWeight<W, n> result;
96  for (size_t i = 0; i < n; ++i) {
97  result.SetValue(i, Times(w1.Value(i), w2.Value(i)));
98  }
99  return result;
100 }
101 
102 // Semiring divide operation.
103 template <class W, size_t n>
105  const PowerWeight<W, n> &w2,
106  DivideType type = DIVIDE_ANY) {
107  PowerWeight<W, n> result;
108  for (size_t i = 0; i < n; ++i) {
109  result.SetValue(i, Divide(w1.Value(i), w2.Value(i), type));
110  }
111  return result;
112 }
113 
114 // Semimodule left scalar product.
115 template <class W, size_t n>
116 inline PowerWeight<W, n> Times(const W &scalar,
117  const PowerWeight<W, n> &weight) {
118  PowerWeight<W, n> result;
119  for (size_t i = 0; i < n; ++i) {
120  result.SetValue(i, Times(scalar, weight.Value(i)));
121  }
122  return result;
123 }
124 
125 // Semimodule right scalar product.
126 template <class W, size_t n>
128  const W &scalar) {
129  PowerWeight<W, n> result;
130  for (size_t i = 0; i < n; ++i) {
131  result.SetValue(i, Times(weight.Value(i), scalar));
132  }
133  return result;
134 }
135 
136 // Semimodule dot product.
137 template <class W, size_t n>
138 inline W DotProduct(const PowerWeight<W, n> &w1, const PowerWeight<W, n> &w2) {
139  W result(W::Zero());
140  for (size_t i = 0; i < n; ++i) {
141  result = Plus(result, Times(w1.Value(i), w2.Value(i)));
142  }
143  return result;
144 }
145 
146 // This function object generates weights over the Cartesian power of rank
147 // n over the underlying weight. This is intended primarily for testing.
148 template <class W, size_t n>
150  public:
153 
154  explicit WeightGenerate(bool allow_zero = true) : generate_(allow_zero) {}
155 
156  Weight operator()() const {
157  Weight result;
158  for (size_t i = 0; i < n; ++i) result.SetValue(i, generate_());
159  return result;
160  }
161 
162  private:
163  Generate generate_;
164 };
165 
166 } // namespace fst
167 
168 #endif // FST_POWER_WEIGHT_H_
void SetValue(size_t i, const W &w)
Definition: tuple-weight.h:100
ExpectationWeight< X1, X2 > Divide(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2, DivideType typ=DIVIDE_ANY)
uint64_t uint64
Definition: types.h:32
PowerWeight< typename W::ReverseWeight, n > ReverseWeight
Definition: power-weight.h:29
constexpr uint64 kRightSemiring
Definition: weight.h:115
PowerWeight(Iterator begin, Iterator end)
Definition: power-weight.h:37
ExpectationWeight< X1, X2 > Times(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
constexpr uint64 kCommutative
Definition: weight.h:120
constexpr uint64 kLeftSemiring
Definition: weight.h:112
ReverseWeight Reverse() const
Definition: power-weight.h:75
static constexpr uint64 Properties()
Definition: power-weight.h:66
W DotProduct(const PowerWeight< W, n > &w1, const PowerWeight< W, n > &w2)
Definition: power-weight.h:138
const W & Value(size_t i) const
Definition: tuple-weight.h:98
static const PowerWeight & NoWeight()
Definition: power-weight.h:55
constexpr uint64 kIdempotent
Definition: weight.h:123
ExpectationWeight< X1, X2 > Plus(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
PowerWeight(const TupleWeight< W, n > &weight)
Definition: power-weight.h:33
static const std::string & Type()
Definition: power-weight.h:60
PowerWeight(size_t index, const W &weight, const W &default_weight=W::Zero())
Definition: power-weight.h:41
static const PowerWeight & One()
Definition: power-weight.h:50
DivideType
Definition: weight.h:142
constexpr float kDelta
Definition: weight.h:109
PowerWeight Quantize(float delta=kDelta) const
Definition: power-weight.h:71
static const PowerWeight & Zero()
Definition: power-weight.h:45