Vaucanson 1.4
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, 2007 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   typename MetaElement<algebra::SeriesBase<Self>, T>::series_set_t
00141   MetaElement<algebra::SeriesBase<Self>, T>::series() const
00142   {
00143     return this->structure();
00144   }
00145 
00146   template<typename Self, typename T>
00147   MetaElement<algebra::SeriesBase<Self>, T>::MetaElement()
00148   {}
00149 
00150   template<typename Self, typename T>
00151   MetaElement<algebra::SeriesBase<Self>, T>::MetaElement(const MetaElement& other) :
00152     MetaElement<algebra::SemiringBase<Self>, T>(other)
00153   {}
00154 
00155 
00156   namespace algebra {
00157 
00158     /*------.
00159     | Ops.  |
00160     `------*/
00161 
00162     template<typename S, typename T>
00163     bool op_is_finite_app(const algebra::SeriesBase<S>& s, const T& t)
00164     {
00165       return false;
00166     }
00167 
00168     template<typename S, typename T, typename M, typename W>
00169     void op_series_structure(const algebra::SeriesBase<S>& s, const T& t, const W& w)
00170     {
00171       pure_service_call ("default implementation of op_series_structure()");
00172     }
00173 
00174     template <class S, class T>
00175     Element<S, T>
00176     op_series_choose(const algebra::SeriesBase<S>& s, SELECTOR(T))
00177     {
00178       pure_service_call ("default implementation of op_series_choose()");
00179       return Element<S, T>();
00180     }
00181 
00182     template <class S, class T>
00183     typename algebra::series_traits<T>::support_t
00184     op_support(const algebra::SeriesBase<S>&, const T& v)
00185     {
00186       return v;
00187     }
00188 
00189   } // algebra
00190 
00191   template <class S, class T>
00192   Element<S, T>
00193   transpose(const algebra::SeriesBase<S>& s, const T& t)
00194   {
00195     T   new_t(t);
00196     new_t.transpose();
00197     return new_t;
00198   }
00199 
00200   template <class S, class T>
00201   bool
00202   is_support_in_alphabet(const Element<S, T>& s)
00203   {
00204     typedef typename algebra::series_traits<T>::support_t support_t;
00205     support_t supp = s.supp();
00206     for_all_const_(support_t, e, supp)
00207       if (op_size(s.structure().monoid(), *e) != 1)
00208         return false;
00209     return true;
00210   }
00211 
00212   template <typename S1, typename S2, typename T1, typename T2>
00213   void
00214   extract_support(Element<S1, T1>& s1, Element<S2, T2>& s2)
00215   {
00216     typedef typename algebra::series_traits<T2>::support_t support_t;
00217     typedef typename algebra::series_traits<T1>::semiring_elt_value_t
00218         semiring_elt_value_t;
00219     for_all_const_(support_t, e, s2.supp())
00220         s1.assoc(*e,
00221                  algebra::identity_as<semiring_elt_value_t>::
00222                  of(s1.structure().semiring()));
00223   }
00224 
00225   template <class S, class T>
00226   Element<S, T>
00227   hadamard(const Element<S, T>& lhs, const Element<S, T>& rhs)
00228   {
00229     typedef Element<S, T> series_set_elt_t;
00230     typedef typename Element<S, T>::monoid_elt_t monoid_elt_t;
00231     typedef typename Element<S, T>::semiring_elt_t semiring_elt_t;
00232     typedef typename Element<S, T>::support_t support_t;
00233     Element<S, T> output;
00234     support_t support = lhs.supp();
00235     for (typename support_t::iterator supp = support.begin();
00236            supp != support.end();
00237            ++supp)
00238     {
00239         output +=        lhs.get(*supp) *
00240           rhs.get(*supp) * series_set_elt_t(lhs.structure(), monoid_elt_t(*supp));
00241     }
00242     return output;
00243   }
00244 
00245   namespace algebra {
00246 
00247     template <class S, class M>
00248     S
00249     op_convert(const algebra::SeriesBase<S>&,
00250                const algebra::FreeMonoidBase<M>& monoid)
00251     {
00252       // Ensures the monoid is compatible with the series.
00253       enum { compatible = misc::static_eq<typename S::monoid_t, M>::value };
00254       static_assertion_(compatible, invalid_conversion_from_monoid_to_series);
00255 
00256       typename S::semiring_t semiring;
00257       return S (semiring, monoid.self());
00258     }
00259 
00260     template <class S, class T>
00261     T
00262     op_convert(const algebra::SeriesBase<S>&, SELECTOR(T), const T& src_)
00263     {
00264       return src_;
00265     }
00266 
00267     template <class S, class T, class U>
00268     T
00269     op_convert(const algebra::SeriesBase<S>& s, SELECTOR(T), const U& src_)
00270     {
00271       typedef typename algebra::series_traits<U>::support_t     support_t;
00272       typedef typename Element<S, T>::monoid_elt_t              monoid_elt_t;
00273       S ts = s.self();
00274       Element<S, U> src(ts, src_);
00275       Element<S, T> dst(ts);
00276       support_t support = src.supp();
00277       for_all_const_(support_t, ss, support)
00278         dst += src.get(monoid_elt_t(s.monoid(), *ss)) *
00279         Element<S, T>(s.self(), monoid_elt_t(s.monoid(), *ss));
00280       return dst.value();
00281     }
00282 
00284     template<typename S, typename St, typename T>
00285     St& op_rout(const algebra::SeriesBase<S>& s, St& st, const T& se)
00286     {
00287       return op_rout(s.self(), st, se);
00288     }
00289   } // algebra
00290 
00291 } // vcsn
00292 
00293 #endif // ! VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX