Vcsn  2.3
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 add(const value_t l, const value_t r)
54  {
55  return - std::log(std::exp(-l) + std::exp(-r));
56  }
57 
58  static value_t sub(const value_t l, const value_t r)
59  {
60  return - std::log(std::exp(-l) - std::exp(-r));
61  }
62 
63  static value_t mul(const value_t l, const value_t r)
64  {
65  // We have to check if a member is +oo
66  return is_zero(l) || is_zero(r) ? zero() : l + r;
67  }
68 
69  value_t
70  rdivide(const value_t l, const value_t r) const
71  {
72  require(!is_zero(r), *this, ": div: division by zero");
73  return is_zero(l) ? l : l - r;
74  }
75 
76  value_t
77  ldivide(const value_t l, const value_t r) const
78  {
79  return rdivide(r, l);
80  }
81 
82  value_t star(const value_t v) const
83  {
84  if (v < 1)
85  return std::log(1 - v);
86  else
87  return 0;
88  }
89 
90  static bool equal(const value_t l, const value_t r)
91  {
92  return l == r;
93  }
94 
96  static bool less(const value_t lhs, const value_t rhs)
97  {
98  return lhs < rhs;
99  }
100 
101  constexpr static bool is_special(const value_t)
102  {
103  return false;
104  }
105 
106  static bool is_zero(const value_t v)
107  {
108  return v == zero();
109  }
110 
111  static bool is_one(const value_t v)
112  {
113  return v == one();
114  }
115 
116  static constexpr bool is_commutative() { return true; }
117  static constexpr bool is_idempotent() { return false; }
118  static constexpr bool has_lightening_weights() { return true; }
119 
120  static constexpr bool show_one() { return true; }
121  static constexpr star_status_t star_status()
122  {
124  }
125 
126  static value_t
128  {
129  return v;
130  }
131 
132  static size_t hash(const value_t v)
133  {
134  return hash_value(v);
135  }
136 
137  static value_t
139  {
140  return v;
141  }
142 
143  value_t
144  conv(std::istream& i, bool = true) const
145  {
146  value_t res;
147  if (i.peek() == 'o')
148  {
149  eat(i, "oo");
150  res = zero();
151  }
152  else if (! (i >> res))
153  raise(*this, ": invalid value: ", i);
154  return res;
155  }
156 
157  static std::ostream&
158  print(const value_t v, std::ostream& o = std::cout,
159  format fmt = {})
160  {
161  if (is_zero(v))
162  return o << (fmt == format::latex ? "\\infty" : "oo");
163  else
164  return o << v;
165  }
166 
167  std::ostream&
168  print_set(std::ostream& o, format fmt = {}) const
169  {
170  switch (fmt.kind())
171  {
172  case format::latex:
173  o << "\\mathrm{Log}";
174  break;
175  case format::sname:
176  o << sname();
177  break;
178  case format::text:
179  case format::utf8:
180  o << "Log";
181  break;
182  case format::raw:
183  assert(0);
184  break;
185  }
186  return o;
187  }
188  };
189  /*-------.
190  | join. |
191  `-------*/
192 
194  }// detail::
195 }
always valid.
Definition: star-status.hh:17
static constexpr star_status_t star_status()
Definition: log.hh:121
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t transpose(const value_t v)
Definition: log.hh:127
star_status_t
Definition: star-status.hh:5
static constexpr bool has_lightening_weights()
Definition: log.hh:118
weightset_mixin< detail::log_impl > log
Definition: fwd.hh:50
Print as a parsable type string.
Definition: format.hh:26
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
static bool is_zero(const value_t v)
Definition: log.hh:106
static bool is_one(const value_t v)
Definition: log.hh:111
static value_t one()
The neutral element for the log semiring is 0.
Definition: log.hh:48
static value_t conv(self_t, const value_t v)
Definition: log.hh:138
return res
Definition: multiply.hh:398
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
value_t rdivide(const value_t l, const value_t r) const
Definition: log.hh:70
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: log.hh:168
value_t ldivide(const value_t l, const value_t r) const
Definition: log.hh:77
VCSN_JOIN_SIMPLE(b, b)
static bool equal(const value_t l, const value_t r)
Definition: log.hh:90
static value_t zero()
The zero for the log semiring is +oo.
Definition: log.hh:42
Definition: a-star.hh:8
static constexpr bool is_idempotent()
Definition: log.hh:117
value_t star(const value_t v) const
Definition: log.hh:82
static value_t add(const value_t l, const value_t r)
Definition: log.hh:53
An input/output format for valuesets.
Definition: format.hh:13
static constexpr bool is_commutative()
Definition: log.hh:116
static constexpr bool is_special(const value_t)
Definition: log.hh:101
static size_t hash(const value_t v)
Definition: log.hh:132
static constexpr bool show_one()
Definition: log.hh:120
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
static log make(std::istream &is)
Build from the description in is.
Definition: log.hh:35
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: log.hh:96
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format fmt={})
Definition: log.hh:158
Print for LaTeX.
Definition: format.hh:22
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
value_t conv(std::istream &i, bool=true) const
Definition: log.hh:144
static value_t sub(const value_t l, const value_t r)
Definition: log.hh:58
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t mul(const value_t l, const value_t r)
Definition: log.hh:63
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:90
static symbol sname()
Definition: log.hh:28
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54