Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
split.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_SPLIT_HH
2 # define VCSN_ALGOS_SPLIT_HH
3 
4 # include <vcsn/core/rat/visitor.hh>
5 # include <vcsn/ctx/fwd.hh>
6 # include <vcsn/dyn/polynomial.hh>
7 # include <vcsn/dyn/ratexp.hh>
8 # include <vcsn/misc/raise.hh>
10 
11 namespace vcsn
12 {
13 
14  namespace rat
15  {
16  // FIXME: this is a general feature which is useful elsewhere.
17  // E.g., expand.
18 
20  template <typename RatExpSet>
22  = polynomialset<context<RatExpSet,
24 
26  template <typename RatExpSet>
29 
31  template <typename RatExpSet>
32  inline
34  make_ratexp_polynomialset(const RatExpSet& rs)
35  {
36  using context_t = context<RatExpSet,
38  return context_t{rs, *rs.weightset()};
39  }
40  }
41 
42 
43  /*----------------.
44  | split(ratexp). |
45  `----------------*/
46 
47  namespace rat
48  {
77  template <typename RatExpSet>
79  : public RatExpSet::const_visitor
80  {
81  public:
82  using ratexpset_t = RatExpSet;
86  using ratexp_t = typename ratexpset_t::value_t;
88  using weight_t = typename weightset_t::value_t;
89 
92 
93  using super_t = typename ratexpset_t::const_visitor;
94 
95  constexpr static const char* me() { return "split"; }
96 
98  : rs_(rs)
99  {}
100 
103  {
104  return split(v);
105  }
106 
109  {
110  v->accept(*this);
111  return std::move(res_);
112  }
113 
115  {
116  res_ = ps_.zero();
117  }
118 
120  {
121  res_ = polynomial_t{{rs_.one(), ws_.one()}};
122  }
123 
125  {
126  res_ = polynomial_t{{rs_.atom(e.value()), ws_.one()}};
127  }
128 
130  {
131  polynomial_t res = ps_.zero();
132  for (const auto& v: e)
133  {
134  v->accept(*this);
135  ps_.add_here(res, res_);
136  }
137  res_ = std::move(res);
138  }
139 
145  {
146  // B(l).
147  polynomial_t l_split = split(l);
148  // constant-term(B(l)).
149  weight_t l_split_const = ps_.get_weight(l_split, rs_.one());
150  // proper(B(l)).
151  ps_.del_weight(l_split, rs_.one());
152 
153  // res = proper(B(l)).r + constant-term(B(l))B(r).
154  return ps_.add(ps_.rmul(l_split, r),
155  ps_.lmul(l_split_const, split(r)));
156  }
157 
162  {
163  polynomial_t res;
164  for (const auto& m: l)
165  ps_.add_here(res, ps_.lmul(m.second, product(m.first, r)));
166  return res;
167  }
168 
171  {
172  auto res = product(e[0], e[1]);
173  for (unsigned i = 2, n = e.size(); i < n; ++i)
174  res = product(res, e[i]);
175  res_ = std::move(res);
176  }
177 
183  {
184  // B(l).
185  polynomial_t l_split = split(l);
186  // constant-term(B(l)).
187  weight_t l_split_const = ps_.get_weight(l_split, rs_.one());
188  // proper(B(l)).
189  ps_.del_weight(l_split, rs_.one());
190 
191  // res = proper(B(l))&r.
192  polynomial_t res;
193  for (const auto& e: l_split)
194  ps_.add_here(res, rs_.conjunction(e.first, r), e.second);
195  // res += constant-term(B(l))B(r)
196  ps_.add_here(res,
197  ps_.lmul(l_split_const, split(r)));
198  return res;
199  }
200 
205  {
206  polynomial_t res;
207  for (const auto& m: l)
208  ps_.add_here(res, ps_.lmul(m.second, conjunction(m.first, r)));
209  return res;
210  }
211 
214  {
215  auto res = conjunction(e[0], e[1]);
216  for (unsigned i = 2, n = e.size(); i < n; ++i)
217  res = conjunction(res, e[i]);
218  res_ = std::move(res);
219  }
220 
225 
227  {
228  res_ = polynomial_t{{e.shared_from_this(), ws_.one()}};
229  }
230 
232  {
233  e.sub()->accept(*this);
234  res_ = ps_.lmul(e.weight(), res_);
235  }
236 
238  {
239  e.sub()->accept(*this);
240  res_ = ps_.rmul(res_, e.weight());
241  }
242 
243  private:
246  weightset_t ws_ = *rs_.weightset();
250  };
251  }
252 
254  template <typename RatExpSet>
255  inline
257  split(const RatExpSet& rs, const typename RatExpSet::value_t& e)
258  {
260  return split(e);
261  }
262 
263  namespace dyn
264  {
265  namespace detail
266  {
268  template <typename RatExpSet>
269  polynomial
270  split(const ratexp& exp)
271  {
272  const auto& e = exp->as<RatExpSet>();
273  const auto& rs = e.ratexpset();
275  return make_polynomial(ps,
276  vcsn::split<RatExpSet>(rs, e.ratexp()));
277  }
278 
280  (const ratexp& e) -> polynomial);
281  }
282  }
283 
285  template <typename RatExpSet>
286  inline
287  rat::ratexp_polynomial_t<RatExpSet>
288  split(const RatExpSet& rs, const rat::ratexp_polynomial_t<RatExpSet>& p)
289  {
290  auto ps = rat::make_ratexp_polynomialset(rs);
291  using polynomial_t = rat::ratexp_polynomial_t<RatExpSet>;
293  polynomial_t res;
294  for (const auto& m: p)
295  res = ps.add(res, ps.lmul(m.second, split(m.first)));
296  return res;
297  }
298 
300  template <typename PolynomialSet>
301  inline
302  typename PolynomialSet::value_t
303  split_polynomial(const PolynomialSet& ps,
304  const typename PolynomialSet::value_t& p)
305  {
306  using polynomial_t = typename PolynomialSet::value_t;
307  // This is a polynomial of rational expressions.
308  const auto& rs = *ps.labelset();
309  polynomial_t res;
310  for (const auto& m: p)
311  res = ps.add(res, ps.lmul(m.second, split(rs, m.first)));
312  return res;
313  }
314 
315  namespace dyn
316  {
317  namespace detail
318  {
320  template <typename PolynomialSet>
321  polynomial
323  {
324  const auto& p = poly->as<PolynomialSet>();
325  const auto& ps = p.polynomialset();
326  return make_polynomial
327  (ps,
328  vcsn::split_polynomial<PolynomialSet>(ps, p.polynomial()));
329  }
330 
332  (const polynomial& p) -> polynomial);
333  }
334  }
335 
336 
337 } // vcsn::
338 
339 #endif // !VCSN_ALGOS_SPLIT_HH
Linear combination of labels: map labels to weights.
Definition: fwd.hh:32
polynomial_t conjunction(const polynomial_t &l, const ratexp_t &r)
The split-product of l with r.
Definition: split.hh:204
RatExpSet ratexpset_t
Definition: split.hh:82
PolynomialSet::value_t split_polynomial(const PolynomialSet &ps, const typename PolynomialSet::value_t &p)
Split a polynomial of ratexps.
Definition: split.hh:303
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)
polynomial split_polynomial(const polynomial &poly)
Bridge.
Definition: split.hh:322
labelset_t_of< context_t > labelset_t
Definition: split.hh:84
polynomial make_polynomial(const PolynomialSet &ps, const typename PolynomialSet::value_t &polynomial)
Definition: polynomial.hh:91
ratexp_polynomialset_t< RatExpSet > make_ratexp_polynomialset(const RatExpSet &rs)
From a RatExpSet to its polynomialset.
Definition: split.hh:34
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
polynomial_t operator()(const ratexp_t &v)
Break a ratexp into a polynomial.
Definition: split.hh:102
Break a rational expression into a polynomial.
Definition: split.hh:78
const weightset_ptr & weightset() const
Definition: context.hh:129
typename ratexpset_t::value_t ratexp_t
Definition: split.hh:86
polynomial_t conjunction(const ratexp_t &l, const ratexp_t &r)
The split-product of l with r.
Definition: split.hh:182
weightset_t ws_
Shorthand to the weightset.
Definition: split.hh:246
VCSN_RAT_VISIT(atom, e)
Definition: split.hh:124
VCSN_RAT_VISIT(lweight, e)
Definition: split.hh:231
split_visitor(const ratexpset_t &rs)
Definition: split.hh:97
value_t rmul(const value_t &v, const weight_t &w) const
Right exterior product.
polynomialset< context< RatExpSet, weightset_t_of< RatExpSet >>> ratexp_polynomialset_t
Type of PolynomialSet of RatExps from the RatExpSet type.
Definition: split.hh:23
polynomial_t product(const ratexp_t &l, const ratexp_t &r)
The split-product of l with r.
Definition: split.hh:144
const value_t & zero() const
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.
typename polynomialset_t::value_t polynomial_t
Definition: split.hh:91
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
VCSN_RAT_VISIT(prod, e)
Handle an n-ary product.
Definition: split.hh:170
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
polynomial_t res_
The result.
Definition: split.hh:249
weightset_t_of< ratexpset_t > weightset_t
Definition: split.hh:87
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
typename ratexpset_t::const_visitor super_t
Definition: split.hh:93
polynomial_t product(const polynomial_t &l, const ratexp_t &r)
The split-product of l with r.
Definition: split.hh:161
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
An inner node implementing a weight.
Definition: fwd.hh:145
typename ratexp_polynomialset_t< RatExpSet >::value_t ratexp_polynomial_t
Type of polynomials of ratexps from the RatExpSet type.
Definition: split.hh:28
std::map< label_t, weight_t, vcsn::less< labelset_t >> value_t
label_t_of< context_t > label_t
Definition: split.hh:85
VCSN_RAT_VISIT(conjunction, e)
Handle an n-ary conjunction.
Definition: split.hh:213
value_t & del_weight(value_t &v, const label_t &w) const
Remove the monomial of w in v.
context_t_of< ratexpset_t > context_t
Definition: split.hh:83
polynomialset_t ps_
Definition: split.hh:247
polynomial_t split(const ratexp_t &v)
Easy recursion.
Definition: split.hh:108
typename weightset_t::value_t weight_t
Definition: split.hh:88
rat::ratexp_polynomial_t< RatExpSet > split(const RatExpSet &rs, const typename RatExpSet::value_t &e)
Split a ratexp.
Definition: split.hh:257
const weight_t get_weight(const value_t &v, const label_t &w) const ATTRIBUTE_PURE
polynomial split(const ratexp &exp)
Bridge.
Definition: split.hh:270
std::shared_ptr< const detail::polynomial_base > polynomial
Definition: fwd.hh:55
static constexpr const char * me()
Definition: split.hh:95
VCSN_RAT_VISIT(rweight, e)
Definition: split.hh:237