Vcsn  2.1
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 symbol res("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>::max();
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  // We use this for -oo
88  return std::numeric_limits<value_t>::min();
89  }
90 
91  static bool equal(const value_t l, const value_t r)
92  {
93  return l == r;
94  }
95 
97  static bool less(const value_t lhs, const value_t rhs)
98  {
99  return lhs < rhs;
100  }
101 
102  constexpr static bool is_special(const value_t)
103  {
104  return false;
105  }
106 
107  static bool is_zero(const value_t v)
108  {
109  return v == zero();
110  }
111 
112  static bool is_one(const value_t v)
113  {
114  return v == one();
115  }
116 
117  static constexpr bool is_commutative() { return true; }
118  static constexpr bool is_idempotent() { return false; }
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  if (fmt == format::latex)
171  o << "\\mathrm{Log}";
172  else if (fmt == format::text)
173  o << sname();
174  else
175  raise("invalid format: ", fmt);
176  return o;
177  }
178  };
179  /*-------.
180  | join. |
181  `-------*/
182 
184  }// detail::
185 }
static value_t one()
The neutral element for the log semiring is 0.
Definition: log.hh:48
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: log.hh:158
static bool is_zero(const value_t v)
Definition: log.hh:107
static value_t conv(self_t, const value_t v)
Definition: log.hh:138
static value_t mul(const value_t l, const value_t r)
Definition: log.hh:63
static bool equal(const value_t l, const value_t r)
Definition: log.hh:91
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static constexpr star_status_t star_status()
Definition: log.hh:121
static constexpr bool is_special(const value_t)
Definition: log.hh:102
static value_t sub(const value_t l, const value_t r)
Definition: log.hh:58
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: log.hh:168
static bool is_one(const value_t v)
Definition: log.hh:112
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static value_t ldiv(const value_t l, const value_t r)
Definition: log.hh:77
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static value_t zero()
The zero for the log semiring is +oo.
Definition: log.hh:42
static constexpr bool show_one()
Definition: log.hh:120
An input/output format.
Definition: format.hh:11
static size_t hash(const value_t v)
Definition: log.hh:132
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
always valid.
Definition: star-status.hh:17
static value_t add(const value_t l, const value_t r)
Definition: log.hh:53
static log make(std::istream &is)
Build from the description in is.
Definition: log.hh:35
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
static symbol sname()
Definition: log.hh:28
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t conv(std::istream &i, bool=true)
Definition: log.hh:144
star_status_t
Definition: star-status.hh:5
VCSN_JOIN_SIMPLE(b, b)
static value_t rdiv(const value_t l, const value_t r)
Definition: log.hh:70
static constexpr bool is_idempotent()
Definition: log.hh:118
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: log.hh:97
value_t star(const value_t v) const
Definition: log.hh:82
static constexpr bool is_commutative()
Definition: log.hh:117
weightset_mixin< detail::log_impl > log
Definition: fwd.hh:50
static value_t transpose(const value_t v)
Definition: log.hh:127