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 | |
---|
19 | CanonicalForm FFRandom::generate () const |
---|
20 | { |
---|
21 | return CanonicalForm( int2imm_p( factoryrandom( ff_prime ) ) ); |
---|
22 | } |
---|
23 | |
---|
24 | CFRandom * FFRandom::clone () const |
---|
25 | { |
---|
26 | return new FFRandom(); |
---|
27 | } |
---|
28 | |
---|
29 | CanonicalForm GFRandom::generate () const |
---|
30 | { |
---|
31 | return CanonicalForm( int2imm_gf( factoryrandom( gf_q ) ) ); |
---|
32 | } |
---|
33 | |
---|
34 | CFRandom * GFRandom::clone () const |
---|
35 | { |
---|
36 | return new GFRandom(); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | IntRandom::IntRandom() |
---|
41 | { |
---|
42 | max = 100; |
---|
43 | } |
---|
44 | |
---|
45 | IntRandom::IntRandom( int m ) |
---|
46 | { |
---|
47 | max = m; |
---|
48 | } |
---|
49 | |
---|
50 | IntRandom::~IntRandom() {} |
---|
51 | |
---|
52 | CanonicalForm IntRandom::generate() const |
---|
53 | { |
---|
54 | return factoryrandom( max ); |
---|
55 | } |
---|
56 | |
---|
57 | CFRandom * IntRandom::clone() const |
---|
58 | { |
---|
59 | return new IntRandom( max ); |
---|
60 | } |
---|
61 | |
---|
62 | AlgExtRandomF::AlgExtRandomF() |
---|
63 | { |
---|
64 | ASSERT( 0, "not a valid random generator" ); |
---|
65 | } |
---|
66 | |
---|
67 | AlgExtRandomF::AlgExtRandomF( const AlgExtRandomF & ) |
---|
68 | { |
---|
69 | ASSERT( 0, "not a valid random generator" ); |
---|
70 | } |
---|
71 | |
---|
72 | AlgExtRandomF& AlgExtRandomF::operator= ( const AlgExtRandomF & ) |
---|
73 | { |
---|
74 | ASSERT( 0, "not a valid random generator" ); |
---|
75 | return *this; |
---|
76 | } |
---|
77 | |
---|
78 | AlgExtRandomF::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 | |
---|
86 | AlgExtRandomF::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 | |
---|
94 | AlgExtRandomF::AlgExtRandomF( const Variable & v, CFRandom * g, int nn ) |
---|
95 | { |
---|
96 | algext = v; |
---|
97 | n = nn; |
---|
98 | gen = g; |
---|
99 | } |
---|
100 | |
---|
101 | AlgExtRandomF::~AlgExtRandomF() |
---|
102 | { |
---|
103 | delete gen; |
---|
104 | } |
---|
105 | |
---|
106 | CanonicalForm 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 | |
---|
114 | CFRandom * AlgExtRandomF::clone () const |
---|
115 | { |
---|
116 | return new AlgExtRandomF( algext, gen->clone(), n ); |
---|
117 | } |
---|
118 | |
---|
119 | CFRandom * 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 | |
---|
129 | int factoryrandom( int n ) |
---|
130 | { |
---|
131 | return rand() % n; |
---|
132 | } |
---|
133 | |
---|
134 | void factoryseed ( int s ) |
---|
135 | { |
---|
136 | srand( s ); |
---|
137 | } |
---|