18 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_POLYNOMS_HXX
19 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_POLYNOMS_HXX
21 # include <vaucanson/algebra/implementation/series/polynoms.hh>
22 # include <vaucanson/algebra/concept/freemonoid_base.hh>
35 template<
typename Tm,
typename Tw>
36 template<
typename M,
typename W>
39 map_.insert(std::make_pair(identity_value(
SELECT(M),
SELECT(Tm)),
43 template<
typename Tm,
typename Tw>
44 polynom<Tm, Tw>::polynom(
const polynom& other) : map_(other.map_)
47 template<
typename Tm,
typename Tw>
48 polynom<Tm, Tw>::polynom() : map_()
51 template<
typename Tm,
typename Tw>
53 polynom<Tm, Tw>::size()
const
58 template<
typename Tm,
typename Tw>
59 bool polynom<Tm, Tw>::empty()
const
64 template<
typename Tm,
typename Tw>
65 typename polynom<Tm, Tw>::iterator
66 polynom<Tm, Tw>::begin()
71 template<
typename Tm,
typename Tw>
72 typename polynom<Tm, Tw>::const_iterator
73 polynom<Tm, Tw>::begin()
const
78 template<
typename Tm,
typename Tw>
79 typename polynom<Tm, Tw>::iterator
80 polynom<Tm, Tw>::end()
85 template<
typename Tm,
typename Tw>
86 typename polynom<Tm, Tw>::const_iterator
87 polynom<Tm, Tw>::end()
const
92 template<
typename Tm,
typename Tw>
93 typename polynom<Tm, Tw>::iterator
94 polynom<Tm, Tw>::find(
const Tm& m)
99 template<
typename Tm,
typename Tw>
100 typename polynom<Tm, Tw>::const_iterator
101 polynom<Tm, Tw>::find(
const Tm& m)
const
106 template<
typename Tm,
typename Tw>
108 Tw& polynom<Tm, Tw>::make_get(
SELECTOR(W),
const Tm& m)
110 std::pair<iterator, bool> i =
111 map_.insert(std::make_pair(m, zero_value(
SELECT(W),
SELECT(Tw))));
112 return i.first->second;
115 template<
typename Tm,
typename Tw>
120 if ((i = map_.find(m)) == map_.end())
125 template<
typename Tm,
typename Tw>
126 void polynom<Tm, Tw>::insert(
const Tm& m,
const Tw& w)
128 map_.insert(std::make_pair(m, w));
131 template<
typename Tm,
typename Tw>
133 void polynom<Tm, Tw>::add(
const W& semiring,
const Tm& m,
const Tw& w)
135 Tw& o = make_get(
SELECT(W), m);
139 template<
typename Tm,
typename Tw>
140 void polynom<Tm, Tw>::erase(iterator i)
145 template<
typename Tm,
typename Tw>
146 void polynom<Tm, Tw>::clear()
151 template<
typename Tm,
typename Tw>
154 map_.swap(other.map_);
157 template<
typename Tm,
typename Tw>
158 const std::map<Tm, Tw>&
159 polynom<Tm, Tw>::as_map()
const
164 template<
typename Tm,
typename Tw>
166 polynom<Tm, Tw>::operator [] (
const Tm& m)
const
168 const_iterator i = map_.find(m);
170 postcondition_ (i != map_.end(),
171 "Word is not in the support");
176 template<
typename Tm,
typename Tw>
178 polynom<Tm, Tw>::operator [] (
const Tm& m)
183 template <
class Series,
class Tm,
class Tw>
185 DefaultTransposeFun<Series, polynom<Tm,Tw> >::
186 operator()(
const Series& s,
const polynom<Tm,Tw>& t)
const
188 typedef typename polynom<Tm, Tw>::const_iterator const_iterator;
189 typedef typename Series::monoid_t monoid_t;
190 typedef Element<monoid_t, Tm> monoid_elt_t;
194 for (const_iterator i = t.begin(); i != t.end(); ++i)
196 monoid_elt_t m (s.monoid(), i->first);
198 p[m.value()] =
transpose(s.semiring(), i->second);
203 template <
class Series,
class Tm,
class Tw>
206 DefaultTransposeFun<Series, polynom<Tm,Tw> >
::
207 transpose(
const SeriesBase<S>& s,
const Tw& t)
209 Element<S, Tw> e (s.self(), t);
214 template <
class Series,
class Tm,
class Tw>
217 DefaultTransposeFun<Series, polynom<Tm,Tw> >
::
218 transpose(
const SemiringBase<S>&,
const Tw& t)
223 template <
class Tm,
class Tw>
224 bool operator==(
const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
226 return lhs.as_map() == rhs.as_map();
229 template <
class Tm,
class Tw>
230 bool operator!=(
const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
232 return !(lhs == rhs);
235 template <
class Tm,
class Tw>
236 bool operator<(const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
238 return lhs.as_map() < rhs.as_map();
241 template <
class Tm,
class Tw>
242 bool operator>(
const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
244 return lhs.as_map() > rhs.as_map();
247 template <
class Tm,
class Tw>
248 bool operator<=(const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
250 return lhs.as_map() <= rhs.as_map();
253 template <
class Tm,
class Tw>
254 bool operator>=(
const polynom<Tm, Tw>& lhs,
const polynom<Tm, Tw>& rhs)
256 return lhs.as_map() >= rhs.as_map();
263 template<
typename W,
typename M,
typename Tm,
typename Tw>
265 const algebra::polynom<Tm, Tw>& m)
267 for (
typename algebra::polynom<Tm, Tw>::const_iterator i = m.begin();
270 if (!s.monoid().contains(i->first)
271 || !s.semiring().contains(i->second))
276 template<
typename Self,
typename Tm,
typename Tw>
277 bool op_starable(
const algebra::SeriesBase<Self>&,
278 const algebra::polynom<Tm, Tw>& m)
286 typename std::pair<Tm, Tw> elt = *m.as_map().begin();
287 if (elt.first != identity_value(
SELECT(
typename Self::monoid_t),
291 return op_starable(
SELECT(
typename Self::semiring_t), elt.second);
295 template<
typename Self,
typename Tm,
typename Tw>
296 void op_in_star(
const algebra::SeriesBase<Self>&,
297 algebra::polynom<Tm, Tw>& m)
301 Tw val (zero_value(
SELECT(
typename Self::semiring_t),
SELECT(Tw)));
302 op_in_star(
SELECT(
typename Self::semiring_t), val);
303 m.insert(identity_value(
SELECT(
typename Self::monoid_t),
SELECT(Tm)),
308 typename std::pair<Tm, Tw> elt = *m.as_map().begin();
309 assertion_ (!(m.size() > 1 ||
310 elt.first != identity_value(
SELECT(
typename
313 "Support is not empty, star cannot be computed.");
315 op_in_star(
SELECT(
typename Self::semiring_t), elt.second);
317 m.insert(elt.first, elt.second);
321 template<
typename W,
typename M,
typename Tm,
typename Tw>
322 bool op_is_finite_app(
const algebra::Series<W, M>&,
323 const algebra::polynom<Tm, Tw>&)
328 template<
typename W,
typename M,
typename Tm,
typename Tw>
329 typename algebra::series_traits<algebra::polynom<Tm, Tw> >::support_t
330 op_support(
const algebra::Series<W, M>&,
331 const algebra::polynom<Tm, Tw>& m)
333 return typename algebra::series_traits<algebra::polynom<Tm, Tw> >::
334 support_t(m.as_map());
337 template<
typename W,
typename M,
typename Tm,
typename Tw>
338 const algebra::polynom<Tm, Tw>&
339 identity_value(
SELECTOR2(algebra::Series<W, M>),
342 static const algebra::polynom<Tm, Tw> instance =
347 template<
typename W,
typename M,
typename Tm,
typename Tw>
348 const algebra::polynom<Tm, Tw>&
349 zero_value(
SELECTOR2(algebra::Series<W, M>),
352 static const algebra::polynom<Tm, Tw> instance;
356 template<
typename W,
typename M,
typename Tm,
typename Tw>
357 void op_in_add(
const algebra::Series<W, M>& s,
358 algebra::polynom<Tm, Tw>& dst,
359 const algebra::polynom<Tm, Tw>& arg)
361 typename algebra::polynom<Tm, Tw>::iterator p;
365 for (
typename algebra::polynom<Tm, Tw>::const_iterator i = arg.begin();
368 if (i->second != zero)
370 p = dst.find(i->first);
381 dst.insert(i->first, i->second);
385 template<
typename W,
typename M,
typename Tm,
typename Tw>
386 algebra::polynom<Tm, Tw>
op_add(
const algebra::Series<W, M>& s,
387 const algebra::polynom<Tm, Tw>& a,
388 const algebra::polynom<Tm, Tw>& b)
390 algebra::polynom<Tm, Tw> ret(a);
399 template<
typename W,
typename M,
typename Tm,
typename Tw>
400 algebra::polynom<Tm, Tw>
op_mul(
const algebra::Series<W, M>& s,
401 const algebra::polynom<Tm, Tw>& a,
402 const algebra::polynom<Tm, Tw>& b)
404 algebra::polynom<Tm, Tw> ret;
405 for (
typename algebra::polynom<Tm, Tw>::const_iterator i = a.begin();
408 for (
typename algebra::polynom<Tm, Tw>::const_iterator j = b.begin();
412 Tw w =
op_mul(s.semiring(), i->second, j->second);
414 ret.add(s.semiring(),
415 op_mul(s.monoid(), i->first, j->first),
421 template<
typename W,
typename M,
typename Tm,
typename Tw>
422 void op_in_mul(
const algebra::Series<W, M>& s,
423 algebra::polynom<Tm, Tw>& dst,
424 const algebra::polynom<Tm, Tw>& arg)
433 template <
typename Tm,
typename Tw,
typename W,
typename M>
434 algebra::polynom<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<W, M>),
439 p.insert(m_value, identity_value(
SELECT(W),
SELECT(Tw)));
443 template<
typename Tm,
typename Tw,
typename W,
typename M,
typename oTm>
444 algebra::polynom<Tm, Tw> op_convert(
const algebra::Series<W, M>& s,
449 const M& monoid = s.monoid();
450 const W& semiring = s.semiring();
452 algebra::polynom<Tm, Tw> ret;
453 ret.insert(op_convert(monoid,
SELECT(Tm), m_value),
454 identity_value(semiring,
SELECT(Tw)));
458 template<
typename Tm,
typename Tw,
typename W,
typename M,
typename oTw>
459 algebra::polynom<Tm, Tw> op_convert(
SELECTOR2(algebra::Series<W, M>),
464 algebra::polynom<Tm, Tw> ret;
471 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
472 void op_assign(
const algebra::Series<W, M>&,
473 const algebra::MonoidBase<M>&,
474 algebra::polynom<Tm, Tw>& dst,
482 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
483 void op_assign(
const algebra::Series<W, M>& s,
484 const algebra::SemiringBase<W>&,
485 algebra::polynom<Tm, Tw>& dst,
498 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
499 void op_in_add(
const algebra::Series<W, M>& s,
500 const algebra::MonoidBase<M>& monoid,
501 algebra::polynom<Tm, Tw>& dst,
504 precondition(& s.monoid() == & monoid);
505 dst.add(s.semiring(),
510 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
511 algebra::polynom<Tm, Tw>
op_add(
const algebra::Series<W, M>& s,
512 const algebra::MonoidBase<M>& monoid,
513 const algebra::polynom<Tm, Tw>& a,
516 algebra::polynom<Tm, Tw> ret(a);
521 template<
typename M,
typename W,
typename oTm,
typename Tm,
typename Tw>
522 algebra::polynom<Tm, Tw>
op_add(
const algebra::MonoidBase<M>& monoid,
523 const algebra::Series<W, M>& s,
525 const algebra::polynom<Tm, Tw>& b)
527 algebra::polynom<Tm, Tw> ret(b);
536 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
537 void op_in_add(
const algebra::Series<W, M>& s,
538 const algebra::SemiringBase<W>& semiring,
539 algebra::polynom<Tm, Tw>& dst,
542 precondition(& s.semiring() == & semiring);
544 dst.add(s.semiring(),
549 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
550 algebra::polynom<Tm, Tw>
op_add(
const algebra::Series<W, M>& s,
551 const algebra::SemiringBase<W>& semiring,
552 const algebra::polynom<Tm, Tw>& a,
555 algebra::polynom<Tm, Tw> ret(a);
560 template<
typename W,
typename M,
typename oTw,
typename Tm,
typename Tw>
561 algebra::polynom<Tm, Tw>
op_add(
const algebra::SemiringBase<W>& semiring,
562 const algebra::Series<W, M>& s,
564 const algebra::polynom<Tm, Tw>& b)
566 algebra::polynom<Tm, Tw> ret(b);
575 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
576 void op_in_mul(
const algebra::Series<W, M>& s,
577 const algebra::SemiringBase<W>& semiring,
578 algebra::polynom<Tm, Tw>& dst,
581 precondition(& s.semiring() == & semiring);
582 (void) s; (void) semiring;
584 typename algebra::polynom<Tm, Tw>::iterator p;
585 for (
typename algebra::polynom<Tm, Tw>::iterator i = dst.begin();
596 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTw>
597 algebra::polynom<Tm, Tw>
op_mul(
const algebra::Series<W, M>& s,
598 const algebra::SemiringBase<W>& semiring,
599 const algebra::polynom<Tm, Tw>& a,
602 algebra::polynom<Tm, Tw> ret(a);
607 template<
typename W,
typename M,
typename oTw,
typename Tm,
typename Tw>
608 algebra::polynom<Tm, Tw>
op_mul(
const algebra::SemiringBase<W>& semiring,
609 const algebra::Series<W, M>& s,
611 const algebra::polynom<Tm, Tw>& b)
613 precondition(& s.semiring() == & semiring);
614 (void) s; (void) semiring;
616 algebra::polynom<Tm, Tw> ret(b);
618 typename algebra::polynom<Tm, Tw>::iterator p;
619 for (
typename algebra::polynom<Tm, Tw>::iterator i = ret.begin();
623 p->second =
op_mul(s.semiring(), a, p->second);
634 template<
typename W,
typename M,
typename St,
typename Tm,
typename Tw>
635 St&
op_rout(
const algebra::Series<W, M>& s,
637 const algebra::polynom<Tm, Tw>& p)
639 typename algebra::polynom<Tm, Tw>::const_iterator i = p.begin();
644 st << s.representation()->plus;
646 bool show_weight = (i->second != identity_value(
SELECT(W),
SELECT(Tw))
651 st << s.representation()->open_par
652 << s.representation()->open_weight;
653 op_rout(s.semiring(), st, i->second);
654 st << s.representation()->close_weight
655 << s.representation()->spaces.front();
658 op_rout(s.monoid(), st, i->first);
661 st << s.representation()->close_par;
676 template<
typename W,
typename M,
typename Tm,
typename Tw,
typename oTm>
677 Tw op_series_get(
const algebra::Series<W, M>& s,
678 const algebra::polynom<Tm, Tw>& p,
681 return p.get(s.semiring(), op_convert(s.semiring(),
SELECT(Tm), m));
684 template <
typename W,
typename M,
685 typename Tm,
typename Tw,
686 typename oTm,
typename oTw>
687 void op_series_set(
const algebra::Series<W, M>& s,
688 algebra::polynom<Tm, Tw>& p,
692 const M& monoid = s.monoid();
693 const W& semiring = s.semiring();
695 typename misc::static_if
696 <misc::static_eq<Tm, oTm>::value,
const Tm&, Tm>::t
697 new_m = op_convert(monoid,
SELECT(Tm), m);
698 typename misc::static_if
699 <misc::static_eq<Tw, oTw>::value,
const Tw&, Tw>::t
700 new_w = op_convert(semiring,
SELECT(Tw), w);
702 typename algebra::polynom<Tm, Tw>::iterator i = p.find(new_m);
703 if (new_w == zero_value(semiring, new_w))
708 else if (i == p.end())
709 p.insert(new_m, new_w);
714 template <
class W,
class M,
class Tm,
class Tw>
715 Tm op_choose_from_supp(
const algebra::Series<W, M>&,
716 const algebra::polynom<Tm, Tw>& p)
718 typedef typename algebra::polynom<Tm, Tw>::const_iterator const_iterator;
720 const unsigned size = p.size();
725 const_iterator i = p.begin();
731 unsigned index = rand() % size;
740 template <
class W,
class M,
class Tm,
class Tw>
741 Element<algebra::Series<W,M>, algebra::polynom<Tm,Tw> >
742 op_choose(
const algebra::Series<W,M>& s,
745 algebra::polynom<Tm, Tw> p;
747 unsigned nb_monome = rand() * 10 / RAND_MAX;
748 for (
unsigned i = 0; i < nb_monome; ++i)
749 p[s.monoid().choose()] = s.semiring().choose();
750 return Element<algebra::Series<W,M>, algebra::polynom<Tm,Tw> >(s, p);
757 template <
typename W,
typename M,
typename Tm,
typename Tw>
758 void op_in_transpose(
const algebra::Series<W, M>& s,
759 algebra::polynom<Tm, Tw>& t)
761 algebra::DefaultTransposeFun<algebra::Series<W, M>,
762 algebra::polynom<Tm, Tw> > f;
770 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_POLYNOMS_HXX