def test_piecewise_interval(): p1 = Piecewise((x, Interval(0, 1).contains(x)), (0, True)) assert p1.subs({x: -0.5}) == 0 assert p1.subs({x: 0.5}) == 0.5 assert p1.diff(x) == Piecewise((1, Interval(0, 1).contains(x)), (0, True)) assert integrate( p1, x) == Piecewise((x**2/2, Interval(0, 1).contains(x)), (0, True))
def test_piecewise_series(): p1 = Piecewise((sin(x), x < 0), (cos(x), x > 0)) assert p1.nseries(x, n=2) == 1 + O(x**2) # issue sympy/sympy#4315: p2 = Piecewise((0, x < -1), (x**2, x <= 1), (log(x), True)) assert p2.series(x) == x**2
def test_piecewise_solve(): abs2 = Piecewise((-x, x <= 0), (x, x > 0)) f = abs2.subs({x: x - 2}) assert solve(f, x) == [{x: 2}] assert solve(f - 1, x) == [{x: 1}, {x: 3}] f = Piecewise(((x - 2)**2, x >= 0), (1, True)) assert solve(f, x) == [{x: 2}] g = Piecewise(((x - 5)**5, x >= 4), (f, True)) assert solve(g, x) == [{x: 2}, {x: 5}] g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4)) assert solve(g, x) == [{x: 2}, {x: 5}] g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) assert solve(g, x) == [{x: 5}] g = Piecewise(((x - 5)**5, x >= 2), (f, True)) assert solve(g, x) == [{x: 5}] g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) assert solve(g, x) == [{x: 5}] g = Piecewise(((x - 5)**5, x >= 2), (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) assert solve(g, x) == [{x: 5}]
def test_conjugate_transpose(): A, B = symbols("A B", commutative=False) p = Piecewise((A*B**2, x > 0), (A**2*B, True)) assert p.adjoint() == \ Piecewise((adjoint(A*B**2), x > 0), (adjoint(A**2*B), True)) assert p.conjugate() == \ Piecewise((conjugate(A*B**2), x > 0), (conjugate(A**2*B), True)) assert p.transpose() == \ Piecewise((transpose(A*B**2), x > 0), (transpose(A**2*B), True))
def test_piecewise_fold_expand(): p1 = Piecewise((1, Interval(0, 1, False, True).contains(x)), (0, True)) p2 = piecewise_fold(expand((1 - x) * p1)) assert p2 == Piecewise( (1 - x, Interval(0, 1, False, True).contains(x)), (Piecewise( (-x, Interval(0, 1, False, True).contains(x)), (0, True)), True)) p2 = expand(piecewise_fold((1 - x) * p1)) assert p2 == Piecewise((1 - x, Interval(0, 1, False, True).contains(x)), (0, True))
def test_sympyissue_4527(): k, m = symbols('k m', integer=True) assert integrate(sin(k * x) * sin(m * x), (x, 0, pi)) == Piecewise( (0, And(Eq(k, 0), Eq(m, 0))), (-pi / 2, Eq(k, -m)), (pi / 2, Eq(k, m)), (0, True)) assert integrate(sin(k * x) * sin(m * x), (x, )) == Piecewise( (0, And(Eq(k, 0), Eq(m, 0))), (-x * sin(m * x)**2 / 2 - x * cos(m * x)**2 / 2 + sin(m * x) * cos(m * x) / (2 * m), Eq(k, -m)), (x * sin(m * x)**2 / 2 + x * cos(m * x)**2 / 2 - sin(m * x) * cos(m * x) / (2 * m), Eq(k, m)), (m * sin(k * x) * cos(m * x) / (k**2 - m**2) - k * sin(m * x) * cos(k * x) / (k**2 - m**2), True))
def test_Piecewise(): e1 = x * (x + y) - y * (x + y) e2 = sin(x)**2 + cos(x)**2 e3 = expand((x + y) * y / x) s1 = simplify(e1) s2 = simplify(e2) s3 = simplify(e3) assert simplify(Piecewise((e1, x < e2), (e3, True))) == \ Piecewise((s1, x < s2), (s3, True)) # trigsimp tries not to touch non-trig containing args assert trigsimp(Piecewise((e1, e3 < e2), (e3, True))) == \ Piecewise((e1, e3 < s2), (e3, True))
def test_basic_degree_1(): d = 1 knots = range(5) splines = bspline_basis_set(d, knots, x) assert splines[0] == Piecewise( (x, Interval(0, 1, False, True).contains(x)), (2 - x, Interval(1, 2).contains(x)), (0, True)) assert splines[1] == Piecewise( (-1 + x, Interval(1, 2, False, True).contains(x)), (3 - x, Interval(2, 3).contains(x)), (0, True)) assert splines[2] == Piecewise( (-2 + x, Interval(2, 3, False, True).contains(x)), (4 - x, Interval(3, 4).contains(x)), (0, True))
def test_piecewise_integrate_inequality_conditions(): x, y = symbols("x y", real=True) c1, c2 = symbols("c1 c2", positive=True, real=True) g = Piecewise((0, c1*x > 1), (1, c1*x > 0), (0, True)) assert integrate(g, (x, -oo, 0)) == 0 assert integrate(g, (x, -5, 0)) == 0 assert integrate(g, (x, 0, 5)) == Min(5, 1/c1) assert integrate(g, (x, 0, oo)) == 1/c1 g = Piecewise((0, c1*x + c2*y > 1), (1, c1*x + c2*y > 0), (0, True)) assert integrate(g, (x, -oo, 0)).subs(y, 0) == 0 assert integrate(g, (x, -5, 0)).subs(y, 0) == 0 assert integrate(g, (x, 0, 5)).subs(y, 0) == Min(5, 1/c1) assert integrate(g, (x, 0, oo)).subs(y, 0) == 1/c1
def test_numpy_piecewise_regression(): # Test that NumPyPrinter needs to print Piecewise()'s choicelist as a list to # avoid breaking compatibility with numpy 1.8. This is not necessary in # numpy 1.9+. See sympy/sympy#9747 and sympy/sympy#9749 for details. p = Piecewise((1, x < 0), (0, True)) assert NumPyPrinter().doprint( p) == 'select([less(x, 0),True], [1,0], default=nan)'
def test_octave_piecewise_times_const(): pw = Piecewise((x, x < 1), (x**2, True)) assert octave_code(2 * pw) == '2*((x < 1).*(x) + (~(x < 1)).*(x.^2))' assert octave_code(pw / x) == '((x < 1).*(x) + (~(x < 1)).*(x.^2))./x' assert octave_code( pw / (x * y)) == '((x < 1).*(x) + (~(x < 1)).*(x.^2))./(x.*y)' assert octave_code(pw / 3) == '((x < 1).*(x) + (~(x < 1)).*(x.^2))/3'
def test_sympyissue_21202(): res = (Piecewise( (s / (s**2 - 4), (4 * abs(s**-2) < 1) | (abs(s**2) / 4 < 1)), (pi * meijerg(((Rational(1, 2), ), (0, 0)), ((0, Rational(1, 2)), (0, )), s**2 / 4) / 2, True)), 2, Ne(s**2 / 4, 1)) assert laplace_transform(cosh(2 * x), x, s) == res
def test_cdf(): X = Normal('x', 0, 1) d = cdf(X) assert P(X < 1) == d(1) assert d(0) == Rational(1, 2) d = cdf(X, X > 0) # given X>0 assert d(0) == 0 Y = Exponential('y', 10) d = cdf(Y) assert d(-5) == 0 assert P(Y > 3) == 1 - d(3) pytest.raises(ValueError, lambda: cdf(X + Y)) Z = Exponential('z', 1) f = cdf(Z) z = Symbol('z') assert f(z) == Piecewise((1 - exp(-z), z >= 0), (0, True)) U = Uniform('x', 3, 5) u = cdf(U) assert u(z) == z/2 - Rational(3, 2)
def test_basic_degree_0(): d = 0 knots = range(5) splines = bspline_basis_set(d, knots, x) for i in range(len(splines)): assert splines[i] == Piecewise((1, Interval(i, i + 1).contains(x)), (0, True))
def test_conjugate_transpose(): A, B = symbols("A B", commutative=False) p = Piecewise((A * B**2, x > 0), (A**2 * B, True)) assert p.adjoint() == \ Piecewise((adjoint(A*B**2), x > 0), (adjoint(A**2*B), True)) assert p.conjugate() == \ Piecewise((conjugate(A*B**2), x > 0), (conjugate(A**2*B), True)) assert p.transpose() == \ Piecewise((transpose(A*B**2), x > 0), (transpose(A**2*B), True))
def test_fcode_Piecewise(): expr = Piecewise((x, x < 1), (x**2, True)) # Check that inline conditional (merge) fails if standard isn't 95+ pytest.raises(NotImplementedError, lambda: fcode(expr)) code = fcode(expr, standard=95) expected = ' merge(x, x**2, x < 1)' assert code == expected assert fcode(Piecewise((x, x < 1), (x**2, True)), assign_to='var') == ( ' if (x < 1) then\n' ' var = x\n' ' else\n' ' var = x**2\n' ' end if' ) a = cos(x)/x b = sin(x)/x for _ in range(10): a = diff(a, x) b = diff(b, x) expected = ( ' if (x < 0) then\n' ' weird_name = -cos(x)/x + 10*sin(x)/x**2 + 90*cos(x)/x**3 - 720*\n' ' @ sin(x)/x**4 - 5040*cos(x)/x**5 + 30240*sin(x)/x**6 + 151200*cos(x\n' ' @ )/x**7 - 604800*sin(x)/x**8 - 1814400*cos(x)/x**9 + 3628800*sin(x\n' ' @ )/x**10 + 3628800*cos(x)/x**11\n' ' else\n' ' weird_name = -sin(x)/x - 10*cos(x)/x**2 + 90*sin(x)/x**3 + 720*\n' ' @ cos(x)/x**4 - 5040*sin(x)/x**5 - 30240*cos(x)/x**6 + 151200*sin(x\n' ' @ )/x**7 + 604800*cos(x)/x**8 - 1814400*sin(x)/x**9 - 3628800*cos(x\n' ' @ )/x**10 + 3628800*sin(x)/x**11\n' ' end if' ) code = fcode(Piecewise((a, x < 0), (b, True)), assign_to='weird_name') assert code == expected code = fcode(Piecewise((x, x < 1), (x**2, x > 1), (sin(x), True)), standard=95) expected = ' merge(x, merge(x**2, sin(x), x > 1), x < 1)' assert code == expected assert (fcode(Piecewise((0, x < -1), (1, And(x >= -1, x < 0)), (-1, True)), assign_to='var') == ' if (x < -1) then\n' ' var = 0\n' ' else if (x >= -1 .and. x < 0) then\n' ' var = 1\n' ' else\n' ' var = -1\n' ' end if')
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_raised_cosine(): mu = Symbol("mu", extended_real=True) s = Symbol("s", positive=True) X = RaisedCosine("x", mu, s) assert density(X)(x) == (Piecewise( ((cos(pi * (x - mu) / s) + 1) / (2 * s), And(x <= mu + s, mu - s <= x)), (0, True)))
def test_quadratic_u(): a = Symbol("a", extended_real=True) b = Symbol("b", extended_real=True) X = QuadraticU("x", a, b) assert density(X)(x) == (Piecewise( (12 * (x - a / 2 - b / 2)**2 / (-a + b)**3, And(x <= b, a <= x)), (0, True)))
def test_raised_cosine(): mu = Symbol('mu', extended_real=True) s = Symbol('s', positive=True) X = RaisedCosine('x', mu, s) assert density(X)(x) == (Piecewise(((cos(pi*(x - mu)/s) + 1)/(2*s), And(x <= mu + s, mu - s <= x)), (0, True))) assert X.pspace.domain.set == Interval(mu - s, mu + s)
def test_quadratic_u(): a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) X = QuadraticU('x', a, b) assert density(X)(x) == (Piecewise((12*(x - a/2 - b/2)**2/(-a + b)**3, And(x <= b, a <= x)), (0, True))) assert X.pspace.domain.set == Interval(a, b)
def test_rewrite(): x = Symbol('x', extended_real=True) assert Heaviside(x).rewrite(Piecewise) == \ Piecewise((1, x > 0), (Rational(1, 2), Eq(x, 0)), (0, True)) assert Heaviside(y).rewrite(Piecewise) == Heaviside(y) assert Heaviside(x).rewrite(sign) == (sign(x)+1)/2 assert Heaviside(y).rewrite(sign) == Heaviside(y)
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_piecewise_collapse(): p1 = Piecewise((x, x < 0), (x**2, x > 1)) p2 = Piecewise((p1, x < 0), (p1, x > 1)) assert p2 == Piecewise((x, x < 0), (x**2, 1 < x)) p1 = Piecewise((Piecewise((x, x < 0), (1, True)), True)) assert p1 == Piecewise((Piecewise((x, x < 0), (1, True)), True))
def test_piecewise_interval(): p1 = Piecewise((x, Interval(0, 1).contains(x)), (0, True)) assert p1.subs(x, -0.5) == 0 assert p1.subs(x, 0.5) == 0.5 assert p1.diff(x) == Piecewise((1, Interval(0, 1).contains(x)), (0, True)) assert integrate(p1, x) == Piecewise( (x**2 / 2, Interval(0, 1).contains(x)), (0, True))
def test_trigintegrate_odd(): assert trigintegrate(Integer(1), x) == x assert trigintegrate(x, x) is None assert trigintegrate(x**2, x) is None assert trigintegrate(sin(x), x) == -cos(x) assert trigintegrate(cos(x), x) == sin(x) assert trigintegrate(sin(3 * x), x) == -cos(3 * x) / 3 assert trigintegrate(cos(3 * x), x) == sin(3 * x) / 3 y = Symbol('y') assert trigintegrate(sin(y*x), x) == \ Piecewise((0, Eq(y, 0)), (-cos(y*x)/y, True)) assert trigintegrate(cos(y*x), x) == \ Piecewise((x, Eq(y, 0)), (sin(y*x)/y, True)) assert trigintegrate(sin(y*x)**2, x) == \ Piecewise((0, Eq(y, 0)), ((x*y/2 - sin(x*y)*cos(x*y)/2)/y, True)) assert trigintegrate(sin(y*x)**2, x, conds='none') == \ (x*y/2 - sin(x*y)*cos(x*y)/2)/y assert trigintegrate(sin(y*x)*cos(y*x), x) == \ Piecewise((0, Eq(y, 0)), (sin(x*y)**2/(2*y), True)) assert trigintegrate(cos(y*x)**2, x) == \ Piecewise((x, Eq(y, 0)), ((x*y/2 + sin(x*y)*cos(x*y)/2)/y, True)) y = Symbol('y', positive=True) # TODO: remove conds='none' below. For this to work we would have to rule # out (e.g. by trying solve) the condition y = 0, incompatible with # y.is_positive being True. assert trigintegrate(sin(y * x), x, conds='none') == -cos(y * x) / y assert trigintegrate(cos(y * x), x, conds='none') == sin(y * x) / y assert trigintegrate(sin(x) * cos(x), x) == sin(x)**2 / 2 assert trigintegrate(sin(x) * cos(x)**2, x) == -cos(x)**3 / 3 assert trigintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3 # check if it selects right function to substitute, # so the result is kept simple assert trigintegrate(sin(x)**7 * cos(x), x) == sin(x)**8 / 8 assert trigintegrate(sin(x) * cos(x)**7, x) == -cos(x)**8 / 8 assert trigintegrate(sin(x)**7 * cos(x)**3, x) == \ -sin(x)**10/10 + sin(x)**8/8 assert trigintegrate(sin(x)**3 * cos(x)**7, x) == \ cos(x)**10/10 - cos(x)**8/8
def test_integrate_hyperexponential_returns_piecewise(): a, b = symbols('a b') DE = DifferentialExtension(a**x, x) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (x, Eq(log(a), 0)), (exp(x * log(a)) / log(a), True)), 0, True) DE = DifferentialExtension(a**(b * x), x) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (x, Eq(b * log(a), 0)), (exp(b * x * log(a)) / (b * log(a)), True)), 0, True) DE = DifferentialExtension(exp(a * x), x) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (x, Eq(a, 0)), (exp(a * x) / a, True)), 0, True) DE = DifferentialExtension(x * exp(a * x), x) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (x**2 / 2, Eq(a**3, 0)), ((x * a**2 - a) * exp(a * x) / a**3, True)), 0, True) DE = DifferentialExtension(x**2 * exp(a * x), x) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (x**3 / 3, Eq(a**6, 0)), ((x**2 * a**5 - 2 * x * a**4 + 2 * a**3) * exp(a * x) / a**6, True)), 0, True) DE = DifferentialExtension(x**y + z, y) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (y, Eq(log(x), 0)), (exp(log(x) * y) / log(x), True)), z, True) DE = DifferentialExtension(x**y + z + x**(2 * y), y) assert integrate_hyperexponential(DE.fa, DE.fd, DE) == (Piecewise( (2 * y, Eq(2 * log(x)**2, 0)), ((exp(2 * log(x) * y) * log(x) + 2 * exp(log(x) * y) * log(x)) / (2 * log(x)**2), True)), z, True)
def test_basic_degree_2(): d = 2 knots = range(5) splines = bspline_basis_set(d, knots, x) b0 = Piecewise( (x**2 / 2, Interval(0, 1, False, True).contains(x)), (Rational(-3, 2) + 3 * x - x**2, Interval(1, 2, False, True).contains(x)), (Rational(9, 2) - 3 * x + x**2 / 2, Interval(2, 3).contains(x)), (0, True)) b1 = Piecewise((Rational(1, 2) - x + x**2 / 2, Interval(1, 2, False, True).contains(x)), (Rational(-11, 2) + 5 * x - x**2, Interval( 2, 3, False, True).contains(x)), (8 - 4 * x + x**2 / 2, Interval(3, 4).contains(x)), (0, True)) assert splines[0] == b0 assert splines[1] == b1
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 = summation(binomial_dist * k, (k, 0, n)) assert s.simplify() == 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_piecewise_lambdify(): p = Piecewise((x**2, x < 0), (x, Interval(0, 1, False, True).contains(x)), (2 - x, x >= 1), (0, True)) f = lambdify(x, p) assert f(-2.0) == 4.0 assert f(0.0) == 0.0 assert f(0.5) == 0.5 assert f(2.0) == 0.0
def test_matrix(): A = Matrix([[x, y], [y * x, z**2]]) assert lambdarepr(A) == "MutableDenseMatrix([[x, y], [x*y, z**2]])" # Test printing a Matrix that has an element that is printed differently # with the LambdaPrinter than in the StrPrinter. p = Piecewise((x, True), evaluate=False) A = Matrix([p]) assert lambdarepr(A) == "MutableDenseMatrix([[((x) if (True) else None)]])"
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)) 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 p7 assert Piecewise((1, And(Not(x < 1), x < 0)), (0, True))
def test_basic4(): assert limit(2*x + y*x, x, 0) == 0 assert limit(2*x + y*x, x, 1) == 2 + y assert limit(2*x**8 + y*x**(-3), x, -2) == 512 - y/8 assert limit(sqrt(x + 1) - sqrt(x), x, oo) == 0 assert integrate(1/(x**3 + 1), (x, 0, oo)) == 2*pi*sqrt(3)/9 # coverage test l = Limit(Piecewise((x, x > 1), (0, True)), x, -1) assert l.doit() == l
def test_piecewise_complex(): p1 = Piecewise((2, x < 0), (1, 0 <= x)) p2 = Piecewise((2*I, x < 0), (I, 0 <= x)) p3 = Piecewise((I*x, x > 1), (1 + I, True)) p4 = Piecewise((-I*conjugate(x), x > 1), (1 - I, True)) assert conjugate(p1) == p1 assert conjugate(p2) == piecewise_fold(-p2) assert conjugate(p3) == p4 assert p1.is_imaginary is False assert p1.is_extended_real is True assert p2.is_imaginary is True assert p2.is_extended_real is False assert p3.is_imaginary is None assert p3.is_extended_real is None assert p1.as_real_imag() == (p1, 0) assert p2.as_real_imag() == (0, -I*p2)
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)) == 2 * 2**(b + 1) - 4 # issue sympy/sympy#7097 assert sum(x**n / n for n in range(1, 401)) == summation(x**n / n, (n, 1, 400)) # 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)) == summation(1 / (n**3 - 1), (n, -oo, -2)) # issue sympy/sympy#11642: assert summation(0.5**n, (n, 1, oo)) == Float('1.0', dps=15)
def test_piecewise_simplify(): p = Piecewise(((x**2 + 1)/x**2, Eq(x*(1 + x) - x**2, 0)), ((-1)**x*(-1), True)) assert p.simplify() == \ Piecewise((1 + 1/x**2, Eq(x, 0)), ((-1)**(x + 1), True))
def test_doit(): p1 = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x)) p2 = Piecewise((x, x < 1), (Integral(2 * x), -1 <= x), (x, 3 < x)) assert p2.doit() == p1 assert p2.doit(deep=False) == p2
def test_piecewise(): # Test canonization assert Piecewise((x, x < 1), (0, True)) == Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, True), (1, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, False), (-1, 1 > 2)) == \ Piecewise((x, x < 1)) assert Piecewise((x, x < 1), (0, x < 1), (0, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (0, x < 2), (0, True)) == \ Piecewise((x, x < 1), (0, True)) assert Piecewise((x, x < 1), (x, x < 2), (0, True)) == \ Piecewise((x, Or(x < 1, x < 2)), (0, True)) assert Piecewise((x, x < 1), (x, x < 2), (x, True)) == x assert Piecewise((x, True)) == x pytest.raises(TypeError, lambda: Piecewise(x)) pytest.raises(TypeError, lambda: Piecewise((x, x**2))) assert Piecewise((0, Eq(z, 0, evaluate=False)), (1, True)) == 1 # Test subs p = Piecewise((-1, x < -1), (x**2, x < 0), (log(x), x >= 0)) p_x2 = Piecewise((-1, x**2 < -1), (x**4, x**2 < 0), (log(x**2), x**2 >= 0)) assert p.subs({x: x**2}) == p_x2 assert p.subs({x: -5}) == -1 assert p.subs({x: -1}) == 1 assert p.subs({x: 1}) == log(1) # More subs tests p2 = Piecewise((1, x < pi), (-1, x < 2*pi), (0, x > 2*pi)) p3 = Piecewise((1, Eq(x, 0)), (1/x, True)) p4 = Piecewise((1, Eq(x, 0)), (2, 1/x > 2)) assert p2.subs({x: 2}) == 1 assert p2.subs({x: 4}) == -1 assert p2.subs({x: 10}) == 0 assert p3.subs({x: 0.0}) == 1 assert p4.subs({x: 0.0}) == 1 f, g, h = symbols('f,g,h', cls=Function) pf = Piecewise((f(x), x < -1), (f(x) + h(x) + 2, x <= 1)) pg = Piecewise((g(x), x < -1), (g(x) + h(x) + 2, x <= 1)) assert pg.subs({g: f}) == pf assert Piecewise((1, Eq(x, 0)), (0, True)).subs({x: 0}) == 1 assert Piecewise((1, Eq(x, 0)), (0, True)).subs({x: 1}) == 0 assert Piecewise((1, Eq(x, y)), (0, True)).subs({x: y}) == 1 assert Piecewise((1, Eq(x, z)), (0, True)).subs({x: z}) == 1 assert Piecewise((1, Eq(exp(x), cos(z))), (0, True)).subs({x: z}) == \ Piecewise((1, Eq(exp(z), cos(z))), (0, True)) assert Piecewise((1, Eq(x, y*(y + 1))), (0, True)).subs({x: y**2 + y}).simplify() == 1 p5 = Piecewise( (0, Eq(cos(x) + y, 0)), (1, True)) assert p5.subs({y: 0}) == Piecewise( (0, Eq(cos(x), 0)), (1, True)) # Test evalf assert p.evalf() == p assert p.evalf(subs={x: -2}) == -1 assert p.evalf(subs={x: -1}) == 1 assert p.evalf(subs={x: 1}) == log(1) # Test doit f_int = Piecewise((Integral(x, (x, 0, 1)), x < 1)) assert f_int.doit() == Piecewise( (1.0/2.0, x < 1) ) # Test differentiation f = x fp = x*p dp = Piecewise((0, x < -1), (2*x, x < 0), (1/x, x >= 0)) fp_dx = x*dp + p assert diff(p, x) == dp assert diff(f*p, x) == fp_dx # Test simple arithmetic assert x*p == fp assert x*p + p == p + x*p assert p + f == f + p assert p + dp == dp + p assert p - dp == -(dp - p) # Test power dp2 = Piecewise((0, x < -1), (4*x**2, x < 0), (1/x**2, x >= 0)) assert dp**2 == dp2 # Test _eval_interval f1 = x*y + 2 f2 = x*y**2 + 3 peval = Piecewise( (f1, x < 0), (f2, x > 0)) peval_interval = (f1.subs({x: 0}) - f1.subs({x: -1}) + f2.subs({x: 1}) - f2.subs({x: 0})) assert peval._eval_interval(x, 0, 0) == 0 assert peval._eval_interval(x, -1, 1) == peval_interval peval2 = Piecewise((f1, x < 0), (f2, True)) assert peval2._eval_interval(x, 0, 0) == 0 assert peval2._eval_interval(x, 1, -1) == -peval_interval assert peval2._eval_interval(x, -1, -2) == (f1.subs({x: -2}) - f1.subs({x: -1})) assert peval2._eval_interval(x, -1, 1) == peval_interval assert peval2._eval_interval(x, None, 0) == peval2.subs({x: 0}) assert peval2._eval_interval(x, -1, None) == -peval2.subs({x: -1}) # Test integration p_int = Piecewise((-x, x < -1), (x**3/3.0, x < 0), (-x + x*log(x), x >= 0)) assert integrate(p, x) == p_int p = Piecewise((x, x < 1), (x**2, -1 <= x), (x, 3 < x)) assert integrate(p, (x, -2, 2)) == 5.0/6.0 assert integrate(p, (x, 2, -2)) == -5.0/6.0 p = Piecewise((0, x < 0), (1, x < 1), (0, x < 2), (1, x < 3), (0, True)) assert integrate(p, (x, -oo, oo)) == 2 p = Piecewise((x, x < -10), (x**2, x <= -1), (x, 1 < x)) pytest.raises(ValueError, lambda: integrate(p, (x, -2, 2))) # Test commutativity assert p.is_commutative is True
def test_sympyissue_6171(): e = Piecewise((0, x < 0), (1, True)) assert e.limit(x, 0) == 1 assert e.limit(x, 0, "-") == 0
def test_piecewise_as_leading_term(): p1 = Piecewise((1/x, x > 1), (0, True)) p2 = Piecewise((x, x > 1), (0, True)) p3 = Piecewise((1/x, x > 1), (x, True)) p4 = Piecewise((x, x > 1), (1/x, True)) p5 = Piecewise((1/x, x > 1), (x, True)) p6 = Piecewise((1/x, x < 1), (x, True)) p7 = Piecewise((x, x < 1), (1/x, True)) p8 = Piecewise((x, x > 1), (1/x, True)) assert p1.as_leading_term(x) == 0 assert p2.as_leading_term(x) == 0 assert p3.as_leading_term(x) == x assert p4.as_leading_term(x) == 1/x assert p5.as_leading_term(x) == x assert p6.as_leading_term(x) == 1/x assert p7.as_leading_term(x) == x assert p8.as_leading_term(x) == 1/x
def test_Function_subs(): f, g, h, i = symbols('f g h i', cls=Function) p = Piecewise((g(f(x, y)), x < -1), (g(x), x <= 1)) assert p.subs({g: h}) == Piecewise((h(f(x, y)), x < -1), (h(x), x <= 1)) assert (f(y) + g(x)).subs({f: h, g: i}) == i(x) + h(y)
def test_sympyissue_6807(): p = Piecewise( (0, x < -1), (y, x <= 1), (t, True)) assert p.atoms(Symbol) == {x, y, t}