Beispiel #1
0
def test_factor_expand_subs():
    # test factoring
    assert Sum(4 * x, (x, 1, y)).factor() == 4 * Sum(x, (x, 1, y))
    assert Sum(x * a, (x, 1, y)).factor() == a * Sum(x, (x, 1, y))
    assert Sum(4 * x * a, (x, 1, y)).factor() == 4 * a * Sum(x, (x, 1, y))
    assert Sum(4 * x * y, (x, 1, y)).factor() == 4 * y * Sum(x, (x, 1, y))

    # test expand
    assert Sum(x + 1,
               (x, 1, y)).expand() == Sum(x, (x, 1, y)) + Sum(1, (x, 1, y))
    assert Sum(
        x + a * x**2,
        (x, 1, y)).expand() == Sum(x, (x, 1, y)) + Sum(a * x**2, (x, 1, y))
    assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand() \
        == Sum(x*x**n, (n, -1, oo)) + Sum(n*x*x**n, (n, -1, oo))
    assert Sum(x**(n + 1)*(n + 1), (n, -1, oo)).expand(power_exp=False) \
        == Sum(n*x**(n+1), (n, -1, oo)) + Sum(x**(n+1), (n, -1, oo))
    assert Sum(a*n+a*n**2,(n,0,4)).expand() \
        == Sum(a*n,(n,0,4)) + Sum(a*n**2,(n,0,4))
    assert Sum(x**a*x**n,(x,0,3)) \
        == Sum(x**(a+n),(x,0,3)).expand(power_exp=True)
    assert Sum(x**(a+n),(x,0,3)) \
        == Sum(x**(a+n),(x,0,3)).expand(power_exp=False)

    # test subs
    assert Sum(1 / (1 + a * x**2),
               (x, 0, 3)).subs([(a, 3)]) == Sum(1 / (1 + 3 * x**2), (x, 0, 3))
    assert Sum(x * y, (x, 0, y),
               (y, 0, x)).subs([(x, 3)]) == Sum(x * y, (x, 0, y), (y, 0, 3))
    assert Sum(x, (x, 1, 10)).subs([(x, y - 2)]) == Sum(x, (x, 1, 10))
    assert Sum(1 / x,
               (x, 1, 10)).subs([(x, (3 + n)**3)]) == Sum(1 / x, (x, 1, 10))
    assert Sum(1 / x,
               (x, 1, 10)).subs([(x, 3 * x - 2)]) == Sum(1 / x, (x, 1, 10))
Beispiel #2
0
def test_evalf_issue_3273():
    assert Sum(0, (k, 1, oo)).evalf() == 0
Beispiel #3
0
def test_Sum_doit():
    assert Sum(n * Integral(a**2), (n, 0, 2)).doit() == a**3
    assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \
        3*Integral(a**2)
    assert summation(n * Integral(a**2), (n, 0, 2)) == 3 * Integral(a**2)

    # test nested sum evaluation
    s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n))
    assert 0 == (s.doit() - n * (n + 1) * (n - 1)).factor()

    assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3
    assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \
        3*Piecewise((1, And(S(1) <= k, k <= 3)), (0, True))
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \
        f(1) + f(2) + f(3)
    assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \
        Sum(Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo))
    l = Symbol('l', integer=True, positive=True)
    assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \
        Sum(f(l), (l, 1, oo))

    # issue 2597
    nmax = symbols('N', integer=True, positive=True)
    pw = Piecewise((1, And(S(1) <= n, n <= nmax)), (0, True))
    assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
Beispiel #4
0
def test_harmonic_sums():
    assert summation(1 / k, (k, 0, n)) == Sum(1 / k, (k, 0, n))
    assert summation(1 / k, (k, 1, n)) == harmonic(n)
    assert summation(n / k, (k, 1, n)) == n * harmonic(n)
    assert summation(1 / k, (k, 5, n)) == harmonic(n) - harmonic(4)
Beispiel #5
0
 def check_exact(f, a, b, m, n):
     A = Sum(f, (k, a, b))
     s, e = A.euler_maclaurin(m, n)
     assert (e == 0) and (s.expand() == A.doit())
