Awali
Another Weighted Automata library
r.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_WEIGHTSET_R_HH
18 # define AWALI_WEIGHTSET_R_HH
19 
20 # include <string>
21 # include <ostream>
22 
23 #include <awali/sttc/misc/raise.hh>
24 #include <awali/common/enums.hh>
27 
28 namespace awali {
29  namespace sttc {
34  class r {
35  public:
36  using self_type = r;
37 
38  static std::string sname()
39  {
40  return "r";
41  }
42 
43  std::string vname(bool = true) const
44  {
45  return sname();
46  }
47 
49  static r make(std::istream& is)
50  {
51  eat(is, sname());
52  return {};
53  }
54 
55  using value_t = double;
56 
57  static value_t
58  zero()
59  {
60  return 0.;
61  }
62 
63  static value_t
64  one()
65  {
66  return 1.;
67  }
68 
69  static value_t
70  add(const value_t l, const value_t r)
71  {
72  return l + r;
73  }
74 
75  static value_t
76  sub(const value_t l, const value_t r)
77  {
78  return l - r;
79  }
80 
81  static value_t
82  mul(const value_t l, const value_t r)
83  {
84  return 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: ", format(*this, v));
107  }
108 
109  constexpr static bool is_special(value_t)
110  {
111  return false;
112  }
113 
114  static bool
115  is_zero(const value_t v)
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  equals(const value_t l, const value_t r)
128  {
129  return l == r;
130  }
131 
133  static bool less_than(value_t lhs, value_t rhs)
134  {
135  return lhs < rhs;
136  }
137 
138  static constexpr bool is_commutative_semiring() { return true; }
139 
140  static constexpr bool show_one() { return false; }
141  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
142 
143  static value_t
144  abs(const value_t v)
145  {
146  return v < 0 ? -v : v;
147  }
148 
149  static value_t
151  {
152  return v;
153  }
154 
155  static size_t hash(value_t v)
156  {
157  return utils::hash_value(v);
158  }
159 
160  static value_t
162  {
163  return v;
164  }
165 
166  static value_t
168  {
169  return value_t(v.num) / value_t(v.den);
170  }
171 
172  static value_t
174  {
175  return v;
176  }
177 
178  static value_t
180  {
181  return (value_t)v;
182  }
183 
184  static value_t
186  {
187  return v;
188  }
189 
190  static value_t
191  conv(std::istream& i)
192  {
193  value_t res;
194  if (i >> res)
195  return res;
196  else
197  fail_reading(i, sname() + ": invalid value");
198  }
199 
200  static value_t
201  parse(const std::string & s, size_t& p) {
202  size_t i=p;
203  for(; i>0 && ((s[i-1]>='0' && s[i-1]<='9') || s[i-1]=='e' || s[i-1]=='E' || s[i-1]=='.' || s[i-1]=='+' || s[i-1]=='-'); --i)
204  ;
205  if(i==p)
206  throw parse_exception("Wrong R value");
207  std::istringstream st(s.substr(i, p-i));
208  value_t x;
209  st >> x;
210  p=i;
211  return x;
212  }
213 
214  static std::ostream&
215  print(const value_t v, std::ostream& o,
216  const std::string& format = "text")
217  {
218  o << v;
219  return o;
220  }
221 
222  std::ostream&
223  print_set(std::ostream& o, const std::string& format = "text") const
224  {
225  if (format == "latex")
226  o << "\\mathbb{R}";
227  else if (format == "text")
228  o << "R";
229  else
230  raise("invalid format: ", format);
231  return o;
232  }
233 
234  template<unsigned version = version::fsm_json>
235  static
237  {
238  switch (version) {
239  case 0:
240  throw parse_exception("[R] Unsupported fsm-json version:"
241  + std::to_string(version));
242  case 1:
243  default:
244  return new json::object_t("semiring",new json::string_t("R"));
245  }
246  }
247 
248  template<unsigned version = version::fsm_json>
250  const
251  {
252  switch (version) {
253  case 0:
254  throw parse_exception("[R] Unsupported fsm-json version:"
255  + std::to_string(version));
256  case 1:
257  default:
258  return new json::float_t(v);
259  }
260  }
261 
262  template<unsigned version = version::fsm_json>
264  const
265  {
266  switch (version) {
267  case 0:
268  case 1:
269  default:
270  return p->to_double();
271  }
272  }
273 
274  };
275 
276  inline r join(const r&, const r&) { return {}; }
277 
278  inline r join(const q&, const r&) { return {}; }
279  inline r join(const r&, const q&) { return {}; }
280 
281  inline r join(const z&, const r&) { return {}; }
282  inline r join(const r&, const z&) { return {}; }
283 
284  inline r join(const n&, const r&) { return {}; }
285  inline r join(const r&, const n&) { return {}; }
286 
287  inline r join(const b&, const r&) { return {}; }
288  inline r join(const r&, const b&) { return {}; }
289 
290 }}//end of ns awali::stc
291 
292 #endif // !AWALI_WEIGHTSET_R_HH
Definition: node.hh:503
Definition: node.hh:191
virtual double to_double() const
Coerces this node_t to a double
Definition: node.hh:341
Definition: node.hh:365
Definition: node.hh:526
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of Natural numbers.
Definition: n.hh:33
unsigned int value_t
Definition: n.hh:54
The semiring of rational numbers.
Definition: q.hh:41
The semiring of floating Numbers.
Definition: r.hh:34
std::string vname(bool=true) const
Definition: r.hh:43
static std::string sname()
Definition: r.hh:38
static json::node_t * to_json()
Definition: r.hh:236
static value_t rdiv(const value_t l, const value_t r)
Definition: r.hh:88
static value_t ldiv(const value_t l, const value_t r)
Definition: r.hh:95
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: r.hh:215
static constexpr star_status_t star_status()
Definition: r.hh:141
static value_t transpose(const value_t v)
Definition: r.hh:150
constexpr static bool is_special(value_t)
Definition: r.hh:109
static bool equals(const value_t l, const value_t r)
Definition: r.hh:127
double value_t
Definition: r.hh:55
static constexpr bool show_one()
Definition: r.hh:140
static value_t one()
Definition: r.hh:64
json::node_t * value_to_json(value_t v) const
Definition: r.hh:249
static size_t hash(value_t v)
Definition: r.hh:155
static value_t conv(self_type, value_t v)
Definition: r.hh:161
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: r.hh:223
static value_t conv(z, z::value_t v)
Definition: r.hh:173
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:76
static value_t conv(q, q::value_t v)
Definition: r.hh:167
static value_t conv(b, b::value_t v)
Definition: r.hh:185
static value_t add(const value_t l, const value_t r)
Definition: r.hh:70
static value_t conv(n, n::value_t v)
Definition: r.hh:179
static bool is_zero(const value_t v)
Definition: r.hh:115
static value_t abs(const value_t v)
Definition: r.hh:144
static constexpr bool is_commutative_semiring()
Definition: r.hh:138
static value_t zero()
Definition: r.hh:58
value_t star(const value_t v) const
Definition: r.hh:101
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: r.hh:133
static bool is_one(const value_t v)
Definition: r.hh:121
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:82
static value_t conv(std::istream &i)
Definition: r.hh:191
value_t value_from_json(json::node_t const *p) const
Definition: r.hh:263
static r make(std::istream &is)
Build from the description in is.
Definition: r.hh:49
static value_t parse(const std::string &s, size_t &p)
Definition: r.hh:201
The semiring of Integers.
Definition: z.hh:34
int value_t
Definition: z.hh:55
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
@ ABSVAL
Definition: enums.hh:166
std::string to_string(identities i)
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:448
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
std::size_t hash_value(const T &v)
Definition: hash.hh:76
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26
Definition: qfraction.hh:26
int num
Definition: qfraction.hh:27
unsigned int den
Definition: qfraction.hh:28