series_base.hxx

00001 // series_base.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX
00018 # define VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX
00019 
00020 # include <vaucanson/algebra/concept/series_base.hh>
00021 # include <vaucanson/misc/usual_macros.hh>
00022 
00023 namespace vcsn {
00024 
00025   namespace algebra {
00026 
00027     /*-----------------.
00028     | SeriesBase<Self> |
00029     `-----------------*/
00030 
00031     template<class Self>
00032     const typename SeriesBase<Self>::monoid_t&
00033     SeriesBase<Self>::monoid() const
00034     {
00035       return this->self().monoid();
00036     }
00037 
00038     template<class Self>
00039     const typename SeriesBase<Self>::semiring_t&
00040     SeriesBase<Self>::semiring() const
00041     {
00042       return this->self().semiring();
00043     }
00044 
00045     template<class Self>
00046     typename SeriesBase<Self>::monoid_t&
00047     SeriesBase<Self>::monoid()
00048     {
00049       return this->self().monoid();
00050     }
00051 
00052     template<class Self>
00053     typename SeriesBase<Self>::semiring_t&
00054     SeriesBase<Self>::semiring()
00055     {
00056       return this->self().semiring();
00057     }
00058 
00059     template<class Self>
00060     SeriesBase<Self>::SeriesBase()
00061     {}
00062 
00063     template<class Self>
00064     SeriesBase<Self>::SeriesBase(const SeriesBase& other) :
00065       SemiringBase<Self>(other)
00066     {}
00067 
00068   } // algebra
00069 
00070 
00071     /*---------------------------------.
00072     | MetaElement<SeriesBase<Self>, T> |
00073     `---------------------------------*/
00074 
00075   template<typename Self, typename T>
00076   typename MetaElement<algebra::SeriesBase<Self>, T>::semiring_elt_value_t
00077   MetaElement<algebra::SeriesBase<Self>, T>::get(const monoid_elt_value_t& m) const
00078   {
00079     // assertion(structure().monoid().contains(m));
00080     return op_series_get(this->structure(), this->value(), m);
00081   }
00082 
00083   template<typename Self, typename T>
00084   typename MetaElement<algebra::SeriesBase<Self>, T>::semiring_elt_t
00085   MetaElement<algebra::SeriesBase<Self>, T>::get(const monoid_elt_t& m) const
00086   {
00087     return semiring_elt_t(this->structure().semiring(), get(m.value()));
00088   }
00089 
00090   template<typename Self, typename T>
00091   void
00092   MetaElement<algebra::SeriesBase<Self>, T>::assoc(const monoid_elt_value_t& m,
00093                                                    const semiring_elt_value_t& w)
00094   {
00095     // assertion(structure().monoid().contains(m));
00096     // assertion(structure().semiring().contains(w));
00097     return op_series_set(this->structure(), this->value(), m, w);
00098   }
00099 
00100   template<typename Self, typename T>
00101   void
00102   MetaElement<algebra::SeriesBase<Self>, T>::assoc(const monoid_elt_t& m,
00103                                                    const semiring_elt_t& w)
00104   {
00105     assoc(m.value(), w.value());
00106   }
00107 
00108   template<typename Self, typename T>
00109   bool
00110   MetaElement<algebra::SeriesBase<Self>, T>::is_finite_app() const
00111   {
00112     return op_is_finite_app(this->structure(), this->value());
00113   }
00114 
00115   template <typename Self, typename T>
00116   typename MetaElement<algebra::SeriesBase<Self>, T>::monoid_elt_t
00117   MetaElement<algebra::SeriesBase<Self>, T>::choose_from_supp() const
00118   {
00119     return
00120       typename MetaElement<algebra::SeriesBase<Self>, T>::monoid_elt_t
00121       (this->structure().monoid(),
00122        op_choose_from_supp(this->structure(), this->value()));
00123   }
00124 
00125   template <typename Self, typename T>
00126   void
00127   MetaElement<algebra::SeriesBase<Self>, T>::transpose()
00128   {
00129     op_in_transpose(this->structure(), this->value());
00130   }
00131 
00132   template <typename Self, typename T>
00133   typename MetaElement<algebra::SeriesBase<Self>, T>::support_t
00134   MetaElement<algebra::SeriesBase<Self>, T>::supp() const
00135   {
00136     return op_support(this->structure(), this->value());
00137   }
00138 
00139   template<typename Self, typename T>
00140   MetaElement<algebra::SeriesBase<Self>, T>::MetaElement()
00141   {}
00142 
00143   template<typename Self, typename T>
00144   MetaElement<algebra::SeriesBase<Self>, T>::MetaElement(const MetaElement& other) :
00145     MetaElement<algebra::SemiringBase<Self>, T>(other)
00146   {}
00147 
00148 
00149   /*------.
00150   | Ops.  |
00151   `------*/
00152 
00153   template<typename S, typename T>
00154   bool op_is_finite_app(const algebra::SeriesBase<S>& s, const T& t)
00155   {
00156     return false;
00157   }
00158 
00159   template<typename S, typename T, typename M, typename W>
00160   void op_series_structure(const algebra::SeriesBase<S>& s, const T& t, const W& w)
00161   {
00162     pure_service_call ("default implementation of op_series_structure()");
00163   }
00164 
00165   template <class S, class T>
00166   Element<S, T> 
00167   op_series_choose(const algebra::SeriesBase<S>& s, SELECTOR(T))
00168   {
00169     pure_service_call ("default implementation of op_series_choose()");
00170     return Element<S, T>();
00171   }
00172 
00173   template <class S, class T>
00174   typename algebra::series_traits<T>::support_t
00175   op_support(const algebra::SeriesBase<S>&, const T& v)
00176   {
00177     return v;
00178   }
00179 
00180   template <class S, class T>
00181   Element<S, T>
00182   transpose(const algebra::SeriesBase<S>& s, const T& t)
00183   {
00184     T   new_t(t);
00185     new_t.transpose();
00186     return new_t;
00187   }
00188 
00189   template <class S, class T>
00190   bool
00191   is_letter_support(const Element<S, T>& s)
00192   {
00193     typedef typename algebra::series_traits<T>::support_t support_t;
00194     support_t supp = s.supp();
00195     for_all_const_(support_t, e, supp)
00196       if (op_size(s.structure().monoid(), *e) != 1)
00197         return false;
00198     return true;
00199   }
00200 
00201   template <typename S1, typename S2, typename T1, typename T2>
00202   void
00203   extract_support(Element<S1, T1>& s1, Element<S2, T2>& s2)
00204   {
00205     typedef typename algebra::series_traits<T2>::support_t support_t;
00206     typedef typename algebra::series_traits<T1>::semiring_elt_value_t
00207       semiring_elt_value_t;
00208     for_all_const_(support_t, e, s2.supp())
00209       s1.assoc(*e,
00210                algebra::identity_as<semiring_elt_value_t>::
00211                of(s1.structure().semiring()));
00212   }
00213 
00214   template <class S, class T>
00215   Element<S, T> 
00216   hadamard(const Element<S, T>& lhs, const Element<S, T>& rhs)
00217   {
00218     typedef Element<S, T> series_set_elt_t;
00219     typedef typename Element<S, T>::monoid_elt_t monoid_elt_t;
00220     typedef typename Element<S, T>::semiring_elt_t semiring_elt_t;
00221     typedef typename Element<S, T>::support_t support_t;
00222     Element<S, T> output;
00223     support_t support = lhs.supp();
00224     for (typename support_t::iterator supp = support.begin();
00225          supp != support.end();
00226          ++supp)
00227     {
00228       output +=  lhs.get(*supp) *
00229         rhs.get(*supp) * series_set_elt_t(lhs.structure(), monoid_elt_t(*supp));
00230     }
00231     return output;
00232   }
00233 
00234   template <class S, class M>
00235   S
00236   op_convert(const algebra::SeriesBase<S>&,
00237              const algebra::FreeMonoidBase<M>& monoid)
00238   {
00239     // Ensures the monoid is compatible with the series.
00240     enum { compatible = misc::static_eq<typename S::monoid_t, M>::value };
00241     static_assertion_(compatible, invalid_conversion_from_monoid_to_series);
00242 
00243     typename S::semiring_t semiring;
00244     return S (semiring, monoid.self());
00245   }
00246 
00247   template <class S, class T>
00248   T 
00249   op_convert(const algebra::SeriesBase<S>&, SELECTOR(T), const T& src_)
00250   {
00251     return src_;
00252   }
00253 
00254   template <class S, class T, class U>
00255   T
00256   op_convert(const algebra::SeriesBase<S>& s, SELECTOR(T), const U& src_)
00257   {
00258     typedef typename algebra::series_traits<U>::support_t       support_t;
00259     typedef typename Element<S, T>::monoid_elt_t                monoid_elt_t;
00260     S ts = s.self();
00261     Element<S, U> src(ts, src_);
00262     Element<S, T> dst(ts);
00263     support_t support = src.supp();
00264     for_all_const_(support_t, ss, support)
00265       dst += src.get(monoid_elt_t(s.monoid(), *ss)) *
00266       Element<S, T>(s.self(), monoid_elt_t(s.monoid(), *ss));
00267     return dst.value();
00268   }
00269 
00270 } // vcsn
00271 
00272 #endif // ! VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX

Generated on Sun Jul 29 19:35:29 2007 for Vaucanson by  doxygen 1.5.2