Loading...
Searching...
No Matches
cf_random.cc
Go to the documentation of this file.
/* emacs edit mode for this file is -*- C++ -*- */
#include "config.h"
#include <time.h>
#include "cf_assert.h"
#include "cf_defs.h"
#include "cf_random.h"
#include "ffops.h"
#include "gfops.h"
#include "imm.h"
#ifdef HAVE_FLINT
#ifndef __GMP_BITS_PER_MP_LIMB
#define __GMP_BITS_PER_MP_LIMB GMP_LIMB_BITS
#endif
#include <flint/flint.h>
GLOBAL_VAR flint_rand_t FLINTrandom;
#endif
class RandomGenerator {
private:
const int ia, im, iq, ir, deflt;
int s;
// s must not equal zero!
void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
public:
RandomGenerator();
RandomGenerator( int ss );
~RandomGenerator() {}
int generate();
void seed( int ss ) { seedInit( ss ); }
};
RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
{
seedInit( (int)time( 0 ) );
}
RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
{
seedInit( ss );
}
int
RandomGenerator::generate()
{
int k;
k = s/iq;
s = ia*(s-k*iq)-ir*k;
if ( s < 0 ) s += im;
return s;
}
INST_VAR RandomGenerator ranGen;
CanonicalForm FFRandom::generate () const
{
return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) );
}
CFRandom * FFRandom::clone () const
{
return new FFRandom();
}
CanonicalForm GFRandom::generate () const
{
int i= factoryrandom( gf_q );
if ( i == gf_q1 ) i++;
return CanonicalForm( int2imm_gf( i ) );
}
CFRandom * GFRandom::clone () const
{
return new GFRandom();
}
IntRandom::IntRandom()
{
max = 50;
}
IntRandom::IntRandom( int m )
{
max = m;
}
IntRandom::~IntRandom() {}
CanonicalForm IntRandom::generate() const
{
return factoryrandom( 2*max )-max;
}
CFRandom * IntRandom::clone() const
{
return new IntRandom( max );
}
AlgExtRandomF::AlgExtRandomF()
{
ASSERT( 0, "not a valid random generator" );
}
AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & )
{
ASSERT( 0, "not a valid random generator" );
}
AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & )
{
ASSERT( 0, "not a valid random generator" );
return *this;
}
AlgExtRandomF::AlgExtRandomF( const Variable & v )
{
ASSERT( v.level() < 0, "not an algebraic extension" );
algext = v;
n = degree( getMipo( v ) );
gen = CFRandomFactory::generate();
}
AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 )
{
ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
algext = v2;
n = degree( getMipo( v2 ) );
gen = new AlgExtRandomF( v1 );
}
AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn )
{
algext = v;
n = nn;
gen = g;
}
AlgExtRandomF::~AlgExtRandomF()
{
delete gen;
}
CanonicalForm AlgExtRandomF::generate() const
{
CanonicalForm result;
for ( int i = 0; i < n; i++ )
result += power( algext, i ) * gen->generate();
return result;
}
CFRandom * AlgExtRandomF::clone () const
{
return new AlgExtRandomF( algext, gen->clone(), n );
}
CFRandom * CFRandomFactory::generate()
{
if ( getCharacteristic() == 0 )
return new IntRandom();
if ( getGFDegree() > 1 )
return new GFRandom();
else
return new FFRandom();
}
int factoryrandom( int n )
{
if ( n == 0 )
return (int)ranGen.generate();
else
return ranGen.generate() % n;
}
void factoryseed ( int s )
{
ranGen.seed( s );
#ifdef HAVE_FLINT
#if (__FLINT_RELEASE>=30300)
flint_rand_init(FLINTrandom);
#else
flint_randinit(FLINTrandom);
#endif
#endif
}
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