My Project
cf_random.cc
Go to the documentation of this file.
1 /* emacs edit mode for this file is -*- C++ -*- */
2 
3 
4 #include "config.h"
5 
6 
7 #include <time.h>
8 
9 #include "cf_assert.h"
10 
11 #include "cf_defs.h"
12 #include "cf_random.h"
13 #include "ffops.h"
14 #include "gfops.h"
15 #include "imm.h"
16 
17 #ifdef HAVE_FLINT
18 extern "C"
19 {
20 #ifndef __GMP_BITS_PER_MP_LIMB
21 #define __GMP_BITS_PER_MP_LIMB GMP_LIMB_BITS
22 #endif
23 #include <flint/flint.h>
24 
25 GLOBAL_VAR flint_rand_t FLINTrandom;
26 }
27 #endif
28 
30 private:
31  const int ia, im, iq, ir, deflt;
32  int s;
33 
34  // s must not equal zero!
35  void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
36 public:
38  RandomGenerator( int ss );
40  int generate();
41  void seed( int ss ) { seedInit( ss ); }
42 };
43 
44 RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
45 {
46  seedInit( (int)time( 0 ) );
47 }
48 
49 RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
50 {
51  seedInit( ss );
52 }
53 
54 int
56 {
57  int k;
58 
59  k = s/iq;
60  s = ia*(s-k*iq)-ir*k;
61  if ( s < 0 ) s += im;
62 
63  return s;
64 }
65 
67 
69 {
71 }
72 
74 {
75  return new FFRandom();
76 }
77 
79 {
80  int i= factoryrandom( gf_q );
81  if ( i == gf_q1 ) i++;
82  return CanonicalForm( int2imm_gf( i ) );
83 }
84 
86 {
87  return new GFRandom();
88 }
89 
90 
92 {
93  max = 50;
94 }
95 
97 {
98  max = m;
99 }
100 
102 
104 {
105  return factoryrandom( 2*max )-max;
106 }
107 
109 {
110  return new IntRandom( max );
111 }
112 
114 {
115  ASSERT( 0, "not a valid random generator" );
116 }
117 
119 {
120  ASSERT( 0, "not a valid random generator" );
121 }
122 
124 {
125  ASSERT( 0, "not a valid random generator" );
126  return *this;
127 }
128 
130 {
131  ASSERT( v.level() < 0, "not an algebraic extension" );
132  algext = v;
133  n = degree( getMipo( v ) );
135 }
136 
138 {
139  ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
140  algext = v2;
141  n = degree( getMipo( v2 ) );
142  gen = new AlgExtRandomF( v1 );
143 }
144 
146 {
147  algext = v;
148  n = nn;
149  gen = g;
150 }
151 
153 {
154  delete gen;
155 }
156 
158 {
160  for ( int i = 0; i < n; i++ )
161  result += power( algext, i ) * gen->generate();
162  return result;
163 }
164 
166 {
167  return new AlgExtRandomF( algext, gen->clone(), n );
168 }
169 
171 {
172  if ( getCharacteristic() == 0 )
173  return new IntRandom();
174  if ( getGFDegree() > 1 )
175  return new GFRandom();
176  else
177  return new FFRandom();
178 }
179 
180 int factoryrandom( int n )
181 {
182  if ( n == 0 )
183  return (int)ranGen.generate();
184  else
185  return ranGen.generate() % n;
186 }
187 
188 
189 void factoryseed ( int s )
190 {
191  ranGen.seed( s );
192 
193 #ifdef HAVE_FLINT
194  flint_randinit(FLINTrandom);
195 #endif
196 }
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
int degree(const CanonicalForm &f)
int getGFDegree()
Definition: cf_char.cc:75
int FACTORY_PUBLIC getCharacteristic()
Definition: cf_char.cc:70
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
int k
Definition: cfEzgcd.cc:99
g
Definition: cfModGcd.cc:4092
assertions for Factory
#define ASSERT(expression, message)
Definition: cf_assert.h:99
factory switches.
INST_VAR RandomGenerator ranGen
Definition: cf_random.cc:66
int factoryrandom(int n)
random integers with abs less than n
Definition: cf_random.cc:180
void factoryseed(int s)
random seed initializer
Definition: cf_random.cc:189
GLOBAL_VAR flint_rand_t FLINTrandom
Definition: cf_random.cc:25
generate random integers, random elements of finite fields
generate random elements in F_p(alpha)
Definition: cf_random.h:70
CFRandom * clone() const
Definition: cf_random.cc:165
CFRandom * gen
Definition: cf_random.h:73
Variable algext
Definition: cf_random.h:72
AlgExtRandomF & operator=(const AlgExtRandomF &)
Definition: cf_random.cc:123
CanonicalForm generate() const
Definition: cf_random.cc:157
static CFRandom * generate()
Definition: cf_random.cc:170
virtual class for random element generation
Definition: cf_random.h:21
virtual CanonicalForm generate() const
Definition: cf_random.h:24
virtual CFRandom * clone() const
Definition: cf_random.h:25
factory's main class
Definition: canonicalform.h:86
generate random elements in F_p
Definition: cf_random.h:44
CanonicalForm generate() const
Definition: cf_random.cc:68
CFRandom * clone() const
Definition: cf_random.cc:73
FFRandom()
Definition: cf_random.h:46
generate random elements in GF
Definition: cf_random.h:32
CFRandom * clone() const
Definition: cf_random.cc:85
CanonicalForm generate() const
Definition: cf_random.cc:78
GFRandom()
Definition: cf_random.h:34
generate random integers
Definition: cf_random.h:56
int max
Definition: cf_random.h:58
CanonicalForm generate() const
Definition: cf_random.cc:103
CFRandom * clone() const
Definition: cf_random.cc:108
const int deflt
Definition: cf_random.cc:31
const int ia
Definition: cf_random.cc:31
const int ir
Definition: cf_random.cc:31
void seed(int ss)
Definition: cf_random.cc:41
const int im
Definition: cf_random.cc:31
const int iq
Definition: cf_random.cc:31
void seedInit(int ss)
Definition: cf_random.cc:35
factory's class for variables
Definition: factory.h:134
int level() const
Definition: factory.h:150
return result
Definition: facAbsBiFact.cc:75
const CanonicalForm int s
Definition: facAbsFact.cc:51
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition: variable.cc:207
VAR int ff_prime
Definition: ffops.cc:14
operations in a finite prime field F_p.
VAR int gf_q
Definition: gfops.cc:47
VAR int gf_q1
Definition: gfops.cc:50
Operations in GF, where GF is a finite field of size less than 2^16 represented by a root of Conway p...
#define GLOBAL_VAR
Definition: globaldefs.h:11
#define INST_VAR
Definition: globaldefs.h:8
operations on immediates, that is elements of F_p, GF, Z, Q that fit into intrinsic int,...
InternalCF * int2imm_p(long i)
Definition: imm.h:101
InternalCF * int2imm_gf(long i)
Definition: imm.h:106