Vcsn  2.4
Be Rational
log.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <iostream>
5 #include <string>
6 
7 #include <vcsn/core/join.hh>
8 #include <vcsn/misc/format.hh>
9 #include <vcsn/misc/functional.hh> // hash_value
10 #include <vcsn/misc/math.hh>
11 #include <vcsn/misc/raise.hh>
12 #include <vcsn/misc/star-status.hh>
13 #include <vcsn/misc/stream.hh> // eat
14 #include <vcsn/misc/symbol.hh>
15 #include <vcsn/weightset/fwd.hh>
17 
18 namespace vcsn
19 {
20  namespace detail
21  {
22  class log_impl
23  {
24  public:
25  using self_t = log;
26  using value_t = double;
27 
28  static symbol sname()
29  {
30  static auto res = symbol{"log"};
31  return res;
32  }
33 
35  static log make(std::istream& is)
36  {
37  eat(is, sname());
38  return {};
39  }
40 
42  static value_t zero()
43  {
44  return std::numeric_limits<value_t>::infinity();
45  }
46 
48  static value_t one()
49  {
50  return 0;
51  }
52 
53  static value_t
54  min()
55  {
57  }
58 
59  static value_t
60  max()
61  {
62  return std::numeric_limits<value_t>::max();
63  }
64 
65  static value_t add(const value_t l, const value_t r)
66  {
67  // https://lingpipe-blog.com/2009/06/25/log-sum-of-exponentials/
68  auto diff = l - r;
69  if (0 < diff)
70  return r - std::log1p(std::exp(-diff));
71  else if (diff <= 0)
72  return l - std::log1p(std::exp(diff));
73  else // NaN
74  return zero();
75  }
76 
77  static value_t sub(const value_t l, const value_t r)
78  {
79  return - std::log(std::exp(-l) - std::exp(-r));
80  }
81 
82  static value_t mul(const value_t l, const value_t r)
83  {
84  // We have to check if a member is +oo
85  return is_zero(l) || is_zero(r) ? zero() : l + r;
86  }
87 
88  value_t
89  rdivide(const value_t l, const value_t r) const
90  {
91  require(!is_zero(r), *this, ": div: division by zero");
92  return is_zero(l) ? l : l - r;
93  }
94 
95  value_t
96  ldivide(const value_t l, const value_t r) const
97  {
98  return rdivide(r, l);
99  }
100 
101  value_t star(const value_t v) const
102  {
103  if (v < 0)
104  raise_not_starrable(*this, v);
105  return std::log1p(-std::exp(-v));
106  }
107 
108  static bool equal(const value_t l, const value_t r)
109  {
110  return l == r;
111  }
112 
114  static bool less(const value_t lhs, const value_t rhs)
115  {
116  return lhs < rhs;
117  }
118 
119  constexpr static bool is_special(const value_t)
120  {
121  return false;
122  }
123 
124  static bool is_zero(const value_t v)
125  {
126  return v == zero();
127  }
128 
129  static bool is_one(const value_t v)
130  {
131  return v == one();
132  }
133 
134  static constexpr bool is_commutative() { return true; }
135  static constexpr bool is_idempotent() { return false; }
136  static constexpr bool has_lightening_weights() { return true; }
137 
138  static constexpr bool show_one() { return true; }
139  static constexpr star_status_t star_status()
140  {
142  }
143 
144  static value_t
146  {
147  return v;
148  }
149 
150  static size_t hash(const value_t v)
151  {
152  return hash_value(v);
153  }
154 
155  static value_t
157  {
158  return v;
159  }
160 
161  value_t
162  conv(std::istream& i, bool = true) const
163  {
164  value_t res;
165  if (i.peek() == 'o')
166  {
167  eat(i, "oo");
168  res = zero();
169  }
170  else if (! (i >> res))
171  raise(*this, ": invalid value: ", i);
172  return res;
173  }
174 
175  static std::ostream&
176  print(const value_t v, std::ostream& o = std::cout,
177  format fmt = {})
178  {
179  if (is_zero(v))
180  return o << (fmt == format::latex ? "\\infty" : "oo");
181  else
182  return o << v;
183  }
184 
185  std::ostream&
186  print_set(std::ostream& o, format fmt = {}) const
187  {
188  switch (fmt.kind())
189  {
190  case format::latex:
191  o << "\\mathrm{Log}";
192  break;
193  case format::sname:
194  o << sname();
195  break;
196  case format::text:
197  case format::utf8:
198  o << "Log";
199  break;
200  case format::raw:
201  assert(0);
202  break;
203  }
204  return o;
205  }
206  };
207 
208  // Random generation.
209  template <typename RandomGenerator>
210  class random_weight<log, RandomGenerator>
211  : public random_weight_base<log, RandomGenerator>
212  {
213  public:
215  using value_t = typename super_t::weight_t;
216 
217  using super_t::super_t;
218 
219  private:
221  {
222  auto dis =
223  std::uniform_real_distribution<log::value_t>(super_t::min_, super_t::max_);
224  return dis(super_t::gen_);
225  }
226  };
227 
228 
229  /*-------.
230  | join. |
231  `-------*/
232 
234  }// detail::
235 }
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: log.hh:176
value_t rdivide(const value_t l, const value_t r) const
Definition: log.hh:89
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
Print as a parsable type string.
Definition: format.hh:26
Generic declaration of the class which is specialized in each weightset.
Definition: weightset.hh:205
return res
Definition: multiply.hh:398
value_t ldivide(const value_t l, const value_t r) const
Definition: log.hh:96
static bool is_one(const value_t v)
Definition: log.hh:129
static bool is_zero(const value_t v)
Definition: log.hh:124
static value_t one()
The neutral element for the log semiring is 0.
Definition: log.hh:48
VCSN_JOIN_SIMPLE(b, b)
Print for LaTeX.
Definition: format.hh:22
static constexpr bool is_special(const value_t)
Definition: log.hh:119
static size_t hash(const value_t v)
Definition: log.hh:150
always valid.
Definition: star-status.hh:17
An input/output format for valuesets.
Definition: format.hh:13
Abstract class for random weight generation.
Definition: weightset.hh:112
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static constexpr bool has_lightening_weights()
Definition: log.hh:136
static value_t mul(const value_t l, const value_t r)
Definition: log.hh:82
static symbol sname()
Definition: log.hh:28
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: log.hh:114
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static value_t transpose(const value_t v)
Definition: log.hh:145
static constexpr bool is_idempotent()
Definition: log.hh:135
static constexpr bool is_commutative()
Definition: log.hh:134
return exp min
Definition: multiply.hh:361
static value_t conv(self_t, const value_t v)
Definition: log.hh:156
Definition: a-star.hh:8
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static value_t sub(const value_t l, const value_t r)
Definition: log.hh:77
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t zero()
The zero for the log semiring is +oo.
Definition: log.hh:42
weightset_mixin< detail::log_impl > log
Definition: fwd.hh:50
value_t conv(std::istream &i, bool=true) const
Definition: log.hh:162
static value_t max()
Definition: log.hh:60
star_status_t
Definition: star-status.hh:5
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: log.hh:186
static log make(std::istream &is)
Build from the description in is.
Definition: log.hh:35
static bool equal(const value_t l, const value_t r)
Definition: log.hh:108
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
static value_t min()
Definition: log.hh:54
static value_t add(const value_t l, const value_t r)
Definition: log.hh:65
value_t star(const value_t v) const
Definition: log.hh:101
static constexpr bool show_one()
Definition: log.hh:138
static constexpr star_status_t star_status()
Definition: log.hh:139