def __init_from_symbol(self, symbol: Symbol) -> None: self.terms = ((S(1), Pdop(symbol)), )
def test_issue_6540_6552(): assert S('[[1/3,2], (2/5,)]') == [[Rational(1, 3), 2], (Rational(2, 5),)] assert S('[[2/6,2], (2/4,)]') == [[Rational(1, 3), 2], (Rational(1, 2),)] assert S('[[[2*(1)]]]') == [[[2]]] assert S('Matrix([2*(1)])') == Matrix([2])
def test_Derivative_as_finite_difference(): # Central 1st derivative at gridpoint x, h = symbols('x h', real=True) dfdx = f(x).diff(x) assert (dfdx.as_finite_difference([x-2, x-1, x, x+1, x+2]) - (S.One/12*(f(x-2)-f(x+2)) + Rational(2, 3)*(f(x+1)-f(x-1)))).simplify() == 0 # Central 1st derivative "half-way" assert (dfdx.as_finite_difference() - (f(x + S.Half)-f(x - S.Half))).simplify() == 0 assert (dfdx.as_finite_difference(h) - (f(x + h/S(2))-f(x - h/S(2)))/h).simplify() == 0 assert (dfdx.as_finite_difference([x - 3*h, x-h, x+h, x + 3*h]) - (S(9)/(8*2*h)*(f(x+h) - f(x-h)) + S.One/(24*2*h)*(f(x - 3*h) - f(x + 3*h)))).simplify() == 0 # One sided 1st derivative at gridpoint assert (dfdx.as_finite_difference([0, 1, 2], 0) - (Rational(-3, 2)*f(0) + 2*f(1) - f(2)/2)).simplify() == 0 assert (dfdx.as_finite_difference([x, x+h], x) - (f(x+h) - f(x))/h).simplify() == 0 assert (dfdx.as_finite_difference([x-h, x, x+h], x-h) - (-S(3)/(2*h)*f(x-h) + 2/h*f(x) - S.One/(2*h)*f(x+h))).simplify() == 0 # One sided 1st derivative "half-way" assert (dfdx.as_finite_difference([x-h, x+h, x + 3*h, x + 5*h, x + 7*h]) - 1/(2*h)*(-S(11)/(12)*f(x-h) + S(17)/(24)*f(x+h) + Rational(3, 8)*f(x + 3*h) - Rational(5, 24)*f(x + 5*h) + S.One/24*f(x + 7*h))).simplify() == 0 d2fdx2 = f(x).diff(x, 2) # Central 2nd derivative at gridpoint assert (d2fdx2.as_finite_difference([x-h, x, x+h]) - h**-2 * (f(x-h) + f(x+h) - 2*f(x))).simplify() == 0 assert (d2fdx2.as_finite_difference([x - 2*h, x-h, x, x+h, x + 2*h]) - h**-2 * (Rational(-1, 12)*(f(x - 2*h) + f(x + 2*h)) + Rational(4, 3)*(f(x+h) + f(x-h)) - Rational(5, 2)*f(x))).simplify() == 0 # Central 2nd derivative "half-way" assert (d2fdx2.as_finite_difference([x - 3*h, x-h, x+h, x + 3*h]) - (2*h)**-2 * (S.Half*(f(x - 3*h) + f(x + 3*h)) - S.Half*(f(x+h) + f(x-h)))).simplify() == 0 # One sided 2nd derivative at gridpoint assert (d2fdx2.as_finite_difference([x, x+h, x + 2*h, x + 3*h]) - h**-2 * (2*f(x) - 5*f(x+h) + 4*f(x+2*h) - f(x+3*h))).simplify() == 0 # One sided 2nd derivative at "half-way" assert (d2fdx2.as_finite_difference([x-h, x+h, x + 3*h, x + 5*h]) - (2*h)**-2 * (Rational(3, 2)*f(x-h) - Rational(7, 2)*f(x+h) + Rational(5, 2)*f(x + 3*h) - S.Half*f(x + 5*h))).simplify() == 0 d3fdx3 = f(x).diff(x, 3) # Central 3rd derivative at gridpoint assert (d3fdx3.as_finite_difference() - (-f(x - Rational(3, 2)) + 3*f(x - S.Half) - 3*f(x + S.Half) + f(x + Rational(3, 2)))).simplify() == 0 assert (d3fdx3.as_finite_difference( [x - 3*h, x - 2*h, x-h, x, x+h, x + 2*h, x + 3*h]) - h**-3 * (S.One/8*(f(x - 3*h) - f(x + 3*h)) - f(x - 2*h) + f(x + 2*h) + Rational(13, 8)*(f(x-h) - f(x+h)))).simplify() == 0 # Central 3rd derivative at "half-way" assert (d3fdx3.as_finite_difference([x - 3*h, x-h, x+h, x + 3*h]) - (2*h)**-3 * (f(x + 3*h)-f(x - 3*h) + 3*(f(x-h)-f(x+h)))).simplify() == 0 # One sided 3rd derivative at gridpoint assert (d3fdx3.as_finite_difference([x, x+h, x + 2*h, x + 3*h]) - h**-3 * (f(x + 3*h)-f(x) + 3*(f(x+h)-f(x + 2*h)))).simplify() == 0 # One sided 3rd derivative at "half-way" assert (d3fdx3.as_finite_difference([x-h, x+h, x + 3*h, x + 5*h]) - (2*h)**-3 * (f(x + 5*h)-f(x-h) + 3*(f(x+h)-f(x + 3*h)))).simplify() == 0 # issue 11007 y = Symbol('y', real=True) d2fdxdy = f(x, y).diff(x, y) ref0 = Derivative(f(x + S.Half, y), y) - Derivative(f(x - S.Half, y), y) assert (d2fdxdy.as_finite_difference(wrt=x) - ref0).simplify() == 0 half = S.Half xm, xp, ym, yp = x-half, x+half, y-half, y+half ref2 = f(xm, ym) + f(xp, yp) - f(xp, ym) - f(xm, yp) assert (d2fdxdy.as_finite_difference() - ref2).simplify() == 0
def test_radsimp(): r2 = sqrt(2) r3 = sqrt(3) r5 = sqrt(5) r7 = sqrt(7) assert fraction(radsimp(1 / r2)) == (sqrt(2), 2) assert radsimp(1/(1 + r2)) == \ -1 + sqrt(2) assert radsimp(1/(r2 + r3)) == \ -sqrt(2) + sqrt(3) assert fraction(radsimp(1/(1 + r2 + r3))) == \ (-sqrt(6) + sqrt(2) + 2, 4) assert fraction(radsimp(1/(r2 + r3 + r5))) == \ (-sqrt(30) + 2*sqrt(3) + 3*sqrt(2), 12) assert fraction(radsimp( 1 / (1 + r2 + r3 + r5))) == ((-34 * sqrt(10) - 26 * sqrt(15) - 55 * sqrt(3) - 61 * sqrt(2) + 14 * sqrt(30) + 93 + 46 * sqrt(6) + 53 * sqrt(5), 71)) assert fraction(radsimp( 1 / (r2 + r3 + r5 + r7))) == ((-50 * sqrt(42) - 133 * sqrt(5) - 34 * sqrt(70) - 145 * sqrt(3) + 22 * sqrt(105) + 185 * sqrt(2) + 62 * sqrt(30) + 135 * sqrt(7), 215)) z = radsimp(1 / (1 + r2 / 3 + r3 / 5 + r5 + r7)) assert len((3616791619821680643598 * z).args) == 16 assert radsimp(1 / z) == 1 / z assert radsimp(1 / z, max_terms=20).expand() == 1 + r2 / 3 + r3 / 5 + r5 + r7 assert radsimp(1/(r2*3)) == \ sqrt(2)/6 assert radsimp(1 / (r2 * a + r3 + r5 + r7)) == ( (8 * sqrt(2) * a**7 - 8 * sqrt(7) * a**6 - 8 * sqrt(5) * a**6 - 8 * sqrt(3) * a**6 - 180 * sqrt(2) * a**5 + 8 * sqrt(30) * a**5 + 8 * sqrt(42) * a**5 + 8 * sqrt(70) * a**5 - 24 * sqrt(105) * a**4 + 84 * sqrt(3) * a**4 + 100 * sqrt(5) * a**4 + 116 * sqrt(7) * a**4 - 72 * sqrt(70) * a**3 - 40 * sqrt(42) * a**3 - 8 * sqrt(30) * a**3 + 782 * sqrt(2) * a**3 - 462 * sqrt(3) * a**2 - 302 * sqrt(7) * a**2 - 254 * sqrt(5) * a**2 + 120 * sqrt(105) * a**2 - 795 * sqrt(2) * a - 62 * sqrt(30) * a + 82 * sqrt(42) * a + 98 * sqrt(70) * a - 118 * sqrt(105) + 59 * sqrt(7) + 295 * sqrt(5) + 531 * sqrt(3)) / (16 * a**8 - 480 * a**6 + 3128 * a**4 - 6360 * a**2 + 3481)) assert radsimp(1 / (r2 * a + r2 * b + r3 + r7)) == ( (sqrt(2) * a * (a + b)**2 - 5 * sqrt(2) * a + sqrt(42) * a + sqrt(2) * b * (a + b)**2 - 5 * sqrt(2) * b + sqrt(42) * b - sqrt(7) * (a + b)**2 - sqrt(3) * (a + b)**2 - 2 * sqrt(3) + 2 * sqrt(7)) / (2 * a**4 + 8 * a**3 * b + 12 * a**2 * b**2 - 20 * a**2 + 8 * a * b**3 - 40 * a * b + 2 * b**4 - 20 * b**2 + 8)) assert radsimp(1/(r2*a + r2*b + r2*c + r2*d)) == \ sqrt(2)/(2*a + 2*b + 2*c + 2*d) assert radsimp(1 / (1 + r2 * a + r2 * b + r2 * c + r2 * d)) == ( (sqrt(2) * a + sqrt(2) * b + sqrt(2) * c + sqrt(2) * d - 1) / (2 * a**2 + 4 * a * b + 4 * a * c + 4 * a * d + 2 * b**2 + 4 * b * c + 4 * b * d + 2 * c**2 + 4 * c * d + 2 * d**2 - 1)) assert radsimp((y**2 - x)/(y - sqrt(x))) == \ sqrt(x) + y assert radsimp(-(y**2 - x)/(y - sqrt(x))) == \ -(sqrt(x) + y) assert radsimp(1/(1 - I + a*I)) == \ (-I*a + 1 + I)/(a**2 - 2*a + 2) assert radsimp(1/((-x + y)*(x - sqrt(y)))) == \ (-x - sqrt(y))/((x - y)*(x**2 - y)) e = (3 + 3 * sqrt(2)) * x * (3 * x - 3 * sqrt(y)) assert radsimp(e) == x * (3 + 3 * sqrt(2)) * (3 * x - 3 * sqrt(y)) assert radsimp(1 / e) == ( (-9 * x + 9 * sqrt(2) * x - 9 * sqrt(y) + 9 * sqrt(2) * sqrt(y)) / (9 * x * (9 * x**2 - 9 * y))) assert radsimp(1 + 1/(1 + sqrt(3))) == \ Mul(S.Half, -1 + sqrt(3), evaluate=False) + 1 A = symbols("A", commutative=False) assert radsimp(x**2 + sqrt(2)*x**2 - sqrt(2)*x*A) == \ x**2 + sqrt(2)*x**2 - sqrt(2)*x*A assert radsimp(1 / sqrt(5 + 2 * sqrt(6))) == -sqrt(2) + sqrt(3) assert radsimp(1 / sqrt(5 + 2 * sqrt(6))**3) == -(-sqrt(3) + sqrt(2))**3 # issue 6532 assert fraction(radsimp(1 / sqrt(x))) == (sqrt(x), x) assert fraction(radsimp(1 / sqrt(2 * x + 3))) == (sqrt(2 * x + 3), 2 * x + 3) assert fraction(radsimp(1 / sqrt(2 * (x + 3)))) == (sqrt(2 * x + 6), 2 * x + 6) # issue 5994 e = S('-(2 + 2*sqrt(2) + 4*2**(1/4))/' '(1 + 2**(3/4) + 3*2**(1/4) + 3*sqrt(2))') assert radsimp(e).expand( ) == -2 * 2**Rational(3, 4) - 2 * 2**Rational(1, 4) + 2 + 2 * sqrt(2) # issue 5986 (modifications to radimp didn't initially recognize this so # the test is included here) assert radsimp(1 / (-sqrt(5) / 2 - S.Half + (-sqrt(5) / 2 - S.Half)**2)) == 1 # from issue 5934 eq = ((-240 * sqrt(2) * sqrt(sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) - 360 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) - 120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) + 120 * sqrt(2) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) + 120 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5) + 120 * sqrt(10) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) + 120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5)) / (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) + 24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2)) assert radsimp(eq) is S.NaN # it's 0/0 # work with normal form e = 1 / sqrt(sqrt(7) / 7 + 2 * sqrt(2) + 3 * sqrt(3) + 5 * sqrt(5)) + 3 assert radsimp(e) == ( -sqrt(sqrt(7) + 14 * sqrt(2) + 21 * sqrt(3) + 35 * sqrt(5)) * (-11654899 * sqrt(35) - 1577436 * sqrt(210) - 1278438 * sqrt(15) - 1346996 * sqrt(10) + 1635060 * sqrt(6) + 5709765 + 7539830 * sqrt(14) + 8291415 * sqrt(21)) / 1300423175 + 3) # obey power rules base = sqrt(3) - sqrt(2) assert radsimp(1 / base**3) == (sqrt(3) + sqrt(2))**3 assert radsimp(1 / (-base)**3) == -(sqrt(2) + sqrt(3))**3 assert radsimp(1 / (-base)**x) == (-base)**(-x) assert radsimp(1 / base**x) == (sqrt(2) + sqrt(3))**x assert radsimp(root(1 / (-1 - sqrt(2)), -x)) == (-1)**(-1 / x) * (1 + sqrt(2))**(1 / x) # recurse e = cos(1 / (1 + sqrt(2))) assert radsimp(e) == cos(-sqrt(2) + 1) assert radsimp(e / 2) == cos(-sqrt(2) + 1) / 2 assert radsimp(1 / e) == 1 / cos(-sqrt(2) + 1) assert radsimp(2 / e) == 2 / cos(-sqrt(2) + 1) assert fraction(radsimp(e / sqrt(x))) == (sqrt(x) * cos(-sqrt(2) + 1), x) # test that symbolic denominators are not processed r = 1 + sqrt(2) assert radsimp(x / r, symbolic=False) == -x * (-sqrt(2) + 1) assert radsimp(x / (y + r), symbolic=False) == x / (y + 1 + sqrt(2)) assert radsimp(x/(y + r)/r, symbolic=False) == \ -x*(-sqrt(2) + 1)/(y + 1 + sqrt(2)) # issue 7408 eq = sqrt(x) / sqrt(y) assert radsimp(eq) == umul(sqrt(x), sqrt(y), 1 / y) assert radsimp(eq, symbolic=False) == eq # issue 7498 assert radsimp(sqrt(x) / sqrt(y)**3) == umul(sqrt(x), sqrt(y**3), 1 / y**3) # for coverage eq = sqrt(x) / y**2 assert radsimp(eq) == eq
def test_issue_4788(): assert srepr(S(1.0 + 0J)) == srepr(S(1.0)) == srepr(Float(1.0))
def test_minimal_polynomial(): assert minimal_polynomial(-7, x) == x + 7 assert minimal_polynomial(-1, x) == x + 1 assert minimal_polynomial(0, x) == x assert minimal_polynomial(1, x) == x - 1 assert minimal_polynomial(7, x) == x - 7 assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(5), x) == x**2 - 5 assert minimal_polynomial(sqrt(6), x) == x**2 - 6 assert minimal_polynomial(2 * sqrt(2), x) == x**2 - 8 assert minimal_polynomial(3 * sqrt(5), x) == x**2 - 45 assert minimal_polynomial(4 * sqrt(6), x) == x**2 - 96 assert minimal_polynomial(2 * sqrt(2) + 3, x) == x**2 - 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) + 6, x) == x**2 - 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) + 7, x) == x**2 - 14 * x - 47 assert minimal_polynomial(2 * sqrt(2) - 3, x) == x**2 + 6 * x + 1 assert minimal_polynomial(3 * sqrt(5) - 6, x) == x**2 + 12 * x - 9 assert minimal_polynomial(4 * sqrt(6) - 7, x) == x**2 + 14 * x - 47 assert minimal_polynomial(sqrt(1 + sqrt(6)), x) == x**4 - 2 * x**2 - 5 assert minimal_polynomial(sqrt(I + sqrt(6)), x) == x**8 - 10 * x**4 + 49 assert minimal_polynomial(2 * I + sqrt(2 + I), x) == x**4 + 4 * x**2 + 8 * x + 37 assert minimal_polynomial(sqrt(2) + sqrt(3), x) == x**4 - 10 * x**2 + 1 assert minimal_polynomial(sqrt(2) + sqrt(3) + sqrt(6), x) == x**4 - 22 * x**2 - 48 * x - 23 a = 1 - 9 * sqrt(2) + 7 * sqrt(3) assert minimal_polynomial( 1 / a, x) == 392 * x**4 - 1232 * x**3 + 612 * x**2 + 4 * x - 1 assert minimal_polynomial( 1 / sqrt(a), x) == 392 * x**8 - 1232 * x**6 + 612 * x**4 + 4 * x**2 - 1 raises(NotAlgebraic, lambda: minimal_polynomial(oo, x)) raises(NotAlgebraic, lambda: minimal_polynomial(2**y, x)) raises(NotAlgebraic, lambda: minimal_polynomial(sin(1), x)) assert minimal_polynomial(sqrt(2)).dummy_eq(x**2 - 2) assert minimal_polynomial(sqrt(2), x) == x**2 - 2 assert minimal_polynomial(sqrt(2), polys=True) == Poly(x**2 - 2) assert minimal_polynomial(sqrt(2), x, polys=True) == Poly(x**2 - 2) assert minimal_polynomial(sqrt(2), x, polys=True, compose=False) == Poly(x**2 - 2) a = AlgebraicNumber(sqrt(2)) b = AlgebraicNumber(sqrt(3)) assert minimal_polynomial(a, x) == x**2 - 2 assert minimal_polynomial(b, x) == x**2 - 3 assert minimal_polynomial(a, x, polys=True) == Poly(x**2 - 2) assert minimal_polynomial(b, x, polys=True) == Poly(x**2 - 3) assert minimal_polynomial(sqrt(a / 2 + 17), x) == 2 * x**4 - 68 * x**2 + 577 assert minimal_polynomial(sqrt(b / 2 + 17), x) == 4 * x**4 - 136 * x**2 + 1153 a, b = sqrt(2) / 3 + 7, AlgebraicNumber(sqrt(2) / 3 + 7) f = 81*x**8 - 2268*x**6 - 4536*x**5 + 22644*x**4 + 63216*x**3 - \ 31608*x**2 - 189648*x + 141358 assert minimal_polynomial(sqrt(a) + sqrt(sqrt(a)), x) == f assert minimal_polynomial(sqrt(b) + sqrt(sqrt(b)), x) == f assert minimal_polynomial(a**Q(3, 2), x) == 729 * x**4 - 506898 * x**2 + 84604519 # issue 5994 eq = S(''' -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)))''') assert minimal_polynomial(eq, x) == 8000 * x**2 - 1 ex = 1 + sqrt(2) + sqrt(3) mp = minimal_polynomial(ex, x) assert mp == x**4 - 4 * x**3 - 4 * x**2 + 16 * x - 8 ex = 1 / (1 + sqrt(2) + sqrt(3)) mp = minimal_polynomial(ex, x) assert mp == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1 p = (expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3))**Rational(1, 3) mp = minimal_polynomial(p, x) assert mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008 p = expand((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(p, x) assert mp == x**8 - 512 * x**7 - 118208 * x**6 + 31131136 * x**5 + 647362560 * x**4 - 56026611712 * x**3 + 116994310144 * x**2 + 404854931456 * x - 27216576512 assert minimal_polynomial(S("-sqrt(5)/2 - 1/2 + (-sqrt(5)/2 - 1/2)**2"), x) == x - 1 a = 1 + sqrt(2) assert minimal_polynomial((a * sqrt(2) + a)**3, x) == x**2 - 198 * x + 1 p = 1 / (1 + sqrt(2) + sqrt(3)) assert minimal_polynomial( p, x, compose=False) == 8 * x**4 - 16 * x**3 + 4 * x**2 + 4 * x - 1 p = 2 / (1 + sqrt(2) + sqrt(3)) assert minimal_polynomial( p, x, compose=False) == x**4 - 4 * x**3 + 2 * x**2 + 4 * x - 2 assert minimal_polynomial(1 + sqrt(2) * I, x, compose=False) == x**2 - 2 * x + 3 assert minimal_polynomial(1 / (1 + sqrt(2)) + 1, x, compose=False) == x**2 - 2 assert minimal_polynomial(sqrt(2) * I + I * (1 + sqrt(2)), x, compose=False) == x**4 + 18 * x**2 + 49
def test_AlgebraicNumber(): minpoly, root = x**2 - 2, sqrt(2) a = AlgebraicNumber(root, gen=x) assert a.rep == DMP([QQ(1), QQ(0)], QQ) assert a.root == root assert a.alias is None assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is False assert a.coeffs() == [S(1), S(0)] assert a.native_coeffs() == [QQ(1), QQ(0)] a = AlgebraicNumber(root, gen=x, alias='y') assert a.rep == DMP([QQ(1), QQ(0)], QQ) assert a.root == root assert a.alias == Symbol('y') assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is True a = AlgebraicNumber(root, gen=x, alias=Symbol('y')) assert a.rep == DMP([QQ(1), QQ(0)], QQ) assert a.root == root assert a.alias == Symbol('y') assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is True assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ) assert AlgebraicNumber(sqrt(2), ()).rep == DMP([], QQ) assert AlgebraicNumber(sqrt(2), (0, 0)).rep == DMP([], QQ) assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ) assert AlgebraicNumber(sqrt(2), [S(8) / 3]).rep == DMP([QQ(8, 3)], QQ) assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ) assert AlgebraicNumber(sqrt(2), [S(7) / 9, S(3) / 2]).rep == DMP( [QQ(7, 9), QQ(3, 2)], QQ) assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ) a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2]) assert a.rep == DMP([QQ(1), QQ(2)], QQ) assert a.root == root assert a.alias is None assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is False assert a.coeffs() == [S(1), S(2)] assert a.native_coeffs() == [QQ(1), QQ(2)] a = AlgebraicNumber((minpoly, root), [1, 2]) assert a.rep == DMP([QQ(1), QQ(2)], QQ) assert a.root == root assert a.alias is None assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is False a = AlgebraicNumber((Poly(minpoly), root), [1, 2]) assert a.rep == DMP([QQ(1), QQ(2)], QQ) assert a.root == root assert a.alias is None assert a.minpoly == minpoly assert a.is_number assert a.is_aliased is False assert AlgebraicNumber(sqrt(3)).rep == DMP([QQ(1), QQ(0)], QQ) assert AlgebraicNumber(-sqrt(3)).rep == DMP([-QQ(1), QQ(0)], QQ) a = AlgebraicNumber(sqrt(2)) b = AlgebraicNumber(sqrt(2)) assert a == b c = AlgebraicNumber(sqrt(2), gen=x) d = AlgebraicNumber(sqrt(2), gen=x) assert a == b assert a == c a = AlgebraicNumber(sqrt(2), [1, 2]) b = AlgebraicNumber(sqrt(2), [1, 3]) assert a != b and a != sqrt(2) + 3 assert (a == x) is False and (a != x) is True a = AlgebraicNumber(sqrt(2), [1, 0]) b = AlgebraicNumber(sqrt(2), [1, 0], alias=y) assert a.as_poly(x) == Poly(x) assert b.as_poly() == Poly(y) assert a.as_expr() == sqrt(2) assert a.as_expr(x) == x assert b.as_expr() == sqrt(2) assert b.as_expr(x) == x a = AlgebraicNumber(sqrt(2), [2, 3]) b = AlgebraicNumber(sqrt(2), [2, 3], alias=y) p = a.as_poly() assert p == Poly(2 * p.gen + 3) assert a.as_poly(x) == Poly(2 * x + 3) assert b.as_poly() == Poly(2 * y + 3) assert a.as_expr() == 2 * sqrt(2) + 3 assert a.as_expr(x) == 2 * x + 3 assert b.as_expr() == 2 * sqrt(2) + 3 assert b.as_expr(x) == 2 * x + 3 a = AlgebraicNumber(sqrt(2)) b = to_number_field(sqrt(2)) assert a.args == b.args == (sqrt(2), Tuple(1, 0)) b = AlgebraicNumber(sqrt(2), alias='alpha') assert b.args == (sqrt(2), Tuple(1, 0), Symbol('alpha')) a = AlgebraicNumber(sqrt(2), [1, 2, 3]) assert a.args == (sqrt(2), Tuple(1, 2, 3))
def test_im(): x, y = symbols('x,y') a, b = symbols('a,b', real=True) r = Symbol('r', real=True) i = Symbol('i', imaginary=True) assert im(nan) == nan assert im(oo * I) == oo assert im(-oo * I) == -oo assert im(0) == 0 assert im(1) == 0 assert im(-1) == 0 assert im(E * I) == E assert im(-E * I) == -E assert im(x) == im(x) assert im(x * I) == re(x) assert im(r * I) == r assert im(r) == 0 assert im(i * I) == 0 assert im(i) == -I * i assert im(x + y) == im(x + y) assert im(x + r) == im(x) assert im(x + r * I) == im(x) + r assert im(im(x) * I) == im(x) assert im(2 + I) == 1 assert im(x + I) == im(x) + 1 assert im(x + y * I) == im(x) + re(y) assert im(x + r * I) == im(x) + r assert im(log(2 * I)) == pi / 2 assert im((2 + I)**2).expand(complex=True) == 4 assert im(conjugate(x)) == -im(x) assert conjugate(im(x)) == im(x) assert im(x).as_real_imag() == (im(x), 0) assert im(i * r * x).diff(r) == im(i * x) assert im(i * r * x).diff(i) == -I * re(r * x) assert im( sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * sin(atan2(b, a) / 2) assert im(a * (2 + b * I)) == a * b assert im((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*sin(atan2(b, a)/2)/2 assert im(x).rewrite(re) == -S.ImaginaryUnit * (x - re(x)) assert (x + im(y)).rewrite(im, re) == x - S.ImaginaryUnit * (y - re(y)) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert im(S.ComplexInfinity) == S.NaN n, m, l = symbols('n m l') A = MatrixSymbol('A', n, m) assert im(A) == (S(1) / (2 * I)) * (A - conjugate(A)) A = Matrix([[1 + 4 * I, 2], [0, -3 * I]]) assert im(A) == Matrix([[4, 0], [0, -3]]) A = ImmutableMatrix([[1 + 3 * I, 3 - 2 * I], [0, 2 * I]]) assert im(A) == ImmutableMatrix([[3, -2], [0, 2]]) X = ImmutableSparseMatrix([[i * I + i for i in range(5)] for i in range(5)]) Y = SparseMatrix([[i for i in range(5)] for i in range(5)]) assert im(X).as_immutable() == Y X = FunctionMatrix(3, 3, Lambda((n, m), n + m * I)) assert im(X) == Matrix([[0, 1, 2], [0, 1, 2], [0, 1, 2]])
def test_re(): x, y = symbols('x,y') a, b = symbols('a,b', real=True) r = Symbol('r', real=True) i = Symbol('i', imaginary=True) assert re(nan) == nan assert re(oo) == oo assert re(-oo) == -oo assert re(0) == 0 assert re(1) == 1 assert re(-1) == -1 assert re(E) == E assert re(-E) == -E assert re(x) == re(x) assert re(x * I) == -im(x) assert re(r * I) == 0 assert re(r) == r assert re(i * I) == I * i assert re(i) == 0 assert re(x + y) == re(x + y) assert re(x + r) == re(x) + r assert re(re(x)) == re(x) assert re(2 + I) == 2 assert re(x + I) == re(x) assert re(x + y * I) == re(x) - im(y) assert re(x + r * I) == re(x) assert re(log(2 * I)) == log(2) assert re((2 + I)**2).expand(complex=True) == 3 assert re(conjugate(x)) == re(x) assert conjugate(re(x)) == re(x) assert re(x).as_real_imag() == (re(x), 0) assert re(i * r * x).diff(r) == re(i * x) assert re(i * r * x).diff(i) == I * r * im(x) assert re( sqrt(a + b * I)) == (a**2 + b**2)**Rational(1, 4) * cos(atan2(b, a) / 2) assert re(a * (2 + b * I)) == 2 * a assert re((1 + sqrt(a + b*I))/2) == \ (a**2 + b**2)**Rational(1, 4)*cos(atan2(b, a)/2)/2 + Rational(1, 2) assert re(x).rewrite(im) == x - S.ImaginaryUnit * im(x) assert (x + re(y)).rewrite(re, im) == x + y - S.ImaginaryUnit * im(y) a = Symbol('a', algebraic=True) t = Symbol('t', transcendental=True) x = Symbol('x') assert re(a).is_algebraic assert re(x).is_algebraic is None assert re(t).is_algebraic is False assert re(S.ComplexInfinity) == S.NaN n, m, l = symbols('n m l') A = MatrixSymbol('A', n, m) assert re(A) == (S(1) / 2) * (A + conjugate(A)) A = Matrix([[1 + 4 * I, 2], [0, -3 * I]]) assert re(A) == Matrix([[1, 2], [0, 0]]) A = ImmutableMatrix([[1 + 3 * I, 3 - 2 * I], [0, 2 * I]]) assert re(A) == ImmutableMatrix([[1, 3], [0, 0]]) X = SparseMatrix([[2 * j + i * I for i in range(5)] for j in range(5)]) assert re(X) - Matrix([[0, 0, 0, 0, 0], [2, 2, 2, 2, 2], [4, 4, 4, 4, 4], [6, 6, 6, 6, 6], [8, 8, 8, 8, 8] ]) == Matrix.zeros(5) assert im(X) - Matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4] ]) == Matrix.zeros(5) X = FunctionMatrix(3, 3, Lambda((n, m), n + m * I)) assert re(X) == Matrix([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
def test_mellin_transform_bessel(): from sympy import Max MT = mellin_transform # 8.4.19 assert MT(besselj(a, 2*sqrt(x)), x, s) == \ (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, Rational(3, 4)), True) assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(-2*s + S.Half)*gamma(a/2 + s + S.Half)/( gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), ( -re(a)/2 - S.Half, Rational(1, 4)), True) assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(a/2 + s)*gamma(-2*s + S.Half)/( gamma(-a/2 - s + S.Half)*gamma(a - 2*s + 1)), ( -re(a)/2, Rational(1, 4)), True) assert MT(besselj(a, sqrt(x))**2, x, s) == \ (gamma(a + s)*gamma(S.Half - s) / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), (-re(a), S.Half), True) assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \ (gamma(s)*gamma(S.Half - s) / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)), (0, S.Half), True) # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as # I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large) assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (gamma(1 - s)*gamma(a + s - S.Half) / (sqrt(pi)*gamma(Rational(3, 2) - s)*gamma(a - s + S.Half)), (S.Half - re(a), S.Half), True) assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \ (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s) / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2) *gamma( 1 - s + (a + b)/2)), (-(re(a) + re(b))/2, S.Half), True) assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \ ((Max(re(a), -re(a)), S.Half), True) # Section 8.4.20 assert MT(bessely(a, 2*sqrt(x)), x, s) == \ (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi, (Max(-re(a)/2, re(a)/2), Rational(3, 4)), True) assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*sin(pi*(a/2 - s))*gamma(S.Half - 2*s) * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s) / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)), (Max(-(re(a) + 1)/2, (re(a) - 1)/2), Rational(1, 4)), True) assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(S.Half - 2*s) / (sqrt(pi)*gamma(S.Half - s - a/2)*gamma(S.Half - s + a/2)), (Max(-re(a)/2, re(a)/2), Rational(1, 4)), True) assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(S.Half - s) / (pi**S('3/2')*gamma(1 + a - s)), (Max(-re(a), 0), S.Half), True) assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \ (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s) * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)), (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), S.Half), True) # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x)) # are a mess (no matter what way you look at it ...) assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \ ((Max(-re(a), 0, re(a)), S.Half), True) # Section 8.4.22 # TODO we can't do any of these (delicate cancellation) # Section 8.4.23 assert MT(besselk(a, 2*sqrt(x)), x, s) == \ (gamma( s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True) assert MT( besselj(a, 2 * sqrt(2 * sqrt(x))) * besselk(a, 2 * sqrt(2 * sqrt(x))), x, s) == (4**(-s) * gamma(2 * s) * gamma(a / 2 + s) / (2 * gamma(a / 2 - s + 1)), (Max(0, -re(a) / 2), oo), True) # TODO bessely(a, x)*besselk(a, x) is a mess assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \ (gamma(s)*gamma( a + s)*gamma(-s + S.Half)/(2*sqrt(pi)*gamma(a - s + 1)), (Max(-re(a), 0), S.Half), True) assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \ (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s)* \ gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1)* \ gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2, \ re(a)/2 - re(b)/2), S.Half), True) # TODO products of besselk are a mess mt = MT(exp(-x / 2) * besselk(a, x / 2), x, s) mt0 = gammasimp((trigsimp(gammasimp(mt[0].expand(func=True))))) assert mt0 == 2 * pi**Rational(3, 2) * cos(pi * s) * gamma(-s + S.Half) / ( (cos(2 * pi * a) - cos(2 * pi * s)) * gamma(-a - s + 1) * gamma(a - s + 1)) assert mt[1:] == ((Max(-re(a), re(a)), oo), True)
def test_ineq_unequal(): S = sympify x, y, z = symbols('x,y,z') e = (S(-1) >= x, S(-1) >= y, S(-1) >= z, S(-1) > x, S(-1) > y, S(-1) > z, S(-1) <= x, S(-1) <= y, S(-1) <= z, S(-1) < x, S(-1) < y, S(-1) < z, S(0) >= x, S(0) >= y, S(0) >= z, S(0) > x, S(0) > y, S(0) > z, S(0) <= x, S(0) <= y, S(0) <= z, S(0) < x, S(0) < y, S(0) < z, S('3/7') >= x, S('3/7') >= y, S('3/7') >= z, S('3/7') > x, S('3/7') > y, S('3/7') > z, S('3/7') <= x, S('3/7') <= y, S('3/7') <= z, S('3/7') < x, S('3/7') < y, S('3/7') < z, S(1.5) >= x, S(1.5) >= y, S(1.5) >= z, S(1.5) > x, S(1.5) > y, S(1.5) > z, S(1.5) <= x, S(1.5) <= y, S(1.5) <= z, S(1.5) < x, S(1.5) < y, S(1.5) < z, S(2) >= x, S(2) >= y, S(2) >= z, S(2) > x, S(2) > y, S(2) > z, S(2) <= x, S(2) <= y, S(2) <= z, S(2) < x, S(2) < y, S(2) < z, x >= -1, y >= -1, z >= -1, x > -1, y > -1, z > -1, x <= -1, y <= -1, z <= -1, x < -1, y < -1, z < -1, x >= 0, y >= 0, z >= 0, x > 0, y > 0, z > 0, x <= 0, y <= 0, z <= 0, x < 0, y < 0, z < 0, x >= 1.5, y >= 1.5, z >= 1.5, x > 1.5, y > 1.5, z > 1.5, x <= 1.5, y <= 1.5, z <= 1.5, x < 1.5, y < 1.5, z < 1.5, x >= 2, y >= 2, z >= 2, x > 2, y > 2, z > 2, x <= 2, y <= 2, z <= 2, x < 2, y < 2, z < 2, x >= y, x >= z, y >= x, y >= z, z >= x, z >= y, x > y, x > z, y > x, y > z, z > x, z > y, x <= y, x <= z, y <= x, y <= z, z <= x, z <= y, x < y, x < z, y < x, y < z, z < x, z < y, x - pi >= y + z, y - pi >= x + z, z - pi >= x + y, x - pi > y + z, y - pi > x + z, z - pi > x + y, x - pi <= y + z, y - pi <= x + z, z - pi <= x + y, x - pi < y + z, y - pi < x + z, z - pi < x + y, True, False) left_e = e[:-1] for i, e1 in enumerate(left_e): for e2 in e[i + 1:]: assert e1 != e2
def test_as_content_primitive(): assert (x / 2 + y).as_content_primitive() == (S.Half, x + 2 * y) assert (x / 2 + y).as_content_primitive(clear=False) == (S.One, x / 2 + y) assert (y * (x / 2 + y)).as_content_primitive() == (S.Half, y * (x + 2 * y)) assert (y * (x / 2 + y)).as_content_primitive(clear=False) == (S.One, y * (x / 2 + y)) # although the _as_content_primitive methods do not alter the underlying structure, # the as_content_primitive function will touch up the expression and join # bases that would otherwise have not been joined. assert ((x*(2 + 2*x)*(3*x + 3)**2)).as_content_primitive() == \ (18, x*(x + 1)**3) assert (2 + 2*x + 2*y*(3 + 3*y)).as_content_primitive() == \ (2, x + 3*y*(y + 1) + 1) assert ((2 + 6*x)**2).as_content_primitive() == \ (4, (3*x + 1)**2) assert ((2 + 6*x)**(2*y)).as_content_primitive() == \ (1, (_keep_coeff(S(2), (3*x + 1)))**(2*y)) assert (5 + 10*x + 2*y*(3 + 3*y)).as_content_primitive() == \ (1, 10*x + 6*y*(y + 1) + 5) assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive() == \ (11, x*(y + 1)) assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive() == \ (121, x**2*(y + 1)**2) assert (y**2).as_content_primitive() == \ (1, y**2) assert (S.Infinity).as_content_primitive() == (1, oo) eq = x**(2 + y) assert (eq).as_content_primitive() == (1, eq) assert (S.Half**(2 + x)).as_content_primitive() == (S(1) / 4, 2**-x) assert ((-S.Half)**(2 + x)).as_content_primitive() == \ (S(1)/4, (-S.Half)**x) assert ((-S.Half)**(2 + x)).as_content_primitive() == \ (S(1)/4, (-S.Half)**x) assert (4**((1 + y) / 2)).as_content_primitive() == (2, 4**(y / 2)) assert (3**((1 + y)/2)).as_content_primitive() == \ (1, 3**(Mul(S(1)/2, 1 + y, evaluate=False))) assert (5**(S(3) / 4)).as_content_primitive() == (1, 5**(S(3) / 4)) assert (5**(S(7) / 4)).as_content_primitive() == (5, 5**(S(3) / 4)) assert Add(5*z/7, 0.5*x, 3*y/2, evaluate=False).as_content_primitive() == \ (S(1)/14, 7.0*x + 21*y + 10*z) assert (2**(S(3)/4) + 2**(S(1)/4)*sqrt(3)).as_content_primitive(radical=True) == \ (1, 2**(S(1)/4)*(sqrt(2) + sqrt(3)))
def test_issue_9448(): tmp = sympify( "1/(1 - (-1)**(2/3) - (-1)**(1/3)) + 1/(1 + (-1)**(2/3) + (-1)**(1/3))" ) assert nsimplify(tmp) == S(1) / 2
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5)) / 2 assert nsimplify((1 + sqrt(5)) / 4, [GoldenRatio]) == GoldenRatio / 2 assert nsimplify(2 / GoldenRatio, [GoldenRatio]) == 2 * GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == \ sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(3*pi/5, evaluate=False)) == \ sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2 * atan('1/4') * I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1 / 3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \ 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1 / .3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi * log(2) / 8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \ -pi/4 - log(2) + S(7)/4 assert nsimplify(x / 7.0) == x / 7 assert nsimplify(pi / 1e2) == pi / 100 assert nsimplify(pi / 1e2, rational=False) == pi / 100.0 assert nsimplify(pi / 1e-7) == 10000000 * pi assert not nsimplify( factor(-3.0 * z**2 * (z**2)**(-2.5) + 3 * (z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == -S(2031) / 10 assert nsimplify(.2, tolerance=0) == S.One / 5 assert nsimplify(-.2, tolerance=0) == -S.One / 5 assert nsimplify(.2222, tolerance=0) == S(1111) / 5000 assert nsimplify(-.2222, tolerance=0) == -S(1111) / 5000 # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == S(1) / 50000000 # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i) * oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333) # Make sure nsimplify on expressions uses full precision assert nsimplify( pi.evalf(100) * x, rational_conversion='exact').evalf(100) == pi.evalf(100) * x
def test_issue_6121(): eq = -I * exp(-3 * I * pi / 4) / (4 * pi**(S(3) / 2) * sqrt(x)) assert cse((eq).expand(complex=True), optimizations='basic') \ == S(''' ([(x0, re(x)), (x1, im(x)), (x2, atan2(x1, x0)/2), (x3, sin(x2)), (x4, cos(x2))], [sqrt(2)*(x3 + I*x3 - x4 + I*x4)/(8*pi**(3/2)*(x0**2 + x1**2)**(1/4))])''')
def test_leading_order2(): assert set((2 + pi + x**2).extract_leading_order(x)) == set( ((pi, O(1, x)), (S(2), O(1, x)))) assert set((2 * x + pi * x + x**2).extract_leading_order(x)) == set( ((2 * x, O(x)), (x * pi, O(x))))
def test_minpoly_compose(): # issue 6868 eq = S(''' -1/(800*sqrt(-1/240 + 1/(18000*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)) + 2*(-1/17280000 + sqrt(15)*I/28800000)**(1/3)))''') mp = minimal_polynomial(eq + 3, x) assert mp == 8000 * x**2 - 48000 * x + 71999 # issue 5888 assert minimal_polynomial(exp(I * pi / 8), x) == x**8 + 1 mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(sin(pi / 7) + sqrt(2), x) assert mp == 4096*x**12 - 63488*x**10 + 351488*x**8 - 826496*x**6 + \ 770912*x**4 - 268432*x**2 + 28561 mp = minimal_polynomial(cos(pi / 7) + sqrt(2), x) assert mp == 64*x**6 - 64*x**5 - 432*x**4 + 304*x**3 + 712*x**2 - \ 232*x - 239 mp = minimal_polynomial(exp(I * pi / 7) + sqrt(2), x) assert mp == x**12 - 2 * x**11 - 9 * x**10 + 16 * x**9 + 43 * x**8 - 70 * x**7 - 97 * x**6 + 126 * x**5 + 211 * x**4 - 212 * x**3 - 37 * x**2 + 142 * x + 127 mp = minimal_polynomial(exp(2 * I * pi / 7), x) assert mp == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1 mp = minimal_polynomial(exp(2 * I * pi / 15), x) assert mp == x**8 - x**7 + x**5 - x**4 + x**3 - x + 1 mp = minimal_polynomial(cos(2 * pi / 7), x) assert mp == 8 * x**3 + 4 * x**2 - 4 * x - 1 mp = minimal_polynomial(sin(2 * pi / 7), x) ex = (5 * cos(2 * pi / 7) - 7) / (9 * cos(pi / 7) - 5 * cos(3 * pi / 7)) mp = minimal_polynomial(ex, x) assert mp == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(-1 / (2 * cos(pi / 7)), x) == x**3 + 2 * x**2 - x - 1 assert minimal_polynomial(sin(2*pi/15), x) == \ 256*x**8 - 448*x**6 + 224*x**4 - 32*x**2 + 1 assert minimal_polynomial(sin(5 * pi / 14), x) == 8 * x**3 - 4 * x**2 - 4 * x + 1 assert minimal_polynomial(cos( pi / 15), x) == 16 * x**4 + 8 * x**3 - 16 * x**2 - 8 * x + 1 ex = RootOf(x**3 + x * 4 + 1, 0) mp = minimal_polynomial(ex, x) assert mp == x**3 + 4 * x + 1 mp = minimal_polynomial(ex + 1, x) assert mp == x**3 - 3 * x**2 + 7 * x - 4 assert minimal_polynomial(exp(I * pi / 3), x) == x**2 - x + 1 assert minimal_polynomial(exp(I * pi / 4), x) == x**4 + 1 assert minimal_polynomial(exp(I * pi / 6), x) == x**4 - x**2 + 1 assert minimal_polynomial(exp(I * pi / 9), x) == x**6 - x**3 + 1 assert minimal_polynomial(exp(I * pi / 10), x) == x**8 - x**6 + x**4 - x**2 + 1 assert minimal_polynomial(sin(pi / 9), x) == 64 * x**6 - 96 * x**4 + 36 * x**2 - 3 assert minimal_polynomial(sin(pi/11), x) == 1024*x**10 - 2816*x**8 + \ 2816*x**6 - 1232*x**4 + 220*x**2 - 11 ex = 2**Rational(1, 3) * exp(Rational(2, 3) * I * pi) assert minimal_polynomial(ex, x) == x**3 - 2 raises(NotAlgebraic, lambda: minimal_polynomial(cos(pi * sqrt(2)), x)) raises(NotAlgebraic, lambda: minimal_polynomial(sin(pi * sqrt(2)), x)) raises(NotAlgebraic, lambda: minimal_polynomial(exp(I * pi * sqrt(2)), x)) # issue 5934 ex = 1 / (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) + 24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2) + 1 raises(ZeroDivisionError, lambda: minimal_polynomial(ex, x)) ex = sqrt(1 + 2**Rational(1, 3)) + sqrt(1 + 2**Rational(1, 4)) + sqrt(2) mp = minimal_polynomial(ex, x) assert degree(mp) == 48 and mp.subs({x: 0}) == -16630256576
def test_simple_8(): assert O(sqrt(-x)) == O(sqrt(x)) assert O(x**2 * sqrt(x)) == O(x**(S(5) / 2)) assert O(x**3 * sqrt(-(-x)**3)) == O(x**(S(9) / 2)) assert O(x**(S(3) / 2) * sqrt((-x)**3)) == O(x**3) assert O(x * (-2 * x)**(I / 2)) == O(x * (-x)**(I / 2))
def test_field_isomorphism(): assert field_isomorphism(3, sqrt(2)) == [3] assert field_isomorphism(I * sqrt(3), I * sqrt(3) / 2) == [2, 0] assert field_isomorphism(-I * sqrt(3), I * sqrt(3) / 2) == [-2, 0] assert field_isomorphism(I * sqrt(3), -I * sqrt(3) / 2) == [-2, 0] assert field_isomorphism(-I * sqrt(3), -I * sqrt(3) / 2) == [2, 0] assert field_isomorphism(2 * I * sqrt(3) / 7, 5 * I * sqrt(3) / 3) == [S(6) / 35, 0] assert field_isomorphism(-2 * I * sqrt(3) / 7, 5 * I * sqrt(3) / 3) == [-S(6) / 35, 0] assert field_isomorphism(2 * I * sqrt(3) / 7, -5 * I * sqrt(3) / 3) == [-S(6) / 35, 0] assert field_isomorphism(-2 * I * sqrt(3) / 7, -5 * I * sqrt(3) / 3) == [S(6) / 35, 0] assert field_isomorphism(2 * I * sqrt(3) / 7 + 27, 5 * I * sqrt(3) / 3) == [S(6) / 35, 27] assert field_isomorphism(-2 * I * sqrt(3) / 7 + 27, 5 * I * sqrt(3) / 3) == [-S(6) / 35, 27] assert field_isomorphism(2 * I * sqrt(3) / 7 + 27, -5 * I * sqrt(3) / 3) == [-S(6) / 35, 27] assert field_isomorphism(-2 * I * sqrt(3) / 7 + 27, -5 * I * sqrt(3) / 3) == [S(6) / 35, 27] p = AlgebraicNumber(sqrt(2) + sqrt(3)) q = AlgebraicNumber(-sqrt(2) + sqrt(3)) r = AlgebraicNumber(sqrt(2) - sqrt(3)) s = AlgebraicNumber(-sqrt(2) - sqrt(3)) pos_coeffs = [S(1) / 2, S(0), -S(9) / 2, S(0)] neg_coeffs = [-S(1) / 2, S(0), S(9) / 2, S(0)] a = AlgebraicNumber(sqrt(2)) assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == pos_coeffs assert field_isomorphism(a, q, fast=True) == neg_coeffs assert field_isomorphism(a, r, fast=True) == pos_coeffs assert field_isomorphism(a, s, fast=True) == neg_coeffs assert field_isomorphism(a, p, fast=False) == pos_coeffs assert field_isomorphism(a, q, fast=False) == neg_coeffs assert field_isomorphism(a, r, fast=False) == pos_coeffs assert field_isomorphism(a, s, fast=False) == neg_coeffs a = AlgebraicNumber(-sqrt(2)) assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == neg_coeffs assert field_isomorphism(a, q, fast=True) == pos_coeffs assert field_isomorphism(a, r, fast=True) == neg_coeffs assert field_isomorphism(a, s, fast=True) == pos_coeffs assert field_isomorphism(a, p, fast=False) == neg_coeffs assert field_isomorphism(a, q, fast=False) == pos_coeffs assert field_isomorphism(a, r, fast=False) == neg_coeffs assert field_isomorphism(a, s, fast=False) == pos_coeffs pos_coeffs = [S(1) / 2, S(0), -S(11) / 2, S(0)] neg_coeffs = [-S(1) / 2, S(0), S(11) / 2, S(0)] a = AlgebraicNumber(sqrt(3)) assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == neg_coeffs assert field_isomorphism(a, q, fast=True) == neg_coeffs assert field_isomorphism(a, r, fast=True) == pos_coeffs assert field_isomorphism(a, s, fast=True) == pos_coeffs assert field_isomorphism(a, p, fast=False) == neg_coeffs assert field_isomorphism(a, q, fast=False) == neg_coeffs assert field_isomorphism(a, r, fast=False) == pos_coeffs assert field_isomorphism(a, s, fast=False) == pos_coeffs a = AlgebraicNumber(-sqrt(3)) assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == pos_coeffs assert field_isomorphism(a, q, fast=True) == pos_coeffs assert field_isomorphism(a, r, fast=True) == neg_coeffs assert field_isomorphism(a, s, fast=True) == neg_coeffs assert field_isomorphism(a, p, fast=False) == pos_coeffs assert field_isomorphism(a, q, fast=False) == pos_coeffs assert field_isomorphism(a, r, fast=False) == neg_coeffs assert field_isomorphism(a, s, fast=False) == neg_coeffs pos_coeffs = [S(3) / 2, S(0), -S(33) / 2, -S(8)] neg_coeffs = [-S(3) / 2, S(0), S(33) / 2, -S(8)] a = AlgebraicNumber(3 * sqrt(3) - 8) assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == neg_coeffs assert field_isomorphism(a, q, fast=True) == neg_coeffs assert field_isomorphism(a, r, fast=True) == pos_coeffs assert field_isomorphism(a, s, fast=True) == pos_coeffs assert field_isomorphism(a, p, fast=False) == neg_coeffs assert field_isomorphism(a, q, fast=False) == neg_coeffs assert field_isomorphism(a, r, fast=False) == pos_coeffs assert field_isomorphism(a, s, fast=False) == pos_coeffs a = AlgebraicNumber(3 * sqrt(2) + 2 * sqrt(3) + 1) pos_1_coeffs = [S(1) / 2, S(0), -S(5) / 2, S(1)] neg_5_coeffs = [-S(5) / 2, S(0), S(49) / 2, S(1)] pos_5_coeffs = [S(5) / 2, S(0), -S(49) / 2, S(1)] neg_1_coeffs = [-S(1) / 2, S(0), S(5) / 2, S(1)] assert is_isomorphism_possible(a, p) is True assert is_isomorphism_possible(a, q) is True assert is_isomorphism_possible(a, r) is True assert is_isomorphism_possible(a, s) is True assert field_isomorphism(a, p, fast=True) == pos_1_coeffs assert field_isomorphism(a, q, fast=True) == neg_5_coeffs assert field_isomorphism(a, r, fast=True) == pos_5_coeffs assert field_isomorphism(a, s, fast=True) == neg_1_coeffs assert field_isomorphism(a, p, fast=False) == pos_1_coeffs assert field_isomorphism(a, q, fast=False) == neg_5_coeffs assert field_isomorphism(a, r, fast=False) == pos_5_coeffs assert field_isomorphism(a, s, fast=False) == neg_1_coeffs a = AlgebraicNumber(sqrt(2)) b = AlgebraicNumber(sqrt(3)) c = AlgebraicNumber(sqrt(7)) assert is_isomorphism_possible(a, b) is True assert is_isomorphism_possible(b, a) is True assert is_isomorphism_possible(c, p) is False assert field_isomorphism(sqrt(2), sqrt(3), fast=True) is None assert field_isomorphism(sqrt(3), sqrt(2), fast=True) is None assert field_isomorphism(sqrt(2), sqrt(3), fast=False) is None assert field_isomorphism(sqrt(3), sqrt(2), fast=False) is None
def test_nan_inequality_raise_errors(): # See discussion in pull request #7776. We test inequalities with # a set including examples of various classes. for q in (x, S.Zero, S(10), S.One / 3, pi, S(1.3), oo, -oo, nan): assert_all_ineq_raise_TypeError(q, nan)
def is_absorbing_state(self, state): trans_probs = self.transition_probabilities if isinstance(trans_probs, ImmutableMatrix) and \ state < trans_probs.shape[0]: return S(trans_probs[state, state]) == S.One
def test_plane(): x, y, z, u, v = symbols('x y z u v', real=True) p1 = Point3D(0, 0, 0) p2 = Point3D(1, 1, 1) p3 = Point3D(1, 2, 3) pl3 = Plane(p1, p2, p3) pl4 = Plane(p1, normal_vector=(1, 1, 1)) pl4b = Plane(p1, p2) pl5 = Plane(p3, normal_vector=(1, 2, 3)) pl6 = Plane(Point3D(2, 3, 7), normal_vector=(2, 2, 2)) pl7 = Plane(Point3D(1, -5, -6), normal_vector=(1, -2, 1)) pl8 = Plane(p1, normal_vector=(0, 0, 1)) pl9 = Plane(p1, normal_vector=(0, 12, 0)) pl10 = Plane(p1, normal_vector=(-2, 0, 0)) pl11 = Plane(p2, normal_vector=(0, 0, 1)) l1 = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1)) l2 = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1)) l3 = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9)) assert Plane(p1, p2, p3) != Plane(p1, p3, p2) assert Plane(p1, p2, p3).is_coplanar(Plane(p1, p3, p2)) assert pl3 == Plane(Point3D(0, 0, 0), normal_vector=(1, -2, 1)) assert pl3 != pl4 assert pl4 == pl4b assert pl5 == Plane(Point3D(1, 2, 3), normal_vector=(1, 2, 3)) assert pl5.equation(x, y, z) == x + 2 * y + 3 * z - 14 assert pl3.equation(x, y, z) == x - 2 * y + z assert pl3.p1 == p1 assert pl4.p1 == p1 assert pl5.p1 == p3 assert pl4.normal_vector == (1, 1, 1) assert pl5.normal_vector == (1, 2, 3) assert p1 in pl3 assert p1 in pl4 assert p3 in pl5 assert pl3.projection(Point(0, 0)) == p1 p = pl3.projection(Point3D(1, 1, 0)) assert p == Point3D(7 / 6, 2 / 3, 1 / 6) assert p in pl3 l = pl3.projection_line(Line(Point(0, 0), Point(1, 1))) assert l == Line3D(Point3D(0, 0, 0), Point3D(7 / 6, 2 / 3, 1 / 6)) assert l in pl3 # get a segment that does not intersect the plane which is also # parallel to pl3's normal veector t = Dummy() r = pl3.random_point() a = pl3.perpendicular_line(r).arbitrary_point(t) s = Segment3D(a.subs(t, 1), a.subs(t, 2)) assert s.p1 not in pl3 and s.p2 not in pl3 assert pl3.projection_line(s).equals(r) assert pl3.projection_line(Segment(Point(1, 0), Point(1, 1))) == \ Segment3D(Point3D(5/6, 1/3, -1/6), Point3D(7/6, 2/3, 1/6)) assert pl6.projection_line(Ray(Point(1, 0), Point(1, 1))) == \ Ray3D(Point3D(14/3, 11/3, 11/3), Point3D(13/3, 13/3, 10/3)) assert pl3.perpendicular_line(r.args) == pl3.perpendicular_line(r) assert pl3.is_parallel(pl6) is False assert pl4.is_parallel(pl6) assert pl6.is_parallel(l1) is False assert pl3.is_perpendicular(pl6) assert pl4.is_perpendicular(pl7) assert pl6.is_perpendicular(pl7) assert pl6.is_perpendicular(l1) is False assert pl6.distance(pl6.arbitrary_point(u, v)) == 0 assert pl7.distance(pl7.arbitrary_point(u, v)) == 0 assert pl6.distance(pl6.arbitrary_point(t)) == 0 assert pl7.distance(pl7.arbitrary_point(t)) == 0 assert pl6.p1.distance(pl6.arbitrary_point(t)).simplify() == 1 assert pl7.p1.distance(pl7.arbitrary_point(t)).simplify() == 1 assert pl3.arbitrary_point(t) == Point3D(-sqrt(30)*sin(t)/30 + \ 2*sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/15 + sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/6) assert pl3.arbitrary_point(u, v) == Point3D(2 * u - v, u + 2 * v, 5 * v) assert pl7.distance(Point3D(1, 3, 5)) == 5 * sqrt(6) / 6 assert pl6.distance(Point3D(0, 0, 0)) == 4 * sqrt(3) assert pl6.distance(pl6.p1) == 0 assert pl7.distance(pl6) == 0 assert pl7.distance(l1) == 0 assert pl6.distance(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == 0 pl6.distance(Plane(Point3D(5, 5, 5), normal_vector=(8, 8, 8))) == sqrt(3) assert pl6.angle_between(pl3) == pi / 2 assert pl6.angle_between(pl6) == 0 assert pl6.angle_between(pl4) == 0 assert pl7.angle_between(Line3D(Point3D(2, 3, 5), Point3D(2, 4, 6))) == \ -asin(sqrt(3)/6) assert pl6.angle_between(Ray3D(Point3D(2, 4, 1), Point3D(6, 5, 3))) == \ asin(sqrt(7)/3) assert pl7.angle_between(Segment3D(Point3D(5, 6, 1), Point3D(1, 2, 4))) == \ asin(7*sqrt(246)/246) assert are_coplanar(l1, l2, l3) is False assert are_coplanar(l1) is False assert are_coplanar(Point3D(2, 7, 2), Point3D(0, 0, 2), Point3D(1, 1, 2), Point3D(1, 2, 2)) assert are_coplanar(Plane(p1, p2, p3), Plane(p1, p3, p2)) assert Plane.are_concurrent(pl3, pl4, pl5) is False assert Plane.are_concurrent(pl6) is False raises(ValueError, lambda: Plane.are_concurrent(Point3D(0, 0, 0))) raises(ValueError, lambda: Plane((1, 2, 3), normal_vector=(0, 0, 0))) assert pl3.parallel_plane(Point3D(1, 2, 5)) == Plane(Point3D(1, 2, 5), \ normal_vector=(1, -2, 1)) # perpendicular_plane p = Plane((0, 0, 0), (1, 0, 0)) # default assert p.perpendicular_plane() == Plane(Point3D(0, 0, 0), (0, 1, 0)) # 1 pt assert p.perpendicular_plane(Point3D(1, 0, 1)) == \ Plane(Point3D(1, 0, 1), (0, 1, 0)) # pts as tuples assert p.perpendicular_plane((1, 0, 1), (1, 1, 1)) == \ Plane(Point3D(1, 0, 1), (0, 0, -1)) a, b = Point3D(0, 0, 0), Point3D(0, 1, 0) Z = (0, 0, 1) p = Plane(a, normal_vector=Z) # case 4 assert p.perpendicular_plane(a, b) == Plane(a, (1, 0, 0)) n = Point3D(*Z) # case 1 assert p.perpendicular_plane(a, n) == Plane(a, (-1, 0, 0)) # case 2 assert Plane(a, normal_vector=b.args).perpendicular_plane(a, a + b) == \ Plane(Point3D(0, 0, 0), (1, 0, 0)) # case 1&3 assert Plane(b, normal_vector=Z).perpendicular_plane(b, b + n) == \ Plane(Point3D(0, 1, 0), (-1, 0, 0)) # case 2&3 assert Plane(b, normal_vector=b.args).perpendicular_plane(n, n + b) == \ Plane(Point3D(0, 0, 1), (1, 0, 0)) assert pl6.intersection(pl6) == [pl6] assert pl4.intersection(pl4.p1) == [pl4.p1] assert pl3.intersection(pl6) == [ Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6)) ] assert pl3.intersection(Line3D(Point3D(1, 2, 4), Point3D(4, 4, 2))) == [Point3D(2, 8 / 3, 10 / 3)] assert pl3.intersection(Plane(Point3D(6, 0, 0), normal_vector=(2, -5, 3))) == [ Line3D(Point3D(-24, -12, 0), Point3D(-25, -13, -1)) ] assert pl6.intersection(Ray3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [Point3D(-1, 3, 10)] assert pl6.intersection(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [Point3D(-1, 3, 10)] assert pl7.intersection(Line(Point(2, 3), Point(4, 2))) == [Point3D(13 / 2, 3 / 4, 0)] r = Ray(Point(2, 3), Point(4, 2)) assert Plane((1, 2, 0), normal_vector=(0, 0, 1)).intersection(r) == [ Ray3D(Point(2, 3), Point(4, 2)) ] assert pl9.intersection(pl8) == [ Line3D(Point3D(0, 0, 0), Point3D(12, 0, 0)) ] assert pl10.intersection(pl11) == [ Line3D(Point3D(0, 0, 1), Point3D(0, 2, 1)) ] assert pl4.intersection(pl8) == [ Line3D(Point3D(0, 0, 0), Point3D(1, -1, 0)) ] assert pl11.intersection(pl8) == [] assert pl9.intersection(pl11) == [ Line3D(Point3D(0, 0, 1), Point3D(12, 0, 1)) ] assert pl9.intersection(pl4) == [ Line3D(Point3D(0, 0, 0), Point3D(12, 0, -12)) ] assert pl3.random_point() in pl3 # test geometrical entity using equals assert pl4.intersection(pl4.p1)[0].equals(pl4.p1) assert pl3.intersection(pl6)[0].equals( Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6))) pl8 = Plane((1, 2, 0), normal_vector=(0, 0, 1)) assert pl8.intersection(Line3D(p1, (1, 12, 0)))[0].equals( Line((0, 0, 0), (0.1, 1.2, 0))) assert pl8.intersection(Ray3D(p1, (1, 12, 0)))[0].equals( Ray((0, 0, 0), (1, 12, 0))) assert pl8.intersection(Segment3D(p1, (21, 1, 0)))[0].equals( Segment3D(p1, (21, 1, 0))) assert pl8.intersection(Plane(p1, normal_vector=(0, 0, 112)))[0].equals(pl8) assert pl8.intersection(Plane(p1, normal_vector=(0, 12, 0)))[0].equals( Line3D(p1, direction_ratio=(112 * pi, 0, 0))) assert pl8.intersection(Plane(p1, normal_vector=(11, 0, 1)))[0].equals( Line3D(p1, direction_ratio=(0, -11, 0))) assert pl8.intersection(Plane(p1, normal_vector=(1, 0, 11)))[0].equals( Line3D(p1, direction_ratio=(0, 11, 0))) assert pl8.intersection(Plane(p1, normal_vector=(-1, -1, -11)))[0].equals( Line3D(p1, direction_ratio=(1, -1, 0))) assert pl3.random_point() in pl3 assert len(pl8.intersection(Ray3D(Point3D(0, 2, 3), Point3D(1, 0, 3)))) is 0 # check if two plane are equals assert pl6.intersection(pl6)[0].equals(pl6) assert pl8.equals(Plane(p1, normal_vector=(0, 12, 0))) is False assert pl8.equals(pl8) assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12))) assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12 * sqrt(3)))) # issue 8570 l2 = Line3D( Point3D( S(50000004459633) / 5000000000000, -S(891926590718643) / 1000000000000000, S(231800966893633) / 100000000000000), Point3D( S(50000004459633) / 50000000000000, -S(222981647679771) / 250000000000000, S(231800966893633) / 100000000000000)) p2 = Plane( Point3D( S(402775636372767) / 100000000000000, -S(97224357654973) / 100000000000000, S(216793600814789) / 100000000000000), (-S('9.00000087501922'), -S('4.81170658872543e-13'), S('0.0'))) assert str([i.n(2) for i in p2.intersection(l2)]) == \ '[Point3D(4.0, -0.89, 2.3)]'
def test_S_sympify(): assert S(1)/2 == sympify(1)/2 assert (-2)**(S(1)/2) == sqrt(2)*I
def __pow__(self, other): from sympy.functions.elementary.miscellaneous import real_root if isinstance(other, Expr): if other is S.Infinity: if self.min.is_nonnegative: if self.max < 1: return S.Zero if self.min > 1: return S.Infinity return AccumBounds(0, oo) elif self.max.is_negative: if self.min > -1: return S.Zero if self.max < -1: return FiniteSet(-oo, oo) return AccumBounds(-oo, oo) else: if self.min > -1: if self.max < 1: return S.Zero return AccumBounds(0, oo) return AccumBounds(-oo, oo) if other is S.NegativeInfinity: return (1 / self)**oo if other.is_real and other.is_number: if other.is_zero: return S.One if other.is_Integer: if self.min.is_positive: return AccumBounds( Min(self.min ** other, self.max ** other), Max(self.min ** other, self.max ** other)) elif self.max.is_negative: return AccumBounds( Min(self.max ** other, self.min ** other), Max(self.max ** other, self.min ** other)) if other % 2 == 0: if other.is_negative: if self.min.is_zero: return AccumBounds(self.max**other, oo) if self.max.is_zero: return AccumBounds(self.min**other, oo) return AccumBounds(0, oo) return AccumBounds( S.Zero, Max(self.min**other, self.max**other)) else: if other.is_negative: if self.min.is_zero: return AccumBounds(self.max**other, oo) if self.max.is_zero: return AccumBounds(-oo, self.min**other) return AccumBounds(-oo, oo) return AccumBounds(self.min**other, self.max**other) num, den = other.as_numer_denom() if num == S(1): if den % 2 == 0: if S.Zero in self: if self.min.is_negative: return AccumBounds(0, real_root(self.max, den)) return AccumBounds(real_root(self.min, den), real_root(self.max, den)) num_pow = self**num return num_pow**(1 / den) return Pow(self, other, evaluate=False) return NotImplemented
def test_issue_4798_None(): assert S(None) is None
def test_settings(): raises(TypeError, lambda: sstr(S(4), method="garbage"))
def laplace(f): return f.diff(x, 2) + f.diff(y, 2) r = sqrt(x**2 + y**2) r_w = sqrt((x - x_w)**2 + (y - y_w)**2) r_p_squared = (x - x_p)**2 + (y - y_p)**2 theta = atan(y / x) u = r**(pi / omega) * sin(theta * pi / omega) + atan( alpha_w * (r_w - r0)) + exp(-alpha_p * r_p_squared) + exp(-(1 + y) / epsilon) params = { omega: 3 * pi / 2, x_w: 0, y_w: -S(3) / 4, r0: S(3) / 4, alpha_p: 1000, alpha_w: 200, x_p: sqrt(5) / 4, y_p: -S(1) / 4, epsilon: S(1) / 100, } u = u.subs(params) lhs = laplace(u) print "lhs, as a formula:" pprint(lhs) print print "-" * 60 print "lhs, as C code:"
def test_differentiation(): from sympy.functions.special.tensor_functions import KroneckerDelta i, j, k, l = symbols('i j k l', cls=Idx) a = symbols('a') m, n = symbols("m, n", integer=True, finite=True) assert m.is_real h, L = symbols('h L', cls=IndexedBase) hi, hj = h[i], h[j] expr = hi assert expr.diff(hj) == KroneckerDelta(i, j) assert expr.diff(hi) == KroneckerDelta(i, i) expr = S(2) * hi assert expr.diff(hj) == S(2) * KroneckerDelta(i, j) assert expr.diff(hi) == S(2) * KroneckerDelta(i, i) assert expr.diff(a) == S.Zero assert Sum(expr, (i, -oo, oo)).diff(hj) == Sum(2 * KroneckerDelta(i, j), (i, -oo, oo)) assert Sum(expr.diff(hj), (i, -oo, oo)) == Sum(2 * KroneckerDelta(i, j), (i, -oo, oo)) assert Sum(expr, (i, -oo, oo)).diff(hj).doit() == 2 assert Sum(expr.diff(hi), (i, -oo, oo)).doit() == Sum(2, (i, -oo, oo)).doit() assert Sum(expr, (i, -oo, oo)).diff(hi).doit() == oo expr = a * hj * hj / S(2) assert expr.diff(hi) == a * h[j] * KroneckerDelta(i, j) assert expr.diff(a) == hj * hj / S(2) assert expr.diff(a, 2) == S.Zero assert Sum(expr, (i, -oo, oo)).diff(hi) == Sum(a * KroneckerDelta(i, j) * h[j], (i, -oo, oo)) assert Sum(expr.diff(hi), (i, -oo, oo)) == Sum(a * KroneckerDelta(i, j) * h[j], (i, -oo, oo)) assert Sum(expr, (i, -oo, oo)).diff(hi).doit() == a * h[j] assert Sum(expr, (j, -oo, oo)).diff(hi) == Sum(a * KroneckerDelta(i, j) * h[j], (j, -oo, oo)) assert Sum(expr.diff(hi), (j, -oo, oo)) == Sum(a * KroneckerDelta(i, j) * h[j], (j, -oo, oo)) assert Sum(expr, (j, -oo, oo)).diff(hi).doit() == a * h[i] expr = a * sin(hj * hj) assert expr.diff(hi) == 2 * a * cos(hj * hj) * hj * KroneckerDelta(i, j) assert expr.diff(hj) == 2 * a * cos(hj * hj) * hj expr = a * L[i, j] * h[j] assert expr.diff(hi) == a * L[i, j] * KroneckerDelta(i, j) assert expr.diff(hj) == a * L[i, j] assert expr.diff(L[i, j]) == a * h[j] assert expr.diff( L[k, l]) == a * KroneckerDelta(i, k) * KroneckerDelta(j, l) * h[j] assert expr.diff(L[i, l]) == a * KroneckerDelta(j, l) * h[j] assert Sum(expr, (j, -oo, oo)).diff(L[k, l]) == Sum( a * KroneckerDelta(i, k) * KroneckerDelta(j, l) * h[j], (j, -oo, oo)) assert Sum(expr, (j, -oo, oo)).diff( L[k, l]).doit() == a * KroneckerDelta(i, k) * h[l] assert h[m].diff(h[m]) == 1 assert h[m].diff(h[n]) == KroneckerDelta(m, n) assert Sum(a * h[m], (m, -oo, oo)).diff(h[n]) == Sum(a * KroneckerDelta(m, n), (m, -oo, oo)) assert Sum(a * h[m], (m, -oo, oo)).diff(h[n]).doit() == a assert Sum(a * h[m], (n, -oo, oo)).diff(h[n]) == Sum(a * KroneckerDelta(m, n), (n, -oo, oo)) assert Sum(a * h[m], (m, -oo, oo)).diff(h[m]).doit() == oo * a
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import rootof x = Symbol("x") eq = x**Rational(4, 3) + 4*x**(S.One/3)/3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0/3)*x**(S.One/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**Rational(4, 3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big*x), Float_big*x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 6342 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solveset(f.subs(x, -0.139))) # issue 6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 7122 eq = cos(3*x**4 + y)*rootof(x**5 + 3*x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)' # issue 10933 for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (S.Half, Float(.5))) for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d, dkeys=True) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (Float(.5), Float(.5))) d = [S.Half] n = nfloat(d) assert type(n) is list assert _aresame(n[0], Float(.5)) assert _aresame(nfloat(Eq(x, S.Half)).rhs, Float(.5)) assert _aresame(nfloat(S(True)), S(True)) assert _aresame(nfloat(Tuple(S.Half))[0], Float(.5)) assert nfloat(Eq((3 - I)**2/2 + I, 0)) == S.false # pass along kwargs assert nfloat([{S.Half: x}], dkeys=True) == [{Float(0.5): x}] # Issue 17706 A = MutableMatrix([[1, 2], [3, 4]]) B = MutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B) A = ImmutableMatrix([[1, 2], [3, 4]]) B = ImmutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B)
def test_hyperrep(): from sympy.functions.special.hyper import (HyperRep, HyperRep_atanh, HyperRep_power1, HyperRep_power2, HyperRep_log1, HyperRep_asin1, HyperRep_asin2, HyperRep_sqrts1, HyperRep_sqrts2, HyperRep_log2, HyperRep_cosasin, HyperRep_sinasin) # First test the base class works. from sympy import Piecewise, exp_polar a, b, c, d, z = symbols('a b c d z') class myrep(HyperRep): @classmethod def _expr_small(cls, x): return a @classmethod def _expr_small_minus(cls, x): return b @classmethod def _expr_big(cls, x, n): return c*n @classmethod def _expr_big_minus(cls, x, n): return d*n assert myrep(z).rewrite('nonrep') == Piecewise((0, abs(z) > 1), (a, True)) assert myrep(exp_polar(I*pi)*z).rewrite('nonrep') == \ Piecewise((0, abs(z) > 1), (b, True)) assert myrep(exp_polar(2*I*pi)*z).rewrite('nonrep') == \ Piecewise((c, abs(z) > 1), (a, True)) assert myrep(exp_polar(3*I*pi)*z).rewrite('nonrep') == \ Piecewise((d, abs(z) > 1), (b, True)) assert myrep(exp_polar(4*I*pi)*z).rewrite('nonrep') == \ Piecewise((2*c, abs(z) > 1), (a, True)) assert myrep(exp_polar(5*I*pi)*z).rewrite('nonrep') == \ Piecewise((2*d, abs(z) > 1), (b, True)) assert myrep(z).rewrite('nonrepsmall') == a assert myrep(exp_polar(I*pi)*z).rewrite('nonrepsmall') == b def t(func, hyp, z): """ Test that func is a valid representation of hyp. """ # First test that func agrees with hyp for small z if not tn(func.rewrite('nonrepsmall'), hyp, z, a=S(-1)/2, b=S(-1)/2, c=S(1)/2, d=S(1)/2): return False # Next check that the two small representations agree. if not tn(func.rewrite('nonrepsmall').subs(z, exp_polar(I*pi)*z).replace(exp_polar, exp), func.subs(z, exp_polar(I*pi)*z).rewrite('nonrepsmall'), z, a=S(-1)/2, b=S(-1)/2, c=S(1)/2, d=S(1)/2): return False # Next check continuity along exp_polar(I*pi)*t expr = func.subs(z, exp_polar(I*pi)*z).rewrite('nonrep') if abs(expr.subs(z, 1 + 1e-15).n() - expr.subs(z, 1 - 1e-15).n()) > 1e-10: return False # Finally check continuity of the big reps. def dosubs(func, a, b): rv = func.subs(z, exp_polar(a)*z).rewrite('nonrep') return rv.subs(z, exp_polar(b)*z).replace(exp_polar, exp) for n in [0, 1, 2, 3, 4, -1, -2, -3, -4]: expr1 = dosubs(func, 2*I*pi*n, I*pi/2) expr2 = dosubs(func, 2*I*pi*n + I*pi, -I*pi/2) if not tn(expr1, expr2, z): return False expr1 = dosubs(func, 2*I*pi*(n + 1), -I*pi/2) expr2 = dosubs(func, 2*I*pi*n + I*pi, I*pi/2) if not tn(expr1, expr2, z): return False return True # Now test the various representatives. a = S(1)/3 assert t(HyperRep_atanh(z), hyper([S(1)/2, 1], [S(3)/2], z), z) assert t(HyperRep_power1(a, z), hyper([-a], [], z), z) assert t(HyperRep_power2(a, z), hyper([a, a - S(1)/2], [2*a], z), z) assert t(HyperRep_log1(z), -z*hyper([1, 1], [2], z), z) assert t(HyperRep_asin1(z), hyper([S(1)/2, S(1)/2], [S(3)/2], z), z) assert t(HyperRep_asin2(z), hyper([1, 1], [S(3)/2], z), z) assert t(HyperRep_sqrts1(a, z), hyper([-a, S(1)/2 - a], [S(1)/2], z), z) assert t(HyperRep_sqrts2(a, z), -2*z/(2*a + 1)*hyper([-a - S(1)/2, -a], [S(1)/2], z).diff(z), z) assert t(HyperRep_log2(z), -z/4*hyper([S(3)/2, 1, 1], [2, 2], z), z) assert t(HyperRep_cosasin(a, z), hyper([-a, a], [S(1)/2], z), z) assert t(HyperRep_sinasin(a, z), 2*a*z*hyper([1-a, 1+a], [S(3)/2], z), z)