FST  openfst-1.8.2
OpenFst Library
power-weight.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Cartesian power weight semiring operation definitions.
19 
20 #ifndef FST_POWER_WEIGHT_H_
21 #define FST_POWER_WEIGHT_H_
22 
23 #include <cstdint>
24 #include <random>
25 #include <string>
26 
27 
28 #include <fst/tuple-weight.h>
29 #include <fst/weight.h>
30 
31 
32 namespace fst {
33 
34 // Cartesian power semiring: W ^ n
35 //
36 // Forms:
37 // - a left semimodule when W is a left semiring,
38 // - a right semimodule when W is a right semiring,
39 // - a bisemimodule when W is a semiring,
40 // the free semimodule of rank n over W
41 // The Times operation is overloaded to provide the left and right scalar
42 // products.
43 template <class W, size_t n>
44 class PowerWeight : public TupleWeight<W, n> {
45  public:
47 
49 
50  explicit PowerWeight(const TupleWeight<W, n> &weight)
51  : TupleWeight<W, n>(weight) {}
52 
53  template <class Iterator>
54  PowerWeight(Iterator begin, Iterator end) : TupleWeight<W, n>(begin, end) {}
55 
56  // Initialize component `index` to `weight`; initialize all other components
57  // to `default_weight`
58  PowerWeight(size_t index, const W &weight,
59  const W &default_weight = W::Zero())
60  : TupleWeight<W, n>(index, weight, default_weight) {}
61 
62  static const PowerWeight &Zero() {
63  static const PowerWeight zero(TupleWeight<W, n>::Zero());
64  return zero;
65  }
66 
67  static const PowerWeight &One() {
68  static const PowerWeight one(TupleWeight<W, n>::One());
69  return one;
70  }
71 
72  static const PowerWeight &NoWeight() {
73  static const PowerWeight no_weight(TupleWeight<W, n>::NoWeight());
74  return no_weight;
75  }
76 
77  static const std::string &Type() {
78  static const std::string *const type =
79  new std::string(W::Type() + "_^" + std::to_string(n));
80  return *type;
81  }
82 
83  static constexpr uint64_t Properties() {
84  return W::Properties() &
86  }
87 
88  PowerWeight Quantize(float delta = kDelta) const {
90  }
91 
94  }
95 };
96 
97 // Semiring plus operation.
98 template <class W, size_t n>
100  const PowerWeight<W, n> &w2) {
101  PowerWeight<W, n> result;
102  for (size_t i = 0; i < n; ++i) {
103  result.SetValue(i, Plus(w1.Value(i), w2.Value(i)));
104  }
105  return result;
106 }
107 
108 // Semiring times operation.
109 template <class W, size_t n>
111  const PowerWeight<W, n> &w2) {
112  PowerWeight<W, n> result;
113  for (size_t i = 0; i < n; ++i) {
114  result.SetValue(i, Times(w1.Value(i), w2.Value(i)));
115  }
116  return result;
117 }
118 
119 // Semiring divide operation.
120 template <class W, size_t n>
122  const PowerWeight<W, n> &w2,
123  DivideType type = DIVIDE_ANY) {
124  PowerWeight<W, n> result;
125  for (size_t i = 0; i < n; ++i) {
126  result.SetValue(i, Divide(w1.Value(i), w2.Value(i), type));
127  }
128  return result;
129 }
130 
131 // Semimodule left scalar product.
132 template <class W, size_t n>
133 inline PowerWeight<W, n> Times(const W &scalar,
134  const PowerWeight<W, n> &weight) {
135  PowerWeight<W, n> result;
136  for (size_t i = 0; i < n; ++i) {
137  result.SetValue(i, Times(scalar, weight.Value(i)));
138  }
139  return result;
140 }
141 
142 // Semimodule right scalar product.
143 template <class W, size_t n>
145  const W &scalar) {
146  PowerWeight<W, n> result;
147  for (size_t i = 0; i < n; ++i) {
148  result.SetValue(i, Times(weight.Value(i), scalar));
149  }
150  return result;
151 }
152 
153 // Semimodule dot product.
154 template <class W, size_t n>
155 inline W DotProduct(const PowerWeight<W, n> &w1, const PowerWeight<W, n> &w2) {
156  W result(W::Zero());
157  for (size_t i = 0; i < n; ++i) {
158  result = Plus(result, Times(w1.Value(i), w2.Value(i)));
159  }
160  return result;
161 }
162 
163 // This function object generates weights over the Cartesian power of rank
164 // n over the underlying weight. This is intended primarily for testing.
165 template <class W, size_t n>
167  public:
170 
171  explicit WeightGenerate(uint64_t seed = std::random_device()(),
172  bool allow_zero = true)
173  : generate_(seed, allow_zero) {}
174 
175  Weight operator()() const {
176  Weight result;
177  for (size_t i = 0; i < n; ++i) result.SetValue(i, generate_());
178  return result;
179  }
180 
181  private:
182  const Generate generate_;
183 };
184 
185 } // namespace fst
186 
187 #endif // FST_POWER_WEIGHT_H_
void SetValue(size_t i, const W &w)
Definition: tuple-weight.h:114
PowerWeight< typename W::ReverseWeight, n > ReverseWeight
Definition: power-weight.h:46
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:60
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:63
PowerWeight(Iterator begin, Iterator end)
Definition: power-weight.h:54
constexpr uint64_t kIdempotent
Definition: weight.h:144
ReverseWeight Reverse() const
Definition: power-weight.h:92
WeightGenerate(uint64_t seed=std::random_device()(), bool allow_zero=true)
Definition: power-weight.h:171
constexpr uint64_t kRightSemiring
Definition: weight.h:136
W DotProduct(const PowerWeight< W, n > &w1, const PowerWeight< W, n > &w2)
Definition: power-weight.h:155
const W & Value(size_t i) const
Definition: tuple-weight.h:112
static const PowerWeight & NoWeight()
Definition: power-weight.h:72
constexpr uint64_t kCommutative
Definition: weight.h:141
PowerWeight(const TupleWeight< W, n > &weight)
Definition: power-weight.h:50
static constexpr uint64_t Properties()
Definition: power-weight.h:83
static const std::string & Type()
Definition: power-weight.h:77
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:66
PowerWeight(size_t index, const W &weight, const W &default_weight=W::Zero())
Definition: power-weight.h:58
static const PowerWeight & One()
Definition: power-weight.h:67
DivideType
Definition: weight.h:162
constexpr uint64_t kLeftSemiring
Definition: weight.h:133
constexpr float kDelta
Definition: weight.h:130
PowerWeight Quantize(float delta=kDelta) const
Definition: power-weight.h:88
static const PowerWeight & Zero()
Definition: power-weight.h:62