Vcsn  2.1
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 symbol res("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 
141  static constexpr bool show_one() { return false; }
142  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
143 
144  static value_t
145  abs(const value_t v)
146  {
147  return v < 0 ? -v : v;
148  }
149 
150  static value_t
152  {
153  return v;
154  }
155 
156  static size_t hash(const value_t v)
157  {
158  return hash_value(v);
159  }
160 
161  static value_t
163  {
164  return v;
165  }
166 
167  static value_t
168  conv(q, const q::value_t v)
169  {
170  return value_t(v.num) / value_t(v.den);
171  }
172 
173  static value_t
174  conv(z, const z::value_t v)
175  {
176  return v;
177  }
178 
179  static value_t
180  conv(b, const b::value_t v)
181  {
182  return v;
183  }
184 
185  static value_t
186  conv(std::istream& i, bool = true)
187  {
188  value_t res;
189  if (i >> res)
190  return res;
191  else
192  raise(sname(), ": invalid value: ", i);
193  }
194 
195  static std::ostream&
196  print(const value_t v, std::ostream& o,
197  format = {})
198  {
199  return o << v;
200  }
201 
202  std::ostream&
203  print_set(std::ostream& o, format fmt = {}) const
204  {
205  if (fmt == format::latex)
206  o << "\\mathbb{R}";
207  else if (fmt == format::text)
208  o << sname();
209  else
210  raise("invalid format: ", fmt);
211  return o;
212  }
213  };
214 
215  VCSN_JOIN_SIMPLE(b, r);
216  VCSN_JOIN_SIMPLE(z, r);
217  VCSN_JOIN_SIMPLE(q, r);
219  VCSN_JOIN_SIMPLE(r, r);
220  }
221 
222 }
static constexpr bool is_idempotent()
Definition: r.hh:139
static value_t abs(const value_t v)
Definition: r.hh:145
static bool equal(const value_t l, const value_t r)
Definition: r.hh:127
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: r.hh:133
static symbol sname()
Definition: r.hh:28
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:62
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
static bool is_zero(const value_t v)
Definition: r.hh:115
static std::ostream & print(const value_t v, std::ostream &o, format={})
Definition: r.hh:196
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:68
static value_t ldiv(const value_t l, const value_t r)
Definition: r.hh:95
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static bool is_one(const value_t v)
Definition: r.hh:121
static size_t hash(const value_t v)
Definition: r.hh:156
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:186
An input/output format.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
static value_t conv(z, const z::value_t v)
Definition: r.hh:174
static value_t zero()
Definition: r.hh:44
static value_t rgcd(const value_t l, const value_t r)
Definition: r.hh:82
weightset_mixin< detail::q_impl > q
Definition: fwd.hh:52
static value_t rdiv(const value_t l, const value_t r)
Definition: r.hh:88
static constexpr bool is_commutative()
Definition: r.hh:138
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
static value_t add(const value_t l, const value_t r)
Definition: r.hh:56
static constexpr star_status_t star_status()
Definition: r.hh:142
static value_t transpose(const value_t v)
Definition: r.hh:151
valid iff proper succeeds on the "absolute value" of the automaton
Definition: star-status.hh:9
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t conv(self_t, const value_t v)
Definition: r.hh:162
static value_t conv(q, const q::value_t v)
Definition: r.hh:168
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: r.hh:203
star_status_t
Definition: star-status.hh:5
value_t star(const value_t v) const
Definition: r.hh:101
static value_t lgcd(const value_t l, const value_t r)
Definition: r.hh:74
static constexpr bool show_one()
Definition: r.hh:141
weightset_mixin< detail::qmp_impl > qmp
Definition: fwd.hh:53
VCSN_JOIN_SIMPLE(b, b)
static value_t conv(b, const b::value_t v)
Definition: r.hh:180
static value_t one()
Definition: r.hh:50
weightset_mixin< detail::z_impl > z
Definition: fwd.hh:56
double value_t
Definition: r.hh:41
static constexpr bool is_special(const value_t)
Definition: r.hh:109