Пример #1
0
def test_dmp_ground_trunc():
    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)
Пример #2
0
def test_dmp_add_term():
    R, x = ring('x', ZZ)

    f = 0

    assert R.dmp_add_term(f, 0, 0) == 0
    assert R.dmp_add_term(f, 1, 0) == 1
    assert R.dmp_add_term(f, 1, 1) == x
    assert R.dmp_add_term(f, 1, 2) == x**2

    f = x**2 + x + 1

    assert R.dmp_add_term(f, 1, 0) == x**2 + x + 2
    assert R.dmp_add_term(f, 1, 1) == x**2 + 2*x + 1
    assert R.dmp_add_term(f, 1, 2) == 2*x**2 + x + 1

    assert R.dmp_add_term(f, 1, 3) == x**3 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 4) == x**4 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 5) == x**5 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 6) == x**6 + x**2 + x + 1

    assert R.dmp_add_term(f, -1, 2) == x + 1

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_add_term(f, 0, 3) == f

    R, x, y, z = ring('x y z', QQ)

    f = f.set_ring(R)/7

    assert R.dmp_add_term(f, 0, 3) == f
Пример #3
0
def test_dmp_add_term():
    R, x = ring('x', ZZ)

    f = 0

    assert R.dmp_add_term(f, 0, 0) == 0
    assert R.dmp_add_term(f, 1, 0) == 1
    assert R.dmp_add_term(f, 1, 1) == x
    assert R.dmp_add_term(f, 1, 2) == x**2

    f = x**2 + x + 1

    assert R.dmp_add_term(f, 1, 0) == x**2 + x + 2
    assert R.dmp_add_term(f, 1, 1) == x**2 + 2 * x + 1
    assert R.dmp_add_term(f, 1, 2) == 2 * x**2 + x + 1

    assert R.dmp_add_term(f, 1, 3) == x**3 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 4) == x**4 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 5) == x**5 + x**2 + x + 1
    assert R.dmp_add_term(f, 1, 6) == x**6 + x**2 + x + 1

    assert R.dmp_add_term(f, -1, 2) == x + 1

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_add_term(f, 0, 3) == f

    R, x, y, z = ring('x y z', QQ)

    f = f.set_ring(R) / 7

    assert R.dmp_add_term(f, 0, 3) == f
Пример #4
0
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))
Пример #5
0
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))
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
def test_dmp_l1_norm():
    R, x = ring('x', ZZ)

    assert R.dmp_l1_norm(0) == 0
    assert R.dmp_l1_norm(1) == 1
    assert R.dmp_l1_norm(2*x**3 - 3*x**2 + 1) == 6
    assert R.dmp_l1_norm(x**3 + 4*x**2 + 2*x + 3) == 10

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_l1_norm(0) == 0
    assert R.dmp_l1_norm(1) == 1

    assert R.dmp_l1_norm(f_polys()[0]) == 31
Пример #9
0
def test_dmp_l1_norm():
    R, x = ring('x', ZZ)

    assert R.dmp_l1_norm(0) == 0
    assert R.dmp_l1_norm(1) == 1
    assert R.dmp_l1_norm(2 * x**3 - 3 * x**2 + 1) == 6
    assert R.dmp_l1_norm(x**3 + 4 * x**2 + 2 * x + 3) == 10

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_l1_norm(0) == 0
    assert R.dmp_l1_norm(1) == 1

    assert R.dmp_l1_norm(f_polys()[0]) == 31
Пример #10
0
def test_dmp_max_norm():
    R, x = ring('x', ZZ)

    assert R.dmp_max_norm(0) == 0
    assert R.dmp_max_norm(1) == 1
    assert R.dmp_max_norm(-x**2 + 2*x - 3) == 3

    assert R.dmp_max_norm(x**3 + 4*x**2 + 2*x + 3) == 4

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_max_norm(0) == 0
    assert R.dmp_max_norm(1) == 1

    assert R.dmp_max_norm(f_polys()[0]) == 6
Пример #11
0
def test_dmp_max_norm():
    R, x = ring('x', ZZ)

    assert R.dmp_max_norm(0) == 0
    assert R.dmp_max_norm(1) == 1
    assert R.dmp_max_norm(-x**2 + 2 * x - 3) == 3

    assert R.dmp_max_norm(x**3 + 4 * x**2 + 2 * x + 3) == 4

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_max_norm(0) == 0
    assert R.dmp_max_norm(1) == 1

    assert R.dmp_max_norm(f_polys()[0]) == 6
