Awali
Another Weighted Automata library
pmax.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_PMAX_HH
18 # define AWALI_WEIGHTSET_PMAX_HH
19 
20 # include <ostream>
21 # include <string>
22 # include <sstream>
23 # include <utility>
24 
25 #include <awali/sttc/misc/raise.hh>
26 #include <awali/common/enums.hh>
27 #include <awali/sttc/misc/stream.hh> // eat
31 
32 namespace awali {
33  namespace sttc {
40  class pmax
41  {
42  public:
43  using self_type = pmax;
44 
45  static std::string sname()
46  {
47  return "pmax";
48  }
49 
50  std::string vname(bool = true) const
51  {
52  return sname();
53  }
54 
56  static pmax make(std::istream& is)
57  {
58  eat(is, sname());
59  return {};
60  }
61 
62  using value_t = double;
63 
64  static value_t
65  add(const value_t l, const value_t r)
66  {
67  return std::max(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 (v <= 1)
93  return one();
94  else
95  raise(sname(), ": star: invalid value: ", format(*this, v));
96  }
97 
98  value_t
99  plus(const value_t v) const
100  {
101  if (v <= 1)
102  return v;
103  else
104  raise(sname(), ": star: invalid value: ", format(*this, v));
105  }
106 
107  static value_t
108  one()
109  {
110  return 1.0;
111  }
112 
113  static value_t
115  {
116  return 0.0;
117  }
118 
119  static bool
120  equals(const value_t l, const value_t r)
121  {
122  return l == r;
123  }
124 
126  static bool less_than(value_t lhs, value_t rhs)
127  {
128  return lhs < rhs;
129  }
130 
131  constexpr static bool is_special(value_t)
132  {
133  return false;
134  }
135 
136  static bool
137  is_zero(const value_t v)
138  {
139  return v == zero();
140  }
141 
142  static bool
143  is_one(const value_t v)
144  {
145  return v == one();
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::TOPS; }
152 
153  static value_t
155  {
156  return v;
157  }
158 
159  static size_t hash(value_t v)
160  {
161  return utils::hash_value(v);
162  }
163 
164  static value_t
166  {
167  return v;
168  }
169 
170  static value_t
172  {
173  return v ? one() : zero();
174  }
175 
176  static value_t
177  conv(std::istream& i)
178  {
179  value_t res;
180  if (i >> res) {
181  if(res<0)
182  sttc::fail_reading(i, sname() + ": invalid value");
183  return res;
184  }
185  else
186  sttc::fail_reading(i, sname() + ": invalid value");
187  }
188 
189  static value_t
190  parse(const std::string & s, size_t& p) {
191  size_t i=p;
192  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)
193  ;
194  if(i==p)
195  throw parse_exception("Wrong R value");
196  std::istringstream st(s.substr(i, p-i));
197  value_t x;
198  st >> x;
199  p=i;
200  return x;
201  }
202 
203  static std::ostream&
204  print(const value_t v, std::ostream& o,
205  const std::string& format = "text")
206  {
207  if(format == "json")
208  o<< '"';
209  o << v;
210  if(format == "json")
211  o<< '"';
212  return o;
213  }
214 
215  std::ostream&
216  print_set(std::ostream& o, const std::string& format = "text") const
217  {
218  if (format == "latex")
219  o << "\\mathbb{R}_{max,*}";
220  else if (format == "text")
221  o << "R-max-prod";
222  else
223  raise("invalid format: ", format);
224  return o;
225  }
226 
227  template<unsigned version = version::fsm_json>
228  value_t
229  value_from_json(json::node_t const* p) const {
230  version::check_fsmjson<version>();
231  switch (version) {
232  case 0: /* Never occurs due to above check. */
233  case 1:
234  default:
235  return p->to_double();
236  }
237  }
238 
239  template<unsigned version = version::fsm_json>
240  static json::node_t*
242  {
243  version::check_fsmjson<version>();
244  switch (version) {
245  case 0: /* Never occurs due to above check. */
246  case 1:
247  default:
248  return new json::object_t( "semiring",
249  new json::string_t("R-max-prod"));
250  }
251  }
252 
253  template<unsigned version = version::fsm_json>
255  const
256  {
257  version::check_fsmjson<version>();
258  switch (version) {
259  case 0: /* Never occurs due to above check. */
260  case 1:
261  default:
262  return new json::float_t(v);
263  }
264  }
265 
266  };
267 
268  inline pmax join(const pmax&, const pmax&) { return {}; }
269 
270  inline pmax join(const b&, const pmax&) { return {}; }
271  inline pmax join(const pmax&, const b&) { return {}; }
272 
273  }
274 }//end of ns awali::stc
275 
276 #endif // !AWALI_WEIGHTSET_PMAX_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
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of maximum probabilities.
Definition: pmax.hh:41
static value_t one()
Definition: pmax.hh:108
static value_t rdiv(const value_t l, const value_t r)
Definition: pmax.hh:77
std::string vname(bool=true) const
Definition: pmax.hh:50
static value_t mul(const value_t l, const value_t r)
Definition: pmax.hh:71
static value_t conv(std::istream &i)
Definition: pmax.hh:177
double value_t
Definition: pmax.hh:62
static std::string sname()
Definition: pmax.hh:45
value_t value_from_json(json::node_t const *p) const
Definition: pmax.hh:229
static value_t conv(self_type, value_t v)
Definition: pmax.hh:165
static value_t parse(const std::string &s, size_t &p)
Definition: pmax.hh:190
static json::node_t * to_json()
Definition: pmax.hh:241
static bool equals(const value_t l, const value_t r)
Definition: pmax.hh:120
static bool is_zero(const value_t v)
Definition: pmax.hh:137
static value_t transpose(const value_t v)
Definition: pmax.hh:154
static constexpr star_status_t star_status()
Definition: pmax.hh:151
static bool is_one(const value_t v)
Definition: pmax.hh:143
static size_t hash(value_t v)
Definition: pmax.hh:159
static constexpr bool show_one()
Definition: pmax.hh:150
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: pmax.hh:204
json::node_t * value_to_json(value_t v) const
Definition: pmax.hh:254
static value_t zero()
Definition: pmax.hh:114
static constexpr bool is_commutative_semiring()
Definition: pmax.hh:148
static value_t conv(b, b::value_t v)
Definition: pmax.hh:171
value_t plus(const value_t v) const
Definition: pmax.hh:99
static value_t add(const value_t l, const value_t r)
Definition: pmax.hh:65
value_t star(const value_t v) const
Definition: pmax.hh:90
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: pmax.hh:126
static pmax make(std::istream &is)
Build from the description in is.
Definition: pmax.hh:56
constexpr static bool is_special(value_t)
Definition: pmax.hh:131
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: pmax.hh:216
static value_t ldiv(const value_t l, const value_t r)
Definition: pmax.hh:84
The semiring of floating Numbers.
Definition: r.hh:35
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ TOPS
Definition: enums.hh:167
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
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26