[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[2dd068] | 3 | |
---|
[e4fe2b] | 4 | #include "config.h" |
---|
[ab4548f] | 5 | |
---|
[650f2d8] | 6 | #include "cf_assert.h" |
---|
[f5f16d] | 7 | |
---|
[2dd068] | 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 ) |
---|
[806c18] | 31 | if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
| 32 | return int2imm( value ); |
---|
| 33 | else |
---|
| 34 | return new InternalInteger( value ); |
---|
[2dd068] | 35 | // else if ( currenttype == RationalDomain ) |
---|
[806c18] | 36 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
| 37 | // return int2imm( value ); |
---|
| 38 | // else |
---|
| 39 | // return new InternalRational( value ); |
---|
[2dd068] | 40 | else if ( currenttype == FiniteFieldDomain ) |
---|
[806c18] | 41 | return int2imm_p( ff_norm( value ) ); |
---|
[2dd068] | 42 | else if ( currenttype == GaloisFieldDomain ) |
---|
[806c18] | 43 | return int2imm_gf( gf_int2gf( value ) ); |
---|
[2dd068] | 44 | else if ( currenttype == PrimePowerDomain ) |
---|
[806c18] | 45 | return new InternalPrimePower( value ); |
---|
[2dd068] | 46 | else { |
---|
[806c18] | 47 | ASSERT( 0, "illegal basic domain!" ); |
---|
| 48 | return 0; |
---|
[2dd068] | 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | InternalCF * |
---|
| 53 | CFFactory::basic ( int type, int value ) |
---|
| 54 | { |
---|
| 55 | if ( type == IntegerDomain ) |
---|
[806c18] | 56 | if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
| 57 | return int2imm( value ); |
---|
| 58 | else |
---|
| 59 | return new InternalInteger( value ); |
---|
[2dd068] | 60 | // else if ( type == RationalDomain ) |
---|
[806c18] | 61 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
| 62 | // return int2imm( value ); |
---|
| 63 | // else |
---|
| 64 | // return new InternalRational( value ); |
---|
[2dd068] | 65 | else if ( type == FiniteFieldDomain ) |
---|
[806c18] | 66 | return int2imm_p( ff_norm( value ) ); |
---|
[2dd068] | 67 | else if ( type == GaloisFieldDomain ) |
---|
[806c18] | 68 | return int2imm_gf( gf_int2gf( value ) ); |
---|
[2dd068] | 69 | else if ( type == PrimePowerDomain ) |
---|
[806c18] | 70 | return new InternalPrimePower( value ); |
---|
[2dd068] | 71 | else { |
---|
[806c18] | 72 | ASSERT1( 0, "illegal basic domain (type = %d)!", type ); |
---|
| 73 | return 0; |
---|
[2dd068] | 74 | } |
---|
| 75 | } |
---|
[f5f16d] | 76 | |
---|
[2dd068] | 77 | InternalCF * |
---|
| 78 | CFFactory::basic ( const char * str ) |
---|
| 79 | { |
---|
| 80 | if ( currenttype == IntegerDomain ) { |
---|
[806c18] | 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; |
---|
[2dd068] | 89 | } |
---|
| 90 | // else if ( currenttype == RationalDomain ) { |
---|
[806c18] | 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; |
---|
[2dd068] | 99 | // } |
---|
| 100 | else if ( currenttype == FiniteFieldDomain ) { |
---|
[806c18] | 101 | InternalInteger * dummy = new InternalInteger( str ); |
---|
| 102 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
| 103 | delete dummy; |
---|
| 104 | return res; |
---|
[2dd068] | 105 | } |
---|
| 106 | else if ( currenttype == GaloisFieldDomain ) { |
---|
[806c18] | 107 | InternalInteger * dummy = new InternalInteger( str ); |
---|
| 108 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
| 109 | delete dummy; |
---|
| 110 | return res; |
---|
[2dd068] | 111 | } |
---|
| 112 | else if ( currenttype == PrimePowerDomain ) |
---|
[806c18] | 113 | return new InternalPrimePower( str ); |
---|
[2dd068] | 114 | else { |
---|
[806c18] | 115 | ASSERT( 0, "illegal basic domain!" ); |
---|
| 116 | return 0; |
---|
[2dd068] | 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
[d07137] | 120 | InternalCF * |
---|
| 121 | CFFactory::basic ( const char * str, int base ) |
---|
| 122 | { |
---|
| 123 | if ( currenttype == IntegerDomain ) { |
---|
[806c18] | 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; |
---|
[d07137] | 132 | } |
---|
| 133 | // else if ( currenttype == RationalDomain ) { |
---|
[806c18] | 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; |
---|
[d07137] | 142 | // } |
---|
| 143 | else if ( currenttype == FiniteFieldDomain ) { |
---|
[806c18] | 144 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
| 145 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
| 146 | delete dummy; |
---|
| 147 | return res; |
---|
[d07137] | 148 | } |
---|
| 149 | else if ( currenttype == GaloisFieldDomain ) { |
---|
[806c18] | 150 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
| 151 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
| 152 | delete dummy; |
---|
| 153 | return res; |
---|
[d07137] | 154 | } |
---|
| 155 | else if ( currenttype == PrimePowerDomain ) |
---|
[806c18] | 156 | return new InternalPrimePower( str, base ); |
---|
[d07137] | 157 | else { |
---|
[806c18] | 158 | ASSERT( 0, "illegal basic domain!" ); |
---|
| 159 | return 0; |
---|
[d07137] | 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[2dd068] | 163 | InternalCF * |
---|
| 164 | CFFactory::basic ( int type, const char * const str ) |
---|
| 165 | { |
---|
| 166 | if ( type == IntegerDomain ) { |
---|
[806c18] | 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; |
---|
[2dd068] | 175 | } |
---|
| 176 | // else if ( type == RationalDomain ) { |
---|
[806c18] | 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; |
---|
[2dd068] | 185 | // } |
---|
| 186 | else if ( type == FiniteFieldDomain ) { |
---|
[806c18] | 187 | InternalInteger * dummy = new InternalInteger( str ); |
---|
| 188 | InternalCF * res = int2imm( dummy->intmod( ff_prime ) ); |
---|
| 189 | delete dummy; |
---|
| 190 | return res; |
---|
[2dd068] | 191 | } |
---|
| 192 | else if ( type == GaloisFieldDomain ) { |
---|
[806c18] | 193 | InternalInteger * dummy = new InternalInteger( str ); |
---|
| 194 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
| 195 | delete dummy; |
---|
| 196 | return res; |
---|
[2dd068] | 197 | } |
---|
| 198 | else if ( type == PrimePowerDomain ) |
---|
[806c18] | 199 | return new InternalPrimePower( str ); |
---|
[2dd068] | 200 | else { |
---|
[806c18] | 201 | ASSERT( 0, "illegal basic domain!" ); |
---|
| 202 | return 0; |
---|
[2dd068] | 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | InternalCF * |
---|
| 207 | CFFactory::basic ( int type, int value, bool nonimm ) |
---|
| 208 | { |
---|
| 209 | if ( nonimm ) |
---|
[806c18] | 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 | } |
---|
[2dd068] | 218 | else |
---|
[806c18] | 219 | return CFFactory::basic( type, value ); |
---|
[2dd068] | 220 | } |
---|
| 221 | |
---|
| 222 | InternalCF * |
---|
[a52291] | 223 | CFFactory::basic ( const mpz_ptr num ) |
---|
[2dd068] | 224 | { |
---|
| 225 | if ( currenttype != IntegerDomain ) { |
---|
[806c18] | 226 | InternalPrimePower * dummy = new InternalPrimePower( num ); |
---|
| 227 | return (InternalCF*)(dummy->normalize_myself()); |
---|
[2dd068] | 228 | } |
---|
| 229 | else |
---|
[806c18] | 230 | return new InternalInteger( num ); |
---|
[2dd068] | 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 * |
---|
[a52291] | 241 | CFFactory::rational ( const mpz_ptr num, const mpz_ptr den, bool normalize ) |
---|
[2dd068] | 242 | { |
---|
| 243 | if ( normalize ) { |
---|
[806c18] | 244 | InternalRational * result = new InternalRational( num, den ); |
---|
| 245 | return result->normalize_myself(); |
---|
[2dd068] | 246 | } |
---|
| 247 | else |
---|
[806c18] | 248 | return new InternalRational( num, den ); |
---|
[2dd068] | 249 | } |
---|
| 250 | |
---|
| 251 | InternalCF * |
---|
| 252 | CFFactory::poly ( const Variable & v, int exp, const CanonicalForm & c ) |
---|
| 253 | { |
---|
| 254 | if ( v.level() == LEVELBASE ) |
---|
[806c18] | 255 | return c.getval(); |
---|
[2dd068] | 256 | else |
---|
[806c18] | 257 | return new InternalPoly( v, exp, c ); |
---|
[2dd068] | 258 | } |
---|
| 259 | |
---|
| 260 | InternalCF * |
---|
| 261 | CFFactory::poly ( const Variable & v, int exp ) |
---|
| 262 | { |
---|
| 263 | if ( v.level() == LEVELBASE ) |
---|
[806c18] | 264 | return CFFactory::basic( 1 ); |
---|
[2dd068] | 265 | else |
---|
[806c18] | 266 | return new InternalPoly( v, exp, 1 ); |
---|
[2dd068] | 267 | } |
---|
| 268 | |
---|
[a52291] | 269 | mpz_ptr getmpi ( InternalCF * value, bool symmetric ) |
---|
[2dd068] | 270 | { |
---|
| 271 | ASSERT( ! is_imm( value ) && ( value->levelcoeff() == PrimePowerDomain || value->levelcoeff() == IntegerDomain ), "illegal operation" ); |
---|
[a52291] | 272 | mpz_ptr dummy= new mpz_t; |
---|
[2dd068] | 273 | if ( value->levelcoeff() == IntegerDomain ) |
---|
[a52291] | 274 | mpz_init_set( dummy, InternalInteger::MPI( value ) ); |
---|
[2dd068] | 275 | else if ( symmetric ) { |
---|
[a52291] | 276 | mpz_init( dummy ); |
---|
| 277 | if ( mpz_cmp( InternalPrimePower::primepowhalf, InternalPrimePower::MPI( value ) ) < 0 ) |
---|
| 278 | mpz_sub( dummy, InternalPrimePower::MPI( value ), InternalPrimePower::primepow ); |
---|
[806c18] | 279 | else |
---|
[a52291] | 280 | mpz_set( dummy, InternalPrimePower::MPI( value ) ); |
---|
[2dd068] | 281 | } |
---|
| 282 | else |
---|
[a52291] | 283 | mpz_init_set( dummy, InternalPrimePower::MPI( value ) ); |
---|
[2dd068] | 284 | return dummy; |
---|
[f5f16d] | 285 | } |
---|