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();
220 template <wet_kind_t WetType,
typename WS>
226 for (
const auto& m:
r)
232 template <wet_kind_t WetType,
typename WS>
236 && std::is_same<WS, b>::value),
244 template <wet_kind_t WetType,
typename WS>
248 && std::is_same<WS, f2>::value),
258 return add_here_impl<value_t::kind, weightset_t>(l,
r);
283 raise(*
this,
": sub_here: invalid arguments: ",
305 for (
const auto& rm: r)
325 template <wet_kind_t WetType>
332 for (
const auto& lm: l)
333 for (
const auto& rm:
r)
342 template <wet_kind_t WetType>
348 return l.set() &
r.set();
356 return mul_impl<value_t::kind>(l,
r);
365 for (
const auto& m: p)
379 template <
typename Ctx>
380 std::enable_if_t<Ctx::is_lar, value_t>
384 for (
const auto& lm: l)
385 for (
const auto& rm: r)
394 template <
typename Ctx>
395 std::enable_if_t<!Ctx::is_lar, value_t>
399 for (
const auto& p: zip_maps<vcsn::as_tuple>(l, r))
410 return conjunction_impl<context_t>(l,
r);
419 for (
const auto& lm: l)
420 for (
const auto& rm: r)
429 template <wet_kind_t WetType = value_t::kind>
436 for (
const auto& p: zip_maps<vcsn::as_tuple>(l,
r))
445 template <wet_kind_t WetType = value_t::kind,
typename WS = weightset_t>
449 && std::is_same<WS, b>::value),
452 return l.set().intersects(
r.set());
457 template <wet_kind_t WetType = value_t::kind,
typename WS = weightset_t>
461 && std::is_same<WS, f2>::value),
464 return (l.set() &
r.set()).count() % 2;
471 for (
const auto& m: v)
489 return {{i->first,
weightset()->star(i->second)}};
502 for (
const auto& m: v)
512 for (
const auto& m: v)
524 for (
const auto& m: v)
532 template <
typename Ctx>
539 template <
typename Ctx>
551 for (
const auto& m:
v)
555 static_if<has_rweight_fn<context_t>{}>
556 ([
this, &
res] (
const auto& ls,
const auto& m,
const auto& w)
562 [
this, &
res] (
const auto&,
const auto& m,
const auto& w)
577 for (
const auto& lhs: v)
589 for (
const auto& lhs: l)
599 raise(*
this,
": rdivide: not implemented (",
615 for (
const auto& m: r)
625 raise(*
this,
": ldivide: division by zero");
630 std::cerr <<
"ldivide(";
631 print(l, std::cerr) <<
", ";
632 print(r, std::cerr) <<
"\n";
638 std::cerr <<
"factor = ";
print(
factor, std::cerr) <<
"\n";
642 std::cerr <<
"res = ";
print(res, std::cerr) <<
"\n";
647 std::cerr <<
"rem = ";
print(remainder, std::cerr) <<
"\n";
651 std::cerr <<
"ldivide(";
652 print(l, std::cerr) <<
", ";
653 print(r, std::cerr) <<
") = ";
654 print(res, std::cerr) <<
" rem: ";
655 print(remainder, std::cerr) <<
"\n";
658 raise(*
this,
": ldivide: not implemented (",
696 auto i = begin(lhs), i_end = end(lhs);
697 auto j = begin(rhs), j_end = end(rhs);
699 i != i_end && j != j_end
700 &&
labelset()->equal(i->first, j->first);
705 if (i != i_end || j != j_end)
715 template <
typename WeightSet,
typename Dummy =
void>
726 template <
typename Dummy>
733 for (
const auto& m: v)
742 template <
typename Ctx, wet_kind_t Knd,
typename Dummy>
750 for (
const auto& p: v)
799 template <
typename... Polys>
804 auto res = value_t{};
809 this->weightset()->mul(ms.second...));
811 std::forward<Polys>(vs)...);
816 template <
size_t Tape>
823 template <
size_t Tape>
826 auto ps = project<Tape>();
827 auto res = ps.zero();
828 for (
const auto& m: v)
840 template <
typename Ctx>
847 template <
typename Ctx>
853 template <
typename Ctx = context_t>
856 -> std::enable_if_t<are_composable<Ctx, Ctx>{},
value_t>
859 for (
const auto& lm: l)
860 for (
const auto& rm:
r)
871 template <
typename Ctx = context_t>
874 -> std::enable_if_t<has_compose_fn<Ctx>{},
value_t>
877 for (
const auto& lm: l)
878 for (
const auto& rm: r)
896 for (
const auto& m: v)
932 template <wet_kind_t WetType>
942 template <wet_kind_t WetType>
949 return l.set() == r.set();
956 return equal_impl<value_t::kind>(l,
r);
981 return weightset_t::is_one(i->second);
1000 return weightset_t::star_status();
1013 template <
typename WS>
1015 conv(
const WS& ws,
const typename WS::value_t&
v)
const
1021 template <
typename C, wet_kind_t K>
1026 const typename C::labelset_t& sls = *sps.labelset();
1027 const typename C::weightset_t& sws = *sps.weightset();
1031 for (
const auto& m: v)
1052 template <wet_kind_t WetType>
1059 return boost::range::lexicographical_compare(l, r,
monomial_less);
1062 template <wet_kind_t WetType>
1069 return l.set() < r.set();
1076 return less_impl<value_t::kind>(l,
r);
1084 for (
const auto& i: v)
1102 template <wet_kind_t WetType>
1110 for (
const auto& m: p)
1115 template <wet_kind_t WetType>
1128 return hash_impl<value_t::kind>(
v);
1136 eat(is,
"polynomialset<");
1137 auto ctx = Context::make(is);
1148 o <<
"\\mathsf{Poly}[";
1153 o <<
"polynomialset<";
1160 context().print_set(o, fmt);
1175 boost::optional<label_t>
1176 conv_label(std::istream& i, bool weighted, const char sep = '+
') const
1178 int peek = i.peek();
1179 assert(peek != '[
');
1183 if (i.peek() == 'z')
1192 // The label is not \z.
1193 // Check if there is a label that comes. Or rather, check if
1194 // there is something else than EOF or the separator, in which
1195 // case it must be a label.
1197 if (peek == EOF || peek == sep || isspace(peek))
1199 // There is no label. This counts as '$
', the special
1202 // Indeed, that's how we represent the initial and
final
1211 *
this,
": conv: invalid monomial: ",
1213 " (did you mean \\e or \\z?)");
1218 auto pos = i.tellg();
1229 require(i.peek() == EOF || pos < i.tellg(),
1230 *
this,
": invalid implicit empty word before: ", i);
1259 boost::optional<monomial_t>
1262 #define SKIP_SPACES() \
1263 while (isspace(i.peek())) \
1268 if (i.peek() == EOF)
1272 bool weighted = i.peek() ==
langle;
1278 require(l, *
this,
": \\z is invalid for monomials");
1292 conv(std::istream& i,
const char sep =
'+')
const
1295 #define SKIP_SPACES() \
1296 while (isspace(i.peek())) \
1303 bool weighted = i.peek() ==
langle;
1309 if (i.peek() ==
'[')
1314 else if (
auto l =
conv_label(i, weighted, sep))
1316 require(l, *
this,
": \\z is invalid for monomials");
1322 if (i.peek() == sep)
1338 static bool parens = getenv(
"VCSN_PARENS");
1357 const std::string& sep =
" + ")
const
1382 static bool parens = getenv(
"VCSN_PARENS");
1388 weightset()->print(w, out, fmt.for_weights());
1400 const std::string& sep)
const
1403 for (
const auto& m: v)
1417 const std::string& sep)
const
1424 using labels_t = std::vector<label_t>;
1430 for (
const auto& m: v)
1436 std::pair<weight_t, labels_t>,
1438 for (
const auto& p: per_weight)
1440 if (p.second.size() < 3)
1441 for (
auto l: p.second)
1442 per_label[l] = std::make_pair(p.first, labels_t{l});
1456 for (
const auto& p: per_label)
1465 if (1 < p.second.second.size())
1478 template <
typename Ctx = context_t>
1483 const std::string& sep =
" + ")
const
1490 template <
typename Ctx = context_t>
1495 const std::string& sep =
" + ")
const
1498 if (sep ==
" + " || v.size() <= 2)
1514 template <
typename Context,
1515 wet_kind_t Kind = detail::wet_kind<labelset_t_of<Context>,
1533 return {
vcsn::join(ps1.context(), ps2.context())};
1546 return {*ps1.labelset(),
vcsn::join(*ps1.weightset(), ws2)};
std::ostream & print(const value_t &v, std::ostream &out, format fmt={}, const std::string &sep=" + ") const
Print a value (a polynomial).
auto mul(const value_t &l, const value_t &r) const -> value_t
The product of polynomials l and r.
static ATTRIBUTE_PURE auto less_impl(const value_t &l, const value_t &r) -> std::enable_if_t< WetType!=wet_kind_t::bitset, bool >
const context_t & context() const
boost::optional< monomial_t > conv_monomial(std::istream &i, const char sep= '+') const
Read a monomial from a stream.
weight_t conv_weight(std::istream &i) const
Read a weight, if there is one, bracketed.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
value_t conjunction(const value_t &l, const value_t &r) const
void clear(value_t &v)
Set to zero.
auto factor(const Aut &aut) -> decltype(::vcsn::copy(aut))
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.
value_t abs(const value_t &v) const
Map all weights to their absolute value.
format for_labels() const
A copy of this format, but to print labels.
value_t lgcd(const value_t &lhs, const value_t &rhs) const
LGCD between two polynomials.
static ATTRIBUTE_PURE bool monomial_equal(const monomial_t &lhs, const monomial_t &rhs)
static type join(const polynomialset< Ctx1, Kind1 > &ps1, const WS2 &ws2)
bool is_zero(const value_t &v) const
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.
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.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
static constexpr star_status_t star_status()
static ATTRIBUTE_PURE auto hash_impl(const value_t &p) -> std::enable_if_t< WetType==wet_kind_t::bitset, size_t >
polynomialset_impl(const context_t &ctx)
value_t sub(const value_t &l, const value_t &r) const
The subtraction of polynomials l and r.
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
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.
WeightSet::value_t operator()(const value_t &v) const
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.
monomial_t ldivide(const monomial_t &l, const monomial_t &r) const
typename context_t::labelset_ptr labelset_ptr
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
static const monomial_t & monomial_one()
The unit monomial.
std::enable_if_t<!is_letterized_t< labelset_t_of< Aut > >{}, bool > is_letterized(const Aut &aut)
ATTRIBUTE_NORETURN void raise_not_starrable(const WeightSet &ws, const typename WeightSet::value_t &w)
This value is not starrable.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
value_t complement(const value_t &v) const
Complement this polynomial.
const labelset_ptr & labelset() const
ps_t::value_t operator()(const value_t &v) const
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, value_t & >
v += p, default case.
const weightset_ptr & weightset() const
value_t & sub_here(value_t &v, const monomial_t &m) const
v -= m.
value_t & add_here(value_t &l, const value_t &r) const
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.
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Request the bitset implementation (bool weights).
A structure that implements the computation of join(V1, V2).
value_t rdivide(const value_t &l, const value_t &r) const
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
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
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.
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.
Print as a parsable type string.
std::ostream & print_weight_(const weight_t w, std::ostream &out, format fmt) const
Print a weight.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
polynomialset_impl()=delete
value_t mul(const value_t &l, const monomial_t &rhs) const
Right product by a monomial.
z::value_t operator()(const value_t &v) const
decltype(join(std::declval< ValueSets >()...)) join_t
The type of the join of the ValueSets.
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.
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.
static self_t make(std::istream &is)
Build from the description in is.
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.
std::string bracketed(std::istream &i, char lbracket, char rbracket)
Extract the string which is here between lbracket and rbracket.
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.
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()
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 tuple(Polys &&...vs) const -> value_t
Build a tuple of polynomials: (e.E+f.F)|(g.G+h.H) => eg.
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
constant< type_t::one, Context > one
static type join(const polynomialset< Ctx1, Kind1 > &ps1, const polynomialset< Ctx2, Kind2 > &ps2)
wet_kind_t
Different implementations of wets.
labelset_t_of< context_t > labelset_t
static constexpr bool show_one()
auto rweight(const value_t &v, const weight_t w) const -> value_t
Right exterior product.
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.
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.
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.
static ATTRIBUTE_PURE auto hash_impl(const value_t &p) -> std::enable_if_t< WetType!=wet_kind_t::bitset, size_t >
monomial_t determinize(value_t v) const
"Determinize" this polynomial: turn into a monomial.
static ATTRIBUTE_PURE bool equal(const value_t &l, const value_t &r)
Linear combination of labels: map labels to weights.
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
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 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.
An input/output format for valuesets.
value_t transpose(const value_t &v) const
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 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 bool less(const value_t &l, const value_t &r)
static ATTRIBUTE_PURE size_t hash(const monomial_t &m, size_t res=0)
const weight_t get_weight(const value_t &v, const label_t &l) const ATTRIBUTE_PURE
Request the map implementation.
for(const auto &lm:l) for(const auto &rm return res
Functor to compare Values of ValueSets.
wet_of< context_t, Kind > value_t
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.
static constexpr char langle
Left marker for weight in concrete syntax.
std::string to_string(direction d)
Conversion to string.
value_t rmul_label(const value_t &v, const label_t &rhs) const
Right product.
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 k.
void weight_set(welement< Label, Weight > &m, const Weight &w)
Set the weight of a welement.
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.
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 ATTRIBUTE_PURE auto equal_impl(const value_t &l, const value_t &r) -> std::enable_if_t< WetType!=wet_kind_t::bitset, bool >
typename labelset_t::value_t label_t
Polynomials over labels.
Print as rich UTF-8 text, escaped.
static constexpr bool has_lightening_weights()
void hash_combine(std::size_t &seed, const T &v)
auto project(const value_t &v) const
Extract a single tape.
weight_t_of< context_t > weight_t
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
polynomialset< Context, Kind > make_polynomialset(const Context &context)
value_t ldivide(const value_t &l, const value_t &r) const
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 value_t conv(self_t, const value_t &v)
Conversion from (this and) other weightsets.
typename value_t::value_type monomial_t
A pair
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
typename context_t::weightset_ptr weightset_ptr
weightset_t_of< context_t > weightset_t
static ATTRIBUTE_PURE auto less_impl(const value_t &l, const value_t &r) -> std::enable_if_t< WetType==wet_kind_t::bitset, bool >
In the general case, normalize by the first (non null) weight.
value_t star(const value_t &v) const
The star of polynomial v.
Provide a variadic mul on top of a binary mul(), and one().
static constexpr char rangle
Right marker for weight in concrete syntax.
value_t conv(const polynomialset< C, K > &sps, const typename polynomialset< C, K >::value_t &v) const
Convert from another polynomialset to type_t.
char eat(std::istream &is, char c)
Check lookahead character and advance.
Container::value_type front(const Container &container)
The first member of this Container.
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.
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 label_is_zero(const LabelSet &ls, const typename LabelSet::value_t *l) -> decltype(ls.is_zero(l), bool())
std::ostream & print_set(std::ostream &o, format fmt={}) const
static bool is_one(const value_t &v) ATTRIBUTE_PURE
Whether is the unit polynomial.
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.
static ATTRIBUTE_PURE bool monomial_less(const monomial_t &lhs, const monomial_t &rhs)
value_t lmul_label(const label_t &lhs, const value_t &v) const
Left product by a label.
weightset_mixin< detail::r_impl > r
value_t & del_weight(value_t &v, const label_t &l) const
Remove the monomial of l in v.