Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
expand.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_EXPAND_HH
2 # define VCSN_ALGOS_EXPAND_HH
3 
4 # include <vcsn/ctx/fwd.hh>
5 # include <vcsn/core/rat/visitor.hh>
6 # include <vcsn/dyn/ratexp.hh>
7 
8 # include <vcsn/algos/derivation.hh> // ratexp_polynomialset_t.
9 
10 namespace vcsn
11 {
12 
13  namespace rat
14  {
15 
16  /*-----------------.
17  | expand(ratexp). |
18  `-----------------*/
19 
21  template <typename RatExpSet>
23  : public RatExpSet::const_visitor
24  {
25  public:
26  using ratexpset_t = RatExpSet;
27  using ratexp_t = typename ratexpset_t::value_t;
30  using weight_t = typename weightset_t::value_t;
31 
34 
35  using super_t = typename RatExpSet::const_visitor;
36 
37  constexpr static const char* me() { return "expand"; }
38 
40  : rs_(rs)
41  {}
42 
43  ratexp_t
44  operator()(const ratexp_t& v)
45  {
46  v->accept(*this);
47  return ratexp(res_);
48  }
49 
50  ratexp_t
52  {
53  ratexp_t res = rs_.zero();
54  for (const auto& m: p)
55  res = rs_.add(res, rs_.lmul(m.second, m.first));
56  return res;
57  }
58 
61  {
62  e->accept(*this);
63  return res_;
64  }
65 
67  {
68  res_ = ps_.zero();
69  }
70 
72  {
73  res_ = polynomial_t{{rs_.one(), ws_.one()}};
74  }
75 
77  {
78  res_ = polynomial_t{{rs_.atom(v.value()), ws_.one()}};
79  }
80 
82  {
83  polynomial_t res = ps_.zero();
84  for (auto c: v)
85  res = ps_.add(res, expand(c));
86  res_ = std::move(res);
87  }
88 
90  {
91  auto res = expand(v.head());
92  for (auto c: v.tail())
93  {
95  for (const auto& l: res)
96  for (const auto& r: expand(c))
97  ps_.add_here(sum,
98  rs_.conjunction(l.first, r.first),
99  ws_.mul(l.second, r.second));
100  res = sum;
101  }
102  res_ = std::move(res);
103  }
104 
109 
111  {
112  polynomial_t res = ps_.one();
113  for (auto c: v)
114  res = ps_.mul(res, expand(c));
115  res_ = std::move(res);
116  }
117 
119  {
120  // Recurse, but make it a star.
121  v.sub()->accept(*this);
122  res_ = polynomial_t{{rs_.star(ratexp(res_)), ws_.one()}};
123  }
124 
126  {
127  v.sub()->accept(*this);
128  res_ = ps_.lmul(v.weight(), std::move(res_));
129  }
130 
132  {
133  v.sub()->accept(*this);
134  res_ = ps_.rmul(std::move(res_), v.weight());
135  }
136 
137  private:
140  weightset_t ws_ = *rs_.weightset();
145  };
146 
147  } // rat::
148 
150  template <typename RatExpSet>
151  typename RatExpSet::value_t
152  expand(const RatExpSet& rs, const typename RatExpSet::value_t& e)
153  {
155  return expand(e);
156  }
157 
158  namespace dyn
159  {
160  namespace detail
161  {
163  template <typename RatExpSet>
164  ratexp
165  expand(const ratexp& exp)
166  {
167  const auto& e = exp->as<RatExpSet>();
168  return make_ratexp(e.ratexpset(),
169  ::vcsn::expand(e.ratexpset(), e.ratexp()));
170  }
171 
172  REGISTER_DECLARE(expand, (const ratexp& e) -> ratexp);
173  }
174  }
175 
176 } // vcsn::
177 
178 #endif // !VCSN_ALGOS_EXPAND_HH
Linear combination of labels: map labels to weights.
Definition: fwd.hh:32
ratexp make_ratexp(const RatExpSet &rs, const typename RatExpSet::value_t &ratexp)
Definition: ratexp.hh:85
typename weightset_t::value_t weight_t
Definition: expand.hh:30
expand_visitor(const ratexpset_t &rs)
Definition: expand.hh:39
value_t & add_here(value_t &v, const value_t &p) const
v += p.
An inner node with multiple children.
Definition: fwd.hh:123
#define VCSN_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:54
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
ratexp_t ratexp(const polynomial_t p)
Definition: expand.hh:51
static constexpr const char * me()
Definition: expand.hh:37
ratexp_polynomialset_t< RatExpSet > make_ratexp_polynomialset(const RatExpSet &rs)
From a RatExpSet to its polynomialset.
Definition: split.hh:34
typename ratexpset_t::value_t ratexp_t
Definition: expand.hh:27
weightset_t ws_
Shorthand to the weightset.
Definition: expand.hh:140
weightset_t_of< ratexpset_t > weightset_t
Definition: expand.hh:29
value_t rmul(const value_t &v, const weight_t &w) const
Right exterior product.
const value_t & zero() const
RatExpSet::value_t expand(const RatExpSet &rs, const typename RatExpSet::value_t &e)
Expanding a typed ratexp shared_ptr.
Definition: expand.hh:152
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
value_t lmul(const weight_t &w, const value_t &v) const
Left exterior product.
value_t mul(const value_t &l, const value_t &r) const
The product of polynomials l and r.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
typename polynomialset_t::value_t polynomial_t
Definition: expand.hh:33
polynomial_t res_
The result.
Definition: expand.hh:144
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
polynomialset_t ps_
Polynomialset of ratexps.
Definition: expand.hh:142
VCSN_RAT_VISIT(rweight, v)
Definition: expand.hh:131
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
ratexp expand(const ratexp &exp)
Bridge.
Definition: expand.hh:165
context_t_of< ratexpset_t > context_t
Definition: expand.hh:28
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
An inner node implementing a weight.
Definition: fwd.hh:145
std::map< label_t, weight_t, vcsn::less< labelset_t >> value_t
VCSN_RAT_VISIT(conjunction, v)
Definition: expand.hh:89
polynomial_t expand(const ratexp_t &e)
Syntactic sugar: recursive call to this visitor.
Definition: expand.hh:60
const value_t & one() const
typename RatExpSet::const_visitor super_t
Definition: expand.hh:35
VCSN_RAT_VISIT(lweight, v)
Definition: expand.hh:125
ratexp_t operator()(const ratexp_t &v)
Definition: expand.hh:44