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