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