Beispiel #6
0
def test_factor_nc():
    x, y = symbols('x,y')
    k = symbols('k', integer=True)
    n, m, o = symbols('n,m,o', commutative=False)

    # mul and multinomial expansion is needed
    from sympy.core.function import _mexpand
    e = x * (1 + y)**2
    assert _mexpand(e) == x + x * 2 * y + x * y**2

    def factor_nc_test(e):
        ex = _mexpand(e)
        assert ex.is_Add
        f = factor_nc(ex)
        assert not f.is_Add and _mexpand(f) == ex

    factor_nc_test(x * (1 + y))
    factor_nc_test(n * (x + 1))
    factor_nc_test(n * (x + m))
    factor_nc_test((x + m) * n)
    factor_nc_test(n * m * (x * o + n * o * m) * n)
    s = Sum(x, (x, 1, 2))
    factor_nc_test(x * (1 + s))
    factor_nc_test(x * (1 + s) * s)
    factor_nc_test(x * (1 + sin(s)))
    factor_nc_test((1 + n)**2)

    factor_nc_test((x + n) * (x + m) * (x + y))
    factor_nc_test(x * (n * m + 1))
    factor_nc_test(x * (n * m + x))
    factor_nc_test(x * (x * n * m + 1))
    factor_nc_test(x * n * (x * m + 1))
    factor_nc_test(x * (m * n + x * n * m))
    factor_nc_test(n * (1 - m) * n**2)

    factor_nc_test((n + m)**2)
    factor_nc_test((n - m) * (n + m)**2)
    factor_nc_test((n + m)**2 * (n - m))
    factor_nc_test((m - n) * (n + m)**2 * (n - m))

    assert factor_nc(n * (n + n * m)) == n**2 * (1 + m)
    assert factor_nc(m * (m * n + n * m * n**2)) == m * (m + n * m * n) * n
    eq = m * sin(n) - sin(n) * m
    assert factor_nc(eq) == eq

    # for coverage:
    from sympy.physics.secondquant import Commutator
    from sympy import factor
    eq = 1 + x * Commutator(m, n)
    assert factor_nc(eq) == eq
    eq = x * Commutator(m, n) + x * Commutator(m, o) * Commutator(m, n)
    assert factor(eq) == x * (1 + Commutator(m, o)) * Commutator(m, n)

    # issue 6534
    assert (2 * n + 2 * m).factor() == 2 * (n + m)

    # issue 6701
    assert factor_nc(n**k + n**(k + 1)) == n**k * (1 + n)
    assert factor_nc((m * n)**k + (m * n)**(k + 1)) == (1 + m * n) * (m * n)**k

    # issue 6918
    assert factor_nc(-n * (2 * x**2 + 2 * x)) == -2 * n * x * (x + 1)
Beispiel #7
0
def test_doit():
    n = Symbol('n', integer=True)
    f = Sum(2 * n * x, (n, 1, 3))
    d = Derivative(f, x)
    assert d.doit() == 12
    assert d.doit(deep=False) == Sum(2 * n, (n, 1, 3))
def test_as_dummy():
    u, v, x, y, z, _0, _1 = symbols('u v x y z _0 _1')
    assert Lambda(x, x + 1).as_dummy() == Lambda(_0, _0 + 1)
    assert Lambda(x, x + _0).as_dummy() == Lambda(_1, _0 + _1)
    assert (1 + Sum(x, (x, 1, x))).as_dummy() == 1 + Sum(_0, (_0, 1, x))
Beispiel #9
0
def test_issue_14567():
    assert factorial(Sum(-1, (x, 0, 0))) + y  # doesn't raise an error
Beispiel #10
0
 def _eval_rewrite_as_polynomial(self, n, x):
     from sympy import Sum
     # TODO: Should make sure n is in N_0
     k = Dummy("k")
     kern = RisingFactorial(-n, k) / factorial(k)**2 * x**k
     return Sum(kern, (k, 0, n))
Beispiel #11
0
def test_exp_summation():
    w = symbols("w")
    m, n, i, j = symbols("m n i j")
    expr = exp(Sum(w * i, (i, 0, n), (j, 0, m)))
    assert expr.expand() == Product(exp(w * i), (i, 0, n), (j, 0, m))
