Changeset 1201:c98ce1d5057a
- Timestamp:
- 09/14/06 19:04:33 (7 years ago)
- Branch:
- default
- Files:
-
- 6 edited
-
sage/rings/integer_mod_pyx.pyx (modified) (12 diffs)
-
sage/rings/integer_mod_ring.py (modified) (1 diff)
-
sage/rings/integer_ring.py (modified) (2 diffs)
-
sage/rings/padic_field.py (modified) (1 diff)
-
sage/rings/quotient_ring.py (modified) (1 diff)
-
setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sage/rings/integer_mod_pyx.pyx
r1200 r1201 391 391 392 392 def __init__(IntegerMod_gmp self, parent, value, empty=False): 393 """ 394 EXAMPLES: 395 sage: a = mod(5,14^20) 396 sage: type(a) 397 <type 'integer_mod_pyx.IntegerMod_gmp'> 398 sage: loads(dumps(a)) == a 399 True 400 """ 393 401 mpz_init(self.value) 394 402 IntegerMod_abstract.__init__(self, parent, value) … … 466 474 467 475 EXAMPLES: 468 sage: mod( 6,5).is_one()469 True 470 sage: mod(0,5 ).is_one()476 sage: mod(1,5^23).is_one() 477 True 478 sage: mod(0,5^23).is_one() 471 479 False 472 480 """ … … 478 486 479 487 EXAMPLES: 480 sage: mod(13,5 ).is_zero()488 sage: mod(13,5^23).is_zero() 481 489 False 482 sage: mod(25,5).is_zero()490 sage: (mod(25,5^23)^23).is_zero() 483 491 True 484 492 """ … … 519 527 """ 520 528 EXAMPLES: 521 sage: R = Integers(10 )529 sage: R = Integers(10^10) 522 530 sage: R(7) + R(8) 523 5531 15 524 532 """ 525 533 cdef sage.ext.integer.Integer modulus … … 535 543 """ 536 544 EXAMPLES: 537 sage: R = Integers(10 )545 sage: R = Integers(10^10) 538 546 sage: R(7) - R(8) 539 9 547 9999999999 540 548 """ 541 549 cdef IntegerMod_gmp x … … 549 557 """ 550 558 EXAMPLES: 551 sage: R = Integers(10 )552 sage: R(7 ) * R(8)553 6 559 sage: R = Integers(10^11) 560 sage: R(700000) * R(800000) 561 60000000000 554 562 """ 555 563 cdef sage.ext.integer.Integer modulus … … 562 570 563 571 def _div_(IntegerMod_gmp self, IntegerMod_gmp right): 572 """ 573 EXAMPLES: 574 sage: R = Integers(10^11) 575 sage: R(3) / R(7) 576 71428571429 577 """ 564 578 return self._mul_(~right) 565 579 … … 579 593 """ 580 594 EXAMPLES: 581 sage: R = Integers(10) 582 sage: R(2)^10 583 4 584 sage: R = Integers(389) 585 sage: R(7)^388 595 sage: R = Integers(10^10) 596 sage: R(2)^1000 597 5668069376 598 sage: p = next_prime(11^10) 599 sage: R = Integers(p) 600 sage: R(9876)^(p-1) 586 601 1 587 602 """ … … 598 613 """ 599 614 EXAMPLES: 600 sage: -mod( 7,10)601 3615 sage: -mod(5,10^10) 616 9999999995 602 617 """ 603 618 cdef IntegerMod_gmp x … … 609 624 """ 610 625 EXAMPLES: 611 sage: ~mod(7,100) 612 43 626 sage: ~mod(3,10^10) 627 6666666667 628 sage: ~mod(2,10^10) 629 Traceback (most recent call last): 630 ... 631 ZeroDivisionError: Inverse does not exist. 613 632 """ 614 633 cdef IntegerMod_gmp x … … 836 855 """ 837 856 cdef IntegerMod_int x 838 try: 839 x = IntegerMod_int(self.parent(), None, empty=True) 840 x.imodulus = self.imodulus 841 x.ivalue = (self.ivalue * mod_inverse_int(right.ivalue, self.imodulus) ) % self.imodulus 842 return x; 843 except ZeroDivisionError: 844 return IntegerMod_int(self.parent(), self.lift() / right.lift() ) 845 857 x = IntegerMod_int(self._parent, None, empty=True) 858 x.ivalue = (self.ivalue * mod_inverse_int(right.ivalue, self.__modulus.int32) ) % self.__modulus.int32 859 return x; 860 846 861 def __int__(IntegerMod_int self): 847 862 return self.ivalue … … 1008 1023 return mod_inverse_int(int(a), int(b)) 1009 1024 1010 1011 1012 1025 1026 1027 ###################################################################### 1028 # class IntegerMod_int64 1029 ###################################################################### 1030 1031 cdef class IntegerMod_int64(IntegerMod_abstract): 1032 """ 1033 Elements of $\Z/n\Z$ for n small enough to be operated on in 64 bits 1034 AUTHORS: 1035 -- Robert Bradshaw (2006-09-14) 1036 """ 1037 1038 def __init__(self, parent, value, empty=False): 1039 """ 1040 EXAMPLES: 1041 sage: a = Mod(10,3^10); a 1042 10 1043 sage: type(a) 1044 <type 'integer_mod_pyx.IntegerMod_int64'> 1045 sage: loads(a.dumps()) == a 1046 True 1047 """ 1048 IntegerMod_abstract.__init__(self, parent, value) 1049 if empty: 1050 return 1051 cdef sage.ext.integer.Integer z 1052 if isinstance(value, sage.ext.integer.Integer): 1053 z = value 1054 elif isinstance(value, rational.Rational): 1055 z = value % self.__modulus.sageInteger 1056 else: 1057 z = sage.rings.integer_ring.Z(value) 1058 self.set_from_mpz(z.value) 1059 1060 1061 cdef void set_from_mpz(IntegerMod_int64 self, mpz_t value): 1062 if mpz_sgn(value) == -1 or mpz_cmp_si(value, self.__modulus.int64) >= 0: 1063 self.ivalue = mpz_fdiv_ui(value, self.__modulus.int64) 1064 else: 1065 self.ivalue = mpz_get_si(value) 1066 1067 cdef void set_from_int(IntegerMod_int64 self, int_fast64_t ivalue): 1068 if ivalue < 0: 1069 self.ivalue = self.__modulus.int64 + (ivalue % self.__modulus.int64) 1070 elif ivalue >= self.__modulus.int64: 1071 self.ivalue = ivalue % self.__modulus.int64 1072 else: 1073 self.ivalue = ivalue 1074 1075 cdef int_fast64_t get_int_value(IntegerMod_int64 self): 1076 return self.ivalue 1077 1078 1079 def __cmp__(IntegerMod_int64 self, right): 1080 if not isinstance(right, IntegerMod_int64): 1081 try: 1082 return coerce_cmp(self, right) 1083 except TypeError: 1084 return -1 1085 return self.cmp(right) 1086 1087 def cmp(IntegerMod_int64 self, IntegerMod_int64 right): 1088 """ 1089 EXAMPLES: 1090 sage: mod(5,13^5) == mod(13^5+5,13^5) 1091 True 1092 sage: mod(5,13^5) == mod(8,13^5) 1093 False 1094 sage: mod(5,13^5) == mod(5,13) 1095 False 1096 sage: mod(0, 13^5) == 0 1097 True 1098 sage: mod(0, 13^5) == int(0) 1099 True 1100 """ 1101 if right._parent != self._parent: 1102 return -1 1103 if self.ivalue == right.ivalue: return 0 1104 elif self.ivalue < right.ivalue: return -1 1105 else: return 1 1106 1107 def __richcmp__(self, right, int op): 1108 cdef int n 1109 if not isinstance(right, IntegerMod_int64): 1110 try: 1111 n = coerce_cmp(self, right) 1112 except TypeError: 1113 n = -1 1114 else: 1115 n = self.cmp(right) 1116 return self._rich_to_bool(op, n) 1117 1118 1119 def is_one(IntegerMod_int64 self): 1120 """ 1121 Returns \\code{True} if this is $1$, otherwise \\code{False}. 1122 1123 EXAMPLES: 1124 sage: (mod(-1,5^10)^2).is_one() 1125 True 1126 sage: mod(0,5^10).is_one() 1127 False 1128 """ 1129 return bool(self.ivalue == 1) 1130 1131 def is_zero(IntegerMod_int64 self): 1132 """ 1133 Returns \\code{True} if this is $0$, otherwise \\code{False}. 1134 1135 EXAMPLES: 1136 sage: mod(13,5^10).is_zero() 1137 False 1138 sage: mod(5^12,5^10).is_zero() 1139 True 1140 """ 1141 return bool(self.ivalue == 0) 1142 1143 def is_unit(IntegerMod_int64 self): 1144 return bool(gcd_int64(self.ivalue, self.__modulus.int64) == 1) 1145 1146 def __crt(IntegerMod_int64 self, IntegerMod_int64 other): 1147 """ 1148 Use the Chinese Remainder Theorem to find an element of the 1149 integers modulo the product of the moduli that reduces to self 1150 and to other. The modulus of other must be coprime to the 1151 modulus of self. 1152 EXAMPLES: 1153 sage: a = mod(3,5^10) 1154 sage: b = mod(2,7) 1155 sage: a.crt(b) 1156 29296878 1157 sage: type(a.crt(b)) == type(b.crt(a)) and type(a.crt(b)) == type(mod(1, 7 * 5^10)) 1158 True 1159 1160 sage: a = mod(3,10^10) 1161 sage: b = mod(2,9) 1162 sage: a.crt(b) 1163 80000000003 1164 sage: type(a.crt(b)) == type(b.crt(a)) and type(a.crt(b)) == type(mod(1, 9 * 10^10)) 1165 True 1166 1167 AUTHOR: 1168 -- Robert Bradshaw 1169 """ 1170 cdef IntegerMod_int64 lift 1171 cdef int_fast64_t x 1172 1173 lift = IntegerMod_int64(integer_mod_ring.IntegerModRing(self.__modulus.int64 * other.__modulus.int64, check_prime=False), None, empty=True) 1174 1175 try: 1176 x = (other.ivalue - self.ivalue % other.__modulus.int64) * mod_inverse_int64(self.__modulus.int64, other.__modulus.int64) 1177 lift.set_from_int( x * self.__modulus.int64 + self.ivalue ) 1178 return lift 1179 except ZeroDivisionError: 1180 raise ZeroDivisionError, "moduli must be coprime" 1181 1182 1183 def copy(IntegerMod_int64 self): 1184 cdef IntegerMod_int64 copy 1185 copy = IntegerMod_int64(self._parent, None, empty=True) 1186 copy.ivalue = self.ivalue 1187 return copy 1188 1189 def _add_(IntegerMod_int64 self, IntegerMod_int64 right): 1190 """ 1191 EXAMPLES: 1192 sage: R = Integers(10^5) 1193 sage: R(7) + R(8) 1194 15 1195 """ 1196 cdef IntegerMod_int64 x 1197 x = IntegerMod_int64(self._parent, None, empty=True) 1198 x.ivalue = self.ivalue + right.ivalue 1199 if x.ivalue >= self.__modulus.int64: 1200 x.ivalue = x.ivalue - self.__modulus.int64 1201 return x; 1202 1203 def _sub_(IntegerMod_int64 self, IntegerMod_int64 right): 1204 """ 1205 EXAMPLES: 1206 sage: R = Integers(10^5) 1207 sage: R(7) - R(8) 1208 99999 1209 """ 1210 cdef IntegerMod_int64 x 1211 x = IntegerMod_int64(self._parent, None, empty=True) 1212 x.ivalue = self.ivalue - right.ivalue 1213 if x.ivalue < 0: 1214 x.ivalue = x.ivalue + self.__modulus.int64 1215 return x; 1216 1217 def _mul_(IntegerMod_int64 self, IntegerMod_int64 right): 1218 """ 1219 EXAMPLES: 1220 sage: R = Integers(10^5) 1221 sage: R(700) * R(800) 1222 60000 1223 """ 1224 cdef IntegerMod_int64 x 1225 x = IntegerMod_int64(self._parent, None, empty=True) 1226 x.ivalue = (self.ivalue * right.ivalue) % self.__modulus.int64 1227 return x; 1228 1229 def _div_(IntegerMod_int64 self, IntegerMod_int64 right): 1230 """ 1231 EXAMPLES: 1232 sage: R = Integers(10^5) 1233 sage: R(2)/3 1234 33334 1235 """ 1236 cdef IntegerMod_int64 x 1237 x = IntegerMod_int64(self._parent, None, empty=True) 1238 x.ivalue = (self.ivalue * mod_inverse_int64(right.ivalue, self.__modulus.int64) ) % self.__modulus.int64 1239 return x; 1240 1241 def __int__(IntegerMod_int64 self): 1242 return self.ivalue 1243 1244 def __long__(IntegerMod_int64 self): 1245 return self.ivalue 1246 1247 def __mod__(IntegerMod_int64 self, right): 1248 right = int(right) 1249 if self.__modulus.int64 % right != 0: 1250 raise ZeroDivisionError, "Error - reduction modulo right not defined." 1251 return integer_mod_ring.IntegerModRing(right)(self) 1252 1253 def __pow__(IntegerMod_int64 self, right, m): # NOTE: m ignored, always use modulus of parent ring 1254 """ 1255 EXAMPLES: 1256 sage: R = Integers(10) 1257 sage: R(2)^10 1258 4 1259 sage: p = next_prime(10^5) 1260 sage: R = Integers(p) 1261 sage: R(1234)^(p-1) 1262 1 1263 """ 1264 cdef sage.ext.integer.Integer exp, base 1265 exp = sage.rings.integer_ring.Z(right) 1266 cdef IntegerMod_int64 x 1267 cdef mpz_t x_mpz 1268 x = IntegerMod_int64(self._parent, None, empty=True) 1269 if mpz_sgn(exp.value) >= 0 and mpz_cmp_si(exp.value, 100000) < 0: # TODO: test to find a good threshold 1270 x.ivalue = mod_pow_int64(self.ivalue, mpz_get_si(exp.value), self.__modulus.int64) 1271 else: 1272 mpz_init(x_mpz) 1273 _sig_on 1274 base = self.lift() 1275 mpz_powm(x_mpz, base.value, exp.value, self.__modulus.sageInteger.value) 1276 _sig_off 1277 x.ivalue = mpz_get_si(x_mpz) 1278 mpz_clear(x_mpz) 1279 return x 1280 1281 1282 def __neg__(IntegerMod_int64 self): 1283 """ 1284 EXAMPLES: 1285 sage: -mod(7,10^5) 1286 99993 1287 """ 1288 cdef IntegerMod_int64 x 1289 x = IntegerMod_int64(self._parent, None, empty=True) 1290 x.ivalue = self.__modulus.int64 - self.ivalue 1291 return x 1292 1293 def __invert__(IntegerMod_int64 self): 1294 """ 1295 EXAMPLES: 1296 sage: ~mod(7,10^5) 1297 57143 1298 """ 1299 cdef IntegerMod_int64 x 1300 x = IntegerMod_int64(self._parent, None, empty=True) 1301 x.ivalue = mod_inverse_int64(self.ivalue, self.__modulus.int64) 1302 return x 1303 1304 def lift(IntegerMod_int64 self): 1305 cdef sage.ext.integer.Integer z 1306 z = sage.ext.integer.Integer() 1307 mpz_set_si(z.value, self.ivalue) 1308 return z 1309 1310 def __float__(IntegerMod_int64 self): 1311 return float(self.ivalue) 1312 1313 def __hash__(self): 1314 return hash(self.ivalue) 1315 1316 ### End of class 1317 1318 1319 cdef int_fast64_t gcd_int64(int_fast64_t a, int_fast64_t b): 1320 """ 1321 Returns the gcd of a and b 1322 For use with IntegerMod_int64 1323 AUTHOR: 1324 -- Robert Bradshaw 1325 """ 1326 cdef int_fast64_t tmp 1327 if a < b: 1328 tmp = b 1329 b = a 1330 a = tmp 1331 while b: 1332 tmp = b 1333 b = a % b 1334 a = tmp 1335 return a 1336 1337 1338 cdef int_fast64_t mod_inverse_int64(int_fast64_t x, int_fast64_t n) except 0: 1339 """ 1340 Returns y such that xy=1 mod n 1341 For use in IntegerMod_int64 1342 AUTHOR: 1343 -- Robert Bradshaw 1344 """ 1345 cdef int_fast64_t tmp, a, b, last_t, t, next_t, q 1346 a = n 1347 b = x 1348 t = 0 1349 next_t = 1 1350 while b: 1351 # a = s * n + t * x 1352 if b == 1: 1353 next_t = next_t % n 1354 if next_t < 0: 1355 next_t = next_t + n 1356 return next_t 1357 q = a / b 1358 tmp = b 1359 b = a % b 1360 a = tmp 1361 last_t = t 1362 t = next_t 1363 next_t = last_t - q * t 1364 raise ZeroDivisionError, "Inverse does not exist." 1365 1366 1367 cdef int_fast64_t mod_pow_int64(int_fast64_t base, int_fast64_t exp, int_fast64_t n): 1368 """ 1369 Returns base^exp mod n 1370 For use in IntegerMod_int64 1371 AUTHOR: 1372 -- Robert Bradshaw 1373 """ 1374 cdef int_fast64_t prod, pow2 1375 if exp <= 5: 1376 if exp == 0: return 1 1377 if exp == 1: return base 1378 prod = base * base % n 1379 if exp == 2: return prod 1380 if exp == 3: return (prod * base) % n 1381 if exp == 4: return (prod * prod) % n 1382 1383 pow2 = base 1384 if exp % 2: prod = base 1385 else: prod = 1 1386 exp = exp >> 1 1387 while(exp != 0): 1388 pow2 = pow2 * pow2 1389 if pow2 >= INTEGER_MOD_INT32_LIMIT: pow2 = pow2 % n 1390 if exp % 2: 1391 prod = prod * pow2 1392 if prod >= INTEGER_MOD_INT32_LIMIT: prod = prod % n 1393 exp = exp >> 1 1394 1395 if prod > n: 1396 prod = prod % n 1397 return prod 1398 -
sage/rings/integer_mod_ring.py
r1199 r1201 140 140 sage: FF = IntegerModRing(17) 141 141 sage: FF 142 Ring of integers modulo17142 Finite Field of size 17 143 143 sage: FF.is_field() 144 144 True -
sage/rings/integer_ring.py
r1145 r1201 180 180 Traceback (most recent call last): 181 181 ... 182 TypeError: unable to coercex182 TypeError: no canonical coercion of x 183 183 sage: k._coerce_(5) # works since there's a natural hom ZZ --> GF(7). 184 184 5 … … 229 229 230 230 EXAMPLES: 231 sage: ZZ/( 3*ZZ)232 Ring of integers modulo 3231 sage: ZZ/(6*ZZ) 232 Ring of integers modulo 6 233 233 sage: ZZ/(0*ZZ) 234 234 Integer Ring 235 235 sage: ZZ/3 236 Ring of integers modulo3236 Finite Field of size 3 237 237 sage: ZZ/(3*QQ) 238 238 Traceback (most recent call last): -
sage/rings/padic_field.py
r963 r1201 101 101 sage: K = Qp(3) 102 102 sage: K.residue_class_field() 103 Ring of integers modulo3103 Finite Field of size 3 104 104 """ 105 105 return IntegerModRing(self.__p) -
sage/rings/quotient_ring.py
r1097 r1201 105 105 Ring morphism: 106 106 From: Integer Ring 107 To: Ring of integers modulo3107 To: Finite Field of size 3 108 108 Defn: Natural quotient map 109 109 sage: pi(5) -
setup.py
r1179 r1201 292 292 ), \ 293 293 294 # Extension('sage.rings.integer_mod_ring_pyx',295 # ['sage/rings/integer_mod_ring_pyx.pyx']296 # ), \297 298 # Extension('sage.rings.quotient_ring.pyx',299 # ['sage/rings/quotient_ring.pyx']300 # ), \301 302 294 ] 303 295
Note: See TracChangeset
for help on using the changeset viewer.
