My Project
Loading...
Searching...
No Matches
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#ifndef __GMP_BITS_PER_MP_LIMB
19#define __GMP_BITS_PER_MP_LIMB GMP_LIMB_BITS
20#endif
21#include <flint/flint.h>
22
24#endif
25
27private:
28 const int ia, im, iq, ir, deflt;
29 int s;
30
31 // s must not equal zero!
32 void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
33public:
35 RandomGenerator( int ss );
37 int generate();
38 void seed( int ss ) { seedInit( ss ); }
39};
40
41RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
42{
43 seedInit( (int)time( 0 ) );
44}
45
46RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
47{
48 seedInit( ss );
49}
50
51int
53{
54 int k;
55
56 k = s/iq;
57 s = ia*(s-k*iq)-ir*k;
58 if ( s < 0 ) s += im;
59
60 return s;
61}
62
64
69
71{
72 return new FFRandom();
73}
74
76{
77 int i= factoryrandom( gf_q );
78 if ( i == gf_q1 ) i++;
79 return CanonicalForm( int2imm_gf( i ) );
80}
81
83{
84 return new GFRandom();
85}
86
87
89{
90 max = 50;
91}
92
94{
95 max = m;
96}
97
99
101{
102 return factoryrandom( 2*max )-max;
103}
104
106{
107 return new IntRandom( max );
108}
109
111{
112 ASSERT( 0, "not a valid random generator" );
113}
114
116{
117 ASSERT( 0, "not a valid random generator" );
118}
119
121{
122 ASSERT( 0, "not a valid random generator" );
123 return *this;
124}
125
127{
128 ASSERT( v.level() < 0, "not an algebraic extension" );
129 algext = v;
130 n = degree( getMipo( v ) );
132}
133
135{
136 ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
137 algext = v2;
138 n = degree( getMipo( v2 ) );
139 gen = new AlgExtRandomF( v1 );
140}
141
143{
144 algext = v;
145 n = nn;
146 gen = g;
147}
148
150{
151 delete gen;
152}
153
155{
157 for ( int i = 0; i < n; i++ )
158 result += power( algext, i ) * gen->generate();
159 return result;
160}
161
163{
164 return new AlgExtRandomF( algext, gen->clone(), n );
165}
166
168{
169 if ( getCharacteristic() == 0 )
170 return new IntRandom();
171 if ( getGFDegree() > 1 )
172 return new GFRandom();
173 else
174 return new FFRandom();
175}
176
177int factoryrandom( int n )
178{
179 if ( n == 0 )
180 return (int)ranGen.generate();
181 else
182 return ranGen.generate() % n;
183}
184
185
186void factoryseed ( int s )
187{
188 ranGen.seed( s );
189
190#ifdef HAVE_FLINT
191#if (__FLINT_RELEASE>=30300)
192 flint_rand_init(FLINTrandom);
193#else
194 flint_randinit(FLINTrandom);
195#endif
196#endif
197}
int m
Definition cfEzgcd.cc:128
int i
Definition cfEzgcd.cc:132
int k
Definition cfEzgcd.cc:99
g
Definition cfModGcd.cc:4098
assertions for Factory
#define ASSERT(expression, message)
Definition cf_assert.h:99
factory switches.
INST_VAR RandomGenerator ranGen
Definition cf_random.cc:63
int factoryrandom(int n)
random integers with abs less than n
Definition cf_random.cc:177
void factoryseed(int s)
random seed initializer
Definition cf_random.cc:186
GLOBAL_VAR flint_rand_t FLINTrandom
Definition cf_random.cc:23
generate random integers, random elements of finite fields
CFRandom * clone() const
Definition cf_random.cc:162
CFRandom * gen
Definition cf_random.h:73
Variable algext
Definition cf_random.h:72
AlgExtRandomF & operator=(const AlgExtRandomF &)
Definition cf_random.cc:120
CanonicalForm generate() const
Definition cf_random.cc:154
static CFRandom * generate()
Definition cf_random.cc:167
virtual class for random element generation
Definition cf_random.h:21
factory's main class
generate random elements in F_p
Definition cf_random.h:44
CanonicalForm generate() const
Definition cf_random.cc:65
CFRandom * clone() const
Definition cf_random.cc:70
generate random elements in GF
Definition cf_random.h:32
CFRandom * clone() const
Definition cf_random.cc:82
CanonicalForm generate() const
Definition cf_random.cc:75
generate random integers
Definition cf_random.h:56
CanonicalForm generate() const
Definition cf_random.cc:100
CFRandom * clone() const
Definition cf_random.cc:105
const int deflt
Definition cf_random.cc:28
const int ia
Definition cf_random.cc:28
const int ir
Definition cf_random.cc:28
void seed(int ss)
Definition cf_random.cc:38
const int im
Definition cf_random.cc:28
const int iq
Definition cf_random.cc:28
void seedInit(int ss)
Definition cf_random.cc:32
factory's class for variables
Definition factory.h:127
int level() const
Definition factory.h:143
return result
const CanonicalForm int s
Definition facAbsFact.cc:51
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
VAR int ff_prime
Definition ffops.cc:23
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
int degree(const CanonicalForm &f)
Definition factory.h:457
int getGFDegree()
Definition cf_char.cc:75
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition variable.cc:207
CanonicalForm FACTORY_PUBLIC power(const CanonicalForm &f, int n)
exponentiation
int factoryrandom(int n)
random integers with abs less than n
Definition cf_random.cc:177
int FACTORY_PUBLIC getCharacteristic()
Definition cf_char.cc:70