def test_dmp_zz_diophantine(): R, x, y = ring('x,y', ZZ) H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23] H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9] H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9] c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74 c_2 = (9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y) c_3 = (-36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y) p = 6291469 assert R.dmp_zz_diophantine(H_1, c_1, [ZZ(0)], 5, p) == [-3*x, -2, 1] assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6] assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1] R, x, y, z = ring('x,y,z', ZZ) F = [47*x*y + 9*z**3 - 9, 45*x**3 - 9*y**3 - y**2 + 3*z**3 - 6*z] c = (-270*x**3*z**3 + 270*x**3 + 94*x*y*z + 54*y**3*z**3 - 54*y**3 + 6*y**2*z**3 - 6*y**2 - 18*z**6 + 54*z**4 + 18*z**3 - 54*z) p = 2345258188817 assert R.dmp_zz_diophantine(F, c, [ZZ(-2), ZZ(0)], 6, p) == [-6*z**3 + 6, 2*z]
def test_dmp_mul_ground(): R, x = ring('x', ZZ) f = 0 assert R.dmp_mul_ground(f, ZZ(2)) == 0 f = x**2 + 2 * x - 1 assert R.dmp_mul_ground(f, ZZ(3)) == 3 * x**2 + 6 * x - 3 f = x**2 + 2 * x + 3 assert R.dmp_mul_ground(f, ZZ(0)) == 0 assert R.dmp_mul_ground(f, ZZ(2)) == 2 * x**2 + 4 * x + 6 R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert (R.dmp_mul_ground(f, ZZ(2)) == 2 * x**2 * y * z**2 + 4 * x**2 * y * z + 6 * x**2 * y + 4 * x**2 + 6 * x + 8 * y**2 * z**2 + 10 * y**2 * z + 12 * y**2 + 2 * y * z**2 + 4 * y * z + 2 * y + 2) R, x, y, z = ring('x y z', QQ) f = f.set_ring(R) / 7 assert (R.dmp_mul_ground(f, QQ( 1, 2)) == x**2 * y * z**2 / 14 + x**2 * y * z / 7 + 3 * x**2 * y / 14 + x**2 / 7 + 3 * x / 14 + 2 * y**2 * z**2 / 7 + 5 * y**2 * z / 14 + 3 * y**2 / 7 + y * z**2 / 14 + y * z / 7 + y / 14 + QQ(1, 14))
def test_dmp_eval_in(): R, x = ring('x', ZZ) assert R.dmp_eval_in(0, ZZ(7), 0) == 0 assert R.dmp_eval_in(x + 2, ZZ(0), 0) == 2 assert R.dmp_eval_in(x**2 + 2 * x + 3, ZZ(7), 0) == 66 assert R.dmp_eval_in(x**2 + 2 * x + 3, ZZ(2), 0) == 11 assert R.dmp_eval_in(0, ZZ(3), 0) == 0 R, x, y = ring('x y', ZZ) R1 = R.drop(x) assert R.dmp_eval_in(0, 3, 0) == 0 assert R.dmp_eval_in(y + 2, 0, 0) == R1.y + 2 assert R.dmp_eval_in(3 * x * y + 2 * x + y + 2, 3, 0) == 10 * R1.y + 8 assert R.dmp_eval_in(2 * x * y + 3 * x + y + 2, 2, 0) == 5 * R1.y + 8 R, x, y, z = ring('x y z', ZZ) R1 = R.drop(x) R3 = R.drop(z) assert R.dmp_eval_in(0, 3, 0) == 0 assert R.dmp_eval_in(1, 3, 0) == 1 assert R.dmp_eval_in(z + 2, 3, 0) == R1.z + 2 assert R.dmp_eval_in(3 * x * z + 2 * x + z + 2, 3, 0) == 10 * R1.z + 8 f = 45 * x**3 - 9 * y**3 - y**2 + 3 * z**3 + 10 * z assert R.dmp_eval_in(f, -2, 2) == 45 * R3.x**3 - 9 * R3.y**3 - R3.y**2 - 44 pytest.raises(IndexError, lambda: R.dmp_eval_in(f, -2, -1)) R, x, y, z, t = ring('x y z t', ZZ) f = f_polys()[6] R2 = R.drop(y) R3 = R.drop(z) x, z, t = R2.gens assert (R.dmp_eval_in(f, -2, 1) == -4230 * x**4 + 45 * x**3 * z**3 * t**2 - 45 * x**3 * t**2 - 282 * x * z**3 - 188 * x * z * t - 6392 * x + 3 * z**6 * t**2 + 2 * z**4 * t**3 + 65 * z**3 * t**2 - 2 * z * t**3 - 68 * t**2) assert (R.dmp_eval_in( f, 7, 1) == 14805 * x**4 + 45 * x**3 * z**3 * t**2 - 45 * x**3 * t**2 + 987 * x * z**3 + 658 * x * z * t - 1031744 * x + 3 * z**6 * t**2 + 2 * z**4 * t**3 - 3139 * z**3 * t**2 - 2 * z * t**3 + 3136 * t**2) x, y, t = R3.gens assert (R.dmp_eval_in(f, -2, 2) == 2115 * x**4 * y - 405 * x**3 * t**2 - 423 * x * y**4 - 47 * x * y**3 - 188 * x * y * t - 1128 * x * y + 81 * y**3 * t**2 + 9 * y**2 * t**2 + 36 * t**3 + 216 * t**2) assert (R.dmp_eval_in(f, 7, 2) == 2115 * x**4 * y + 15390 * x**3 * t**2 - 423 * x * y**4 - 47 * x * y**3 + 658 * x * y * t + 48363 * x * y - 3078 * y**3 * t**2 - 342 * y**2 * t**2 + 4788 * t**3 + 351918 * t**2)
def test_FractionField_methods(): F = ZZ.inject('x').field assert F.domain_new(2) == ZZ(2) x = symbols('x') assert F(x**2 + x) == F.x**2 + F.x
def test_QuotientRing(): I = QQ.old_poly_ring(x).ideal(x**2 + 1) R = QQ.old_poly_ring(x) / I assert R == QQ.old_poly_ring(x) / [x**2 + 1] assert R == QQ.old_poly_ring(x) / QQ.old_poly_ring(x).ideal(x**2 + 1) assert R != QQ.old_poly_ring(x) assert R.convert(1) / x == -x + I assert -1 + I == x**2 + I assert R.convert(ZZ(1), ZZ) == 1 + I assert R.convert(R.convert(x), R) == R.convert(x) X = R.convert(x) Y = QQ.old_poly_ring(x).convert(x) assert -1 + I == X**2 + I assert -1 + I == Y**2 + I assert R.to_diofant(X) == x pytest.raises( ValueError, lambda: QQ.old_poly_ring(x) / QQ.old_poly_ring(x, y).ideal(x)) R = QQ.old_poly_ring(x, order="ilex") I = R.ideal(x) assert R.convert(1) + I == (R / I).convert(1)
def test_dmp_convert(): K0, K1 = ZZ.inject('x'), ZZ assert dmp_convert([K0(1), K0(2)], 0, K0, K1) == [ZZ(1), ZZ(2)] assert dmp_convert([K1(1), K1(2)], 0, K1, K0) == [K0(1), K0(2)] f = [K0(1), K0(2), K0(0), K0(3)] assert dmp_convert(f, 0, K0, K1) == [ZZ(1), ZZ(2), ZZ(0), ZZ(3)] f = [[K0(1)], [K0(2)], [], [K0(3)]] assert dmp_convert(f, 1, K0, K1) == [[ZZ(1)], [ZZ(2)], [], [ZZ(3)]]
def test__zz_hensel_lift(): R, x = ring('x', ZZ) f = x**4 - 1 F = [x - 1, x - 2, x + 2, x + 1] assert R._zz_hensel_lift(ZZ(5), f, F, 4) == [x - 1, x - 182, x + 182, x + 1]
def test_pickling_polys_polyclasses(): for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c)
def test_arithmetics(): assert ZZ.rem(ZZ(2), ZZ(3)) == 2 assert ZZ.div(ZZ(2), ZZ(3)) == (0, 2) assert QQ.rem(QQ(2, 3), QQ(4, 7)) == 0 assert QQ.div(QQ(2, 3), QQ(4, 7)) == (QQ(7, 6), 0) assert QQ.lcm(QQ(2, 3), QQ(4, 9)) == QQ(4, 3) assert CC.gcd(CC(1), CC(2)) == 1 assert CC.lcm(CC(1), CC(2)) == 2 assert EX(Rational(2, 3)).numerator == 2 assert EX(Rational(2, 3)).denominator == 3 assert abs(EX(-2)) == 2 assert -EX(2) == -2 assert 2 + EX(3) == EX(3) + 2 == 5 assert 2 - EX(3) == EX(2) - 3 == -1 assert 2*EX(3) == EX(3)*2 == 6 assert 2/EX(3) == EX(2)/3 == EX(Rational(2, 3)) assert EX(2)**2 == 4 pytest.raises(TypeError, lambda: EX(2) + object()) pytest.raises(TypeError, lambda: EX(2) - object()) pytest.raises(TypeError, lambda: EX(2)*object()) pytest.raises(TypeError, lambda: EX(2)**object()) pytest.raises(TypeError, lambda: EX(2)/object()) F11 = FF(11) assert +F11(2) == F11(2) assert F11(5) + 7 == 7 + F11(5) == F11(1) assert F11(5) - 7 == 5 - F11(7) == F11(9) assert F11(5)*7 == 7*F11(5) == F11(2) assert F11(5)/9 == 5/F11(9) == F11(3) assert F11(4) % 9 == 4 % F11(9) == F11(4) pytest.raises(TypeError, lambda: F11(2) + object()) pytest.raises(TypeError, lambda: F11(2) - object()) pytest.raises(TypeError, lambda: F11(2)*object()) pytest.raises(TypeError, lambda: F11(2)**object()) pytest.raises(TypeError, lambda: F11(2)/object()) pytest.raises(TypeError, lambda: F11(2) % object()) pytest.raises(TypeError, lambda: object() % F11(2))
def test_dmp_quo_ground(): R, x = ring('x', ZZ) pytest.raises(ZeroDivisionError, lambda: R.dmp_quo_ground(x**2 + 2 * x + 3, ZZ(0))) f = 3 * x**2 + 2 assert R.dmp_quo_ground(f, ZZ(2)) == x**2 + 1 R, x = ring('x', QQ) f = 3 * x**2 + 2 assert R.dmp_quo_ground(f, QQ(2)) == 3 * x**2 / 2 + 1 R, x = ring('x', ZZ) f = 0 assert R.dmp_quo_ground(f, ZZ(3)) == 0 f = 6 * x**2 + 2 * x + 8 assert R.dmp_quo_ground(f, ZZ(1)) == f assert R.dmp_quo_ground(f, ZZ(2)) == 3 * x**2 + x + 4 assert R.dmp_quo_ground(f, ZZ(3)) == 2 * x**2 + 2 R, x = ring('x', QQ) f = 6 * x**2 + 2 * x + 8 assert R.dmp_quo_ground(f, QQ(1)) == f assert R.dmp_quo_ground(f, QQ(2)) == 3 * x**2 + x + 4 assert R.dmp_quo_ground(f, QQ(7)) == 6 * x**2 / 7 + 2 * x / 7 + QQ(8, 7) R, x, y = ring('x y', ZZ) f = 6 * x**2 + 2 * x + 8 assert R.dmp_quo_ground(f, ZZ(1)) == f assert R.dmp_quo_ground(f, ZZ(2)) == 3 * x**2 + x + 4 assert R.dmp_quo_ground(f, ZZ(3)) == 2 * x**2 + 2
def test_dmp_ground_trunc(): R, x = ring('x', ZZ) assert R.dmp_ground_trunc( x**5 + 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6, ZZ(3)) == x**5 - x**4 + x**2 - x assert R.dmp_ground_trunc( 6 * x**5 + 5 * x**4 + 4 * x**3 + 3 * x**2 + 2 * x + 1, ZZ(3)) == -x**4 + x**3 - x + 1 R, x = ring('x', QQ) assert R.dmp_ground_trunc( x**5 + 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6, ZZ(3)) == x**5 + 2 * x**4 + x**2 + 2 * x R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert R.dmp_ground_trunc( f, ZZ(3)) == (x**2 * y * z**2 - x**2 * y * z - x**2 + y**2 * z**2 - y**2 * z + y * z**2 - y * z + y + 1)
def test_Domain_convert(): assert QQ.convert(10e-52) == QQ( 1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576) R, x = ring('x', ZZ) assert ZZ.convert(x - x) == 0 assert ZZ.convert(x - x, R) == 0 F3 = FF(3) assert F3.convert(Float(2.0)) == F3.dtype(2) assert F3.convert(PythonRational(2, 1)) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2))) assert F3.convert(2.0) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(2.1)) assert RR.convert(CC(1)) == RR(1) pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2))) assert QQ.convert(ALG(1), ALG) == QQ(1) pytest.raises(CoercionFailed, lambda: QQ.convert(ALG([1, 1]), ALG)) assert ZZ.convert(ALG(1), ALG) == ZZ(1) pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG([1, 1]), ALG)) assert EX.convert(ALG([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1 ALG2 = QQ.algebraic_field(sqrt(2)) a2 = ALG2.convert(sqrt(2)) a = ALG.convert(a2, ALG2) assert a.rep.all_coeffs() == [0, -QQ(9, 2), 0, QQ(1, 2)] assert RR.convert(a) == RR(1.4142135623730951) assert CC.convert(a) == CC(1.4142135623730951) assert ZZ_python.convert(3.0) == ZZ_python.dtype(3) pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2)) assert CC.convert(QQ_python(1, 2)) == CC(0.5) CC01 = ComplexField(tol=0.1) assert CC.convert(CC01(0.3)) == CC(0.3) assert RR.convert(complex(2 + 0j)) == RR(2) pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j))) assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2)) pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX)) pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
def test_dmp_zz_wang(): R, x, y, z = ring('x,y,z', ZZ) UV, _x = ring('x', ZZ) p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1))) assert p == 6291469 t_1, k_1, e_1 = y, 1, ZZ(-14) t_2, k_2, e_2 = z, 2, ZZ(3) t_3, k_3, e_3 = y + z, 2, ZZ(-11) t_4, k_4, e_4 = y - z, 1, ZZ(-17) T = [t_1, t_2, t_3, t_4] K = [k_1, k_2, k_3, k_4] E = [e_1, e_2, e_3, e_4] T = zip([t.drop(x) for t in T], K) A = [ZZ(-14), ZZ(3)] S = R.dmp_eval_tail(w_1, A) cs, s = S.primitive() assert cs == 1 and s == S == (1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644) assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17] assert s.is_squarefree and UV.dmp_degree_in(s, 0) == R.dmp_degree_in(w_1, 0) _, H = UV.dup_zz_factor_sqf(s) h_1 = 44*_x**2 + 42*_x + 1 h_2 = 126*_x**2 - 9*_x + 28 h_3 = 187*_x**2 - 23 LC = [lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2]] factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p) assert H == [h_1, h_2, h_3] assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC) assert R.dmp_expand(factors) == w_1
def test_dmp_ground_p(): assert dmp_ground_p([], 0, 0) is True assert dmp_ground_p([[]], 0, 1) is True assert dmp_ground_p([[]], 1, 1) is False assert dmp_ground_p([[ZZ(1)]], 1, 1) is True assert dmp_ground_p([[[ZZ(2)]]], 2, 2) is True assert dmp_ground_p([[[ZZ(2)]]], 3, 2) is False assert dmp_ground_p([[[ZZ(3)], []]], 3, 2) is False assert dmp_ground_p([], None, 0) is True assert dmp_ground_p([[]], None, 1) is True assert dmp_ground_p([ZZ(1)], None, 0) is True assert dmp_ground_p([[[ZZ(1)]]], None, 2) is True assert dmp_ground_p([[[ZZ(3)], []]], None, 2) is False
def test_dmp_eval_tail(): R, x, y = ring('x y', ZZ) assert R.dmp_eval_tail(0, [ZZ(1)]) == 0 f = 2 * x * y + 3 * x + y + 2 R0 = R.drop(y) assert R.dmp_eval_tail(f, [ZZ(2)]) == 7 * R0.x + 4 assert R.dmp_eval_tail(f, [ZZ(2), ZZ(2)]) == 18 R, x, y, z = ring('x y z', ZZ) R12 = R.drop(y, z) R2 = R.drop(z) assert R.dmp_eval_tail(0, [ZZ(1)]) == 0 assert R.dmp_eval_tail(0, [ZZ(1), ZZ(2)]) == 0 f = f_polys()[0] assert R.dmp_eval_tail(f, []) == f assert R.dmp_eval_tail(f, [ZZ(1), ZZ(-17), ZZ(8)]) == 84496 assert R.dmp_eval_tail( f, [ZZ(-17), ZZ(8)]) == -1409 * R12.x**2 + 3 * R12.x + 85902 assert (R.dmp_eval_tail(f, [ZZ(8)]) == 83 * R2.x**2 * R2.y + 2 * R2.x**2 + 3 * R2.x + 302 * R2.y**2 + 81 * R2.y + 1) f = f_polys()[1] assert (R.dmp_eval_tail(f, [ZZ(-17), ZZ(8)]) == -136 * R12.x**3 + 15699 * R12.x**2 + 9166 * R12.x - 27144) f = f_polys()[2] assert (R.dmp_eval_tail(f, [ZZ(-12), ZZ(3)]) == -1377 * R12.x**5 - 702 * R12.x**3 - 1224 * R12.x**2 - 624) f = f_polys()[3] assert (R.dmp_eval_tail( f, [ZZ(-12), ZZ(3)]) == 144 * R12.x**5 + 82 * R12.x**4 - 5181 * R12.x**3 - 28872 * R12.x**2 - 14868 * R12.x - 540) f = f_polys()[4] assert (R.dmp_eval_tail(f, [ZZ(25), ZZ(-1)]) == 152587890625 * R12.x**9 + 9765625 * R12.x**8 - 59605407714843750 * R12.x**7 - 3839159765625 * R12.x**6 - 1562475 * R12.x**5 + 9536712644531250 * R12.x**4 + 610349546750 * R12.x**3 - 4 * R12.x**2 + 24414375000 * R12.x + 1562520) f = f_polys()[5] assert (R.dmp_eval_tail(f, [ZZ(25), ZZ(-1)]) == -R12.x**3 - 78 * R12.x**2 - 2028 * R12.x - 17576) R, x, y, z, t = ring('x y z t', ZZ) R123 = R.drop(y, z, t) f = f_polys()[6] assert R.dmp_eval_tail(f, [ZZ(0), ZZ(2), ZZ(4)]) == 5040 * R123.x**3 + 4480
def test_Ring(): assert ZZ(3).numerator == 3 assert ZZ(3).denominator == 1
def test__zz_wang(): R, x, y, z = ring('x y z', ZZ) UV, _x = ring('x', ZZ) p = ZZ(nextprime(R._zz_mignotte_bound(w_1))) assert p == 6291469 t_1, k_1, e_1 = y, 1, ZZ(-14) t_2, k_2, e_2 = z, 2, ZZ(3) t_3, k_3, e_3 = y + z, 2, ZZ(-11) t_4, k_4, e_4 = y - z, 1, ZZ(-17) T = [t_1, t_2, t_3, t_4] K = [k_1, k_2, k_3, k_4] E = [e_1, e_2, e_3, e_4] T = list(zip([t.drop(x) for t in T], K)) A = [ZZ(-14), ZZ(3)] S = w_1.eval([(y, A[0]), (z, A[1])]) cs, s = S.primitive() assert cs == 1 and s == S == (1036728 * _x**6 + 915552 * _x**5 + 55748 * _x**4 + 105621 * _x**3 - 17304 * _x**2 - 26841 * _x - 644) assert R._zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17] assert s.is_squarefree and s.degree() == w_1.degree() _, H = UV._zz_factor_sqf(s) h_1 = 187 * _x**2 - 23 h_2 = 44 * _x**2 + 42 * _x + 1 h_3 = 126 * _x**2 - 9 * _x + 28 LC = [lc.drop(x) for lc in [y**2 - z**2, -4 * y - 4 * z, -y * z**2]] factors = R._zz_wang_hensel_lifting(w_1, H, LC, A, p) assert H == [h_1, h_2, h_3] assert R._zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC) assert functools.reduce(operator.mul, factors) == w_1 # coverage tests f = x**6 + 5 * x**4 * y - 5 * x**2 * y**2 - y**3 assert R._zz_wang(f, mod=4, seed=1) == [x**2 - y, x**4 + 6 * x**2 * y + y**2] # This tests a bug in the Wang algorithm that occured only with a very # specific set of random numbers; issue sympy/sympy#6355. random_sequence = [ -1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3 ] R, x, y, z = ring('x y z', ZZ) f = 2 * x**2 + y * z - y - z**2 + z assert R._zz_wang(f, seed=random_sequence) == [f] with using(eez_restart_if_needed=False): pytest.raises(ExtraneousFactors, lambda: R._zz_wang(f, seed=random_sequence))
def test_mulmatscaler(): a = eye(3, ZZ) b = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]] assert mulmatscaler(a, ZZ(4), ZZ) == [[ZZ(4), ZZ(0), ZZ(0)], [ZZ(0), ZZ(4), ZZ(0)], [ZZ(0), ZZ(0), ZZ(4)]] assert mulmatscaler(b, ZZ(1), ZZ) == [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
def test_pickling_polys_polyclasses(): for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)): check(c)
def test_transpose(): a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]] b = eye(4, ZZ) assert transpose(a, ZZ) == ([[ZZ(3), ZZ(2), ZZ(6)], [ZZ(7), ZZ(4), ZZ(2)], [ZZ(4), ZZ(5), ZZ(3)]]) assert transpose(b, ZZ) == b
f = -x**3 + I * x**2 + sqrt(2) * x - 1 assert R._isolate_complex_roots_sqf(f) == [ ((QQ(-283, 100), 0), (0, QQ(283, 100))), ((0, QQ(-283, 100)), (QQ(283, 100), 0)), ((0, 0), (QQ(283, 100), QQ(283, 100))) ] R, x = ring('x', EX) pytest.raises(DomainError, lambda: R._isolate_complex_roots_sqf(x)) @pytest.mark.timeout(200) @pytest.mark.slow @pytest.mark.skipif(isinstance(ZZ(42), int), reason='gmpy2 is not used') def test__isolate_complex_roots_sqf_2(): R, x = ring('x', ZZ) f = x**40 - 15 * x**17 - 21 * x**3 + 11 res = R._isolate_complex_roots_sqf(f) ans = [((QQ(-21, 16), QQ(-21, 128)), (QQ(-63, 64), 0)), ((QQ(-21, 16), 0), (QQ(-63, 64), QQ(21, 128))), ((QQ(-21, 16), QQ(-21, 64)), (QQ(-63, 64), QQ(-21, 128))), ((QQ(-21, 16), QQ(21, 128)), (QQ(-63, 64), QQ(21, 64))), ((QQ(-21, 16), QQ(-21, 32)), (QQ(-63, 64), QQ(-21, 64))), ((QQ(-21, 16), QQ(21, 64)), (QQ(-63, 64), QQ(21, 32))), ((QQ(-63, 64), QQ(-21, 32)), (QQ(-21, 32), QQ(-21, 64))), ((QQ(-63, 64), QQ(21, 64)), (QQ(-21, 32), QQ(21, 32))), ((QQ(-63, 64), QQ(-105, 128)), (QQ(-21, 32), QQ(-21, 32))),
def test_construct_domain(): assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([Integer(1), Integer(2), Integer(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain( [Integer(1), Integer(2), Integer(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([Rational(1, 2), Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)]) assert construct_domain([3.14, 1, Rational(1, 2) ]) == (RR, [RR(3.14), RR(1.0), RR(0.5)]) assert construct_domain([3.14, sqrt(2)], extension=False) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([3.14, sqrt(2)]) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([sqrt(2), 3.14]) == (EX, [EX(sqrt(2)), EX(3.14)]) assert construct_domain([1, sqrt(2)], extension=False) == (EX, [EX(1), EX(sqrt(2))]) assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))]) assert construct_domain([x, sqrt(x), sqrt(y) ]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))]) alg = QQ.algebraic_field(sqrt(2)) assert (construct_domain( [7, Rational(1, 2), sqrt(2)]) == (alg, [alg([7]), alg([Rational(1, 2)]), alg([1, 0])])) alg = QQ.algebraic_field(sqrt(2) + sqrt(3)) assert (construct_domain([7, sqrt(2), sqrt(3)]) == (alg, [ alg([7]), alg.from_expr(sqrt(2)), alg.from_expr(sqrt(3)) ])) dom = ZZ.inject(x) assert construct_domain([2 * x, 3]) == (dom, [dom(2 * x), dom(3)]) dom = ZZ.inject(x, y) assert construct_domain([2 * x, 3 * y]) == (dom, [dom(2 * x), dom(3 * y)]) dom = QQ.inject(x) assert construct_domain([x / 2, 3]) == (dom, [dom(x / 2), dom(3)]) dom = QQ.inject(x, y) assert construct_domain([x / 2, 3 * y]) == (dom, [dom(x / 2), dom(3 * y)]) dom = RR.inject(x) assert construct_domain([x / 2, 3.5]) == (dom, [dom(x / 2), dom(3.5)]) dom = RR.inject(x, y) assert construct_domain([x / 2, 3.5 * y]) == (dom, [dom(x / 2), dom(3.5 * y)]) dom = ZZ.inject(x).field assert construct_domain([2 / x, 3]) == (dom, [dom(2 / x), dom(3)]) dom = ZZ.inject(x, y).field assert construct_domain([2 / x, 3 * y]) == (dom, [dom(2 / x), dom(3 * y)]) dom = RR.inject(x).field assert construct_domain([2 / x, 3.5]) == (dom, [dom(2 / x), dom(3.5)]) dom = RR.inject(x, y).field assert construct_domain([2 / x, 3.5 * y]) == (dom, [dom(2 / x), dom(3.5 * y)]) assert construct_domain(2) == (ZZ, ZZ(2)) assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3)) assert construct_domain({}) == (ZZ, {}) assert construct_domain([-x * y + x * (y + 42) - 42 * x ]) == (EX, [EX(-x * y + x * (y + 42) - 42 * x)])
def test_AlgebraicElement(): A = QQ.algebraic_field(I) rep = [QQ(1), QQ(1)] mod = [QQ(1), QQ(0), QQ(1)] f = A(rep) assert f.rep.all_coeffs() == rep assert f.mod.all_coeffs() == mod assert f.domain.domain == QQ f = A(1) assert f.rep.all_coeffs() == [QQ(1)] assert f.mod.all_coeffs() == mod assert f.domain.domain == QQ f = A([QQ(3, 2)]) assert f.rep.all_coeffs() == [QQ(3, 2)] assert f.mod.all_coeffs() == mod assert f.domain.domain == QQ B = QQ.algebraic_field(I * sqrt(2)) a = A([QQ(1), QQ(1)]) b = B([QQ(1), QQ(1)]) assert (a == a) is True assert (a != a) is False assert (a == b) is False assert (a != b) is True b = A([QQ(1), QQ(2)]) assert (a == b) is False assert (a != b) is True assert A([1, 1]) == A([int(1), int(1)]) assert hash(A([1, 1])) == hash(A([int(1), int(1)])) assert a.to_dict() == {(0, ): QQ(1), (1, ): QQ(1)} assert bool(A([])) is False assert bool(A([QQ(1)])) is True a = A([QQ(2), -QQ(1), QQ(1)]) assert a.rep.all_coeffs() == [1, -1] A = QQ.algebraic_field(root(2, 3)) assert A.unit > 0 assert A.unit >= 0 assert (A.unit < 0) is False assert (A.unit <= 0) is False pytest.raises(TypeError, lambda: A.unit > x) pytest.raises(TypeError, lambda: QQ.algebraic_field(I).unit > 0) assert abs(+A.unit) == A.unit assert abs(-A.unit) == A.unit a = A([QQ(1), QQ(-1), QQ(2)]) b = A([QQ(2), QQ(1)]) c = A([QQ(-1), QQ(1), QQ(-2)]) assert +a == a assert -a == c c = A([QQ(3), QQ(0), QQ(2)]) assert a + b == c assert b + a == c assert c + 1 == A([QQ(4), QQ(0), QQ(2)]) pytest.raises(TypeError, lambda: c + 'x') pytest.raises(TypeError, lambda: 'x' + c) c = A([QQ(-1), QQ(-2), QQ(2)]) assert a - b == c c = A([QQ(1), QQ(2), QQ(-2)]) assert b - a == c assert c - 1 == A([QQ(0), QQ(2), QQ(-2)]) pytest.raises(TypeError, lambda: c - 'x') pytest.raises(TypeError, lambda: 'x' - c) c = A([QQ(6), QQ(-1), QQ(3)]) assert a * b == c assert b * a == c assert c * 2 == A([QQ(12), QQ(-2), QQ(6)]) pytest.raises(TypeError, lambda: c * 'x') pytest.raises(TypeError, lambda: 'x' * c) c = A([-QQ(3, 5), -QQ(1, 5), QQ(11, 10)]) assert c / 2 == A([-QQ(3, 10), -QQ(1, 10), QQ(11, 20)]) pytest.raises(TypeError, lambda: c / 'x') pytest.raises(TypeError, lambda: 'x' / c) c = A([QQ(5, 43), QQ(9, 43), QQ(-1, 43)]) assert a**0 == A(1) assert a**1 == a assert a**-1 == c pytest.raises(TypeError, lambda: a**QQ(1, 2)) assert a * a**(-1) == A(1) assert 1 / a == a**(-1) A = QQ.algebraic_field(I) a = A([QQ(2), QQ(1), QQ(1, 2)]) b = A([ZZ(2), ZZ(1), ZZ(1)]) c = A([QQ(4), QQ(2), QQ(3, 2)]) assert a + b == b + a == c
def test_sub(): a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]] b = [[ZZ(5), ZZ(4), ZZ(9)], [ZZ(3), ZZ(7), ZZ(1)], [ZZ(12), ZZ(13), ZZ(14)]] c = [[ZZ(12)], [ZZ(17)], [ZZ(21)]] d = [[ZZ(3)], [ZZ(4)], [ZZ(5)]] e = [[ZZ(12), ZZ(78)], [ZZ(56), ZZ(79)]] f = [[ZZ.zero, ZZ.zero], [ZZ.zero, ZZ.zero]] assert sub(a, b, ZZ) == [[ZZ(-2), ZZ(3), ZZ(-5)], [ZZ(-1), ZZ(-3), ZZ(4)], [ZZ(-6), ZZ(-11), ZZ(-11)]] assert sub(c, d, ZZ) == [[ZZ(9)], [ZZ(13)], [ZZ(16)]] assert sub(e, f, ZZ) == e
def test_mulmatmat(): a = [[ZZ(3), ZZ(4)], [ZZ(5), ZZ(6)]] b = [[ZZ(1), ZZ(2)], [ZZ(7), ZZ(8)]] c = eye(2, ZZ) d = [[ZZ(6)], [ZZ(7)]] assert mulmatmat(a, b, ZZ) == [[ZZ(31), ZZ(38)], [ZZ(47), ZZ(58)]] assert mulmatmat(b, d, ZZ) == [[ZZ(20)], [ZZ(98)]]
def test_dmp_nest(): assert dmp_nest(ZZ(1), 2, ZZ) == [[[1]]] assert dmp_nest([[1]], 0, ZZ) == [[1]] assert dmp_nest([[1]], 1, ZZ) == [[[1]]] assert dmp_nest([[1]], 2, ZZ) == [[[[1]]]]
def test_add(): a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]] b = [[ZZ(5), ZZ(4), ZZ(9)], [ZZ(3), ZZ(7), ZZ(1)], [ZZ(12), ZZ(13), ZZ(14)]] c = [[ZZ(12)], [ZZ(17)], [ZZ(21)]] d = [[ZZ(3)], [ZZ(4)], [ZZ(5)]] e = [[ZZ(12), ZZ(78)], [ZZ(56), ZZ(79)]] f = [[ZZ.zero, ZZ.zero], [ZZ.zero, ZZ.zero]] assert add(a, b, ZZ) == [[ZZ(8), ZZ(11), ZZ(13)], [ZZ(5), ZZ(11), ZZ(6)], [ZZ(18), ZZ(15), ZZ(17)]] assert add(c, d, ZZ) == [[ZZ(15)], [ZZ(21)], [ZZ(26)]] assert add(e, f, ZZ) == e
A3 = QQ.algebraic_field(sqrt(3)) R, x, y, z, t = ring('x y z t', A3) f1 = x*y - sqrt(3) f2 = z*t + 1 f3 = x**2 + 1 f4 = x**2 + z*t f = f1*f2*f3*f4 for seed in [0, 1, 2, 6]: random.seed(seed) assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1), (f4, 1)]) @pytest.mark.slow @pytest.mark.skipif(isinstance(ZZ(42), int), reason='not with gmpy') def test_efactor_wang(): a = (-1 + sqrt(5))/4 - I*sqrt((sqrt(5) + 5)/8) A = QQ.algebraic_field(a) a = A.unit R, x, y, z = ring('x y z', A) f1 = x**2 - 2*a**2*x + a**3*z**2 - (a**3 + a**2 + a + 1)*y + 12*a f2 = x**2 - 2*a**3*x + a**2*z**2 + a*y - 12*(a**3 + a**2 + a + 1) f3 = x**2 + 2*(a**3 + a**2 + a + 1)*x + a*z**2 + a**3*y + 12*a**2 f4 = x**2 - 2*a*x - (a**3 + a**2 + a + 1)*z**2 + a**2*y + 12*a**3 f = f1*f2*f3*f4 assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1), (f4, 1)])
def test_trace(): a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]] b = eye(2, ZZ) assert trace(a, ZZ) == ZZ(10) assert trace(b, ZZ) == ZZ(2)
def test_dmp_ground(): assert dmp_ground(ZZ(0), 2) == [[[]]] assert dmp_ground(ZZ(7), -1) == ZZ(7) assert dmp_ground(ZZ(7), 0) == [ZZ(7)] assert dmp_ground(ZZ(7), 2) == [[[ZZ(7)]]]