Vcsn  2.3
Be Rational
split.hh
Go to the documentation of this file.
1 #pragma once
2 
4 #include <vcsn/ctx/fwd.hh>
5 #include <vcsn/dyn/value.hh>
6 #include <vcsn/misc/raise.hh>
8 
9 namespace vcsn
10 {
11 
12  namespace rat
13  {
14  // FIXME: this is a general feature which is useful elsewhere.
15  // E.g., expand.
16 
18  template <typename ExpSet>
20  = polynomialset<context<ExpSet,
22 
24  template <typename ExpSet>
27 
29  template <typename ExpSet>
32  {
33  using context_t = context<ExpSet,
35  return context_t{rs, *rs.weightset()};
36  }
37  }
38 
39 
40  /*---------------------.
41  | split(expression). |
42  `---------------------*/
43 
44  namespace rat
45  {
74  template <typename ExpSet>
76  : public ExpSet::const_visitor
77  {
78  public:
79  using expressionset_t = ExpSet;
83  using expression_t = typename expressionset_t::value_t;
85  using weight_t = typename weightset_t::value_t;
86 
89 
90  using super_t = typename expressionset_t::const_visitor;
91 
93  constexpr static const char* me() { return "split"; }
94 
96  : rs_(rs)
97  {}
98 
101  {
102  return split(v);
103  }
104 
107  {
108  v->accept(*this);
109  return std::move(res_);
110  }
111 
113  {
114  res_ = ps_.zero();
115  }
116 
118  {
119  res_ = polynomial_t{{rs_.one(), ws_.one()}};
120  }
121 
123  {
124  res_ = polynomial_t{{rs_.atom(e.value()), ws_.one()}};
125  }
126 
128  {
129  polynomial_t res = ps_.zero();
130  for (const auto& v: e)
131  {
132  v->accept(*this);
133  ps_.add_here(res, res_);
134  }
135  res_ = std::move(res);
136  }
137 
143  {
144  // B(l).
145  polynomial_t l_split = split(l);
146  // constant-term(B(l)).
147  weight_t l_split_const = ps_.get_weight(l_split, rs_.one());
148  // proper(B(l)).
149  ps_.del_weight(l_split, rs_.one());
150 
151  // res = proper(B(l)).r + constant-term(B(l))B(r).
152  return ps_.add(ps_.rmul_label(l_split, r),
153  ps_.lweight(l_split_const, split(r)));
154  }
155 
160  {
162  for (const auto& m: l)
163  ps_.add_here(res, ps_.lweight(weight_of(m), product(label_of(m), r)));
164  return res;
165  }
166 
169  {
170  auto res = product(e[0], e[1]);
171  for (unsigned i = 2, n = e.size(); i < n; ++i)
172  res = product(res, e[i]);
173  res_ = std::move(res);
174  }
175 
181  {
182  // B(l).
183  polynomial_t l_split = split(l);
184  // constant-term(B(l)).
185  weight_t l_split_const = ps_.get_weight(l_split, rs_.one());
186  // proper(B(l)).
187  ps_.del_weight(l_split, rs_.one());
188 
189  // res = proper(B(l))&r.
191  for (const auto& e: l_split)
192  ps_.add_here(res, rs_.conjunction(label_of(e), r), weight_of(e));
193  // res += constant-term(B(l))B(r)
194  ps_.add_here(res,
195  ps_.lweight(l_split_const, split(r)));
196  return res;
197  }
198 
203  {
205  for (const auto& m: l)
206  ps_.add_here(res, ps_.lweight(weight_of(m), conjunction(label_of(m), r)));
207  return res;
208  }
209 
212  {
213  auto res = conjunction(e[0], e[1]);
214  for (unsigned i = 2, n = e.size(); i < n; ++i)
215  res = conjunction(res, e[i]);
216  res_ = std::move(res);
217  }
218 
225 
226  using tuple_t = typename super_t::tuple_t;
227  void visit(const tuple_t&, std::true_type) override
228  {
229  raise(me(), ": tuple is not supported");
230  }
231 
233  {
234  res_ = polynomial_t{{e.shared_from_this(), ws_.one()}};
235  }
236 
238  {
239  e.sub()->accept(*this);
240  res_ = ps_.lweight(e.weight(), res_);
241  }
242 
244  {
245  e.sub()->accept(*this);
246  res_ = ps_.rweight(res_, e.weight());
247  }
248 
249  private:
252  weightset_t ws_ = *rs_.weightset();
256  };
257  }
258 
260  template <typename ExpSet>
262  split(const ExpSet& rs, const typename ExpSet::value_t& e)
263  {
265  return split(e);
266  }
267 
268  namespace dyn
269  {
270  namespace detail
271  {
273  template <typename ExpSet>
274  polynomial
275  split(const expression& exp)
276  {
277  const auto& e = exp->as<ExpSet>();
278  const auto& rs = e.valueset();
280  return {ps, vcsn::split<ExpSet>(rs, e.value())};
281  }
282  }
283  }
284 
286  template <typename PolynomialSet>
287  typename PolynomialSet::value_t
288  split_polynomial(const PolynomialSet& ps,
289  const typename PolynomialSet::value_t& p)
290  {
291  using polynomial_t = typename PolynomialSet::value_t;
292  // This is a polynomial of rational expressions.
293  const auto& rs = *ps.labelset();
294  polynomial_t res;
295  for (const auto& m: p)
296  res = ps.add(res, ps.lweight(weight_of(m), split(rs, label_of(m))));
297  return res;
298  }
299 
301  template <typename ExpSet>
302  rat::expression_polynomial_t<ExpSet>
304  {
306  }
307 
308  namespace dyn
309  {
310  namespace detail
311  {
313  template <typename PolynomialSet>
314  polynomial
316  {
317  const auto& p = poly->as<PolynomialSet>();
318  const auto& ps = p.valueset();
319  return {ps, vcsn::split_polynomial<PolynomialSet>(ps, p.value())};
320  }
321  }
322  }
323 } // vcsn::
return v
Definition: multiply.hh:361
typename expressionset_t::const_visitor super_t
Definition: split.hh:90
VCSN_RAT_VISIT(atom, e)
Definition: split.hh:122
VCSN_RAT_VISIT(lweight, e)
Definition: split.hh:237
weightset_mixin< detail::polynomialset_impl< Context, Kind >> polynomialset
Definition: fwd.hh:63
Break a rational expression into a polynomial.
Definition: split.hh:75
VCSN_RAT_VISIT(mul, e)
Handle an n-ary product.
Definition: split.hh:168
STL namespace.
polynomial split(const expression &exp)
Bridge.
Definition: split.hh:275
expressionset_t rs_
Definition: split.hh:250
labelset_t_of< context_t > labelset_t
Definition: split.hh:81
weightset_t ws_
Shorthand to the weightset.
Definition: split.hh:252
VCSN_RAT_VISIT(star, e)
Definition: split.hh:232
polynomial_t product(const polynomial_t &l, const expression_t &r)
The split-product of l with r.
Definition: split.hh:159
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:63
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
Definition: wet.hh:146
polynomialset< context< ExpSet, weightset_t_of< ExpSet >>> expression_polynomialset_t
Type of PolynomialSet of expressions from the ExpSet type.
Definition: split.hh:21
An inner node implementing a weight.
Definition: expression.hh:264
A dyn Value/ValueSet.
Definition: fwd.hh:23
return res
Definition: multiply.hh:398
typename polynomialset_t::value_t polynomial_t
Definition: split.hh:88
polynomial_t res_
The result.
Definition: split.hh:255
VCSN_RAT_VISIT(conjunction, e)
Handle an n-ary conjunction.
Definition: split.hh:211
split_visitor(const expressionset_t &rs)
Definition: split.hh:95
weightset_t_of< expressionset_t > weightset_t
Definition: split.hh:84
polynomial_t conjunction(const expression_t &l, const expression_t &r)
The split-product of l with r.
Definition: split.hh:180
auto rs
Definition: lift.hh:152
PolynomialSet::value_t split_polynomial(const PolynomialSet &ps, const typename PolynomialSet::value_t &p)
Split a polynomial of expressions, given the polynomialset.
Definition: split.hh:288
polynomialset_t ps_
Definition: split.hh:253
label_t_of< context_t > label_t
Definition: split.hh:82
Definition: a-star.hh:8
context_t_of< expressionset_t > context_t
Definition: split.hh:80
polynomial_t operator()(const expression_t &v)
Break an expression into a polynomial.
Definition: split.hh:100
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:62
void visit(const tuple_t &, std::true_type) override
Definition: split.hh:227
polynomial_t conjunction(const polynomial_t &l, const expression_t &r)
The split-product of l with r.
Definition: split.hh:202
value_impl< detail::polynomial_tag > polynomial
Definition: fwd.hh:27
VCSN_RAT_VISIT(rweight, e)
Definition: split.hh:243
typename super_t::tuple_t tuple_t
Definition: split.hh:226
rat::expression_polynomial_t< ExpSet > split(const ExpSet &rs, const typename ExpSet::value_t &e)
Split an expression.
Definition: split.hh:262
polynomial split(const expression &exp)
Break exp.
Definition: split.hh:275
polynomial_t split(const expression_t &v)
Easy recursion.
Definition: split.hh:106
typename expression_polynomialset_t< ExpSet >::value_t expression_polynomial_t
Type of polynomials of expressions from the ExpSet type.
Definition: split.hh:26
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
Definition: wet.hh:154
polynomial_t product(const expression_t &l, const expression_t &r)
The split-product of l with r.
Definition: split.hh:142
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: split.hh:93
auto & as()
Extract wrapped typed value.
Definition: value.hh:53
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
typename expressionset_t::value_t expression_t
Definition: split.hh:83
expression_polynomialset_t< ExpSet > make_expression_polynomialset(const ExpSet &rs)
From a ExpSet to its polynomialset.
Definition: split.hh:31
An inner node with multiple children.
Definition: expression.hh:118
#define VCSN_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:75
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
typename weightset_t::value_t weight_t
Definition: split.hh:85