def test_rewrite(): x, y = Symbol('x', real=True), Symbol('y') assert Heaviside(x).rewrite(Piecewise) == ( Piecewise((0, x < 0), (Heaviside(0), Eq(x, 0)), (1, x > 0))) assert Heaviside(y).rewrite(Piecewise) == ( Piecewise((0, y < 0), (Heaviside(0), Eq(y, 0)), (1, y > 0))) assert Heaviside(x, y).rewrite(Piecewise) == ( Piecewise((0, x < 0), (y, Eq(x, 0)), (1, x > 0))) assert Heaviside(x, 0).rewrite(Piecewise) == ( Piecewise((0, x <= 0), (1, x > 0))) assert Heaviside(x, 1).rewrite(Piecewise) == ( Piecewise((0, x < 0), (1, x >= 0))) assert Heaviside(x).rewrite(sign) == (sign(x)+1)/2 assert Heaviside(y).rewrite(sign) == Heaviside(y) assert Heaviside(x, S.Half).rewrite(sign) == (sign(x)+1)/2 assert Heaviside(x, y).rewrite(sign) == Heaviside(x, y) assert DiracDelta(y).rewrite(Piecewise) == Piecewise((DiracDelta(0), Eq(y, 0)), (0, True)) assert DiracDelta(y, 1).rewrite(Piecewise) == DiracDelta(y, 1) assert DiracDelta(x - 5).rewrite(Piecewise) == ( Piecewise((DiracDelta(0), Eq(x - 5, 0)), (0, True))) assert (x*DiracDelta(x - 10)).rewrite(SingularityFunction) == x*SingularityFunction(x, 10, -1) assert 5*x*y*DiracDelta(y, 1).rewrite(SingularityFunction) == 5*x*y*SingularityFunction(y, 0, -2) assert DiracDelta(0).rewrite(SingularityFunction) == SingularityFunction(0, 0, -1) assert DiracDelta(0, 1).rewrite(SingularityFunction) == SingularityFunction(0, 0, -2) assert Heaviside(x).rewrite(SingularityFunction) == SingularityFunction(x, 0, 0) assert 5*x*y*Heaviside(y + 1).rewrite(SingularityFunction) == 5*x*y*SingularityFunction(y, -1, 0) assert ((x - 3)**3*Heaviside(x - 3)).rewrite(SingularityFunction) == (x - 3)**3*SingularityFunction(x, 3, 0) assert Heaviside(0).rewrite(SingularityFunction) == SingularityFunction(0, 0, 0)
def test_rewrite(): assert SingularityFunction(x, 4, 5).rewrite(Piecewise) == ( Piecewise(((x - 4)**5, x - 4 > 0), (0, True))) assert SingularityFunction(x, -10, 0).rewrite(Piecewise) == ( Piecewise((1, x + 10 > 0), (0, True))) assert SingularityFunction(x, 2, -1).rewrite(Piecewise) == ( Piecewise((oo, Eq(x - 2, 0)), (0, True))) assert SingularityFunction(x, 0, -2).rewrite(Piecewise) == ( Piecewise((oo, Eq(x, 0)), (0, True))) n = Symbol('n', nonnegative=True) assert SingularityFunction(x, a, n).rewrite(Piecewise) == ( Piecewise(((x - a)**n, x - a > 0), (0, True))) expr_in = SingularityFunction(x, 4, 5) + SingularityFunction(x, -3, -1) - SingularityFunction(x, 0, -2) expr_out = (x - 4)**5*Heaviside(x - 4) + DiracDelta(x + 3) - DiracDelta(x, 1) assert expr_in.rewrite(Heaviside) == expr_out assert expr_in.rewrite(DiracDelta) == expr_out assert expr_in.rewrite('HeavisideDiracDelta') == expr_out expr_in = SingularityFunction(x, a, n) + SingularityFunction(x, a, -1) - SingularityFunction(x, a, -2) expr_out = (x - a)**n*Heaviside(x - a) + DiracDelta(x - a) - DiracDelta(x - a, 1) assert expr_in.rewrite(Heaviside) == expr_out assert expr_in.rewrite(DiracDelta) == expr_out assert expr_in.rewrite('HeavisideDiracDelta') == expr_out
def test_series(): l = Symbol('l', positive=True) assert SingularityFunction(x, -3, 2).series(x) == x**2 + 6 * x + 9 assert SingularityFunction(x, -2, 1).series(x) == x + 2 assert SingularityFunction(x, 0, 0).series(x) == 1 assert SingularityFunction(x, 0, 0).series(x, dir='-') == 0 assert SingularityFunction(x, 0, -1).series(x) == 0 assert SingularityFunction(x, 0, -2).series(x) == 0 assert (SingularityFunction(x + l, 0, 1)/2\ - SingularityFunction(x + l, l/2, 1)\ + SingularityFunction(x + l, l, 1)/2).nseries(x) == -x/2 + O(x**6)
def test_leading_term(): l = Symbol('l', positive=True) assert SingularityFunction(x, 3, 2).as_leading_term(x) == 0 assert SingularityFunction(x, -2, 1).as_leading_term(x) == 2 assert SingularityFunction(x, 0, 0).as_leading_term(x) == 1 assert SingularityFunction(x, 0, 0).as_leading_term(x, cdir=-1) == 0 assert SingularityFunction(x, 0, -1).as_leading_term(x) == 0 assert SingularityFunction(x, 0, -2).as_leading_term(x) == 0 assert (SingularityFunction(x + l, 0, 1)/2\ - SingularityFunction(x + l, l/2, 1)\ + SingularityFunction(x + l, l, 1)/2).as_leading_term(x) == -x/2
def test_integrate_SingularityFunction(): in_1 = SingularityFunction(x, a, 3) + SingularityFunction(x, 5, -1) out_1 = SingularityFunction(x, a, 4) / 4 + SingularityFunction(x, 5, 0) assert integrate(in_1, x) == out_1 in_2 = 10 * SingularityFunction(x, 4, 0) - 5 * SingularityFunction( x, -6, -2) out_2 = 10 * SingularityFunction(x, 4, 1) - 5 * SingularityFunction( x, -6, -1) assert integrate(in_2, x) == out_2 in_3 = 2 * x**2 * y - 10 * SingularityFunction( x, -4, 7) - 2 * SingularityFunction(y, 10, -2) out_3_1 = 2 * x**3 * y / 3 - 2 * x * SingularityFunction( y, 10, -2) - 5 * SingularityFunction(x, -4, 8) / 4 out_3_2 = x**2 * y**2 - 10 * y * SingularityFunction( x, -4, 7) - 2 * SingularityFunction(y, 10, -1) assert integrate(in_3, x) == out_3_1 assert integrate(in_3, y) == out_3_2 assert Integral(in_3, x) == Integral(in_3, x) assert Integral(in_3, x).doit() == out_3_1 in_4 = 10 * SingularityFunction(x, -4, 7) - 2 * SingularityFunction( x, 10, -2) out_4 = 5 * SingularityFunction(x, -4, 8) / 4 - 2 * SingularityFunction( x, 10, -1) assert integrate(in_4, (x, -oo, x)) == out_4 assert integrate(SingularityFunction(x, 5, -1), x) == SingularityFunction(x, 5, 0) assert integrate(SingularityFunction(x, 0, -1), (x, -oo, oo)) == 1 assert integrate(5 * SingularityFunction(x, 5, -1), (x, -oo, oo)) == 5 assert integrate(SingularityFunction(x, 5, -1) * f(x), (x, -oo, oo)) == f(5)
def test_rewrite(): assert SingularityFunction(x, 4, 5).rewrite(Piecewise) == (Piecewise( ((x - 4)**5, x - 4 > 0), (0, True))) assert SingularityFunction(x, -10, 0).rewrite(Piecewise) == (Piecewise( (1, x + 10 > 0), (0, True))) assert SingularityFunction(x, 2, -1).rewrite(Piecewise) == (Piecewise( (oo, Eq(x - 2, 0)), (0, True))) assert SingularityFunction(x, 0, -2).rewrite(Piecewise) == (Piecewise( (oo, Eq(x, 0)), (0, True))) n = Symbol('n', nonnegative=True) assert SingularityFunction(x, a, n).rewrite(Piecewise) == (Piecewise( ((x - a)**n, x - a > 0), (0, True))) expr_in = SingularityFunction(x, 4, 5) + SingularityFunction( x, -3, -1) - SingularityFunction(x, 0, -2) expr_out = (x - 4)**5 * Heaviside(x - 4) + DiracDelta(x + 3) - DiracDelta( x, 1) assert expr_in.rewrite(Heaviside) == expr_out assert expr_in.rewrite(DiracDelta) == expr_out assert expr_in.rewrite('HeavisideDiracDelta') == expr_out expr_in = SingularityFunction(x, a, n) + SingularityFunction( x, a, -1) - SingularityFunction(x, a, -2) expr_out = (x - a)**n * Heaviside(x - a) + DiracDelta(x - a) + DiracDelta( a - x, 1) assert expr_in.rewrite(Heaviside) == expr_out assert expr_in.rewrite(DiracDelta) == expr_out assert expr_in.rewrite('HeavisideDiracDelta') == expr_out
def test_eval(): assert SingularityFunction(x, a, n).func == SingularityFunction assert SingularityFunction(x, 5, n) == SingularityFunction(x, 5, n) assert SingularityFunction(5, 3, 2) == 4 assert SingularityFunction(3, 5, 1) == 0 assert SingularityFunction(3, 3, 0) == 1 assert SingularityFunction(4, 4, -1) == oo assert SingularityFunction(4, 2, -1) == 0 assert SingularityFunction(4, 7, -1) == 0 assert SingularityFunction(5, 6, -2) == 0 assert SingularityFunction(4, 2, -2) == 0 assert SingularityFunction(4, 4, -2) == oo assert (SingularityFunction(6.1, 4, 5)).evalf(5) == Float('40.841', '5') assert SingularityFunction(6.1, pi, 2) == (-pi + 6.1)**2 assert SingularityFunction(x, a, nan) == nan assert SingularityFunction(x, nan, 1) == nan assert SingularityFunction(nan, a, n) == nan raises(ValueError, lambda: SingularityFunction(x, a, I)) raises(ValueError, lambda: SingularityFunction(2 * I, I, n)) raises(ValueError, lambda: SingularityFunction(x, a, -3))
def test_fdiff(): assert SingularityFunction(x, 4, 5).fdiff() == 5 * SingularityFunction(x, 4, 4) assert SingularityFunction(x, 4, -1).fdiff() == SingularityFunction(x, 4, -2) assert SingularityFunction(x, 4, 0).fdiff() == SingularityFunction(x, 4, -1) assert SingularityFunction(y, 6, 2).diff(y) == 2 * SingularityFunction(y, 6, 1) assert SingularityFunction(y, -4, -1).diff(y) == SingularityFunction(y, -4, -2) assert SingularityFunction(y, 4, 0).diff(y) == SingularityFunction(y, 4, -1) assert SingularityFunction(y, 4, 0).diff(y, 2) == SingularityFunction(y, 4, -2) n = Symbol('n', positive=True) assert SingularityFunction( x, a, n).fdiff() == n * SingularityFunction(x, a, n - 1) assert SingularityFunction( y, a, n).diff(y) == n * SingularityFunction(y, a, n - 1) expr_in = 4 * SingularityFunction(x, a, n) + 3 * SingularityFunction( x, a, -1) + -10 * SingularityFunction(x, a, 0) expr_out = n * 4 * SingularityFunction( x, a, n - 1) + 3 * SingularityFunction( x, a, -2) - 10 * SingularityFunction(x, a, -1) assert diff(expr_in, x) == expr_out assert SingularityFunction(x, -10, 5).diff(evaluate=False) == (Derivative( SingularityFunction(x, -10, 5), x)) raises(ArgumentIndexError, lambda: SingularityFunction(x, 4, 5).fdiff(2))
def test_singularityintegrate(): assert singularityintegrate(x, x) is None assert singularityintegrate(x + SingularityFunction(x, 9, 1), x) is None assert 4 * singularityintegrate(SingularityFunction(x, a, 3), x) == 4 * SingularityFunction(x, a, 4) / 4 assert singularityintegrate(5 * SingularityFunction(x, 5, -2), x) == 5 * SingularityFunction(x, 5, -1) assert singularityintegrate(6 * SingularityFunction(x, 5, -1), x) == 6 * SingularityFunction(x, 5, 0) assert singularityintegrate(x * SingularityFunction(x, 0, -1), x) == 0 assert singularityintegrate((x - 5) * SingularityFunction(x, 5, -1), x) == 0 assert singularityintegrate(SingularityFunction(x, 0, -1) * f(x), x) == f(0) * SingularityFunction(x, 0, 0) assert singularityintegrate(SingularityFunction(x, 1, -1) * f(x), x) == f(1) * SingularityFunction(x, 1, 0) assert singularityintegrate(y*SingularityFunction(x, 0, -1)**2, x) == \ y*SingularityFunction(0, 0, -1)*SingularityFunction(x, 0, 0)