Пример #12
0
def test_dmp_sub_term():
    R, x = ring('x', ZZ)

    f = 0

    assert R.dmp_sub_term(f, 0, 0) == 0

    assert R.dmp_sub_term(f, 1, 0) == -1
    assert R.dmp_sub_term(f, 1, 1) == -x
    assert R.dmp_sub_term(f, 1, 2) == -x**2

    f = x**2 + x + 1

    assert R.dmp_sub_term(f, 2, 0) == x**2 + x - 1
    assert R.dmp_sub_term(f, 2, 1) == x**2 - x + 1
    assert R.dmp_sub_term(f, 2, 2) == -x**2 + x + 1

    assert R.dmp_sub_term(f, 1, 3) == -x**3 + x**2 + x + 1
    assert R.dmp_sub_term(f, 1, 4) == -x**4 + x**2 + x + 1
    assert R.dmp_sub_term(f, 1, 5) == -x**5 + x**2 + x + 1
    assert R.dmp_sub_term(f, 1, 6) == -x**6 + x**2 + x + 1

    assert R.dmp_sub_term(f, 1, 2) == x + 1

    assert R.dmp_sub_term(2*x**4 + x**2 - 1, ZZ(2), 4) == x**2 - 1

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_sub_term(f, 0, 3) == f

    R, x, y, z = ring('x y z', QQ)

    f = f.set_ring(R)/7

    assert R.dmp_sub_term(f, 0, 3) == f

    R, x, y = ring('x y', ZZ)

    f = 2*x**2 + x*y + 1

    assert R.dmp_sub_term(f, 2, 3) == -2*x**3 + 2*x**2 + x*y + 1
    assert R.dmp_sub_term(f, 2, 1) == 2*x**2 + x*y - 2*x + 1

    assert R.dmp_sub_term(f, 2, 2) == x*y + 1
Пример #13
0
def test_dmp_diff_eval_in():
    R, x = ring('x', ZZ)

    assert R.dmp_diff_eval_in(x**2 + x + 1, 1, 1, 0) == 3

    R, x, y = ring('x y', ZZ)
    R0 = R.drop(x)

    f = x * y**2 + 2 * x * y + 3 * x + 2 * y**2 + 3 * y + 1

    assert R.dmp_diff_eval_in(f, 1, 2, 0) == R0.y**2 + 2 * R0.y + 3

    R, x, y, z, t = ring('x y z t', ZZ)
    R1 = R.drop(y)

    f = f_polys()[6]

    assert (R.dmp_diff_eval_in(f, 2, 7, 1) == -250698 * R1.x -
            380 * R1.z**3 * R1.t**2 + 380 * R1.t**2)

    pytest.raises(IndexError, lambda: R.dmp_diff_eval_in(f, 2, 7, 4))
Пример #14
0
def test_dmp_diff_eval_in():
    R, x = ring('x', ZZ)

    assert R.dmp_diff_eval_in(x**2 + x + 1, 1, 1, 0) == 3

    R, x, y = ring('x y', ZZ)
    R0 = R.drop(x)

    f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1

    assert R.dmp_diff_eval_in(f, 1, 2, 0) == R0.y**2 + 2*R0.y + 3

    R, x, y, z, t = ring('x y z t', ZZ)
    R1 = R.drop(y)

    f = f_polys()[6]

    assert (R.dmp_diff_eval_in(f, 2, 7, 1) ==
            -250698*R1.x - 380*R1.z**3*R1.t**2 + 380*R1.t**2)

    pytest.raises(IndexError, lambda: R.dmp_diff_eval_in(f, 2, 7, 4))
Пример #15
0
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)
Пример #16
0
import pytest

from diofant.domains import CC, QQ, RR, ZZ
from diofant.functions import sqrt
from diofant.polys.polyconfig import using
from diofant.polys.polyerrors import HeuristicGCDFailed, NotInvertible
from diofant.polys.rings import ring
from diofant.polys.specialpolys import (dmp_fateman_poly_F_1,
                                        dmp_fateman_poly_F_2,
                                        dmp_fateman_poly_F_3, f_polys)


__all__ = ()

f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()


def test_dup_gcdex():
    R, x = ring("x", QQ)

    f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
    g = x**3 + x**2 - 4*x - 4

    s = -x/5 + QQ(3, 5)
    t = x**2/5 - 6*x/5 + 2
    h = x + 1

    assert R.dup_half_gcdex(f, g) == (s, h)
    assert R.dup_gcdex(f, g) == (s, t, h)
Пример #17
0
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
Пример #18
0
                                      dmp_integrate_in, dmp_lift, dmp_trunc,
                                      dup_clear_denoms, dup_decompose,
                                      dup_diff, dup_eval, dup_integrate,
                                      dup_mirror, dup_real_imag, dup_revert,
                                      dup_scale, dup_shift,
                                      dup_sign_variations, dup_transform,
                                      dup_trunc)
from diofant.polys.polyerrors import (DomainError, ExactQuotientFailed,
                                      NotReversible)
from diofant.polys.rings import ring
from diofant.polys.specialpolys import f_polys


__all__ = ()

f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]


