9 #include <boost/optional.hpp>
10 #include <boost/range/algorithm/equal.hpp>
11 #include <boost/range/algorithm/find_if.hpp>
12 #include <boost/range/algorithm/lexicographical_compare.hpp>
35 # pragma clang diagnostic push
36 # pragma clang diagnostic ignored "-Wunused-value"
38 template <
typename LabelSet>
39 auto label_is_zero(
const LabelSet& ls,
const typename LabelSet::value_t* l)
40 -> decltype(ls.is_zero(l), bool())
42 return ls.is_zero(*l);
46 # pragma clang diagnostic pop
49 template <
typename LabelSet>
53 template <typename LabelSet>
61 template <
typename WeightSet>
71 template <
typename Context, wet_kind_t Kind>
78 template <
typename Context, wet_kind_t Kind>
90 using label_t =
typename labelset_t::value_t;
114 const self_t&
self()
const {
return static_cast<const self_t&
>(*this); }
130 return weightset_t::has_lightening_weights();
226 template <wet_kind_t WetType,
typename WS>
232 for (
const auto& m:
r)
238 template <wet_kind_t WetType,
typename WS>
242 && std::is_same<WS, b>::value),
250 template <wet_kind_t WetType,
typename WS>
254 && std::is_same<WS, f2>::value),
264 return add_here_impl_<value_t::kind, weightset_t>(l,
r);
289 raise(*
this,
": sub_here: invalid arguments: ",
311 for (
const auto& rm: r)
331 template <wet_kind_t WetType>
338 for (
const auto& lm: l)
339 for (
const auto& rm:
r)
348 template <wet_kind_t WetType>
354 return l.set() &
r.set();
362 return mul_impl_<value_t::kind>(l,
r);
371 for (
const auto& m: p)
385 template <
typename Ctx>
386 std::enable_if_t<Ctx::is_lar, value_t>
390 for (
const auto& lm: l)
391 for (
const auto& rm: r)
400 template <
typename Ctx>
401 std::enable_if_t<!Ctx::is_lar, value_t>
405 for (
const auto& p: zip_maps<vcsn::as_tuple>(l, r))
416 return conjunction_impl_<context_t>(l,
r);
425 for (
const auto& lm: l)
426 for (
const auto& rm: r)
435 template <wet_kind_t WetType = value_t::kind>
442 for (
const auto& p: zip_maps<vcsn::as_tuple>(l,
r))
451 template <wet_kind_t WetType = value_t::kind,
typename WS = weightset_t>
455 && std::is_same<WS, b>::value),
458 return l.set().intersects(
r.set());
463 template <wet_kind_t WetType = value_t::kind,
typename WS = weightset_t>
467 && std::is_same<WS, f2>::value),
470 return (l.set() &
r.set()).count() % 2;
477 for (
const auto& m: v)
495 return {{i->first,
weightset()->star(i->second)}};
508 for (
const auto& m: v)
518 for (
const auto& m: v)
530 for (
const auto& m: v)
538 template <
typename Ctx>
545 template <
typename Ctx>
557 for (
const auto& m:
v)
561 static_if<has_rweight_fn<context_t>{}>
562 ([
this, &
res] (
const auto& ls,
const auto& m,
const auto& w)
568 [
this, &
res] (
const auto&,
const auto& m,
const auto& w)
583 for (
const auto& lhs: v)
595 for (
const auto& lhs: l)
605 raise(*
this,
": rdivide: not implemented (",
622 for (
const auto& m: r)
629 template <
typename Ctx = context_t>
630 std::enable_if_t<Ctx::is_lar, value_t&>
633 for (
const auto& lm: l)
634 for (
const auto& rm: r)
641 template <
typename Ctx = context_t>
642 std::enable_if_t<!Ctx::is_lar, value_t&>
646 raise(*
this,
": ldivide: division by zero");
658 raise(*
this,
": ldivide: not implemented (",
705 auto i = begin(lhs), i_end = end(lhs);
706 auto j = begin(rhs), j_end = end(rhs);
708 i != i_end && j != j_end
709 &&
labelset()->equal(i->first, j->first);
714 if (i != i_end || j != j_end)
724 template <
typename WeightSet,
typename Dummy =
void>
735 template <
typename Dummy>
742 for (
const auto& m: v)
751 template <
typename Ctx, wet_kind_t Knd,
typename Dummy>
759 for (
const auto& p: v)
808 template <
typename... Polys>
813 auto res = value_t{};
818 this->weightset()->mul(ms.second...));
820 std::forward<Polys>(vs)...);
825 template <
size_t Tape>
832 template <
size_t Tape>
835 auto ps = project<Tape>();
836 auto res = ps.zero();
837 for (
const auto& m: v)
849 template <
typename Ctx>
856 template <
typename Ctx>
862 template <
typename Ctx = context_t>
865 -> std::enable_if_t<are_composable<Ctx, Ctx>{},
value_t>
868 for (
const auto& lm: l)
869 for (
const auto& rm:
r)
880 template <
typename Ctx = context_t>
883 -> std::enable_if_t<has_compose_fn<Ctx>{},
value_t>
886 for (
const auto& lm: l)
887 for (
const auto& rm: r)
905 for (
const auto& m: v)
941 template <wet_kind_t WetType>
951 template <wet_kind_t WetType>
958 return l.set() == r.set();
965 return equal_impl_<value_t::kind>(l,
r);
990 return weightset_t::is_one(i->second);
1009 return weightset_t::star_status();
1022 template <
typename WS>
1024 conv(
const WS& ws,
const typename WS::value_t&
v)
const
1030 template <
typename C, wet_kind_t K>
1035 const typename C::labelset_t& sls = *sps.labelset();
1036 const typename C::weightset_t& sws = *sps.weightset();
1040 for (
const auto& m: v)
1061 template <wet_kind_t WetType>
1068 return boost::range::lexicographical_compare(l, r,
monomial_less);
1071 template <wet_kind_t WetType>
1078 return l.set() < r.set();
1085 return less_impl_<value_t::kind>(l,
r);
1098 auto label_transpose
1099 = static_if<context_t::is_lar>
1100 ([](
const auto& ls,
const auto& l) {
return ls.transposition(l); },
1101 [](
const auto& ls,
const auto& l) {
return ls.transpose(l); });
1102 for (
const auto& i: v)
1120 template <wet_kind_t WetType>
1128 for (
const auto& m: p)
1133 template <wet_kind_t WetType>
1146 return hash_impl_<value_t::kind>(
v);
1154 eat(is,
"polynomialset<");
1155 auto ctx = Context::make(is);
1166 o <<
"\\mathsf{Poly}[";
1171 o <<
"polynomialset<";
1178 context().print_set(o, fmt);
1193 boost::optional<label_t>
1194 conv_label(std::istream& i, bool weighted, const char sep = '+
') const
1196 int peek = i.peek();
1197 assert(peek != '[
');
1201 if (i.peek() == 'z')
1210 // The label is not \z.
1211 // Check if there is a label that comes. Or rather, check if
1212 // there is something else than EOF or the separator, in which
1213 // case it must be a label.
1215 if (peek == EOF || peek == sep || isspace(peek))
1217 // There is no label. This counts as '$
', the special
1220 // Indeed, that's how we represent the initial and
final
1229 *
this,
": conv: invalid monomial: ",
1231 " (did you mean \\e or \\z?)");
1236 auto pos = i.tellg();
1247 require(i.peek() == EOF || pos < i.tellg(),
1248 *
this,
": invalid implicit empty word before: ", i);
1277 boost::optional<monomial_t>
1280 #define SKIP_SPACES() \
1281 while (isspace(i.peek())) \
1286 if (i.peek() == EOF)
1290 bool weighted = i.peek() ==
langle;
1296 require(l, *
this,
": \\z is invalid for monomials");
1310 conv(std::istream& i,
const char sep =
'+')
const
1313 #define SKIP_SPACES() \
1314 while (isspace(i.peek())) \
1321 bool weighted = i.peek() ==
langle;
1327 if (i.peek() ==
'[')
1332 else if (
auto l =
conv_label(i, weighted, sep))
1334 require(l, *
this,
": \\z is invalid for monomials");
1340 if (i.peek() == sep)
1356 static bool parens = getenv(
"VCSN_PARENS");
1375 const std::string& sep =
" + ")
const
1400 static bool parens = getenv(
"VCSN_PARENS");
1406 weightset()->print(w, out, fmt.for_weights());
1418 const std::string& sep)
const
1421 for (
const auto& m: v)
1435 const std::string& sep)
const
1442 using labels_t = std::vector<label_t>;
1448 for (
const auto& m: v)
1454 std::pair<weight_t, labels_t>,
1456 for (
const auto& p: per_weight)
1458 if (p.second.size() < 3)
1459 for (
auto l: p.second)
1460 per_label[l] = std::make_pair(p.first, labels_t{l});
1474 for (
const auto& p: per_label)
1483 if (1 < p.second.second.size())
1496 template <
typename Ctx = context_t>
1501 const std::string& sep =
" + ")
const
1508 template <
typename Ctx = context_t>
1513 const std::string& sep =
" + ")
const
1516 if (sep ==
" + " || v.size() <= 2)
1532 template <
typename Context,
1533 wet_kind_t Kind = detail::wet_kind<labelset_t_of<Context>,
1551 return {
vcsn::join(ps1.context(), ps2.context())};
1564 return {*ps1.labelset(),
vcsn::join(*ps1.weightset(), ws2)};
auto tuple(Polys &&...vs) const -> value_t
Build a tuple of polynomials: (e.E+f.F)|(g.G+h.H) => eg.
auto mul_impl_(const value_t &l, const value_t &r) const -> std::enable_if_t< WetType==wet_kind_t::bitset, value_t >
The product of polynomials l and r.
constant< type_t::one, Context > one
weight_t normalize_here(value_t &v) const
Normalize v in place: compute the LGCD of the weights, ldivide the monomials with that factor...
value_t ldivide(const monomial_t &l, const value_t &r) const
Left division by a monomial: l \ r.
value_t transpose(const value_t &v) const
Transpose the labels and the weights.
labelset_t_of< context_t > labelset_t
value_t mul(const monomial_t &lhs, const value_t &v) const
Left product by a monomial.
std::ostream & print_without_classes_(const value_t &v, std::ostream &out, format fmt, const std::string &sep) const
Print a polynomial value without classes.
value_t & ldivide_here(const weight_t w, value_t &v) const
Left exterior division.
static ATTRIBUTE_PURE auto less_impl_(const value_t &l, const value_t &r) -> std::enable_if_t< WetType!=wet_kind_t::bitset, bool >
static ATTRIBUTE_PURE size_t hash(const monomial_t &m, size_t res=0)
char eat(std::istream &is, char c)
Check lookahead character and advance.
Print as a parsable type string.
Container::value_type front(const Container &container)
The first member of this Container.
WeightSet::value_t operator()(const value_t &v) const
auto scalar_product(const value_t &l, const value_t &r) const -> std::enable_if_t< WetType!=wet_kind_t::bitset, weight_t >
The sum of the weights of the common labels.
std::ostream & print(const monomial_t &m, std::ostream &out, format fmt={}) const
Print a monomial.
monomial_t mul(const monomial_t &l, const monomial_t &r) const
The product of monomials l and r.
label_t to_label(const value_t &v) const
Convert into a label.
auto compose(const value_t &l, const value_t &r) const -> std::enable_if_t< has_compose_fn< Ctx >
The composition of polynomials l and r when the context features compose.
static constexpr char langle
Left marker for weight in concrete syntax.
value_t & new_weight(value_t &v, const label_t &l, const weight_t w) const
Set the monomial of l in v to weight w.
format for_labels() const
A copy of this format, but to print labels.
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
static ATTRIBUTE_PURE bool equal(const value_t &l, const value_t &r)
Functor to compare Values of ValueSets.
static type join(const polynomialset< Ctx1, Kind1 > &ps1, const WS2 &ws2)
auto mul(const value_t &p, const label_t &l, const weight_t w) const -> value_t
The product of polynomials l and r.
value_t ldivide(const value_t &l, const value_t &r) const
Left division of two polynomials: l \ r.
value_t & rdivide_here(value_t &v, const weight_t w) const
Right exterior division.
value_t normalize(value_t res) const
Normalized v.
static ATTRIBUTE_PURE bool less(const value_t &l, const value_t &r)
const weight_t get_weight(const value_t &v, const label_t &l) const ATTRIBUTE_PURE
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
for(const auto &lm:l) for(const auto &rm return res
typename value_t::value_type monomial_t
A pair
An input/output format for valuesets.
typename context_t::weightset_ptr weightset_ptr
wet_of< context_t, Kind > value_t
weightset_mixin< detail::r_impl > r
decltype(join(std::declval< ValueSets >()...)) join_t
The type of the join of the ValueSets.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
auto factor(const Aut &aut) -> decltype(::vcsn::copy(aut))
value_t rmul_label(const value_t &v, const label_t &rhs) const
Right product.
Provide a variadic mul on top of a binary mul(), and one().
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
typename labelset_t::value_t label_t
Polynomials over labels.
value_t conv(const polynomialset< C, K > &sps, const typename polynomialset< C, K >::value_t &v) const
Convert from another polynomialset to type_t.
static constexpr bool has_lightening_weights()
Linear combination of labels: map labels to weights.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
static ATTRIBUTE_PURE auto equal_impl_(const value_t &l, const value_t &r) -> std::enable_if_t< WetType!=wet_kind_t::bitset, bool >
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
std::enable_if_t< Ctx::is_lar, value_t & > add_ldivide_here(value_t &res, const value_t &l, const value_t &r) const
The left-division of polynomials l and r: res += l \ r.
auto compose(const value_t &l, const value_t &r) const -> std::enable_if_t< are_composable< Ctx, Ctx >
The composition of polynomials l and r when the context is a composable tupleset. ...
auto project(const value_t &v) const
Extract a single tape.
static bool is_one(const value_t &v) ATTRIBUTE_PURE
Whether is the unit polynomial.
weight_t_of< context_t > weight_t
static symbol sname()
The static name.
value_t conv(const WS &ws, const typename WS::value_t &v) const
FIXME: use enable_if to prevent this from being instantiated when WS is a polynomialset.
Request the map implementation.
static ATTRIBUTE_PURE bool monomial_less(const monomial_t &lhs, const monomial_t &rhs)
polynomialset< Context, Kind > make_polynomialset(const Context &context)
static ATTRIBUTE_PURE auto hash_impl_(const value_t &p) -> std::enable_if_t< WetType!=wet_kind_t::bitset, size_t >
wet< label_t_of< Context >, weight_t_of< Context >, Kind, vcsn::less< labelset_t_of< Context >>, vcsn::hash< labelset_t_of< Context >>, vcsn::equal_to< labelset_t_of< Context >>> wet_of
The corresponding wet for a LabelSet -> WeightSet context.
static ATTRIBUTE_PURE auto equal_impl_(const value_t &l, const value_t &r) -> std::enable_if_t< WetType==wet_kind_t::bitset, bool >
static value_t conv(self_t, const value_t &v)
Conversion from (this and) other weightsets.
auto mul(const value_t &l, const value_t &r) const -> value_t
The product of polynomials l and r.
const context_t & context() const
weightset_t_of< context_t > weightset_t
boost::optional< monomial_t > conv_monomial(std::istream &i, const char sep= '+') const
Read a monomial from a stream.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
value_t star(const value_t &v) const
The star of polynomial v.
auto scalar_product(const value_t &l, const value_t &r) const -> std::enable_if_t<(WetType==wet_kind_t::bitset &&std::is_same< WS, f2 >::value), weight_t >
The sum of the weights of the common labels.
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Print as rich UTF-8 text, escaped.
value_t lgcd(const value_t &lhs, const value_t &rhs) const
LGCD between two polynomials.
static constexpr char rangle
Right marker for weight in concrete syntax.
static ATTRIBUTE_PURE auto hash_impl_(const value_t &p) -> std::enable_if_t< WetType==wet_kind_t::bitset, size_t >
A structure that implements the computation of join(V1, V2).
static type join(const polynomialset< Ctx1, Kind1 > &ps1, const polynomialset< Ctx2, Kind2 > &ps2)
std::enable_if_t<!labelset_t_of< Ctx >::is_letterized(), std::ostream & > print_(const value_t &v, std::ostream &out, format fmt={}, const std::string &sep=" + ") const
Print a non-null value for a non letterized labelset.
std::enable_if_t< Ctx::is_lar, value_t > conjunction_impl_(const value_t &l, const value_t &r) const
The conjunction of polynomials l and r.
static constexpr star_status_t star_status()
static ATTRIBUTE_PURE auto less_impl_(const value_t &l, const value_t &r) -> std::enable_if_t< WetType==wet_kind_t::bitset, bool >
auto add_here_impl_(value_t &l, const value_t &r) const -> std::enable_if_t< WetType!=wet_kind_t::bitset, value_t & >
v += p, default case.
std::enable_if_t< labelset_t_of< Ctx >::is_letterized(), std::ostream & > print_(const value_t &v, std::ostream &out, format fmt={}, const std::string &sep=" + ") const
Print a non-null value for a letterized labelset (e.g., letterset or nullableset. ...
value_t & set_weight(value_t &v, const label_t &l, const weight_t w) const
Set the monomial of l in v to weight w.
std::enable_if_t<!is_letterized_t< labelset_t_of< Aut > >{}, bool > is_letterized(const Aut &aut)
value_t sub(const value_t &l, const value_t &r) const
The subtraction of polynomials l and r.
std::ostream & print_set(std::ostream &o, format fmt={}) const
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
value_t lmul_label(const label_t &lhs, const value_t &v) const
Left product by a label.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
boost::optional< label_t > conv_label(std::istream &i, bool weighted, const char sep= '+') const
Read a label, if there is one.
value_t & add_here(value_t &v, const label_t &l, const weight_t k) const
v += l.
value_t & del_weight(value_t &v, const label_t &l) const
Remove the monomial of l in v.
std::ostream & print(const value_t &v, std::ostream &out, format fmt={}, const std::string &sep=" + ") const
Print a value (a polynomial).
auto add_here_impl_(value_t &l, const value_t &r) const -> std::enable_if_t<(WetType==wet_kind_t::bitset &&std::is_same< WS, f2 >::value), value_t & >
v += p, F2 and bitsets.
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
auto mul_impl_(const value_t &l, const value_t &r) const -> std::enable_if_t< WetType!=wet_kind_t::bitset, value_t >
The product of polynomials l and r.
weight_t conv_weight(std::istream &i) const
Read a weight, if there is one, bracketed.
value_t complement(const value_t &v) const
Complement this polynomial.
value_t conjunction(const value_t &l, const value_t &r) const
void clear(value_t &v)
Set to zero.
value_t abs(const value_t &v) const
Map all weights to their absolute value.
const weightset_ptr & weightset() const
std::enable_if_t<!Ctx::is_lar, value_t > conjunction_impl_(const value_t &l, const value_t &r) const
The conjunction of polynomials l and r.
value_t & add_here(value_t &l, const value_t &r) const
static ATTRIBUTE_PURE bool monomial_equal(const monomial_t &lhs, const monomial_t &rhs)
bool is_zero(const value_t &v) const
value_t rdivide(const value_t &l, const value_t &r) const
In the general case, normalize by the first (non null) weight.
polynomialset_impl(const context_t &ctx)
std::ostream & print_weight_(const weight_t w, std::ostream &out, format fmt) const
Print a weight.
std::string to_string(direction d)
Conversion to string.
std::ostream & print_with_classes_(const value_t &v, std::ostream &out, format fmt, const std::string &sep) const
Print a polynomial value with classes.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
static self_t make(std::istream &is)
Build from the description in is.
auto conv(const ValueSet &vs, const std::string &str, Args &&...args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
decltype(std::declval< labelset_t_of< Ctx >>().compose(std::declval< label_t_of< Ctx >>(), std::declval< label_t_of< Ctx >>())) compose_t
Detect whether the labelset features compose.
value_t conv(std::istream &i, const char sep= '+') const
Read a polynomial from a stream.
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
monomial_t ldivide(const monomial_t &l, const monomial_t &r) const
Left division between two mononials: l \ r.
typename context_t::labelset_ptr labelset_ptr
ps_t::value_t operator()(const value_t &v) const
static const monomial_t & monomial_one()
The unit monomial.
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
std::enable_if_t<!Ctx::is_lar, value_t & > add_ldivide_here(value_t &res, const value_t &l, const value_t &r) const
The left-division of polynomials l and r: res += l \ r.
const labelset_ptr & labelset() const
void hash_combine(std::size_t &seed, const T &v)
void cross(Fun f)
Variadic Cartesian product of containers.
auto add_here_impl_(value_t &l, const value_t &r) const -> std::enable_if_t<(WetType==wet_kind_t::bitset &&std::is_same< WS, b >::value), value_t & >
v += p, B and bitsets.
value_t & sub_here(value_t &v, const monomial_t &m) const
v -= m.
auto label_is_zero(const LabelSet &ls, const typename LabelSet::value_t *l) -> decltype(ls.is_zero(l), bool())
static constexpr bool show_one()
z::value_t operator()(const value_t &v) const
auto rweight(const value_t &v, const weight_t w) const -> value_t
Right exterior product.
auto norm(const value_t &v) const -> weight_t
The norm: the weight with which we should divide a polynomial to normalize it.
const value_t & zero() const
decltype(std::declval< labelset_t_of< Ctx >>().rweight(std::declval< label_t_of< Ctx >>(), std::declval< weight_t_of< Ctx >>())) rweight_t
Detect whether the labelset features rweight.
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
static const value_t & one()
The unit polynomial.
static ATTRIBUTE_PURE size_t hash(const value_t &v)
value_t infiltrate(const value_t &l, const value_t &r) const
The infiltration of polynomials l and r.
Request the bitset implementation (bool weights).
polynomialset_impl()=delete
value_t mul(const value_t &l, const monomial_t &rhs) const
Right product by a monomial.
std::ostream & print_label_class(const LabelSet &ls, const std::vector< typename LabelSet::value_t > &letters, std::ostream &out, format fmt)
Print a set of labels (letterized) with classes.
monomial_t determinize(value_t v) const
"Determinize" this polynomial: turn into a monomial.
wet_kind_t
Different implementations of wets.
value_t value(const label_t &l, const weight_t w) const
Create a polynomial with a single value.
auto project() const
The polynomialset for tape Tape.
value_t lweight(const weight_t w, const value_t &v) const
Left exterior product.
value_t & add_here(value_t &v, const monomial_t &m) const
v += m.
void weight_set(welement< Label, Weight > &m, const Weight &w)
Set the weight of a welement.
auto scalar_product(const value_t &l, const value_t &r) const -> std::enable_if_t<(WetType==wet_kind_t::bitset &&std::is_same< WS, b >::value), weight_t >
The sum of the weights of the common labels.
static constexpr bool is_commutative()