1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #include "assert.h" |
---|
7 | |
---|
8 | #include "cf_defs.h" |
---|
9 | #include "cf_factory.h" |
---|
10 | #include "canonicalform.h" |
---|
11 | #include "int_cf.h" |
---|
12 | #include "int_int.h" |
---|
13 | #include "int_rat.h" |
---|
14 | #include "int_poly.h" |
---|
15 | #include "int_pp.h" |
---|
16 | #include "imm.h" |
---|
17 | |
---|
18 | int CFFactory::currenttype = IntegerDomain; |
---|
19 | |
---|
20 | void |
---|
21 | CFFactory::settype ( int type ) |
---|
22 | { |
---|
23 | ASSERT( type==FiniteFieldDomain || type==GaloisFieldDomain || type==IntegerDomain || type==RationalDomain || type==PrimePowerDomain, "illegal basic domain!" ); |
---|
24 | currenttype = type; |
---|
25 | } |
---|
26 | |
---|
27 | InternalCF * |
---|
28 | CFFactory::basic ( int value ) |
---|
29 | { |
---|
30 | if ( currenttype == IntegerDomain ) |
---|
31 | if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
32 | return int2imm( value ); |
---|
33 | else |
---|
34 | return new InternalInteger( value ); |
---|
35 | // else if ( currenttype == RationalDomain ) |
---|
36 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
37 | // return int2imm( value ); |
---|
38 | // else |
---|
39 | // return new InternalRational( value ); |
---|
40 | else if ( currenttype == FiniteFieldDomain ) |
---|
41 | return int2imm_p( ff_norm( value ) ); |
---|
42 | else if ( currenttype == GaloisFieldDomain ) |
---|
43 | return int2imm_gf( gf_int2gf( value ) ); |
---|
44 | else if ( currenttype == PrimePowerDomain ) |
---|
45 | return new InternalPrimePower( value ); |
---|
46 | else { |
---|
47 | ASSERT( 0, "illegal basic domain!" ); |
---|
48 | return 0; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | InternalCF * |
---|
53 | CFFactory::basic ( int type, int value ) |
---|
54 | { |
---|
55 | if ( type == IntegerDomain ) |
---|
56 | if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
57 | return int2imm( value ); |
---|
58 | else |
---|
59 | return new InternalInteger( value ); |
---|
60 | // else if ( type == RationalDomain ) |
---|
61 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
62 | // return int2imm( value ); |
---|
63 | // else |
---|
64 | // return new InternalRational( value ); |
---|
65 | else if ( type == FiniteFieldDomain ) |
---|
66 | return int2imm_p( ff_norm( value ) ); |
---|
67 | else if ( type == GaloisFieldDomain ) |
---|
68 | return int2imm_gf( gf_int2gf( value ) ); |
---|
69 | else if ( type == PrimePowerDomain ) |
---|
70 | return new InternalPrimePower( value ); |
---|
71 | else { |
---|
72 | ASSERT1( 0, "illegal basic domain (type = %d)!", type ); |
---|
73 | return 0; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | InternalCF * |
---|
78 | CFFactory::basic ( const char * str ) |
---|
79 | { |
---|
80 | if ( currenttype == IntegerDomain ) { |
---|
81 | InternalInteger * dummy = new InternalInteger( str ); |
---|
82 | if ( dummy->is_imm() ) { |
---|
83 | InternalCF * res = int2imm( dummy->intval() ); |
---|
84 | delete dummy; |
---|
85 | return res; |
---|
86 | } |
---|
87 | else |
---|
88 | return dummy; |
---|
89 | } |
---|
90 | // else if ( currenttype == RationalDomain ) { |
---|
91 | // InternalRational * dummy = new InternalRational( str ); |
---|
92 | // if ( dummy->is_imm() ) { |
---|
93 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
94 | // delete dummy; |
---|
95 | // return res; |
---|
96 | // } |
---|
97 | // else |
---|
98 | // return dummy; |
---|
99 | // } |
---|
100 | else if ( currenttype == FiniteFieldDomain ) { |
---|
101 | InternalInteger * dummy = new InternalInteger( str ); |
---|
102 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
103 | delete dummy; |
---|
104 | return res; |
---|
105 | } |
---|
106 | else if ( currenttype == GaloisFieldDomain ) { |
---|
107 | InternalInteger * dummy = new InternalInteger( str ); |
---|
108 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
109 | delete dummy; |
---|
110 | return res; |
---|
111 | } |
---|
112 | else if ( currenttype == PrimePowerDomain ) |
---|
113 | return new InternalPrimePower( str ); |
---|
114 | else { |
---|
115 | ASSERT( 0, "illegal basic domain!" ); |
---|
116 | return 0; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | InternalCF * |
---|
121 | CFFactory::basic ( const char * str, int base ) |
---|
122 | { |
---|
123 | if ( currenttype == IntegerDomain ) { |
---|
124 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
125 | if ( dummy->is_imm() ) { |
---|
126 | InternalCF * res = int2imm( dummy->intval() ); |
---|
127 | delete dummy; |
---|
128 | return res; |
---|
129 | } |
---|
130 | else |
---|
131 | return dummy; |
---|
132 | } |
---|
133 | // else if ( currenttype == RationalDomain ) { |
---|
134 | // InternalRational * dummy = new InternalRational( str ); |
---|
135 | // if ( dummy->is_imm() ) { |
---|
136 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
137 | // delete dummy; |
---|
138 | // return res; |
---|
139 | // } |
---|
140 | // else |
---|
141 | // return dummy; |
---|
142 | // } |
---|
143 | else if ( currenttype == FiniteFieldDomain ) { |
---|
144 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
145 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
146 | delete dummy; |
---|
147 | return res; |
---|
148 | } |
---|
149 | else if ( currenttype == GaloisFieldDomain ) { |
---|
150 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
151 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
152 | delete dummy; |
---|
153 | return res; |
---|
154 | } |
---|
155 | else if ( currenttype == PrimePowerDomain ) |
---|
156 | return new InternalPrimePower( str, base ); |
---|
157 | else { |
---|
158 | ASSERT( 0, "illegal basic domain!" ); |
---|
159 | return 0; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | InternalCF * |
---|
164 | CFFactory::basic ( int type, const char * const str ) |
---|
165 | { |
---|
166 | if ( type == IntegerDomain ) { |
---|
167 | InternalInteger * dummy = new InternalInteger( str ); |
---|
168 | if ( dummy->is_imm() ) { |
---|
169 | InternalCF * res = int2imm( dummy->intval() ); |
---|
170 | delete dummy; |
---|
171 | return res; |
---|
172 | } |
---|
173 | else |
---|
174 | return dummy; |
---|
175 | } |
---|
176 | // else if ( type == RationalDomain ) { |
---|
177 | // InternalRational * dummy = new InternalRational( str ); |
---|
178 | // if ( dummy->is_imm() ) { |
---|
179 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
180 | // delete dummy; |
---|
181 | // return res; |
---|
182 | // } |
---|
183 | // else |
---|
184 | // return dummy; |
---|
185 | // } |
---|
186 | else if ( type == FiniteFieldDomain ) { |
---|
187 | InternalInteger * dummy = new InternalInteger( str ); |
---|
188 | InternalCF * res = int2imm( dummy->intmod( ff_prime ) ); |
---|
189 | delete dummy; |
---|
190 | return res; |
---|
191 | } |
---|
192 | else if ( type == GaloisFieldDomain ) { |
---|
193 | InternalInteger * dummy = new InternalInteger( str ); |
---|
194 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
195 | delete dummy; |
---|
196 | return res; |
---|
197 | } |
---|
198 | else if ( type == PrimePowerDomain ) |
---|
199 | return new InternalPrimePower( str ); |
---|
200 | else { |
---|
201 | ASSERT( 0, "illegal basic domain!" ); |
---|
202 | return 0; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | InternalCF * |
---|
207 | CFFactory::basic ( int type, int value, bool nonimm ) |
---|
208 | { |
---|
209 | if ( nonimm ) |
---|
210 | if ( type == IntegerDomain ) |
---|
211 | return new InternalInteger( value ); |
---|
212 | else if ( type == RationalDomain ) |
---|
213 | return new InternalRational( value ); |
---|
214 | else { |
---|
215 | ASSERT( 0, "illegal basic domain!" ); |
---|
216 | return 0; |
---|
217 | } |
---|
218 | else |
---|
219 | return CFFactory::basic( type, value ); |
---|
220 | } |
---|
221 | |
---|
222 | InternalCF * |
---|
223 | CFFactory::basic ( const MP_INT & num ) |
---|
224 | { |
---|
225 | if ( currenttype != IntegerDomain ) { |
---|
226 | InternalPrimePower * dummy = new InternalPrimePower( num ); |
---|
227 | return (InternalCF*)(dummy->normalize_myself()); |
---|
228 | } |
---|
229 | else |
---|
230 | return new InternalInteger( num ); |
---|
231 | } |
---|
232 | |
---|
233 | InternalCF * |
---|
234 | CFFactory::rational ( int num, int den ) |
---|
235 | { |
---|
236 | InternalRational * res = new InternalRational( num, den ); |
---|
237 | return res->normalize_myself(); |
---|
238 | } |
---|
239 | |
---|
240 | InternalCF * |
---|
241 | CFFactory::rational ( const MP_INT & num, const MP_INT & den, bool normalize ) |
---|
242 | { |
---|
243 | if ( normalize ) { |
---|
244 | InternalRational * result = new InternalRational( num, den ); |
---|
245 | return result->normalize_myself(); |
---|
246 | } |
---|
247 | else |
---|
248 | return new InternalRational( num, den ); |
---|
249 | } |
---|
250 | |
---|
251 | InternalCF * |
---|
252 | CFFactory::poly ( const Variable & v, int exp, const CanonicalForm & c ) |
---|
253 | { |
---|
254 | if ( v.level() == LEVELBASE ) |
---|
255 | return c.getval(); |
---|
256 | else |
---|
257 | return new InternalPoly( v, exp, c ); |
---|
258 | } |
---|
259 | |
---|
260 | InternalCF * |
---|
261 | CFFactory::poly ( const Variable & v, int exp ) |
---|
262 | { |
---|
263 | if ( v.level() == LEVELBASE ) |
---|
264 | return CFFactory::basic( 1 ); |
---|
265 | else |
---|
266 | return new InternalPoly( v, exp, 1 ); |
---|
267 | } |
---|
268 | |
---|
269 | MP_INT getmpi ( InternalCF * value, bool symmetric ) |
---|
270 | { |
---|
271 | ASSERT( ! is_imm( value ) && ( value->levelcoeff() == PrimePowerDomain || value->levelcoeff() == IntegerDomain ), "illegal operation" ); |
---|
272 | MP_INT dummy; |
---|
273 | if ( value->levelcoeff() == IntegerDomain ) |
---|
274 | mpz_init_set( &dummy, &InternalInteger::MPI( value ) ); |
---|
275 | else if ( symmetric ) { |
---|
276 | mpz_init( &dummy ); |
---|
277 | if ( mpz_cmp( &InternalPrimePower::primepowhalf, &InternalPrimePower::MPI( value ) ) < 0 ) |
---|
278 | mpz_sub( &dummy, &InternalPrimePower::MPI( value ), &InternalPrimePower::primepow ); |
---|
279 | else |
---|
280 | mpz_set( &dummy, &InternalPrimePower::MPI( value ) ); |
---|
281 | } |
---|
282 | else |
---|
283 | mpz_init_set( &dummy, &InternalPrimePower::MPI( value ) ); |
---|
284 | return dummy; |
---|
285 | } |
---|