Vcsn  2.2
Be Rational
log.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <iostream>
5 #include <cmath>
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  static value_t
70  rdiv(const value_t l, const value_t r)
71  {
72  require(!is_zero(r), "div: division by zero");
73  return is_zero(l) ? l : l - r;
74  }
75 
76  static value_t
77  ldiv(const value_t l, const value_t r)
78  {
79  return rdiv(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  static value_t
144  conv(std::istream& i, bool = true)
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(sname(), ": invalid value: ", i);
154  return res;
155  }
156 
157  static std::ostream&
158  print(const value_t v, std::ostream& o,
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 }
static log make(std::istream &is)
Build from the description in is.
Definition: log.hh:35
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 constexpr star_status_t star_status()
Definition: log.hh:121
static value_t mul(const value_t l, const value_t r)
Definition: log.hh:63
static symbol sname()
Definition: log.hh:28
static value_t transpose(const value_t v)
Definition: log.hh:127
Print as rich UTF-8 text, escaped.
Definition: format.hh:28
static constexpr bool is_special(const value_t)
Definition: log.hh:101
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:66
VCSN_JOIN_SIMPLE(b, b)
static bool is_zero(const value_t v)
Definition: log.hh:106
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
static constexpr bool has_lightening_weights()
Definition: log.hh:118
Print for LaTeX.
Definition: format.hh:20
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: log.hh:96
Print as is. For instance, don't try to escape labels.
Definition: format.hh:22
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
An input/output format for valuesets.
Definition: format.hh:11
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static value_t conv(self_t, const value_t v)
Definition: log.hh:138
static value_t rdiv(const value_t l, const value_t r)
Definition: log.hh:70
weightset_mixin< detail::log_impl > log
Definition: fwd.hh:50
static value_t sub(const value_t l, const value_t r)
Definition: log.hh:58
always valid.
Definition: star-status.hh:17
static constexpr bool is_commutative()
Definition: log.hh:116
static value_t zero()
The zero for the log semiring is +oo.
Definition: log.hh:42
static size_t hash(const value_t v)
Definition: log.hh:132
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
Print as plain (ASCII) text, escaped.
Definition: format.hh:26
static value_t ldiv(const value_t l, const value_t r)
Definition: log.hh:77
star_status_t
Definition: star-status.hh:5
static constexpr bool is_idempotent()
Definition: log.hh:117
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
static bool equal(const value_t l, const value_t r)
Definition: log.hh:90
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: log.hh:158
Print as a parsable type string.
Definition: format.hh:24
static value_t conv(std::istream &i, bool=true)
Definition: log.hh:144
static value_t add(const value_t l, const value_t r)
Definition: log.hh:53
static constexpr bool show_one()
Definition: log.hh:120
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: log.hh:168
value_t star(const value_t v) const
Definition: log.hh:82