18 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_HXX
19 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_HXX
24 # include <vaucanson/algebra/implementation/series/series.hh>
25 # include <vaucanson/algebra/implementation/series/rat/exp.hh>
26 # include <vaucanson/algebra/implementation/series/rat/random_visitor.hh>
27 # include <vaucanson/misc/usual_macros.hh>
29 # include <vaucanson/algebra/implementation/series/krat_exp_is_finite_app.hxx>
30 # include <vaucanson/algebra/implementation/series/krat_exp_support.hxx>
31 # include <vaucanson/algebra/implementation/series/krat_exp_transpose.hxx>
36 # include <vaucanson/algebra/implementation/series/polynoms.hh>
37 # include <vaucanson/automata/concept/automata.hh>
59 template<
typename W,
typename M,
typename Tm,
typename Tw>
62 pure_service_call (
"default version of op_contains(Series<W,M>, exp<Tm,Tw>)");
66 template<
typename W,
typename M,
typename Tm,
typename Tw>
70 vcsn::IsFiniteAppMatcher<
73 algebra::DispatchFunction<vcsn::rat::exp<Tm, Tw> >
75 return matcher.match(m);
78 template<
typename W,
typename M,
typename Tm,
typename Tw>
79 typename algebra::series_traits<rat::exp<Tm, Tw> >::support_t
80 op_support(
const algebra::Series<W, M>& s,
const rat::exp<Tm, Tw>& m)
83 algebra::Series<W, M>, rat::exp<Tm, Tw>,
84 algebra::DispatchFunction<rat::exp<Tm, Tw> >
90 template <
typename W,
typename M,
typename Tm,
typename Tw>
91 Tm op_choose_from_supp(
const algebra::Series<W, M>&,
92 const rat::exp<Tm, Tw>& m)
94 rat::RandomVisitor<Tm, Tw> v;
99 template<
typename W,
typename M,
typename Tm,
typename Tw>
100 const rat::exp<Tm, Tw>& identity_value(
SELECTOR2(algebra::Series<W, M>),
107 template<
typename W,
typename M,
typename Tm,
typename Tw>
108 bool show_identity_value(
SELECTOR2(algebra::Series<W, M>),
114 template<
typename W,
typename M,
typename Tm,
typename Tw>
115 const rat::exp<Tm, Tw>& zero_value(
SELECTOR2(algebra::Series<W, M>),
122 template <
typename W,
typename M,
typename Tm,
typename Tw>
123 void op_in_transpose(
const algebra::Series<W, M>& s,
124 rat::exp<Tm, Tw>& exp)
126 Element<algebra::Series<W, M>,
127 rat::exp<Tm, Tw> > elt(s, exp);
129 vcsn::algebra::KRatExpTranspose<
130 algebra::Series<W, M>,
132 algebra::DispatchFunction<vcsn::rat::exp<Tm, Tw> >
135 elt = matcher.match(exp);
139 template<
typename W,
typename M,
typename Tm,
typename Tw>
140 void op_in_add(
const algebra::Series<W, M>&,
141 rat::exp<Tm, Tw>& dst,
142 const rat::exp<Tm, Tw>& arg)
145 if (arg.base()->what() == rat::Node<Tm, Tw>::zero)
149 if (dst.base()->what() == rat::Node<Tm, Tw>::zero)
152 dst.base() = arg.base()->clone();
156 dst.base() =
new rat::Sum<Tm, Tw>(dst.base(), arg.base()->clone());
159 template<
typename W,
typename M,
typename Tm,
typename Tw>
160 rat::exp<Tm, Tw> op_add(
const algebra::Series<W, M>& s,
161 const rat::exp<Tm, Tw>& a,
162 const rat::exp<Tm, Tw>& b)
164 rat::exp<Tm, Tw> ret(a);
165 op_in_add(s, ret, b);
169 template<
typename W,
typename M,
typename Tm,
typename Tw>
170 void op_in_mul(
const algebra::Series<W, M>& s,
171 rat::exp<Tm, Tw>& dst,
172 const rat::exp<Tm, Tw>& arg)
174 typedef rat::Node<Tm, Tw> node_t;
175 typedef typename rat::Node<Tm, Tw>::type type;
176 typedef rat::One<Tm, Tw> n_one_t;
177 typedef rat::Constant<Tm, Tw> n_const_t;
178 typedef rat::Zero<Tm, Tw> n_zero_t;
179 typedef rat::Star<Tm, Tw> n_star_t;
180 typedef rat::LeftWeighted<Tm, Tw> n_lweight_t;
181 typedef rat::RightWeighted<Tm, Tw> n_rweight_t;
182 typedef rat::Sum<Tm, Tw> n_sum_t;
183 typedef rat::Product<Tm, Tw> n_prod_t;
185 type this_type = dst.base()->what();
186 type arg_type = arg.base()->what();
189 if (this_type == node_t::zero)
193 if (arg_type == node_t::zero)
196 dst.base() =
new n_zero_t;
201 if (this_type == node_t::one)
204 dst.base() = arg.base()->clone();
209 if (arg_type == node_t::one)
215 if (arg_type == node_t::lweight)
217 n_lweight_t *p =
dynamic_cast<n_lweight_t*
>(arg.base());
218 if (p->child_->what() == node_t::one)
220 op_in_mul(s, s.semiring(), dst, p->weight_);
226 if (this_type == node_t::lweight)
228 n_lweight_t *p =
dynamic_cast<n_lweight_t*
>(dst.base());
229 if (p->child_->what() == node_t::one)
231 dst = op_mul(s.semiring(), s, p->weight_, arg);
237 dst.base() =
new n_prod_t(dst.base(), arg.base()->clone());
241 template<
typename W,
typename M,
typename Tm,
typename Tw>
242 rat::exp<Tm, Tw> op_mul(
const algebra::Series<W, M>& s,
243 const rat::exp<Tm, Tw>& a,
244 const rat::exp<Tm, Tw>& b)
246 rat::exp<Tm, Tw> ret(a);
247 op_in_mul(s, ret, b);
251 template <
typename W,
typename M,
typename Tm,
typename Tw,
typename St>
253 op_rout(
const algebra::Series<W, M>& s,
255 const rat::exp<Tm, Tw>& e)
257 rat::DumpVisitor<Tm, Tw, W, M> v (st, s.monoid(), s.representation());
266 template<
typename Tm,
typename Tw,
typename M,
typename W>
267 rat::exp<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<M, W>),
271 return new rat::Constant<Tm, Tw>(m_value);
274 template<
typename Tm,
typename Tw,
typename M,
typename W>
275 rat::exp<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<M, W>),
279 const char str[] = {m_value,
'\0'};
280 return new rat::Constant<Tm, Tw>(str);
283 template<
typename Tm,
typename Tw,
typename W,
typename M,
typename oTm>
284 rat::exp<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<W, M>) s,
296 template<
typename Tm,
typename Tw,
typename W,
typename M,
typename oTw>
297 rat::exp<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<W, M>),
307 ret.base() =
new rat::LeftWeighted<Tm, Tw>
309 w_value), ret.base());
313 template <
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
314 void op_assign(
SELECTOR2(algebra::Series<W, M>) s,
316 rat::exp<Tm, Tw>& dst,
324 template <
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
325 void op_assign(
SELECTOR2(algebra::Series<W, M>) s,
327 rat::exp<Tm, Tw>& dst,
339 template<
typename W,
typename M,
typename Tm,
typename Tw>
340 bool op_starable(
const algebra::Series<W, M>&,
341 const rat::exp<Tm, Tw>&)
346 template<
typename W,
typename M,
typename Tm,
typename Tw>
347 void op_in_star(
const algebra::Series<W, M>&,
348 rat::exp<Tm, Tw>& dst)
351 if (dst.base()->what() == rat::Node<Tm, Tw>::zero)
354 dst.
base() =
new rat::Star<Tm, Tw>(dst.base());
357 template<
typename W,
typename M,
typename Tm,
typename Tw>
359 op_star(
const algebra::Series<W, M>& s,
360 const rat::exp<Tm, Tw>& src)
362 rat::exp<Tm, Tw> ret(src);
371 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
372 void op_in_add(
const algebra::Series<W, M>& s,
374 rat::exp<Tm, Tw>& dst,
377 op_in_add(s, dst, op_convert(s,
383 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
384 rat::exp<Tm, Tw> op_add(
const algebra::Series<W, M>& s,
386 const rat::exp<Tm, Tw>& a,
389 rat::exp<Tm, Tw> ret(a);
390 op_in_add(s, monoid, ret, b);
394 template<
typename M,
typename W,
typename oTm,
typename Tm,
typename Tw>
395 rat::exp<Tm, Tw> op_add(
const M& monoid,
396 const algebra::Series<W, M>& s,
398 const rat::exp<Tm, Tw>& b)
400 rat::exp<Tm, Tw> ret(b);
401 op_in_add(s, monoid, ret, a);
409 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
410 void op_in_add(
const algebra::Series<W, M>& s,
412 rat::exp<Tm, Tw>& dst,
415 precondition(& s.semiring() == & semiring);
416 op_in_add(s, dst, op_convert(s,
422 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
423 rat::exp<Tm, Tw> op_add(
const algebra::Series<W, M>& s,
425 const rat::exp<Tm, Tw>& a,
428 rat::exp<Tm, Tw> ret(a);
429 op_in_add(s, semiring, ret, b);
433 template<
typename W,
typename M,
typename oTw,
typename Tm,
typename Tw>
434 rat::exp<Tm, Tw> op_add(
const W& semiring,
435 const algebra::Series<W, M>& s,
437 const rat::exp<Tm, Tw>& b)
439 rat::exp<Tm, Tw> ret(b);
440 op_in_add(s, semiring, ret, a);
448 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
449 void op_in_mul(
const algebra::Series<W, M>& s,
451 rat::exp<Tm, Tw>& ret,
454 precondition(& s.semiring() == & semiring);
455 (void) s; (void) semiring;
457 typedef rat::Node<Tm, Tw> node_t;
458 typedef typename rat::Node<Tm, Tw>::type type;
459 typedef rat::One<Tm, Tw> n_one_t;
460 typedef rat::Constant<Tm, Tw> n_const_t;
461 typedef rat::Zero<Tm, Tw> n_zero_t;
462 typedef rat::Star<Tm, Tw> n_star_t;
463 typedef rat::LeftWeighted<Tm, Tw> n_lweight_t;
464 typedef rat::RightWeighted<Tm, Tw> n_rweight_t;
465 typedef rat::Sum<Tm, Tw> n_sum_t;
466 typedef rat::Product<Tm, Tw> n_prod_t;
468 type this_type = ret.base()->what();
471 if (this_type == node_t::zero)
482 ret.base() =
new n_zero_t;
487 if (this_type == node_t::one)
489 ret.base() =
new n_lweight_t
495 if ((this_type == node_t::constant)
496 && op_is_atom(s.monoid(),
497 dynamic_cast<n_const_t*
>(ret.base())->value_))
499 ret.base() =
new n_lweight_t
505 if (this_type == node_t::rweight)
507 op_in_mul(s.semiring(),
508 dynamic_cast<n_rweight_t*
>(ret.base())->weight_,
519 if (this_type == node_t::lweight)
522 n_lweight_t* p =
dynamic_cast<n_lweight_t*
>(ret.base());
523 if ((p->child_->what() == node_t::constant
524 && op_is_atom(s.monoid(),
525 dynamic_cast<n_const_t*
>(p->child_)->value_))
526 || p->child_->what() == node_t::one)
528 p->weight_ = op_mul (s.semiring(), p->weight_,
534 if (p->child_->what() == node_t::rweight)
536 n_rweight_t* q =
dynamic_cast<n_rweight_t*
>(p->child_);
538 q->weight_ = op_mul (s.semiring(),
554 new n_rweight_t(op_convert(
SELECT(W),
SELECT(Tw), w), ret.base());
558 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
559 rat::exp<Tm, Tw> op_mul(
const algebra::Series<W, M>& s,
561 const rat::exp<Tm, Tw>& a,
564 rat::exp<Tm, Tw> ret(a);
565 op_in_mul(s, semiring, ret, w);
569 template<
typename W,
typename M,
typename oTw,
typename Tm,
typename Tw>
570 rat::exp<Tm, Tw> op_mul(
const W& semiring,
571 const algebra::Series<W, M>& s,
573 const rat::exp<Tm, Tw>& b)
575 precondition(& s.semiring() == & semiring);
576 (void) s; (void) semiring;
578 typedef rat::Node<Tm, Tw> node_t;
579 typedef typename rat::Node<Tm, Tw>::type type;
580 typedef rat::One<Tm, Tw> n_one_t;
581 typedef rat::Constant<Tm, Tw> n_const_t;
582 typedef rat::Zero<Tm, Tw> n_zero_t;
583 typedef rat::Star<Tm, Tw> n_star_t;
584 typedef rat::LeftWeighted<Tm, Tw> n_lweight_t;
585 typedef rat::RightWeighted<Tm, Tw> n_rweight_t;
586 typedef rat::Sum<Tm, Tw> n_sum_t;
587 typedef rat::Product<Tm, Tw> n_prod_t;
589 rat::exp<Tm, Tw> ret(b);
591 type this_type = ret.base()->what();
594 if (this_type == node_t::zero)
608 if (this_type == node_t::lweight)
610 n_lweight_t* p =
dynamic_cast<n_lweight_t*
>(ret.base());
612 (s.semiring(), op_convert(
SELECT(W),
SELECT(Tw), w), p->weight_);
617 ret.base() =
new n_lweight_t(w, ret.base());
621 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
622 Tw op_series_get(
const algebra::Series<W, M>& s,
623 const rat::exp<Tm, Tw>& p,
626 typedef typename standard_of_traits<algebra::Series<W, M>,
628 automaton_t automaton_t;
630 typename automaton_t::set_t automata (s);
631 automaton_t a (automata);
633 return eval(a, m).value();
636 template<
typename W,
typename M,
typename Tm,
typename Tw,
637 typename oTm,
typename oTw>
638 void op_series_set(
const algebra::Series<W, M>& s,
643 if ((m == algebra::identity_as<oTm>::of(s.monoid())) &&
644 (w == algebra::identity_as<oTw>::of(s.semiring())) &&
645 (p == algebra::zero_as<rat::exp<Tm, Tw> >::of(s)))
647 p = algebra::identity_as<rat::exp<Tm, Tw> >::of(s).value();
651 typedef Element<algebra::Series<W, M>, rat::exp<Tm, Tw> > series_set_elt_t;
652 typedef typename series_set_elt_t::monoid_elt_t monoid_elt_t;
653 typedef typename monoid_elt_t::value_t monoid_elt_value_t;
654 typedef std::list<monoid_elt_value_t> support_t;
656 rat::exp<Tm, Tw> pp = p;
657 p = algebra::zero_as<rat::exp<Tm, Tw> >::of(s).value();
661 support_t supp = op_support(s, pp);
664 for_all_const_(support_t, e, supp)
666 rat::exp<Tm, Tw> ret = op_convert(s,
675 sw = op_series_get(s, pp, *e);
677 op_in_add(s, p, op_mul(s.semiring(), s, sw, ret));
680 op_in_add(s, p, op_mul(s.semiring(), s, w,
692 template <
typename W,
typename M,
typename Tm,
typename Tw>
695 (
const rat::ConstNodeVisitor<Tm, Tw>& v)
const
697 this->value().accept(v);
700 template <
typename W,
typename M,
typename Tm,
typename Tw>
704 return this->value().depth();
709 template <
class W,
class M,
class Tm,
class Tw>
710 Element<algebra::Series<W,M>, rat::exp<Tm,Tw> >
711 op_choose(
const algebra::Series<W,M>& s,
714 Element<algebra::Series<W,M>, rat::exp<Tm, Tw> > e(s);
716 unsigned nb = RAND___(10);
720 unsigned t = RAND___(3);
732 Element<algebra::Series<W,M>, rat::exp<Tm,Tw> >
733 ep(s, s.monoid().choose(
SELECT(Tm)));
734 ep = ep * s.semiring().choose(
SELECT(Tw));
735 unsigned t = RAND___(2);
745 Element<algebra::Series<W,M>, rat::exp<Tm,Tw> >
746 ep(s, s.monoid().choose(
SELECT(Tm)));
747 ep = ep * s.semiring().choose(
SELECT(Tw));
748 unsigned t = RAND___(2);
757 return Element<algebra::Series<W,M>, rat::exp<Tm,Tw> >(s, e);
764 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_HXX