Awali
Another Weighted Automata library
r.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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  value_t
111  plus(const value_t v) const
112  {
113  if (-1 < v && v < 1)
114  return v/(1-v);
115  else
116  raise(sname(), ": star: invalid value: ", format(*this, v));
117  }
118 
119  constexpr static bool is_special(value_t)
120  {
121  return false;
122  }
123 
124  static bool
125  is_zero(const value_t v)
126  {
127  return v == 0;
128  }
129 
130  static bool
131  is_one(const value_t v)
132  {
133  return v == 1;
134  }
135 
136  static bool
137  equals(const value_t l, const value_t r)
138  {
139  return l == r;
140  }
141 
143  static bool less_than(value_t lhs, value_t rhs)
144  {
145  return lhs < rhs;
146  }
147 
148  static constexpr bool is_commutative_semiring() { return true; }
149 
150  static constexpr bool show_one() { return false; }
151  static constexpr star_status_t star_status() { return star_status_t::ABSVAL; }
152 
153  static value_t
154  abs(const value_t v)
155  {
156  return v < 0 ? -v : v;
157  }
158 
159  static value_t
161  {
162  return v;
163  }
164 
165  static size_t hash(value_t v)
166  {
167  return utils::hash_value(v);
168  }
169 
170  static value_t
172  {
173  return v;
174  }
175 
176  static value_t
178  {
179  return value_t(v.num) / value_t(v.den);
180  }
181 
182  static value_t
184  {
185  return v;
186  }
187 
188  static value_t
190  {
191  return (value_t)v;
192  }
193 
194  static value_t
196  {
197  return v;
198  }
199 
200  static value_t
201  conv(std::istream& i)
202  {
203  value_t res;
204  if (i >> res)
205  return res;
206  else
207  fail_reading(i, sname() + ": invalid value");
208  }
209 
210  static value_t
211  parse(const std::string & s, size_t& p) {
212  return internal::lr_parse_double(s,p);
213  }
214 
215  static std::ostream&
216  print(const value_t v, std::ostream& o,
217  const std::string& /*format*/ = "text")
218  {
219  o << v;
220  return o;
221  }
222 
223  std::ostream&
224  print_set(std::ostream& o, const std::string& format = "text") const
225  {
226  if (format == "latex")
227  o << "\\mathbb{R}";
228  else if (format == "text")
229  o << "R";
230  else
231  raise("invalid format: ", format);
232  return o;
233  }
234 
235  template<unsigned version = version::fsm_json>
236  static
238  {
239  version::check_fsmjson<version>();
240  switch (version) {
241  case 0: /* Never occurs due to above check. */
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  version::check_fsmjson<version>();
253  switch (version) {
254  case 0: /* Never occurs due to above check. */
255  case 1:
256  default:
257  return new json::float_t(v);
258  }
259  }
260 
261  template<unsigned version = version::fsm_json>
263  const
264  {
265  version::check_fsmjson<version>();
266  switch (version) {
267  case 0: /* Never occurs due to above check. */
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:508
Definition: node.hh:193
virtual double to_double() const
Coerces this node_t to a double
Definition: node.hh:343
Definition: node.hh:367
Definition: node.hh:529
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:237
value_t plus(const value_t v) const
Definition: r.hh:111
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 constexpr star_status_t star_status()
Definition: r.hh:151
static value_t transpose(const value_t v)
Definition: r.hh:160
constexpr static bool is_special(value_t)
Definition: r.hh:119
static bool equals(const value_t l, const value_t r)
Definition: r.hh:137
double value_t
Definition: r.hh:56
static constexpr bool show_one()
Definition: r.hh:150
static value_t one()
Definition: r.hh:65
json::node_t * value_to_json(value_t v) const
Definition: r.hh:249
static size_t hash(value_t v)
Definition: r.hh:165
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: r.hh:216
static value_t conv(self_type, value_t v)
Definition: r.hh:171
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: r.hh:224
static value_t conv(z, z::value_t v)
Definition: r.hh:183
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:177
static value_t conv(b, b::value_t v)
Definition: r.hh:195
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:189
static bool is_zero(const value_t v)
Definition: r.hh:125
static value_t abs(const value_t v)
Definition: r.hh:154
static constexpr bool is_commutative_semiring()
Definition: r.hh:148
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:143
static bool is_one(const value_t v)
Definition: r.hh:131
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:201
value_t value_from_json(json::node_t const *p) const
Definition: r.hh:262
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:211
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:163
@ ABSVAL
Definition: enums.hh:168
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
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:449
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