arctic-weight.h
Go to the documentation of this file.
1 // lat/arctic-weight.h
2 
3 // Copyright 2012 Johns Hopkins University (Author: Guoguo Chen)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #ifndef KALDI_LAT_ARCTIC_WEIGHT_H_
22 #define KALDI_LAT_ARCTIC_WEIGHT_H_
23 
24 #include "fst/float-weight.h"
25 
26 namespace fst {
27 
28 // Arctic semiring: (max, +, inf, 0)
29 // We define the Arctic semiring T' = (R \cup {-inf, +inf}, max, +, -inf, 0).
30 // The term "Arctic" came from Keith Kintzley (kintzley@jhu.edu), as opposite
31 // to the Tropical semiring.
32 template <class T>
33 class ArcticWeightTpl : public FloatWeightTpl<T> {
34  public:
36 
38 
40 
42 
44 
45  static const ArcticWeightTpl<T> Zero() {
46  return ArcticWeightTpl<T>(-std::numeric_limits<T>::infinity()); }
47 
48  static const ArcticWeightTpl<T> One() {
49  return ArcticWeightTpl<T>(0.0F); }
50 
51  static const std::string &Type() {
52  static const std::string type = std::string("arctic") +
54  return type;
55  }
56 
58  return ArcticWeightTpl<T>(std::numeric_limits<T>::infinity());
59  }
60 
61  bool Member() const {
62  // First part fails for IEEE NaN
63  return Value() == Value() && Value() != std::numeric_limits<T>::infinity();
64  }
65 
66  ArcticWeightTpl<T> Quantize(float delta = kDelta) const {
67  if (Value() == -std::numeric_limits<T>::infinity() ||
68  Value() == std::numeric_limits<T>::infinity() ||
69  Value() != Value())
70  return *this;
71  else
72  return ArcticWeightTpl<T>(floor(Value()/delta + 0.5F) * delta);
73  }
74 
75  ArcticWeightTpl<T> Reverse() const { return *this; }
76 
77  static uint64 Properties() {
78  return kLeftSemiring | kRightSemiring | kCommutative |
79  kPath | kIdempotent;
80  }
81 };
82 
83 // Single precision arctic weight
85 
86 template <class T>
88  const ArcticWeightTpl<T> &w2) {
89  return w1.Value() > w2.Value() ? w1 : w2;
90 }
91 
93  const ArcticWeightTpl<float> &w2) {
94  return Plus<float>(w1, w2);
95 }
96 
98  const ArcticWeightTpl<double> &w2) {
99  return Plus<double>(w1, w2);
100 }
101 
102 template <class T>
104  const ArcticWeightTpl<T> &w2) {
105  T f1 = w1.Value(), f2 = w2.Value();
106  if (f1 == -std::numeric_limits<T>::infinity())
107  return w1;
108  else if (f2 == -std::numeric_limits<T>::infinity())
109  return w2;
110  else
111  return ArcticWeightTpl<T>(f1 + f2);
112 }
113 
115  const ArcticWeightTpl<float> &w2) {
116  return Times<float>(w1, w2);
117 }
118 
120  const ArcticWeightTpl<double> &w2) {
121  return Times<double>(w1, w2);
122 }
123 
124 template <class T>
126  const ArcticWeightTpl<T> &w2,
127  DivideType typ = DIVIDE_ANY) {
128  T f1 = w1.Value(), f2 = w2.Value();
129  if (f2 == -std::numeric_limits<T>::infinity())
130  return std::numeric_limits<T>::quiet_NaN();
131  else if (f1 == -std::numeric_limits<T>::infinity())
132  return -std::numeric_limits<T>::infinity();
133  else
134  return ArcticWeightTpl<T>(f1 - f2);
135 }
136 
138  const ArcticWeightTpl<float> &w2,
139  DivideType typ = DIVIDE_ANY) {
140  return Divide<float>(w1, w2, typ);
141 }
142 
144  const ArcticWeightTpl<double> &w2,
145  DivideType typ = DIVIDE_ANY) {
146  return Divide<double>(w1, w2, typ);
147 }
148 
149 
150 
151 } // namespace fst
152 
153 #endif // KALDI_LAT_ARCTIC_WEIGHT_H_
LatticeWeightTpl< FloatType > Divide(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2, DivideType typ=DIVIDE_ANY)
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
ArcticWeightTpl< T > ReverseWeight
Definition: arctic-weight.h:37
static uint64 Properties()
Definition: arctic-weight.h:77
static ArcticWeightTpl< T > NoWeight()
Definition: arctic-weight.h:57
ArcticWeightTpl< float > ArcticWeight
Definition: arctic-weight.h:84
LatticeWeightTpl< FloatType > Times(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
static const ArcticWeightTpl< T > One()
Definition: arctic-weight.h:48
static const std::string & Type()
Definition: arctic-weight.h:51
bool Member() const
Definition: arctic-weight.h:61
ArcticWeightTpl< T > Quantize(float delta=kDelta) const
Definition: arctic-weight.h:66
ArcticWeightTpl(const ArcticWeightTpl< T > &w)
Definition: arctic-weight.h:43
static const ArcticWeightTpl< T > Zero()
Definition: arctic-weight.h:45
ArcticWeightTpl< T > Reverse() const
Definition: arctic-weight.h:75