00001
00002
00003
00004
00005
00006
00007
00008
00014 #ifndef __ARAGELI_rand_hpp__
00015 #define __ARAGELI_rand_hpp__
00016
00017 #include <cstdlib>
00018
00019 #include "config.hpp"
00020 #include "exception.hpp"
00021 #include "factory.hpp"
00022 #include "cmp.hpp"
00023 #include "big_int.hpp"
00024 #include "rational.hpp"
00025 #include "vector.hpp"
00026 #include "matrix.hpp"
00027 #include "sparse_polynom.hpp"
00028
00029 namespace Arageli
00030 {
00031
00032
00033
00034
00035
00036 #define ARAGELI_RAND_GENERAL ( std::rand() )
00037 #define ARAGELI_RAND_SHORT ( (unsigned short)ARAGELI_RAND_GENERAL )
00038 #define ARAGELI_RAND_INT ( ((unsigned int)ARAGELI_RAND_SHORT << 16) | ((unsigned int)ARAGELI_RAND_SHORT) )
00039 #define ARAGELI_RAND_LONG ( (unsigned long)ARAGELI_RAND_INT )
00040 #define ARAGELI_RAND_LONG_LONG ( (((unsigned long long)ARAGELI_RAND_LONG) << 32) | ((unsigned long long)ARAGELI_RAND_LONG) )
00041
00042
00043
00044
00045 #define ARAGELI_DEFINE_FUNCTION_T_RAND_T(type, rand_value) \
00046 inline type rand(type maxVal) { return 0==maxVal ? 0 : (((type)rand_value)%(maxVal+1)); }
00047
00048 ARAGELI_DEFINE_FUNCTION_T_RAND_T(char, ARAGELI_RAND_GENERAL);
00049 ARAGELI_DEFINE_FUNCTION_T_RAND_T(unsigned char, ARAGELI_RAND_GENERAL);
00050 ARAGELI_DEFINE_FUNCTION_T_RAND_T(short, ARAGELI_RAND_SHORT);
00051 ARAGELI_DEFINE_FUNCTION_T_RAND_T(unsigned short, ARAGELI_RAND_SHORT);
00052 ARAGELI_DEFINE_FUNCTION_T_RAND_T(int, ARAGELI_RAND_INT);
00053 ARAGELI_DEFINE_FUNCTION_T_RAND_T(unsigned int, ARAGELI_RAND_INT);
00054 ARAGELI_DEFINE_FUNCTION_T_RAND_T(long, ARAGELI_RAND_LONG);
00055 ARAGELI_DEFINE_FUNCTION_T_RAND_T(unsigned long, ARAGELI_RAND_LONG);
00056 #if defined(ARAGELI_LONG_LONG_SUPPORT)
00057 ARAGELI_DEFINE_FUNCTION_T_RAND_T(long long, ARAGELI_RAND_LONG_LONG);
00058 ARAGELI_DEFINE_FUNCTION_T_RAND_T(unsigned long long, ARAGELI_RAND_LONG_LONG);
00059 #endif // ARAGELI_LONG_LONG_SUPPORT
00060
00061 inline big_int rand(big_int maxVal) { return big_int::random_in_range(maxVal); }
00062
00063 template <typename T>
00064 inline rational<T> rand(const rational<T> &maxVal)
00065 {
00066 if (is_unit(maxVal.denominator()))
00067 return rational<T>(rand(maxVal.numerator()));
00068
00069 T q(rand(maxVal.denominator()));
00070 if (is_null(q))
00071 return factory<rational<T> >::null();
00072 return rational<T>(rand(maxVal.numerator()), q);
00073 }
00074
00075 template <typename F, typename I>
00076 inline monom<F, I> rand(const monom<F, I> &maxVal)
00077 {
00078 return monom<F, I>(rand(maxVal.coef()), rand(maxVal.degree()));
00079 }
00080
00081 template <typename F, typename I, bool REFCNT>
00082 inline sparse_polynom<F, I, REFCNT>
00083 rand(const sparse_polynom<F, I, REFCNT> &maxVal)
00084 {
00085 sparse_polynom<F, I, REFCNT> res;
00086 for (typename sparse_polynom<F, I, REFCNT>::monom_const_iterator it = maxVal.monoms_begin();
00087 it != maxVal.monoms_end(); it++)
00088 res += rand(*it);
00089 return res;
00090 }
00091
00092 template <typename T, bool REFCNT>
00093 inline matrix<T, REFCNT> rand(const matrix<T, REFCNT> &maxVal)
00094 {
00095 matrix<T, REFCNT> res(maxVal.rows(), maxVal.cols());
00096 for (typename matrix<T, REFCNT>::size_type i = 0; i < res.rows(); i++)
00097 for (typename matrix<T, REFCNT>::size_type j = 0; j < res.cols(); j++)
00098 res[i] = rand(maxVal.el(i, j));
00099 return res;
00100 }
00101
00102
00103
00104
00105
00106 }
00107
00108 #ifdef ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE
00109 #define ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE_rand
00110
00111 #undef ARAGELI_INCLUDE_CPP_WITH_EXPORT_TEMPLATE_rand
00112 #endif
00113
00114 #endif
00115