source: git/factory/cf_random.cc @ 2dd068

spielwiese
Last change on this file since 2dd068 was 2dd068, checked in by Rüdiger Stobbe <stobbe@…>, 28 years ago
Initial revision git-svn-id: file:///usr/local/Singular/svn/trunk@6 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.4 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: cf_random.cc,v 1.0 1996-05-17 10:59:44 stobbe Exp $
3
4/*
5$Log: not supported by cvs2svn $
6*/
7
8#include <stdlib.h>
9#include <math.h>
10
11#include "assert.h"
12#include "cf_defs.h"
13#include "cf_random.h"
14#include "ffops.h"
15#include "gfops.h"
16#include "imm.h"
17
18
19CanonicalForm FFRandom::generate () const
20{
21    return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) );
22}
23
24CFRandom * FFRandom::clone () const
25{
26    return new FFRandom();
27}
28
29CanonicalForm GFRandom::generate () const
30{
31    return CanonicalForm( int2imm_gf( factoryrandom( gf_q ) ) );
32}
33
34CFRandom * GFRandom::clone () const
35{
36    return new GFRandom();
37}
38
39
40IntRandom::IntRandom()
41{
42    max = 100;
43}
44
45IntRandom::IntRandom( int m )
46{
47    max = m;
48}
49
50IntRandom::~IntRandom() {}
51
52CanonicalForm IntRandom::generate() const
53{
54    return factoryrandom( max );
55}
56 
57CFRandom * IntRandom::clone() const
58{
59    return new IntRandom( max );
60}
61
62AlgExtRandomF::AlgExtRandomF()
63{
64    ASSERT( 0, "not a valid random generator" );
65}
66
67AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & )
68{
69    ASSERT( 0, "not a valid random generator" );
70}
71
72AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & )
73{
74    ASSERT( 0, "not a valid random generator" );
75    return *this;
76}
77
78AlgExtRandomF::AlgExtRandomF( const Variable & v )
79{
80    ASSERT( v.level() < 0, "not an algebraic extension" );
81    algext = v;
82    n = degree( getMipo( v ) );
83    gen = CFRandomFactory::generate();
84}
85
86AlgExtRandomF::AlgExtRandomF( const Variable & v1, const Variable & v2 )
87{
88    ASSERT( v1.level() < 0 && v2.level() < 0 && v1 != v2, "not an algebraic extension" );
89    algext = v2;
90    n = degree( getMipo( v2 ) );
91    gen = new AlgExtRandomF( v1 );
92}
93
94AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn )
95{
96    algext = v;
97    n = nn;
98    gen = g;
99}
100
101AlgExtRandomF::~AlgExtRandomF()
102{
103    delete gen;
104}
105
106CanonicalForm AlgExtRandomF::generate() const
107{
108    CanonicalForm result;
109    for ( int i = 0; i < n; i++ )
110        result += power( algext, i ) * gen->generate();
111    return result;
112};
113
114CFRandom * AlgExtRandomF::clone () const
115{
116    return new AlgExtRandomF( algext, gen->clone(), n );
117}
118
119CFRandom * CFRandomFactory::generate()
120{
121    if ( getCharacteristic() == 0 )
122        return new IntRandom();
123    if ( getGFDegree() > 1 )
124        return new GFRandom();
125    else
126        return new FFRandom();
127}
128
129int factoryrandom( int n )
130{
131    return rand() % n;
132}
133
134void factoryseed ( int s )
135{
136    srand( s );
137}
Note: See TracBrowser for help on using the repository browser.