Vaucanson  1.4.1
series_base.hxx
1 // series_base.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX
18 # define VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX
19 
20 # include <vaucanson/algebra/concept/series_base.hh>
21 # include <vaucanson/misc/usual_macros.hh>
22 
23 namespace vcsn {
24 
25  namespace algebra {
26 
27  /*-----------------.
28  | SeriesBase<Self> |
29  `-----------------*/
30 
31  template<class Self>
32  const typename SeriesBase<Self>::monoid_t&
34  {
35  return this->self().monoid();
36  }
37 
38  template<class Self>
39  const typename SeriesBase<Self>::semiring_t&
41  {
42  return this->self().semiring();
43  }
44 
45  template<class Self>
48  {
49  return this->self().monoid();
50  }
51 
52  template<class Self>
55  {
56  return this->self().semiring();
57  }
58 
59  template<class Self>
61  {}
62 
63  template<class Self>
65  SemiringBase<Self>(other)
66  {}
67 
68  } // algebra
69 
70 
71  /*---------------------------------.
72  | MetaElement<SeriesBase<Self>, T> |
73  `---------------------------------*/
74 
75  template<typename Self, typename T>
76  typename MetaElement<algebra::SeriesBase<Self>, T>::semiring_elt_value_t
78  {
79  // assertion(structure().monoid().contains(m));
80  return op_series_get(this->structure(), this->value(), m);
81  }
82 
83  template<typename Self, typename T>
84  typename MetaElement<algebra::SeriesBase<Self>, T>::semiring_elt_t
86  {
87  return semiring_elt_t(this->structure().semiring(), get(m.value()));
88  }
89 
90  template<typename Self, typename T>
91  void
93  const semiring_elt_value_t& w)
94  {
95  // assertion(structure().monoid().contains(m));
96  // assertion(structure().semiring().contains(w));
97  return op_series_set(this->structure(), this->value(), m, w);
98  }
99 
100  template<typename Self, typename T>
101  void
103  const semiring_elt_t& w)
104  {
105  assoc(m.value(), w.value());
106  }
107 
108  template<typename Self, typename T>
109  bool
110  MetaElement<algebra::SeriesBase<Self>, T>::is_finite_app() const
111  {
112  return op_is_finite_app(this->structure(), this->value());
113  }
114 
115  template <typename Self, typename T>
116  typename MetaElement<algebra::SeriesBase<Self>, T>::monoid_elt_t
117  MetaElement<algebra::SeriesBase<Self>, T>::choose_from_supp() const
118  {
119  return
121  (this->structure().monoid(),
122  op_choose_from_supp(this->structure(), this->value()));
123  }
124 
125  template <typename Self, typename T>
126  void
128  {
129  op_in_transpose(this->structure(), this->value());
130  }
131 
132  template <typename Self, typename T>
133  typename MetaElement<algebra::SeriesBase<Self>, T>::support_t
135  {
136  return op_support(this->structure(), this->value());
137  }
138 
139  template <typename Self, typename T>
140  typename MetaElement<algebra::SeriesBase<Self>, T>::series_set_t
141  MetaElement<algebra::SeriesBase<Self>, T>::series() const
142  {
143  return this->structure();
144  }
145 
146  template<typename Self, typename T>
148  {}
149 
150  template<typename Self, typename T>
152  MetaElement<algebra::SemiringBase<Self>, T>(other)
153  {}
154 
155 
156  namespace algebra {
157 
158  /*------.
159  | Ops. |
160  `------*/
161 
162  template<typename S, typename T>
163  bool op_is_finite_app(const algebra::SeriesBase<S>& s, const T& t)
164  {
165  return false;
166  }
167 
168  template<typename S, typename T, typename M, typename W>
169  void op_series_structure(const algebra::SeriesBase<S>& s, const T& t, const W& w)
170  {
171  pure_service_call ("default implementation of op_series_structure()");
172  }
173 
174  template <class S, class T>
175  Element<S, T>
176  op_series_choose(const algebra::SeriesBase<S>& s, SELECTOR(T))
177  {
178  pure_service_call ("default implementation of op_series_choose()");
179  return Element<S, T>();
180  }
181 
182  template <class S, class T>
183  typename algebra::series_traits<T>::support_t
184  op_support(const algebra::SeriesBase<S>&, const T& v)
185  {
186  return v;
187  }
188 
189  } // algebra
190 
191  template <class S, class T>
192  Element<S, T>
193  transpose(const algebra::SeriesBase<S>& s, const T& t)
194  {
195  T new_t(t);
196  new_t.transpose();
197  return new_t;
198  }
199 
200  template <class S, class T>
201  bool
203  {
204  typedef typename algebra::series_traits<T>::support_t support_t;
205  support_t supp = s.supp();
206  for_all_const_(support_t, e, supp)
207  if (op_size(s.structure().monoid(), *e) != 1)
208  return false;
209  return true;
210  }
211 
212  template <typename S1, typename S2, typename T1, typename T2>
213  void
215  {
216  typedef typename algebra::series_traits<T2>::support_t support_t;
217  typedef typename algebra::series_traits<T1>::semiring_elt_value_t
218  semiring_elt_value_t;
219  for_all_const_(support_t, e, s2.supp())
220  s1.assoc(*e,
221  algebra::identity_as<semiring_elt_value_t>::
222  of(s1.structure().semiring()));
223  }
224 
225  template <class S, class T>
226  Element<S, T>
227  hadamard(const Element<S, T>& lhs, const Element<S, T>& rhs)
228  {
229  typedef Element<S, T> series_set_elt_t;
230  typedef typename Element<S, T>::monoid_elt_t monoid_elt_t;
231  typedef typename Element<S, T>::semiring_elt_t semiring_elt_t;
232  typedef typename Element<S, T>::support_t support_t;
233  Element<S, T> output;
234  support_t support = lhs.supp();
235  for (typename support_t::iterator supp = support.begin();
236  supp != support.end();
237  ++supp)
238  {
239  output += lhs.get(*supp) *
240  rhs.get(*supp) * series_set_elt_t(lhs.structure(), monoid_elt_t(*supp));
241  }
242  return output;
243  }
244 
245  namespace algebra {
246 
247  template <class S, class M>
248  S
249  op_convert(const algebra::SeriesBase<S>&,
250  const algebra::FreeMonoidBase<M>& monoid)
251  {
252  // Ensures the monoid is compatible with the series.
253  enum { compatible = misc::static_eq<typename S::monoid_t, M>::value };
254  static_assertion_(compatible, invalid_conversion_from_monoid_to_series);
255 
256  typename S::semiring_t semiring;
257  return S (semiring, monoid.self());
258  }
259 
260  template <class S, class T>
261  T
262  op_convert(const algebra::SeriesBase<S>&, SELECTOR(T), const T& src_)
263  {
264  return src_;
265  }
266 
267  template <class S, class T, class U>
268  T
269  op_convert(const algebra::SeriesBase<S>& s, SELECTOR(T), const U& src_)
270  {
271  typedef typename algebra::series_traits<U>::support_t support_t;
272  typedef typename Element<S, T>::monoid_elt_t monoid_elt_t;
273  S ts = s.self();
274  Element<S, U> src(ts, src_);
275  Element<S, T> dst(ts);
276  support_t support = src.supp();
277  for_all_const_(support_t, ss, support)
278  dst += src.get(monoid_elt_t(s.monoid(), *ss)) *
279  Element<S, T>(s.self(), monoid_elt_t(s.monoid(), *ss));
280  return dst.value();
281  }
282 
284  template<typename S, typename St, typename T>
285  St& op_rout(const algebra::SeriesBase<S>& s, St& st, const T& se)
286  {
287  return op_rout(s.self(), st, se);
288  }
289  } // algebra
290 
291 } // vcsn
292 
293 #endif // ! VCSN_ALGEBRA_CONCEPT_SERIES_BASE_HXX