Beispiel #12
0
def test_is_convergent():
    # divergence tests --
    assert Sum(n / (2 * n + 1), (n, 1, oo)).is_convergent() is S.false
    assert Sum(factorial(n) / 5**n, (n, 1, oo)).is_convergent() is S.false
    assert Sum(3**(-2 * n - 1) * n**n, (n, 1, oo)).is_convergent() is S.false
    assert Sum((-1)**n * n, (n, 3, oo)).is_convergent() is S.false
    assert Sum((-1)**n, (n, 1, oo)).is_convergent() is S.false
    assert Sum(log(1 / n), (n, 2, oo)).is_convergent() is S.false

    # root test --
    assert Sum((-12)**n / n, (n, 1, oo)).is_convergent() is S.false
    assert Sum(2**n / factorial(n), (n, 1, oo)).is_convergent() is S.true

    # integral test --

    # p-series test --
    assert Sum(1 / (n**2 + 1), (n, 1, oo)).is_convergent() is S.true
    assert Sum(1 / n**(S(6) / 5), (n, 1, oo)).is_convergent() is S.true
    assert Sum(2 / (n * sqrt(n - 1)), (n, 2, oo)).is_convergent() is S.true

    # comparison test --
    assert Sum(1 / (n + log(n)), (n, 1, oo)).is_convergent() is S.false
    assert Sum(1 / (n**2 * log(n)), (n, 2, oo)).is_convergent() is S.true
    assert Sum(1 / (n * log(n)), (n, 2, oo)).is_convergent() is S.false
    assert Sum(2 / (n * log(n) * log(log(n))**2),
               (n, 5, oo)).is_convergent() is S.true
    assert Sum(2 / (n * log(n)**2), (n, 2, oo)).is_convergent() is S.true
    assert Sum((n - 1) / (n**2 * log(n)**3),
               (n, 2, oo)).is_convergent() is S.true
    assert Sum(1 / (n * log(n) * log(log(n))),
               (n, 5, oo)).is_convergent() is S.false
    assert Sum((n - 1) / (n * log(n)**3),
               (n, 3, oo)).is_convergent() is S.false
    assert Sum(2 / (n**2 * log(n)), (n, 2, oo)).is_convergent() is S.true

    # alternating series tests --
    assert Sum((-1)**(n - 1) / (n**2 - 1),
               (n, 3, oo)).is_convergent() is S.true

    # with -negativeInfinite Limits
    assert Sum(1 / (n**2 + 1), (n, -oo, 1)).is_convergent() is S.true
    assert Sum(1 / (n - 1), (n, -oo, -1)).is_convergent() is S.false
    assert Sum(1 / (n**2 - 1), (n, -oo, -5)).is_convergent() is S.true
    assert Sum(1 / (n**2 - 1), (n, -oo, 2)).is_convergent() is S.true
    assert Sum(1 / (n**2 - 1), (n, -oo, oo)).is_convergent() is S.true

    # piecewise functions
    f = Piecewise((n**(-2), n <= 1), (n**2, n > 1))
    assert Sum(f, (n, 1, oo)).is_convergent() is S.false
    assert Sum(f, (n, -oo, oo)).is_convergent() is S.false
    #assert Sum(f, (n, -oo, 1)).is_convergent() is S.true

    # integral test

    assert Sum(log(n) / n**3, (n, 1, oo)).is_convergent() is S.true
    assert Sum(-log(n) / n**3, (n, 1, oo)).is_convergent() is S.true
    # the following function has maxima located at (x, y) =
    # (1.2, 0.43), (3.0, -0.25) and (6.8, 0.050)
    eq = (x - 2) * (x**2 - 6 * x + 4) * exp(-x)
    assert Sum(eq, (x, 1, oo)).is_convergent() is S.true
Beispiel #13
0
def test_matrix_sum():
    A = Matrix([[0, 1], [n, 0]])
    assert Sum(A, (n, 0, 3)).doit() == Matrix([[0, 4], [6, 0]])
Beispiel #14
0
def test_distribution_over_equality():
    assert Product(Eq(x * 2, f(x)),
                   (x, 1, 3)).doit() == Eq(48,
                                           f(1) * f(2) * f(3))
    assert Sum(Eq(f(x), x**2), (x, 0, y)) == \
        Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
def test_product_pow():
    # issue 4817
    assert product(2**f(k), (k, 1, n)) == 2**Sum(f(k), (k, 1, n))
    assert product(2**(2 * f(k)), (k, 1, n)) == 2**Sum(2 * f(k), (k, 1, n))
