Vcsn  2.5.dev
Be Rational
expansionset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/algos/project.hh>
4 #include <vcsn/algos/split.hh> // expression_polynomialset_t
5 #include <vcsn/ctx/traits.hh>
6 #include <vcsn/misc/algorithm.hh>
7 #include <vcsn/misc/map.hh>
8 #include <vcsn/misc/static-if.hh>
9 
10 namespace vcsn
11 {
12 
13  namespace rat
14  {
15 
16  /*---------------.
17  | expansionset. |
18  `---------------*/
19 
20  template <typename ExpSet>
21  struct expansionset
22  {
23  public:
24  using expressionset_t = ExpSet;
29  using expression_t = typename expressionset_t::value_t;
31  using weight_t = typename weightset_t::value_t;
32 
35  using monomial_t = typename polynomialset_t::monomial_t;
36 
37  constexpr static const char* me() { return "expansion"; }
38 
39  // Keep it sorted to ensure determinism, and better looking
40  // results. Anyway, rough benches show no difference between
41  // map and unordered_map here.
42  using polys_t = std::map<label_t, polynomial_t, vcsn::less<labelset_t>>;
43 
45  struct value_t
46  {
48  // Default value to silence the warnings from
49  // -Wmissing-field-initializers with `value_t{weight}`.
51  };
52 
54  : rs_(rs)
55  {}
56 
58  static symbol sname()
59  {
60  static auto res
61  = symbol{"expansionset<" + expressionset_t::sname() + '>'};
62  return res;
63  }
64 
67  {
68  return rs_;
69  }
70 
73  {
74  return ps_;
75  }
76 
78  const context_t& context() const
79  {
80  return rs_.context();
81  }
82 
84  std::ostream& print_set(std::ostream& o, format fmt = {}) const
85  {
86  switch (fmt.kind())
87  {
88  case format::latex:
89  o << "\\mathsf{Expansion}[";
90  rs_.print_set(o, fmt);
91  o << ']';
92  break;
93  case format::sname:
94  o << "expansionset<";
95  rs_.print_set(o, fmt);
96  o << ">";
97  break;
98  case format::text:
99  case format::utf8:
100  o << "Expansion[";
101  rs_.print_set(o, fmt);
102  o << ']';
103  break;
104  case format::raw:
105  assert(0);
106  break;
107  }
108  return o;
109  }
110 
112  std::ostream& print(const value_t& v,
113  std::ostream& o = std::cout,
114  format fmt = {}) const
115  {
116  bool first = true;
117  if (!ws_.is_zero(v.constant) || v.polynomials.empty())
118  {
119  o << (fmt == format::latex ? "\\left\\langle "
120  : fmt == format::utf8 ? "⟨"
121  : "<");
122  ws_.print(v.constant, o, fmt.for_weights());
123  o << (fmt == format::latex ? "\\right\\rangle "
124  : fmt == format::utf8 ? "⟩"
125  : ">");
126  first = false;
127  }
128  for (const auto& p: v.polynomials)
129  {
130  if (!first)
131  o << (fmt == format::latex ? " \\oplus "
132  : fmt == format::utf8 ? " ⊕ "
133  : " + ");
134  first = false;
135  ls_.print(p.first, o, fmt.for_labels());
136  o << (fmt == format::latex ? " \\odot \\left["
137  : fmt == format::utf8 ? "⊙["
138  : ".[");
139  ps_.print(p.second, o, fmt);
140  o << (fmt == format::latex ? "\\right]"
141  : fmt == format::utf8 ? "]"
142  : "]");
143  }
144  return o;
145  }
146 
148  bool is_normal(const value_t& x) const
149  {
150  return detail::static_if<context_t::has_one()>
151  ([this](const auto& ls, const value_t& x)
152  {
153  return !has(x.polynomials, ls.one());
154  },
155  [](const auto&, const value_t&)
156  {
157  return true;
158  })
159  (ls_, x);
160  }
161 
165  {
166  detail::erase_if(res.polynomials,
167  [this](auto& p)
168  {
169  return ps_.is_zero(p.second);
170  });
172  return normalize_(res, has_one);
173  }
174 
180  value_t& normalize_(value_t& res, std::true_type) const
181  {
182  auto one = ls_.one();
183  auto i = res.polynomials.find(one);
184  if (i != std::end(res.polynomials))
185  {
186  auto j = i->second.find(rs_.one());
187  if (j != std::end(i->second))
188  {
189  res.constant = ws_.add(res.constant, weight_of(*j));
190  i->second.erase(j);
191  if (ps_.is_zero(i->second))
192  res.polynomials.erase(i);
193  }
194  }
195  return res;
196  }
197 
199  value_t& normalize_(value_t& res, std::false_type) const
200  {
201  return res;
202  }
203 
212  {
214  return denormalize_(res, has_one);
215  }
216 
219  value_t& denormalize_(value_t& res, std::true_type) const
220  {
221  if (!ws_.is_zero(res.constant))
222  {
223  auto one = ls_.one();
224  ps_.add_here(res.polynomials[one],
225  polynomial_t{{rs_.one(), res.constant}});
226  res.constant = ws_.zero();
227  }
228  return res;
229  }
230 
232  value_t& denormalize_(value_t& res, std::false_type) const
233  {
234  return res;
235  }
236 
237  /*--------.
238  | Conv. |
239  `--------*/
240 
242  static value_t
243  conv(self_t, const value_t& v)
244  {
245  return v;
246  }
247 
249  template <typename OtherExpSet>
250  value_t
252  const typename expansionset<OtherExpSet>::value_t& v) const
253  {
254  const auto& other_ws = other.expressionset().weightset();
255  const auto& other_ps = other.polynomialset();
256  return {ws_.conv(other_ws, v.constant),
257  ps_.conv(other_ps, v.polynomials)};
258  }
259 
261  value_t zero() const
262  {
263  return {ws_.zero()};
264  }
265 
267  value_t one() const
268  {
269  return {ws_.one()};
270  }
271 
273  value_t atom(const label_t& l) const
274  {
275  return {ws_.zero(), {{l, ps_.one()}}};
276  }
277 
279  void add_here(value_t& lhs, const value_t& rhs) const
280  {
281  lhs.constant = ws_.add(lhs.constant, rhs.constant);
282  for (const auto& p: rhs.polynomials)
283  ps_.add_here(lhs.polynomials[p.first], p.second);
284  normalize(lhs);
285  }
286 
288  value_t add(value_t res, const value_t& rhs) const
289  {
290  add_here(res, rhs);
291  return res;
292  }
293 
295  value_t lweight(const weight_t& w, value_t res) const
296  {
297  lweight_here(w, res);
298  return res;
299  }
300 
303  {
304  if (ws_.is_zero(w))
305  res = zero();
306  else
307  {
308  res.constant = ws_.mul(w, res.constant);
309  for (auto& p: res.polynomials)
310  p.second = ps_.lweight(w, p.second);
311  }
312  return res;
313  }
314 
316  value_t rweight(const value_t& lhs, const weight_t& w) const
317  {
318  auto res = value_t{ws_.mul(lhs.constant, w)};
319  if (!ws_.is_zero(w))
320  for (auto& p: lhs.polynomials)
321  for (const auto& m: p.second)
322  ps_.add_here(res.polynomials[p.first],
323  rs_.rweight(label_of(m), w), weight_of(m));
324  return res;
325  }
326 
329  {
330  assert(ws_.is_zero(res.constant));
331  for (auto& p: res.polynomials)
332  p.second = ps_.rmul_label(p.second, rhs);
333  return res;
334  }
335 
338  {
339  res.constant = ws_.ldivide(w, res.constant);
340  for (auto& p: res.polynomials)
341  for (auto&& m: p.second)
342  weight_set(m, ws_.ldivide(w, weight_of(m)));
343  return normalize(res);
344  }
345 
346  private:
347  template <typename Conjunction>
348  void
350  const value_t&, const value_t&,
351  std::false_type,
352  Conjunction) const
353  {}
354 
355  template <typename Conjunction>
356  void
358  const value_t& l, const value_t& r,
359  std::true_type,
360  Conjunction conjunction) const
361  {
362  // Spontaneous transitions from the lhs.
363  auto one = ls_.one();
364  {
365  auto i = l.polynomials.find(one);
366  if (i != std::end(l.polynomials))
367  for (const auto& rhs: r.polynomials)
368  if (!ls_.is_one(rhs.first))
369  ps_.add_here(res.polynomials[one],
370  conjunction(i->second,
371  ps_.lmul_label(rs_.atom(rhs.first),
372  rhs.second)));
373  }
374  // Spontaneous transitions from the rhs.
375  {
376  auto i = r.polynomials.find(one);
377  if (i != std::end(r.polynomials))
378  for (const auto& lhs: l.polynomials)
379  if (!ls_.is_one(lhs.first))
380  ps_.add_here(res.polynomials[one],
381  conjunction(ps_.lmul_label(rs_.atom(lhs.first),
382  lhs.second),
383  i->second));
384  }
385  }
386 
390  template <typename LabelSet = labelset_t, typename Conjunction>
392  Conjunction conjunction) const
393  -> std::enable_if_t<detail::is_letterized_t<LabelSet>{},
394  value_t>
395  {
396  denormalize(l);
397  denormalize(r);
398  auto res = value_t{ws_.mul(l.constant, r.constant)};
399  for (const auto& p: zip_maps(l.polynomials, r.polynomials))
400  res.polynomials[p.first]
401  = conjunction(std::get<0>(p.second), std::get<1>(p.second));
402 
405  normalize(res);
406  return res;
407  }
408 
412  template <typename LabelSet = labelset_t, typename Conjunction>
413  auto conjunction_(const value_t& lhs, const value_t& rhs,
414  Conjunction conjunction) const
415  -> std::enable_if_t<!detail::is_letterized_t<LabelSet>{},
416  value_t>
417  {
418  auto res = value_t{ws_.mul(lhs.constant, rhs.constant)};
419  for (const auto& l: lhs.polynomials)
420  for (const auto& r: rhs.polynomials)
421  {
422  // The longest common prefix.
423  auto lcp = ls_.lgcd(l.first, r.first);
424  if (!ls_.is_one(lcp))
425  {
426  auto left = rs_.atom(ls_.ldivide(lcp, l.first));
427  auto right = rs_.atom(ls_.ldivide(lcp, r.first));
428  ps_.add_here(res.polynomials[lcp],
429  conjunction(ps_.lmul_label(left, l.second),
430  ps_.lmul_label(right, r.second)));
431  }
432  }
433  normalize(res);
434  return res;
435  }
436 
438  template <typename Shuffle>
440  const value_t& lhs_xpn, const expression_t& lhs_xpr,
441  const value_t& rhs_xpn, const expression_t& rhs_xpr,
442  Shuffle shuffle) const
443  {
444  // (i) lhs_xpn:rhs_xpr.
445  for (const auto& p: lhs_xpn.polynomials)
446  for (const auto& m: p.second)
447  ps_.add_here(res.polynomials[p.first],
448  shuffle(label_of(m), rhs_xpr), weight_of(m));
449  // (ii) lhs_xpr:rhs_xpn
450  for (const auto& p: rhs_xpn.polynomials)
451  for (const auto& m: p.second)
452  ps_.add_here(res.polynomials[p.first],
453  shuffle(lhs_xpr, label_of(m)), weight_of(m));
454  // FIXME: normalize?
455  return res;
456  }
457 
458  public:
460  value_t conjunction(const value_t& l, const value_t& r) const
461  {
462  return conjunction_(l, r,
463  [this](const polynomial_t& l,
464  const polynomial_t& r)
465  {
466  return ps_.conjunction(l, r);
467  });
468  }
469 
473  value_t shuffle(const value_t& de, const expression_t& e,
474  const value_t& df, const expression_t& f) const
475  {
476  auto res = value_t{ws_.mul(de.constant, df.constant)};
477  return shuffle_(res,
478  de, e, df, f,
479  [this](const expression_t& l, const expression_t& r)
480  {
481  return rs_.shuffle(l, r);
482  });
483  }
484 
486  value_t infiltrate(const value_t& de, const expression_t& e,
487  const value_t& df, const expression_t& f) const
488  {
489  // Conjunction part: de&:df.
490  auto res =
491  conjunction_(de, df,
492  [this](const polynomial_t& l, const polynomial_t& r)
493  {
494  return ps_.infiltrate(l, r);
495  });
496 
497  // Shuffle part: de&:f + e&:df.
498  shuffle_(res,
499  de, e, df, f,
500  [this](const expression_t& l, const expression_t& r)
501  {
502  return rs_.infiltrate(l, r);
503  });
504  // FIXME: normalize?
505  return res;
506  }
507 
508  /*--------------.
509  | complement. |
510  `--------------*/
511 
513  value_t complement(const value_t& v) const
514  {
515  // We need an expansion whose firsts are letters. However,
516  // requiring a free labelset, i.e., rulling out lan, is too
517  // demanding, since, for instance, to compute {\} requires lan
518  // instead of lal.
519  //
520  // So require a letterized labelset.
521  return complement_<detail::is_letterized_t<labelset_t>{}>(v);
522  }
523 
524  private:
526  template <bool IsLetterized>
527  std::enable_if_t<!IsLetterized, value_t>
528  complement_(const value_t&) const
529  {
530  raise(me(),
531  ": complement: labelset must be letterized: ", ls_);
532  }
533 
535  template <bool IsLetterized>
536  std::enable_if_t<IsLetterized, value_t>
537  complement_(const value_t& v) const
538  {
539  auto res = value_t{ws_.is_zero(v.constant) ? ws_.one() : ws_.zero()};
540 
541  detail::static_if<labelset_t::has_one()>
542  ([this](const auto& v, const auto& ls)
543  {
544  require(!has(v.polynomials, ls.one()),
545  me(), ": complement: expansion must be normalized: ",
546  to_string(*this, v));
547  })(v, ls_);
548 
549  // Turn the polynomials into expressions, and complement them.
550  // The static-if is made of oneset.
551  detail::static_if<detail::has_generators_mem_fn<labelset_t>{}>
552  ([this, &res](const auto& v, const auto& ls)
553  {
554  for (auto l: ls.generators())
555  {
556  auto i = v.polynomials.find(l);
557  res.polynomials[l] =
558  ps_.complement(i == end(v.polynomials)
559  ? ps_.zero() : i->second);
560  }
561  })(v, ls_);
562  return res;
563  }
564 
565  /*-----------.
566  | ldivide. |
567  `-----------*/
568 
569  public:
571  value_t transpose(const value_t& v) const
572  {
573  auto res = value_t{ws_.transpose(v.constant)};
574  for (const auto& p: v.polynomials)
575  {
576  VCSN_REQUIRE(ls_.is_one(p.first),
577  *this, ": cannot transpose an expansion "
578  "with proper firsts: ", to_string(*this, v));
579  res.polynomials[p.first] = ps_.transpose(p.second);
580  }
581  return res;
582  }
583 
584  value_t ldivide(value_t lhs, value_t rhs) const
585  {
586  denormalize(lhs);
587  denormalize(rhs);
588  auto res = value_t{ws_.mul(lhs.constant, rhs.constant)};
589  auto one = detail::label_one(ls_);
590  auto& res_one = res.polynomials[one];
591 
592  // ε⊙[X_a \ Y_a + ...] for common firsts, included ε.
593  for (const auto& p: zip_maps(lhs.polynomials, rhs.polynomials))
594  ps_.add_ldivide_here(res_one,
595  std::get<0>(p.second),
596  std::get<1>(p.second));
597 
598  // If ε ∈ f(X) then ε⊙[X_ε \ (b Y_b) + ...]
599  if (has(lhs.polynomials, one))
600  for (const auto& rhsp: rhs.polynomials)
601  if (!ls_.is_one(rhsp.first))
602  ps_.add_ldivide_here(res_one,
603  lhs.polynomials[one],
604  ps_.lmul_label(rs_.atom(rhsp.first),
605  rhsp.second));
606 
607  // If ε ∈ f(Y) then ε⊙[(a X_a) \ Y_ε + ...]
608  if (has(rhs.polynomials, one))
609  for (const auto& lhsp: lhs.polynomials)
610  if (!ls_.is_one(lhsp.first))
611  ps_.add_ldivide_here(res_one,
612  ps_.lmul_label(rs_.atom(lhsp.first),
613  lhsp.second),
614  rhs.polynomials[one]);
615 
616  // It was handy to use res_one, but if it's zero, then remove it.
617  if (ps_.is_zero(res_one))
618  res.polynomials.erase(one);
619  normalize(res);
620  return res;
621  }
622 
623  public:
625  value_t
626  determinize(const value_t& v) const
627  {
628  auto res = value_t{v.constant};
629  for (const auto& lp: v.polynomials)
630  res.polynomials[lp.first] = {ps_.determinize(lp.second)};
631  return res;
632  }
633 
634  /*---------------.
635  | tuple(v...). |
636  `---------------*/
637 
639  template <unsigned Tape>
640  using project_t
642 
644  template <unsigned Tape>
645  auto project() const
646  -> project_t<Tape>
647  {
648  return {rs_.template project<Tape>()};
649  }
650 
652  template <typename... Expansions>
653  struct tuple_impl
654  {
657  template <size_t Tape>
658  auto denormalize_tape(const typename project_t<Tape>::value_t& e) const
659  -> typename project_t<Tape>::polys_t
660  {
661  auto es = eset_.template project<Tape>();
662  auto res = e;
663  es.denormalize(res);
664  VCSN_REQUIRE(es.expressionset().weightset()->is_zero(res.constant),
665  es, ": to-expansion: cannot denormalize ",
666  to_string(es, res),
667  ", need support for label one (the empty label)");
668  return res.polynomials;
669  }
670 
672  template <size_t... Tape>
673  auto denormalize(std::tuple<const Expansions&...>& es,
675  -> std::tuple<typename project_t<Tape>::polys_t...>
676  {
677  using res_t = std::tuple<typename project_t<Tape>::polys_t...>;
678  return res_t{denormalize_tape<Tape>(std::get<Tape>(es))...};
679  }
680 
682  auto denormalize(const Expansions&... es) const
683  {
684  auto t = std::tuple<const Expansions&...>{es...};
685  constexpr auto indices
686  = detail::make_index_sequence<sizeof...(Expansions)>{};
687  return denormalize(t, indices);
688  }
689 
690  auto
691  tuple(Expansions&&... es) const
692  -> value_t
693  {
694  auto res = eset_.zero();
695  auto polys = denormalize(std::forward<Expansions>(es)...);
697  ([&res, this](const auto&... ps)
698  {
699  auto l = label_t{ps.first...};
700  eset_.ps_.add_here(res.polynomials[l],
701  eset_.ps_.tuple(ps.second...));
702  },
703  polys);
705  return res;
706  }
707 
709  };
710 
764  template <typename... Expansions>
765  auto
766  tuple(Expansions&&... es) const
767  -> value_t
768  {
769  auto t = tuple_impl<Expansions...>{*this};
770  return t.tuple(std::forward<Expansions>(es)...);
771  }
772 
774  template <size_t Tape>
775  auto project(const value_t& v) const
776  {
777  auto xs = project<Tape>();
778  const auto& ps = xs.polynomialset();
779  using res_t = typename decltype(xs)::value_t;
780  auto res = res_t{};
781  res.constant = v.constant;
782  for (const auto& p: v.polynomials)
783  ps.add_here(res.polynomials[ls_.template project<Tape>(p.first)],
784  ps_.template project<Tape>(p.second));
785  // We might generate denormalized expansions, e.g., when
786  // projecting the expansion of `\e|a`, `\e` is a label.
787  xs.normalize(res);
788  return res;
789  }
790 
791  void
793  const value_t&, const value_t&,
794  std::false_type) const
795  {}
796 
797  void
799  const value_t& l, const value_t& r,
800  std::true_type) const
801  {
802  if (old_way_)
803  return compose_with_one_old_(res, l, r);
804  else
805  return compose_with_one_new_(res, l, r);
806  }
807 
808  void
810  const value_t& l, const value_t& r) const
811  {
812  assert(ws_.is_zero(l.constant));
813  assert(ws_.is_zero(r.constant));
814  const auto& ls0 = ls_.template set<0>();
815  const auto& ls1 = ls_.template set<1>();
816 
817  // Handle lhs labels with one on the second tape.
818  for (const auto& lhs: l.polynomials)
819  if (ls1.is_one(std::get<1>(lhs.first)))
820  for (const auto& rhs: r.polynomials)
821  if (!ls0.is_one(std::get<0>(rhs.first)))
822  // a|\e . [P1] @ b|c . [P2] becomes a|\e . [P1 @ (b|c)P2]
823  ps_.add_here(res.polynomials[lhs.first],
824  ps_.compose(lhs.second,
825  ps_.lmul_label(rs_.atom(rhs.first),
826  rhs.second)));
827  // Handle rhs labels with one on the first tape.
828  for (const auto& rhs: r.polynomials)
829  if (ls0.is_one(std::get<0>(rhs.first)))
830  for (const auto& lhs: l.polynomials)
831  if (!ls1.is_one(std::get<1>(lhs.first)))
832  // a|b . [P1] @ \e|c . [P2] becomes \e|c . [(a|b)P1 @ P2]
833  ps_.add_here(res.polynomials[rhs.first],
834  ps_.compose(ps_.lmul_label(rs_.atom(lhs.first),
835  lhs.second),
836  rhs.second));
837  }
838 
839  void
841  const value_t& l, const value_t& r) const
842  {
843  const auto& ls0 = ls_.template set<0>();
844  const auto& ls1 = ls_.template set<1>();
845 
846  // Handle lhs labels with one on the second tape.
847  for (const auto& lhs: l.polynomials)
848  if (ls1.is_one(std::get<1>(lhs.first)))
849  for (const auto& rhs: r.polynomials)
850  if (!ls0.is_one(std::get<0>(rhs.first)))
851  // a|\e . [P1] @ b|c . [P2] becomes a|c . [P1 @ (b|\e)P2]
852  {
853  // a|c.
854  auto l0 = ls_.tuple(ls_.template project<0>(lhs.first),
855  ls_.template project<1>(rhs.first));
856  // b|\e.
857  auto l1 = ls_.tuple(ls_.template project<0>(rhs.first),
858  ls_.template project<1>(lhs.first));
859  ps_.add_here(res.polynomials[l0],
860  ps_.compose(lhs.second,
861  ps_.lmul_label(rs_.atom(l1),
862  rhs.second)));
863  }
864  // Left constant.
865  //
866  // `$|$ . [<k>1] @ \e|c . [P2]` => `$|c . [(\e|$)<k>1 @ P2]`
867  // `<k> @ \e|c . [P2]` => `\e|c . [<k>1 @ P2]`
868  if (!ws_.is_zero(l.constant))
869  for (const auto& rhs: r.polynomials)
870  if (ls0.is_one(std::get<0>(rhs.first)))
871  ps_.add_here(res.polynomials[rhs.first],
872  ps_.compose(ps_.lweight(l.constant, ps_.one()),
873  rhs.second));
874 
875  // Handle rhs labels with one on the first tape.
876  for (const auto& rhs: r.polynomials)
877  if (ls0.is_one(std::get<0>(rhs.first)))
878  for (const auto& lhs: l.polynomials)
879  if (!ls1.is_one(std::get<1>(lhs.first)))
880  // a|b . [P1] @ \e|c . [P2] becomes a|c . [(\e|b)P1 @ P2]
881  {
882  // a|c.
883  auto l0 = ls_.tuple(ls_.template project<0>(lhs.first),
884  ls_.template project<1>(rhs.first));
885  // \e|b.
886  auto l1 = ls_.tuple(ls_.template project<0>(rhs.first),
887  ls_.template project<1>(lhs.first));
888  ps_.add_here(res.polynomials[l0],
889  ps_.compose(ps_.lmul_label(rs_.atom(l1),
890  lhs.second),
891  rhs.second));
892  }
893 
894  // Right constant.
895  //
896  // `a|\e . [P1] @ $|$ . [<k>1] ` => `a|$ . [P1 @ ($|\e)<k>1]`
897  // `a|\e . [P1] @ <k>` => `a|\e . [P1 @ <k>1]`.
898  if (!ws_.is_zero(r.constant))
899  for (const auto& lhs: l.polynomials)
900  if (ls0.is_one(std::get<1>(lhs.first)))
901  ps_.add_here(res.polynomials[lhs.first],
902  ps_.compose(lhs.second,
903  ps_.lweight(r.constant, ps_.one())));
904  }
905 
906 
908  template <typename Ctx = context_t>
909  auto compose(value_t l, value_t r) const
910  -> std::enable_if_t<are_composable<Ctx, Ctx>()
912  value_t>
913  {
914  // Tape of the lhs on which we compose.
915  constexpr auto out = labelset_t::size() - 1;
916  // Tape of the rhs on which we compose.
917  constexpr auto in = 0;
918  if (denorm_)
919  {
920  denormalize(l);
921  denormalize(r);
922  }
923  auto res = value_t{ws_.mul(l.constant, r.constant)};
924  for (const auto& lm: l.polynomials)
925  for (const auto& rm: r.polynomials)
926  if (ls_.template set<out>().equal(std::get<out>(label_of(lm)),
927  std::get<in>(label_of(rm))))
928  {
929  auto l = ls_.compose(ls_, label_of(lm),
930  ls_, label_of(rm));
931  ps_.add_here(res.polynomials[l],
932  ps_.compose(lm.second, rm.second));
933  }
936  // Beware that we might have introduced some constant terms
937  // (e.g., \e|x @ x|\e), and some polynomials equal to 0 (\e|x @ y|\e).
938  normalize(res);
939  return res;
940  }
941 
942 
943  private:
947  const labelset_t& ls_ = *rs_.labelset();
949  const weightset_t& ws_ = *rs_.weightset();
953  bool old_way_ = !!getenv("VCSN_OLDWAY");
955  bool denorm_ = old_way_ || !!getenv("VCSN_DENORM");
956  };
957  }
958 
959  template <typename Context>
962  {
963  return {es};
964  }
965 
966  namespace detail
967  {
969  template <typename Ctx1, typename Ctx2>
972  {
974 
976  const expansionset<expressionset<Ctx2>>& rhs)
977  {
978  return type(vcsn::join(lhs.expressionset(), rhs.expressionset()));
979  }
980  };
981  }
982 }
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
zipped_maps< Dereference, Maps... > zip_maps(Maps &&... maps)
Definition: zip-maps.hh:250
value_t lweight(const weight_t &w, value_t res) const
Left-multiplication by w of rhs.
auto project(const value_t &v) const
Project a multitape expansion.
auto conjunction_(value_t l, value_t r, Conjunction conjunction) const -> std::enable_if_t< detail::is_letterized_t< LabelSet >
The conjunction of l and r.
const expressionset_t & expressionset() const
The expressionset.
Definition: expansionset.hh:66
expansionset(const expressionset_t &rs)
Definition: expansionset.hh:53
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:238
static type join(const expansionset< expressionset< Ctx1 >> &lhs, const expansionset< expressionset< Ctx2 >> &rhs)
const weightset_t & ws_
Shorthand to the weightset.
static constexpr const char * me()
Definition: expansionset.hh:37
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
auto label_one(const LabelSet &ls) -> typename LabelSet::value_t
Enjoy type inference.
Definition: labelset.hh:54
void compose_with_one_(value_t &res, const value_t &l, const value_t &r, std::true_type) const
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
Definition: wet.hh:146
value_t one() const
The one.
labelset_t_of< context_t > labelset_t
Definition: expansionset.hh:27
expressionset_t rs_
The expressionset used for the expressions.
Print as a parsable type string.
Definition: format.hh:26
value_t ldivide(value_t lhs, value_t rhs) const
std::integral_constant< bool, B > bool_constant
Definition: type_traits.hh:12
value_t & shuffle_(value_t &res, const value_t &lhs_xpn, const expression_t &lhs_xpr, const value_t &rhs_xpn, const expression_t &rhs_xpr, Shuffle shuffle) const
The shuffle product of l and r.
value_t add(value_t res, const value_t &rhs) const
Addition.
typename polynomialset_t::value_t polynomial_t
Definition: expansionset.hh:34
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
auto tuple(Expansions &&... es) const -> value_t
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:102
void conjunctions_with_one_(value_t &res, const value_t &l, const value_t &r, std::true_type, Conjunction conjunction) const
A static list of size_t.
Definition: tuple.hh:32
value_t & normalize_(value_t &res, std::true_type) const
Normalize res.
return v
Definition: multiply.hh:362
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:135
symbol sname()
Definition: name.hh:65
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
Definition: traits.hh:62
weightset_t_of< expressionset_t > weightset_t
Definition: expansionset.hh:30
auto denormalize(const Expansions &... es) const
Entry point: Denormalize all these expansions.
auto denormalize(std::tuple< const Expansions &... > &es, detail::index_sequence< Tape... >) const -> std::tuple< typename project_t< Tape >::polys_t... >
Denormalize on all these tapes.
auto conjunction_(const value_t &lhs, const value_t &rhs, Conjunction conjunction) const -> std::enable_if_t<!detail::is_letterized_t< LabelSet >
The conjunction of l and r.
value_t transpose(const value_t &v) const
Transpose an expansion. The firsts must be reduced to one.
bool denorm_
Denormalize if requested explicitly, or if running the old way.
value_t infiltrate(const value_t &de, const expression_t &e, const value_t &df, const expression_t &f) const
The infiltration product of l and r.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
Definition: setalpha.hh:26
typename weightset_t::value_t weight_t
Definition: expansionset.hh:31
value_t & denormalize(value_t &res) const
Move the constant to the polynomial associated to one.
An inner node with multiple children.
Definition: expression.hh:119
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
Definition: wet.hh:154
context_t_of< expressionset_t > context_t
Definition: expansionset.hh:26
value_t atom(const label_t &l) const
A single label.
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
value_t complement(const value_t &v) const
The complement of v.
value_t & denormalize_(value_t &res, std::false_type) const
Denormalize when there is no label one: identity.
static symbol sname()
The static name.
Definition: expansionset.hh:58
bool is_normal(const value_t &x) const
Whether an expansion is normal.
const context_t & context() const
The context.
Definition: expansionset.hh:78
static value_t conv(self_t, const value_t &v)
Conversion from (this and) other weightsets.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:86
Print as is. For instance, don&#39;t try to escape labels.
Definition: format.hh:24
void compose_with_one_new_(value_t &res, const value_t &l, const value_t &r) const
Print for LaTeX.
Definition: format.hh:22
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
const polynomialset_t & polynomialset() const
The polynomialset.
Definition: expansionset.hh:72
value_t & ldivide_here(const weight_t &w, value_t &res) const
Inplace left-division by w of res.
void conjunctions_with_one_(value_t &, const value_t &, const value_t &, std::false_type, Conjunction) const
void weight_set(welement< Label, Weight > &m, const Weight &w)
Set the weight of a welement.
Definition: wet.hh:162
bool old_way_
Whether to running the old composition code.
value_t shuffle(const value_t &de, const expression_t &e, const value_t &df, const expression_t &f) const
The shuffle product of de and df.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
typename expressionset_t::value_t expression_t
Definition: expansionset.hh:29
value_t & rmul_label_here(value_t &res, const expression_t &rhs) const
In place right multiplication by an expression.
Definition: a-star.hh:8
std::map< label_t, polynomial_t, vcsn::less< labelset_t > > polys_t
Definition: expansionset.hh:42
auto project() const -> project_t< Tape >
The expansionset for tape Tape.
expression_polynomialset_t< ExpSet > make_expression_polynomialset(const ExpSet &rs)
From a ExpSet to its polynomialset.
Definition: split.hh:31
polynomialset_t ps_
The polynomialset for the polynomials.
value_t conv(const expansionset< OtherExpSet > &other, const typename expansionset< OtherExpSet >::value_t &v) const
Convert from another expansionset to self.
void cross_tuple(Fun f, const std::tuple< Ts... > &ts)
Definition: tuple.hh:347
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
std::enable_if_t<!IsLetterized, value_t > complement_(const value_t &) const
Complement on an invalid labelset.
An input/output format for valuesets.
Definition: format.hh:13
typename polynomialset_t::monomial_t monomial_t
Definition: expansionset.hh:35
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
void compose_with_one_(value_t &, const value_t &, const value_t &, std::false_type) const
std::enable_if_t< IsLetterized, value_t > complement_(const value_t &v) const
Complement on a letterized labelset.
value_t conjunction(const value_t &l, const value_t &r) const
The conjunction of l and r.
void add_here(value_t &lhs, const value_t &rhs) const
In place addition.
A structure that implements the computation of join(V1, V2).
Definition: join.hh:18
auto tuple(Expansions &&... es) const -> value_t
The tuplization of single-tape expansions into a multitape expansion.
Build the static sequence of size_t [0, N[.
Definition: tuple.hh:56
value_t & denormalize_(value_t &res, std::true_type) const
Denormalize res move the constant to the polynomial associated to one.
value_t rweight(const value_t &lhs, const weight_t &w) const
Right-multiplication of lhs by w.
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
value_t & normalize(value_t &res) const
Normalize: eliminate null polynomials and move the constant term from the label one.
expansionset< expressionset< Context > > make_expansionset(const expressionset< Context > &es)
Denormalize a pack of one-tape expansions.
const labelset_t & ls_
Shorthand to the labelset.
value_t zero() const
The zero.
void erase_if(Container &c, Predicate p)
In place removal of entries matching the predicate.
Definition: algorithm.hh:55
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:38
std::ostream & print_set(std::ostream &o, format fmt={}) const
Print this valueset.
Definition: expansionset.hh:84
std::ostream & print(const value_t &v, std::ostream &o=std::cout, format fmt={}) const
Print this expansion.
void compose_with_one_old_(value_t &res, const value_t &l, const value_t &r) const
value_t & lweight_here(const weight_t &w, value_t &res) const
Inplace left-multiplication by w of res.
value_t determinize(const value_t &v) const
Turn the polynomials into (normalized) monomials.
auto denormalize_tape(const typename project_t< Tape >::value_t &e) const -> typename project_t< Tape >::polys_t
Denormalize on this tape: from expansion to pure polynomial.
value_t & normalize_(value_t &res, std::false_type) const
Normalize when there is no label one: identity.
auto compose(value_t l, value_t r) const -> std::enable_if_t< are_composable< Ctx, Ctx >() &&number_of_tapes< Ctx >::value==2, value_t >
The composition of l and r.