My Project
Toggle main menu visibility
Loading...
Searching...
No Matches
factory
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
23
GLOBAL_VAR
flint_rand_t
FLINTrandom
;
24
#endif
25
26
class
RandomGenerator
{
27
private
:
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); }
33
public
:
34
RandomGenerator
();
35
RandomGenerator
(
int
ss );
36
~RandomGenerator
() {}
37
int
generate
();
38
void
seed
(
int
ss ) {
seedInit
( ss ); }
39
};
40
41
RandomGenerator::RandomGenerator
() :
ia
(16807),
im
(2147483647),
iq
(127773),
ir
(2836),
deflt
(123459876)
42
{
43
seedInit
( (
int
)time( 0 ) );
44
}
45
46
RandomGenerator::RandomGenerator
(
int
ss ) :
ia
(16807),
im
(2147483647),
iq
(127773),
ir
(2836),
deflt
(123459876)
47
{
48
seedInit
( ss );
49
}
50
51
int
52
RandomGenerator::generate
()
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
63
INST_VAR
RandomGenerator
ranGen
;
64
65
CanonicalForm
FFRandom::generate
()
const
66
{
67
return
CanonicalForm
(
int2imm_p
(
factoryrandom
(
ff_prime
) ) );
68
}
69
70
CFRandom
*
FFRandom::clone
()
const
71
{
72
return
new
FFRandom
();
73
}
74
75
CanonicalForm
GFRandom::generate
()
const
76
{
77
int
i
=
factoryrandom
(
gf_q
);
78
if
(
i
==
gf_q1
)
i
++;
79
return
CanonicalForm
(
int2imm_gf
(
i
) );
80
}
81
82
CFRandom
*
GFRandom::clone
()
const
83
{
84
return
new
GFRandom
();
85
}
86
87
88
IntRandom::IntRandom
()
89
{
90
max
= 50;
91
}
92
93
IntRandom::IntRandom
(
int
m
)
94
{
95
max
=
m
;
96
}
97
98
IntRandom::~IntRandom
() {}
99
100
CanonicalForm
IntRandom::generate
()
const
101
{
102
return
factoryrandom
( 2*
max
)-
max
;
103
}
104
105
CFRandom
*
IntRandom::clone
()
const
106
{
107
return
new
IntRandom
(
max
);
108
}
109
110
AlgExtRandomF::AlgExtRandomF
()
111
{
112
ASSERT
( 0,
"not a valid random generator"
);
113
}
114
115
AlgExtRandomF::AlgExtRandomF
(
const
AlgExtRandomF
& )
116
{
117
ASSERT
( 0,
"not a valid random generator"
);
118
}
119
120
AlgExtRandomF
&
AlgExtRandomF::operator=
(
const
AlgExtRandomF
& )
121
{
122
ASSERT
( 0,
"not a valid random generator"
);
123
return
*
this
;
124
}
125
126
AlgExtRandomF::AlgExtRandomF
(
const
Variable
&
v
)
127
{
128
ASSERT
(
v
.level() < 0,
"not an algebraic extension"
);
129
algext
=
v
;
130
n
=
degree
(
getMipo
(
v
) );
131
gen
=
CFRandomFactory::generate
();
132
}
133
134
AlgExtRandomF::AlgExtRandomF
(
const
Variable
& v1,
const
Variable
& v2 )
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
142
AlgExtRandomF::AlgExtRandomF
(
const
Variable
&
v
,
CFRandom
*
g
,
int
nn )
143
{
144
algext
=
v
;
145
n
= nn;
146
gen
=
g
;
147
}
148
149
AlgExtRandomF::~AlgExtRandomF
()
150
{
151
delete
gen
;
152
}
153
154
CanonicalForm
AlgExtRandomF::generate
()
const
155
{
156
CanonicalForm
result
;
157
for
(
int
i
= 0;
i
<
n
;
i
++ )
158
result
+=
power
(
algext
,
i
) *
gen
->generate();
159
return
result
;
160
}
161
162
CFRandom
*
AlgExtRandomF::clone
()
const
163
{
164
return
new
AlgExtRandomF
(
algext
,
gen
->clone(),
n
);
165
}
166
167
CFRandom
*
CFRandomFactory::generate
()
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
177
int
factoryrandom
(
int
n )
178
{
179
if
( n == 0 )
180
return
(
int
)
ranGen
.generate();
181
else
182
return
ranGen
.generate() % n;
183
}
184
185
186
void
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
}
m
int m
Definition
cfEzgcd.cc:128
i
int i
Definition
cfEzgcd.cc:132
k
int k
Definition
cfEzgcd.cc:99
g
g
Definition
cfModGcd.cc:4098
cf_assert.h
assertions for Factory
ASSERT
#define ASSERT(expression, message)
Definition
cf_assert.h:99
cf_defs.h
factory switches.
ranGen
INST_VAR RandomGenerator ranGen
Definition
cf_random.cc:63
factoryrandom
int factoryrandom(int n)
random integers with abs less than n
Definition
cf_random.cc:177
factoryseed
void factoryseed(int s)
random seed initializer
Definition
cf_random.cc:186
FLINTrandom
GLOBAL_VAR flint_rand_t FLINTrandom
Definition
cf_random.cc:23
cf_random.h
generate random integers, random elements of finite fields
AlgExtRandomF::clone
CFRandom * clone() const
Definition
cf_random.cc:162
AlgExtRandomF::gen
CFRandom * gen
Definition
cf_random.h:73
AlgExtRandomF::algext
Variable algext
Definition
cf_random.h:72
AlgExtRandomF::~AlgExtRandomF
~AlgExtRandomF()
Definition
cf_random.cc:149
AlgExtRandomF::operator=
AlgExtRandomF & operator=(const AlgExtRandomF &)
Definition
cf_random.cc:120
AlgExtRandomF::n
int n
Definition
cf_random.h:74
AlgExtRandomF::AlgExtRandomF
AlgExtRandomF()
Definition
cf_random.cc:110
AlgExtRandomF::generate
CanonicalForm generate() const
Definition
cf_random.cc:154
CFRandomFactory::generate
static CFRandom * generate()
Definition
cf_random.cc:167
CFRandom
virtual class for random element generation
Definition
cf_random.h:21
CanonicalForm
factory's main class
Definition
canonicalform.h:86
FFRandom
generate random elements in F_p
Definition
cf_random.h:44
FFRandom::generate
CanonicalForm generate() const
Definition
cf_random.cc:65
FFRandom::clone
CFRandom * clone() const
Definition
cf_random.cc:70
FFRandom::FFRandom
FFRandom()
Definition
cf_random.h:46
GFRandom
generate random elements in GF
Definition
cf_random.h:32
GFRandom::clone
CFRandom * clone() const
Definition
cf_random.cc:82
GFRandom::generate
CanonicalForm generate() const
Definition
cf_random.cc:75
GFRandom::GFRandom
GFRandom()
Definition
cf_random.h:34
IntRandom
generate random integers
Definition
cf_random.h:56
IntRandom::IntRandom
IntRandom()
Definition
cf_random.cc:88
IntRandom::max
int max
Definition
cf_random.h:58
IntRandom::generate
CanonicalForm generate() const
Definition
cf_random.cc:100
IntRandom::clone
CFRandom * clone() const
Definition
cf_random.cc:105
IntRandom::~IntRandom
~IntRandom()
Definition
cf_random.cc:98
RandomGenerator
Definition
cf_random.cc:26
RandomGenerator::deflt
const int deflt
Definition
cf_random.cc:28
RandomGenerator::s
int s
Definition
cf_random.cc:29
RandomGenerator::generate
int generate()
Definition
cf_random.cc:52
RandomGenerator::ia
const int ia
Definition
cf_random.cc:28
RandomGenerator::ir
const int ir
Definition
cf_random.cc:28
RandomGenerator::RandomGenerator
RandomGenerator()
Definition
cf_random.cc:41
RandomGenerator::~RandomGenerator
~RandomGenerator()
Definition
cf_random.cc:36
RandomGenerator::seed
void seed(int ss)
Definition
cf_random.cc:38
RandomGenerator::im
const int im
Definition
cf_random.cc:28
RandomGenerator::iq
const int iq
Definition
cf_random.cc:28
RandomGenerator::seedInit
void seedInit(int ss)
Definition
cf_random.cc:32
Variable
factory's class for variables
Definition
factory.h:127
Variable::level
int level() const
Definition
factory.h:143
result
return result
Definition
facAbsBiFact.cc:76
s
const CanonicalForm int s
Definition
facAbsFact.cc:51
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition
facBivar.h:39
ff_prime
VAR int ff_prime
Definition
ffops.cc:23
ffops.h
operations in a finite prime field F_p.
gf_q
VAR int gf_q
Definition
gfops.cc:47
gf_q1
VAR int gf_q1
Definition
gfops.cc:50
gfops.h
Operations in GF, where GF is a finite field of size less than 2^16 represented by a root of Conway p...
GLOBAL_VAR
#define GLOBAL_VAR
Definition
globaldefs.h:11
INST_VAR
#define INST_VAR
Definition
globaldefs.h:8
imm.h
operations on immediates, that is elements of F_p, GF, Z, Q that fit into intrinsic int,...
int2imm_p
InternalCF * int2imm_p(long i)
Definition
imm.h:101
int2imm_gf
InternalCF * int2imm_gf(long i)
Definition
imm.h:106
degree
int degree(const CanonicalForm &f)
Definition
factory.h:457
getGFDegree
int getGFDegree()
Definition
cf_char.cc:75
getMipo
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition
variable.cc:207
power
CanonicalForm FACTORY_PUBLIC power(const CanonicalForm &f, int n)
exponentiation
Definition
canonicalform.cc:1896
factoryrandom
int factoryrandom(int n)
random integers with abs less than n
Definition
cf_random.cc:177
getCharacteristic
int FACTORY_PUBLIC getCharacteristic()
Definition
cf_char.cc:70
Generated on
for My Project by
doxygen 1.17.0
for
Singular