17 #ifndef VCSN_ALGORITHMS_DERIVED_TERM_AUTOMATON_HXX
18 # define VCSN_ALGORITHMS_DERIVED_TERM_AUTOMATON_HXX
22 # include <vaucanson/algorithms/internal/build_pattern.hh>
28 # include <vaucanson/misc/usual_macros.hh>
36 std::ostream& operator << (std::ostream& o, const std::list<T>& l)
38 typename std::list<T>::const_iterator i = l.begin();
44 for (i++; i != l.end(); ++i)
51 # define DERIVATES_TRACE_DEBUG(undef, e, l, s) \
54 std::cerr << "Deriv " \
60 std::cerr << s << std::endl; \
61 std::cerr << std::endl; \
66 # define DERIVATES_TRACE_DEBUG(undef, e, l, s)
72 using namespace algorithm_patterns;
76 template <
typename T_auto,
typename S,
typename T>
77 struct DerivativesAlgo :
public IncAutomataConstructor <
78 DerivativesAlgo<T_auto, S, T>,
82 typedef PartialExp<S, T> exp_t;
83 typedef std::list<exp_t> exp_list_t;
84 typedef typename exp_list_t::iterator exp_list_iterator;
85 AUTOMATON_TYPES(T_auto);
86 AUTOMATON_FREEMONOID_TYPES(T_auto);
90 DerivativesAlgo(
const series_set_t& series,
const Element<S, T>& exp):
91 IncAutomataConstructor<DerivativesAlgo, T_auto, PartialExp<S, T> >
92 (series, prat_exp_convert(exp)),
99 DerivativesAlgo(
const series_set_t& series,
100 const std::list<Element<S, T> >& listexp):
101 IncAutomataConstructor<DerivativesAlgo, T_auto, PartialExp<S, T> >
102 (series, prat_exp_convert(listexp)),
107 void on_state(
const PartialExp<S, T>& e)
109 alphabet_t alpha = this->
get()->series().monoid().alphabet();
113 std::pair<semiring_elt_t, bool> c_term = constant_term(e);
117 e.exp_structure().semiring().zero(
SELECT(semiring_elt_value_t)))
118 this->set_final(series_set_elt_t (e.exp_structure(), c_term.first));
122 for (alphabet_iterator a = alpha.begin(); a != alpha.end(); ++a)
124 std::pair<std::list<PartialExp<S, T> >,
bool>
125 s = prat_exp_derivate(e, *a);
128 DERIVATES_TRACE_DEBUG(undefined, e, *a, s.first);
129 for (exp_list_iterator i = s.first.begin(); i != s.first.end(); ++i)
131 PartialExp<S, T> p_exp = *i;
132 series_set_elt_t s_elt (e.exp_structure(),
133 monoid_elt_t(e.exp_structure().monoid(), *a));
134 s_elt = p_exp.begin().semiring_elt() * s_elt;
135 p_exp.begin().semiring_elt() =
136 e.exp_structure().semiring().identity(
SELECT(semiring_elt_value_t));
137 this->link_to(p_exp, s_elt);
145 template<
typename T_auto,
typename S,
typename T>
147 do_derived_term_automaton(
const T_auto& out,
148 const Element<S, T>& kexp)
151 DerivativesAlgo<T_auto, S, T> derivatives_algo(out.series(), exp);
152 derivatives_algo.run();
153 if (derivatives_algo.undefined)
155 delete derivatives_algo.get();
159 return derivatives_algo.get();
162 template<
typename A,
typename T,
typename Exp>
166 BENCH_TASK_SCOPED(
"derived_term_automaton");
172 template<
typename A,
typename T,
typename Exp>
176 A a_structure(kexp.structure());
183 template<
typename T_auto,
typename S,
typename T>
185 do_broken_derived_term_automaton(
const T_auto& out,
186 const Element<S, T>& kexp)
189 KRatExpInitialDerivation< S, T, algebra::DispatchFunction<T> >
191 std::list< Element<S, T> > listexp = matcher.match(exp.value());
192 DerivativesAlgo<T_auto, S, T> derivatives_algo(out.series(),
194 derivatives_algo.run();
195 if (derivatives_algo.undefined)
197 delete derivatives_algo.get();
201 return derivatives_algo.get();
204 template<
typename A,
typename T,
typename Exp>
206 broken_derived_term_automaton(Element<A, T>& out,
const Exp& kexp)
208 Element<A, T>* result = do_broken_derived_term_automaton(out, kexp);
213 template<
typename A,
typename T,
typename Exp>
215 broken_derived_term_automaton(
const Exp& kexp)
217 A a_structure(kexp.structure());
219 Element<A, T>* result = do_broken_derived_term_automaton(out, kexp);
229 #endif // ! VCSN_ALGORITHMS_DERIVED_TERM_AUTOMATON_HXX