def test_integrate_DiracDelta(): # This is here to check that deltaintegrate is being called, but also # to test definite integrals. More tests are in test_deltafunctions.py assert integrate(DiracDelta(x) * f(x), (x, -oo, oo)) == f(0) assert integrate(DiracDelta(x) * f(x), (x, 0, oo)) == f(0)/2 assert integrate(DiracDelta(x)**2, (x, -oo, oo)) == DiracDelta(0) # issue sympy/sympy#4522 assert integrate(integrate((4 - 4*x + x*y - 4*y) * DiracDelta(x)*DiracDelta(y - 1), (x, 0, 1)), (y, 0, 1)) == 0 # issue sympy/sympy#5729 p = exp(-(x**2 + y**2))/pi assert integrate(p*DiracDelta(x - 10*y), (x, -oo, oo), (y, -oo, oo)) == \ integrate(p*DiracDelta(x - 10*y), (y, -oo, oo), (x, -oo, oo)) == \ integrate(p*DiracDelta(10*x - y), (x, -oo, oo), (y, -oo, oo)) == \ integrate(p*DiracDelta(10*x - y), (y, -oo, oo), (x, -oo, oo)) == \ 1/sqrt(101*pi)
def test_heaviside(): x, y = symbols('x, y', extended_real=True) z = Symbol('z') assert Heaviside(0) == 0.5 assert Heaviside(-5) == 0 assert Heaviside(1) == 1 assert Heaviside(nan) == nan assert Heaviside(x).is_real assert Heaviside(z).is_real is None assert adjoint(Heaviside(x)) == Heaviside(x) assert adjoint(Heaviside(x - y)) == Heaviside(x - y) assert conjugate(Heaviside(x)) == Heaviside(x) assert conjugate(Heaviside(x - y)) == Heaviside(x - y) assert transpose(Heaviside(x)) == Heaviside(x) assert transpose(Heaviside(x - y)) == Heaviside(x - y) assert Heaviside(x).diff(x) == DiracDelta(x) assert Heaviside(z + I).is_Function is True assert Heaviside(I * z).is_Function is True pytest.raises(ArgumentIndexError, lambda: Heaviside(x).fdiff(2)) pytest.raises(ValueError, lambda: Heaviside(I)) pytest.raises(ValueError, lambda: Heaviside(2 + 3 * I))
def doit(self, **kwargs): evaluate = kwargs.pop('evaluate', True) expr, condition = self.expr, self.condition if condition is not None: # Recompute on new conditional expr expr = given(expr, condition, **kwargs) if not random_symbols(expr): return Lambda(x, DiracDelta(x - expr)) if (isinstance(expr, RandomSymbol) and hasattr(expr.pspace, 'distribution') and isinstance(pspace(expr), SinglePSpace)): return expr.pspace.distribution result = pspace(expr).compute_density(expr, **kwargs) if evaluate and hasattr(result, 'doit'): return result.doit() else: return result
def test_sign(): assert sign(1.2) == 1 assert sign(-1.2) == -1 assert sign(3*I) == I assert sign(-3*I) == -I assert sign(0) == 0 assert sign(nan) == nan assert sign(2 + 2*I).doit() == sqrt(2)*(2 + 2*I)/4 assert sign(2 + 3*I).simplify() == sign(2 + 3*I) assert sign(2 + 2*I).simplify() == sign(1 + I) assert sign(im(sqrt(1 - sqrt(3)))) == 1 assert sign(sqrt(1 - sqrt(3))) == I x = Symbol('x') assert sign(x).is_finite is True assert sign(x).is_complex is True assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).doit() == sign(x) assert sign(1.2*x) == sign(x) assert sign(2*x) == sign(x) assert sign(I*x) == I*sign(x) assert sign(-2*I*x) == -I*sign(x) assert sign(conjugate(x)) == conjugate(sign(x)) p = Symbol('p', positive=True) n = Symbol('n', negative=True) m = Symbol('m', negative=True) assert sign(2*p*x) == sign(x) assert sign(n*x) == -sign(x) assert sign(n*m*x) == sign(x) x = Symbol('x', imaginary=True) xn = Symbol('xn', imaginary=True, nonzero=True) assert sign(x).is_imaginary is True assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is None assert sign(x).diff(x) == 2*DiracDelta(-I*x) assert sign(xn).doit() == xn / Abs(xn) assert conjugate(sign(x)) == -sign(x) x = Symbol('x', extended_real=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is None assert sign(x).diff(x) == 2*DiracDelta(x) assert sign(x).doit() == sign(x) assert conjugate(sign(x)) == sign(x) assert sign(sin(x)).nseries(x) == 1 y = Symbol('y') assert sign(x*y).nseries(x).removeO() == sign(y) x = Symbol('x', nonzero=True) assert sign(x).is_imaginary is None assert sign(x).is_integer is None assert sign(x).is_extended_real is None assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = Symbol('x', positive=True) assert sign(x).is_imaginary is False assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is False assert sign(x).doit() == x / Abs(x) assert sign(Abs(x)) == 1 assert Abs(sign(x)) == 1 x = 0 assert sign(x).is_imaginary is True assert sign(x).is_integer is True assert sign(x).is_extended_real is True assert sign(x).is_zero is True assert sign(x).doit() == 0 assert sign(Abs(x)) == 0 assert Abs(sign(x)) == 0 nz = Symbol('nz', nonzero=True, integer=True) assert sign(nz).is_imaginary is False assert sign(nz).is_integer is True assert sign(nz).is_extended_real is True assert sign(nz).is_zero is False assert sign(nz)**2 == 1 assert (sign(nz)**3).args == (sign(nz), 3) assert sign(Symbol('x', nonnegative=True)).is_nonnegative assert sign(Symbol('x', nonnegative=True)).is_nonpositive is None assert sign(Symbol('x', nonpositive=True)).is_nonnegative is None assert sign(Symbol('x', nonpositive=True)).is_nonpositive assert sign(Symbol('x', extended_real=True)).is_nonnegative is None assert sign(Symbol('x', extended_real=True)).is_nonpositive is None assert sign(Symbol('x', extended_real=True, zero=False)).is_nonpositive is None x, y = Symbol('x', extended_real=True), Symbol('y') assert sign(x).rewrite(Piecewise) == \ Piecewise((1, x > 0), (-1, x < 0), (0, True)) assert sign(y).rewrite(Piecewise) == sign(y) assert sign(x).rewrite(Heaviside) == 2*Heaviside(x)-1 assert sign(y).rewrite(Heaviside) == sign(y) # evaluate what can be evaluated assert sign(exp_polar(I*pi)*pi) is Integer(-1) eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) # if there is a fast way to know when and when you cannot prove an # expression like this is zero then the equality to zero is ok assert sign(eq) == 0 q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6) p = cbrt(expand(q**3)) d = p - q assert sign(d) == 0 assert abs(sign(z)) == Abs(sign(z), evaluate=False)
def test_density_constant(): assert density(3)(2) == 0 assert density(3)(3) == DiracDelta(0)
def test_DiracDelta(): i = Symbol('i', nonzero=True) j = Symbol('j', positive=True) k = Symbol('k', negative=True) assert DiracDelta(1) == 0 assert DiracDelta(5.1) == 0 assert DiracDelta(-pi) == 0 assert DiracDelta(5, 7) == 0 assert DiracDelta(i) == 0 assert DiracDelta(j) == 0 assert DiracDelta(k) == 0 assert DiracDelta(nan) == nan assert isinstance(DiracDelta(0), DiracDelta) assert isinstance(DiracDelta(x), DiracDelta) assert adjoint(DiracDelta(x)) == DiracDelta(x) assert adjoint(DiracDelta(x - y)) == DiracDelta(x - y) assert conjugate(DiracDelta(x)) == DiracDelta(x) assert conjugate(DiracDelta(x - y)) == DiracDelta(x - y) assert transpose(DiracDelta(x)) == DiracDelta(x) assert transpose(DiracDelta(x - y)) == DiracDelta(x - y) assert DiracDelta(x).diff(x) == DiracDelta(x, 1) assert DiracDelta(x, 1).diff(x) == DiracDelta(x, 2) assert DiracDelta(x).is_simple(x) is True assert DiracDelta(3*x).is_simple(x) is True assert DiracDelta(x**2).is_simple(x) is False assert DiracDelta(sqrt(x)).is_simple(x) is False assert DiracDelta(x).is_simple(y) is False assert DiracDelta(x*y).simplify(x) == DiracDelta(x)/abs(y) assert DiracDelta(x*y).simplify(y) == DiracDelta(y)/abs(x) assert DiracDelta(x**2*y).simplify(x) == DiracDelta(x**2*y) assert DiracDelta(y).simplify(x) == DiracDelta(y) assert DiracDelta((x - 1)*(x - 2)*(x - 3)).simplify(x) == \ DiracDelta(x - 3)/2 + DiracDelta(x - 2) + DiracDelta(x - 1)/2 pytest.raises(ArgumentIndexError, lambda: DiracDelta(x).fdiff(2)) pytest.raises(ValueError, lambda: DiracDelta(x, -1))
def test_deltaintegrate(): assert deltaintegrate(x, x) is None assert deltaintegrate(x + DiracDelta(x), x) is None assert deltaintegrate(DiracDelta(x, 0), x) == Heaviside(x) for n in range(10): assert deltaintegrate(DiracDelta(x, n + 1), x) == DiracDelta(x, n) assert deltaintegrate(DiracDelta(x), x) == Heaviside(x) assert deltaintegrate(DiracDelta(-x), x) == Heaviside(x) assert deltaintegrate(DiracDelta(x - y), x) == Heaviside(x - y) assert deltaintegrate(DiracDelta(y - x), x) == Heaviside(x - y) assert deltaintegrate(x * DiracDelta(x), x) == 0 assert deltaintegrate((x - y) * DiracDelta(x - y), x) == 0 assert deltaintegrate(DiracDelta(x)**2, x) == DiracDelta(0) * Heaviside(x) assert deltaintegrate(y*DiracDelta(x)**2, x) == \ y*DiracDelta(0)*Heaviside(x) assert deltaintegrate(DiracDelta(x, 1)**2, x) is None assert deltaintegrate(y * DiracDelta(x, 1)**2, x) is None assert deltaintegrate(DiracDelta(x) * f(x), x) == f(0) * Heaviside(x) assert deltaintegrate(DiracDelta(-x) * f(x), x) == f(0) * Heaviside(x) assert deltaintegrate(DiracDelta(x - 1) * f(x), x) == f(1) * Heaviside(x - 1) assert deltaintegrate(DiracDelta(1 - x) * f(x), x) == f(1) * Heaviside(x - 1) assert deltaintegrate(DiracDelta(x**2 + x - 2), x) == \ Heaviside(x - 1)/3 + Heaviside(x + 2)/3 p = cos(x) * (DiracDelta(x) + DiracDelta(x**2 - 1)) * sin(x) * (x - pi) assert deltaintegrate( p, x) - (-pi * (cos(1) * Heaviside(-1 + x) * sin(1) / 2 - cos(1) * Heaviside(1 + x) * sin(1) / 2) + cos(1) * Heaviside(1 + x) * sin(1) / 2 + cos(1) * Heaviside(-1 + x) * sin(1) / 2) == 0 p = x_2 * DiracDelta(x - x_2) * DiracDelta(x_2 - x_1) assert deltaintegrate(p, x_2) == x * DiracDelta(x - x_1) * Heaviside(x_2 - x) p = x * y**2 * z * DiracDelta(y - x) * DiracDelta(y - z) * DiracDelta(x - z) assert deltaintegrate( p, y) == x**3 * z * DiracDelta(x - z)**2 * Heaviside(y - x) assert deltaintegrate((x + 1) * DiracDelta(2 * x), x) == Rational(1, 2) * Heaviside(x) assert deltaintegrate((x + 1)*DiracDelta(2*x/3 + 4/Integer(9)), x) == \ Rational(1, 2) * Heaviside(x + Rational(2, 3)) a, b, c = symbols('a b c', commutative=False) assert deltaintegrate(DiracDelta(x - y)*f(x - b)*f(x - a), x) == \ f(y - b)*f(y - a)*Heaviside(x - y) p = f(x - a) * DiracDelta(x - y) * f(x - c) * f(x - b) assert deltaintegrate( p, x) == f(y - a) * f(y - c) * f(y - b) * Heaviside(x - y) p = DiracDelta(x - z) * f(x - b) * f(x - a) * DiracDelta(x - y) assert deltaintegrate(p, x) == DiracDelta(y - z)*f(y - b)*f(y - a) * \ Heaviside(x - y)
def test_change_mul(): assert change_mul(x, x) == x assert change_mul(x * y, x) == (None, None) assert change_mul(x * y * DiracDelta(x), x) == (DiracDelta(x), x * y) assert change_mul(x*y*DiracDelta(x)*DiracDelta(y), x) == \ (DiracDelta(x), x*y*DiracDelta(y)) assert change_mul(DiracDelta(x)**2, x) == \ (DiracDelta(x), DiracDelta(x)) assert change_mul(y*DiracDelta(x)**2, x) == \ (DiracDelta(x), y*DiracDelta(x))
def test_integrate_DiracDelta_fails(): # issue sympy/sympy#6427 assert integrate( integrate(integrate(DiracDelta(x - y - z), (z, 0, oo)), (y, 0, 1)), (x, 0, 1)) == Rational(1, 2)
def test_DiracDelta(): assert DiracDelta(1) == 0 assert DiracDelta(5.1) == 0 assert DiracDelta(-pi) == 0 assert DiracDelta(5, 7) == 0 assert DiracDelta(nan) == nan assert DiracDelta(0).func is DiracDelta assert DiracDelta(x).func is DiracDelta assert adjoint(DiracDelta(x)) == DiracDelta(x) assert adjoint(DiracDelta(x - y)) == DiracDelta(x - y) assert conjugate(DiracDelta(x)) == DiracDelta(x) assert conjugate(DiracDelta(x - y)) == DiracDelta(x - y) assert transpose(DiracDelta(x)) == DiracDelta(x) assert transpose(DiracDelta(x - y)) == DiracDelta(x - y) assert DiracDelta(x).diff(x) == DiracDelta(x, 1) assert DiracDelta(x, 1).diff(x) == DiracDelta(x, 2) assert DiracDelta(x).is_simple(x) is True assert DiracDelta(3 * x).is_simple(x) is True assert DiracDelta(x**2).is_simple(x) is False assert DiracDelta(sqrt(x)).is_simple(x) is False assert DiracDelta(x).is_simple(y) is False assert DiracDelta(x * y).simplify(x) == DiracDelta(x) / abs(y) assert DiracDelta(x * y).simplify(y) == DiracDelta(y) / abs(x) assert DiracDelta(x**2 * y).simplify(x) == DiracDelta(x**2 * y) assert DiracDelta(y).simplify(x) == DiracDelta(y) assert DiracDelta((x - 1)*(x - 2)*(x - 3)).simplify(x) == \ DiracDelta(x - 3)/2 + DiracDelta(x - 2) + DiracDelta(x - 1)/2 pytest.raises(ArgumentIndexError, lambda: DiracDelta(x).fdiff(2)) pytest.raises(ValueError, lambda: DiracDelta(x, -1))