Vcsn  2.4
Be Rational
r.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ostream>
4 #include <string>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/misc/format.hh>
8 #include <vcsn/misc/functional.hh> // hash_value
9 #include <vcsn/misc/raise.hh>
10 #include <vcsn/misc/star-status.hh>
11 #include <vcsn/misc/stream.hh>
12 #include <vcsn/weightset/b.hh>
13 #include <vcsn/weightset/fwd.hh>
14 #include <vcsn/weightset/q.hh>
15 #include <vcsn/weightset/qmp.hh>
17 #include <vcsn/weightset/z.hh>
18 
19 namespace vcsn
20 {
21  namespace detail
22  {
23  class r_impl
24  {
25  public:
26  using self_t = r;
27 
28  static symbol sname()
29  {
30  static auto res = symbol{"r"};
31  return res;
32  }
33 
35  static r make(std::istream& is)
36  {
37  eat(is, sname());
38  return {};
39  }
40 
41  using value_t = double;
42 
43  static value_t
44  zero()
45  {
46  return 0.;
47  }
48 
49  static value_t
50  one()
51  {
52  return 1.;
53  }
54 
55  static value_t
56  min()
57  {
59  }
60 
61  static value_t
62  max()
63  {
64  return std::numeric_limits<value_t>::max();
65  }
66 
67  static value_t
68  add(const value_t l, const value_t r)
69  {
70  return l + r;
71  }
72 
73  static value_t
74  sub(const value_t l, const value_t r)
75  {
76  return l - r;
77  }
78 
79  static value_t
80  mul(const value_t l, const value_t r)
81  {
82  return l * r;
83  }
84 
85  value_t
86  lgcd(const value_t l, const value_t r) const
87  {
88  require(!is_zero(l), *this, ": lgcd: invalid lhs: zero");
89  require(!is_zero(r), *this, ": lgcd: invalid rhs: zero");
90  return l;
91  }
92 
93  value_t
94  rgcd(const value_t l, const value_t r) const
95  {
96  return lgcd(l, r);
97  }
98 
99  value_t
100  rdivide(const value_t l, const value_t r) const
101  {
102  require(!is_zero(r), *this, ": div: division by zero");
103  return l / r;
104  }
105 
106  value_t
107  ldivide(const value_t l, const value_t r) const
108  {
109  return rdivide(r, l);
110  }
111 
112  value_t
113  star(const value_t v) const
114  {
115  if (-1 < v && v < 1)
116  return 1/(1-v);
117  else
118  raise_not_starrable(*this, v);
119  }
120 
121  constexpr static bool is_special(const value_t)
122  {
123  return false;
124  }
125 
126  static bool
128  {
129  return v == 0;
130  }
131 
132  static bool
133  is_one(const value_t v)
134  {
135  return v == 1;
136  }
137 
138  static bool
139  equal(const value_t l, const value_t r)
140  {
141  return l == r;
142  }
143 
145  static bool less(const value_t lhs, const value_t rhs)
146  {
147  return lhs < rhs;
148  }
149 
150  static constexpr bool is_commutative() { return true; }
151  static constexpr bool is_idempotent() { return false; }
152  static constexpr bool has_lightening_weights() { return true; }
153 
154  static constexpr bool show_one() { return false; }
155  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
156 
157  static value_t
158  abs(const value_t v)
159  {
160  return v < 0 ? -v : v;
161  }
162 
163  static value_t
165  {
166  return v;
167  }
168 
169  static size_t hash(const value_t v)
170  {
171  return hash_value(v);
172  }
173 
174  static value_t
176  {
177  return v;
178  }
179 
180  static value_t
181  conv(q, const q::value_t v)
182  {
183  return value_t(v.num) / value_t(v.den);
184  }
185 
186  static value_t
187  conv(z, const z::value_t v)
188  {
189  return v;
190  }
191 
192  static value_t
193  conv(b, const b::value_t v)
194  {
195  return v;
196  }
197 
198  value_t
199  conv(std::istream& i, bool = true) const
200  {
201  value_t res;
202  if (i >> res)
203  return res;
204  else
205  raise(*this, ": invalid value: ", i);
206  }
207 
208  static std::ostream&
209  print(const value_t v, std::ostream& o = std::cout,
210  format = {})
211  {
212  return o << v;
213  }
214 
215  std::ostream&
216  print_set(std::ostream& o, format fmt = {}) const
217  {
218  switch (fmt.kind())
219  {
220  case format::latex:
221  o << "\\mathbb{R}";
222  break;
223  case format::sname:
224  o << sname();
225  break;
226  case format::text:
227  o << "R";
228  break;
229  case format::utf8:
230  o << "ℝ";
231  break;
232  case format::raw:
233  assert(0);
234  break;
235  }
236  return o;
237  }
238  };
239 
240  // Random generation.
241  template <typename RandomGenerator>
242  class random_weight<r, RandomGenerator>
243  : public random_weight_base<r, RandomGenerator>
244  {
245  public:
247  using value_t = typename super_t::weight_t;
248 
249  using super_t::super_t;
250 
251  private:
253  {
254  auto dis = std::uniform_real_distribution<>(super_t::min_, super_t::max_);
255  return dis(super_t::gen_);
256  }
257  };
258 
259  VCSN_JOIN_SIMPLE(b, r);
260  VCSN_JOIN_SIMPLE(z, r);
261  VCSN_JOIN_SIMPLE(q, r);
263  VCSN_JOIN_SIMPLE(r, r);
264  }
265 
266 }
value_t star(const value_t v) const
Definition: r.hh:113
value_t rgcd(const value_t l, const value_t r) const
Definition: r.hh:94
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 lgcd(const value_t l, const value_t r) const
Definition: r.hh:86
VCSN_JOIN_SIMPLE(b, b)
Print for LaTeX.
Definition: format.hh:22
static constexpr star_status_t star_status()
Definition: r.hh:155
static value_t min()
Definition: r.hh:56
static bool is_zero(const value_t v)
Definition: r.hh:127
value_t ldivide(const value_t l, const value_t r) const
Definition: r.hh:107
An input/output format for valuesets.
Definition: format.hh:13
Abstract class for random weight generation.
Definition: weightset.hh:112
static bool equal(const value_t l, const value_t r)
Definition: r.hh:139
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
typename super_t::weight_t value_t
Definition: r.hh:247
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 size_t hash(const value_t v)
Definition: r.hh:169
static value_t conv(z, const z::value_t v)
Definition: r.hh:187
static constexpr bool has_lightening_weights()
Definition: r.hh:152
return exp min
Definition: multiply.hh:361
static value_t transpose(const value_t v)
Definition: r.hh:164
Definition: a-star.hh:8
static bool is_one(const value_t v)
Definition: r.hh:133
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static value_t abs(const value_t v)
Definition: r.hh:158
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: r.hh:216
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: r.hh:145
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static value_t add(const value_t l, const value_t r)
Definition: r.hh:68
static value_t zero()
Definition: r.hh:44
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:80
static constexpr bool is_commutative()
Definition: r.hh:150
static value_t conv(q, const q::value_t v)
Definition: r.hh:181
valid iff proper succeeds on the "absolute value" of the automaton
Definition: star-status.hh:9
static constexpr bool is_special(const value_t)
Definition: r.hh:121
static value_t max()
Definition: r.hh:62
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:74
star_status_t
Definition: star-status.hh:5
value_t rdivide(const value_t l, const value_t r) const
Definition: r.hh:100
static value_t one()
Definition: r.hh:50
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
Definition: raise.hh:100
double value_t
Definition: r.hh:41
static constexpr bool show_one()
Definition: r.hh:154
static r make(std::istream &is)
Build from the description in is.
Definition: r.hh:35
static symbol sname()
Definition: r.hh:28
static value_t conv(b, const b::value_t v)
Definition: r.hh:193
static std::ostream & print(const value_t v, std::ostream &o=std::cout, format={})
Definition: r.hh:209
value_t conv(std::istream &i, bool=true) const
Definition: r.hh:199
static value_t conv(self_t, const value_t v)
Definition: r.hh:175
static constexpr bool is_idempotent()
Definition: r.hh:151