17 #ifndef VCSN_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_DERIVATION_HXX
18 # define VCSN_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_DERIVATION_HXX
20 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hh>
22 # include <vaucanson/algebra/implementation/series/krat.hh>
28 void list_fusion_here(std::list<T>& dst, std::list<T>& src)
30 typedef typename std::list<T>::iterator iterator;
31 typedef typename T::series_set_t series_set_t;
32 typedef typename T::series_set_elt_value_t series_set_elt_value_t;
33 typedef typename T::semiring_elt_t::value_t semiring_elt_value_t;
35 src.sort(unweighted_inf<series_set_t, series_set_elt_value_t>);
36 dst.sort(unweighted_inf<series_set_t, series_set_elt_value_t>);
37 dst.merge(src, unweighted_inf<series_set_t, series_set_elt_value_t>);
38 iterator i = dst.begin();
39 while (i != dst.end())
42 if (++next != dst.end() and unweighted_eq(*next, *i))
44 i->begin().semiring_elt() += next->begin().semiring_elt();
46 if (i->begin().semiring_elt() ==
47 i->begin().semiring_elt().structure().zero(
48 SELECT(semiring_elt_value_t)))
59 template <
typename S,
typename T>
60 void list_multiply_here_left(std::list<PartialExp<S, T> >& l,
61 const typename PartialExp<S, T>::semiring_elt_t& w)
63 typedef std::list<PartialExp<S, T> > list_t;
64 for (
typename list_t::iterator i = l.begin(); i != l.end(); ++i)
68 template <
typename S,
typename T>
69 void list_multiply_here_right(std::list<PartialExp<S, T> >& l,
70 const typename PartialExp<S, T>::semiring_elt_t& w)
72 typedef std::list<PartialExp<S, T> > list_t;
73 for (
typename list_t::iterator i = l.begin(); i != l.end(); ++i)
77 template <
typename S,
typename T>
78 void list_insert_here(std::list<PartialExp<S, T> >& l,
79 const typename PartialExp<S,T>::node_t* elm)
81 typedef std::list<PartialExp<S, T> > list_t;
82 for (
typename list_t::iterator i = l.begin(); i != l.end(); ++i)
92 template <
typename Series,
typename T>
94 public rat::ConstNodeVisitor<typename T::monoid_elt_value_t,
95 typename T::semiring_elt_value_t>
99 typedef T series_set_elt_value_t;
100 typedef typename T::node_t node_t;
101 typedef typename std::list<PartialExp<Series, T> > return_type;
103 typedef typename semiring_elt_t::value_t semiring_elt_value_t;
105 typedef typename monoid_elt_t::value_t monoid_elt_value_t;
106 typedef typename monoid_elt_t::set_t monoid_t;
107 typedef typename monoid_t::alphabet_t alphabet_t;
108 typedef typename alphabet_t::letter_t letter_t;
123 void match(
const node_t* node)
137 product(
const node_t* lhs,
const node_t* rhs)
139 std::pair<semiring_elt_t, bool> ret =
140 constant_term(series(series_set_elt_value_t(lhs)));
141 if (ret.second ==
false)
152 list_multiply_here_left(result, ret.first);
157 list_insert_here(result, rhs);
158 list_fusion_here(result, tmp);
162 sum(
const node_t* lhs,
const node_t* rhs)
165 return_type tmp = result;
167 list_fusion_here(result, tmp);
171 star(
const node_t* node)
173 assertion(father_ != NULL);
174 const typename T::node_t* father = father_;
176 std::pair<semiring_elt_t, bool> ret =
177 constant_term(series(series_set_elt_value_t(node)));
178 if ((ret.second ==
false) || (ret.first.starable() ==
false))
185 list_insert_here(result, father);
186 list_multiply_here_left(result, ret.first.star());
190 left_weight(
const semiring_elt_value_t& w,
const node_t* node)
193 list_multiply_here_left(result, semiring_elt_t(w));
197 right_weight(
const semiring_elt_value_t& w,
const node_t* node)
200 list_multiply_here_right(result, semiring_elt_t(w));
204 constant(
const monoid_elt_value_t& m)
206 result = return_type();
209 WARNING(
"PartialExp base is not realtime.");
214 result.push_back(PartialExp<Series, T>(exp_));
220 result = return_type();
226 result = return_type();
236 const typename T::node_t* father_;
244 template <
class Series,
class T,
class Letter>
245 std::pair<std::list<PartialExp<Series, T> >,
bool>
250 visitor.match(exp.
value().base());
251 return std::make_pair(visitor.result, !visitor.undefined);
254 template <
class Series,
class T,
class Letter>
255 std::pair<std::list<PartialExp<Series, T> >,
bool>
256 prat_exp_derivate(
const PartialExp<Series, T>& exp,
259 typedef PartialExp<Series, T> exp_t;
260 typedef std::list<PartialExp<Series, T> > exp_list_t;
261 typedef std::pair<exp_list_t, bool> return_type;
262 typedef typename exp_t::const_iterator const_iterator;
263 typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
264 typedef typename semiring_elt_t::value_t semiring_elt_value_t;
265 typedef T series_set_elt_value_t;
268 const_iterator i = exp.begin();
269 semiring_elt_t w = i.semiring_elt();
274 return std::make_pair(exp_list_t(),
true);
277 PRatExpDerivationVisitor<Series, T> visitor(exp.exp(), a);
278 const typename exp_t::node_t* n = i.node();
282 for (i++; i != exp.end(); ++i)
284 list_insert_here(visitor.result, i.node());
286 list_multiply_here_right(visitor.result, i.semiring_elt());
289 std::pair<semiring_elt_t, bool> cterm =
290 constant_term(Element<Series, T>(exp.exp_structure(), series_set_elt_value_t(n)));
291 if (cterm.second ==
false)
292 return std::make_pair(exp_list_t(),
false);
296 exp.exp_structure().semiring().zero(
SELECT(semiring_elt_value_t)) )
300 new_exp.nodes().pop_front();
301 new_exp.weights().pop_front();
304 return_type ret = prat_exp_derivate(new_exp, a);
307 list_multiply_here_left(ret.first, cterm.first);
310 visitor.undefined = visitor.undefined || !ret.second;
311 list_fusion_here(visitor.result, ret.first);
316 list_multiply_here_left(visitor.result, w);
319 return std::make_pair(visitor.result, !visitor.undefined);
324 #endif // ! VCSN_ALGORITHMS_INTERNAL_PARTIAL_RAT_EXP_DERIVATION_HXX