FST  openfst-1.8.3
OpenFst Library
power-weight.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 <cstddef>
24 #include <cstdint>
25 #include <random>
26 #include <string>
27 
28 #include <fst/tuple-weight.h>
29 #include <fst/weight.h>
30 
31 namespace fst {
32 
33 // Cartesian power semiring: W ^ n
34 //
35 // Forms:
36 // - a left semimodule when W is a left semiring,
37 // - a right semimodule when W is a right semiring,
38 // - a bisemimodule when W is a semiring,
39 // the free semimodule of rank n over W
40 // The Times operation is overloaded to provide the left and right scalar
41 // products.
42 template <class W, size_t n>
43 class PowerWeight : public TupleWeight<W, n> {
44  public:
46 
47  PowerWeight() = default;
48 
49  explicit PowerWeight(const TupleWeight<W, n> &weight)
50  : TupleWeight<W, n>(weight) {}
51 
52  template <class Iterator>
53  PowerWeight(Iterator begin, Iterator end) : TupleWeight<W, n>(begin, end) {}
54 
55  // Initialize component `index` to `weight`; initialize all other components
56  // to `default_weight`
57  PowerWeight(size_t index, const W &weight,
58  const W &default_weight = W::Zero())
59  : TupleWeight<W, n>(index, weight, default_weight) {}
60 
61  static const PowerWeight &Zero() {
62  static const PowerWeight zero(TupleWeight<W, n>::Zero());
63  return zero;
64  }
65 
66  static const PowerWeight &One() {
67  static const PowerWeight one(TupleWeight<W, n>::One());
68  return one;
69  }
70 
71  static const PowerWeight &NoWeight() {
72  static const PowerWeight no_weight(TupleWeight<W, n>::NoWeight());
73  return no_weight;
74  }
75 
76  static const std::string &Type() {
77  static const std::string *const type =
78  new std::string(W::Type() + "_^" + std::to_string(n));
79  return *type;
80  }
81 
82  static constexpr uint64_t Properties() {
83  return W::Properties() &
85  }
86 
87  PowerWeight Quantize(float delta = kDelta) const {
89  }
90 
93  }
94 };
95 
96 // Semiring plus operation.
97 template <class W, size_t n>
99  const PowerWeight<W, n> &w2) {
100  PowerWeight<W, n> result;
101  for (size_t i = 0; i < n; ++i) {
102  result.SetValue(i, Plus(w1.Value(i), w2.Value(i)));
103  }
104  return result;
105 }
106 
107 // Semiring times operation.
108 template <class W, size_t n>
110  const PowerWeight<W, n> &w2) {
111  PowerWeight<W, n> result;
112  for (size_t i = 0; i < n; ++i) {
113  result.SetValue(i, Times(w1.Value(i), w2.Value(i)));
114  }
115  return result;
116 }
117 
118 // Semiring divide operation.
119 template <class W, size_t n>
121  const PowerWeight<W, n> &w2,
122  DivideType type = DIVIDE_ANY) {
123  PowerWeight<W, n> result;
124  for (size_t i = 0; i < n; ++i) {
125  result.SetValue(i, Divide(w1.Value(i), w2.Value(i), type));
126  }
127  return result;
128 }
129 
130 // Semimodule left scalar product.
131 template <class W, size_t n>
132 inline PowerWeight<W, n> Times(const W &scalar,
133  const PowerWeight<W, n> &weight) {
134  PowerWeight<W, n> result;
135  for (size_t i = 0; i < n; ++i) {
136  result.SetValue(i, Times(scalar, weight.Value(i)));
137  }
138  return result;
139 }
140 
141 // Semimodule right scalar product.
142 template <class W, size_t n>
144  const W &scalar) {
145  PowerWeight<W, n> result;
146  for (size_t i = 0; i < n; ++i) {
147  result.SetValue(i, Times(weight.Value(i), scalar));
148  }
149  return result;
150 }
151 
152 // Semimodule dot product.
153 template <class W, size_t n>
154 inline W DotProduct(const PowerWeight<W, n> &w1, const PowerWeight<W, n> &w2) {
155  W result(W::Zero());
156  for (size_t i = 0; i < n; ++i) {
157  result = Plus(result, Times(w1.Value(i), w2.Value(i)));
158  }
159  return result;
160 }
161 
162 // This function object generates weights over the Cartesian power of rank
163 // n over the underlying weight. This is intended primarily for testing.
164 template <class W, size_t n>
166  public:
169 
170  explicit WeightGenerate(uint64_t seed = std::random_device()(),
171  bool allow_zero = true)
172  : generate_(seed, allow_zero) {}
173 
174  Weight operator()() const {
175  Weight result;
176  for (size_t i = 0; i < n; ++i) result.SetValue(i, generate_());
177  return result;
178  }
179 
180  private:
181  const Generate generate_;
182 };
183 
184 } // namespace fst
185 
186 #endif // FST_POWER_WEIGHT_H_
void SetValue(size_t i, const W &w)
Definition: tuple-weight.h:115
PowerWeight< typename W::ReverseWeight, n > ReverseWeight
Definition: power-weight.h:45
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:61
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:64
PowerWeight(Iterator begin, Iterator end)
Definition: power-weight.h:53
constexpr uint64_t kIdempotent
Definition: weight.h:147
ReverseWeight Reverse() const
Definition: power-weight.h:91
WeightGenerate(uint64_t seed=std::random_device()(), bool allow_zero=true)
Definition: power-weight.h:170
constexpr uint64_t kRightSemiring
Definition: weight.h:139
W DotProduct(const PowerWeight< W, n > &w1, const PowerWeight< W, n > &w2)
Definition: power-weight.h:154
const W & Value(size_t i) const
Definition: tuple-weight.h:113
static const PowerWeight & NoWeight()
Definition: power-weight.h:71
constexpr uint64_t kCommutative
Definition: weight.h:144
PowerWeight()=default
PowerWeight(const TupleWeight< W, n > &weight)
Definition: power-weight.h:49
static constexpr uint64_t Properties()
Definition: power-weight.h:82
static const std::string & Type()
Definition: power-weight.h:76
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:67
PowerWeight(size_t index, const W &weight, const W &default_weight=W::Zero())
Definition: power-weight.h:57
static const PowerWeight & One()
Definition: power-weight.h:66
DivideType
Definition: weight.h:165
constexpr uint64_t kLeftSemiring
Definition: weight.h:136
constexpr float kDelta
Definition: weight.h:133
PowerWeight Quantize(float delta=kDelta) const
Definition: power-weight.h:87
static const PowerWeight & Zero()
Definition: power-weight.h:61