17 #ifndef AWALI_ALGOS_DERIVATION_HH
18 # define AWALI_ALGOS_DERIVATION_HH
31 # include <unordered_map>
34 # define DEBUG_IFELSE(Then, Else) Then
36 # define DEBUG_IFELSE(Then, Else) Else
39 # define DEBUG_IF(Then) DEBUG_IFELSE(Then,)
41 namespace awali {
namespace sttc {
50 template <
typename RatExpSet>
52 :
public RatExpSet::const_visitor
59 using ratexp_t =
typename ratexpset_t::value_t;
61 using weight_t =
typename weightset_t::value_t;
67 using node_t =
typename super_type::node_t;
69 constexpr
static const char*
me() {
return "derivation"; }
80 return std::move(res_);
88 for (
const auto& m: p)
89 res = rs_.add(res, rs_.lmul(m.second, m.first));
110 if (e.value() == variable_)
121 for (
const auto& v: e)
125 w = ws_.add(w, cst_);
127 res_ = std::move(res);
134 auto res = ps_.
zero();
138 for (
unsigned i = 0,
n = e.size(); i <
n; ++i)
140 const auto& v = e[i];
142 for (
unsigned j = i + 1; j <
n; ++j)
145 w = ws_.mul(w, cst_);
147 res_ = std::move(res);
157 e.sub()->accept(*
this);
159 cst_ = ws_.star(cst_);
160 res_ = ps_.
lmul(cst_,
166 e.sub()->accept(*
this);
167 res_ = ps_.
lmul(e.weight(), res_);
168 cst_ = ws_.mul( e.weight(), cst_);
173 e.sub()->accept(*
this);
175 for (
const auto& m: res_)
176 ps_.
add_here(res, rs_.rmul(m.first, e.weight()), m.second);
178 cst_ = ws_.mul(cst_, e.weight());
196 template <
typename RatExpSet>
198 rat::ratexp_polynomial_t<RatExpSet>
200 const typename RatExpSet::ratexp_t& e,
202 bool breaking =
false)
204 static_assert(RatExpSet::context_t::labelset_t::is_free(),
205 "requires free labelset");
209 res =
split(rs, res);
215 template <
typename RatExpSet>
217 rat::ratexp_polynomial_t<RatExpSet>
221 bool breaking =
false)
226 for (
const auto& m: p)
228 ps.lmul(m.second,
derivation(rs, m.first, a, breaking)));
234 template <
typename RatExpSet>
236 rat::ratexp_polynomial_t<RatExpSet>
238 const typename RatExpSet::ratexp_t& e,
240 bool breaking =
false)
242 auto i = std::begin(word);
243 auto end = std::end(word);
244 require(i != end,
"derivation: word cannot be empty");
246 for (++i; i != end; ++i)
257 template <
typename RatExpSet>
279 , breaking_(breaking)
281 , keep_history_(keep_history)
291 auto i = map_.find(
r);
292 if (i == std::end(map_))
294 DEBUG_IF(std::cerr <<
"New state: "; rs_.print(
r, std::cerr) <<
'\n'; );
295 res = res_->add_state();
312 for (
const auto& p: initial)
314 res_->set_initial(
state(p.first), p.second);
317 const auto& ls = rs_.labelset()->genset();
319 while (!todo_.empty())
326 auto der = derivator(src, l);
328 der =
split(rs_, der);
330 for (
const auto& m: der)
331 res_->add_transition(s,
state(m.first), l, m.second);
335 auto history = std::make_shared<ratexp_history<ratexpset_t>>(rs_);
336 res_->set_history(history);
337 for (
const auto& p: map_)
339 history->add_state(p.second, p.first);
342 return std::move(res_);
349 bool breaking_ =
false;
355 std::stack<ratexp_t> todo_;
362 template <
typename RatExpSet>
364 mutable_automaton<typename RatExpSet::context_t>
366 bool breaking =
false,
bool keep_history =
true)
carries the algebraic settings of automata
Definition: context.hh:40
The semiring of Natural numbers.
Definition: n.hh:34
Linear combination of labels: map labels to weights.
Definition: polynomialset.hh:69
const value_t & one() const
Definition: polynomialset.hh:327
std::map< label_t, weight_t, internal::less< labelset_t > > value_t
Definition: polynomialset.hh:83
value_t & add_here(value_t &v, const value_t &p) const
v += p.
Definition: polynomialset.hh:130
const value_t & zero() const
Definition: polynomialset.hh:353
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
The semiring of floating Numbers.
Definition: r.hh:35
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: derivation.hh:53
typename ratexpset_t::const_visitor super_type
Definition: derivation.hh:66
ratexp_polynomialset_t< ratexpset_t > polynomialset_t
Definition: derivation.hh:63
derivation_visitor(const ratexpset_t &rs)
Definition: derivation.hh:71
AWALI_RAT_VISIT(zero,)
Definition: derivation.hh:96
typename super_type::node_t node_t
Definition: derivation.hh:67
context_t_of< ratexpset_t > context_t
Definition: derivation.hh:56
weightset_t_of< ratexpset_t > weightset_t
Definition: derivation.hh:60
ratexp_t ratexp(const polynomial_t p)
Definition: derivation.hh:85
constexpr static const char * me()
Definition: derivation.hh:69
RatExpSet ratexpset_t
Definition: derivation.hh:55
AWALI_RAT_VISIT(atom, e)
Definition: derivation.hh:108
labelset_t_of< context_t > labelset_t
Definition: derivation.hh:57
AWALI_RAT_VISIT(prod, e)
Definition: derivation.hh:131
typename ratexpset_t::value_t ratexp_t
Definition: derivation.hh:59
typename weightset_t::value_t weight_t
Definition: derivation.hh:61
AWALI_RAT_VISIT(sum, e)
Definition: derivation.hh:117
polynomial_t operator()(const ratexp_t &v, label_t var)
Definition: derivation.hh:76
label_t_of< context_t > label_t
Definition: derivation.hh:58
typename polynomialset_t::value_t polynomial_t
Definition: derivation.hh:64
AWALI_RAT_VISIT(rweight, e)
Definition: derivation.hh:171
AWALI_RAT_VISIT(one,)
Definition: derivation.hh:102
AWALI_RAT_VISIT(lweight, e)
Definition: derivation.hh:164
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:42
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: hash.hh:58
typename ratexp_polynomialset_t< RatExpSet >::value_t ratexp_polynomial_t
Type of polynomials of ratexps from the RatExpSet type.
Definition: split.hh:42
std::shared_ptr< const node< Label, Weight > > ratexp
Definition: fwd.hh:172
ratexp_polynomialset_t< RatExpSet > make_ratexp_polynomialset(const RatExpSet &rs)
From a RatExpSet to its polynomialset.
Definition: split.hh:48
mutable_automaton< typename RatExpSet::context_t > derived_term(const RatExpSet &rs, const typename RatExpSet::ratexp_t &r, bool breaking=false, bool keep_history=true)
Derive a ratexp wrt to a string.
Definition: derivation.hh:365
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
rat::ratexp_polynomial_t< RatExpSet > derivation(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e, label_t_of< RatExpSet > a, bool breaking=false)
Derive a ratexp wrt to a letter.
Definition: derivation.hh:199
SharedPtr make_shared_ptr(Args &&... args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
Definition: memory.hh:29
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
weight_t_of< RatExpSet > constant_term(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e)
Definition: constant_term.hh:155
rat::ratexp_polynomial_t< RatExpSet > split(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e)
Split a ratexp.
Definition: split.hh:243
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
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
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Definition: derivation.hh:259
typename ratexpset_t::value_t ratexp_t
Definition: derivation.hh:261
weightset_t_of< context_t > weightset_t
Definition: derivation.hh:264
mutable_automaton< context_t > automaton_t
Definition: derivation.hh:266
typename polynomialset_t::value_t polynomial_t
Definition: derivation.hh:270
RatExpSet ratexpset_t
Definition: derivation.hh:260
context_t_of< ratexpset_t > context_t
Definition: derivation.hh:263
derived_termer(const ratexpset_t &rs, bool breaking=false, bool keep_history=true)
Definition: derivation.hh:277
std::unordered_map< ratexp_t, state_t, utils::hash< ratexpset_t >, utils::equal_to< ratexpset_t > > smap
Symbolic states to state handlers.
Definition: derivation.hh:275
state_t state(const ratexp_t &r)
The state for ratexp r.
Definition: derivation.hh:286
automaton_t operator()(const ratexp_t &ratexp)
Definition: derivation.hh:304
trait that computes the related types of a labelset
Definition: traits.hh:34
#define DEBUG_IF(Then)
Definition: derivation.hh:39
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73