def as_real_imag(self, deep=True, **hints): """ Returns this function as a complex coordinate. Examples ======== >>> from diofant import I >>> from diofant.abc import x >>> from diofant.functions import log >>> log(x).as_real_imag() (log(Abs(x)), arg(x)) >>> log(I).as_real_imag() (0, pi/2) >>> log(1 + I).as_real_imag() (log(sqrt(2)), pi/4) >>> log(I*x).as_real_imag() (log(Abs(x)), arg(I*x)) """ from diofant import Abs, arg if deep: abs = Abs(self.args[0].expand(deep, **hints)) arg = arg(self.args[0].expand(deep, **hints)) else: abs = Abs(self.args[0]) arg = arg(self.args[0]) if hints.get('log', False): # Expand the log hints['complex'] = False return log(abs).expand(deep, **hints), arg else: return log(abs), arg
def test_erfc(): assert erfc(nan) == nan assert erfc(oo) == 0 assert erfc(-oo) == 2 assert erfc(0) == 1 assert erfc(I * oo) == -oo * I assert erfc(-I * oo) == oo * I assert erfc(-x) == Integer(2) - erfc(x) assert erfc(erfcinv(x)) == x assert erfc(erfinv(x)) == 1 - x assert erfc(I).is_extended_real is False assert erfc(w).is_extended_real is True assert erfc(z).is_extended_real is None assert conjugate(erfc(z)) == erfc(conjugate(z)) assert erfc(x).as_leading_term(x) == 1 assert erfc(1 / x).as_leading_term(x) == erfc(1 / x) assert erfc(z).rewrite('erf') == 1 - erf(z) assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z) assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite('hyper') == 1 - 2 * z * hyper( [Rational(1, 2)], [Rational(3, 2)], -z**2) / sqrt(pi) assert erfc(z).rewrite('meijerg') == 1 - z * meijerg( [Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2) / sqrt(pi) assert erfc(z).rewrite( 'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z assert erfc(z).rewrite('expint') == 1 - sqrt(z**2) / z + z * expint( Rational(1, 2), z**2) / sqrt(pi) assert erfc(x).as_real_imag() == \ ((erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag() assert erfc(w).as_real_imag() == (erfc(w), 0) assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag() assert erfc(I).as_real_imag() == (1, -erfi(1)) pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2)) assert erfc(x).taylor_term(3, x, *(-2 * x / sqrt(pi), 0)) == 2 * x**3 / 3 / sqrt(pi) assert erfc(x).limit(x, oo) == 0 assert erfc(x).diff(x) == -2 * exp(-x**2) / sqrt(pi)
def test_real_imag(): x, y, z = symbols('x, y, z') X, Y, Z = symbols('X, Y, Z', commutative=False) a = Symbol('a', extended_real=True) assert (2 * a * x).as_real_imag() == (2 * a * re(x), 2 * a * im(x)) # issue 5395: assert (x * x.conjugate()).as_real_imag() == (Abs(x)**2, 0) assert im(x * x.conjugate()) == 0 assert im(x * y.conjugate() * z * y) == im(x * z) * Abs(y)**2 assert im(x * y.conjugate() * x * y) == im(x**2) * Abs(y)**2 assert im(Z * y.conjugate() * X * y) == im(Z * X) * Abs(y)**2 assert im(X * X.conjugate()) == im(X * X.conjugate(), evaluate=False) assert (sin(x)*sin(x).conjugate()).as_real_imag() == \ (Abs(sin(x))**2, 0) # issue 6573: assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2 * re(x) * im(x)) # issue 6428: r = Symbol('r', extended_real=True) i = Symbol('i', imaginary=True) assert (i * r * x).as_real_imag() == (I * i * r * im(x), -I * i * r * re(x)) assert (i * r * x * (y + 2)).as_real_imag() == (I * i * r * (re(y) + 2) * im(x) + I * i * r * re(x) * im(y), -I * i * r * (re(y) + 2) * re(x) + I * i * r * im(x) * im(y)) # issue 7106: assert ((1 + I) / (1 - I)).as_real_imag() == (0, 1) assert ((1 + 2 * I) * (1 + 3 * I)).as_real_imag() == (-5, 5)
def test_periodic_argument(): p = Symbol('p', positive=True) assert unbranched_argument(2 + I) == periodic_argument(2 + I, oo) assert unbranched_argument(1 + x) == periodic_argument(1 + x, oo) assert N_equals(unbranched_argument((1 + I)**2), pi/2) assert N_equals(unbranched_argument((1 - I)**2), -pi/2) assert N_equals(periodic_argument((1 + I)**2, 3*pi), pi/2) assert N_equals(periodic_argument((1 - I)**2, 3*pi), -pi/2) assert unbranched_argument(principal_branch(x, pi)) == \ periodic_argument(x, pi) assert unbranched_argument(polar_lift(2 + I)) == unbranched_argument(2 + I) assert periodic_argument(polar_lift(2 + I), 2*pi) == \ periodic_argument(2 + I, 2*pi) assert periodic_argument(polar_lift(2 + I), 3*pi) == \ periodic_argument(2 + I, 3*pi) assert periodic_argument(polar_lift(2 + I), pi) == \ periodic_argument(polar_lift(2 + I), pi) assert unbranched_argument(polar_lift(1 + I)) == pi/4 assert periodic_argument(2*p, p) == periodic_argument(p, p) assert periodic_argument(pi*p, p) == periodic_argument(p, p) assert Abs(polar_lift(1 + I)) == Abs(1 + I) assert periodic_argument(x, pi).is_real is True assert periodic_argument(x, oo, evaluate=False).is_real is None
def test_erf(): assert erf(nan) == nan assert erf(oo) == 1 assert erf(-oo) == -1 assert erf(0) == 0 assert erf(I * oo) == oo * I assert erf(-I * oo) == -oo * I assert erf(-2) == -erf(2) assert erf(-x * y) == -erf(x * y) assert erf(-x - y) == -erf(x + y) assert erf(erfinv(x)) == x assert erf(erfcinv(x)) == 1 - x assert erf(erf2inv(0, x)) == x assert erf(erf2inv(0, erf(erfcinv(1 - erf(erfinv(x)))))) == x assert erf(I).is_extended_real is False assert erf(w).is_extended_real is True assert erf(z).is_extended_real is None assert conjugate(erf(z)) == erf(conjugate(z)) assert erf(x).as_leading_term(x) == 2 * x / sqrt(pi) assert erf(1 / x).as_leading_term(x) == erf(1 / x) assert erf(z).rewrite('uppergamma') == sqrt(z**2) * erf(sqrt(z**2)) / z assert erf(z).rewrite('erfc') == S.One - erfc(z) assert erf(z).rewrite('erfi') == -I * erfi(I * z) assert erf(z).rewrite('fresnels') == (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erf(z).rewrite('fresnelc') == (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erf(z).rewrite('hyper') == 2 * z * hyper([S.Half], [3 * S.Half], -z**2) / sqrt(pi) assert erf(z).rewrite('meijerg') == z * meijerg([S.Half], [], [0], [-S.Half], z**2) / sqrt(pi) assert erf(z).rewrite( 'expint') == sqrt(z**2) / z - z * expint(S.Half, z**2) / sqrt(S.Pi) assert limit(exp(x)*exp(x**2)*(erf(x + 1/exp(x)) - erf(x)), x, oo) == \ 2/sqrt(pi) assert limit((1 - erf(z)) * exp(z**2) * z, z, oo) == 1 / sqrt(pi) assert limit((1 - erf(x)) * exp(x**2) * sqrt(pi) * x, x, oo) == 1 assert limit(((1 - erf(x)) * exp(x**2) * sqrt(pi) * x - 1) * 2 * x**2, x, oo) == -1 assert erf(x).as_real_imag() == \ ((erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) pytest.raises(ArgumentIndexError, lambda: erf(x).fdiff(2))
def test_issue_7173(): assert laplace_transform(sinh(a*x)*cosh(a*x), x, s) == \ (a/(s**2 - 4*a**2), 0, And(Or(Abs(periodic_argument(exp_polar(I*pi)*polar_lift(a), oo)) < pi/2, Abs(periodic_argument(exp_polar(I*pi)*polar_lift(a), oo)) <= pi/2), Or(Abs(periodic_argument(a, oo)) < pi/2, Abs(periodic_argument(a, oo)) <= pi/2)))
def test_numpy_numexpr(): a, b, c = numpy.random.randn(3, 128, 128) # ensure that numpy and numexpr return same value for complicated expression expr = (sin(x) + cos(y) + tan(z)**2 + Abs(z - y)*acos(sin(y*z)) + Abs(y - z)*acosh(2 + exp(y - x)) - sqrt(x**2 + I*y**2)) npfunc = lambdify((x, y, z), expr, modules='numpy') nefunc = lambdify((x, y, z), expr, modules='numexpr') assert numpy.allclose(npfunc(a, b, c), nefunc(a, b, c))
def test_erfi(): assert erfi(nan) == nan assert erfi(+oo) == +oo assert erfi(-oo) == -oo assert erfi(0) == 0 assert erfi(I * oo) == I assert erfi(-I * oo) == -I assert erfi(-x) == -erfi(x) assert erfi(I * erfinv(x)) == I * x assert erfi(I * erfcinv(x)) == I * (1 - x) assert erfi(I * erf2inv(0, x)) == I * x assert erfi(I).is_extended_real is False assert erfi(w).is_extended_real is True assert erfi(z).is_extended_real is None assert conjugate(erfi(z)) == erfi(conjugate(z)) assert erfi(z).rewrite('erf') == -I * erf(I * z) assert erfi(z).rewrite('erfc') == I * erfc(I * z) - I assert erfi(z).rewrite('fresnels') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('fresnelc') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('hyper') == 2 * z * hyper( [Rational(1, 2)], [Rational(3, 2)], z**2) / sqrt(pi) assert erfi(z).rewrite('meijerg') == z * meijerg( [Rational(1, 2)], [], [0], [Rational(-1, 2)], -z**2) / sqrt(pi) assert erfi(z).rewrite('uppergamma') == ( sqrt(-z**2) / z * (uppergamma(Rational(1, 2), -z**2) / sqrt(pi) - 1)) assert erfi(z).rewrite('expint') == sqrt(-z**2) / z - z * expint( Rational(1, 2), -z**2) / sqrt(pi) assert erfi(x).as_real_imag() == \ ((erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) assert erfi(x).as_real_imag(deep=False) == erfi(x).as_real_imag() assert erfi(w).as_real_imag() == (erfi(w), 0) assert erfi(w).as_real_imag(deep=False) == erfi(w).as_real_imag() assert erfi(I).as_real_imag() == (0, erf(1)) pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2)) assert erfi(x).taylor_term(3, x, *(2 * x / sqrt(pi), 0)) == 2 * x**3 / 3 / sqrt(pi) assert erfi(x).limit(x, oo) == oo
def test_Abs_rewrite(): x = Symbol('x', extended_real=True) a = Abs(x).rewrite(Heaviside).expand() assert a == x*Heaviside(x) - x*Heaviside(-x) for i in [-2, -1, 0, 1, 2]: assert a.subs({x: i}) == abs(i) y = Symbol('y') assert Abs(y).rewrite(Heaviside) == Abs(y) x, y = Symbol('x', extended_real=True), Symbol('y') assert Abs(x).rewrite(Piecewise) == Piecewise((x, x >= 0), (-x, True)) assert Abs(y).rewrite(Piecewise) == Abs(y) assert Abs(y).rewrite(sign) == y/sign(y)
def test_derivatives_issue_4757(): x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert re(f(x)).diff(x) == re(f(x).diff(x)) assert im(f(x)).diff(x) == im(f(x).diff(x)) assert re(f(y)).diff(y) == -I * im(f(y).diff(y)) assert im(f(y)).diff(y) == -I * re(f(y).diff(y)) assert Abs(f(x)).diff(x).subs(f(x), 1 + I * x).doit() == x / sqrt(1 + x**2) assert arg(f(x)).diff(x).subs(f(x), 1 + I * x**2).doit() == 2 * x / (1 + x**4) assert Abs(f(y)).diff(y).subs(f(y), 1 + y).doit() == -y / sqrt(1 - y**2) assert arg(f(y)).diff(y).subs(f(y), I + y**2).doit() == 2 * y / (1 + y**4)
def test_airy_base(): z = Symbol('z') x = Symbol('x', extended_real=True) y = Symbol('y', extended_real=True) assert conjugate(airyai(z)) == airyai(conjugate(z)) assert airyai(x).is_extended_real assert airyai(z).is_extended_real is None assert airyai(x + I * y).as_real_imag() == ( airyai(x - I * x * Abs(y) / Abs(x)) / 2 + airyai(x + I * x * Abs(y) / Abs(x)) / 2, I * x * (airyai(x - I * x * Abs(y) / Abs(x)) - airyai(x + I * x * Abs(y) / Abs(x))) * Abs(y) / (2 * y * Abs(x)))
def test_erfc(): assert erfc(nan) == nan assert erfc(oo) == 0 assert erfc(-oo) == 2 assert erfc(0) == 1 assert erfc(I * oo) == -oo * I assert erfc(-I * oo) == oo * I assert erfc(-x) == Integer(2) - erfc(x) assert erfc(erfcinv(x)) == x assert erfc(I).is_extended_real is False assert erfc(w).is_extended_real is True assert erfc(z).is_extended_real is None assert conjugate(erfc(z)) == erfc(conjugate(z)) assert erfc(x).as_leading_term(x) == S.One assert erfc(1 / x).as_leading_term(x) == erfc(1 / x) assert erfc(z).rewrite('erf') == 1 - erf(z) assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z) assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite( 'hyper') == 1 - 2 * z * hyper([S.Half], [3 * S.Half], -z**2) / sqrt(pi) assert erfc(z).rewrite('meijerg') == 1 - z * meijerg( [S.Half], [], [0], [-S.Half], z**2) / sqrt(pi) assert erfc(z).rewrite( 'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z assert erfc(z).rewrite('expint') == S.One - sqrt(z**2) / z + z * expint( S.Half, z**2) / sqrt(S.Pi) assert erfc(x).as_real_imag() == \ ((erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2))
def test_erfi(): assert erfi(nan) == nan assert erfi(oo) == S.Infinity assert erfi(-oo) == S.NegativeInfinity assert erfi(0) == S.Zero assert erfi(I * oo) == I assert erfi(-I * oo) == -I assert erfi(-x) == -erfi(x) assert erfi(I * erfinv(x)) == I * x assert erfi(I * erfcinv(x)) == I * (1 - x) assert erfi(I * erf2inv(0, x)) == I * x assert erfi(I).is_extended_real is False assert erfi(w).is_extended_real is True assert erfi(z).is_extended_real is None assert conjugate(erfi(z)) == erfi(conjugate(z)) assert erfi(z).rewrite('erf') == -I * erf(I * z) assert erfi(z).rewrite('erfc') == I * erfc(I * z) - I assert erfi(z).rewrite('fresnels') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('fresnelc') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('hyper') == 2 * z * hyper([S.Half], [3 * S.Half], z **2) / sqrt(pi) assert erfi(z).rewrite('meijerg') == z * meijerg( [S.Half], [], [0], [-S.Half], -z**2) / sqrt(pi) assert erfi(z).rewrite('uppergamma') == ( sqrt(-z**2) / z * (uppergamma(S.Half, -z**2) / sqrt(S.Pi) - S.One)) assert erfi(z).rewrite( 'expint') == sqrt(-z**2) / z - z * expint(S.Half, -z**2) / sqrt(S.Pi) assert erfi(x).as_real_imag() == \ ((erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2))
def test_imageset_intersect_real(): assert (imageset(Lambda(n, n + (n - 1)*(n + 1)*I), S.Integers).intersect(S.Reals) == FiniteSet(-1, 1)) s = ImageSet(Lambda(n, -I*(I*(2*pi*n - pi/4) + log(Abs(sqrt(-I))))), S.Integers) assert s.intersect(S.Reals) == imageset(Lambda(n, 2*n*pi - pi/4), S.Integers)
def test_issue_8514(): a, b, c, = symbols('a b c', positive=True, finite=True) t = symbols('t', positive=True) ft = simplify(inverse_laplace_transform(1 / (a * s**2 + b * s + c), s, t)) assert ft.rewrite(atan2) == ( (exp(t * (exp(I * atan2(0, -4 * a * c + b**2) / 2) - exp(-I * atan2( 0, -4 * a * c + b**2) / 2)) * sqrt(Abs(4 * a * c - b**2)) / (4 * a)) * exp(t * cos(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / a) + I * sin(t * sin(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / (2 * a)) - cos(t * sin(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2)) / (2 * a))) * exp(-t * (b + cos(atan2(0, -4 * a * c + b**2) / 2) * sqrt(Abs(4 * a * c - b**2))) / (2 * a)) / sqrt(-4 * a * c + b**2))
def test_geometric_sums(): assert summation(pi**n, (n, 0, b)) == (1 - pi**(b + 1)) / (1 - pi) assert summation(2 * 3**n, (n, 0, b)) == 3**(b + 1) - 1 assert summation(Rational(1, 2)**n, (n, 1, oo)) == 1 assert summation(2**n, (n, 0, b)) == 2**(b + 1) - 1 assert summation(2**n, (n, 1, oo)) == oo assert summation(2**(-n), (n, 1, oo)) == 1 assert summation(3**(-n), (n, 4, oo)) == Rational(1, 54) assert summation(2**(-4 * n + 3), (n, 1, oo)) == Rational(8, 15) assert summation(2**(n + 1), (n, 1, b)).expand() == 4 * (2**b - 1) # issue sympy/sympy#6664: assert summation(x**n, (n, 0, oo)) == \ Piecewise((1/(-x + 1), Abs(x) < 1), (Sum(x**n, (n, 0, oo)), True)) assert summation(-2**n, (n, 0, oo)) == -oo assert summation(I**n, (n, 0, oo)) == Sum(I**n, (n, 0, oo)) # issue sympy/sympy#6802: assert summation((-1)**(2 * x + 2), (x, 0, n)) == n + 1 assert summation((-2)**(2 * x + 2), (x, 0, n)) == 4 * 4**(n + 1) / 3 - Rational(4, 3) assert summation((-1)**x, (x, 0, n)) == -(-1)**(n + 1) / 2 + Rational(1, 2) assert summation(y**x, (x, a, b)) == \ Piecewise((-a + b + 1, Eq(y, 1)), ((y**a - y**(b + 1))/(-y + 1), True)) assert summation((-2)**(y*x + 2), (x, 0, n)) == \ 4*Piecewise((n + 1, Eq((-2)**y, 1)), ((-(-2)**(y*(n + 1)) + 1)/(-(-2)**y + 1), True)) # issue sympy/sympy#8251: assert summation((1 / (n + 1)**2) * n**2, (n, 0, oo)) == oo # issue sympy/sympy#9908: assert Sum(1 / (n**3 - 1), (n, -oo, -2)).doit() == summation(1 / (n**3 - 1), (n, -oo, -2))
def test_Abs_rewrite(): x = Symbol('x', extended_real=True) a = Abs(x).rewrite(Heaviside).expand() assert a == x*Heaviside(x) - x*Heaviside(-x) for i in [-2, -1, 0, 1, 2]: assert a.subs(x, i) == abs(i) y = Symbol('y') assert Abs(y).rewrite(Heaviside) == Abs(y) x, y = Symbol('x', extended_real=True), Symbol('y') assert Abs(x).rewrite(Piecewise) == Piecewise((x, x >= 0), (-x, True)) assert Abs(y).rewrite(Piecewise) == Abs(y) assert Abs(y).rewrite(sign) == y/sign(y)
def test_sympyissue_4884(): assert integrate(sqrt(x)*(1 + x)) == \ Piecewise( (2*sqrt(x)*(x + 1)**2/5 - 2*sqrt(x)*(x + 1)/15 - 4*sqrt(x)/15, Abs(x + 1) > 1), (2*I*sqrt(-x)*(x + 1)**2/5 - 2*I*sqrt(-x)*(x + 1)/15 - 4*I*sqrt(-x)/15, True)) assert integrate(x**x * (1 + log(x))) == x**x
def test_sympyissue_8368(): assert integrate(exp(-s*x)*cosh(x), (x, 0, oo)) == \ Piecewise((pi*Piecewise((-s/(pi*(-s**2 + 1)), Abs(s**2) < 1), (1/(pi*s*(1 - 1/s**2)), Abs(s**(-2)) < 1), (meijerg(((Rational(1, 2),), (0, 0)), ((0, Rational(1, 2)), (0,)), polar_lift(s)**2), True)), And(Abs(periodic_argument(polar_lift(s)**2, oo)) < pi, Ne(s**2, 1), cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0)), (Integral(exp(-s*x)*cosh(x), (x, 0, oo)), True)) assert integrate(exp(-s*x)*sinh(x), (x, 0, oo)) == \ Piecewise((pi*Piecewise((2/(pi*(2*s**2 - 2)), Abs(s**2) < 1), (-2/(pi*s**2*(-2 + 2/s**2)), Abs(s**(-2)) < 1), (meijerg(((0,), (Rational(-1, 2), Rational(1, 2))), ((0, Rational(1, 2)), (Rational(-1, 2),)), polar_lift(s)**2), True)), And(Abs(periodic_argument(polar_lift(s)**2, oo)) < pi, Ne(s**2, 1), cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0)), (Integral(E**(-s*x)*sinh(x), (x, 0, oo)), True))
def test_sympyissue_4492(): assert simplify(integrate(x**2 * sqrt(5 - x**2), x)) == Piecewise( (I * (2 * x**5 - 15 * x**3 + 25 * x - 25 * sqrt(x**2 - 5) * acosh(sqrt(5) * x / 5)) / (8 * sqrt(x**2 - 5)), 1 < Abs(x**2) / 5), ((-2 * x**5 + 15 * x**3 - 25 * x + 25 * sqrt(-x**2 + 5) * asin(sqrt(5) * x / 5)) / (8 * sqrt(-x**2 + 5)), True))
def test_builtins(): cases = [('abs(x)', 'Abs(x)'), ('max(x, y)', 'Max(x, y)'), ('min(x, y)', 'Min(x, y)'), ('pow(x, y)', 'Pow(x, y)')] for built_in_func_call, sympy_func_call in cases: assert parse_expr(built_in_func_call) == parse_expr(sympy_func_call) assert parse_expr('pow(38, -1)') == Rational(1, 38) # issue sympy/sympy#22322 assert parse_expr('abs(-42)', evaluate=False) == Abs(-42, evaluate=False)
def test_numpy_matmul(): xmat = Matrix([[x, y], [z, 1+z]]) ymat = Matrix([[x**2], [Abs(x)]]) mat_func = lambdify((x, y, z), xmat*ymat, modules="numpy") numpy.testing.assert_array_equal(mat_func(0.5, 3, 4), numpy.array([[1.625], [3.5]])) numpy.testing.assert_array_equal(mat_func(-0.5, 3, 4), numpy.array([[1.375], [3.5]])) # Multiple matrices chained together in multiplication f = lambdify((x, y, z), xmat*xmat*xmat, modules="numpy") numpy.testing.assert_array_equal(f(0.5, 3, 4), numpy.array([[72.125, 119.25], [159, 251]]))
def test_sympyissue_2787(): n, k = symbols('n k', positive=True, integer=True) p = symbols('p', positive=True) binomial_dist = binomial(n, k) * p**k * (1 - p)**(n - k) s = Sum(binomial_dist * k, (k, 0, n)) res = s.doit().simplify() assert res == Piecewise( (n * p, And(Or(-n + 1 < 0, Ne(p / (p - 1), 1)), p / Abs(p - 1) <= 1)), (Sum(k * p**k * (-p + 1)**(-k) * (-p + 1)**n * binomial(n, k), (k, 0, n)), True))
def test_derivatives_sympyissue_4757(): x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert re(f(x)).diff(x) == re(f(x).diff(x)) assert im(f(x)).diff(x) == im(f(x).diff(x)) assert re(f(y)).diff(y) == -I * im(f(y).diff(y)) assert im(f(y)).diff(y) == -I * re(f(y).diff(y)) assert re(f(z)).diff(z) == Derivative(re(f(z)), z) assert im(f(z)).diff(z) == Derivative(im(f(z)), z) assert Abs(f(x)).diff(x).subs({ f(x): 1 + I * x }).doit() == x / sqrt(1 + x**2) assert arg(f(x)).diff(x).subs({ f(x): 1 + I * x**2 }).doit() == 2 * x / (1 + x**4) assert Abs(f(y)).diff(y).subs({f(y): 1 + y}).doit() == -y / sqrt(1 - y**2) assert arg(f(y)).diff(y).subs({ f(y): I + y**2 }).doit() == 2 * y / (1 + y**4)
def test_python_functions(): # Simple assert python((2 * x + exp(x))) in "x = Symbol('x')\ne = E**x + 2*x" assert python(sqrt(2)) == 'e = sqrt(2)' assert python(cbrt(2)) == 'e = 2**Rational(1, 3)' assert python(sqrt(2 + pi)) == 'e = sqrt(2 + pi)' assert python(cbrt(2 + pi)) == 'e = (2 + pi)**Rational(1, 3)' assert python(root(2, 4)) == 'e = 2**Rational(1, 4)' assert python(Abs(x)) == "x = Symbol('x')\ne = Abs(x)" assert python(Abs(x / (x**2 + 1))) in [ "x = Symbol('x')\ne = Abs(x/(1 + x**2))", "x = Symbol('x')\ne = Abs(x/(x**2 + 1))" ] # Univariate/Multivariate functions f = Function('f') assert python(f(x)) == "x = Symbol('x')\nf = Function('f')\ne = f(x)" assert python( f(x, y) ) == "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x, y)" assert python(f(x / (y + 1), y)) in [ "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(1 + y), y)", "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(y + 1), y)" ] # Nesting of square roots assert python(sqrt((sqrt(x + 1)) + 1)) in [ "x = Symbol('x')\ne = sqrt(1 + sqrt(1 + x))", "x = Symbol('x')\ne = sqrt(sqrt(x + 1) + 1)" ] # Nesting of powers assert python(cbrt(cbrt(x + 1) + 1)) in [ "x = Symbol('x')\ne = (1 + (1 + x)**Rational(1, 3))**Rational(1, 3)", "x = Symbol('x')\ne = ((x + 1)**Rational(1, 3) + 1)**Rational(1, 3)" ] # Function powers assert python(sin(x)**2) == "x = Symbol('x')\ne = sin(x)**2"
def test_lerchphi(): from diofant import combsimp, exp_polar, polylog, log, lerchphi assert hyperexpand(hyper([1, a], [a + 1], z) / a) == lerchphi(z, 1, a) assert hyperexpand(hyper([1, a, a], [a + 1, a + 1], z) / a**2) == lerchphi( z, 2, a) assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \ lerchphi(z, 3, a) assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \ lerchphi(z, 10, a) assert combsimp( hyperexpand(meijerg([0, 1 - a], [], [0], [-a], exp_polar(-I * pi) * z))) == lerchphi(z, 1, a) assert combsimp( hyperexpand( meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a], exp_polar(-I * pi) * z))) == lerchphi(z, 2, a) assert combsimp( hyperexpand( meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a], exp_polar(-I * pi) * z))) == lerchphi(z, 3, a) assert hyperexpand(z * hyper([1, 1], [2], z)) == -log(1 + -z) assert hyperexpand(z * hyper([1, 1, 1], [2, 2], z)) == polylog(2, z) assert hyperexpand(z * hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z) assert hyperexpand(hyper([1, a, 1 + Rational(1, 2)], [a + 1, Rational(1, 2)], z)) == \ -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a) # Now numerical tests. These make sure reductions etc are carried out # correctly # a rational function (polylog at negative integer order) assert can_do([2, 2, 2], [1, 1]) # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1 # reduction of order for polylog assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10) # reduction of order for lerchphi # XXX lerchphi in mpmath is flaky assert can_do([1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b], numerical=False) # test a bug from diofant import Abs assert hyperexpand(hyper([Rational(1, 2), Rational(1, 2), Rational(1, 2), 1], [Rational(3, 2), Rational(3, 2), Rational(3, 2)], Rational(1, 4))) == \ Abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, Rational(1, 2)))
def test_piecewise_fold_piecewise_in_cond(): p1 = Piecewise((cos(x), x < 0), (0, True)) p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True)) p3 = piecewise_fold(p2) assert (p2.subs(x, -pi / 2) == 0.0) assert (p2.subs(x, 1) == 0.0) assert (p2.subs(x, -pi / 4) == 1.0) p4 = Piecewise((0, Eq(p1, 0)), (1, True)) assert (piecewise_fold(p4) == Piecewise( (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True))) r1 = 1 < Piecewise((1, x < 1), (3, True)) assert (piecewise_fold(r1) == Not(x < 1)) p5 = Piecewise((1, x < 0), (3, True)) p6 = Piecewise((1, x < 1), (3, True)) p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True))) assert (Piecewise((1, And(Not(x < 1), x < 0)), (0, True)))
def test_maxima_functions(): assert parse_maxima('expand( (x+1)^2)') == x**2 + 2*x + 1 assert parse_maxima('factor( x**2 + 2*x + 1)') == (x + 1)**2 assert parse_maxima('2*cos(x)^2 + sin(x)^2') == 2*cos(x)**2 + sin(x)**2 assert parse_maxima('trigexpand(sin(2*x)+cos(2*x))') == \ -1 + 2*cos(x)**2 + 2*cos(x)*sin(x) assert parse_maxima('solve(x^2-4,x)') == [{x: -2}, {x: 2}] assert parse_maxima('limit((1+1/x)^x,x,inf)') == E assert parse_maxima('limit(sqrt(-x)/x,x,0,minus)') == -oo assert parse_maxima('diff(x^x, x)') == x**x*(1 + log(x)) assert parse_maxima('sum(k, k, 1, n)', name_dict={'k': Symbol('k', integer=True), 'n': n}) == (n**2 + n)/2 assert parse_maxima('product(k, k, 1, n)', name_dict={'k': Symbol('k', integer=True), 'n': n}) == factorial(n) assert parse_maxima('ratsimp((x^2-1)/(x+1))') == x - 1 assert Abs( parse_maxima( 'float(sec(%pi/3) + csc(%pi/3))') - 3.154700538379252) < 10**(-5)
def l2_norm(f, lim): """ Calculates L2 norm of the function "f", over the domain lim=(x, a, b). x ...... the independent variable in f over which to integrate a, b ... the limits of the interval Examples ======== >>> from diofant import Symbol >>> from gibbs_phenomenon import l2_norm >>> x = Symbol('x', extended_real=True) >>> l2_norm(1, (x, -1, 1)) sqrt(2) >>> l2_norm(x, (x, -1, 1)) sqrt(6)/3 """ return sqrt(integrate(Abs(f)**2, lim))
def test_Abs_real(): # test some properties of abs that only apply # to real numbers x = Symbol('x', complex=True) assert sqrt(x**2) != Abs(x) assert Abs(x**2) != x**2 x = Symbol('x', extended_real=True) assert sqrt(x**2) == Abs(x) assert Abs(x**2) == x**2 # if the symbol is zero, the following will still apply nn = Symbol('nn', nonnegative=True, extended_real=True) np = Symbol('np', nonpositive=True, extended_real=True) assert Abs(nn) == nn assert Abs(np) == -np
def evalf_log(expr, prec, options): from diofant import Abs, Add, log if len(expr.args) > 1: expr = expr.doit() return evalf(expr, prec, options) arg = expr.args[0] workprec = prec + 10 xre, xim, xacc, _ = evalf(arg, workprec, options) if xim: # XXX: use get_abs etc instead re = evalf_log(log(Abs(arg, evaluate=False), evaluate=False), prec, options) im = mpf_atan2(xim, xre or fzero, prec) return re[0], im, re[2], prec imaginary_term = (mpf_cmp(xre, fzero) < 0) re = mpf_log(mpf_abs(xre), prec, rnd) size = fastlog(re) if prec - size > workprec: # We actually need to compute 1+x accurately, not x arg = Add(S.NegativeOne, arg, evaluate=False) xre, xim, _, _ = evalf_add(arg, prec, options) prec2 = workprec - fastlog(xre) # xre is now x - 1 so we add 1 back here to calculate x re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd) re_acc = prec if imaginary_term: return re, mpf_pi(prec), re_acc, prec else: return re, None, re_acc, None