def test_dup_integrate():
    assert dup_integrate([], 1, QQ) == []
    assert dup_integrate([], 2, QQ) == []

    assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
    assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]

    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == \
        [QQ(1), QQ(2), QQ(3)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == \
        [QQ(1, 3), QQ(1), QQ(3), QQ(0)]
    assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == \
        [QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
Пример #19
0
def test_dmp_diff_in():
    R, x = ring('x', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(7, 1, 0) == 0
    assert R.dmp_diff_in(2 * x + 7, 1, 0) == 2
    assert R.dmp_diff_in(x**2 + 2 * x + 1, 1, 0) == 2 * x + 2
    assert R.dmp_diff_in(x**3 + 2 * x**2 + 3 * x + 4, 1,
                         0) == 3 * x**2 + 4 * x + 3
    assert R.dmp_diff_in(x**4 - x**3 + 2, 1, 0) == 4 * x**3 - 3 * x**2
    assert R.dmp_diff_in(x**3 + 2 * x**2 + 3 * x + 4, 2, 0) == 6 * x + 4

    f = 17 * x**10 + 34 * x**9 + 56 * x**8 - 345 * x**7 + 23 * x**6 + 76 * x**5 + 12 * x**2 + 3 * x + 7

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

    R, x = ring('x', FF(3))

    f = 2 * x**10 + x**9 + 2 * x**8 + 2 * x**6 + x**5 + 1

    assert R.dmp_diff_in(f, 1, 0) == 2 * x**9 + x**7 + 2 * x**4
    assert R.dmp_diff_in(f, 2, 0) == x**6 + 2 * x**3
    assert R.dmp_diff_in(f, 3, 0) == 0

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0

    f = x * y**2 + 2 * x * y + 3 * x + 2 * y**2 + 3 * y + 1

    assert R.dmp_diff_in(f, 1, 0) == y**2 + 2 * y + 3
    assert R.dmp_diff_in(f, 2, 0) == 0
    assert R.dmp_diff_in(f, 1, 1) == 2 * x * y + 2 * x + 4 * y + 3

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(y + 2, 1, 0) == 0
    assert R.dmp_diff_in(x, 1, 0) == 1
    assert R.dmp_diff_in(3 * x**2 + x, 1, 0) == 6 * x + 1

    R, x, y, z, t = ring('x y z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)

    assert (R.dmp_diff_in(f, 2, 1) == -5076 * x * y**2 - 282 * x * y -
            54 * y * z**3 * t**2 + 54 * y * t**2 - 2 * z**3 * t**2 + 2 * t**2)
    assert R.dmp_diff_in(
        f, 3, 1) == -10152 * x * y - 282 * x - 54 * z**3 * t**2 + 54 * t**2
    assert (R.dmp_diff_in(f, 2, 2) == 270 * x**3 * z * t**2 + 846 * x * y * z -
            54 * y**3 * z * t**2 - 6 * y**2 * z * t**2 + 90 * z**4 * t**2 +
            24 * z**2 * t**3 - 18 * z * t**2)
    assert (R.dmp_diff_in(
        f, 3, 2) == 270 * x**3 * t**2 + 846 * x * y - 54 * y**3 * t**2 -
            6 * y**2 * t**2 + 360 * z**3 * t**2 + 48 * z * t**3 - 18 * t**2)

    pytest.raises(IndexError, lambda: R.dmp_diff_in(f, 2, -1))

    R, x, y, z, t = ring('x y z t', FF(23))

    f = R.from_dense(f_polys()[6].to_dense())

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1,
                                                   0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(
        R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0), 1, 0)
Пример #20
0
def test_dmp_ground_primitive():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(1) == (1, 1)
    assert R.dmp_ground_primitive(x + 1) == (1, x + 1)
    assert R.dmp_ground_primitive(2 * x + 2) == (2, x + 1)
    assert R.dmp_ground_primitive(x**2 + 2 * x + 1) == (1, x**2 + 2 * x + 1)
    assert R.dmp_ground_primitive(2 * x**2 + 4 * x + 2) == (2,
                                                            x**2 + 2 * x + 1)
    assert R.dmp_ground_primitive(6 * x**2 + 8 * x + 12) == (2, 3 * x**2 +
                                                             4 * x + 6)

    R, x = ring('x', QQ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(1) == (1, 1)
    assert R.dmp_ground_primitive(x + 1) == (1, x + 1)
    assert R.dmp_ground_primitive(2 * x + 2) == (2, x + 1)
    assert R.dmp_ground_primitive(x**2 + 2 * x + 1) == (1, x**2 + 2 * x + 1)
    assert R.dmp_ground_primitive(2 * x**2 + 4 * x + 2) == (2,
                                                            x**2 + 2 * x + 1)
    assert R.dmp_ground_primitive(6 * x**2 + 8 * x + 12) == (2, 3 * x**2 +
                                                             4 * x + 6)

    assert R.dmp_ground_primitive(2 * x / 3 + QQ(4, 9)) == (QQ(2,
                                                               9), 3 * x + 2)
    assert R.dmp_ground_primitive(2 * x / 3 + QQ(4, 5)) == (QQ(2,
                                                               15), 5 * x + 6)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(2) == (2, 1)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(2 * f) == (2, f)

    f = f_polys()[1]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(3 * f) == (3, f)

    f = f_polys()[2]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(4 * f) == (4, f)

    f = f_polys()[3]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(5 * f) == (5, f)

    f = f_polys()[4]

    assert R.dmp_ground_primitive(f) == (-1, -f)
    assert R.dmp_ground_primitive(6 * f) == (-6, -f)

    f = f_polys()[5]

    assert R.dmp_ground_primitive(f) == (-1, -f)
    assert R.dmp_ground_primitive(7 * f) == (-7, -f)

    R, x, y, z, t = ring('x y z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(8 * f) == (8, f)

    R, x, y = ring('x y', QQ)

    assert R.dmp_ground_primitive(2) == (2, 1)

    assert R.dmp_ground_primitive(2 * x / 3 + QQ(4, 9)) == (QQ(2,
                                                               9), 3 * x + 2)
    assert R.dmp_ground_primitive(2 * x / 3 + QQ(4, 5)) == (QQ(2,
                                                               15), 5 * x + 6)
Пример #21
0
def test_dmp_ground_content():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(+1) == 1
    assert R.dmp_ground_content(-1) == -1
    assert R.dmp_ground_content(x + 1) == 1
    assert R.dmp_ground_content(2 * x + 2) == 2
    assert R.dmp_ground_content(x**2 + 2 * x + 1) == 1
    assert R.dmp_ground_content(2 * x**2 + 4 * x + 2) == 2
    assert R.dmp_ground_content(6 * x**2 + 8 * x + 12) == 2

    R, x = ring('x', QQ)

    assert R.dmp_ground_content(6 * x**2 + 8 * x + 12) == 2

    assert R.dmp_ground_content(2 * x / 3 + QQ(4, 9)) == QQ(2, 9)
    assert R.dmp_ground_content(2 * x / 3 + QQ(4, 5)) == QQ(2, 15)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(+1) == 1
    assert R.dmp_ground_content(-1) == -1
    assert R.dmp_ground_content(x + 1) == 1
    assert R.dmp_ground_content(2 * x + 2) == 2
    assert R.dmp_ground_content(x**2 + 2 * x + 1) == 1
    assert R.dmp_ground_content(2 * x**2 + 4 * x + 2) == 2

    R, x, y = ring('x y', QQ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(2 * x / 3 + QQ(4, 9)) == QQ(2, 9)
    assert R.dmp_ground_content(2 * x / 3 + QQ(4, 5)) == QQ(2, 15)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(2 * f) == 2

    f = f_polys()[1]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(3 * f) == 3

    f = f_polys()[2]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(4 * f) == 4

    f = f_polys()[3]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(5 * f) == 5

    f = f_polys()[4]

    assert R.dmp_ground_content(f) == -1
    assert R.dmp_ground_content(6 * f) == -6

    f = f_polys()[5]

    assert R.dmp_ground_content(f) == -1
    assert R.dmp_ground_content(7 * f) == -7

    R, x, y, z, t = ring('x y, z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(8 * f) == 8
Пример #22
0
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
Пример #23
0
def test_dmp_ground_content():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(+1) == 1
    assert R.dmp_ground_content(-1) == 1
    assert R.dmp_ground_content(x + 1) == 1
    assert R.dmp_ground_content(2*x + 2) == 2
    assert R.dmp_ground_content(x**2 + 2*x + 1) == 1
    assert R.dmp_ground_content(2*x**2 + 4*x + 2) == 2
    assert R.dmp_ground_content(6*x**2 + 8*x + 12) == 2

    R, x = ring('x', QQ)

    assert R.dmp_ground_content(6*x**2 + 8*x + 12) == 2

    assert R.dmp_ground_content(2*x/3 + QQ(4, 9)) == QQ(2, 9)
    assert R.dmp_ground_content(2*x/3 + QQ(4, 5)) == QQ(2, 15)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(+1) == 1
    assert R.dmp_ground_content(-1) == 1
    assert R.dmp_ground_content(x + 1) == 1
    assert R.dmp_ground_content(2*x + 2) == 2
    assert R.dmp_ground_content(x**2 + 2*x + 1) == 1
    assert R.dmp_ground_content(2*x**2 + 4*x + 2) == 2

    R, x, y = ring('x y', QQ)

    assert R.dmp_ground_content(0) == 0
    assert R.dmp_ground_content(2*x/3 + QQ(4, 9)) == QQ(2, 9)
    assert R.dmp_ground_content(2*x/3 + QQ(4, 5)) == QQ(2, 15)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(2*f) == 2

    f = f_polys()[1]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(3*f) == 3

    f = f_polys()[2]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(4*f) == 4

    f = f_polys()[3]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(5*f) == 5

    f = f_polys()[4]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(6*f) == 6

    f = f_polys()[5]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(7*f) == 7

    R, x, y, z, t = ring('x y, z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_ground_content(f) == 1
    assert R.dmp_ground_content(8*f) == 8
Пример #24
0
def test_PolyElement_integrate():
    R, x = ring('x', QQ)

    assert R(0).integrate() == 0
    assert R(0).integrate(m=2) == 0

    assert R(1).integrate() == x
    assert R(1).integrate(m=2) == x**2 / 2

    assert (x**2 + 2 * x + 3).integrate(m=0) == x**2 + 2 * x + 3
    assert (x**2 + 2 * x + 3).integrate() == x**3 / 3 + x**2 + 3 * x
    assert (x**2 + 2 * x +
            3).integrate(m=2) == x**4 / 12 + x**3 / 3 + 3 * x**2 / 2
    assert (x**2 + 2 * x +
            3).integrate(m=3) == x**5 / 60 + x**4 / 12 + x**3 / 2

    assert (x**2 + 2 * x).integrate() == x**3 / 3 + x**2
    assert (x**2 + 2 * x).integrate(m=2) == x**4 / 12 + x**3 / 3

    assert (17 * x**29).integrate(m=3) == 17 * x**32 / 29760

    assert (17 * x**29 +
            x**5 / 2).integrate(m=3) == 17 * x**32 / 29760 + x**8 / 672

    R, x, y, z = ring('x y z', QQ)

    assert R(0).integrate() == 0
    assert R(0).integrate(m=2) == 0

    assert R(1).integrate() == x
    assert R(1).integrate(m=2) == x**2 / 2

    R, x, y = ring('x y', QQ)

    assert (x**2 + 2 * x + 3).integrate(m=0) == x**2 + 2 * x + 3
    assert (x**2 + 2 * x + 3).integrate(m=1) == x**3 / 3 + x**2 + 3 * x
    assert (x**2 + 2 * x +
            3).integrate(m=2) == x**4 / 12 + x**3 / 3 + 3 * x**2 / 2
    assert (x**2 + 2 * x +
            3).integrate(m=3) == x**5 / 60 + x**4 / 12 + x**3 / 2

    assert (x + 2 * y).integrate() == x**2 / 2 + 2 * x * y
    assert (x + 2 * y).integrate(x=y) == x * y + y**2
    assert (x + 2 * y).integrate(m=2) == x**3 / 6 + x**2 * y

    R, x, y, z, t = ring('x y z t', QQ)

    f = R.from_dense(f_polys()[6].to_dense())

    assert (f.integrate(
        x=y,
        m=2) == 705 * x**4 * y**3 / 2 + 45 * x**3 * y**2 * z**3 * t**2 / 2 -
            45 * x**3 * y**2 * t**2 / 2 - 141 * x * y**6 / 10 -
            47 * x * y**5 / 20 + 47 * x * y**3 * z**3 / 2 +
            47 * x * y**3 * z * t / 3 - 9 * y**5 * z**3 * t**2 / 20 +
            9 * y**5 * t**2 / 20 - y**4 * z**3 * t**2 / 12 + y**4 * t**2 / 12 +
            3 * y**2 * z**6 * t**2 / 2 + y**2 * z**4 * t**3 -
            3 * y**2 * z**3 * t**2 / 2 - y**2 * z * t**3)
    assert (f.integrate(
        x=y,
        m=3) == 705 * x**4 * y**4 / 8 + 15 * x**3 * y**3 * z**3 * t**2 / 2 -
            15 * x**3 * y**3 * t**2 / 2 - 141 * x * y**7 / 70 -
            47 * x * y**6 / 120 + 47 * x * y**4 * z**3 / 8 +
            47 * x * y**4 * z * t / 12 - 3 * y**6 * z**3 * t**2 / 40 +
            3 * y**6 * t**2 / 40 - y**5 * z**3 * t**2 / 60 + y**5 * t**2 / 60 +
            y**3 * z**6 * t**2 / 2 + y**3 * z**4 * t**3 / 3 -
            y**3 * z**3 * t**2 / 2 - y**3 * z * t**3 / 3)
    assert (f.integrate(
        x=z, m=2) == 2115 * x**4 * y * z**2 / 2 + 9 * x**3 * z**5 * t**2 / 4 -
            45 * x**3 * z**2 * t**2 / 2 - 423 * x * y**4 * z**2 / 2 -
            47 * x * y**3 * z**2 / 2 + 141 * x * y * z**5 / 20 +
            47 * x * y * z**3 * t / 3 - 9 * y**3 * z**5 * t**2 / 20 +
            9 * y**3 * z**2 * t**2 / 2 - y**2 * z**5 * t**2 / 20 +
            y**2 * z**2 * t**2 / 2 + 3 * z**8 * t**2 / 56 + z**6 * t**3 / 15 -
            3 * z**5 * t**2 / 20 - z**3 * t**3 / 3)
    assert (f.integrate(
        x=z, m=3) == 705 * x**4 * y * z**3 / 2 + 3 * x**3 * z**6 * t**2 / 8 -
            15 * x**3 * z**3 * t**2 / 2 - 141 * x * y**4 * z**3 / 2 -
            47 * x * y**3 * z**3 / 6 + 47 * x * y * z**6 / 40 +
            47 * x * y * z**4 * t / 12 - 3 * y**3 * z**6 * t**2 / 40 +
            3 * y**3 * z**3 * t**2 / 2 - y**2 * z**6 * t**2 / 120 +
            y**2 * z**3 * t**2 / 6 + z**9 * t**2 / 168 + z**7 * t**3 / 105 -
            z**6 * t**2 / 40 - z**4 * t**3 / 12)
Пример #25
0
def test_dmp_integrate_in():
    R, x = ring('x', QQ)

    assert R.dmp_integrate_in(0, 1, 0) == 0
    assert R.dmp_integrate_in(0, 2, 0) == 0

    assert R.dmp_integrate_in(1, 1, 0) == x
    assert R.dmp_integrate_in(1, 2, 0) == x**2/2

    assert R.dmp_integrate_in(x**2 + 2*x + 3, 0, 0) == x**2 + 2*x + 3
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 1, 0) == x**3/3 + x**2 + 3*x
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 2, 0) == x**4/12 + x**3/3 + 3*x**2/2
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 3, 0) == x**5/60 + x**4/12 + x**3/2

    assert R.dmp_integrate_in(x**2 + 2*x, 1, 0) == x**3/3 + x**2
    assert R.dmp_integrate_in(x**2 + 2*x, 2, 0) == x**4/12 + x**3/3

    assert R.dmp_integrate_in(17*x**29, 3, 0) == 17*x**32/29760

    assert R.dmp_integrate_in(17*x**29 + x**5/2, 3, 0) == 17*x**32/29760 + x**8/672

    R, x, y, z = ring('x y z', QQ)

    assert R.dmp_integrate_in(0, 1, 0) == 0
    assert R.dmp_integrate_in(0, 2, 0) == 0

    assert R.dmp_integrate_in(1, 1, 0) == x
    assert R.dmp_integrate_in(1, 2, 0) == x**2/2

    R, x, y = ring('x y', QQ)

    assert R.dmp_integrate_in(x**2 + 2*x + 3, 0, 0) == x**2 + 2*x + 3
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 1, 0) == x**3/3 + x**2 + 3*x
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 2, 0) == x**4/12 + x**3/3 + 3*x**2/2
    assert R.dmp_integrate_in(x**2 + 2*x + 3, 3, 0) == x**5/60 + x**4/12 + x**3/2

    assert R.dmp_integrate_in(x + 2*y, 1, 0) == x**2/2 + 2*x*y
    assert R.dmp_integrate_in(x + 2*y, 2, 0) == x**3/6 + x**2*y

    R, x, y, z, t = ring('x y, z, t', QQ)

    f = R.from_dense(f_polys()[6].to_dense())

    assert (R.dmp_integrate_in(f, 2, 1) ==
            705*x**4*y**3/2 + 45*x**3*y**2*z**3*t**2/2 - 45*x**3*y**2*t**2/2 -
            141*x*y**6/10 - 47*x*y**5/20 + 47*x*y**3*z**3/2 + 47*x*y**3*z*t/3 -
            9*y**5*z**3*t**2/20 + 9*y**5*t**2/20 - y**4*z**3*t**2/12 + y**4*t**2/12 +
            3*y**2*z**6*t**2/2 + y**2*z**4*t**3 - 3*y**2*z**3*t**2/2 - y**2*z*t**3)
    assert (R.dmp_integrate_in(f, 3, 1) ==
            705*x**4*y**4/8 + 15*x**3*y**3*z**3*t**2/2 - 15*x**3*y**3*t**2/2 -
            141*x*y**7/70 - 47*x*y**6/120 + 47*x*y**4*z**3/8 + 47*x*y**4*z*t/12 -
            3*y**6*z**3*t**2/40 + 3*y**6*t**2/40 - y**5*z**3*t**2/60 + y**5*t**2/60 +
            y**3*z**6*t**2/2 + y**3*z**4*t**3/3 - y**3*z**3*t**2/2 - y**3*z*t**3/3)
    assert (R.dmp_integrate_in(f, 2, 2) ==
            2115*x**4*y*z**2/2 + 9*x**3*z**5*t**2/4 - 45*x**3*z**2*t**2/2 -
            423*x*y**4*z**2/2 - 47*x*y**3*z**2/2 + 141*x*y*z**5/20 + 47*x*y*z**3*t/3 -
            9*y**3*z**5*t**2/20 + 9*y**3*z**2*t**2/2 - y**2*z**5*t**2/20 +
            y**2*z**2*t**2/2 + 3*z**8*t**2/56 + z**6*t**3/15 - 3*z**5*t**2/20 -
            z**3*t**3/3)
    assert (R.dmp_integrate_in(f, 3, 2) ==
            705*x**4*y*z**3/2 + 3*x**3*z**6*t**2/8 - 15*x**3*z**3*t**2/2 -
            141*x*y**4*z**3/2 - 47*x*y**3*z**3/6 + 47*x*y*z**6/40 + 47*x*y*z**4*t/12 -
            3*y**3*z**6*t**2/40 + 3*y**3*z**3*t**2/2 - y**2*z**6*t**2/120 +
            y**2*z**3*t**2/6 + z**9*t**2/168 + z**7*t**3/105 - z**6*t**2/40 -
            z**4*t**3/12)

    pytest.raises(IndexError, lambda: R.dmp_integrate_in(f, 2, -1))
Пример #26
0
def test_dmp_ground_primitive():
    R, x = ring('x', ZZ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(1) == (1, 1)
    assert R.dmp_ground_primitive(x + 1) == (1, x + 1)
    assert R.dmp_ground_primitive(2*x + 2) == (2, x + 1)
    assert R.dmp_ground_primitive(x**2 + 2*x + 1) == (1, x**2 + 2*x + 1)
    assert R.dmp_ground_primitive(2*x**2 + 4*x + 2) == (2, x**2 + 2*x + 1)
    assert R.dmp_ground_primitive(6*x**2 + 8*x + 12) == (2, 3*x**2 + 4*x + 6)

    R, x = ring('x', QQ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(1) == (1, 1)
    assert R.dmp_ground_primitive(x + 1) == (1, x + 1)
    assert R.dmp_ground_primitive(2*x + 2) == (2, x + 1)
    assert R.dmp_ground_primitive(x**2 + 2*x + 1) == (1, x**2 + 2*x + 1)
    assert R.dmp_ground_primitive(2*x**2 + 4*x + 2) == (2, x**2 + 2*x + 1)
    assert R.dmp_ground_primitive(6*x**2 + 8*x + 12) == (2, 3*x**2 + 4*x + 6)

    assert R.dmp_ground_primitive(2*x/3 + QQ(4, 9)) == (QQ(2, 9), 3*x + 2)
    assert R.dmp_ground_primitive(2*x/3 + QQ(4, 5)) == (QQ(2, 15), 5*x + 6)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_ground_primitive(0) == (0, 0)
    assert R.dmp_ground_primitive(2) == (2, 1)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(2*f) == (2, f)

    f = f_polys()[1]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(3*f) == (3, f)

    f = f_polys()[2]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(4*f) == (4, f)

    f = f_polys()[3]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(5*f) == (5, f)

    f = f_polys()[4]

    assert R.dmp_ground_primitive(f) == (-1, -f)
    assert R.dmp_ground_primitive(6*f) == (-6, -f)

    f = f_polys()[5]

    assert R.dmp_ground_primitive(f) == (-1, -f)
    assert R.dmp_ground_primitive(7*f) == (-7, -f)

    R, x, y, z, t = ring('x y z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_ground_primitive(f) == (1, f)
    assert R.dmp_ground_primitive(8*f) == (8, f)

    R, x, y = ring('x y', QQ)

    assert R.dmp_ground_primitive(2) == (2, 1)

    assert R.dmp_ground_primitive(2*x/3 + QQ(4, 9)) == (QQ(2, 9), 3*x + 2)
    assert R.dmp_ground_primitive(2*x/3 + QQ(4, 5)) == (QQ(2, 15), 5*x + 6)
Пример #27
0
def test_dmp_diff_in():
    R, x = ring('x', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(7, 1, 0) == 0
    assert R.dmp_diff_in(2*x + 7, 1, 0) == 2
    assert R.dmp_diff_in(x**2 + 2*x + 1, 1, 0) == 2*x + 2
    assert R.dmp_diff_in(x**3 + 2*x**2 + 3*x + 4, 1, 0) == 3*x**2 + 4*x + 3
    assert R.dmp_diff_in(x**4 - x**3 + 2, 1, 0) == 4*x**3 - 3*x**2
    assert R.dmp_diff_in(x**3 + 2*x**2 + 3*x + 4, 2, 0) == 6*x + 4

    f = 17*x**10 + 34*x**9 + 56*x**8 - 345*x**7 + 23*x**6 + 76*x**5 + 12*x**2 + 3*x + 7

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(R.dmp_diff_in(R.dmp_diff_in(f, 1, 0),
                                                                 1, 0), 1, 0)

    R, x = ring('x', FF(3))

    f = 2*x**10 + x**9 + 2*x**8 + 2*x**6 + x**5 + 1

    assert R.dmp_diff_in(f, 1, 0) == 2*x**9 + x**7 + 2*x**4
    assert R.dmp_diff_in(f, 2, 0) == x**6 + 2*x**3
    assert R.dmp_diff_in(f, 3, 0) == 0

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(R.dmp_diff_in(R.dmp_diff_in(f, 1, 0),
                                                                 1, 0), 1, 0)

    R, x, y = ring('x y', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0

    f = x*y**2 + 2*x*y + 3*x + 2*y**2 + 3*y + 1

    assert R.dmp_diff_in(f, 1, 0) == y**2 + 2*y + 3
    assert R.dmp_diff_in(f, 2, 0) == 0
    assert R.dmp_diff_in(f, 1, 1) == 2*x*y + 2*x + 4*y + 3

    R, x, y, z = ring('x y z', ZZ)

    assert R.dmp_diff_in(0, 1, 0) == 0
    assert R.dmp_diff_in(y + 2, 1, 0) == 0
    assert R.dmp_diff_in(x, 1, 0) == 1
    assert R.dmp_diff_in(3*x**2 + x, 1, 0) == 6*x + 1

    R, x, y, z, t = ring('x y z t', ZZ)

    f = f_polys()[6]

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(R.dmp_diff_in(R.dmp_diff_in(f, 1, 0),
                                                                 1, 0), 1, 0)

    assert (R.dmp_diff_in(f, 2, 1) ==
            -5076*x*y**2 - 282*x*y - 54*y*z**3*t**2 + 54*y*t**2 - 2*z**3*t**2 + 2*t**2)
    assert R.dmp_diff_in(f, 3, 1) == -10152*x*y - 282*x - 54*z**3*t**2 + 54*t**2
    assert (R.dmp_diff_in(f, 2, 2) ==
            270*x**3*z*t**2 + 846*x*y*z - 54*y**3*z*t**2 - 6*y**2*z*t**2 +
            90*z**4*t**2 + 24*z**2*t**3 - 18*z*t**2)
    assert (R.dmp_diff_in(f, 3, 2) ==
            270*x**3*t**2 + 846*x*y - 54*y**3*t**2 - 6*y**2*t**2 +
            360*z**3*t**2 + 48*z*t**3 - 18*t**2)

    pytest.raises(IndexError, lambda: R.dmp_diff_in(f, 2, -1))

    R, x, y, z, t = ring('x y z t', FF(23))

    f = R.from_dense(f_polys()[6].to_dense())

    assert R.dmp_diff_in(f, 0, 0) == f
    assert R.dmp_diff_in(f, 2, 0) == R.dmp_diff_in(R.dmp_diff_in(f, 1, 0), 1, 0)
    assert R.dmp_diff_in(f, 3, 0) == R.dmp_diff_in(R.dmp_diff_in(R.dmp_diff_in(f, 1, 0),
                                                                 1, 0), 1, 0)
Пример #28
0
"""Tools for polynomial factorization routines in characteristic zero."""

import functools
import operator

import pytest

from diofant import (EX, FF, QQ, RR, ZZ, DomainError, ExtraneousFactors, I,
                     nextprime, pi, ring, sin, sqrt)
from diofant.polys.polyconfig import using
from diofant.polys.specialpolys import f_polys, w_polys

__all__ = ()

f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
w_1, w_2 = w_polys()


def test__trial_division():
    R, x = ring('x', ZZ)

    assert R._trial_division(
        x**5 + 8 * x**4 + 25 * x**3 + 38 * x**2 + 28 * x + 8,
        (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]

    R, x, y = ring('x y', ZZ)

    assert R._trial_division(
        x**5 + 8 * x**4 + 25 * x**3 + 38 * x**2 + 28 * x + 8,
        (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]
Пример #29
0
"""Tests for OO layer of several polynomial representations. """

import pytest

from diofant.domains import QQ, ZZ
from diofant.polys.polyclasses import DMP
from diofant.polys.polyerrors import ExactQuotientFailed, PolynomialError
from diofant.polys.specialpolys import f_polys


__all__ = ()

f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [f.to_dense() for f in f_polys()]


def test_DMP___init__():
    f = DMP([[0], [], [0, 1, 2], [3]], ZZ)

    assert f.rep == [[1, 2], [3]]
    assert f.domain == ZZ
    assert f.lev == 1

    f = DMP([[1, 2], [3]], ZZ, 1)

    assert f.rep == [[1, 2], [3]]
    assert f.domain == ZZ
    assert f.lev == 1

    f = DMP({(1, 1): 1, (0, 0): 2}, ZZ, 1)

    assert f.rep == [[1, 0], [2]]