Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
r.hh
Go to the documentation of this file.
1 #ifndef VCSN_WEIGHTSET_R_HH
2 # define VCSN_WEIGHTSET_R_HH
3 
4 # include <string>
5 # include <ostream>
6 
7 # include <vcsn/core/join.hh>
8 # include <vcsn/misc/raise.hh>
9 # include <vcsn/misc/star_status.hh>
10 # include <vcsn/misc/stream.hh>
11 # include <vcsn/weightset/fwd.hh>
12 # include <vcsn/weightset/b.hh>
13 # include <vcsn/weightset/q.hh>
14 # include <vcsn/weightset/qmp.hh>
15 # include <vcsn/weightset/z.hh>
17 
18 namespace vcsn
19 {
20  namespace detail
21  {
22  class r_impl
23  {
24  public:
25  using self_type = r;
26 
27  static std::string sname()
28  {
29  return "r";
30  }
31 
32  std::string vname(bool = true) const
33  {
34  return sname();
35  }
36 
38  static r make(std::istream& is)
39  {
40  eat(is, sname());
41  return {};
42  }
43 
44  using value_t = double;
45 
46  static value_t
47  zero()
48  {
49  return 0.;
50  }
51 
52  static value_t
53  one()
54  {
55  return 1.;
56  }
57 
58  static value_t
59  add(const value_t l, const value_t r)
60  {
61  return l + r;
62  }
63 
64  static value_t
65  sub(const value_t l, const value_t r)
66  {
67  return l - r;
68  }
69 
70  static value_t
71  mul(const value_t l, const value_t r)
72  {
73  return l * r;
74  }
75 
76  static value_t
77  rdiv(const value_t l, const value_t r)
78  {
79  require(!is_zero(r), "div: division by zero");
80  return l / r;
81  }
82 
83  static value_t
84  ldiv(const value_t l, const value_t r)
85  {
86  return rdiv(r, l);
87  }
88 
89  value_t
90  star(const value_t v) const
91  {
92  if (-1 < v && v < 1)
93  return 1/(1-v);
94  else
95  raise(sname(), ": star: invalid value: ", to_string(*this, v));
96  }
97 
98  constexpr static bool is_special(value_t)
99  {
100  return false;
101  }
102 
103  static bool
104  is_zero(const value_t v)
105  {
106  return v == 0;
107  }
108 
109  static bool
110  is_one(const value_t v)
111  {
112  return v == 1;
113  }
114 
115  static bool
116  equals(const value_t l, const value_t r)
117  {
118  return l == r;
119  }
120 
122  static bool less_than(value_t lhs, value_t rhs)
123  {
124  return lhs < rhs;
125  }
126 
127  static constexpr bool is_commutative() { return true; }
128  static constexpr bool is_idempotent() { return false; }
129 
130  static constexpr bool show_one() { return false; }
131  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
132 
133  static value_t
134  abs(const value_t v)
135  {
136  return v < 0 ? -v : v;
137  }
138 
139  static value_t
141  {
142  return v;
143  }
144 
145  static size_t hash(value_t v)
146  {
147  return hash_value(v);
148  }
149 
150  static value_t
152  {
153  return v;
154  }
155 
156  static value_t
157  conv(q, q::value_t v)
158  {
159  return value_t(v.num) / value_t(v.den);
160  }
161 
162  static value_t
163  conv(z, z::value_t v)
164  {
165  return v;
166  }
167 
168  static value_t
169  conv(b, b::value_t v)
170  {
171  return v;
172  }
173 
174  static value_t
175  conv(std::istream& i)
176  {
177  value_t res;
178  if (i >> res)
179  return res;
180  else
181  vcsn::fail_reading(i, sname() + ": invalid value");
182  }
183 
184  static std::ostream&
185  print(const value_t v, std::ostream& o,
186  const std::string& = "text")
187  {
188  return o << v;
189  }
190 
191  std::ostream&
192  print_set(std::ostream& o, symbol format = symbol{"text"}) const
193  {
194  if (format == "latex")
195  o << "\\mathbb{R}";
196  else if (format == "text")
197  o << vname();
198  else
199  raise("invalid format: ", format);
200  return o;
201  }
202  };
203 
204  VCSN_JOIN_SIMPLE(b, r);
205  VCSN_JOIN_SIMPLE(z, r);
206  VCSN_JOIN_SIMPLE(q, r);
208  VCSN_JOIN_SIMPLE(r, r);
209  }
210 
211 }
212 
213 #endif // !VCSN_WEIGHTSET_R_HH
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: r.hh:122
static value_t rdiv(const value_t l, const value_t r)
Definition: r.hh:77
static bool is_zero(const value_t v)
Definition: r.hh:104
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:65
value_t star(const value_t v) const
Definition: r.hh:90
static value_t one()
Definition: r.hh:53
static std::string sname()
Definition: r.hh:27
static constexpr bool is_idempotent()
Definition: r.hh:128
static value_t conv(b, b::value_t v)
Definition: r.hh:169
variadic_mul_mixin< detail::qmp_impl > qmp
Definition: fwd.hh:41
std::string vname(bool=true) const
Definition: r.hh:32
std::ostream & print_set(std::ostream &o, symbol format=symbol{"text"}) const
Definition: r.hh:192
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
Definition: symbol.hh:24
variadic_mul_mixin< detail::b_impl > b
Definition: fwd.hh:38
variadic_mul_mixin< detail::q_impl > q
Definition: fwd.hh:40
static value_t add(const value_t l, const value_t r)
Definition: r.hh:59
std::size_t hash_value(const T &v)
Definition: hash.hh:61
static value_t ldiv(const value_t l, const value_t r)
Definition: r.hh:84
static size_t hash(value_t v)
Definition: r.hh:145
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:71
variadic_mul_mixin< detail::z_impl > z
Definition: fwd.hh:43
static constexpr bool is_commutative()
Definition: r.hh:127
static constexpr star_status_t star_status()
Definition: r.hh:131
static bool equals(const value_t l, const value_t r)
Definition: r.hh:116
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
star_status_t
Definition: star_status.hh:6
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: r.hh:185
double value_t
Definition: r.hh:44
static value_t abs(const value_t v)
Definition: r.hh:134
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
static value_t zero()
Definition: r.hh:47
static value_t transpose(const value_t v)
Definition: r.hh:140
static value_t conv(std::istream &i)
Definition: r.hh:175
VCSN_JOIN_SIMPLE(b, b)
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static r make(std::istream &is)
Build from the description in is.
Definition: r.hh:38
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.cc:107
static value_t conv(q, q::value_t v)
Definition: r.hh:157
static value_t conv(z, z::value_t v)
Definition: r.hh:163
static constexpr bool is_special(value_t)
Definition: r.hh:98
static bool is_one(const value_t v)
Definition: r.hh:110
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42
static constexpr bool show_one()
Definition: r.hh:130
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39
static value_t conv(self_type, value_t v)
Definition: r.hh:151
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7