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