@@ -90,6 +90,7 @@ def test_fmpz():
90
90
assert int (f ) == i
91
91
assert flint .fmpz (f ) == f
92
92
assert flint .fmpz (str (i )) == f
93
+ assert raises (lambda : flint .fmpz (1 ,2 ), TypeError )
93
94
assert raises (lambda : flint .fmpz ("qwe" ), ValueError )
94
95
assert raises (lambda : flint .fmpz ([]), TypeError )
95
96
for s in L :
@@ -162,6 +163,9 @@ def test_fmpz():
162
163
# XXX: Handle negative modulus like int?
163
164
assert raises (lambda : pow (flint .fmpz (2 ), 2 , - 1 ), ValueError )
164
165
166
+ assert raises (lambda : pow (flint .fmpz (2 ), "asd" , 2 ), TypeError )
167
+ assert raises (lambda : pow (flint .fmpz (2 ), 2 , "asd" ), TypeError )
168
+
165
169
f = flint .fmpz (2 )
166
170
assert f .numerator == f
167
171
assert type (f .numerator ) is flint .fmpz
@@ -543,7 +547,8 @@ def test_fmpz_mat():
543
547
assert str (M (2 ,2 ,[1 ,2 ,3 ,4 ])) == '[1, 2]\n [3, 4]'
544
548
assert M (1 ,2 ,[3 ,4 ]) * flint .fmpq (1 ,3 ) == flint .fmpq_mat (1 , 2 , [1 , flint .fmpq (4 ,3 )])
545
549
assert flint .fmpq (1 ,3 ) * M (1 ,2 ,[3 ,4 ]) == flint .fmpq_mat (1 , 2 , [1 , flint .fmpq (4 ,3 )])
546
- assert M (1 ,2 ,[3 ,4 ]) / 3 == flint .fmpq_mat (1 , 2 , [1 , flint .fmpq (4 ,3 )])
550
+ assert raises (lambda : M (1 ,2 ,[3 ,4 ]) / 3 , DomainError )
551
+ assert M (1 ,2 ,[2 ,4 ]) / 2 == M (1 ,2 ,[1 ,2 ])
547
552
assert M (2 ,2 ,[1 ,2 ,3 ,4 ]).inv ().det () == flint .fmpq (1 ) / M (2 ,2 ,[1 ,2 ,3 ,4 ]).det ()
548
553
assert M (2 ,2 ,[1 ,2 ,3 ,4 ]).inv ().inv () == M (2 ,2 ,[1 ,2 ,3 ,4 ])
549
554
assert raises (lambda : M .randrank (4 ,3 ,4 ,1 ), ValueError )
@@ -965,7 +970,8 @@ def test_fmpq_poly():
965
970
assert 3 * Q ([1 ,2 ,3 ]) == Q ([3 ,6 ,9 ])
966
971
assert Q ([1 ,2 ,3 ]) * flint .fmpq (2 ,3 ) == (Q ([1 ,2 ,3 ]) * 2 ) / 3
967
972
assert flint .fmpq (2 ,3 ) * Q ([1 ,2 ,3 ]) == (Q ([1 ,2 ,3 ]) * 2 ) / 3
968
- assert raises (lambda : Q ([1 ,2 ]) / Q ([1 ,2 ]), TypeError )
973
+ assert Q ([1 ,2 ]) / Q ([1 ,2 ]) == Q ([1 ])
974
+ assert raises (lambda : Q ([1 ,2 ]) / Q ([2 ,2 ]), DomainError )
969
975
assert Q ([1 ,2 ,3 ]) / flint .fmpq (2 ,3 ) == Q ([1 ,2 ,3 ]) * flint .fmpq (3 ,2 )
970
976
assert Q ([1 ,2 ,3 ]) ** 2 == Q ([1 ,2 ,3 ]) * Q ([1 ,2 ,3 ])
971
977
assert raises (lambda : pow (Q ([1 ,2 ]), 3 , 5 ), NotImplementedError )
@@ -2056,7 +2062,7 @@ def test_fmpz_mod_poly():
2056
2062
assert raises (lambda : f .exact_division (0 ), ZeroDivisionError )
2057
2063
2058
2064
assert (f * g ).exact_division (g ) == f
2059
- assert raises (lambda : f .exact_division (g ), ValueError )
2065
+ assert raises (lambda : f .exact_division (g ), DomainError )
2060
2066
2061
2067
# true div
2062
2068
assert raises (lambda : f / "AAA" , TypeError )
@@ -2276,6 +2282,151 @@ def test_fmpz_mod_mat():
2276
2282
assert raises (lambda : flint .fmpz_mod_mat (A , c11 ), TypeError )
2277
2283
2278
2284
2285
+ def test_division_scalar ():
2286
+ Z = flint .fmpz
2287
+ Q = flint .fmpq
2288
+ F17 = lambda x : flint .nmod (x , 17 )
2289
+ ctx = flint .fmpz_mod_ctx (163 )
2290
+ F163 = lambda a : flint .fmpz_mod (a , ctx )
2291
+ # fmpz exact division
2292
+ for (a , b ) in [(Z (4 ), Z (2 )), (Z (4 ), 2 ), (4 , Z (2 ))]:
2293
+ assert a / b == Z (2 )
2294
+ for (a , b ) in [(Z (5 ), Z (2 )), (Z (5 ), 2 ), (5 , Z (2 ))]:
2295
+ assert raises (lambda : a / b , DomainError )
2296
+ # fmpz Euclidean division
2297
+ for (a , b ) in [(Z (5 ), Z (2 )), (Z (5 ), 2 ), (5 , Z (2 ))]:
2298
+ assert a // b == 2
2299
+ assert a % b == 1
2300
+ assert divmod (a , b ) == (2 , 1 )
2301
+ # field division
2302
+ for (a , b ) in [(Q (5 ), Q (2 )), (Q (5 ), 2 ), (5 , Q (2 ))]:
2303
+ assert a / b == Q (5 ,2 )
2304
+ for (a , b ) in [(F17 (5 ), F17 (2 )), (F17 (5 ), 2 ), (5 , F17 (2 ))]:
2305
+ assert a / b == F17 (11 )
2306
+ for (a , b ) in [(F163 (5 ), F163 (2 )), (F163 (5 ), 2 ), (5 , F163 (2 ))]:
2307
+ assert a / b == F163 (84 )
2308
+ # divmod with fields - should this give remainder zero instead of error?
2309
+ for K in [Q , F17 , F163 ]:
2310
+ for (a , b ) in [(K (5 ), K (2 )), (K (5 ), 2 ), (5 , K (2 ))]:
2311
+ assert raises (lambda : divmod (a , b ), TypeError )
2312
+ # Zero division
2313
+ for R in [Z , Q , F17 , F163 ]:
2314
+ assert raises (lambda : R (5 ) / 0 , ZeroDivisionError )
2315
+ assert raises (lambda : R (5 ) / R (0 ), ZeroDivisionError )
2316
+ assert raises (lambda : 5 / R (0 ), ZeroDivisionError )
2317
+ # Bad types
2318
+ for R in [Z , Q , F17 , F163 ]:
2319
+ assert raises (lambda : R (5 ) / "AAA" , TypeError )
2320
+ assert raises (lambda : "AAA" / R (5 ), TypeError )
2321
+
2322
+
2323
+ def test_division_poly ():
2324
+ Z = flint .fmpz
2325
+ Q = flint .fmpq
2326
+ F17 = lambda x : flint .nmod (x , 17 )
2327
+ ctx = flint .fmpz_mod_ctx (163 )
2328
+ F163 = lambda a : flint .fmpz_mod (a , ctx )
2329
+ PZ = lambda x : flint .fmpz_poly (x )
2330
+ PQ = lambda x : flint .fmpq_poly (x )
2331
+ PF17 = lambda x : flint .nmod_poly (x , 17 )
2332
+ PF163 = lambda x : flint .fmpz_mod_poly (x , flint .fmpz_mod_poly_ctx (163 ))
2333
+ # fmpz exact scalar division
2334
+ assert PZ ([2 , 4 ]) / Z (2 ) == PZ ([1 , 2 ])
2335
+ assert PZ ([2 , 4 ]) / 2 == PZ ([1 , 2 ])
2336
+ assert raises (lambda : PZ ([2 , 5 ]) / Z (2 ), DomainError )
2337
+ assert raises (lambda : PZ ([2 , 5 ]) / 2 , DomainError )
2338
+ # field division by scalar
2339
+ for (K , PK ) in [(Q , PQ ), (F17 , PF17 ), (F163 , PF163 )]:
2340
+ assert PK ([2 , 5 ]) / K (2 ) == PK ([K (2 )/ K (2 ), K (5 )/ K (2 )])
2341
+ assert PK ([2 , 5 ]) / 2 == PK ([K (2 )/ K (2 ), K (5 )/ K (2 )])
2342
+ # No other scalar division is allowed
2343
+ for (R , PR ) in [(Z , PZ ), (Q , PQ ), (F17 , PF17 ), (F163 , PF163 )]:
2344
+ assert raises (lambda : R (2 ) / PR ([2 , 5 ]), DomainError )
2345
+ assert raises (lambda : 2 / PR ([2 , 5 ]), DomainError )
2346
+ assert raises (lambda : PR ([2 , 5 ]) / 0 , ZeroDivisionError )
2347
+ assert raises (lambda : PR ([2 , 5 ]) / R (0 ), ZeroDivisionError )
2348
+ # exact polynomial division
2349
+ for (R , PR ) in [(Z , PZ ), (Q , PQ ), (F17 , PF17 ), (F163 , PF163 )]:
2350
+ assert PR ([2 , 4 ]) / PR ([1 , 2 ]) == PR ([2 ])
2351
+ assert PR ([2 , - 3 , 1 ]) / PR ([- 1 , 1 ]) == PR ([- 2 , 1 ])
2352
+ assert raises (lambda : PR ([2 , 4 ]) / PR ([1 , 3 ]), DomainError )
2353
+ assert PR ([2 ]) / PR ([2 ]) == 2 / PR ([2 ]) == PR ([1 ])
2354
+ assert PR ([0 ]) / PR ([1 , 2 ]) == 0 / PR ([1 , 2 ]) == PR ([0 ])
2355
+ if R is Z :
2356
+ assert raises (lambda : PR ([1 , 2 ]) / PR ([2 , 4 ]), DomainError )
2357
+ assert raises (lambda : 1 / PR ([2 ]), DomainError )
2358
+ else :
2359
+ assert PR ([1 , 2 ]) / PR ([2 , 4 ]) == PR ([R (1 )/ R (2 )])
2360
+ assert 1 / PR ([2 ]) == PR ([R (1 )/ R (2 )])
2361
+ assert raises (lambda : PR ([1 , 2 ]) / PR ([0 ]), ZeroDivisionError )
2362
+ # Euclidean polynomial division
2363
+ for (R , PR ) in [(Z , PZ ), (Q , PQ ), (F17 , PF17 ), (F163 , PF163 )]:
2364
+ assert PR ([2 , 4 ]) // PR ([1 , 2 ]) == PR ([2 ])
2365
+ assert PR ([2 , 4 ]) % PR ([1 , 2 ]) == PR ([0 ])
2366
+ assert divmod (PR ([2 , 4 ]), PR ([1 , 2 ])) == (PR ([2 ]), PR ([0 ]))
2367
+ assert PR ([3 , - 3 , 1 ]) // PR ([- 1 , 1 ]) == PR ([- 2 , 1 ])
2368
+ assert PR ([3 , - 3 , 1 ]) % PR ([- 1 , 1 ]) == PR ([1 ])
2369
+ assert divmod (PR ([3 , - 3 , 1 ]), PR ([- 1 , 1 ])) == (PR ([- 2 , 1 ]), PR ([1 ]))
2370
+ assert PR ([2 ]) // PR ([2 ]) == 2 // PR ([2 ]) == PR ([1 ])
2371
+ assert PR ([2 ]) % PR ([2 ]) == 2 % PR ([2 ]) == PR ([0 ])
2372
+ assert divmod (PR ([2 ]), PR ([2 ])) == (PR ([1 ]), PR ([0 ]))
2373
+ assert PR ([0 ]) // PR ([1 , 2 ]) == 0 // PR ([1 , 2 ]) == PR ([0 ])
2374
+ assert PR ([0 ]) % PR ([1 , 2 ]) == 0 % PR ([1 , 2 ]) == PR ([0 ])
2375
+ assert divmod (PR ([0 ]), PR ([1 , 2 ])) == (PR ([0 ]), PR ([0 ]))
2376
+ if R is Z :
2377
+ assert PR ([2 , 2 ]) // PR ([2 , 4 ]) == PR ([2 , 2 ]) // PR ([2 , 4 ]) == PR ([0 ])
2378
+ assert PR ([2 , 2 ]) % PR ([2 , 4 ]) == PR ([2 , 2 ]) % PR ([2 , 4 ]) == PR ([2 , 2 ])
2379
+ assert divmod (PR ([2 , 2 ]), PR ([2 , 4 ])) == (PR ([0 ]), PR ([2 , 2 ]))
2380
+ assert 1 // PR ([2 ]) == PR ([1 ]) // PR ([2 ]) == PR ([0 ])
2381
+ assert 1 % PR ([2 ]) == PR ([1 ]) % PR ([2 ]) == PR ([1 ])
2382
+ assert divmod (1 , PR ([2 ])) == (PR ([0 ]), PR ([1 ]))
2383
+ else :
2384
+ assert PR ([2 , 2 ]) // PR ([2 , 4 ]) == PR ([R (1 )/ R (2 )])
2385
+ assert PR ([2 , 2 ]) % PR ([2 , 4 ]) == PR ([1 ])
2386
+ assert divmod (PR ([2 , 2 ]), PR ([2 , 4 ])) == (PR ([R (1 )/ R (2 )]), PR ([1 ]))
2387
+ assert 1 // PR ([2 ]) == PR ([R (1 )/ R (2 )])
2388
+ assert 1 % PR ([2 ]) == PR ([0 ])
2389
+ assert divmod (1 , PR ([2 ])) == (PR ([R (1 )/ R (2 )]), PR ([0 ]))
2390
+
2391
+
2392
+ def test_division_matrix ():
2393
+ Z = flint .fmpz
2394
+ Q = flint .fmpq
2395
+ F17 = lambda x : flint .nmod (x , 17 )
2396
+ ctx = flint .fmpz_mod_ctx (163 )
2397
+ F163 = lambda a : flint .fmpz_mod (a , ctx )
2398
+ MZ = lambda x : flint .fmpz_mat (x )
2399
+ MQ = lambda x : flint .fmpq_mat (x )
2400
+ MF17 = lambda x : flint .nmod_mat (x , 17 )
2401
+ MF163 = lambda x : flint .fmpz_mod_mat (x , ctx )
2402
+ # fmpz exact division
2403
+ assert MZ ([[2 , 4 ]]) / Z (2 ) == MZ ([[1 , 2 ]])
2404
+ assert MZ ([[2 , 4 ]]) / 2 == MZ ([[1 , 2 ]])
2405
+ assert raises (lambda : MZ ([[2 , 5 ]]) / Z (2 ), DomainError )
2406
+ assert raises (lambda : MZ ([[2 , 5 ]]) / 2 , DomainError )
2407
+ # field division by scalar
2408
+ for (K , MK ) in [(Q , MQ ), (F17 , MF17 ), (F163 , MF163 )]:
2409
+ assert MK ([[2 , 5 ]]) / K (2 ) == MK ([[K (2 )/ K (2 ), K (5 )/ K (2 )]])
2410
+ assert MK ([[2 , 5 ]]) / 2 == MK ([[K (2 )/ K (2 ), K (5 )/ K (2 )]])
2411
+ # No other division is allowed
2412
+ for (R , MR ) in [(Z , MZ ), (Q , MQ ), (F17 , MF17 ), (F163 , MF163 )]:
2413
+ M = MR ([[2 , 5 ]])
2414
+ for s in (2 , R (2 )):
2415
+ assert raises (lambda : s / M , TypeError )
2416
+ assert raises (lambda : M // s , TypeError )
2417
+ assert raises (lambda : s // M , TypeError )
2418
+ assert raises (lambda : M % s , TypeError )
2419
+ assert raises (lambda : s % M , TypeError )
2420
+ assert raises (lambda : divmod (s , M ), TypeError )
2421
+ assert raises (lambda : divmod (M , s ), TypeError )
2422
+ assert raises (lambda : M / M , TypeError )
2423
+ assert raises (lambda : M // M , TypeError )
2424
+ assert raises (lambda : M % M , TypeError )
2425
+ assert raises (lambda : divmod (M , M ), TypeError )
2426
+ assert raises (lambda : M / 0 , ZeroDivisionError )
2427
+ assert raises (lambda : M / R (0 ), ZeroDivisionError )
2428
+
2429
+
2279
2430
def _all_polys ():
2280
2431
return [
2281
2432
# (poly_type, scalar_type, is_field)
@@ -2436,16 +2587,18 @@ def setbad(obj, i, val):
2436
2587
assert raises (lambda : P ([1 , 2 , 1 ]) % P ([0 ]), ZeroDivisionError )
2437
2588
assert raises (lambda : divmod (P ([1 , 2 , 1 ]), P ([0 ])), ZeroDivisionError )
2438
2589
2590
+ # Exact/field scalar division
2439
2591
if is_field :
2440
2592
assert P ([2 , 2 ]) / 2 == P ([1 , 1 ])
2441
2593
assert P ([1 , 2 ]) / 2 == P ([S (1 )/ 2 , 1 ])
2442
- assert raises (lambda : P ([1 , 2 ]) / 0 , ZeroDivisionError )
2443
2594
else :
2444
- assert raises (lambda : P ([2 , 2 ]) / 2 , TypeError )
2595
+ assert P ([2 , 2 ]) / 2 == P ([1 , 1 ])
2596
+ assert raises (lambda : P ([1 , 2 ]) / 2 , DomainError )
2597
+ assert raises (lambda : P ([1 , 2 ]) / 0 , ZeroDivisionError )
2445
2598
2446
- assert raises ( lambda : 1 / P ([1 , 1 ]), TypeError )
2447
- assert raises (lambda : P ([ 1 , 2 , 1 ]) / P ([1 , 1 ]), TypeError )
2448
- assert raises (lambda : P ([1 , 2 , 1 ]) / P ([1 , 2 ]), TypeError )
2599
+ assert P ([ 1 , 2 , 1 ]) / P ([1 , 1 ]) == P ([ 1 , 1 ] )
2600
+ assert raises (lambda : 1 / P ([1 , 1 ]), DomainError )
2601
+ assert raises (lambda : P ([1 , 2 , 1 ]) / P ([1 , 2 ]), DomainError )
2449
2602
2450
2603
assert P ([1 , 1 ]) ** 0 == P ([1 ])
2451
2604
assert P ([1 , 1 ]) ** 1 == P ([1 , 1 ])
@@ -3023,7 +3176,9 @@ def test_all_tests():
3023
3176
test_fmpz_mod_poly ,
3024
3177
test_fmpz_mod_mat ,
3025
3178
3026
- test_arb ,
3179
+ test_division_scalar ,
3180
+ test_division_poly ,
3181
+ test_division_matrix ,
3027
3182
3028
3183
test_polys ,
3029
3184
@@ -3048,7 +3203,9 @@ def test_all_tests():
3048
3203
test_matrices_rref ,
3049
3204
test_matrices_solve ,
3050
3205
3206
+ test_arb ,
3207
+
3051
3208
test_pickling ,
3052
- test_all_tests ,
3053
3209
3210
+ test_all_tests ,
3054
3211
]
0 commit comments