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