Awali
Another Weighted Automata library
pmax.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_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  static value_t
99  one()
100  {
101  return 1.0;
102  }
103 
104  static value_t
106  {
107  return 0.0;
108  }
109 
110  static bool
111  equals(const value_t l, const value_t r)
112  {
113  return l == r;
114  }
115 
117  static bool less_than(value_t lhs, value_t rhs)
118  {
119  return lhs < rhs;
120  }
121 
122  constexpr static bool is_special(value_t)
123  {
124  return false;
125  }
126 
127  static bool
128  is_zero(const value_t v)
129  {
130  return v == zero();
131  }
132 
133  static bool
134  is_one(const value_t v)
135  {
136  return v == one();
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::TOPS; }
143 
144  static value_t
146  {
147  return v;
148  }
149 
150  static size_t hash(value_t v)
151  {
152  return utils::hash_value(v);
153  }
154 
155  static value_t
157  {
158  return v;
159  }
160 
161  static value_t
163  {
164  return v ? one() : zero();
165  }
166 
167  static value_t
168  conv(std::istream& i)
169  {
170  value_t res;
171  if (i >> res) {
172  if(res<0)
173  sttc::fail_reading(i, sname() + ": invalid value");
174  return res;
175  }
176  else
177  sttc::fail_reading(i, sname() + ": invalid value");
178  }
179 
180  static value_t
181  parse(const std::string & s, size_t& p) {
182  size_t i=p;
183  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)
184  ;
185  if(i==p)
186  throw parse_exception("Wrong R value");
187  std::istringstream st(s.substr(i, p-i));
188  value_t x;
189  st >> x;
190  p=i;
191  return x;
192  }
193 
194  static std::ostream&
195  print(const value_t v, std::ostream& o,
196  const std::string& format = "text")
197  {
198  if(format == "json")
199  o<< '"';
200  o << v;
201  if(format == "json")
202  o<< '"';
203  return o;
204  }
205 
206  std::ostream&
207  print_set(std::ostream& o, const std::string& format = "text") const
208  {
209  if (format == "latex")
210  o << "\\mathbb{R}_{max,*}";
211  else if (format == "text")
212  o << "R-max-prod";
213  else
214  raise("invalid format: ", format);
215  return o;
216  }
217 
218  template<unsigned version = version::fsm_json>
219  value_t
221  switch (version) {
222  case 0:
223  throw parse_exception("[pmax] Unsupported fsm-json version:"
224  + std::to_string(version));
225  case 1:
226  default:
227  return p->to_double();
228  }
229  }
230 
231  template<unsigned version = version::fsm_json>
232  static json::node_t*
234  {
235  switch (version) {
236  case 0:
237  throw parse_exception("[pmax] Unsupported fsm-json version:"
238  + std::to_string(version));
239  case 1:
240  default:
241  return new json::object_t( "semiring",
242  new json::string_t("R-max-prod"));
243  }
244  }
245 
246  template<unsigned version = version::fsm_json>
248  const
249  {
250  switch (version) {
251  case 0:
252  throw parse_exception("[pmax] Unsupported fsm-json version:"
253  + std::to_string(version));
254  case 1:
255  default:
256  return new json::float_t(v);
257  }
258  }
259 
260  };
261 
262  inline pmax join(const pmax&, const pmax&) { return {}; }
263 
264  inline pmax join(const b&, const pmax&) { return {}; }
265  inline pmax join(const pmax&, const b&) { return {}; }
266 
267  }
268 }//end of ns awali::stc
269 
270 #endif // !AWALI_WEIGHTSET_PMAX_HH
awali::sttc::pmax::is_one
static bool is_one(const value_t v)
Definition: pmax.hh:134
awali::sttc::pmax::value_from_json
value_t value_from_json(json::node_t *p) const
Definition: pmax.hh:220
awali::sttc::b::value_t
bool value_t
Definition: b.hh:56
awali
Main namespace of Awali.
Definition: ato.hh:22
awali::sttc::pmax::mul
static value_t mul(const value_t l, const value_t r)
Definition: pmax.hh:71
awali::star_status_t
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
awali::sttc::pmax::zero
static value_t zero()
Definition: pmax.hh:105
awali::sttc::join
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:448
awali::sttc::pmax::make
static pmax make(std::istream &is)
Build from the description in is.
Definition: pmax.hh:56
awali::sttc::pmax::value_to_json
json::node_t * value_to_json(value_t v) const
Definition: pmax.hh:247
awali::sttc::pmax::to_json
static json::node_t * to_json()
Definition: pmax.hh:233
awali::sttc::pmax::hash
static size_t hash(value_t v)
Definition: pmax.hh:150
b.hh
awali::sttc::pmax::vname
std::string vname(bool=true) const
Definition: pmax.hh:50
awali::sttc::pmax::conv
static value_t conv(self_type, value_t v)
Definition: pmax.hh:156
stream.hh
fwd.hh
awali::sttc::b
The Boolean semring.
Definition: b.hh:38
awali::TOPS
@ TOPS
Definition: enums.hh:165
awali::sttc::pmax::equals
static bool equals(const value_t l, const value_t r)
Definition: pmax.hh:111
awali::sttc::pmax::is_zero
static bool is_zero(const value_t v)
Definition: pmax.hh:128
awali::sttc::pmax::star
value_t star(const value_t v) const
Definition: pmax.hh:90
weightset.hh
awali::json::node_t::to_double
virtual double to_double() const
Coerces this node_t to a double
Definition: node.hh:340
awali::sttc::pmax::print
static std::ostream & print(const value_t v, std::ostream &o, const std::string &format="text")
Definition: pmax.hh:195
enums.hh
awali::sttc::require
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
awali::sttc::pmax::show_one
static constexpr bool show_one()
Definition: pmax.hh:141
awali::sttc::rat::to_string
std::string to_string(identities i)
awali::json::node_t
Definition: node.hh:191
awali::sttc::pmax::ldiv
static value_t ldiv(const value_t l, const value_t r)
Definition: pmax.hh:84
awali::sttc::pmax::conv
static value_t conv(std::istream &i)
Definition: pmax.hh:168
awali::parse_exception
Exceptions thrown during parsing.
Definition: parse_exception.hh:26
awali::sttc::pmax::is_commutative_semiring
static constexpr bool is_commutative_semiring()
Definition: pmax.hh:139
raise.hh
awali::sttc::pmax::less_than
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: pmax.hh:117
awali::json::object_t
Definition: node.hh:364
awali::sttc::format
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
awali::sttc::r
The semiring of floating Numbers.
Definition: r.hh:34
awali::sttc::pmax::sname
static std::string sname()
Definition: pmax.hh:45
awali::sttc::pmax::conv
static value_t conv(b, b::value_t v)
Definition: pmax.hh:162
awali::sttc::pmax::rdiv
static value_t rdiv(const value_t l, const value_t r)
Definition: pmax.hh:77
awali::json::float_t
Definition: node.hh:502
awali::sttc::pmax::parse
static value_t parse(const std::string &s, size_t &p)
Definition: pmax.hh:181
awali::sttc::pmax::one
static value_t one()
Definition: pmax.hh:99
awali::sttc::pmax::value_t
double value_t
Definition: pmax.hh:62
awali::sttc::pmax::star_status
static constexpr star_status_t star_status()
Definition: pmax.hh:142
awali::sttc::pmax::print_set
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: pmax.hh:207
awali::sttc::pmax::is_special
constexpr static bool is_special(value_t)
Definition: pmax.hh:122
awali::sttc::eat
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
awali::sttc::pmax::add
static value_t add(const value_t l, const value_t r)
Definition: pmax.hh:65
awali::sttc::fail_reading
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
awali::json::string_t
Definition: node.hh:525
awali::utils::max
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
awali::utils::hash_value
std::size_t hash_value(const T &v)
Definition: hash.hh:76
awali::sttc::pmax
The semiring of maximum probabilities.
Definition: pmax.hh:40
awali::sttc::pmax::transpose
static value_t transpose(const value_t v)
Definition: pmax.hh:145