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 
18 private:
19  const int ia, im, iq, ir, deflt;
20  int s;
21 
22  // s must not equal zero!
23  void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
24 public:
26  RandomGenerator( int ss );
28  int generate();
29  void seed( int ss ) { seedInit( ss ); }
30 };
31 
32 RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
33 {
34  seedInit( (int)time( 0 ) );
35 }
36 
37 RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
38 {
39  seedInit( ss );
40 }
41 
42 int
44 {
45  int k;
46 
47  k = s/iq;
48  s = ia*(s-k*iq)-ir*k;
49  if ( s < 0 ) s += im;
50 
51  return s;
52 }
53 
55 
57 {
59 }
60 
62 {
63  return new FFRandom();
64 }
65 
67 {
68  int i= factoryrandom( gf_q );
69  if ( i == gf_q1 ) i++;
70  return CanonicalForm( int2imm_gf( i ) );
71 }
72 
74 {
75  return new GFRandom();
76 }
77 
78 
80 {
81  max = 50;
82 }
83 
85 {
86  max = m;
87 }
88 
90 
92 {
93  return factoryrandom( 2*max )-max;
94 }
95 
97 {
98  return new IntRandom( max );
99 }
100 
102 {
103  ASSERT( 0, "not a valid random generator" );
104 }
105 
107 {
108  ASSERT( 0, "not a valid random generator" );
109 }
110 
112 {
113  ASSERT( 0, "not a valid random generator" );
114  return *this;
115 }
116 
118 {
119  ASSERT( v.level() < 0, "not an algebraic extension" );
120  algext = v;
121  n = degree( getMipo( v ) );
123 }
124 
126 {
127  ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
128  algext = v2;
129  n = degree( getMipo( v2 ) );
130  gen = new AlgExtRandomF( v1 );
131 }
132 
134 {
135  algext = v;
136  n = nn;
137  gen = g;
138 }
139 
141 {
142  delete gen;
143 }
144 
146 {
148  for ( int i = 0; i < n; i++ )
149  result += power( algext, i ) * gen->generate();
150  return result;
151 }
152 
154 {
155  return new AlgExtRandomF( algext, gen->clone(), n );
156 }
157 
159 {
160  if ( getCharacteristic() == 0 )
161  return new IntRandom();
162  if ( getGFDegree() > 1 )
163  return new GFRandom();
164  else
165  return new FFRandom();
166 }
167 
168 int factoryrandom( int n )
169 {
170  if ( n == 0 )
171  return (int)ranGen.generate();
172  else
173  return ranGen.generate() % n;
174 }
175 
176 void factoryseed ( int s )
177 {
178  ranGen.seed( s );
179 }
virtual CanonicalForm generate() const
Definition: cf_random.h:24
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
CanonicalForm generate() const
Definition: cf_random.cc:145
CanonicalForm generate() const
Definition: cf_random.cc:66
const CanonicalForm int s
Definition: facAbsFact.cc:55
int level() const
Definition: variable.h:49
generate random elements in GF
Definition: cf_random.h:31
void factoryseed(int s)
random seed initializer
Definition: cf_random.cc:176
void seed(int ss)
Definition: cf_random.cc:29
int gf_q1
Definition: gfops.cc:50
int factoryrandom(int n)
random integers with abs less than n
Definition: cf_random.cc:168
FFRandom()
Definition: cf_random.h:46
factory's class for variables
Definition: variable.h:32
CanonicalForm generate() const
Definition: cf_random.cc:56
const CanonicalForm CFMap CFMap int &both_non_zero int n
Definition: cfEzgcd.cc:52
factory's main class
Definition: canonicalform.h:75
assertions for Factory
generate random integers
Definition: cf_random.h:55
g
Definition: cfModGcd.cc:4031
int k
Definition: cfEzgcd.cc:93
RandomGenerator ranGen
Definition: cf_random.cc:54
InternalCF * int2imm_p(long i)
Definition: imm.h:97
AlgExtRandomF & operator=(const AlgExtRandomF &)
Definition: cf_random.cc:111
~IntRandom()
Definition: cf_random.cc:89
int getCharacteristic()
Definition: cf_char.cc:51
CFRandom * clone() const
Definition: cf_random.cc:61
const int ir
Definition: cf_random.cc:19
int gf_q
Definition: gfops.cc:47
int max
Definition: cf_random.h:58
GFRandom()
Definition: cf_random.h:34
CanonicalForm generate() const
Definition: cf_random.cc:91
virtual CFRandom * clone() const
Definition: cf_random.h:25
Operations in GF, where GF is a finite field of size less than 2^16 represented by a root of Conway p...
const int deflt
Definition: cf_random.cc:19
void seedInit(int ss)
Definition: cf_random.cc:23
const int iq
Definition: cf_random.cc:19
const int ia
Definition: cf_random.cc:19
int m
Definition: cfEzgcd.cc:119
InternalCF * int2imm_gf(long i)
Definition: imm.h:102
generate random elements in F_p
Definition: cf_random.h:43
int i
Definition: cfEzgcd.cc:123
factory switches.
generate random integers, random elements of finite fields
static CFRandom * generate()
Definition: cf_random.cc:158
Variable algext
Definition: cf_random.h:72
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
CFRandom * clone() const
Definition: cf_random.cc:153
const int im
Definition: cf_random.cc:19
CFRandom * gen
Definition: cf_random.h:73
generate random elements in F_p(alpha)
Definition: cf_random.h:70
int getGFDegree()
Definition: cf_char.cc:56
operations on immediates, that is elements of F_p, GF, Z, Q that fit into intrinsic int...
virtual class for random element generation
Definition: cf_random.h:21
#define ASSERT(expression, message)
Definition: cf_assert.h:99
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition: variable.cc:207
int degree(const CanonicalForm &f)
int ff_prime
Definition: ffops.cc:14
operations in a finite prime field F_p.
CFRandom * clone() const
Definition: cf_random.cc:96
return result
Definition: facAbsBiFact.cc:76
CFRandom * clone() const
Definition: cf_random.cc:73