def test_issue_8404():
    i = Symbol('i', integer=True)
    assert Abs(
        Sum(1 /
            (3 * i + 1)**2, (i, 0, S.Infinity)).doit().n(4) - 1.122) < 0.001
Beispiel #17
0
def test_factor_terms():
    A = Symbol('A', commutative=False)
    assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \
        9*x*y + 9*x + _keep_coeff(S(3), x + 1)**_keep_coeff(S(2), x + 1) + 9
    assert factor_terms(9*(x + x*y + 1) + (3)**(2 + 2*x)) == \
        _keep_coeff(S(9), 3**(2*x) + x*y + x + 1)
    assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \
        9*3**(2*x)*(a + 1)
    assert factor_terms(x + x*A) == \
        x*(1 + A)
    assert factor_terms(sin(x + x*A)) == \
        sin(x*(1 + A))
    assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \
        _keep_coeff(S(3), x + 1)**_keep_coeff(S(2)/3, x + 1)
    assert factor_terms(x + (x*y + x)**(3*x + 3)) == \
        x + (x*(y + 1))**_keep_coeff(S(3), x + 1)
    assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \
        x*(a + 2*b)*(y + 1)
    i = Integral(x, (x, 0, oo))
    assert factor_terms(i) == i

    assert factor_terms(x / 2 + y) == x / 2 + y
    # fraction doesn't apply to integer denominators
    assert factor_terms(x / 2 + y, fraction=True) == x / 2 + y
    # clear *does* apply to the integer denominators
    assert factor_terms(x / 2 + y, clear=True) == Mul(S.Half,
                                                      x + 2 * y,
                                                      evaluate=False)

    # check radical extraction
    eq = sqrt(2) + sqrt(10)
    assert factor_terms(eq) == eq
    assert factor_terms(eq, radical=True) == sqrt(2) * (1 + sqrt(5))
    eq = root(-6, 3) + root(6, 3)
    assert factor_terms(eq,
                        radical=True) == 6**(S(1) / 3) * (1 + (-1)**(S(1) / 3))

    eq = [x + x * y]
    ans = [x * (y + 1)]
    for c in [list, tuple, set]:
        assert factor_terms(c(eq)) == c(ans)
    assert factor_terms(Tuple(x + x * y)) == Tuple(x * (y + 1))
    assert factor_terms(Interval(0, 1)) == Interval(0, 1)
    e = 1 / sqrt(a / 2 + 1)
    assert factor_terms(e, clear=False) == 1 / sqrt(a / 2 + 1)
    assert factor_terms(e, clear=True) == sqrt(2) / sqrt(a + 2)

    eq = x / (x + 1 / x) + 1 / (x**2 + 1)
    assert factor_terms(eq, fraction=False) == eq
    assert factor_terms(eq, fraction=True) == 1

    assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \
        y*(2 + 1/(x + 1))/x**2

    # if not True, then processesing for this in factor_terms is not necessary
    assert gcd_terms(-x - y) == -x - y
    assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False)

    # if not True, then "special" processesing in factor_terms is not necessary
    assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1)
    e = exp(-x - 2) + x
    assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x
    assert factor_terms(e, sign=False) == e
    assert factor_terms(exp(-4 * x - 2) -
                        x) == -x + exp(Mul(-2, 2 * x + 1, evaluate=False))

    # sum tests
    assert factor_terms(Sum(x, (y, 1, 10))) == x * Sum(1, (y, 1, 10))
    assert factor_terms(Sum(x, (y, 1, 10)) + x) == x * (1 + Sum(1, (y, 1, 10)))
    assert factor_terms(Sum(x * y + x * y**2,
                            (y, 1, 10))) == x * Sum(y * (y + 1), (y, 1, 10))
Beispiel #18
0
 def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
     from sympy import Sum
     k = Dummy("k")
     kern = (-1)**k / (factorial(k) *
                       factorial(n - 2 * k)) * (2 * x)**(n - 2 * k)
     return factorial(n) * Sum(kern, (k, 0, floor(n / 2)))
Beispiel #19
0
def test_Sum():
    assert str(summation(cos(3 * z), (z, x, y))) == "Sum(cos(3*z), (z, x, y))"
    assert str(Sum(x*y**2, (x, -2, 2), (y, -5, 5))) == \
        "Sum(x*y**2, (x, -2, 2), (y, -5, 5))"
