20 template <
typename ExpSet>
31 using weight_t =
typename weightset_t::value_t;
37 constexpr
static const char*
me() {
return "expansion"; }
42 using polys_t = std::map<label_t, polynomial_t, vcsn::less<labelset_t>>;
89 o <<
"\\mathsf{Expansion}[";
90 rs_.print_set(o, fmt);
95 rs_.print_set(o, fmt);
101 rs_.print_set(o, fmt);
113 std::ostream& o = std::cout,
117 if (!
ws_.is_zero(v.constant) || v.polynomials.empty())
122 ws_.print(v.constant, o, fmt.for_weights());
128 for (
const auto& p: v.polynomials)
135 ls_.print(p.first, o, fmt.for_labels());
139 ps_.print(p.second, o, fmt);
150 return detail::static_if<context_t::has_one()>
151 ([
this](
const auto& ls,
const value_t& x)
153 return !
has(x.polynomials, ls.one());
155 [](
const auto&,
const value_t&)
169 return ps_.is_zero(p.second);
183 auto i = res.polynomials.find(
one);
184 if (i != std::end(res.polynomials))
186 auto j = i->second.find(
rs_.one());
187 if (j != std::end(i->second))
191 if (
ps_.is_zero(i->second))
192 res.polynomials.erase(i);
221 if (!
ws_.is_zero(res.constant))
224 ps_.add_here(res.polynomials[
one],
226 res.constant =
ws_.zero();
249 template <
typename OtherExpSet>
275 return {
ws_.zero(), {{l,
ps_.one()}}};
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);
309 for (
auto& p: res.polynomials)
310 p.second =
ps_.lweight(w, p.second);
320 for (
auto& p: lhs.polynomials)
321 for (
const auto& m: p.second)
322 ps_.add_here(
res.polynomials[p.first],
330 assert(
ws_.is_zero(res.constant));
331 for (
auto& p: res.polynomials)
332 p.second =
ps_.rmul_label(p.second, rhs);
340 for (
auto& p: res.polynomials)
341 for (
auto&& m: p.second)
347 template <
typename Conjunction>
355 template <
typename Conjunction>
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),
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),
390 template <
typename LabelSet = labelset_t,
typename Conjunction>
393 -> std::enable_if_t<detail::is_letterized_t<LabelSet>{},
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));
412 template <
typename LabelSet = labelset_t,
typename Conjunction>
415 -> std::enable_if_t<!detail::is_letterized_t<LabelSet>{},
419 for (
const auto& l: lhs.polynomials)
420 for (
const auto&
r: rhs.polynomials)
423 auto lcp =
ls_.lgcd(l.first,
r.first);
424 if (!
ls_.is_one(lcp))
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],
430 ps_.lmul_label(right,
r.second)));
438 template <
typename Shuffle>
445 for (
const auto& p: lhs_xpn.polynomials)
446 for (
const auto& m: p.second)
447 ps_.add_here(res.polynomials[p.first],
450 for (
const auto& p: rhs_xpn.polynomials)
451 for (
const auto& m: p.second)
452 ps_.add_here(res.polynomials[p.first],
466 return ps_.conjunction(l, r);
481 return rs_.shuffle(l, r);
494 return ps_.infiltrate(l, r);
502 return rs_.infiltrate(l, r);
521 return complement_<detail::is_letterized_t<labelset_t>{}>(
v);
526 template <
bool IsLetterized>
527 std::enable_if_t<!IsLetterized, value_t>
531 ": complement: labelset must be letterized: ",
ls_);
535 template <
bool IsLetterized>
536 std::enable_if_t<IsLetterized, value_t>
541 detail::static_if<labelset_t::has_one()>
542 ([
this](
const auto&
v,
const auto& ls)
545 me(),
": complement: expansion must be normalized: ",
551 detail::static_if<detail::has_generators_mem_fn<labelset_t>{}>
552 ([
this, &
res](
const auto&
v,
const auto& ls)
554 for (
auto l: ls.generators())
556 auto i = v.polynomials.find(l);
558 ps_.complement(i == end(v.polynomials)
559 ?
ps_.zero() : i->second);
574 for (
const auto& p: v.polynomials)
577 *
this,
": cannot transpose an expansion " 578 "with proper firsts: ",
to_string(*
this, v));
579 res.polynomials[p.first] =
ps_.transpose(p.second);
590 auto& res_one =
res.polynomials[
one];
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));
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),
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),
614 rhs.polynomials[
one]);
617 if (
ps_.is_zero(res_one))
618 res.polynomials.erase(
one);
629 for (
const auto& lp: v.polynomials)
630 res.polynomials[lp.first] = {
ps_.determinize(lp.second)};
639 template <
unsigned Tape>
644 template <
unsigned Tape>
648 return {
rs_.template project<Tape>()};
652 template <
typename... Expansions>
657 template <
size_t Tape>
661 auto es =
eset_.template project<Tape>();
664 VCSN_REQUIRE(es.expressionset().weightset()->is_zero(res.constant),
665 es,
": to-expansion: cannot denormalize ",
667 ", need support for label one (the empty label)");
668 return res.polynomials;
672 template <
size_t... Tape>
675 -> std::tuple<typename project_t<Tape>::polys_t...>
677 using res_t = std::tuple<typename project_t<Tape>::polys_t...>;
678 return res_t{denormalize_tape<Tape>(std::get<Tape>(es))...};
684 auto t =
std::tuple<
const Expansions&...>{es...};
685 constexpr
auto indices
695 auto polys =
denormalize(std::forward<Expansions>(es)...);
697 ([&
res,
this](
const auto&... ps)
764 template <
typename... Expansions>
769 auto t = tuple_impl<Expansions...>{*
this};
770 return t.tuple(std::forward<Expansions>(es)...);
774 template <
size_t Tape>
777 auto xs = project<Tape>();
778 const auto& ps = xs.polynomialset();
779 using res_t =
typename decltype(xs)::
value_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));
794 std::false_type)
const 800 std::true_type)
const 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>();
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)))
823 ps_.add_here(res.polynomials[lhs.first],
824 ps_.compose(lhs.second,
825 ps_.lmul_label(
rs_.atom(rhs.first),
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)))
833 ps_.add_here(res.polynomials[rhs.first],
834 ps_.compose(
ps_.lmul_label(
rs_.atom(lhs.first),
843 const auto& ls0 =
ls_.template set<0>();
844 const auto& ls1 =
ls_.template set<1>();
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)))
854 auto l0 =
ls_.tuple(
ls_.template project<0>(lhs.first),
855 ls_.template project<1>(rhs.first));
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),
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()),
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)))
883 auto l0 =
ls_.tuple(
ls_.template project<0>(lhs.first),
884 ls_.template project<1>(rhs.first));
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],
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())));
908 template <
typename Ctx = context_t>
910 -> std::enable_if_t<are_composable<Ctx, Ctx>()
917 constexpr
auto in = 0;
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)),
931 ps_.add_here(
res.polynomials[l],
932 ps_.compose(lm.second, rm.second));
955 bool denorm_ = old_way_ || !!getenv(
"VCSN_DENORM");
959 template <
typename Context>
969 template <
typename Ctx1,
typename Ctx2>
978 return type(
vcsn::join(lhs.expressionset(), rhs.expressionset()));
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
zipped_maps< Dereference, Maps... > zip_maps(Maps &&... maps)
label_t_of< context_t > label_t
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.
expansionset(const expressionset_t &rs)
expressionset_t expressionset_t
std::string type(const automaton &a)
The implementation type of a.
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()
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
auto label_one(const LabelSet &ls) -> typename LabelSet::value_t
Enjoy type inference.
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.
value_t one() const
The one.
labelset_t_of< context_t > labelset_t
expressionset_t rs_
The expressionset used for the expressions.
Print as a parsable type string.
value_t ldivide(value_t lhs, value_t rhs) const
std::integral_constant< bool, B > bool_constant
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
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
auto tuple(Expansions &&... es) const -> value_t
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
void conjunctions_with_one_(value_t &res, const value_t &l, const value_t &r, std::true_type, Conjunction conjunction) const
value_t & normalize_(value_t &res, std::true_type) const
Normalize res.
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
weightset_t_of< expressionset_t > weightset_t
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.
typename weightset_t::value_t weight_t
value_t & denormalize(value_t &res) const
Move the constant to the polynomial associated to one.
An inner node with multiple children.
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
context_t_of< expressionset_t > context_t
value_t atom(const label_t &l) const
A single label.
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
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.
bool is_normal(const value_t &x) const
Whether an expansion is normal.
const context_t & context() const
The context.
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.
Print as is. For instance, don't try to escape labels.
void compose_with_one_new_(value_t &res, const value_t &l, const value_t &r) const
Print as rich UTF-8 text, escaped.
const polynomialset_t & polynomialset() const
The polynomialset.
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.
const expansionset & eset_
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.
typename expressionset_t::value_t expression_t
value_t & rmul_label_here(value_t &res, const expression_t &rhs) const
In place right multiplication by an expression.
std::map< label_t, polynomial_t, vcsn::less< labelset_t > > polys_t
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.
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)
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
std::enable_if_t<!IsLetterized, value_t > complement_(const value_t &) const
Complement on an invalid labelset.
An input/output format for valuesets.
typename polynomialset_t::monomial_t monomial_t
Provide a variadic mul on top of a binary mul(), and one().
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).
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[.
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.
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.
std::string to_string(identities i)
Wrapper around operator<<.
std::ostream & print_set(std::ostream &o, format fmt={}) const
Print this valueset.
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.