def test_var(): var("a") assert a == Symbol("a") var("b bb cc zz _x") assert b == Symbol("b") assert bb == Symbol("bb") assert cc == Symbol("cc") assert zz == Symbol("zz") assert _x == Symbol("_x") v = var(['d', 'e', 'fg']) assert d == Symbol('d') assert e == Symbol('e') assert fg == Symbol('fg') # check return value assert v == (d, e, fg) # see if var() really injects into global namespace raises(NameError, "z1") make_z1() assert z1 == Symbol("z1") raises(NameError, "z2") make_z2() assert z2 == Symbol("z2")
def test__dict_from_basic_no_gens(): raises(GeneratorsNeeded, "_dict_from_basic_no_gens(Integer(17))") assert _dict_from_basic_no_gens(x) == ({(1, ): Integer(1)}, (x, )) assert _dict_from_basic_no_gens(y) == ({(1, ): Integer(1)}, (y, )) assert _dict_from_basic_no_gens(x * y) == ({(1, 1): Integer(1)}, (x, y)) assert _dict_from_basic_no_gens(x + y) == ({ (1, 0): Integer(1), (0, 1): Integer(1) }, (x, y)) assert _dict_from_basic_no_gens(sqrt(2)) == ({ (1, ): Integer(1) }, (sqrt(2), )) raises(GeneratorsNeeded, "_dict_from_basic_no_gens(sqrt(2), greedy=False)") assert _dict_from_basic_no_gens(x * y, domain=ZZ[x]) == ({(1, ): x}, (y, )) assert _dict_from_basic_no_gens(x * y, domain=ZZ[y]) == ({(1, ): y}, (x, )) assert _dict_from_basic_no_gens(3 * sqrt(2) * pi * x * y, extension=False) == ({ (1, 1, 1, 1): 3 }, (x, y, sqrt(2), pi)) assert _dict_from_basic_no_gens(3 * sqrt(2) * pi * x * y, extension=True) == ({ (1, 1, 1): 3 * sqrt(2) }, (x, y, pi))
def test_lambda(): x = Symbol('x') assert sympify('lambda : 1') == Lambda(x, 1) assert sympify('lambda x: 2*x') == Lambda(x, 2*x) assert sympify('lambda x, y: 2*x+y') == Lambda([x, y], 2*x+y) raises(SympifyError, "_sympify('lambda : 1')")
def test__dict_from_basic_if_gens(): assert _dict_from_basic_if_gens(Integer(17), (x,)) == {(0,): Integer(17)} assert _dict_from_basic_if_gens(Integer(17), (x,y)) == {(0,0): Integer(17)} assert _dict_from_basic_if_gens(Integer(17), (x,y,z)) == {(0,0,0): Integer(17)} assert _dict_from_basic_if_gens(Integer(-17), (x,)) == {(0,): Integer(-17)} assert _dict_from_basic_if_gens(Integer(-17), (x,y)) == {(0,0): Integer(-17)} assert _dict_from_basic_if_gens(Integer(-17), (x,y,z)) == {(0,0,0): Integer(-17)} assert _dict_from_basic_if_gens(Integer(17)*x, (x,)) == {(1,): Integer(17)} assert _dict_from_basic_if_gens(Integer(17)*x, (x,y)) == {(1,0): Integer(17)} assert _dict_from_basic_if_gens(Integer(17)*x, (x,y,z)) == {(1,0,0): Integer(17)} assert _dict_from_basic_if_gens(Integer(17)*x**7, (x,)) == {(7,): Integer(17)} assert _dict_from_basic_if_gens(Integer(17)*x**7*y, (x,y)) == {(7,1): Integer(17)} assert _dict_from_basic_if_gens(Integer(17)*x**7*y*z**12, (x,y,z)) == {(7,1,12): Integer(17)} assert _dict_from_basic_if_gens(x+2*y+3*z, (x,)) == \ {(1,): Integer(1), (0,): 2*y+3*z} assert _dict_from_basic_if_gens(x+2*y+3*z, (x,y)) == \ {(1,0): Integer(1), (0,1): Integer(2), (0,0): 3*z} assert _dict_from_basic_if_gens(x+2*y+3*z, (x,y,z)) == \ {(1,0,0): Integer(1), (0,1,0): Integer(2), (0,0,1): Integer(3)} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,)) == \ {(1,): y+2*z, (0,): 3*y*z} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,y)) == \ {(1,1): Integer(1), (1,0): 2*z, (0,1): 3*z} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,y,z)) == \ {(1,1,0): Integer(1), (1,0,1): Integer(2), (0,1,1): Integer(3)} assert _dict_from_basic_if_gens(2**y*x, (x,)) == {(1,): 2**y} raises(PolynomialError, "_dict_from_basic_if_gens(2**y*x, (x,y))")
def test_var(): var("a") assert a == Symbol("a") var("b bb cc zz _x") assert b == Symbol("b") assert bb == Symbol("bb") assert cc == Symbol("cc") assert zz == Symbol("zz") assert _x == Symbol("_x") v = var(['d','e','fg']) assert d == Symbol('d') assert e == Symbol('e') assert fg == Symbol('fg') # check return value assert v == (d, e, fg) # see if var() really injects into global namespace raises(NameError, "z1") make_z1() assert z1 == Symbol("z1") raises(NameError, "z2") make_z2() assert z2 == Symbol("z2")
def test_dmp_ff_div(): raises(ZeroDivisionError, "dmp_ff_div([[1,2],[3]], [[]], 1, QQ)") f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ) g = dmp_normal([[1], [-1, 0]], 1, QQ) q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r) f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ) g = dmp_normal([[-1], [1, 0]], 1, QQ) q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r) f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ) g = dmp_normal([[2], [-2, 0]], 1, QQ) q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r)
def test_solve_polynomial1(): x, y = symbols('xy') assert solve(3 * x - 2, x) == [Rational(2, 3)] assert solve(Eq(3 * x, 2), x) == [Rational(2, 3)] assert solve(x**2 - 1, x) in [[-1, 1], [1, -1]] assert solve(Eq(x**2, 1), x) in [[-1, 1], [1, -1]] assert solve(x - y**3, x) == [y**3] assert sorted(solve(x - y**3, y)) == sorted([ (-x**Rational(1, 3)) / 2 + I * sqrt(3) * x**Rational(1, 3) / 2, x**Rational(1, 3), (-x**Rational(1, 3)) / 2 - I * sqrt(3) * x**Rational(1, 3) / 2, ]) a11, a12, a21, a22, b1, b2 = symbols('a11', 'a12', 'a21', 'a22', 'b1', 'b2') assert solve([a11*x + a12*y - b1, a21*x + a22*y - b2], x, y) == \ { y : (a11*b2 - a21*b1)/(a11*a22 - a12*a21), x : (a22*b1 - a12*b2)/(a11*a22 - a12*a21) } solution = {y: S.Zero, x: S.Zero} assert solve((x - y, x + y), x, y) == solution assert solve((x - y, x + y), (x, y)) == solution assert solve((x - y, x + y), [x, y]) == solution assert solve(x**3 - 15 * x - 4, x) == [-2 + 3**Rational(1, 2), 4, -2 - 3**Rational(1, 2)] raises(TypeError, "solve(x**2-pi, pi)") raises(ValueError, "solve(x**2-pi)")
def test_sympify3(): assert sympify("x**3") == x**3 assert sympify("x^3") == x**3 assert sympify("1/2") == Integer(1)/2 raises(SympifyError, "_sympify('x**3')") raises(SympifyError, "_sympify('1/2')")
def test_dmp_ff_div(): raises(ZeroDivisionError, "dmp_ff_div([[1,2],[3]], [[]], 1, QQ)") f = dmp_normal([[1], [], [1,0,0]], 1, QQ) g = dmp_normal([[1], [-1,0]], 1, QQ) q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r) f = dmp_normal([[1], [], [1,0,0]], 1, QQ) g = dmp_normal([[-1], [1,0]], 1, QQ) q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r) f = dmp_normal([[1], [], [1,0,0]], 1, QQ) g = dmp_normal([[2], [-2,0]], 1, QQ) q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]] r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]] assert dmp_ff_div(f, g, 1, QQ) == (q, r)
def test_solve_polynomial1(): x, y = symbols('xy') assert solve(3*x-2, x) == [Rational(2,3)] assert solve(Eq(3*x, 2), x) == [Rational(2,3)] assert solve(x**2-1, x) in [[-1, 1], [1, -1]] assert solve(Eq(x**2, 1), x) in [[-1, 1], [1, -1]] assert solve( x - y**3, x) == [y**3] assert sorted(solve( x - y**3, y)) == sorted([ (-x**Rational(1,3))/2 + I*sqrt(3)*x**Rational(1,3)/2, x**Rational(1,3), (-x**Rational(1,3))/2 - I*sqrt(3)*x**Rational(1,3)/2, ]) a11,a12,a21,a22,b1,b2 = symbols('a11','a12','a21','a22','b1','b2') assert solve([a11*x + a12*y - b1, a21*x + a22*y - b2], x, y) == \ { y : (a11*b2 - a21*b1)/(a11*a22 - a12*a21), x : (a22*b1 - a12*b2)/(a11*a22 - a12*a21) } solution = {y: S.Zero, x: S.Zero} assert solve((x-y, x+y), x, y ) == solution assert solve((x-y, x+y), (x, y)) == solution assert solve((x-y, x+y), [x, y]) == solution assert solve( x**3 - 15*x - 4, x) == [-2 + 3**Rational(1,2), 4, -2 - 3**Rational(1,2) ] raises(TypeError, "solve(x**2-pi, pi)") raises(ValueError, "solve(x**2-pi)")
def test_conj_dirac(): raises(ShapeError, "eye(3).D") M = Matrix([[1, I, I, I], [0, 1, I, I], [0, 0, 1, I], [0, 0, 0, 1]]) assert M.D == Matrix([[1, 0, 0, 0], [-I, 1, 0, 0], [-I, -I, -1, 0], [-I, -I, I, -1]])
def test_solve_poly_system(): assert solve_poly_system([x-1], x) == [(S.One,)] assert solve_poly_system([y - x, y - x - 1], x, y) == None assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)] assert solve_poly_system([2*x - 3, 3*y/2 - 2*x, z - 5*y], x, y, z) == \ [(Rational(3, 2), Integer(2), Integer(10))] assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \ [(0, 0), (2, -2**S.Half), (2, 2**S.Half)] assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \ [(I*S.Half**S.Half, -S.Half), (-I*S.Half**S.Half, -S.Half)] a, b = -1 - 2**S.Half, -1 + 2**S.Half assert solve_poly_system([x**2+y+z-1, x+y**2+z-1, x+y+z**2-1], x, y, z) == \ [(a, a, a), (0, 0, 1), (0, 1, 0), (b, b, b), (1, 0, 0)] solution = [(1, -1), (1, 1)] assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution assert solve_poly_system([x**2 - y**2, x - 1]) == solution raises(ValueError, "solve_poly_system([x**3-y**3], x, y)")
def test_gf_irreducible_p(): assert gf_irred_p_ben_or([7], 11, ZZ) == True assert gf_irred_p_ben_or([7,3], 11, ZZ) == True assert gf_irred_p_ben_or([7,3,1], 11, ZZ) == False assert gf_irred_p_rabin([7], 11, ZZ) == True assert gf_irred_p_rabin([7,3], 11, ZZ) == True assert gf_irred_p_rabin([7,3,1], 11, ZZ) == False assert gf_irreducible_p([7], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7,3], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7,3,1], 11, ZZ, method='ben-or') == False assert gf_irreducible_p([7], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7,3], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7,3,1], 11, ZZ, method='rabin') == False raises(KeyError, "gf_irreducible_p([7], 11, ZZ, method='other')") f = [1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10] g = [1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9] h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) == True assert gf_irred_p_ben_or(g, 17, ZZ) == True assert gf_irred_p_ben_or(h, 17, ZZ) == False assert gf_irred_p_rabin(f, 17, ZZ) == True assert gf_irred_p_rabin(g, 17, ZZ) == True assert gf_irred_p_rabin(h, 17, ZZ) == False
def test_poly_cancel(): assert Poly.cancel(x) == x assert Poly.cancel(x+1) == x+1 assert Poly.cancel((x+1)/(1-x)) == (x+1)/(1-x) assert Poly.cancel((x**2-1)/(x-1)) == x+1 assert Poly.cancel((x**2-y**2)/(x-y)) == x+y assert Poly.cancel((x**2-y)/(x-y)) == (x**2 - y)/(x - y) assert Poly.cancel((x**2-2)/(x+sqrt(2))) == x - sqrt(2) assert Poly.cancel((x**2-y**2)/(x-y), x) == x+y assert Poly.cancel((x, S.One), x) == x assert Poly.cancel((x+1, S.One), x) == x+1 assert Poly.cancel((x+1, x-1), x) == (x+1)/(x-1) assert Poly.cancel((x**2-1, x-1), x) == x+1 assert Poly.cancel((x**2-y**2, x-y), x, y) == x+y assert Poly.cancel((x**2-y, x-y), x, y) == (x**2 - y)/(x - y) assert Poly.cancel((x**2-2, x+sqrt(2)), x) == x - sqrt(2) assert Poly.cancel((x**2-y**2, x-y), x) == x+y assert Poly.cancel(((x**2-y**2).as_poly(x), (x-y).as_poly(x))) == x+y f = -1/(3 + 2*sqrt(2))*(1 + 1/(3 + 2*sqrt(2))*(7 + 5*sqrt(2))) assert Poly.cancel(f) == -2 + sqrt(2) raises(SymbolsError, "Poly.cancel((x**2-y**2, x-y))")
def test_poly_nth(): assert poly_nth([1, 2, 3], 0) == 3 assert poly_nth([1, 2, 3], 1) == 2 assert poly_nth([1, 2, 3], 2) == 1 raises(IndexError, 'poly_nth([3,4,5],-1)') raises(IndexError, 'poly_nth([3,4,5], 3)')
def test_solve_poly_system(): assert solve_poly_system([x - 1], x) == [(S.One, )] assert solve_poly_system([y - x, y - x - 1], x, y) == None assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)] assert solve_poly_system([2*x - 3, 3*y/2 - 2*x, z - 5*y], x, y, z) == \ [(Rational(3, 2), Integer(2), Integer(10))] assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \ [(0, 0), (2, -2**S.Half), (2, 2**S.Half)] assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \ [(I*S.Half**S.Half, -S.Half), (-I*S.Half**S.Half, -S.Half)] a, b = -1 - 2**S.Half, -1 + 2**S.Half assert solve_poly_system([x**2+y+z-1, x+y**2+z-1, x+y+z**2-1], x, y, z) == \ [(a, a, a), (0, 0, 1), (0, 1, 0), (b, b, b), (1, 0, 0)] solution = [(1, -1), (1, 1)] assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution assert solve_poly_system([x**2 - y**2, x - 1]) == solution raises(ValueError, "solve_poly_system([x**3-y**3], x, y)")
def test_union(): assert Union(Interval(1, 2), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 2), Interval(2, 3, True)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(2, 4)) == Interval(1, 4) assert Union(Interval(1, 2), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(1, 2)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(1, 2)) == \ Interval(1, 3, False, True) assert Union(Interval(1, 3), Interval(1, 2, False, True)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3, True)) == Interval(1, 3, True) assert Union(Interval(1, 2, True), Interval(1, 3, True, True)) == \ Interval(1, 3, True, True) assert Union(Interval(1, 2, True, True), Interval(1, 3, True)) == \ Interval(1, 3, True) assert Union(Interval(1, 3), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(2, 3)) == \ Interval(1, 3) assert Union(Interval(1, 2, False, True), Interval(2, 3, True)) != \ Interval(1, 3) assert Union(Interval(1, 2), S.EmptySet) == Interval(1, 2) assert Union(S.EmptySet) == S.EmptySet assert Interval(1, 2).union(Interval(2, 3)) == \ Interval(1, 2) + Interval(2, 3) assert Interval(1, 2).union(Interval(2, 3)) == Interval(1, 3) assert Union(Set()) == Set() raises(ValueError, "Union(1, 2, 3)")
def test_dmp_rr_div(): raises(ZeroDivisionError, "dmp_rr_div([[1,2],[3]], [[]], 1, ZZ)") f = dmp_normal([[1], [], [1,0,0]], 1, ZZ) g = dmp_normal([[1], [-1,0]], 1, ZZ) q = dmp_normal([[1], [1, 0]], 1, ZZ) r = dmp_normal([[2, 0, 0]], 1, ZZ) assert dmp_rr_div(f, g, 1, ZZ) == (q, r) f = dmp_normal([[1], [], [1,0,0]], 1, ZZ) g = dmp_normal([[-1], [1,0]], 1, ZZ) q = dmp_normal([[-1], [-1, 0]], 1, ZZ) r = dmp_normal([[2, 0, 0]], 1, ZZ) assert dmp_rr_div(f, g, 1, ZZ) == (q, r) f = dmp_normal([[1], [], [1,0,0]], 1, ZZ) g = dmp_normal([[2], [-2,0]], 1, ZZ) q, r = [[]], f assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
def test_dmp_rr_div(): raises(ZeroDivisionError, "dmp_rr_div([[1,2],[3]], [[]], 1, ZZ)") f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ) g = dmp_normal([[1], [-1, 0]], 1, ZZ) q = dmp_normal([[1], [1, 0]], 1, ZZ) r = dmp_normal([[2, 0, 0]], 1, ZZ) assert dmp_rr_div(f, g, 1, ZZ) == (q, r) f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ) g = dmp_normal([[-1], [1, 0]], 1, ZZ) q = dmp_normal([[-1], [-1, 0]], 1, ZZ) r = dmp_normal([[2, 0, 0]], 1, ZZ) assert dmp_rr_div(f, g, 1, ZZ) == (q, r) f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ) g = dmp_normal([[2], [-2, 0]], 1, ZZ) q, r = [[]], f assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
def test_poly_cancel(): assert Poly.cancel(x) == x assert Poly.cancel(x + 1) == x + 1 assert Poly.cancel((x + 1) / (1 - x)) == (x + 1) / (1 - x) assert Poly.cancel((x**2 - 1) / (x - 1)) == x + 1 assert Poly.cancel((x**2 - y**2) / (x - y)) == x + y assert Poly.cancel((x**2 - y) / (x - y)) == (x**2 - y) / (x - y) assert Poly.cancel((x**2 - 2) / (x + sqrt(2))) == x - sqrt(2) assert Poly.cancel((x**2 - y**2) / (x - y), x) == x + y assert Poly.cancel((x, S.One), x) == x assert Poly.cancel((x + 1, S.One), x) == x + 1 assert Poly.cancel((x + 1, x - 1), x) == (x + 1) / (x - 1) assert Poly.cancel((x**2 - 1, x - 1), x) == x + 1 assert Poly.cancel((x**2 - y**2, x - y), x, y) == x + y assert Poly.cancel((x**2 - y, x - y), x, y) == (x**2 - y) / (x - y) assert Poly.cancel((x**2 - 2, x + sqrt(2)), x) == x - sqrt(2) assert Poly.cancel((x**2 - y**2, x - y), x) == x + y assert Poly.cancel(((x**2 - y**2).as_poly(x), (x - y).as_poly(x))) == x + y f = -1 / (3 + 2 * sqrt(2)) * (1 + 1 / (3 + 2 * sqrt(2)) * (7 + 5 * sqrt(2))) assert Poly.cancel(f) == -2 + sqrt(2) raises(SymbolsError, "Poly.cancel((x**2-y**2, x-y))")
def test_interval_symbolic(): x = Symbol('x') e = Interval(0, 1) assert e.contains(x) == And(0<=x, x<=1) raises(TypeError, "x in e") e = Interval(0, 1, True, True) assert e.contains(x) == And(0<x, x<1)
def test_interval_symbolic(): x = Symbol('x') e = Interval(0, 1) assert e.contains(x) == And(0 <= x, x <= 1) raises(TypeError, "x in e") e = Interval(0, 1, True, True) assert e.contains(x) == And(0 < x, x < 1)
def test_poly_nth(): assert poly_nth([1,2,3], 0) == 3 assert poly_nth([1,2,3], 1) == 2 assert poly_nth([1,2,3], 2) == 1 raises(IndexError, 'poly_nth([3,4,5],-1)') raises(IndexError, 'poly_nth([3,4,5], 3)')
def test_tsolve_1(): a, b = symbols('ab') x, y, z = symbols('xyz') assert solve(exp(x)-3, x) == [log(3)] assert solve((a*x+b)*(exp(x)-3), x) == [-b/a, log(3)] assert solve(cos(x)-y, x) == [acos(y)] assert solve(2*cos(x)-y,x)== [acos(y/2)] raises(NotImplementedError, "solve(Eq(cos(x), sin(x)), x)") # XXX in the following test, log(2*y + 2*...) should -> log(2) + log(y +...) assert solve(exp(x)+exp(-x)-y,x) == [-log(4) + log(2*y + 2*(-4 + y**2)**Rational(1,2)), -log(4) + log(2*y - 2*(-4 + y**2)**Rational(1,2))] assert solve(exp(x)-3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x)-3, x) == [exp(3)] assert solve(sqrt(3*x)-4, x) == [Rational(16,3)] assert solve(3**(x+2), x) == [-oo] assert solve(3**(2-x), x) == [oo] assert solve(4*3**(5*x+2)-7, x) == [(-log(4) - 2*log(3) + log(7))/(5*log(3))] assert solve(x+2**x, x) == [-LambertW(log(2))/log(2)] assert solve(3*x+5+2**(-5*x+3), x) in \ [[-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))],\ [(-25*log(2) + 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2))]] assert solve(5*x-1+3*exp(2-7*x), x) == \ [Rational(1,5) + LambertW(-21*exp(Rational(3,5))/5)/7] assert solve(2*x+5+log(3*x-2), x) == \ [Rational(2,3) + LambertW(2*exp(-Rational(19,3))/3)/2] assert solve(3*x+log(4*x), x) == [LambertW(Rational(3,4))/3] assert solve((2*x+8)*(8+exp(x)), x) == [-4, log(8) + pi*I] assert solve(2*exp(3*x+4)-3, x) in [ [-Rational(4,3)+log(Rational(3,2))/3],\ [Rational(-4, 3) - log(2)/3 + log(3)/3]] assert solve(2*log(3*x+4)-3, x) == [(exp(Rational(3,2))-4)/3] assert solve(exp(x)+1, x) == [pi*I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3/log(3)*LambertW(-log(3)/3)] assert solve(2*(3*x+4)**5 - 6*7**(3*x+9), x) in \ [[Rational(-4,3) - 5/log(7)/3*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10)],\ [(-5*LambertW(-7*2**(Rational(4, 5))*6**(Rational(1, 5))*log(7)/10) - 4*log(7))/(3*log(7))], \ [-((4*log(7) + 5*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10))/(3*log(7)))]] assert solve(z*cos(x)-y, x) == [acos(y/z)] assert solve(z*cos(2*x)-y, x) == [acos(y/z)/2] assert solve(z*cos(sin(x))-y, x) == [asin(acos(y/z))] assert solve(z*cos(x), x) == [acos(0)] assert solve(exp(x)+exp(-x)-y, x)== [-log(4) + log(2*y + 2*(-4 + y**2)**(Rational(1, 2))), -log(4) + log(2*y - 2*(-4 + y**2)**(Rational(1, 2)))] # issue #1409 assert solve(y - b*x/(a+x), x) in [[-a*y/(y - b)], [a*y/(b - y)]] assert solve(y - b*exp(a/x), x) == [a/(-log(b) + log(y))] # issue #1408 assert solve(y-b/(1+a*x),x) == [(b - y)/(a*y)] # issue #1407 assert solve(y-a*x**b , x) == [y**(1/b)*(1/a)**(1/b)] # issue #1406 assert solve(z**x - y, x) == [log(y)/log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10)/log(2)]
def test_multiple_results_c(): x, y, z = symbols('xyz') expr1 = (x + y) * z expr2 = (x - y) * z routine = Routine("test", [InputArgument(symbol) for symbol in x, y, z], [Result(expr1), Result(expr2)]) code_gen = CCodeGen() raises(CodeGenError, 'get_string(code_gen.dump_h, [routine])')
def test_as_integer(): assert Poly(3*x**2 + x, x).as_integer() == \ (Integer(1), Poly(3*x**2 + x, x)) assert Poly(x**2 + x/2, x).as_integer() == \ (Integer(2), Poly(2*x**2 + x, x)) raises(CoefficientError, "Poly(x**2 + t*x, x).as_integer()")
def test_dmp_nth(): assert dmp_nth([[1],[2],[3]], 0, 1, ZZ) == [3] assert dmp_nth([[1],[2],[3]], 1, 1, ZZ) == [2] assert dmp_nth([[1],[2],[3]], 2, 1, ZZ) == [1] assert dmp_nth([[1],[2],[3]], 9, 1, ZZ) == [] raises(IndexError, 'dmp_nth([[3],[4],[5]], -1, 1, ZZ)')
def test_dmp_validate(): assert dmp_validate([]) == ([], 0) assert dmp_validate([0,0,0,1,0]) == ([1,0], 0) assert dmp_validate([[[]]]) == ([[[]]], 2) assert dmp_validate([[0],[],[0],[1],[0]]) == ([[1],[]], 1) raises(ValueError, 'dmp_validate([[0],0,[0],[1],[0]])')
def test_dup_nth(): assert dup_nth([1, 2, 3], 0, ZZ) == 3 assert dup_nth([1, 2, 3], 1, ZZ) == 2 assert dup_nth([1, 2, 3], 2, ZZ) == 1 assert dup_nth([1, 2, 3], 9, ZZ) == 0 raises(IndexError, 'dup_nth([3,4,5], -1, ZZ)')
def test_swinnerton_dyer_poly(): raises(ValueError, "swinnerton_dyer_poly(0, x)") assert swinnerton_dyer_poly(1, x, polys=True) == Poly(x**2 - 2) assert swinnerton_dyer_poly(1, x) == x**2 - 2 assert swinnerton_dyer_poly(2, x) == x**4 - 10*x**2 + 1 assert swinnerton_dyer_poly(3, x) == x**8 - 40*x**6 + 352*x**4 - 960*x**2 + 576
def test_dmp_nth(): assert dmp_nth([[1], [2], [3]], 0, 1, ZZ) == [3] assert dmp_nth([[1], [2], [3]], 1, 1, ZZ) == [2] assert dmp_nth([[1], [2], [3]], 2, 1, ZZ) == [1] assert dmp_nth([[1], [2], [3]], 9, 1, ZZ) == [] raises(IndexError, 'dmp_nth([[3],[4],[5]], -1, 1, ZZ)')
def test_dup_nth(): assert dup_nth([1,2,3], 0, ZZ) == 3 assert dup_nth([1,2,3], 1, ZZ) == 2 assert dup_nth([1,2,3], 2, ZZ) == 1 assert dup_nth([1,2,3], 9, ZZ) == 0 raises(IndexError, 'dup_nth([3,4,5], -1, ZZ)')
def test_dmp_validate(): assert dmp_validate([]) == ([], 0) assert dmp_validate([0, 0, 0, 1, 0]) == ([1, 0], 0) assert dmp_validate([[[]]]) == ([[[]]], 2) assert dmp_validate([[0], [], [0], [1], [0]]) == ([[1], []], 1) raises(ValueError, 'dmp_validate([[0],0,[0],[1],[0]])')
def test_zzx_compose_term(): assert zzx_compose_term([], 17) == [] assert zzx_compose_term([1,2,3], 1) == [1, 2, 3] assert zzx_compose_term([1,2,3], 2) == [1, 0, 2, 0, 3] assert zzx_compose_term([1,2,3], 3) == [1, 0, 0, 2, 0, 0, 3] assert zzx_compose_term([1,2,3], 4) == [1, 0, 0, 0, 2, 0, 0, 0, 3] raises(ValueError, 'zzx_compose_term([1,2,3], 0)')
def test_dmp_ground_nth(): assert dmp_ground_nth([[1], [2], [3]], (0, 0), 1, ZZ) == 3 assert dmp_ground_nth([[1], [2], [3]], (1, 0), 1, ZZ) == 2 assert dmp_ground_nth([[1], [2], [3]], (2, 0), 1, ZZ) == 1 assert dmp_ground_nth([[1], [2], [3]], (2, 1), 1, ZZ) == 0 assert dmp_ground_nth([[1], [2], [3]], (3, 0), 1, ZZ) == 0 raises(IndexError, 'dmp_ground_nth([[3],[4],[5]], (2,-1), 1, ZZ)')
def test_dup_inflate(): assert dup_inflate([], 17, ZZ) == [] assert dup_inflate([1, 2, 3], 1, ZZ) == [1, 2, 3] assert dup_inflate([1, 2, 3], 2, ZZ) == [1, 0, 2, 0, 3] assert dup_inflate([1, 2, 3], 3, ZZ) == [1, 0, 0, 2, 0, 0, 3] assert dup_inflate([1, 2, 3], 4, ZZ) == [1, 0, 0, 0, 2, 0, 0, 0, 3] raises(IndexError, 'dup_inflate([1,2,3], 0, ZZ)')
def test_dup_rr_div(): raises(ZeroDivisionError, "dup_rr_div([1,2,3], [], ZZ)") f = dup_normal([3,1,1,5], ZZ) g = dup_normal([5,-3,1], ZZ) q, r = [], f assert dup_rr_div(f, g, ZZ) == (q, r)
def test_zzx_compose_term(): assert zzx_compose_term([], 17) == [] assert zzx_compose_term([1, 2, 3], 1) == [1, 2, 3] assert zzx_compose_term([1, 2, 3], 2) == [1, 0, 2, 0, 3] assert zzx_compose_term([1, 2, 3], 3) == [1, 0, 0, 2, 0, 0, 3] assert zzx_compose_term([1, 2, 3], 4) == [1, 0, 0, 0, 2, 0, 0, 0, 3] raises(ValueError, 'zzx_compose_term([1,2,3], 0)')
def test_dup_inflate(): assert dup_inflate([], 17, ZZ) == [] assert dup_inflate([1,2,3], 1, ZZ) == [1,2,3] assert dup_inflate([1,2,3], 2, ZZ) == [1,0,2,0,3] assert dup_inflate([1,2,3], 3, ZZ) == [1,0,0,2,0,0,3] assert dup_inflate([1,2,3], 4, ZZ) == [1,0,0,0,2,0,0,0,3] raises(IndexError, 'dup_inflate([1,2,3], 0, ZZ)')
def test_dmp_ground_nth(): assert dmp_ground_nth([[1],[2],[3]], (0,0), 1, ZZ) == 3 assert dmp_ground_nth([[1],[2],[3]], (1,0), 1, ZZ) == 2 assert dmp_ground_nth([[1],[2],[3]], (2,0), 1, ZZ) == 1 assert dmp_ground_nth([[1],[2],[3]], (2,1), 1, ZZ) == 0 assert dmp_ground_nth([[1],[2],[3]], (3,0), 1, ZZ) == 0 raises(IndexError, 'dmp_ground_nth([[3],[4],[5]], (2,-1), 1, ZZ)')
def test_nonvectorJacobian(): x, y, z = symbols('x', 'y', 'z') X = Matrix([ [exp(x + y + z), exp(x + y + z)], [exp(x + y + z), exp(x + y + z)] ]) Y = Matrix([x, y, z]) raises(TypeError, 'X.jacobian(Y)') X = X[0,:] Y = Matrix([ [x, y], [x,z] ]) raises(TypeError, 'X.jacobian(Y)')
def test_dup_rr_div(): raises(ZeroDivisionError, "dup_rr_div([1,2,3], [], ZZ)") f = dup_normal([3, 1, 1, 5], ZZ) g = dup_normal([5, -3, 1], ZZ) q, r = [], f assert dup_rr_div(f, g, ZZ) == (q, r)
def test_nonvectorJacobian(): x, y, z = symbols('x', 'y', 'z') X = Matrix([[exp(x + y + z), exp(x + y + z)], [exp(x + y + z), exp(x + y + z)]]) Y = Matrix([x, y, z]) raises(TypeError, 'X.jacobian(Y)') X = X[0, :] Y = Matrix([[x, y], [x, z]]) raises(TypeError, 'X.jacobian(Y)')
def test__dict_from_basic_if_gens(): assert _dict_from_basic_if_gens(Integer(17), (x, )) == {(0, ): Integer(17)} assert _dict_from_basic_if_gens(Integer(17), (x, y)) == { (0, 0): Integer(17) } assert _dict_from_basic_if_gens(Integer(17), (x, y, z)) == { (0, 0, 0): Integer(17) } assert _dict_from_basic_if_gens(Integer(-17), (x, )) == { (0, ): Integer(-17) } assert _dict_from_basic_if_gens(Integer(-17), (x, y)) == { (0, 0): Integer(-17) } assert _dict_from_basic_if_gens(Integer(-17), (x, y, z)) == { (0, 0, 0): Integer(-17) } assert _dict_from_basic_if_gens(Integer(17) * x, (x, )) == { (1, ): Integer(17) } assert _dict_from_basic_if_gens(Integer(17) * x, (x, y)) == { (1, 0): Integer(17) } assert _dict_from_basic_if_gens(Integer(17) * x, (x, y, z)) == { (1, 0, 0): Integer(17) } assert _dict_from_basic_if_gens(Integer(17) * x**7, (x, )) == { (7, ): Integer(17) } assert _dict_from_basic_if_gens(Integer(17) * x**7 * y, (x, y)) == { (7, 1): Integer(17) } assert _dict_from_basic_if_gens(Integer(17) * x**7 * y * z**12, (x, y, z)) == { (7, 1, 12): Integer(17) } assert _dict_from_basic_if_gens(x+2*y+3*z, (x,)) == \ {(1,): Integer(1), (0,): 2*y+3*z} assert _dict_from_basic_if_gens(x+2*y+3*z, (x,y)) == \ {(1,0): Integer(1), (0,1): Integer(2), (0,0): 3*z} assert _dict_from_basic_if_gens(x+2*y+3*z, (x,y,z)) == \ {(1,0,0): Integer(1), (0,1,0): Integer(2), (0,0,1): Integer(3)} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,)) == \ {(1,): y+2*z, (0,): 3*y*z} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,y)) == \ {(1,1): Integer(1), (1,0): 2*z, (0,1): 3*z} assert _dict_from_basic_if_gens(x*y+2*x*z+3*y*z, (x,y,z)) == \ {(1,1,0): Integer(1), (1,0,1): Integer(2), (0,1,1): Integer(3)} assert _dict_from_basic_if_gens(2**y * x, (x, )) == {(1, ): 2**y} raises(PolynomialError, "_dict_from_basic_if_gens(2**y*x, (x,y))")
def test_dup_ff_div(): raises(ZeroDivisionError, "dup_ff_div([1,2,3], [], QQ)") f = dup_normal([3, 1, 1, 5], QQ) g = dup_normal([5, -3, 1], QQ) q = [QQ(3, 5), QQ(14, 25)] r = [QQ(52, 25), QQ(111, 25)] assert dup_ff_div(f, g, QQ) == (q, r)
def test_swap_back(): # A solution comes back from solve even though it shouldn't be: f(x) is # in the solution's Integral as the upper limit. When solve is fixed this # test should be removed. For now, since there are ode's that come back # with these sorts of solutions, the swap_back feature is performed in # solve and tested here. # This appears to be fixed - the equation is not solved. x = Symbol('x') f = Function('f', dummy = True) raises(NotImplementedError, "solve(Eq(log(f(x)), Integral(x, (x, 1, f(x)))), f(x))")
def test_dmp_swap(): f = dmp_normal([[1, 0, 0], [], [1, 0], [], [1]], 1, ZZ) g = dmp_normal([[1, 0, 0, 0, 0], [1, 0, 0], [1]], 1, ZZ) assert dmp_swap(f, 1, 1, 1, ZZ) == f assert dmp_swap(f, 0, 1, 1, ZZ) == g assert dmp_swap(g, 0, 1, 1, ZZ) == f raises(IndexError, "dmp_swap(f, -1, -7, 1, ZZ)")
def test_dup_ff_div(): raises(ZeroDivisionError, "dup_ff_div([1,2,3], [], QQ)") f = dup_normal([3,1,1,5], QQ) g = dup_normal([5,-3,1], QQ) q = [QQ(3,5), QQ(14,25)] r = [QQ(52,25), QQ(111,25)] assert dup_ff_div(f, g, QQ) == (q, r)
def test_to_number_field(): assert to_number_field(sqrt(2)) == AlgebraicNumber(sqrt(2)) assert to_number_field([sqrt(2), sqrt(3)]) == AlgebraicNumber(sqrt(2)+sqrt(3)) a = AlgebraicNumber(sqrt(2)+sqrt(3), [S(1)/2, S(0), -S(9)/2, S(0)]) assert to_number_field(sqrt(2), sqrt(2)+sqrt(3)) == a assert to_number_field(sqrt(2), AlgebraicNumber(sqrt(2)+sqrt(3))) == a raises(IsomorphismFailed, "to_number_field(sqrt(2), sqrt(3))")
def test_dmp_swap(): f = dmp_normal([[1,0,0],[],[1,0],[],[1]], 1, ZZ) g = dmp_normal([[1,0,0,0,0],[1,0,0],[1]], 1, ZZ) assert dmp_swap(f, 1, 1, 1, ZZ) == f assert dmp_swap(f, 0, 1, 1, ZZ) == g assert dmp_swap(g, 0, 1, 1, ZZ) == f raises(IndexError, "dmp_swap(f, -1, -7, 1, ZZ)")
def test_multiple_results_c(): x,y,z = symbols('xyz') expr1 = (x+y)*z expr2 = (x-y)*z routine = Routine( "test", [expr1,expr2] ) code_gen = CCodeGen() raises(CodeGenError, 'get_string(code_gen.dump_h, [routine])')
def test_randprime(): import random random.seed(1234) assert randprime(2, 3) == 2 assert randprime(1, 3) == 2 assert randprime(3, 5) == 3 raises(ValueError, 'randprime(20, 22)') for a in [100, 300, 500, 250000]: for b in [100, 300, 500, 250000]: p = randprime(a, a+b) assert a <= p < (a+b) and isprime(p)
def test_symmetric_poly(): raises(ValueError, "symmetric_poly(-1, x, y, z)") raises(ValueError, "symmetric_poly(5, x, y, z)") assert symmetric_poly(1, x, y, z, polys=True) == Poly(x + y + z) assert symmetric_poly(1, (x, y, z), polys=True) == Poly(x + y + z) assert symmetric_poly(0, x, y, z) == 1 assert symmetric_poly(1, x, y, z) == x + y + z assert symmetric_poly(2, x, y, z) == x*y + x*z + y*z assert symmetric_poly(3, x, y, z) == x*y*z