Beispiel #20
0
 def _eval_rewrite_as_polynomial(self, n, a, x, **kwargs):
     from sympy import Sum
     k = Dummy("k")
     kern = ((-1)**k * RisingFactorial(a, n - k) * (2 * x)**(n - 2 * k) /
             (factorial(k) * factorial(n - 2 * k)))
     return Sum(kern, (k, 0, floor(n / 2)))
Beispiel #21
0
def test_karr_convention():
    # Test the Karr summation convention that we want to hold.
    # See his paper "Summation in Finite Terms" for a detailed
    # reasoning why we really want exactly this definition.
    # The convention is described on page 309 and essentially
    # in section 1.4, definition 3:
    #
    # \sum_{m <= i < n} f(i) 'has the obvious meaning'   for m < n
    # \sum_{m <= i < n} f(i) = 0                         for m = n
    # \sum_{m <= i < n} f(i) = - \sum_{n <= i < m} f(i)  for m > n
    #
    # It is important to note that he defines all sums with
    # the upper limit being *exclusive*.
    # In contrast, sympy and the usual mathematical notation has:
    #
    # sum_{i = a}^b f(i) = f(a) + f(a+1) + ... + f(b-1) + f(b)
    #
    # with the upper limit *inclusive*. So translating between
    # the two we find that:
    #
    # \sum_{m <= i < n} f(i) = \sum_{i = m}^{n-1} f(i)
    #
    # where we intentionally used two different ways to typeset the
    # sum and its limits.

    i = Symbol("i", integer=True)
    k = Symbol("k", integer=True)
    j = Symbol("j", integer=True)

    # A simple example with a concrete summand and symbolic limits.

    # The normal sum: m = k and n = k + j and therefore m < n:
    m = k
    n = k + j

    a = m
    b = n - 1
    S1 = Sum(i**2, (i, a, b)).doit()

    # The reversed sum: m = k + j and n = k and therefore m > n:
    m = k + j
    n = k

    a = m
    b = n - 1
    S2 = Sum(i**2, (i, a, b)).doit()

    assert simplify(S1 + S2) == 0

    # Test the empty sum: m = k and n = k and therefore m = n:
    m = k
    n = k

    a = m
    b = n - 1
    Sz = Sum(i**2, (i, a, b)).doit()

    assert Sz == 0

    # Another example this time with an unspecified summand and
    # numeric limits. (We can not do both tests in the same example.)
    f = Function("f")

    # The normal sum with m < n:
    m = 2
    n = 11

    a = m
    b = n - 1
    S1 = Sum(f(i), (i, a, b)).doit()

    # The reversed sum with m > n:
    m = 11
    n = 2

    a = m
    b = n - 1
    S2 = Sum(f(i), (i, a, b)).doit()

    assert simplify(S1 + S2) == 0

    # Test the empty sum with m = n:
    m = 5
    n = 5

    a = m
    b = n - 1
    Sz = Sum(f(i), (i, a, b)).doit()

    assert Sz == 0

    e = Piecewise((exp(-i), Mod(i, 2) > 0), (0, True))
    s = Sum(e, (i, 0, 11))
    assert s.n(3) == s.doit().n(3)
Beispiel #22
0
 def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
     from sympy import Sum
     k = Dummy("k")
     kern = binomial(n, 2 * k) * (x**2 - 1)**k * x**(n - 2 * k)
     return Sum(kern, (k, 0, floor(n / 2)))
