1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | |
---|
4 | #include "config.h" |
---|
5 | |
---|
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 "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, "illegal basic domain!" ); |
---|
24 | currenttype = type; |
---|
25 | } |
---|
26 | |
---|
27 | InternalCF * |
---|
28 | CFFactory::basic ( long 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 { |
---|
45 | ASSERT( 0, "illegal basic domain!" ); |
---|
46 | return 0; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | InternalCF * |
---|
51 | CFFactory::basic ( int type, long value ) |
---|
52 | { |
---|
53 | if ( type == IntegerDomain ) |
---|
54 | if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
55 | return int2imm( value ); |
---|
56 | else |
---|
57 | return new InternalInteger( value ); |
---|
58 | // else if ( type == RationalDomain ) |
---|
59 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
60 | // return int2imm( value ); |
---|
61 | // else |
---|
62 | // return new InternalRational( value ); |
---|
63 | else if ( type == FiniteFieldDomain ) |
---|
64 | return int2imm_p( ff_norm( value ) ); |
---|
65 | else if ( type == GaloisFieldDomain ) |
---|
66 | return int2imm_gf( gf_int2gf( value ) ); |
---|
67 | else { |
---|
68 | ASSERT1( 0, "illegal basic domain (type = %d)!", type ); |
---|
69 | return 0; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | InternalCF * |
---|
74 | CFFactory::basic ( const char * str ) |
---|
75 | { |
---|
76 | if ( currenttype == IntegerDomain ) { |
---|
77 | InternalInteger * dummy = new InternalInteger( str ); |
---|
78 | if ( dummy->is_imm() ) { |
---|
79 | InternalCF * res = int2imm( dummy->intval() ); |
---|
80 | delete dummy; |
---|
81 | return res; |
---|
82 | } |
---|
83 | else |
---|
84 | return dummy; |
---|
85 | } |
---|
86 | // else if ( currenttype == RationalDomain ) { |
---|
87 | // InternalRational * dummy = new InternalRational( str ); |
---|
88 | // if ( dummy->is_imm() ) { |
---|
89 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
90 | // delete dummy; |
---|
91 | // return res; |
---|
92 | // } |
---|
93 | // else |
---|
94 | // return dummy; |
---|
95 | // } |
---|
96 | else if ( currenttype == FiniteFieldDomain ) { |
---|
97 | InternalInteger * dummy = new InternalInteger( str ); |
---|
98 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
99 | delete dummy; |
---|
100 | return res; |
---|
101 | } |
---|
102 | else if ( currenttype == GaloisFieldDomain ) { |
---|
103 | InternalInteger * dummy = new InternalInteger( str ); |
---|
104 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
105 | delete dummy; |
---|
106 | return res; |
---|
107 | } |
---|
108 | else { |
---|
109 | ASSERT( 0, "illegal basic domain!" ); |
---|
110 | return 0; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | InternalCF * |
---|
115 | CFFactory::basic ( const char * str, int base ) |
---|
116 | { |
---|
117 | if ( currenttype == IntegerDomain ) { |
---|
118 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
119 | if ( dummy->is_imm() ) { |
---|
120 | InternalCF * res = int2imm( dummy->intval() ); |
---|
121 | delete dummy; |
---|
122 | return res; |
---|
123 | } |
---|
124 | else |
---|
125 | return dummy; |
---|
126 | } |
---|
127 | // else if ( currenttype == RationalDomain ) { |
---|
128 | // InternalRational * dummy = new InternalRational( str ); |
---|
129 | // if ( dummy->is_imm() ) { |
---|
130 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
131 | // delete dummy; |
---|
132 | // return res; |
---|
133 | // } |
---|
134 | // else |
---|
135 | // return dummy; |
---|
136 | // } |
---|
137 | else if ( currenttype == FiniteFieldDomain ) { |
---|
138 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
139 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
140 | delete dummy; |
---|
141 | return res; |
---|
142 | } |
---|
143 | else if ( currenttype == GaloisFieldDomain ) { |
---|
144 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
145 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
146 | delete dummy; |
---|
147 | return res; |
---|
148 | } |
---|
149 | else { |
---|
150 | ASSERT( 0, "illegal basic domain!" ); |
---|
151 | return 0; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | InternalCF * |
---|
156 | CFFactory::basic ( int type, const char * const str ) |
---|
157 | { |
---|
158 | if ( type == IntegerDomain ) { |
---|
159 | InternalInteger * dummy = new InternalInteger( str ); |
---|
160 | if ( dummy->is_imm() ) { |
---|
161 | InternalCF * res = int2imm( dummy->intval() ); |
---|
162 | delete dummy; |
---|
163 | return res; |
---|
164 | } |
---|
165 | else |
---|
166 | return dummy; |
---|
167 | } |
---|
168 | // else if ( type == RationalDomain ) { |
---|
169 | // InternalRational * dummy = new InternalRational( str ); |
---|
170 | // if ( dummy->is_imm() ) { |
---|
171 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
172 | // delete dummy; |
---|
173 | // return res; |
---|
174 | // } |
---|
175 | // else |
---|
176 | // return dummy; |
---|
177 | // } |
---|
178 | else if ( type == FiniteFieldDomain ) { |
---|
179 | InternalInteger * dummy = new InternalInteger( str ); |
---|
180 | InternalCF * res = int2imm( dummy->intmod( ff_prime ) ); |
---|
181 | delete dummy; |
---|
182 | return res; |
---|
183 | } |
---|
184 | else if ( type == GaloisFieldDomain ) { |
---|
185 | InternalInteger * dummy = new InternalInteger( str ); |
---|
186 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
187 | delete dummy; |
---|
188 | return res; |
---|
189 | } |
---|
190 | else { |
---|
191 | ASSERT( 0, "illegal basic domain!" ); |
---|
192 | return 0; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | InternalCF * |
---|
197 | CFFactory::basic ( int type, long value, bool nonimm ) |
---|
198 | { |
---|
199 | if ( nonimm ) |
---|
200 | if ( type == IntegerDomain ) |
---|
201 | return new InternalInteger( value ); |
---|
202 | else if ( type == RationalDomain ) |
---|
203 | return new InternalRational( value ); |
---|
204 | else { |
---|
205 | ASSERT( 0, "illegal basic domain!" ); |
---|
206 | return 0; |
---|
207 | } |
---|
208 | else |
---|
209 | return CFFactory::basic( type, value ); |
---|
210 | } |
---|
211 | |
---|
212 | InternalCF * |
---|
213 | CFFactory::basic ( const mpz_ptr num ) |
---|
214 | { |
---|
215 | ASSERT (currenttype == IntegerDomain, "Integer domain expected"); |
---|
216 | return new InternalInteger( num ); |
---|
217 | } |
---|
218 | |
---|
219 | InternalCF * |
---|
220 | CFFactory::rational ( long num, long den ) |
---|
221 | { |
---|
222 | InternalRational * res = new InternalRational( num, den ); |
---|
223 | return res->normalize_myself(); |
---|
224 | } |
---|
225 | |
---|
226 | InternalCF * |
---|
227 | CFFactory::rational ( const mpz_ptr num, const mpz_ptr den, bool normalize ) |
---|
228 | { |
---|
229 | if ( normalize ) { |
---|
230 | InternalRational * result = new InternalRational( num, den ); |
---|
231 | return result->normalize_myself(); |
---|
232 | } |
---|
233 | else |
---|
234 | return new InternalRational( num, den ); |
---|
235 | } |
---|
236 | |
---|
237 | InternalCF * |
---|
238 | CFFactory::poly ( const Variable & v, int exp, const CanonicalForm & c ) |
---|
239 | { |
---|
240 | if ( v.level() == LEVELBASE ) |
---|
241 | return c.getval(); |
---|
242 | else |
---|
243 | return new InternalPoly( v, exp, c ); |
---|
244 | } |
---|
245 | |
---|
246 | InternalCF * |
---|
247 | CFFactory::poly ( const Variable & v, int exp ) |
---|
248 | { |
---|
249 | if ( v.level() == LEVELBASE ) |
---|
250 | return CFFactory::basic( 1L ); |
---|
251 | else |
---|
252 | return new InternalPoly( v, exp, 1 ); |
---|
253 | } |
---|
254 | |
---|
255 | void getmpi ( InternalCF * value, mpz_t mpi) |
---|
256 | { |
---|
257 | ASSERT( ! is_imm( value ) && (value->levelcoeff() == IntegerDomain ), "illegal operation" ); |
---|
258 | mpz_init_set (mpi, ((InternalInteger*)value)->thempi); |
---|
259 | } |
---|
260 | |
---|