17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_NUMERICAL_SEMIRING_HXX
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_NUMERICAL_SEMIRING_HXX
21 # include <vaucanson/algebra/concept/semiring_base.hh>
22 # include <vaucanson/algebra/implementation/semiring/numerical_semiring.hh>
33 bool op_contains(
const algebra::NumericalSemiring&, T)
38 template<
typename T,
typename U>
39 void op_in_mul(
const algebra::NumericalSemiring&,
45 template<
typename T,
typename U>
46 void op_in_add(
const algebra::NumericalSemiring&,
56 template<
typename T,
typename U>
57 T op_mul(
const algebra::NumericalSemiring&, T a, U b)
62 template<
typename T,
typename U>
63 T op_add(
const algebra::NumericalSemiring&, T a, U b)
87 Element<algebra::NumericalSemiring, T>
88 op_choose(
const algebra::NumericalSemiring&
set,
SELECTOR(T))
91 Element<algebra::NumericalSemiring, T>
92 (
set, misc::random::generate<T>());
108 Element<algebra::NumericalSemiring, int>
112 return Element<algebra::NumericalSemiring, int>(
set, 0);
116 Element<algebra::NumericalSemiring, int>
123 return Element<algebra::NumericalSemiring, int>(
set, r);
129 template <
typename T>
130 void op_in_mul(
const algebra::NumericalSemiring&,
bool& dst,
bool src)
135 inline bool op_mul(
const algebra::NumericalSemiring&,
bool a,
bool b)
140 inline void op_in_add(
const algebra::NumericalSemiring&,
146 inline bool op_add(
const algebra::NumericalSemiring&,
bool a,
bool b)
151 inline bool identity_value(
SELECTOR(algebra::NumericalSemiring),
157 inline bool zero_value(
SELECTOR(algebra::NumericalSemiring),
163 inline bool op_starable(
const algebra::NumericalSemiring&,
bool)
168 inline void op_in_star(
const algebra::NumericalSemiring&,
bool& b)
174 Element<algebra::NumericalSemiring, bool>
178 return op_choose(
set,
SELECT(
bool));
182 Element<algebra::NumericalSemiring, bool>
186 assertion_(
false,
"Cannot choose non-starable boolean: all boolean are starable.");
187 return Element<algebra::NumericalSemiring, bool>(
set,
false);
194 inline float op_sub(
const algebra::NumericalSemiring&,
const float& a,
const float& b)
199 inline double op_sub(
const algebra::NumericalSemiring&,
const double& a,
const double& b)
204 inline float op_div(
const algebra::NumericalSemiring&,
const float& a,
const float& b)
209 inline double op_div(
const algebra::NumericalSemiring&,
const double& a,
const double& b)
214 inline bool op_eq(
const algebra::NumericalSemiring&,
float& a,
float& b)
216 return std::abs(a - b) < 0.00001;
219 inline bool op_eq(
const algebra::NumericalSemiring&,
double& a,
double& b)
221 return std::abs(a - b) < 0.00001;
225 bool op_starable(
const algebra::NumericalSemiring&, T v)
230 inline bool op_starable(
const algebra::NumericalSemiring&,
233 return (-1.0 < f) && (f < 1.0);
236 inline bool op_starable(
const algebra::NumericalSemiring&,
239 return (-1.0 < f) && (f < 1.0);
242 inline void op_in_star(
const algebra::NumericalSemiring&,
float& f)
244 static_assertion(misc::limits<float>::has_infinity,
247 f = (1.0 / (1.0 - f));
249 f = misc::limits<float>::infinity();
252 inline void op_in_star(
const algebra::NumericalSemiring&,
double& f)
255 f = (1.0 / (1.0 - f));
257 assertion(!
"star not defined.");
270 Element<algebra::NumericalSemiring, float>
279 Element<algebra::NumericalSemiring, float>
284 Element<algebra::NumericalSemiring, float>
289 while (op_starable(
set, res))
291 return Element<algebra::NumericalSemiring, float>(
set, res);
304 Element<algebra::NumericalSemiring, double>
313 Element<algebra::NumericalSemiring, double>
318 Element<algebra::NumericalSemiring, double>
323 while (op_starable(
set, res))
325 return Element<algebra::NumericalSemiring, double>(
set, res);
335 inline RationalNumber
336 op_sub(
const algebra::NumericalSemiring&,
const RationalNumber& a,
const RationalNumber& b)
341 inline RationalNumber
342 op_div(
const algebra::NumericalSemiring&,
const RationalNumber& a,
const RationalNumber& b)
348 inline algebra::RationalNumber
349 identity_value(
SELECTOR(algebra::NumericalSemiring),
352 return algebra::RationalNumber(1);
355 inline algebra::RationalNumber
356 zero_value(
SELECTOR(algebra::NumericalSemiring),
359 return algebra::RationalNumber(0);
363 op_starable(
const algebra::NumericalSemiring&,
364 const algebra::RationalNumber& r)
366 return std::abs(r.num_get()) < r.den_get ();
370 op_starable(
const algebra::NumericalSemiring&,
371 algebra::RationalNumber& r)
373 return std::abs(r.num_get()) < r.den_get ();
376 inline void op_in_star(
const algebra::NumericalSemiring&,
377 algebra::RationalNumber& r)
379 if (std::abs(r.num_get()) < r.den_get ())
381 algebra::RationalNumber one = algebra::RationalNumber(1);
385 assertion(!
"vcsn::algebra::RationalNumber: star not defined.");
398 Element<algebra::NumericalSemiring, algebra::RationalNumber>
402 algebra::RationalNumber min = algebra::RationalNumber(-1, 1);
403 algebra::RationalNumber max = algebra::RationalNumber(1, 1);
405 Element<algebra::NumericalSemiring, algebra::RationalNumber>
406 (
set, misc::random::generate<algebra::RationalNumber>(min, max));
410 Element<algebra::NumericalSemiring, algebra::RationalNumber>
414 algebra::RationalNumber res;
417 res = misc::random::generate<algebra::RationalNumber> ();
419 while (res.den_get () >= std::abs(res.num_get ()));
420 return Element<algebra::NumericalSemiring, algebra::RationalNumber> (
set, res);
427 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_NUMERICAL_SEMIRING_HXX