Beispiel #23
0
def test_evalf_fast_series():
    # Euler transformed series for sqrt(1+x)
    assert NS(Sum(fac(2 * n + 1) / fac(n)**2 / 2**(3 * n + 1), (n, 0, oo)),
              100) == NS(sqrt(2), 100)

    # Some series for exp(1)
    estr = NS(E, 100)
    assert NS(Sum(1 / fac(n), (n, 0, oo)), 100) == estr
    assert NS(1 / Sum((1 - 2 * n) / fac(2 * n), (n, 0, oo)), 100) == estr
    assert NS(Sum((2 * n + 1) / fac(2 * n), (n, 0, oo)), 100) == estr
    assert NS(
        Sum((4 * n + 3) / 2**(2 * n + 1) / fac(2 * n + 1), (n, 0, oo))**2,
        100) == estr

    pistr = NS(pi, 100)
    # Ramanujan series for pi
    assert NS(
        9801 / sqrt(8) / Sum(
            fac(4 * n) * (1103 + 26390 * n) / fac(n)**4 / 396**(4 * n),
            (n, 0, oo)), 100) == pistr
    assert NS(
        1 / Sum(
            binomial(2 * n, n)**3 * (42 * n + 5) / 2**(12 * n + 4),
            (n, 0, oo)), 100) == pistr
    # Machin's formula for pi
    assert NS(
        16 * Sum((-1)**n / (2 * n + 1) / 5**(2 * n + 1), (n, 0, oo)) - 4 * Sum(
            (-1)**n / (2 * n + 1) / 239**(2 * n + 1), (n, 0, oo)),
        100) == pistr

    # Apery's constant
    astr = NS(zeta(3), 100)
    P = 126392*n**5 + 412708*n**4 + 531578*n**3 + 336367*n**2 + 104000* \
        n + 12463
    assert NS(
        Sum((-1)**n * P / 24 * (fac(2 * n + 1) * fac(2 * n) * fac(n))**3 /
            fac(3 * n + 2) / fac(4 * n + 3)**3, (n, 0, oo)), 100) == astr
    assert NS(
        Sum((-1)**n * (205 * n**2 + 250 * n + 77) / 64 * fac(n)**10 /
            fac(2 * n + 1)**5, (n, 0, oo)), 100) == astr
Beispiel #24
0
 def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
     from sympy import Sum
     k = Dummy("k")
     kern = S.NegativeOne**k * factorial(n - k) * (2 * x)**(n - 2 * k) / (
         factorial(k) * factorial(n - 2 * k))
     return Sum(kern, (k, 0, floor(n / 2)))
Beispiel #25
0
def test_evalf_symbolic():
    f, g = symbols('f g', cls=Function)
    # issue 6328
    expr = Sum(f(x), (x, 1, 3)) + Sum(g(x), (x, 1, 3))
    assert expr.evalf() == expr
Beispiel #26
0
 def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
     from sympy import Sum
     k = Dummy("k")
     kern = (-1)**k * binomial(n, k)**2 * ((1 + x) / 2)**(n - k) * (
         (1 - x) / 2)**k
     return Sum(kern, (k, 0, n))
Beispiel #27
0
def test_sum_reconstruct():
    s = Sum(n**2, (n, -1, 1))
    assert s == Sum(*s.args)
    raises(ValueError, lambda: Sum(x, x))
    raises(ValueError, lambda: Sum(x, (x, 1)))
