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>
28 
29 namespace awali {
30  namespace sttc {
35  class r {
36  public:
37  using self_type = r;
38 
39  static std::string sname()
40  {
41  return "r";
42  }
43 
44  std::string vname(bool = true) const
45  {
46  return sname();
47  }
48 
50  static r make(std::istream& is)
51  {
52  eat(is, sname());
53  return {};
54  }
55 
56  using value_t = double;
57 
58  static value_t
59  zero()
60  {
61  return 0.;
62  }
63 
64  static value_t
65  one()
66  {
67  return 1.;
68  }
69 
70  static value_t
71  add(const value_t l, const value_t r)
72  {
73  return l + r;
74  }
75 
76  static value_t
77  sub(const value_t l, const value_t r)
78  {
79  return l - r;
80  }
81 
82  static value_t
83  mul(const value_t l, const value_t r)
84  {
85  return l * r;
86  }
87 
88  static value_t
89  rdiv(const value_t l, const value_t r)
90  {
91  require(!is_zero(r), "div: division by zero");
92  return l / r;
93  }
94 
95  static value_t
96  ldiv(const value_t l, const value_t r)
97  {
98  return rdiv(r, l);
99  }
100 
101  value_t
102  star(const value_t v) const
103  {
104  if (-1 < v && v < 1)
105  return 1/(1-v);
106  else
107  raise(sname(), ": star: invalid value: ", format(*this, v));
108  }
109 
110  constexpr static bool is_special(value_t)
111  {
112  return false;
113  }
114 
115  static bool
116  is_zero(const value_t v)
117  {
118  return v == 0;
119  }
120 
121  static bool
122  is_one(const value_t v)
123  {
124  return v == 1;
125  }
126 
127  static bool
128  equals(const value_t l, const value_t r)
129  {
130  return l == r;
131  }
132 
134  static bool less_than(value_t lhs, value_t rhs)
135  {
136  return lhs < rhs;
137  }
138 
139  static constexpr bool is_commutative_semiring() { return true; }
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(value_t v)
157  {
158  return utils::hash_value(v);
159  }
160 
161  static value_t
163  {
164  return v;
165  }
166 
167  static value_t
169  {
170  return value_t(v.num) / value_t(v.den);
171  }
172 
173  static value_t
175  {
176  return v;
177  }
178 
179  static value_t
181  {
182  return (value_t)v;
183  }
184 
185  static value_t
187  {
188  return v;
189  }
190 
191  static value_t
192  conv(std::istream& i)
193  {
194  value_t res;
195  if (i >> res)
196  return res;
197  else
198  fail_reading(i, sname() + ": invalid value");
199  }
200 
201  static value_t
202  parse(const std::string & s, size_t& p) {
203  return internal::lr_parse_double(s,p);
204  }
205 
206  static std::ostream&
207  print(const value_t v, std::ostream& o,
208  const std::string& format = "text")
209  {
210  o << v;
211  return o;
212  }
213 
214  std::ostream&
215  print_set(std::ostream& o, const std::string& format = "text") const
216  {
217  if (format == "latex")
218  o << "\\mathbb{R}";
219  else if (format == "text")
220  o << "R";
221  else
222  raise("invalid format: ", format);
223  return o;
224  }
225 
226  template<unsigned version = version::fsm_json>
227  static
229  {
230  switch (version) {
231  case 0:
232  throw parse_exception("[R] Unsupported fsm-json version:"
233  + std::to_string(version));
234  case 1:
235  default:
236  return new json::object_t("semiring",new json::string_t("R"));
237  }
238  }
239 
240  template<unsigned version = version::fsm_json>
242  const
243  {
244  switch (version) {
245  case 0:
246  throw parse_exception("[R] Unsupported fsm-json version:"
247  + std::to_string(version));
248  case 1:
249  default:
250  return new json::float_t(v);
251  }
252  }
253 
254  template<unsigned version = version::fsm_json>
256  const
257  {
258  switch (version) {
259  case 0:
260  case 1:
261  default:
262  return p->to_double();
263  }
264  }
265 
266  };
267 
268  inline r join(const r&, const r&) { return {}; }
269 
270  inline r join(const q&, const r&) { return {}; }
271  inline r join(const r&, const q&) { return {}; }
272 
273  inline r join(const z&, const r&) { return {}; }
274  inline r join(const r&, const z&) { return {}; }
275 
276  inline r join(const n&, const r&) { return {}; }
277  inline r join(const r&, const n&) { return {}; }
278 
279  inline r join(const b&, const r&) { return {}; }
280  inline r join(const r&, const b&) { return {}; }
281 
282 }}//end of ns awali::stc
283 
284 #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
Definition: qfraction.hh:26
den_t den
Definition: qfraction.hh:32
num_t num
Definition: qfraction.hh:31
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of Natural numbers.
Definition: n.hh:34
unsigned int value_t
Definition: n.hh:55
The semiring of rational numbers.
Definition: q.hh:42
The semiring of floating Numbers.
Definition: r.hh:35
std::string vname(bool=true) const
Definition: r.hh:44
static std::string sname()
Definition: r.hh:39
static json::node_t * to_json()
Definition: r.hh:228
static value_t rdiv(const value_t l, const value_t r)
Definition: r.hh:89
static value_t ldiv(const value_t l, const value_t r)
Definition: r.hh:96
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: r.hh:207
static constexpr star_status_t star_status()
Definition: r.hh:142
static value_t transpose(const value_t v)
Definition: r.hh:151
constexpr static bool is_special(value_t)
Definition: r.hh:110
static bool equals(const value_t l, const value_t r)
Definition: r.hh:128
double value_t
Definition: r.hh:56
static constexpr bool show_one()
Definition: r.hh:141
static value_t one()
Definition: r.hh:65
json::node_t * value_to_json(value_t v) const
Definition: r.hh:241
static size_t hash(value_t v)
Definition: r.hh:156
static value_t conv(self_type, value_t v)
Definition: r.hh:162
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: r.hh:215
static value_t conv(z, z::value_t v)
Definition: r.hh:174
static value_t sub(const value_t l, const value_t r)
Definition: r.hh:77
static value_t conv(q, q::value_t v)
Definition: r.hh:168
static value_t conv(b, b::value_t v)
Definition: r.hh:186
static value_t add(const value_t l, const value_t r)
Definition: r.hh:71
static value_t conv(n, n::value_t v)
Definition: r.hh:180
static bool is_zero(const value_t v)
Definition: r.hh:116
static value_t abs(const value_t v)
Definition: r.hh:145
static constexpr bool is_commutative_semiring()
Definition: r.hh:139
static value_t zero()
Definition: r.hh:59
value_t star(const value_t v) const
Definition: r.hh:102
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: r.hh:134
static bool is_one(const value_t v)
Definition: r.hh:122
static value_t mul(const value_t l, const value_t r)
Definition: r.hh:83
static value_t conv(std::istream &i)
Definition: r.hh:192
value_t value_from_json(json::node_t const *p) const
Definition: r.hh:255
static r make(std::istream &is)
Build from the description in is.
Definition: r.hh:50
static value_t parse(const std::string &s, size_t &p)
Definition: r.hh:202
The semiring of Integers.
Definition: z.hh:35
int value_t
Definition: z.hh:56
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
@ ABSVAL
Definition: enums.hh:166
double lr_parse_double(std::string const &s, size_t &p, bool allow_empty=false, double value_if_empty=0)
Reads a double left of position p in string, by using as many characters as possible.
Definition: lr_parse_number.hh:409
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