17 #ifndef AWALI_WEIGHTSET_POLYNOMIALSET_HH
18 # define AWALI_WEIGHTSET_POLYNOMIALSET_HH
23 # include <type_traits>
38 namespace awali {
namespace sttc {
40 # if defined __clang__
41 # pragma clang diagnostic push
42 # pragma clang diagnostic ignored "-Wunused-value"
44 template <
typename LabelSet>
45 auto label_is_zero(
const LabelSet& ls,
const typename LabelSet::value_t* l)
46 -> decltype(ls.is_zero(l),
bool())
48 return ls.is_zero(*l);
51 # if defined __clang__
52 # pragma clang diagnostic pop
55 template <
typename LabelSet>
59 template <typename LabelSet>
67 template <
class Context>
80 using label_t =
typename labelset_t::value_t;
83 using value_t = std::map<label_t, weight_t, internal::less<labelset_t>>;
96 return "polynomialset<" + context_t::sname() +
">";
102 return "polynomialset<" +
context().vname(
full) +
">";
132 for (
const auto& m: p)
141 return add_here(v, p.first, p.second);
160 auto w2 =
weightset()->add(i->second, k);
198 labelset()->concat(i.first, j.first),
236 raise(
sname(),
": star: invalid value: ",
format(*
this, v));
246 for (
const auto& m: v)
270 for (
const auto& m: v)
290 throw std::runtime_error(
"not implemented for polynomials");
304 m.second =
weightset()->ldiv(w, m.second);
313 m.second =
weightset()->rdiv(m.second, w);
320 return l.size() ==
r.size()
368 return weightset_t::star_status();
380 conv_from_this_weightset(
const typename weightset_t::value_t& v)
const
391 template <
typename WS>
393 conv(
const WS& ws,
const typename WS::value_t& v)
const
399 template <
typename C>
405 typename C::labelset_t sls = * sps.
labelset();
406 typename C::weightset_t sws = * sps.
weightset();
409 for (
const auto& m: v)
411 add_here(res, tls.conv(sls, m.first), tws.conv(sws, m.second));
431 return std::lexicographical_compare(lhs.begin(), lhs.end(),
432 rhs.begin(), rhs.end(),
440 for (
const auto& i: v)
448 std::hash_combine(res, labelset_t::hash(m.first));
449 std::hash_combine(res, weightset_t::hash(m.second));
456 for (
const auto& m: v)
458 std::hash_combine(res, labelset_t::hash(m.first));
459 std::hash_combine(res, weightset_t::hash(m.second));
468 eat(is,
"polynomialset<");
469 auto ctx = Context::make(is);
479 o <<
"\\mathsf{Poly}[";
487 o <<
"polynomialset<";
505 conv(std::istream& i,
const char sep =
'+')
const
508 #define SKIP_SPACES() \
509 while (isspace(i.peek())) \
517 bool default_w =
true;
518 if (i.peek() == langle)
529 if (i.peek() ==
'\\')
556 std::streampos p = i.tellg();
564 catch (
const std::domain_error&)
567 if (default_w && p == i.tellg())
568 raise(
sname(),
": conv: invalid value: ",
570 " contains an empty label"
571 " (did you mean \\e or \\z?)");
592 const std::string&
format =
"text")
const
594 static bool parens = getenv(
"AWALI_PARENS");
595 print_weight_(m.second, out,
format);
597 out << (
format ==
"latex" ?
"\\left(" :
"(");
600 out << (
format ==
"latex" ?
"\\right)" :
")");
607 const std::string&
format =
"text",
608 const std::string& sep =
" + ")
const
610 bool latex =
format ==
"latex";
612 out << (latex ?
"\\emptyset" :
"\\z");
614 print_<context_t>(v, out,
format,
615 latex && sep ==
" + " ?
" \\oplus " : sep);
621 const std::string& fmt =
"text")
const
623 std::ostringstream o;
624 print(v, o, fmt, sep);
632 std::ostringstream o;
640 print_weight_(
const weight_t& w, std::ostream& out,
641 const std::string&
format =
"text")
const
643 static bool parens = getenv(
"AWALI_PARENS");
646 out << (
format ==
"latex" ?
"\\langle " : std::string{langle});
648 out << (
format ==
"latex" ?
"\\rangle " : std::string{rangle});
655 print_without_ranges_(
const value_t& v, std::ostream& out,
656 const std::string&
format =
"text",
657 const std::string& sep =
" + ")
const
660 for (
const auto& m: v)
672 print_with_ranges_(
const value_t& v, std::ostream& out,
673 const std::string&
format =
"text",
674 const std::string& sep =
" + ")
const
676 if (sep ==
" + " || v.size() <= 3)
677 return print_without_ranges_(v, out,
format, sep);
680 std::vector<label_t> letters;
683 for (
const auto& m: v)
693 letters.push_back(m.first);
706 for(
const auto &l : letters)
727 for(
auto le = left; le<=last; ++le) {
745 for(
auto le = left; le<=last; ++le) {
756 template <
typename context>
759 print_(
const value_t& v, std::ostream& out,
760 const std::string&
format =
"text",
761 const std::string& sep =
" + ")
const
763 return print_without_ranges_(v, out,
format, sep);
767 template <
typename Ctx>
768 typename std::enable_if<Ctx::is_lal || Ctx::is_lan,
770 print_(
const value_t& v, std::ostream& out,
771 const std::string&
format =
"text",
772 const std::string& sep =
" + ")
const
774 return print_with_ranges_(v, out,
format, sep);
782 constexpr
static char langle =
'<';
784 constexpr
static char rangle =
'>';
788 template <
typename Aut>
789 typename polynomialset<context_t_of<Aut>>::value_t
793 using automaton_t = Aut;
796 using polynomial_t =
typename polynomialset_t::value_t;
799 for (
auto t : aut->outin(s, d))
802 res[aut->label_of(t)] = aut->weight_of(t);
807 template <
typename PLS1,
typename PWS1,
808 typename PLS2,
typename PWS2>
816 return {
join(p1.context(), p2.context())};
819 template <
typename WS1,
820 typename PLS2,
typename PWS2>
828 return ctx_t{* p2.
labelset(),
join(w1, * p2.weightset())};
831 template <
typename PLS1,
typename PWS1,
The semiring of complex numbers.
Definition: c.hh:44
carries the algebraic settings of automata
Definition: context.hh:40
@ is_lal
Definition: context.hh:50
@ is_lan
Definition: context.hh:51
const labelset_ptr & labelset() const
Definition: context.hh:152
Linear combination of labels: map labels to weights.
Definition: polynomialset.hh:69
typename context_t::labelset_ptr labelset_ptr
Definition: polynomialset.hh:77
value_t & add_here(value_t &v, const label_t &l, const weight_t k) const
v += (l, k).
Definition: polynomialset.hh:147
polynomialset(const polynomialset &)=default
static size_t hash(const value_t &v)
Definition: polynomialset.hh:453
static std::string sname()
The static name.
Definition: polynomialset.hh:94
value_t lmul_letter(const label_t &lhs, const value_t &v) const
Left product by a label.
Definition: polynomialset.hh:253
value_t mul(const value_t &l, const value_t &r) const
The product of polynomials l and r.
Definition: polynomialset.hh:192
const monomial_t & monomial_one() const
Definition: polynomialset.hh:334
const value_t & one() const
Definition: polynomialset.hh:327
typename value_t::value_type monomial_t
A pair <label, weight>.
Definition: polynomialset.hh:85
value_t conjunction(const value_t &l, const value_t &r) const
The conjunction of polynomials l and r.
Definition: polynomialset.hh:206
const weightset_ptr & weightset() const
Definition: polynomialset.hh:107
std::map< label_t, weight_t, internal::less< labelset_t > > value_t
Definition: polynomialset.hh:83
Context context_t
Definition: polynomialset.hh:72
labelset_t_of< context_t > labelset_t
Definition: polynomialset.hh:73
static constexpr star_status_t star_status()
Definition: polynomialset.hh:366
bool is_one(const value_t &v) const ATTRIBUTE_PURE
Definition: polynomialset.hh:342
typename labelset_t::value_t label_t
Polynomials over labels.
Definition: polynomialset.hh:80
static value_t rdiv(const value_t &, const value_t &)
Definition: polynomialset.hh:288
value_t & add_here(value_t &v, const monomial_t &p) const
v += m.
Definition: polynomialset.hh:139
static size_t hash(const monomial_t &m)
Definition: polynomialset.hh:445
std::string format(const value_t &v, const std::string &sep=" + ", const std::string &fmt="text") const
Definition: polynomialset.hh:620
value_t & add_here(value_t &v, const value_t &p) const
v += p.
Definition: polynomialset.hh:130
value_t conv(const polynomialset< C > &sps, const typename polynomialset< C >::value_t &v) const
Convert from another polynomialset to type_t.
Definition: polynomialset.hh:401
bool is_zero(const value_t &v) const
Definition: polynomialset.hh:360
value_t & del_weight(value_t &v, const label_t &w) const
Remove the monomial of w in v.
Definition: polynomialset.hh:111
weightset_t_of< context_t > weightset_t
Definition: polynomialset.hh:74
const context_t & context() const
Definition: polynomialset.hh:105
static ATTRIBUTE_PURE bool monomial_less_than(const monomial_t &lhs, const monomial_t &rhs)
Definition: polynomialset.hh:417
value_t add(const value_t &l, const value_t &r) const
The sum of polynomials l and r.
Definition: polynomialset.hh:182
static bool less_than(const value_t &lhs, const value_t &rhs)
Definition: polynomialset.hh:428
polynomialset(const context_t &ctx)
Definition: polynomialset.hh:89
value_t star(const value_t &v) const
The star of polynomial v.
Definition: polynomialset.hh:219
weight_t_of< context_t > weight_t
Definition: polynomialset.hh:81
value_t & set_weight(value_t &v, const label_t &w, const weight_t k) const
Set the monomial of w in v to weight k.
Definition: polynomialset.hh:119
static value_t conv(self_type, value_t v)
Conversion from (this and) other weightsets.
Definition: polynomialset.hh:373
std::ostream & print(const value_t &v, std::ostream &out, const std::string &format="text", const std::string &sep=" + ") const
Print a value (a polynomial).
Definition: polynomialset.hh:606
value_t conv(std::istream &i, const char sep='+') const
Construct from a string.
Definition: polynomialset.hh:505
std::ostream & print(const monomial_t &m, std::ostream &out, const std::string &format="text") const
Print a monomial.
Definition: polynomialset.hh:591
typename context_t::weightset_ptr weightset_ptr
Definition: polynomialset.hh:78
value_t transpose(const value_t &v) const
Definition: polynomialset.hh:437
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.
Definition: polynomialset.hh:393
static constexpr bool show_one()
Definition: polynomialset.hh:365
static value_t ldiv(const value_t &l, const value_t &r)
Definition: polynomialset.hh:294
std::string format(const monomial_t &m) const
Format a monomial.
Definition: polynomialset.hh:630
const labelset_ptr & labelset() const
Definition: polynomialset.hh:106
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: polynomialset.hh:475
static bool equals(const value_t &l, const value_t &r) ATTRIBUTE_PURE
Definition: polynomialset.hh:318
const weight_t get_weight(const value_t &v, const label_t &w) const ATTRIBUTE_PURE
Definition: polynomialset.hh:171
value_t & ldiv_here(const weight_t &w, value_t &v) const
Left exterior division.
Definition: polynomialset.hh:301
const value_t & zero() const
Definition: polynomialset.hh:353
static self_type make(std::istream &is)
Build from the description in is.
Definition: polynomialset.hh:465
value_t & rdiv_here(value_t &v, const weight_t &w) const
Right exterior division.
Definition: polynomialset.hh:310
value_t rmul(const value_t &v, const weight_t &w) const
Right exterior product.
Definition: polynomialset.hh:266
value_t lmul(const weight_t &w, const value_t &v) const
Left exterior product.
Definition: polynomialset.hh:241
value_t rmul_letter(const value_t &v, const label_t &rhs) const
Right product.
Definition: polynomialset.hh:277
std::string vname(bool full=true) const
The dynamic name.
Definition: polynomialset.hh:100
The semiring of floating Numbers.
Definition: r.hh:35
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
bool equal(node_t const *left, node_t const *right, path_t *path=nullptr)
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
RatExpSet::ratexp_t less_than(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: less_than.hh:166
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
auto label_is_zero(const LabelSet &ls, const typename LabelSet::value_t *l) -> decltype(ls.is_zero(l), bool())
Definition: polynomialset.hh:45
decltype(join(std::declval< ValueSets >()...)) join_t
Computation of the join of some value sets.
Definition: context.hh:210
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:445
polynomialset< context_t_of< Aut > >::value_t get_entry(const Aut &aut, state_t s, state_t d)
The entry between two states of an automaton.
Definition: polynomialset.hh:790
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
std::string bracketed(std::istream &i, const char lbracket, const char rbracket)
An narrow-char stream that discards the output.
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21