Awali
Another Weighted Automata library
expand.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 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_ALGOS_EXPAND_HH
18 # define AWALI_ALGOS_EXPAND_HH
19 
20 #include <awali/sttc/ctx/fwd.hh>
22 
23 #include <awali/sttc/algos/derivation.hh> // ratexp_polynomialset_t.
24 
25 namespace awali { namespace sttc {
26 
27 
28  namespace rat
29  {
30 
31  /*-----------------.
32  | expand(ratexp). |
33  `-----------------*/
34 
36  template <typename RatExpSet>
38  : public RatExpSet::const_visitor
39  {
40  public:
41  using ratexpset_t = RatExpSet;
42  using ratexp_t = typename ratexpset_t::value_t;
45  using weight_t = typename weightset_t::value_t;
46 
49 
50  using super_type = typename RatExpSet::const_visitor;
51 
52  constexpr static const char* me() { return "expand"; }
53 
55  : rs_(rs)
56  {}
57 
58  ratexp_t
59  operator()(const ratexp_t& v)
60  {
61  v->accept(*this);
62  return ratexp(res_);
63  }
64 
65  ratexp_t
67  {
68  ratexp_t res = rs_.zero();
69  for (const auto& m: p)
70  res = rs_.add(res, rs_.lmul(m.second, m.first));
71  return res;
72  }
73 
76  {
77  e->accept(*this);
78  return res_;
79  }
80 
82  {
83  res_ = ps_.zero();
84  }
85 
87  {
88  res_ = polynomial_t{{rs_.one(), ws_.one()}};
89  }
90 
92  {
93  res_ = polynomial_t{{rs_.atom(v.value()), ws_.one()}};
94  }
95 
97  {
98  polynomial_t res = ps_.zero();
99  for (auto c: v)
100  res = ps_.add(res, expand(c));
101  res_ = std::move(res);
102  }
103 
105  {
106  auto res = expand(v.head());
107  for (auto c: v.tail())
108  {
109  polynomial_t sum = ps_.zero();
110  for (const auto& l: res)
111  for (const auto& r: expand(c))
112  ps_.add_here(sum,
113  rs_.conjunction(l.first, r.first),
114  ws_.mul(l.second, r.second));
115  res = sum;
116  }
117  res_ = std::move(res);
118  }
119 
124 
126  {
127  polynomial_t res = ps_.one();
128  for (auto c: v)
129  res = ps_.mul(res, expand(c));
130  res_ = std::move(res);
131  }
132 
134  {
135  // Recurse, but make it a star.
136  v.sub()->accept(*this);
137  res_ = polynomial_t{{rs_.star(ratexp(res_)), ws_.one()}};
138  }
139 
141  {
142  v.sub()->accept(*this);
143  res_ = ps_.lmul(v.weight(), std::move(res_));
144  }
145 
147  {
148  v.sub()->accept(*this);
149  res_ = ps_.rmul(std::move(res_), v.weight());
150  }
151 
152  private:
153  ratexpset_t rs_;
155  weightset_t ws_ = *rs_.weightset();
159  polynomial_t res_;
160  };
161 
162  } // rat::
163 
165  template <typename RatExpSet>
166  typename RatExpSet::value_t
167  expand(const RatExpSet& rs, const typename RatExpSet::value_t& e)
168  {
170  return expand(e);
171  }
172 
173 }}//end of ns awali::stc
174 
175 #endif // !AWALI_ALGOS_EXPAND_HH
The semiring of complex numbers.
Definition: c.hh:44
Linear combination of labels: map labels to weights.
Definition: polynomialset.hh:69
value_t mul(const value_t &l, const value_t &r) const
The product of polynomials l and r.
Definition: polynomialset.hh:192
const value_t & one() const
Definition: polynomialset.hh:327
std::map< label_t, weight_t, internal::less< labelset_t > > value_t
Definition: polynomialset.hh:83
value_t & add_here(value_t &v, const value_t &p) const
v += p.
Definition: polynomialset.hh:130
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
Definition: polynomialset.hh:182
const value_t & zero() const
Definition: polynomialset.hh:353
value_t rmul(const value_t &v, const weight_t &w) const
Right exterior product.
Definition: polynomialset.hh:266
value_t lmul(const weight_t &w, const value_t &v) const
Left exterior product.
Definition: polynomialset.hh:241
The semiring of floating Numbers.
Definition: r.hh:35
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: expand.hh:39
ratexp_polynomialset_t< ratexpset_t > polynomialset_t
Definition: expand.hh:47
expand_visitor(const ratexpset_t &rs)
Definition: expand.hh:54
polynomial_t expand(const ratexp_t &e)
Syntactic sugar: recursive call to this visitor.
Definition: expand.hh:75
AWALI_RAT_VISIT(atom, v)
Definition: expand.hh:91
typename weightset_t::value_t weight_t
Definition: expand.hh:45
typename RatExpSet::const_visitor super_type
Definition: expand.hh:50
AWALI_RAT_VISIT(lweight, v)
Definition: expand.hh:140
AWALI_RAT_VISIT(zero,)
Definition: expand.hh:81
ratexp_t operator()(const ratexp_t &v)
Definition: expand.hh:59
RatExpSet ratexpset_t
Definition: expand.hh:41
context_t_of< ratexpset_t > context_t
Definition: expand.hh:43
AWALI_RAT_VISIT(conjunction, v)
Definition: expand.hh:104
weightset_t_of< ratexpset_t > weightset_t
Definition: expand.hh:44
typename polynomialset_t::value_t polynomial_t
Definition: expand.hh:48
AWALI_RAT_VISIT(star, v)
Definition: expand.hh:133
AWALI_RAT_VISIT(rweight, v)
Definition: expand.hh:146
AWALI_RAT_VISIT(sum, v)
Definition: expand.hh:96
AWALI_RAT_VISIT(one,)
Definition: expand.hh:86
ratexp_t ratexp(const polynomial_t p)
Definition: expand.hh:66
typename ratexpset_t::value_t ratexp_t
Definition: expand.hh:42
constexpr static const char * me()
Definition: expand.hh:52
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
variadic< type_t::sum, Label, Weight > sum
Definition: fwd.hh:154
ratexp_polynomialset_t< RatExpSet > make_ratexp_polynomialset(const RatExpSet &rs)
From a RatExpSet to its polynomialset.
Definition: split.hh:48
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
RatExpSet::value_t expand(const RatExpSet &rs, const typename RatExpSet::value_t &e)
Expanding a typed ratexp shared_ptr.
Definition: expand.hh:167
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73