def _expr_big(cls, z, n): if n.is_even: return (n - Rational(1, 2)) * pi * I + log( sqrt(z) / 2) + I * asin(1 / sqrt(z)) else: return (n - Rational(1, 2)) * pi * I + log( sqrt(z) / 2) - I * asin(1 / sqrt(z))
def eval_rational(self, tol): """ Returns a Rational approximation to ``self`` with the tolerance ``tol``. This method uses bisection, which is very robust and it will always converge. The returned Rational instance will be at most 'tol' from the exact root. The following example first obtains Rational approximation to 1e-7 accuracy for all roots of the 4-th order Legendre polynomial, and then evaluates it to 5 decimal digits (so all digits will be correct including rounding): >>> from diofant import Rational, legendre_poly, Symbol >>> x = Symbol("x") >>> p = legendre_poly(4, x, polys=True) >>> roots = [r.eval_rational(Rational(1, 10)**7) for r in p.real_roots()] >>> roots = [str(r.n(5)) for r in roots] >>> roots ['-0.86114', '-0.33998', '0.33998', '0.86114'] """ if not self.is_extended_real: raise NotImplementedError( "eval_rational() only works for real polynomials so far") func = lambdify(self.poly.gen, self.expr) interval = self._get_interval() a = Rational(str(interval.a)) b = Rational(str(interval.b)) return bisect(func, a, b, tol)
def test_pow_as_base_exp(): assert (oo**(2 - x)).as_base_exp() == (oo, 2 - x) assert (oo**(x - 2)).as_base_exp() == (oo, x - 2) p = Rational(1, 2)**x assert p.base, p.exp == p.as_base_exp() == (Integer(2), -x) # issue sympy/sympy#8344: assert Pow(1, 2, evaluate=False).as_base_exp() == (Integer(1), Integer(2))
def test_pow_as_base_exp(): assert (oo**(2 - x)).as_base_exp() == (oo, 2 - x) assert (oo**(x - 2)).as_base_exp() == (oo, x - 2) p = Rational(1, 2)**x assert p.base, p.exp == p.as_base_exp() == (2, -x) # issue sympy/sympy#8344: assert Pow(1, 2, evaluate=False).as_base_exp() == (1, 2)
def test_root(): from diofant.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_sympyissue_7638(): f = pi/log(sqrt(2)) assert ((1 + I)**(I*f/2))**0.3 == (1 + I)**(0.15*I*f) # if 1/3 -> 1.0/3 this should fail since it cannot be shown that the # sign will be +/-1; for the previous "small arg" case, it didn't matter # that this could not be proved assert (1 + I)**(4*I*f) == ((1 + I)**(12*I*f))**Rational(1, 3) assert (((1 + I)**(I*(1 + 7*f)))**Rational(1, 3)).exp == Rational(1, 3) r = symbols('r', extended_real=True) assert sqrt(r**2) == abs(r) assert cbrt(r**3) != r assert sqrt(Pow(2*I, 5*S.Half)) != (2*I)**(5/Integer(4)) p = symbols('p', positive=True) assert cbrt(p**2) == p**(2/Integer(3)) assert NS(((0.2 + 0.7*I)**(0.7 + 1.0*I))**(0.5 - 0.1*I), 1) == '0.4 + 0.2*I' assert sqrt(1/(1 + I)) == sqrt((1 - I)/2) # or 1/sqrt(1 + I) e = 1/(1 - sqrt(2)) assert sqrt(e) == I/sqrt(-1 + sqrt(2)) assert e**-S.Half == -I*sqrt(-1 + sqrt(2)) assert sqrt((cos(1)**2 + sin(1)**2 - 1)**(3 + I)).exp == S.Half assert sqrt(r**(4/Integer(3))) != r**(2/Integer(3)) assert sqrt((p + I)**(4/Integer(3))) == (p + I)**(2/Integer(3)) assert sqrt((p - p**2*I)**2) == p - p**2*I assert sqrt((p + r*I)**2) != p + r*I e = (1 + I/5) assert sqrt(e**5) == e**(5*S.Half) assert sqrt(e**6) == e**3 assert sqrt((1 + I*r)**6) != (1 + I*r)**3
def test_root(): n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == cbrt(2) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(4, 2, 1, evaluate=False) == -Pow(4, Rational(1, 2), evaluate=False) assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == cbrt(x) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_mix_number_mult_symbols(): assert mcode(3 * x) == "3*x" assert mcode(pi * x) == "pi*x" assert mcode(3 / x) == "3./x" assert mcode(pi / x) == "pi./x" assert mcode(x / 3) == "x/3" assert mcode(x / pi) == "x/pi" assert mcode(x * y) == "x.*y" assert mcode(3 * x * y) == "3*x.*y" assert mcode(3 * pi * x * y) == "3*pi*x.*y" assert mcode(x / y) == "x./y" assert mcode(3 * x / y) == "3*x./y" assert mcode(x * y / z) == "x.*y./z" assert mcode(x / y * z) == "x.*z./y" assert mcode(1 / x / y) == "1./(x.*y)" assert mcode(2 * pi * x / y / z) == "2*pi*x./(y.*z)" assert mcode(3 * pi / x) == "3*pi./x" assert mcode(Rational(3, 5)) == "3/5" assert mcode(Rational(3, 5) * x) == "3*x/5" assert mcode(x / y / z) == "x./(y.*z)" assert mcode((x + y) / z) == "(x + y)./z" assert mcode((x + y) / (z + x)) == "(x + y)./(x + z)" assert mcode((x + y) / EulerGamma) == "(x + y)/0.5772156649015329" assert mcode(x / 3 / pi) == "x/(3*pi)" assert mcode(Rational(3, 5) * x * y / pi) == "3*x.*y/(5*pi)"
def test_sympyissue_6208(): assert sqrt(33**(9*I/10)) == -33**(9*I/20) assert root((6*I)**(2*I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6*I)**(I/3), 3).as_base_exp()[1] == I/9 assert sqrt(exp(3*I)) == exp(3*I/2) assert sqrt(-sqrt(3)*(1 + 2*I)) == sqrt(sqrt(3))*sqrt(-1 - 2*I) assert sqrt(exp(5*I)) == -exp(5*I/2) assert root(exp(5*I), 3).exp == Rational(1, 3)
def test_as_explicit(): A = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(A, 0).as_explicit() == ImmutableMatrix(Identity(2)) assert MatPow(A, 1).as_explicit() == A assert MatPow(A, 2).as_explicit() == A**2 assert MatPow(A, -1).as_explicit() == A.inv() assert MatPow(A, -2).as_explicit() == (A.inv())**2 # less expensive than testing on a 2x2 A = ImmutableMatrix([4]) assert MatPow(A, Rational(1, 2)).as_explicit() == A**Rational(1, 2)
def test_Max(): from diofant.abc import x, y, z n = Symbol('n', negative=True) n_ = Symbol('n_', negative=True) nn = Symbol('nn', nonnegative=True) nn_ = Symbol('nn_', nonnegative=True) p = Symbol('p', positive=True) p_ = Symbol('p_', positive=True) np = Symbol('np', nonpositive=True) np_ = Symbol('np_', nonpositive=True) r = Symbol('r', extended_real=True) assert Max(5, 4) == 5 # lists pytest.raises(ValueError, lambda: Max()) assert Max(x, y) == Max(y, x) assert Max(x, y, z) == Max(z, y, x) assert Max(x, Max(y, z)) == Max(z, y, x) assert Max(x, Min(y, oo)) == Max(x, y) assert Max(n, -oo, n_, p, 2) == Max(p, 2) assert Max(n, -oo, n_, p) == p assert Max(2, x, p, n, -oo, S.NegativeInfinity, n_, p, 2) == Max(2, x, p) assert Max(0, x, 1, y) == Max(1, x, y) assert Max(r, r + 1, r - 1) == 1 + r assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000) assert Max(cos(x), sin(x)) == Max(sin(x), cos(x)) assert Max(cos(x), sin(x)).subs(x, 1) == sin(1) assert Max(cos(x), sin(x)).subs(x, Rational(1, 2)) == cos(Rational(1, 2)) pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs(x, I)) pytest.raises(ValueError, lambda: Max(I)) pytest.raises(ValueError, lambda: Max(I, x)) pytest.raises(ValueError, lambda: Max(S.ComplexInfinity, 1)) # interesting: # Max(n, -oo, n_, p, 2) == Max(p, 2) # True # Max(n, -oo, n_, p, 1000) == Max(p, 1000) # False assert Max(1, x).diff(x) == Heaviside(x - 1) assert Max(x, 1).diff(x) == Heaviside(x - 1) assert Max(x**2, 1 + x, 1).diff(x) == \ 2*x*Heaviside(x**2 - Max(1, x + 1)) \ + Heaviside(x - Max(1, x**2) + 1) a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True) # a and b are both real, Max(a, b) should be real assert Max(a, b).is_extended_real # issue sympy/sympy#7233 e = Max(0, x) assert e.evalf == e.n assert e.n().args == (0, x)
def test_issue_6208(): from diofant import root, Rational I = S.ImaginaryUnit assert sqrt(33**(9 * I / 10)) == -33**(9 * I / 20) assert root((6 * I)**(2 * I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6 * I)**(I / 3), 3).as_base_exp()[1] == I / 9 assert sqrt(exp(3 * I)) == exp(3 * I / 2) assert sqrt(-sqrt(3) * (1 + 2 * I)) == sqrt(sqrt(3)) * sqrt(-1 - 2 * I) assert sqrt(exp(5 * I)) == -exp(5 * I / 2) assert root(exp(5 * I), 3).exp == Rational(1, 3)
def eval(cls, z): if z is S.Zero: return pi / 2 elif z is S.Half: return 8 * pi**Rational(3, 2) / gamma(-Rational(1, 4))**2 elif z is S.One: return S.ComplexInfinity elif z is S.NegativeOne: return gamma(Rational(1, 4))**2 / (4 * sqrt(2 * pi)) elif z in (S.Infinity, S.NegativeInfinity, I * S.Infinity, I * S.NegativeInfinity, S.ComplexInfinity): return S.Zero
def _calc_bernoulli(n): s = 0 a = int(binomial(n + 3, n - 6)) for j in range(1, n // 6 + 1): s += a * bernoulli(n - 6 * j) # Avoid computing each binomial coefficient from scratch a *= _product(n - 6 - 6 * j + 1, n - 6 * j) a //= _product(6 * j + 4, 6 * j + 9) if n % 6 == 4: s = -Rational(n + 3, 6) - s else: s = Rational(n + 3, 3) - s return s / binomial(n + 3, n)
def test_Max(): n = Symbol('n', negative=True) n_ = Symbol('n_', negative=True) p = Symbol('p', positive=True) r = Symbol('r', extended_real=True) assert Max(5, 4) == 5 # lists pytest.raises(ValueError, lambda: Max()) assert Max(x, y) == Max(y, x) assert Max(x, y, z) == Max(z, y, x) assert Max(x, Max(y, z)) == Max(z, y, x) assert Max(x, Min(y, oo)) == Max(x, y) assert Max(n, -oo, n_, p, 2) == Max(p, 2) assert Max(n, -oo, n_, p) == p assert Max(2, x, p, n, -oo, -oo, n_, p, 2) == Max(2, x, p) assert Max(0, x, 1, y) == Max(1, x, y) assert Max(r, r + 1, r - 1) == 1 + r assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000) assert Max(cos(x), sin(x)) == Max(sin(x), cos(x)) assert Max(cos(x), sin(x)).subs({x: 1}) == sin(1) assert Max(cos(x), sin(x)).subs({x: Rational(1, 2)}) == cos(Rational(1, 2)) pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs({x: I})) pytest.raises(ValueError, lambda: Max(I)) pytest.raises(ValueError, lambda: Max(I, x)) pytest.raises(ValueError, lambda: Max(zoo, 1)) # interesting: # Max(n, -oo, n_, p, 2) == Max(p, 2) # True # Max(n, -oo, n_, p, 1000) == Max(p, 1000) # False assert Max(1, x).diff(x) == Heaviside(x - 1) assert Max(x, 1).diff(x) == Heaviside(x - 1) assert Max(x**2, 1 + x, 1).diff(x) == \ 2*x*Heaviside(x**2 - Max(1, x + 1)) \ + Heaviside(x - Max(1, x**2) + 1) pytest.raises(ArgumentIndexError, lambda: Max(1, x).fdiff(3)) a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True) # a and b are both real, Max(a, b) should be real assert Max(a, b).is_extended_real # issue sympy/sympy#7233 e = Max(0, x) assert e.evalf == e.n assert e.evalf(strict=False).args == (0, x)
def test_add2(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m, n, p, q = tensor_indices('m,n,p,q', Lorentz) R = tensorhead('R', [Lorentz] * 4, [[2, 2]]) A = tensorhead('A', [Lorentz] * 3, [[3]]) t1 = 2 * R(m, n, p, q) - R(m, q, n, p) + R(m, p, n, q) t2 = t1 * A(-n, -p, -q) assert t2 == 0 t1 = Rational(2, 3) * R(m, n, p, q) - Rational(1, 3) * R( m, q, n, p) + Rational(1, 3) * R(m, p, n, q) t2 = t1 * A(-n, -p, -q) assert t2 == 0 t = A(m, -m, n) + A(n, p, -p) assert t == 0
def test_sympyissue_3449(): # test if powers are simplified correctly # see also issue sympy/sympy#3995 assert cbrt(x)**2 == x**Rational(2, 3) assert (x**3)**Rational(2, 5) == Pow(x**3, Rational(2, 5), evaluate=False) a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert (a**2)**b == (abs(a)**b)**2 assert sqrt(1/a) != 1/sqrt(a) # e.g. for a = -1 assert cbrt(a**3) != a assert (x**a)**b != x**(a*b) # e.g. x = -1, a=2, b=1/2 assert (x**.5)**b == x**(.5*b) assert (x**.5)**.5 == x**.25 assert (x**2.5)**.5 != x**1.25 # e.g. for x = 5*I k = Symbol('k', integer=True) m = Symbol('m', integer=True) assert (x**k)**m == x**(k*m) assert Number(5)**Rational(2, 3) == cbrt(Number(25)) assert (x**.5)**2 == x**1.0 assert (x**2)**k == (x**k)**2 == x**(2*k) a = Symbol('a', positive=True) assert (a**3)**Rational(2, 5) == a**Rational(6, 5) assert (a**2)**b == (a**b)**2 assert (a**Rational(2, 3))**x == (a**(2*x/3)) != (a**x)**Rational(2, 3)
def _print_Mul(self, expr): prec = precedence(expr) c, e = expr.as_coeff_Mul() if c < 0: expr = _keep_coeff(-c, e) sign = "-" else: sign = "" a = [] # items in the numerator b = [] # items that are in the denominator (if any) if self.order != 'none': args = expr.as_ordered_factors() else: # use make_args in case expr was something like -x -> x args = Mul.make_args(expr) multiple_ones = len([x for x in args if x == S.One]) > 1 # Gather args for numerator/denominator for item in args: if item.is_commutative and item.is_Pow and item.exp.is_Rational and item.exp.is_negative: if item.exp != -1: b.append(Pow(item.base, -item.exp, evaluate=False)) else: b.append(Pow(item.base, -item.exp)) elif item.is_Rational and item is not S.Infinity: if item.p != 1 or multiple_ones: a.append(Rational(item.p)) if item.q != 1: b.append(Rational(item.q)) else: a.append(item) a = a or [S.One] a_str = [self.parenthesize(x, prec) for x in a] b_str = [self.parenthesize(x, prec) for x in b] if len(b) == 0: return sign + '*'.join(a_str) elif len(b) == 1: return sign + '*'.join(a_str) + "/" + b_str[0] else: return sign + '*'.join(a_str) + "/(%s)" % '*'.join(b_str)
def test_Pow(): assert mcode(x**3) == "x.^3" assert mcode(x**(y**3)) == "x.^(y.^3)" assert mcode(x**Rational(2, 3)) == 'x.^(2/3)' g = implemented_function('g', Lambda(x, 2 * x)) assert mcode(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \ "(3.5*2*x).^(-x + y.^x)./(x.^2 + y)"
def test_Pow(): assert mcode(x**3) == "x^3" assert mcode(x**(y**3)) == "x^(y^3)" assert mcode(1/(f(x)*3.5)**(x - y**x)/(x**2 + y)) == \ "(3.5*f[x])^(-x + y^x)/(x^2 + y)" assert mcode(x**-1.0) == 'x^(-1.0)' assert mcode(x**Rational(2, 3)) == 'x^(2/3)'
def test_sympyissue_6990(): x = Symbol('x') a = Symbol('a') b = Symbol('b') assert (sqrt(a + b*x + x**2)).series(x, 0, 3).removeO() == \ b*x/(2*sqrt(a)) + x**2*(1/(2*sqrt(a)) - b**2/(8*a**Rational(3, 2))) + sqrt(a)
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="-")': Limit(sin(x), x, 0, dir='-'), } for text, result in inputs.items(): assert parse_expr(text) == result
def test_rationalize(): inputs = { '0.123': Rational(123, 1000) } transformations = standard_transformations + (rationalize,) for text, result in inputs.items(): assert parse_expr(text, transformations=transformations) == result
def test_simplex(): pytest.raises(ValueError, lambda: simplex([1], [[1, 2], [2, 3]], [4, 7])) pytest.raises(ValueError, lambda: simplex([1, 3], [[1, 2], [2, 3]], [4])) pytest.raises(InfeasibleProblem, lambda: simplex([1, 3], [[1, 2], [2, 3]], [-3, 1])) assert simplex([2, 3, 2], [[2, 1, 1], [1, 2, 1], [0, 0, 1]], [4, 7, 5]) == (11, (0, 3, 1)) assert simplex([2, 3, 4], [[3, 2, 1], [2, 5, 3]], [10, 15]) == (20, (0, 0, 5)) assert simplex([Rational(1, 2), 3, 1, 1], [[1, 1, 1, 1], [-2, -1, 1, 1], [0, 1, 0, -1]], [40, 10, 10]) == (90, (0, 25, 0, 15)) assert simplex([2, 1], [[-1, 1], [1, -2]], [1, 2]) == (oo, (oo, oo)) assert simplex([1, 2, 3, -2], [[3, -2, 1, 1], [-2, 1, 10, -1], [2, 0, 0, 1]], [1, -2, 3]) == (-4, (0, 1, 0, 3)) pytest.raises( InfeasibleProblem, lambda: simplex( [1, 2, 3, -2], [[3, -2, 1, 1], [-2, 1, 10, -1], [2, 0, 0, 1], [0, -1, 2, 0]], [1, -2, 3, -3]))
def test_doit_with_MatrixBase(): X = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(X, 0).doit() == ImmutableMatrix(Identity(2)) assert MatPow(X, 1).doit() == X assert MatPow(X, 2).doit() == X**2 assert MatPow(X, -1).doit() == X.inv() assert MatPow(X, -2).doit() == (X.inv())**2 # less expensive than testing on a 2x2 assert MatPow(ImmutableMatrix([4]), Rational(1, 2)).doit() == ImmutableMatrix([2])
def test_minimize_linear(): assert minimize([ -2 * x - 3 * y - 2 * z, 2 * x + y + z <= 4, x + 2 * y + z <= 7, z <= 5, x >= 0, y >= 0, z >= 0 ], x, y, z) == (-11, { x: 0, y: 3, z: 1 }) assert minimize([ -2 * x - 3 * y - 2 * z, 2 * x + y + z <= 4, x + 2 * y + z <= 7, z <= 5, x >= 0, y >= 0, z >= 0 ], x, y, z) == (-11, { x: 0, y: 3, z: 1 }) assert minimize([ -2 * x - 3 * y - 2 * z, 2 * x + y + z <= 4, x + 2 * y + z < 7, z <= 5, x >= 0, y >= 0, z >= 0 ], x, y, z) is None assert minimize([ -2 * x - 3 * y - 4 * z, 3 * x + 2 * y + z <= 10, 2 * x + 5 * y + 3 * z <= 15, x >= 0, y >= 0, z >= 0 ], x, y, z) == (-20, { x: 0, y: 0, z: 5 }) assert maximize([ 12 * x + 40 * y, x + y <= 15, x + 3 * y <= 36, x <= 10, x >= 0, y >= 0 ], x, y) == (480, { x: 0, y: 12 }) assert minimize([ -2 * x - 3 * y - 4 * z, Eq(3 * x + 2 * y + z, 10), Eq(2 * x + 5 * y + 3 * z, 15), x >= 0, y >= 0, z >= 0 ], x, y, z) == (Rational(-130, 7), { x: Rational(15, 7), y: 0, z: Rational(25, 7) })
def _sin_pow_integrate(n, x): if n > 0: if n == 1: # Recursion break return -cos(x) # n > 0 # / / # | | # | n -1 n-1 n - 1 | n-2 # | sin (x) dx = ______ cos (x) sin (x) + _______ | sin (x) dx # | | # | n n | # / / # return (Rational(-1, n) * cos(x) * sin(x)**(n - 1) + Rational(n - 1, n) * _sin_pow_integrate(n - 2, x)) if n < 0: if n == -1: # Make sure this does not come back here again. # Recursion breaks here or at n==0. return trigintegrate(1 / sin(x), x) # n < 0 # / / # | | # | n 1 n+1 n + 2 | n+2 # | sin (x) dx = _______ cos (x) sin (x) + _______ | sin (x) dx # | | # | n + 1 n + 1 | # / / # return (Rational(1, n + 1) * cos(x) * sin(x)**(n + 1) + Rational(n + 2, n + 1) * _sin_pow_integrate(n + 2, x)) else: # n == 0 # Recursion break. return x
def _eval_Eq(self, other): # RootOf represents a Root, so if other is that root, it should set # the expression to zero *and* it should be in the interval of the # RootOf instance. It must also be a number that agrees with the # is_real value of the RootOf instance. if type(self) == type(other): return sympify(self.__eq__(other)) if not (other.is_number and not other.has(AppliedUndef)): return S.false if not other.is_finite: return S.false z = self.expr.subs(self.expr.free_symbols.pop(), other).is_zero if z is False: # all roots will make z True but we don't know # whether this is the right root if z is True return S.false o = other.is_extended_real, other.is_imaginary s = self.is_extended_real, self.is_imaginary if o != s and None not in o and None not in s: return S.false i = self._get_interval() was = i.a, i.b need = [True] * 2 # make sure it would be distinct from others while any(need): i = i.refine() a, b = i.a, i.b if need[0] and a != was[0]: need[0] = False if need[1] and b != was[1]: need[1] = False re, im = other.as_real_imag() if not im: if self.is_extended_real: a, b = [Rational(str(i)) for i in (a, b)] return sympify(a < other and other < b) return S.false if self.is_extended_real: return S.false z = r1, r2, i1, i2 = [ Rational(str(j)) for j in (i.ax, i.bx, i.ay, i.by) ] return sympify((r1 < re and re < r2) and (i1 < im and im < i2))
def test_1_over_x_and_sqrt(): # 1.0 and 0.5 would do something different in regular StrPrinter, # but these are exact in IEEE floating point so no different here. assert mcode(1 / x) == '1./x' assert mcode(x**-1) == mcode(x**-1.0) == '1./x' assert mcode(1 / sqrt(x)) == '1./sqrt(x)' assert mcode(x**Rational(-1, 2)) == mcode(x**-0.5) == '1./sqrt(x)' assert mcode(sqrt(x)) == mcode(x**0.5) == 'sqrt(x)' assert mcode(1 / pi) == '1/pi' assert mcode(pi**-1) == mcode(pi**-1.0) == '1/pi' assert mcode(pi**-0.5) == '1/sqrt(pi)'
def test_ccode_Pow(): assert ccode(x**3) == "pow(x, 3)" assert ccode(x**(y**3)) == "pow(x, pow(y, 3))" g = implemented_function('g', Lambda(x, 2 * x)) assert ccode(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \ "pow(3.5*2*x, -x + pow(y, x))/(pow(x, 2) + y)" assert ccode(x**-1.0) == '1.0/x' assert ccode(x**Rational(2, 3)) == 'pow(x, 2.0L/3.0L)' _cond_cfunc = [(lambda base, exp: exp.is_integer, "dpowi"), (lambda base, exp: not exp.is_integer, "pow")] assert ccode(x**3, user_functions={'Pow': _cond_cfunc}) == 'dpowi(x, 3)' assert ccode(x**3.2, user_functions={'Pow': _cond_cfunc}) == 'pow(x, 3.2)'
def test_MatrixSymbol(): n = Symbol('n', integer=True) A = MatrixSymbol('A', n, n) B = MatrixSymbol('B', n, n) assert mcode(A * B) == "A*B" assert mcode(B * A) == "B*A" assert mcode(2 * A * B) == "2*A*B" assert mcode(B * 2 * A) == "2*B*A" assert mcode(A * (B + 3 * Identity(n))) == "A*(B + 3*eye(n))" assert mcode(A**(x**2)) == "A^(x.^2)" assert mcode(A**3) == "A^3" assert mcode(A**Rational(1, 2)) == "A^(1/2)"