Vcsn  2.2a
Be Rational
r.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <ostream>
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/fwd.hh>
13 #include <vcsn/weightset/b.hh>
14 #include <vcsn/weightset/q.hh>
15 #include <vcsn/weightset/qmp.hh>
16 #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  add(const value_t l, const value_t r)
57  {
58  return l + r;
59  }
60 
61  static value_t
62  sub(const value_t l, const value_t r)
63  {
64  return l - r;
65  }
66 
67  static value_t
68  mul(const value_t l, const value_t r)
69  {
70  return l * r;
71  }
72 
73  static value_t
74  lgcd(const value_t l, const value_t r)
75  {
76  require(!is_zero(l), sname(), ": lgcd: invalid lhs: zero");
77  require(!is_zero(r), sname(), ": lgcd: invalid rhs: zero");
78  return l;
79  }
80 
81  static value_t
82  rgcd(const value_t l, const value_t r)
83  {
84  return lgcd(l, r);
85  }
86 
87  static value_t
88  rdiv(const value_t l, const value_t r)
89  {
90  require(!is_zero(r), "div: division by zero");
91  return l / r;
92  }
93 
94  static value_t
95  ldiv(const value_t l, const value_t r)
96  {
97  return rdiv(r, l);
98  }
99 
100  value_t
101  star(const value_t v) const
102  {
103  if (-1 < v && v < 1)
104  return 1/(1-v);
105  else
106  raise(sname(), ": star: invalid value: ", v);
107  }
108 
109  constexpr static bool is_special(const value_t)
110  {
111  return false;
112  }
113 
114  static bool
116  {
117  return v == 0;
118  }
119 
120  static bool
121  is_one(const value_t v)
122  {
123  return v == 1;
124  }
125 
126  static bool
127  equal(const value_t l, const value_t r)
128  {
129  return l == r;
130  }
131 
133  static bool less(const value_t lhs, const value_t rhs)
134  {
135  return lhs < rhs;
136  }
137 
138  static constexpr bool is_commutative() { return true; }
139  static constexpr bool is_idempotent() { return false; }
140  static constexpr bool has_lightening_weights() { return true; }
141 
142  static constexpr bool show_one() { return false; }
143  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
144 
145  static value_t
146  abs(const value_t v)
147  {
148  return v < 0 ? -v : v;
149  }
150 
151  static value_t
153  {
154  return v;
155  }
156 
157  static size_t hash(const value_t v)
158  {
159  return hash_value(v);
160  }
161 
162  static value_t
164  {
165  return v;
166  }
167 
168  static value_t
169  conv(q, const q::value_t v)
170  {
171  return value_t(v.num) / value_t(v.den);
172  }
173 
174  static value_t
175  conv(z, const z::value_t v)
176  {
177  return v;
178  }
179 
180  static value_t
181  conv(b, const b::value_t v)
182  {
183  return v;
184  }
185 
186  static value_t
187  conv(std::istream& i, bool = true)
188  {
189  value_t res;
190  if (i >> res)
191  return res;
192  else
193  raise(sname(), ": invalid value: ", i);
194  }
195 
196  static std::ostream&
197  print(const value_t v, std::ostream& o,
198  format = {})
199  {
200  return o << v;
201  }
202 
203  std::ostream&
204  print_set(std::ostream& o, format fmt = {}) const
205  {
206  switch (fmt.kind())
207  {
208  case format::latex:
209  o << "\\mathbb{R}";
210  break;
211  case format::sname:
212  o << sname();
213  break;
214  case format::text:
215  o << "R";
216  break;
217  case format::utf8:
218  o << "ℝ";
219  break;
220  case format::raw:
221  assert(0);
222  break;
223  }
224  return o;
225  }
226  };
227 
228  VCSN_JOIN_SIMPLE(b, r);
229  VCSN_JOIN_SIMPLE(z, r);
230  VCSN_JOIN_SIMPLE(q, r);
232  VCSN_JOIN_SIMPLE(r, r);
233  }
234 
235 }
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
valid iff proper succeeds on the "absolute value" of the automaton
Definition: star-status.hh:9
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:66
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
VCSN_JOIN_SIMPLE(b, b)
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t conv(z, const z::value_t v)
Definition: r.hh:175
static value_t conv(b, const b::value_t v)
Definition: r.hh:181
static symbol sname()
Definition: r.hh:28
static constexpr bool is_commutative()
Definition: r.hh:138
Print as plain (ASCII) text, escaped.
Definition: format.hh:26
static value_t ldiv(const value_t l, const value_t r)
Definition: r.hh:95
star_status_t
Definition: star-status.hh:5
static value_t rdiv(const value_t l, const value_t r)
Definition: r.hh:88
static std::ostream & print(const value_t v, std::ostream &o, format={})
Definition: r.hh:197
static bool is_zero(const value_t v)
Definition: r.hh:115
static bool is_one(const value_t v)
Definition: r.hh:121
static value_t rgcd(const value_t l, const value_t r)
Definition: r.hh:82
static bool equal(const value_t l, const value_t r)
Definition: r.hh:127
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
Print as a parsable type string.
Definition: format.hh:24
static value_t one()
Definition: r.hh:50
static value_t zero()
Definition: r.hh:44
static constexpr bool has_lightening_weights()
Definition: r.hh:140
static value_t transpose(const value_t v)
Definition: r.hh:152
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:68
Print for LaTeX.
Definition: format.hh:20
static constexpr bool is_special(const value_t)
Definition: r.hh:109
weightset_mixin< detail::q_impl > q
Definition: fwd.hh:52
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: r.hh:204
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
Print as is. For instance, don't try to escape labels.
Definition: format.hh:22
static constexpr star_status_t star_status()
Definition: r.hh:143
static value_t add(const value_t l, const value_t r)
Definition: r.hh:56
static value_t conv(self_t, const value_t v)
Definition: r.hh:163
weightset_mixin< detail::qmp_impl > qmp
Definition: fwd.hh:53
static value_t lgcd(const value_t l, const value_t r)
Definition: r.hh:74
static value_t conv(q, const q::value_t v)
Definition: r.hh:169
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static constexpr bool show_one()
Definition: r.hh:142
static r make(std::istream &is)
Build from the description in is.
Definition: r.hh:35
static value_t conv(std::istream &i, bool=true)
Definition: r.hh:187
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: r.hh:133
static value_t abs(const value_t v)
Definition: r.hh:146
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:62
Print as rich UTF-8 text, escaped.
Definition: format.hh:28
static size_t hash(const value_t v)
Definition: r.hh:157
static constexpr bool is_idempotent()
Definition: r.hh:139
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
double value_t
Definition: r.hh:41
value_t star(const value_t v) const
Definition: r.hh:101
An input/output format for valuesets.
Definition: format.hh:11
Definition: a-star.hh:8