source: git/factory/cf_random.cc @ 3ef2d6

fieker-DuValspielwiese
Last change on this file since 3ef2d6 was e4fe2b, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: Fixed huge BUG in cf_gmp.h CHG: starting to cleanup factory
  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[493c477]1/* emacs edit mode for this file is -*- C++ -*- */
[341696]2/* $Id$ */
[2dd068]3
[e4fe2b]4#include "config.h"
[b973c0]5
[4345a0]6#include <time.h>
[2dd068]7
[650f2d8]8#include "cf_assert.h"
[b973c0]9
[2dd068]10#include "cf_defs.h"
11#include "cf_random.h"
12#include "ffops.h"
13#include "gfops.h"
14#include "imm.h"
15
[4345a0]16class RandomGenerator {
17private:
[f67df3]18    const int ia, im, iq, ir, deflt;
19    int s;
[4345a0]20
21    // s must not equal zero!
[f67df3]22    void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
[4345a0]23public:
[553daf]24    RandomGenerator();
[f67df3]25    RandomGenerator( int ss );
[4345a0]26    ~RandomGenerator() {}
[f67df3]27    int generate();
28    void seed( int ss ) { seedInit( ss ); }
[4345a0]29};
30
[9c9e2a4]31RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
[553daf]32{
[f67df3]33  seedInit( (int)time( 0 ) );
[553daf]34}
35
[f67df3]36RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
[553daf]37{
38  seedInit( ss );
39}
40
[f67df3]41int
[4345a0]42RandomGenerator::generate()
43{
[f67df3]44    int k;
[4345a0]45
46    k = s/iq;
47    s = ia*(s-k*iq)-ir*k;
48    if ( s < 0 ) s += im;
49
50    return s;
51}
52
53RandomGenerator ranGen;
[2dd068]54
55CanonicalForm FFRandom::generate () const
56{
57    return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) );
58}
59
60CFRandom * FFRandom::clone () const
61{
62    return new FFRandom();
63}
64
65CanonicalForm GFRandom::generate () const
66{
[08daea]67    int i= factoryrandom( gf_q );
68    if ( i == gf_q1 ) i++;
69    return CanonicalForm( int2imm_gf( i ) );
[2dd068]70}
71
72CFRandom * GFRandom::clone () const
73{
74    return new GFRandom();
75}
76
77
78IntRandom::IntRandom()
79{
[afd067]80    max = 50;
[2dd068]81}
82
83IntRandom::IntRandom( int m )
84{
85    max = m;
86}
87
88IntRandom::~IntRandom() {}
89
90CanonicalForm IntRandom::generate() const
91{
[afd067]92    return factoryrandom( 2*max )-max;
[2dd068]93}
[b973c0]94
[2dd068]95CFRandom * IntRandom::clone() const
96{
97    return new IntRandom( max );
98}
99
100AlgExtRandomF::AlgExtRandomF()
101{
102    ASSERT( 0, "not a valid random generator" );
103}
104
105AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & )
106{
107    ASSERT( 0, "not a valid random generator" );
108}
109
110AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & )
111{
112    ASSERT( 0, "not a valid random generator" );
113    return *this;
114}
115
116AlgExtRandomF::AlgExtRandomF( const Variable & v )
117{
118    ASSERT( v.level() < 0, "not an algebraic extension" );
119    algext = v;
120    n = degree( getMipo( v ) );
121    gen = CFRandomFactory::generate();
122}
123
124AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 )
125{
126    ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
127    algext = v2;
128    n = degree( getMipo( v2 ) );
129    gen = new AlgExtRandomF( v1 );
130}
131
132AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn )
133{
134    algext = v;
135    n = nn;
136    gen = g;
137}
138
139AlgExtRandomF::~AlgExtRandomF()
140{
141    delete gen;
142}
143
144CanonicalForm AlgExtRandomF::generate() const
145{
146    CanonicalForm result;
147    for ( int i = 0; i < n; i++ )
[806c18]148      result += power( algext, i ) * gen->generate();
[2dd068]149    return result;
[e76d7a6]150}
[2dd068]151
152CFRandom * AlgExtRandomF::clone () const
153{
154    return new AlgExtRandomF( algext, gen->clone(), n );
155}
156
157CFRandom * CFRandomFactory::generate()
158{
159    if ( getCharacteristic() == 0 )
[806c18]160        return new IntRandom();
[2dd068]161    if ( getGFDegree() > 1 )
[806c18]162        return new GFRandom();
[2dd068]163    else
[806c18]164        return new FFRandom();
[2dd068]165}
166
167int factoryrandom( int n )
168{
[3aca31]169    if ( n == 0 )
[806c18]170        return (int)ranGen.generate();
[3aca31]171    else
[806c18]172        return ranGen.generate() % n;
[2dd068]173}
174
175void factoryseed ( int s )
176{
[4345a0]177    ranGen.seed( s );
[2dd068]178}
Note: See TracBrowser for help on using the repository browser.