source: git/factory/cf_random.cc @ 341696

spielwiese
Last change on this file since 341696 was 341696, checked in by Hans Schönemann <hannes@…>, 14 years ago
Adding Id property to all files git-svn-id: file:///usr/local/Singular/svn/trunk@12231 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.2 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id$ */
3
4#include <config.h>
5
6#include <time.h>
7
8#include "assert.h"
9
10#include "cf_defs.h"
11#include "cf_random.h"
12#include "ffops.h"
13#include "gfops.h"
14#include "imm.h"
15
16class RandomGenerator {
17private:
18    const int ia, im, iq, ir, deflt;
19    int s;
20
21    // s must not equal zero!
22    void seedInit( int ss ) { s = ((ss == 0) ? deflt : ss); }
23public:
24    RandomGenerator();
25    RandomGenerator( int ss );
26    ~RandomGenerator() {}
27    int generate();
28    void seed( int ss ) { seedInit( ss ); }
29};
30
31RandomGenerator::RandomGenerator() : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
32{
33  seedInit( (int)time( 0 ) );
34}
35
36RandomGenerator::RandomGenerator( int ss ) : ia(16807), im(2147483647), iq(127773), ir(2836), deflt(123459876)
37{
38  seedInit( ss );
39}
40
41int
42RandomGenerator::generate()
43{
44    int k;
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;
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{
67    return CanonicalForm( int2imm_gf( factoryrandom( gf_q ) ) );
68}
69
70CFRandom * GFRandom::clone () const
71{
72    return new GFRandom();
73}
74
75
76IntRandom::IntRandom()
77{
78    max = 50;
79}
80
81IntRandom::IntRandom( int m )
82{
83    max = m;
84}
85
86IntRandom::~IntRandom() {}
87
88CanonicalForm IntRandom::generate() const
89{
90    return factoryrandom( 2*max )-max;
91}
92
93CFRandom * IntRandom::clone() const
94{
95    return new IntRandom( max );
96}
97
98AlgExtRandomF::AlgExtRandomF()
99{
100    ASSERT( 0, "not a valid random generator" );
101}
102
103AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & )
104{
105    ASSERT( 0, "not a valid random generator" );
106}
107
108AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & )
109{
110    ASSERT( 0, "not a valid random generator" );
111    return *this;
112}
113
114AlgExtRandomF::AlgExtRandomF( const Variable & v )
115{
116    ASSERT( v.level() < 0, "not an algebraic extension" );
117    algext = v;
118    n = degree( getMipo( v ) );
119    gen = CFRandomFactory::generate();
120}
121
122AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 )
123{
124    ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
125    algext = v2;
126    n = degree( getMipo( v2 ) );
127    gen = new AlgExtRandomF( v1 );
128}
129
130AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn )
131{
132    algext = v;
133    n = nn;
134    gen = g;
135}
136
137AlgExtRandomF::~AlgExtRandomF()
138{
139    delete gen;
140}
141
142CanonicalForm AlgExtRandomF::generate() const
143{
144    CanonicalForm result;
145    for ( int i = 0; i < n; i++ )
146        result += power( algext, i ) * gen->generate();
147    return result;
148};
149
150CFRandom * AlgExtRandomF::clone () const
151{
152    return new AlgExtRandomF( algext, gen->clone(), n );
153}
154
155CFRandom * CFRandomFactory::generate()
156{
157    if ( getCharacteristic() == 0 )
158        return new IntRandom();
159    if ( getGFDegree() > 1 )
160        return new GFRandom();
161    else
162        return new FFRandom();
163}
164
165int factoryrandom( int n )
166{
167    if ( n == 0 )
168        return (int)ranGen.generate();
169    else
170        return ranGen.generate() % n;
171}
172
173void factoryseed ( int s )
174{
175    ranGen.seed( s );
176}
Note: See TracBrowser for help on using the repository browser.