def test_differentiation():
    from sympy.functions.special.tensor_functions import KroneckerDelta
    i, j, k, l = symbols('i j k l', cls=Idx)
    a = symbols('a')
    m, n = symbols("m, n", integer=True, finite=True)
    assert m.is_real
    h, L = symbols('h L', cls=IndexedBase)
    hi, hj = h[i], h[j]

    expr = hi
    assert expr.diff(hj) == KroneckerDelta(i, j)
    assert expr.diff(hi) == KroneckerDelta(i, i)

    expr = S(2) * hi
    assert expr.diff(hj) == S(2) * KroneckerDelta(i, j)
    assert expr.diff(hi) == S(2) * KroneckerDelta(i, i)
    assert expr.diff(a) == S.Zero

    assert Sum(expr, (i, -oo, oo)).diff(hj) == Sum(2*KroneckerDelta(i, j), (i, -oo, oo))
    assert Sum(expr.diff(hj), (i, -oo, oo)) == Sum(2*KroneckerDelta(i, j), (i, -oo, oo))
    assert Sum(expr, (i, -oo, oo)).diff(hj).doit() == 2

    assert Sum(expr.diff(hi), (i, -oo, oo)).doit() == Sum(2, (i, -oo, oo)).doit()
    assert Sum(expr, (i, -oo, oo)).diff(hi).doit() == oo

    expr = a * hj * hj / S(2)
    assert expr.diff(hi) == a * h[j] * KroneckerDelta(i, j)
    assert expr.diff(a) == hj * hj / S(2)
    assert expr.diff(a, 2) == S.Zero

    assert Sum(expr, (i, -oo, oo)).diff(hi) == Sum(a*KroneckerDelta(i, j)*h[j], (i, -oo, oo))
    assert Sum(expr.diff(hi), (i, -oo, oo)) == Sum(a*KroneckerDelta(i, j)*h[j], (i, -oo, oo))
    assert Sum(expr, (i, -oo, oo)).diff(hi).doit() == a*h[j]

    assert Sum(expr, (j, -oo, oo)).diff(hi) == Sum(a*KroneckerDelta(i, j)*h[j], (j, -oo, oo))
    assert Sum(expr.diff(hi), (j, -oo, oo)) == Sum(a*KroneckerDelta(i, j)*h[j], (j, -oo, oo))
    assert Sum(expr, (j, -oo, oo)).diff(hi).doit() == a*h[i]

    expr = a * sin(hj * hj)
    assert expr.diff(hi) == 2*a*cos(hj * hj) * hj * KroneckerDelta(i, j)
    assert expr.diff(hj) == 2*a*cos(hj * hj) * hj

    expr = a * L[i, j] * h[j]
    assert expr.diff(hi) == a*L[i, j]*KroneckerDelta(i, j)
    assert expr.diff(hj) == a*L[i, j]
    assert expr.diff(L[i, j]) == a*h[j]
    assert expr.diff(L[k, l]) == a*KroneckerDelta(i, k)*KroneckerDelta(j, l)*h[j]
    assert expr.diff(L[i, l]) == a*KroneckerDelta(j, l)*h[j]

    assert Sum(expr, (j, -oo, oo)).diff(L[k, l]) == Sum(a * KroneckerDelta(i, k) * KroneckerDelta(j, l) * h[j], (j, -oo, oo))
    assert Sum(expr, (j, -oo, oo)).diff(L[k, l]).doit() == a * KroneckerDelta(i, k) * h[l]

    assert h[m].diff(h[m]) == 1
    assert h[m].diff(h[n]) == KroneckerDelta(m, n)
    assert Sum(a*h[m], (m, -oo, oo)).diff(h[n]) == Sum(a*KroneckerDelta(m, n), (m, -oo, oo))
    assert Sum(a*h[m], (m, -oo, oo)).diff(h[n]).doit() == a
    assert Sum(a*h[m], (n, -oo, oo)).diff(h[n]) == Sum(a*KroneckerDelta(m, n), (n, -oo, oo))
    assert Sum(a*h[m], (m, -oo, oo)).diff(h[m]).doit() == oo*a
Beispiel #29
0
def test_conjugate_transpose():
    A, B = symbols("A B", commutative=False)
    p = Sum(A * B**n, (n, 1, 3))
    assert p.adjoint().doit() == p.doit().adjoint()
    assert p.conjugate().doit() == p.doit().conjugate()
    assert p.transpose().doit() == p.doit().transpose()
Beispiel #30
0
def test_reverse_order():
    assert Sum(x, (x, 0, 3)).reverse_order(0) == Sum(-x, (x, 4, -1))
    assert Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(0, 1) == \
           Sum(x*y, (x, 6, 0), (y, 7, -1))
    assert Sum(x, (x, 1, 2)).reverse_order(0) == Sum(-x, (x, 3, 0))
    assert Sum(x, (x, 1, 3)).reverse_order(0) == Sum(-x, (x, 4, 0))
    assert Sum(x, (x, 1, a)).reverse_order(0) == Sum(-x, (x, a + 1, 0))
    assert Sum(x, (x, a, 5)).reverse_order(0) == Sum(-x, (x, 6, a - 1))
    assert Sum(x, (x, a + 1, a + 5)).reverse_order(0) == \
                         Sum(-x, (x, a + 6, a))
    assert Sum(x, (x, a + 1, a + 2)).reverse_order(0) == \
           Sum(-x, (x, a + 3, a))
    assert Sum(x, (x, a + 1, a + 1)).reverse_order(0) == \
           Sum(-x, (x, a + 2, a))
    assert Sum(x, (x, a, b)).reverse_order(0) == Sum(-x, (x, b + 1, a - 1))
    assert Sum(x, (x, a, b)).reverse_order(x) == Sum(-x, (x, b + 1, a - 1))
    assert Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1) == \
        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
    assert Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x) == \
        Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))