def test_systematic_basic(): def s(diofant_object, numpy_array): diofant_object + numpy_array numpy_array + diofant_object diofant_object - numpy_array numpy_array - diofant_object diofant_object * numpy_array numpy_array * diofant_object diofant_object / numpy_array numpy_array / diofant_object diofant_object**numpy_array numpy_array**diofant_object x = Symbol("x") y = Symbol("y") diofant_objs = [ Rational(2, 3), Float("1.3"), x, y, pow(x, y) * y, Integer(5), Float(5.5), ] numpy_objs = [ numpy.array([1]), numpy.array([3, 8, -1]), numpy.array([x, x**2, Integer(5)]), numpy.array([x / y * sin(y), 5, Integer(5)]), ] for x in diofant_objs: for y in numpy_objs: s(x, y)
def test_sympyissue_3728(): assert (Rational(1, 2)*numpy.array([2*x, 0]) == numpy.array([x, 0])).all() assert (Rational(1, 2) + numpy.array( [2*x, 0]) == numpy.array([2*x + Rational(1, 2), Rational(1, 2)])).all() assert (Float("0.5")*numpy.array([2*x, 0]) == numpy.array([Float("1.0")*x, 0])).all() assert (Float("0.5") + numpy.array( [2*x, 0]) == numpy.array([2*x + Float("0.5"), Float("0.5")])).all()
def test_diofantissue_730(): e = RootOf(x**3 + 10 * x**2 + 1, 2) assert e.is_real is False assert e.is_imaginary is False assert e.evalf(3) == Float('0.00498962', dps=3) + I * Float('0.31604', dps=3) assert e.conjugate().conjugate() == e
def test_sympifyit(): x = Symbol('x') y = Symbol('y') @_sympifyit('b', NotImplemented) def add(a, b): return a + b assert add(x, 1) == x + 1 assert add(x, 0.5) == x + Float('0.5') assert add(x, y) == x + y assert add(x, '1') == NotImplemented @_sympifyit('b') def add_raises(a, b): return a + b assert add_raises(x, 1) == x + 1 assert add_raises(x, 0.5) == x + Float('0.5') assert add_raises(x, y) == x + y pytest.raises(SympifyError, lambda: add_raises(x, '1')) with pytest.raises(LookupError): @_sympifyit('x', NotImplemented) def spam(): return
def test_systematic_basic(): def s(diofant_object, numpy_array): # pylint: disable=pointless-statement diofant_object + numpy_array numpy_array + diofant_object diofant_object - numpy_array numpy_array - diofant_object diofant_object * numpy_array numpy_array * diofant_object diofant_object / numpy_array numpy_array / diofant_object diofant_object**numpy_array numpy_array**diofant_object x = Symbol('x') y = Symbol('y') diofant_objs = [ Rational(2, 3), Float('1.3'), x, y, pow(x, y) * y, Integer(5), Float(5.5), ] numpy_objs = [ numpy.array([1]), numpy.array([3, 8, -1]), numpy.array([x, x**2, Integer(5)]), numpy.array([x / y * sin(y), 5, Integer(5)]), ] for x in diofant_objs: for y in numpy_objs: s(x, y)
def test_Float(): sT(Float('1.23', dps=3), "Float('1.22998', dps=3)") sT(Float('1.23456789', dps=9), "Float('1.23456788994', dps=9)") sT(Float('1.234567890123456789', dps=19), "Float('1.234567890123456789013', dps=19)") sT(Float('0.60038617995049726', 15), "Float('0.60038617995049726', dps=15)")
def test_nsimplify(): 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)) == Rational(1, 2) - sqrt(3)*I/2 assert nsimplify(sin(3*pi/5, evaluate=False)) == sqrt(sqrt(5)/8 + Rational(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)) == Rational(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) == cbrt(2) 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).evalf(), rational=True) == Rational(109861228866811, 100000000000000) assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).evalf(3), [pi, log(2)]) == \ -pi/4 - log(2) + Rational(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) == 33 assert nsimplify(33.33, tolerance=10, rational=True) == 30 assert nsimplify(37.76, tolerance=10, rational=True) == 40 assert nsimplify(-203.1) == -Rational(2031, 10) assert nsimplify(+.2, tolerance=0) == Rational(+1, 5) assert nsimplify(-.2, tolerance=0) == Rational(-1, 5) assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == -Rational(1111, 5000) # issue sympy/sympy#7211, PR sympy/sympy#4112 assert nsimplify(Float(2e-8)) == Rational(1, 50000000) # issue sympy/sympy#7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue sympy/sympy#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(Sum(1/n**2, (n, 1, oo)), [pi]) == pi**2/6
def test_issue_3728(): assert (Rational(1, 2) * array([2 * x, 0]) == array([x, 0])).all() assert (Rational(1, 2) + array([2 * x, 0]) == array( [2 * x + Rational(1, 2), Rational(1, 2)])).all() assert (Float("0.5") * array([2 * x, 0]) == array([Float("1.0") * x, 0])).all() assert (Float("0.5") + array([2 * x, 0]) == array( [2 * x + Float("0.5"), Float("0.5")])).all()
def test_sympyissue_11678(): p = Matrix([[1./2, 1./4, 1./4], [1./2, 0, 1./2], [1./4, 0, 3./4]]) e = (p**x).applyfunc(lambda i: limit(i, x, oo)) assert e == Matrix([[Float('0.36363636363636359', prec=15), Float('0.090909090909090898', prec=15), Float('0.54545454545454541', prec=15)]]*3)
def test_RealField_from_diofant(): assert RR.convert(Integer(0)) == RR.dtype(0) assert RR.convert(Float(0.0)) == RR.dtype(0.0) assert RR.convert(Integer(1)) == RR.dtype(1) assert RR.convert(Float(1.0)) == RR.dtype(1.0) assert RR.convert(sin(1)) == RR.dtype(sin(1).evalf()) assert RR.convert(oo) == RR("+inf") assert RR.convert(-oo) == RR("-inf") pytest.raises(CoercionFailed, lambda: RR.convert(x))
def test_Number(): assert precedence(Integer(0)) == PRECEDENCE['Atom'] assert precedence(Integer(1)) == PRECEDENCE['Atom'] assert precedence(Integer(-1)) == PRECEDENCE['Add'] assert precedence(Integer(10)) == PRECEDENCE['Atom'] assert precedence(Rational(5, 2)) == PRECEDENCE['Mul'] assert precedence(Rational(-5, 2)) == PRECEDENCE['Add'] assert precedence(Float(5)) == PRECEDENCE['Atom'] assert precedence(Float(-5)) == PRECEDENCE['Add'] assert precedence(oo) == PRECEDENCE['Atom'] assert precedence(-oo) == PRECEDENCE['Add']
def test_RR_Float(): f1 = Float("1.01") f2 = Float("1.0000000000000000000001") assert f1._prec == 53 assert f2._prec == 80 assert RR(f1) - 1 > 1e-50 assert RR(f2) - 1 < 1e-50 # RR's precision is lower than f2's RR2 = RealField(prec=f2._prec) assert RR2(f1) - 1 > 1e-50 assert RR2(f2) - 1 > 1e-50 # RR's precision is equal to f2's
def test_RootSum_evalf(): rs = RootSum(x**2 + 1, Lambda(x, exp(x))) assert rs.evalf(20, chop=True).epsilon_eq( Float("1.0806046117362794348", 20), Float("1e-20")) is true assert rs.evalf(15, chop=True).epsilon_eq(Float("1.08060461173628", 15), Float("1e-15")) is true rs = RootSum(x**2 + a, Lambda(x, exp(x)), x) assert rs.evalf() == rs
def test_function_comparable(): assert sin(x).is_comparable is False assert cos(x).is_comparable is False assert sin(Float('0.1')).is_comparable is True assert cos(Float('0.1')).is_comparable is True assert sin(E).is_comparable is True assert cos(E).is_comparable is True assert sin(Rational(1, 3)).is_comparable is True assert cos(Rational(1, 3)).is_comparable is True
def test_nsimplify(): 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) + Rational(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) == -Rational(2031, 10) assert nsimplify(.2, tolerance=0) == S.One/5 assert nsimplify(-.2, tolerance=0) == -S.One/5 assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == -Rational(1111, 5000) # issue 7211, PR 4112 assert nsimplify(Float(2e-8)) == Rational(1, 50000000) # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0
def test_Float(): # NOTE prec is the whole number of decimal digits assert str(Float('1.23', prec=1 + 2)) == '1.23' assert str(Float('1.23456789', prec=1 + 8)) == '1.23456789' assert str(Float('1.234567890123456789', prec=1 + 18)) == '1.234567890123456789' assert str(pi.evalf(1 + 2)) == '3.14' assert str(pi.evalf(1 + 14)) == '3.14159265358979' assert str(pi.evalf(1 + 64)) == ('3.141592653589793238462643383279' '5028841971693993751058209749445923') assert str(pi.round(-1)) == '0.' assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88'
def test_lambertw(): k = Symbol('k') assert LambertW(x, 0) == LambertW(x) assert LambertW(x, 0, evaluate=False) != LambertW(x) assert LambertW(0) == 0 assert LambertW(E) == 1 assert LambertW(-1 / E) == -1 assert LambertW(-log(2) / 2) == -log(2) assert LambertW(oo) == oo assert LambertW(0, 1) == -oo assert LambertW(0, 42) == -oo assert LambertW(-pi / 2, -1) == -I * pi / 2 assert LambertW(-1 / E, -1) == -1 assert LambertW(-2 * exp(-2), -1) == -2 assert LambertW( x**2).diff(x) == 2 * LambertW(x**2) / x / (1 + LambertW(x**2)) assert LambertW(x, k).diff(x) == LambertW(x, k) / x / (1 + LambertW(x, k)) pytest.raises(ArgumentIndexError, lambda: LambertW(x).fdiff(3)) pytest.raises(ArgumentIndexError, lambda: LambertW(x, k).fdiff(3)) assert LambertW(sqrt(2)).evalf(30).epsilon_eq( Float('0.701338383413663009202120278965', 30), 1e-29) assert re(LambertW(2, -1)).evalf().epsilon_eq(Float('-0.834310366631110')) assert LambertW(-1).is_extended_real is False # issue sympy/sympy#5215 assert LambertW(2, evaluate=False).is_extended_real p = Symbol('p', positive=True) assert LambertW(p, evaluate=False).is_extended_real assert LambertW(p - 1, evaluate=False).is_extended_real is None assert LambertW(-p - 2 / E, evaluate=False).is_extended_real is False assert LambertW(Rational(1, 2), -1, evaluate=False).is_extended_real is False assert LambertW(Rational(-1, 10), -1, evaluate=False).is_extended_real assert LambertW(0, evaluate=False).is_algebraic na = Symbol('na', nonzero=True, algebraic=True) assert LambertW(na).is_algebraic is False assert LambertW(x, -1).is_extended_real is None assert LambertW(x, 2).is_extended_real is None # See sympy/sympy#7259: assert LambertW(x).series(x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + \ 125*x**5/24 + O(x**6) assert LambertW(x).series(x, n=0) == O(1, x) assert LambertW(x, k).series(x, x0=1, n=1) == (LambertW(1, k) + O(x - 1, (x, 1)))
def test_RR_Float(): f1 = Float("1.01", 15) f2 = Float("1.0000000000000000000001") assert f1._prec == 53 assert f2._prec == 80 assert RR(f1) - 1 > 1e-50 assert RR(f2) - 1 < 1e-50 # RR's precision is lower than f2's RR2 = RealField(prec=f2._prec) assert RR2(f1) - 1 > 1e-50 assert RR2(f2) - 1 > 1e-50 # RR's precision is equal to f2's a = RR(2.1) assert a.numerator == a and a.denominator == 1
def test_reflect(): b = Symbol('b') m = Symbol('m') l = Line((0, b), slope=m) pytest.raises(ValueError, lambda: Line((0, b))) p = Point(x, y) r = p.reflect(l) dp = l.perpendicular_segment(p).length dr = l.perpendicular_segment(r).length assert verify_numerically(dp, dr) t = Triangle((0, 0), (1, 0), (2, 3)) assert t.area == -t.reflect(l).area e = Ellipse((1, 0), 1, 2) assert e.area == -e.reflect(Line((1, 0), slope=0)).area assert e.area == -e.reflect(Line((1, 0), slope=oo)).area pytest.raises(NotImplementedError, lambda: e.reflect(Line( (1, 0), slope=m))) pytest.raises( NotImplementedError, lambda: Ellipse(Point(0, 0), x, 1).reflect( Line(Point(0, 1), Point(1, 0)))) assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=oo)) \ == Triangle(Point(5, 0), Point(4, 0), Point(4, 2)) assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=oo)) \ == Triangle(Point(-1, 0), Point(-2, 0), Point(-2, 2)) assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=0)) \ == Triangle(Point(1, 6), Point(2, 6), Point(2, 4)) assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=0)) \ == Triangle(Point(1, 0), Point(2, 0), Point(2, -2)) # test entity overrides c = Circle((x, y), 3) cr = c.reflect(l) assert cr == Circle(r, -3) assert c.area == -cr.area pent = RegularPolygon((1, 2), 1, 5) l = Line((0, pi), slope=sqrt(2)) rpent = pent.reflect(l) assert rpent.center == pent.center.reflect(l) assert [w.evalf(3) for w in rpent.vertices] == \ [Point(Float('-0.585815', dps=3), Float('4.27051', dps=3)), Point(Float('-1.69409', dps=3), Float('4.66211', dps=3)), Point(Float('-2.40918', dps=3), Float('3.72949', dps=3)), Point(Float('-1.74292', dps=3), Float('2.76123', dps=3)), Point(Float('-0.615967', dps=3), Float('3.0957', dps=3))] assert pent.area.equals(-rpent.area)
def test_precision(): f1 = Float("1.01") f2 = Float("1.0000000000000000000001") for x in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300, f1, f2]: result = construct_domain([x]) y = float(result[1][0]) assert abs(x - y)/x < 1e-14 # Test relative accuracy result = construct_domain([f1]) assert result[1][0] - 1 > 1e-50 result = construct_domain([f2]) assert result[1][0] - 1 > 1e-50
def test_sympyissue_9296(): q = symbols('q_1:5') dq = symbols('dq_1:5') a = (dq[0]*(0.1*(0.01*sin(q[2]) + 0.01*sin(q[1] + q[2]))*cos(q[1] + q[2]) + 0.01*(0.1*sin(q[2]) + 0.1*sin(q[1] + q[2]))*cos(q[1] + q[2]) + 0.05*(-0.05*cos(q[1]) - 0.025)*sin(q[1]) + 0.01*(-0.1*cos(q[2]) - 0.1*cos(q[1] + q[2]))*sin(q[1] + q[2]) + 0.1*(-0.01*cos(q[2]) - 0.01*cos(q[1] + q[2]))*sin(q[1] + q[2]) + 0.0025*sin(q[1])*cos(q[1]) - 0.00125*sin(q[1]))) b = dq[1]*(-0.0025*sin(q[1]) + 0.002*sin(q[2])*cos(q[1] + q[2]) - 0.002*sin(q[1] + q[2])*cos(q[2])) r = simplify(a + b).replace(lambda x: x.is_Float and abs(x) < 1e-15, lambda x: 0) assert r == (-Float('0.0045000000000000005', dps=15)*dq[0]*sin(q[1]) - Float('0.0045000000000000005', dps=15)*dq[1]*sin(q[1]))
def test_sympyissue_9398(): assert cancel(1e-14) != 0 assert cancel(1e-14 * I) != 0 assert simplify(1e-14) != 0 assert simplify(1e-14 * I) != 0 assert (I * Number(1.) * Number(10)**Number(-14)).simplify() != 0 assert cancel(1e-20) != 0 assert cancel(1e-20 * I) != 0 assert simplify(1e-20) != 0 assert simplify(1e-20 * I) != 0 assert cancel(1e-100) != 0 assert cancel(1e-100 * I) != 0 assert simplify(1e-100) != 0 assert simplify(1e-100 * I) != 0 f = Float("1e-1000", 15) assert cancel(f) != 0 assert cancel(f * I) != 0 assert simplify(f) != 0 assert simplify(f * I) != 0
def test_mathml_core(): mml_1 = mp._print(1 + x) assert mml_1.nodeName == 'apply' nodes = mml_1.childNodes assert len(nodes) == 3 assert nodes[0].nodeName == 'plus' assert nodes[0].hasChildNodes() is False assert nodes[0].nodeValue is None assert nodes[1].nodeName in ['cn', 'ci'] if nodes[1].nodeName == 'cn': assert nodes[1].childNodes[0].nodeValue == '1' assert nodes[2].childNodes[0].nodeValue == 'x' else: assert nodes[1].childNodes[0].nodeValue == 'x' assert nodes[2].childNodes[0].nodeValue == '1' mml_2 = mp._print(x**2) assert mml_2.nodeName == 'apply' nodes = mml_2.childNodes assert nodes[1].childNodes[0].nodeValue == 'x' assert nodes[2].childNodes[0].nodeValue == '2' mml_3 = mp._print(2 * x) assert mml_3.nodeName == 'apply' nodes = mml_3.childNodes assert nodes[0].nodeName == 'times' assert nodes[1].childNodes[0].nodeValue == '2' assert nodes[2].childNodes[0].nodeValue == 'x' mml = mp._print(Float(1.0, 2) * x) assert mml.nodeName == 'apply' nodes = mml.childNodes assert nodes[0].nodeName == 'times' assert nodes[1].childNodes[0].nodeValue == '1.0' assert nodes[2].childNodes[0].nodeValue == 'x'
def test_reduce_poly_inequalities_complex_relational(): assert reduce_rational_inequalities([[Eq(x**2, 0)]], x, relational=True) == Eq(x, 0) assert reduce_rational_inequalities([[x**2 <= 0]], x, relational=True) == Eq(x, 0) assert reduce_rational_inequalities([[x**2 < 0]], x, relational=True) is false assert reduce_rational_inequalities([[x**2 >= 0]], x, relational=True) is true assert reduce_rational_inequalities( [[x**2 > 0]], x, relational=True) == \ (x < 0) | (Integer(0) < x) assert reduce_rational_inequalities( [[Ne(x**2, 0)]], x, relational=True) == \ (x < 0) | (Integer(0) < x) for one in (Integer(1), Float(1.0)): assert reduce_rational_inequalities( [[Eq(x**2, one)]], x, relational=True) == \ Eq(x, -one) | Eq(x, one) assert reduce_rational_inequalities( [[x**2 <= one]], x, relational=True) == \ (-one <= x) & (x <= one) assert reduce_rational_inequalities( [[x**2 < one]], x, relational=True) == \ (-one < x) & (x < one) assert reduce_rational_inequalities( [[x**2 >= one]], x, relational=True) == \ (one <= x) | (x <= -one) assert reduce_rational_inequalities( [[x**2 > one]], x, relational=True) == \ (one < x) | (x < -one) assert reduce_rational_inequalities( [[Ne(x**2, one)]], x, relational=True) == \ (x < -one) | ((-one < x) & (x < one)) | (one < x)
def test_Mul(): assert str(x / y) == "x/y" assert str(y / x) == "y/x" assert str(x / y / z) == "x/(y*z)" assert str((x + 1) / (y + 2)) == "(x + 1)/(y + 2)" assert str(2 * x / 3) == '2*x/3' assert str(-2 * x / 3) == '-2*x/3' assert str(-1.0 * x) == '-1.0*x' assert str(1.0 * x) == '1.0*x' class CustomClass1(Expr): is_commutative = True class CustomClass2(Expr): is_commutative = True cc1 = CustomClass1() cc2 = CustomClass2() assert str(Rational(2) * cc1) == '2*CustomClass1()' assert str(cc1 * Rational(2)) == '2*CustomClass1()' assert str(cc1 * Float("1.5")) == '1.5*CustomClass1()' assert str(cc2 * Rational(2)) == '2*CustomClass2()' assert str(cc2 * Rational(2) * cc1) == '2*CustomClass1()*CustomClass2()' assert str(cc1 * Rational(2) * cc2) == '2*CustomClass1()*CustomClass2()' assert str(Mul(1, 1, evaluate=False)) == '1*1'
def test_diofant_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3 * I, 'exp(x)': exp(x), '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, 'Float(Integer(3).evalf(3))': 3.00, 'factorint(12, visual=True)': Mul(Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir=1)': Limit(sin(x), x, 0, dir=1), } for text, result in inputs.items(): assert parse_expr(text) == result
def test_sympyissue_7457(): pickle.loads(pickle.dumps(Point(1.1, 2.1).evalf())) # not raises a = Float('1.2') b = pickle.loads(pickle.dumps(a)) b.evalf(strict=False) # not raises assert a == b
def test_RootOf_algebraic(): e = RootOf(sqrt(2) * x**4 + sqrt(2) * x**3 - I * x + sqrt(2), x, 0) assert e.interval.as_tuple() == ((Rational(-201, 100), 0), (Rational(-201, 200), Rational(201, 200))) assert e.evalf(7) == Float('-1.22731258', dps=7) + I * Float('0.6094138324', dps=7) t = RootOf(x**5 + 4 * x + 2, 0) e = RootOf(x**4 + t * x + 1, 0) assert e.interval.as_tuple() == ((Rational(-201, 200), Rational(-201, 200)), (Rational(-201, 400), Rational(-201, 400))) assert e.evalf(7) == Float('-0.7123350278', dps=7) - I * Float('0.8248345032', dps=7)
def test_diofant_lambda(): mpmath.mp.dps = 50 sin02 = mpmath.mpf('0.19866933079506121545941262711838975037020672954020') f = lambdify(x, sin(x), 'diofant') assert f(x) == sin(x) prec = 1e-15 assert -prec < f(Rational(1, 5)).evalf() - Float(str(sin02)) < prec
def test_nfloat(): x = Symbol("x") eq = x**Rational(4, 3) + 4 * cbrt(x) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * cbrt(x)) 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({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue sympy/sympy#6342 lamda = Symbol('lamda') f = x * lamda + lamda**3 * (x / 2 + Rational(1, 2)) + lamda**2 + Rational( 1, 4) assert not any(a[lamda].free_symbols for a in solve(f.subs({x: -0.139}))) # issue sympy/sympy#6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue sympy/sympy#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)'