My Project  debian-1:4.1.1-p2+ds-4
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 }
IntRandom::~IntRandom
~IntRandom()
Definition: cf_random.cc:89
ranGen
RandomGenerator ranGen
Definition: cf_random.cc:54
IntRandom::IntRandom
IntRandom()
Definition: cf_random.cc:79
FFRandom
generate random elements in F_p
Definition: cf_random.h:43
RandomGenerator
Definition: cf_random.cc:17
k
int k
Definition: cfEzgcd.cc:92
result
return result
Definition: facAbsBiFact.cc:76
AlgExtRandomF::gen
CFRandom * gen
Definition: cf_random.h:73
AlgExtRandomF::algext
Variable algext
Definition: cf_random.h:72
IntRandom::clone
CFRandom * clone() const
Definition: cf_random.cc:96
RandomGenerator::seedInit
void seedInit(int ss)
Definition: cf_random.cc:23
AlgExtRandomF::AlgExtRandomF
AlgExtRandomF()
Definition: cf_random.cc:101
RandomGenerator::iq
const int iq
Definition: cf_random.cc:19
power
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
Definition: canonicalform.cc:1837
AlgExtRandomF
generate random elements in F_p(alpha)
Definition: cf_random.h:70
GFRandom::generate
CanonicalForm generate() const
Definition: cf_random.cc:66
g
g
Definition: cfModGcd.cc:4031
gf_q1
int gf_q1
Definition: gfops.cc:50
GFRandom::GFRandom
GFRandom()
Definition: cf_random.h:34
RandomGenerator::RandomGenerator
RandomGenerator()
Definition: cf_random.cc:32
cf_random.h
getCharacteristic
int getCharacteristic()
Definition: cf_char.cc:51
CanonicalForm
factory's main class
Definition: canonicalform.h:77
factoryseed
void factoryseed(int s)
random seed initializer
Definition: cf_random.cc:176
i
int i
Definition: cfEzgcd.cc:125
getMipo
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition: variable.cc:207
IntRandom::generate
CanonicalForm generate() const
Definition: cf_random.cc:91
CFRandom
virtual class for random element generation
Definition: cf_random.h:21
ASSERT
#define ASSERT(expression, message)
Definition: cf_assert.h:99
IntRandom
generate random integers
Definition: cf_random.h:55
AlgExtRandomF::operator=
AlgExtRandomF & operator=(const AlgExtRandomF &)
Definition: cf_random.cc:111
cf_defs.h
RandomGenerator::ir
const int ir
Definition: cf_random.cc:19
AlgExtRandomF::n
int n
Definition: cf_random.h:74
FFRandom::clone
CFRandom * clone() const
Definition: cf_random.cc:61
gfops.h
gf_q
int gf_q
Definition: gfops.cc:47
Variable::level
int level() const
Definition: factory.h:134
imm.h
GFRandom
generate random elements in GF
Definition: cf_random.h:31
RandomGenerator::deflt
const int deflt
Definition: cf_random.cc:19
CFRandom::clone
virtual CFRandom * clone() const
Definition: cf_random.h:25
getGFDegree
int getGFDegree()
Definition: cf_char.cc:56
RandomGenerator::s
int s
Definition: cf_random.cc:20
CFRandom::generate
virtual CanonicalForm generate() const
Definition: cf_random.h:24
AlgExtRandomF::clone
CFRandom * clone() const
Definition: cf_random.cc:153
RandomGenerator::ia
const int ia
Definition: cf_random.cc:19
Variable
factory's class for variables
Definition: factory.h:117
factoryrandom
int factoryrandom(int n)
random integers with abs less than n
Definition: cf_random.cc:168
int2imm_p
InternalCF * int2imm_p(long i)
Definition: imm.h:101
RandomGenerator::seed
void seed(int ss)
Definition: cf_random.cc:29
FFRandom::FFRandom
FFRandom()
Definition: cf_random.h:46
IntRandom::max
int max
Definition: cf_random.h:58
GFRandom::clone
CFRandom * clone() const
Definition: cf_random.cc:73
FFRandom::generate
CanonicalForm generate() const
Definition: cf_random.cc:56
m
int m
Definition: cfEzgcd.cc:121
ff_prime
int ff_prime
Definition: ffops.cc:14
CFRandomFactory::generate
static CFRandom * generate()
Definition: cf_random.cc:158
RandomGenerator::im
const int im
Definition: cf_random.cc:19
cf_assert.h
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
RandomGenerator::~RandomGenerator
~RandomGenerator()
Definition: cf_random.cc:27
AlgExtRandomF::generate
CanonicalForm generate() const
Definition: cf_random.cc:145
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
int2imm_gf
InternalCF * int2imm_gf(long i)
Definition: imm.h:106
AlgExtRandomF::~AlgExtRandomF
~AlgExtRandomF()
Definition: cf_random.cc:140
degree
int degree(const CanonicalForm &f)
Definition: canonicalform.h:309
RandomGenerator::generate
int generate()
Definition: cf_random.cc:43
ffops.h