18 template <
typename ExpSet>
29 using weight_t =
typename weightset_t::value_t;
35 constexpr
static const char*
me() {
return "expansion"; }
40 using polys_t = std::map<label_t, polynomial_t, vcsn::less<labelset_t>>;
85 o <<
"\\mathsf{Expansion}[";
86 rs_.print_set(o, fmt);
91 rs_.print_set(o, fmt);
97 rs_.print_set(o, fmt);
112 if (!
ws_.is_zero(v.constant) || v.polynomials.empty())
117 ws_.print(v.constant, o, fmt.for_weights());
123 for (
const auto& p: v.polynomials)
130 ls_.print(p.first, o, fmt.for_labels());
134 ps_.print(p.second, o, fmt);
157 auto i = res.polynomials.find(
one);
158 if (i != std::end(res.polynomials))
160 auto j = i->second.find(
rs_.one());
161 if (j != std::end(i->second))
165 if (i->second.empty())
166 res.polynomials.erase(i);
189 if (!
ws_.is_zero(res.constant))
192 ps_.add_here(res.polynomials[
one],
194 res.constant =
ws_.zero();
217 template <
typename OtherExpSet>
224 return {ws_.conv(other_ws, v.
constant),
231 return {ws_.zero(),
polys_t{}};
243 return {ws_.zero(), {{l, ps_.one()}}};
247 void add_here(value_t& lhs,
const value_t& rhs)
const
249 lhs.constant = ws_.add(lhs.constant, rhs.constant);
250 for (
const auto& p: rhs.polynomials)
251 ps_.add_here(lhs.polynomials[p.first], p.second);
255 value_t
add(
const value_t& lhs,
const value_t& rhs)
const
273 res.constant = ws_.mul(w, res.constant);
274 for (
auto& p: res.polynomials)
275 p.second = ps_.lweight(w, p.second);
282 value_t
res = {ws_.mul(lhs.constant, w),
polys_t{}};
283 for (
auto& p: lhs.polynomials)
284 for (
const auto& m: p.second)
285 ps_.add_here(res.polynomials[p.first],
293 for (
auto& p: res.polynomials)
294 p.second = ps_.rmul_label(p.second, rhs);
301 res.constant = ws_.ldivide(w, res.constant);
302 for (
auto& p: res.polynomials)
303 for (
auto&& m: p.second)
309 template <
typename Conjunction>
312 const value_t&,
const value_t&,
317 template <
typename Conjunction>
320 const value_t& l,
const value_t&
r,
325 auto one = ls_.one();
327 auto i = l.polynomials.find(
one);
328 if (i != std::end(l.polynomials))
329 for (
const auto& rhs: r.polynomials)
330 if (!ls_.is_one(rhs.first))
331 ps_.add_here(res.polynomials[
one],
332 conjunction(i->second,
333 ps_.lmul_label(rs_.atom(rhs.first),
338 auto i = r.polynomials.find(
one);
339 if (i != std::end(r.polynomials))
340 for (
const auto& lhs: l.polynomials)
341 if (!ls_.is_one(lhs.first))
342 ps_.add_here(res.polynomials[
one],
343 conjunction(ps_.lmul_label(rs_.atom(lhs.first),
352 template <
typename LabelSet = labelset_t,
typename Conjunction>
355 -> std::enable_if_t<detail::is_letterized_t<LabelSet>{},
361 res.constant = ws_.mul(l.constant,
r.constant);
362 for (
const auto& p:
zip_maps(l.polynomials,
r.polynomials))
363 res.polynomials[p.first]
364 =
conjunction(std::get<0>(p.second), std::get<1>(p.second));
367 conjunctions_with_one_(res, l,
r, has_one,
conjunction);
375 template <
typename LabelSet = labelset_t,
typename Conjunction>
378 -> std::enable_if_t<!detail::is_letterized_t<LabelSet>{},
381 value_t res =
zero();
382 res.constant = ws_.mul(lhs.constant, rhs.constant);
383 for (
const auto& l: lhs.polynomials)
384 for (
const auto&
r: rhs.polynomials)
387 auto lcp = ls_.lgcd(l.first,
r.first);
388 if (!ls_.is_one(lcp))
390 auto left = rs_.atom(ls_.ldivide(lcp, l.first));
391 auto right = rs_.atom(ls_.ldivide(lcp,
r.first));
392 ps_.add_here(res.polynomials[lcp],
394 ps_.lmul_label(right,
r.second)));
401 template <
typename Shuffle>
408 for (
const auto& p: lhs_xpn.polynomials)
409 for (
const auto& m: p.second)
410 ps_.add_here(res.polynomials[p.first],
413 for (
const auto& p: rhs_xpn.polynomials)
414 for (
const auto& m: p.second)
415 ps_.add_here(res.polynomials[p.first],
429 return ps_.conjunction(l, r);
440 res.constant = ws_.mul(de.constant, df.constant);
445 return rs_.shuffle(l, r);
458 return ps_.infiltrate(l, r);
466 return rs_.infiltrate(l, r);
484 return complement_<detail::is_letterized_t<labelset_t>{}>(
v);
489 template <
bool IsLetterized>
490 std::enable_if_t<!IsLetterized, value_t>
494 ": complement: labelset must be letterized: ", ls_);
498 template <
bool IsLetterized>
499 std::enable_if_t<IsLetterized, value_t>
503 res.constant = ws_.is_zero(v.constant) ? ws_.one() : ws_.zero();
505 detail::static_if<labelset_t::has_one()>
506 ([
this](
const auto&
v,
const auto& ls)
509 me(),
": complement: expansion must be normalized: ",
515 detail::static_if<detail::has_generators_mem_fn<labelset_t>{}>
516 ([
this, &
res](
const auto&
v,
const auto& ls)
518 for (
auto l: ls.generators())
520 auto i = v.polynomials.find(l);
522 ps_.complement(i == end(v.polynomials)
523 ? ps_.zero() : i->second);
537 value_t res = {ws_.transpose(v.constant),
polys_t{}};
538 for (
const auto& p: v.polynomials)
541 *
this,
": cannot transpose an expansion "
542 "with proper firsts: ",
to_string(*
this, v));
543 res.polynomials[p.first] = ps_.transpose(p.second);
548 value_t
ldivide(value_t lhs, value_t rhs)
const
550 value_t res =
zero();
554 auto& res_one = res.polynomials[
one];
557 for (
const auto& p:
zip_maps(lhs.polynomials, rhs.polynomials))
558 ps_.add_ldivide_here(res_one,
559 std::get<0>(p.second),
560 std::get<1>(p.second));
563 if (
has(lhs.polynomials,
one))
564 for (
const auto& rhsp: rhs.polynomials)
565 if (!ls_.is_one(rhsp.first))
566 ps_.add_ldivide_here(res_one,
567 lhs.polynomials[
one],
568 ps_.lmul_label(rs_.atom(rhsp.first),
572 if (
has(rhs.polynomials,
one))
573 for (
const auto& lhsp: lhs.polynomials)
574 if (!ls_.is_one(lhsp.first))
575 ps_.add_ldivide_here(res_one,
576 ps_.lmul_label(rs_.atom(lhsp.first),
578 rhs.polynomials[
one]);
581 if (ps_.is_zero(res_one))
582 res.polynomials.erase(
one);
593 res.constant = v.constant;
594 for (
const auto& lp: v.polynomials)
595 res.polynomials[lp.first] = {ps_.determinize(lp.second)};
604 template <
unsigned Tape>
609 template <
unsigned Tape>
613 return {rs_.template project<Tape>()};
617 template <
typename... Expansions>
622 template <
size_t Tape>
626 auto es = eset_.template project<Tape>();
629 VCSN_REQUIRE(es.expressionset().weightset()->is_zero(res.constant),
630 es,
": to-expansion: cannot denormalize ",
632 ", need support for label one (the empty label)");
633 return res.polynomials;
637 template <
size_t... Tape>
642 using res_t = std::tuple<typename project_t<Tape>::polys_t...>;
643 return res_t{denormalize_tape<Tape>(std::get<Tape>(es))...};
649 auto t =
std::tuple<
const Expansions&...>{es...};
650 constexpr
auto indices
652 return denormalize(t, indices);
659 auto res = eset_.zero();
660 auto polys = denormalize(std::forward<Expansions>(es)...);
662 ([&res,
this](
const auto&... ps)
665 eset_.ps_.add_here(res.polynomials[l],
666 eset_.ps_.tuple(ps.second...));
669 eset_.normalize(res);
730 template <
typename... Expansions>
735 auto t = tuple_impl<Expansions...>{*
this};
736 return t.tuple(std::forward<Expansions>(es)...);
740 template <
size_t Tape>
743 auto xs = project<Tape>();
744 const auto& ps = xs.polynomialset();
745 using res_t =
typename decltype(xs)::value_t;
747 res.constant = v.constant;
748 for (
const auto& p: v.polynomials)
749 ps.add_here(res.polynomials[ls_.template project<Tape>(p.first)],
750 ps_.template project<Tape>(p.second));
759 const value_t&,
const value_t&,
760 std::false_type)
const
765 const value_t& l,
const value_t&
r,
766 std::true_type)
const
770 for (
const auto& lhs: l.polynomials)
771 if (ls_.template set<1>().is_one(std::get<1>(lhs.first)))
772 for (
const auto& rhs: r.polynomials)
773 if (!ls_.template set<0>().is_one(std::get<0>(rhs.first)))
775 ps_.add_here(res.polynomials[lhs.first],
776 ps_.compose(lhs.second,
777 ps_.lmul_label(rs_.atom(rhs.first),
782 for (
const auto& rhs: r.polynomials)
783 if (ls_.template set<0>().is_one(std::get<0>(rhs.first)))
784 for (
const auto& lhs: l.polynomials)
785 if (!ls_.template set<1>().is_one(std::get<1>(lhs.first)))
787 ps_.add_here(res.polynomials[rhs.first],
788 ps_.compose(ps_.lmul_label(rs_.atom(lhs.first),
795 template <
typename Ctx = context_t>
797 -> std::enable_if_t<are_composable<Ctx, Ctx>{}, value_t>
799 value_t res =
zero();
802 res.constant = ws_.mul(l.constant,
r.constant);
803 for (
const auto& lm: l.polynomials)
804 for (
const auto& rm:
r.polynomials)
805 if (ls_.template set<0>().equal(std::get<1>(
label_of(lm)),
808 auto l = ls_.tuple(std::get<0>(
label_of(lm)),
810 ps_.add_here(res.polynomials[l],
811 ps_.compose(lm.second, rm.second));
814 compose_with_one_(res, l,
r, has_one);
832 template <
typename Context>
842 template <
typename Ctx1,
typename Ctx2>
851 return type(
vcsn::join(lhs.expressionset(), rhs.expressionset()));
value_t & ldivide_here(const weight_t &w, value_t &res) const
Inplace left-division by w of res.
constant< type_t::one, Context > one
polynomialset_t ps_
The polynomialset for the polynomials.
value_t add(const value_t &lhs, const value_t &rhs) const
Addition.
value_t conjunction(const value_t &l, const value_t &r) const
The conjunction of l and r.
auto tuple(Expansions &&...es) const -> value_t
The tuplization of single-tape expansions into a multitape expansion.
Print as is. For instance, don't try to escape labels.
constant< type_t::zero, Context > zero
Print as a parsable type string.
Implementation of nodes of tuple of rational expressions.
value_t lweight(const weight_t &w, const value_t &rhs) const
Left-multiplication by w of rhs.
value_t & normalize_(value_t &res, std::false_type) const
Normalize when there is no label one: identity.
value_t rweight(const value_t &lhs, const weight_t &w) const
Right-multiplication of lhs by w.
expression_polynomialset_t< ExpSet > make_expression_polynomialset(const ExpSet &rs)
From a ExpSet to its polynomialset.
value_t ldivide(value_t lhs, value_t rhs) const
void conjunctions_with_one_(value_t &, const value_t &, const value_t &, std::false_type, Conjunction) const
typename polynomialset_t::value_t polynomial_t
std::ostream & print(const value_t &v, std::ostream &o, format fmt={}) const
Print this expansion.
expansionset< expressionset< Context > > make_expansionset(const expressionset< Context > &es)
variadic< type_t::conjunction, Context > conjunction
Aut & lweight_here(const weight_t_of< Aut > &w, Aut &res, Tag tag={})
In place left-multiplication of an automaton by a weight.
auto compose(value_t l, value_t r) const -> std::enable_if_t< are_composable< Ctx, Ctx >
The composition of l and r.
value_t atom(const label_t &l) const
A single label.
const polynomialset_t & polynomialset() const
The polynomialset.
Aut1 & add_here(Aut1 &res, const Aut2 &b, standard_tag)
Merge transitions of b into those of res.
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
An input/output format for valuesets.
void cross_tuple(Fun f, const std::tuple< Ts... > &ts)
value_t & denormalize_(value_t &res, std::true_type) const
Denormalize res move the constant to the polynomial associated to one.
weightset_t_of< expressionset_t > weightset_t
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.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Provide a variadic mul on top of a binary mul(), and one().
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.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
std::map< label_t, polynomial_t, vcsn::less< labelset_t >> polys_t
typename polynomialset_t::monomial_t monomial_t
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
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.
value_t & denormalize(value_t &res) const
Move the constant to the polynomial associated to one.
value_t & lweight_here(const weight_t &w, value_t &res) const
Inplace left-multiplication by w of res.
static value_t conv(self_t, const value_t &v)
Conversion from (this and) other weightsets.
automaton conjunction_(const std::vector< automaton > &as, bool lazy, vcsn::detail::index_sequence< I... >)
Bridge helper.
auto tuple(Expansions &&...es) const -> value_t
value_t complement(const value_t &v) const
The complement of v.
std::string type(const automaton &a)
The implementation type of a.
void compose_with_one_(value_t &res, const value_t &l, const value_t &r, std::true_type) const
value_t & denormalize_(value_t &res, std::false_type) const
Denormalize when there is no label one: identity.
auto normalize(const Aut &a) -> decltype(copy(a))
Normalize a automaton.
zipped_maps< Dereference, Maps... > zip_maps(Maps &&...maps)
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Print as rich UTF-8 text, escaped.
A structure that implements the computation of join(V1, V2).
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.
typename expressionset_t::value_t expression_t
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.
std::enable_if_t<!IsLetterized, value_t > complement_(const value_t &) const
Complement on an invalid labelset.
Print as plain (ASCII) text, escaped.
auto project() const -> project_t< Tape >
The expansionset for tape Tape.
value_t & normalize_(value_t &res, std::true_type) const
Normalize res.
labelset_t_of< context_t > labelset_t
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.
value_t zero() const
The zero.
auto label_one(const LabelSet &ls) -> typename LabelSet::value_t
Enjoy type inference.
auto project(const value_t &v) const
Project a multitape expansion.
const expressionset_t & expressionset() const
The expressionset.
std::integral_constant< bool, B > bool_constant
value_t transpose(const value_t &v) const
Transpose an expansion. The firsts must be reduced to one.
static type join(const expansionset< expressionset< Ctx1 >> &lhs, const expansionset< expressionset< Ctx2 >> &rhs)
std::ostream & print_set(std::ostream &o, format fmt={}) const
Print this valueset.
const expansionset & eset_
An inner node with multiple children.
label_t_of< context_t > label_t
const labelset_t & ls_
Shorthand to the labelset.
expressionset_t rs_
The expressionset used for the expressions.
value_t & rweight_here(value_t &res, const expression_t &rhs) const
In place right multiplication by an expression.
value_t conv(const expansionset< OtherExpSet > &other, const typename expansionset< OtherExpSet >::value_t &v) const
Convert from another expansionset to self.
expressionset_t expressionset_t
context_t_of< expressionset_t > context_t
void add_here(value_t &lhs, const value_t &rhs) const
In place addition.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
std::enable_if_t< IsLetterized, value_t > complement_(const value_t &v) const
Complement on a letterized labelset.
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.
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
value_t & normalize(value_t &res) const
Normalize: move the constant term to the label one.
expansionset(const expressionset_t &rs)
automaton shuffle_(const std::vector< automaton > &as, vcsn::detail::index_sequence< I... >)
Variadic bridge helper.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
const context_t & context() const
The context.
static constexpr const char * me()
typename weightset_t::value_t weight_t
void compose_with_one_(value_t &, const value_t &, const value_t &, std::false_type) const
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
Denormalize a pack of one-tape expansions.
auto denormalize(const Expansions &...es) const
Entry point: Denormalize all these expansions.
value_t one() const
The one.
const weightset_t & ws_
Shorthand to the weightset.
std::string to_string(identities i)
Wrapper around operator<<.
static symbol sname()
The static name.
value_t determinize(const value_t &v) const
Turn the polynomials into (normalized) monomials.
void conjunctions_with_one_(value_t &res, const value_t &l, const value_t &r, std::true_type, Conjunction conjunction) const
void weight_set(welement< Label, Weight > &m, const Weight &w)
Set the weight of a welement.