コード例 #1
0
ファイル: test_mathematica.py プロジェクト: yyht/sympy
def test_Sum():
    assert mcode(Sum(sin(x), (x, 0, 10))) == "Hold[Sum[Sin[x], {x, 0, 10}]]"
    assert mcode(Sum(exp(-x**2 - y**2),
                     (x, -oo, oo),
                     (y, -oo, oo))) == \
        "Hold[Sum[Exp[-x^2 - y^2], {x, -Infinity, Infinity}, " \
        "{y, -Infinity, Infinity}]]"
コード例 #2
0
def test_log_product():
    from sympy.abc import n, m
    i, j = symbols('i,j', positive=True, integer=True)
    x, y = symbols('x,y', positive=True)
    from sympy.concrete import Product, Sum
    f, g = Function('f'), Function('g')
    assert simplify(log(Product(x**i, (i, 1, n)))) == Sum(log(x**i), (i, 1, n))
    assert simplify(log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))) == \
            Sum(log(x**i*y**j), (i, 1, n), (j, 1, m))

    expr = log(Product(-2, (n, 0, 4)))
    assert Eq(simplify(expr), expr)
コード例 #3
0
ファイル: exponential.py プロジェクト: ndfcampbell/sympy
    def _eval_expand_log(self, deep=True, **hints):
        from sympy import unpolarify
        from sympy.concrete import Sum, Product
        force = hints.get('force', False)
        arg = self.args[0]
        if arg.is_Mul:
            expr = []
            nonpos = []
            for x in arg.args:
                if force or x.is_positive or x.is_polar:
                    a = self.func(x)
                    if isinstance(a, log):
                        expr.append(self.func(x)._eval_expand_log(**hints))
                    else:
                        expr.append(a)
                else:
                    nonpos.append(x)
            return Add(*expr) + log(Mul(*nonpos))
        elif arg.is_Pow:
            if force or (arg.exp.is_real and arg.base.is_positive) or \
                    arg.base.is_polar:
                b = arg.base
                e = arg.exp
                a = self.func(b)
                if isinstance(a, log):
                    return unpolarify(e) * a._eval_expand_log(**hints)
                else:
                    return unpolarify(e) * a
        elif isinstance(arg, Product):
            if arg.function.is_positive:
                return Sum(log(arg.function), *arg.limits)

        return self.func(arg)
コード例 #4
0
    def infinite(self):
        """Returns an infinite representation of the series"""
        from sympy.concrete import Sum
        ak, xk = self.ak, self.xk
        k = ak.variables[0]
        inf_sum = Sum(ak.formula * xk.formula, (k, ak.start, ak.stop))

        return self.ind + inf_sum
コード例 #5
0
ファイル: exponential.py プロジェクト: cosmosZhou/sagemath
 def as_Add(self):
     if self.arg.is_Mul:
         return Add(*(self.func(arg).simplify() for arg in self.arg.args))
     
     from sympy import Sum
     if self.arg.is_Product:
         product = self.arg
         return Sum(self.func(product.function), *product.limits)
     return self
コード例 #6
0
ファイル: exponential.py プロジェクト: yyht/sympy
    def _eval_expand_log(self, deep=True, **hints):
        from sympy import unpolarify, expand_log
        from sympy.concrete import Sum, Product
        force = hints.get('force', False)
        if (len(self.args) == 2):
            return expand_log(self.func(*self.args), deep=deep, force=force)
        arg = self.args[0]
        if arg.is_Integer:
            # remove perfect powers
            p = perfect_power(int(arg))
            if p is not False:
                return p[1] * self.func(p[0])
        elif arg.is_Rational:
            return log(arg.p) - log(arg.q)
        elif arg.is_Mul:
            expr = []
            nonpos = []
            for x in arg.args:
                if force or x.is_positive or x.is_polar:
                    a = self.func(x)
                    if isinstance(a, log):
                        expr.append(self.func(x)._eval_expand_log(**hints))
                    else:
                        expr.append(a)
                elif x.is_negative:
                    a = self.func(-x)
                    expr.append(a)
                    nonpos.append(S.NegativeOne)
                else:
                    nonpos.append(x)
            return Add(*expr) + log(Mul(*nonpos))
        elif arg.is_Pow or isinstance(arg, exp):
            if force or (
                    arg.exp.is_extended_real and
                (arg.base.is_positive or
                 ((arg.exp + 1).is_positive and
                  (arg.exp - 1).is_nonpositive))) or arg.base.is_polar:
                b = arg.base
                e = arg.exp
                a = self.func(b)
                if isinstance(a, log):
                    return unpolarify(e) * a._eval_expand_log(**hints)
                else:
                    return unpolarify(e) * a
        elif isinstance(arg, Product):
            if arg.function.is_positive:
                return Sum(log(arg.function), *arg.limits)

        return self.func(arg)
コード例 #7
0
ファイル: test_delta.py プロジェクト: DarkMagician0611/IITZL
def test_deltasummation_basic_numerical():
    n = symbols('n', integer=True, nonzero=True)
    assert ds(KD(n, 0), (n, 1, 3)) == 0

    # return unevaluated, until it gets implemented
    assert ds(KD(i**2, j**2), (j, -oo, oo)) == \
        Sum(KD(i**2, j**2), (j, -oo, oo))

    assert Piecewise((KD(i, k), And(S(1) <= i, i <= 3)), (0, True)) == \
        ds(KD(i, j)*KD(j, k), (j, 1, 3)) == \
        ds(KD(j, k)*KD(i, j), (j, 1, 3))

    assert ds(KD(i, k), (k, -oo, oo)) == 1
    assert ds(KD(i, k), (k, 0, oo)) == Piecewise((1, S(0) <= i), (0, True))
    assert ds(KD(i, k), (k, 1, 3)) == \
        Piecewise((1, And(S(1) <= i, i <= 3)), (0, True))
    assert ds(k * KD(i, j) * KD(j, k), (k, -oo, oo)) == j * KD(i, j)
    assert ds(j * KD(i, j), (j, -oo, oo)) == i
    assert ds(i * KD(i, j), (i, -oo, oo)) == j
    assert ds(x, (i, 1, 3)) == 3 * x
    assert ds((i + j) * KD(i, j), (j, -oo, oo)) == 2 * i
コード例 #8
0
def change_index(expr, var, trafo, newvar=None):
    """
    Change index of a Sum or Product.

    Perform a linear transformation `x \mapsto a x + b` on the index variable
    `x`. For `a` the only values allowed are `\pm 1`. A new variable to be used
    after the change of index can also be specified.

    Usage
    =====

    ``change_index(expr, var, trafo, newvar=None)`` where ``var`` specifies the
    index variable `x` to transform. The transformation ``trafo`` must be linear
    and given in terms of ``var``. If the optional argument ``newvar`` is
    provided then ``var`` gets replaced by ``newvar`` in the final expression.

    Examples
    ========

    >>> from sympy.concrete.simplification import change_index
    >>> from sympy import Sum, Product, simplify
    >>> from sympy.abc import x, y, a, b, c, d, u, v, i, j, k, l

    >>> S = Sum(x, (x, a, b))
    >>> S.doit()
    -a**2/2 + a/2 + b**2/2 + b/2

    >>> Sn = change_index(S, x, x + 1, y)
    >>> Sn
    Sum(y - 1, (y, a + 1, b + 1))
    >>> Sn.doit()
    -a**2/2 + a/2 + b**2/2 + b/2

    >>> Sn = change_index(S, x, -x, y)
    >>> Sn
    Sum(-y, (y, -b, -a))
    >>> Sn.doit()
    -a**2/2 + a/2 + b**2/2 + b/2

    >>> Sn = change_index(S, x, x+u)
    >>> Sn
    Sum(-u + x, (x, a + u, b + u))
    >>> Sn.doit()
    -a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
    >>> simplify(Sn.doit())
    -a**2/2 + a/2 + b**2/2 + b/2

    >>> Sn = change_index(S, x, -x - u, y)
    >>> Sn
    Sum(-u - y, (y, -b - u, -a - u))
    >>> Sn.doit()
    -a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
    >>> simplify(Sn.doit())
    -a**2/2 + a/2 + b**2/2 + b/2

    >>> P = Product(i*j**2, (i, a, b), (j, c, d))
    >>> P
    Product(i*j**2, (i, a, b), (j, c, d))
    >>> P2 = change_index(P, i, i+3, k)
    >>> P2
    Product(j**2*(k - 3), (k, a + 3, b + 3), (j, c, d))
    >>> P3 = change_index(P2, j, -j, l)
    >>> P3
    Product(l**2*(k - 3), (k, a + 3, b + 3), (l, -d, -c))

    When dealing with symbols only, we can make a
    general linear transformation:

    >>> Sn = change_index(S, x, u*x+v, y)
    >>> Sn
    Sum((-v + y)/u, (y, b*u + v, a*u + v))
    >>> Sn.doit()
    -v*(a*u - b*u + 1)/u + (a**2*u**2/2 + a*u*v + a*u/2 - b**2*u**2/2 - b*u*v + b*u/2 + v)/u
    >>> simplify(Sn.doit())
    a**2*u/2 + a/2 - b**2*u/2 + b/2

    However, the last result can be inconsistent with usual
    summation where the index increment is always 1. This is
    obvious as we get back the original value only for ``u``
    equal +1 or -1.

    See Also
    ========

    sympy.concrete.simplification.index,
    sympy.concrete.simplification.reorder_limit,
    sympy.concrete.simplification.reorder,
    sympy.concrete.simplification.reverse_order
    """
    if newvar is None:
        newvar = var

    limits = []
    for limit in expr.limits:
        if limit[0] == var:
            p = trafo.as_poly(var)
            if p.degree() != 1:
                raise ValueError("Index transformation is not linear")
            alpha = p.coeff_monomial(var)
            beta = p.coeff_monomial(S.One)
            if alpha.is_number:
                if alpha == S.One:
                    limits.append((newvar, alpha * limit[1] + beta,
                                   alpha * limit[2] + beta))
                elif alpha == S.NegativeOne:
                    limits.append((newvar, alpha * limit[2] + beta,
                                   alpha * limit[1] + beta))
                else:
                    raise ValueError(
                        "Linear transformation results in non-linear summation stepsize"
                    )
            else:
                # Note that the case of alpha being symbolic can give issues if alpha < 0.
                limits.append(
                    (newvar, alpha * limit[2] + beta, alpha * limit[1] + beta))
        else:
            limits.append(limit)

    function = expr.function.subs(var, (var - beta) / alpha)
    function = function.subs(var, newvar)

    if isinstance(expr, Sum):
        return Sum(function, *tuple(limits))
    elif isinstance(expr, Product):
        return Product(function, *tuple(limits))
    else:
        raise NotImplementedError(
            expr, "change_index only implemented for Sum and Product")
コード例 #9
0
def reverse_order(expr, *indices):
    """
    Reverse the order of a limit in a Sum or Product.

    Usage
    =====

    ``reverse_order(expr, *indices)`` reverses some limits in the expression
    ``expr`` which can be either a ``Sum`` or a ``Product``. The selectors in
    the argument ``indices`` specify some indices whose limits get reversed.
    These selectors are either variable names or numerical indices counted
    starting from the inner-most limit tuple.

    Examples
    ========

    >>> from sympy.concrete.simplification import reverse_order
    >>> from sympy import Sum
    >>> from sympy.abc import x, y, a, b, c, d

    >>> reverse_order(Sum(x, (x, 0, 3)), x)
    Sum(-x, (x, 4, -1))
    >>> reverse_order(Sum(x*y, (x, 1, 5), (y, 0, 6)), x, y)
    Sum(x*y, (x, 6, 0), (y, 7, -1))
    >>> reverse_order(Sum(x, (x, a, b)), x)
    Sum(-x, (x, b + 1, a - 1))
    >>> reverse_order(Sum(x, (x, a, b)), 0)
    Sum(-x, (x, b + 1, a - 1))

    >>> from sympy import Product, simplify, RisingFactorial, gamma
    >>> P = Product(x, (x, a, b))
    >>> Pr = reverse_order(P, x)
    >>> Pr
    Product(1/x, (x, b + 1, a - 1))
    >>> Pr = Pr.doit()
    >>> Pr
    1/RisingFactorial(b + 1, a - b - 1)
    >>> simplify(Pr)
    gamma(b + 1)/gamma(a)
    >>> P = P.doit()
    >>> P
    RisingFactorial(a, -a + b + 1)
    >>> simplify(P)
    gamma(b + 1)/gamma(a)

    While one should prefer variable names when specifying which limits
    to reverse, the index counting notation comes in handy in case there
    are several symbols with the same name.

    >>> S = Sum(x**2, (x, a, b), (x, c, d))
    >>> S
    Sum(x**2, (x, a, b), (x, c, d))
    >>> S0 = reverse_order(S, 0)
    >>> S0
    Sum(-x**2, (x, b + 1, a - 1), (x, c, d))
    >>> S1 = reverse_order(S0, 1)
    >>> S1
    Sum(x**2, (x, b + 1, a - 1), (x, d + 1, c - 1))

    Of course we can mix both notations:

    >>> reverse_order(Sum(x*y, (x, a, b), (y, 2, 5)), x, 1)
    Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
    >>> reverse_order(Sum(x*y, (x, a, b), (y, 2, 5)), y, x)
    Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))

    See Also
    ========

    sympy.concrete.simplification.index,
    sympy.concrete.simplification.change_index,
    sympy.concrete.simplification.reorder_limit,
    sympy.concrete.simplification.reorder

    References
    ==========

    .. [1] Michael Karr, "Summation in Finite Terms", Journal of the ACM,
           Volume 28 Issue 2, April 1981, Pages 305-350
           http://dl.acm.org/citation.cfm?doid=322248.322255
    """
    l_indices = list(indices)

    for i, indx in enumerate(l_indices):
        if not isinstance(indx, int):
            l_indices[i] = index(expr, indx)

    if isinstance(expr, Sum) or isinstance(expr, Product):
        e = 1
        limits = []
        for i, limit in enumerate(expr.limits):
            l = limit
            if i in l_indices:
                e = -e
                l = (limit[0], limit[2] + 1, limit[1] - 1)
            limits.append(l)

    if isinstance(expr, Sum):
        return Sum(e * expr.function, *limits)
    elif isinstance(expr, Product):
        return Product(expr.function**e, *limits)
    else:
        return expr
コード例 #10
0
def reorder_limit(expr, x, y):
    """
    Interchange two limit tuples of a Sum or Product expression.

    Usage
    =====

    ``reorder_limit(expr, x, y)`` interchanges two limit tuples. The
    arguments ``x`` and ``y`` are integers corresponding to the index
    variables of the two limits which are to be interchanged. The
    expression ``expr`` has to be either a Sum or a Product.

    Examples
    ========

    >>> from sympy.concrete.simplification import reorder_limit
    >>> from sympy.abc import x, y, z, a, b, c, d, e, f
    >>> from sympy import Sum, Product

    >>> reorder_limit(Sum(x*y*z, (x, a, b), (y, c, d), (z, e, f)), 0, 2)
    Sum(x*y*z, (z, e, f), (y, c, d), (x, a, b))
    >>> reorder_limit(Sum(x**2, (x, a, b), (x, c, d)), 1, 0)
    Sum(x**2, (x, c, d), (x, a, b))

    >>> reorder_limit(Product(x*y*z, (x, a, b), (y, c, d), (z, e, f)), 0, 2)
    Product(x*y*z, (z, e, f), (y, c, d), (x, a, b))

    See Also
    ========

    sympy.concrete.simplification.index,
    sympy.concrete.simplification.change_index,
    sympy.concrete.simplification.reorder,
    sympy.concrete.simplification.reverse_order
    """
    var = set([limit[0] for limit in expr.limits])
    limit_x = expr.limits[x]
    limit_y = expr.limits[y]

    if (len(set(limit_x[1].free_symbols).intersection(var)) == 0
            and len(set(limit_x[2].free_symbols).intersection(var)) == 0
            and len(set(limit_y[1].free_symbols).intersection(var)) == 0
            and len(set(limit_y[2].free_symbols).intersection(var)) == 0):

        limits = []
        for i, limit in enumerate(expr.limits):
            if i == x:
                limits.append(limit_y)
            elif i == y:
                limits.append(limit_x)
            else:
                limits.append(limit)

        if isinstance(expr, Sum):
            return Sum(expr.function, *limits)
        elif isinstance(expr, Product):
            return Product(expr.function, *limits)
        else:
            raise NotImplementedError(
                expr, "reorder only implemented for Sum and Product")

    else:
        raise ReorderError(expr,
                           "could not interchange the two limits specified")
コード例 #11
0
ファイル: test_trace.py プロジェクト: sidhu1012/sympy
def test_trace_rewrite():
    assert trace(A).rewrite(Sum) == Sum(A[i, i], (i, 0, n - 1))
    assert trace(eye(3)).rewrite(Sum) == 3
コード例 #12
0
ファイル: test_delta.py プロジェクト: twobitlogic/sympy
def test_deltasummation():
    ds = deltasummation
    assert ds(x, (j, 1, 0)) == 0
    assert ds(x, (j, 1, 3)) == 3*x
    assert ds(x + y, (j, 1, 3)) == 3*(x + y)
    assert ds(x*y, (j, 1, 3)) == 3*x*y
    assert ds(KD(i, j), (k, 1, 3)) == 3*KD(i, j)
    assert ds(x*KD(i, j), (k, 1, 3)) == 3*x*KD(i, j)
    assert ds(x*y*KD(i, j), (k, 1, 3)) == 3*x*y*KD(i, j)

    n = symbols('n', integer=True, nonzero=True)
    assert ds(KD(n, 0), (n, 1, 3)) == 0

    # return unevaluated, until it gets implemented
    assert ds(KD(i**2, j**2), (j, -oo, oo)) == \
        Sum(KD(i**2, j**2), (j, -oo, oo))

    assert Piecewise((KD(i, k), And(S(1) <= i, i <= 3)), (0, True)) == \
        ds(KD(i, j)*KD(j, k), (j, 1, 3)) == \
        ds(KD(j, k)*KD(i, j), (j, 1, 3))

    assert ds(KD(i, k), (k, -oo, oo)) == 1
    assert ds(KD(i, k), (k, 0, oo)) == Piecewise((1, i >= 0), (0, True))
    assert ds(KD(i, k), (k, 1, 3)) == \
        Piecewise((1, And(S(1) <= i, i <= 3)), (0, True))
    assert ds(k*KD(i, j)*KD(j, k), (k, -oo, oo)) == j*KD(i, j)
    assert ds(j*KD(i, j), (j, -oo, oo)) == i
    assert ds(i*KD(i, j), (i, -oo, oo)) == j
    assert ds(x, (i, 1, 3)) == 3*x
    assert ds((i + j)*KD(i, j), (j, -oo, oo)) == 2*i

    assert ds(KD(i, j), (j, 1, 3)) == \
        Piecewise((1, And(S(1) <= i, i <= 3)), (0, True))
    assert ds(KD(i, j), (j, 1, 1)) == Piecewise((1, Eq(i, 1)), (0, True))
    assert ds(KD(i, j), (j, 2, 2)) == Piecewise((1, Eq(i, 2)), (0, True))
    assert ds(KD(i, j), (j, 3, 3)) == Piecewise((1, Eq(i, 3)), (0, True))
    assert ds(KD(i, j), (j, 1, k)) == \
        Piecewise((1, And(S(1) <= i, i <= k)), (0, True))
    assert ds(KD(i, j), (j, k, 3)) == \
        Piecewise((1, And(k <= i, i <= 3)), (0, True))
    assert ds(KD(i, j), (j, k, l)) == \
        Piecewise((1, And(k <= i, i <= l)), (0, True))

    assert ds(x*KD(i, j), (j, 1, 3)) == \
        Piecewise((x, And(S(1) <= i, i <= 3)), (0, True))
    assert ds(x*KD(i, j), (j, 1, 1)) == Piecewise((x, Eq(i, 1)), (0, True))
    assert ds(x*KD(i, j), (j, 2, 2)) == Piecewise((x, Eq(i, 2)), (0, True))
    assert ds(x*KD(i, j), (j, 3, 3)) == Piecewise((x, Eq(i, 3)), (0, True))
    assert ds(x*KD(i, j), (j, 1, k)) == \
        Piecewise((x, And(S(1) <= i, i <= k)), (0, True))
    assert ds(x*KD(i, j), (j, k, 3)) == \
        Piecewise((x, And(k <= i, i <= 3)), (0, True))
    assert ds(x*KD(i, j), (j, k, l)) == \
        Piecewise((x, And(k <= i, i <= l)), (0, True))

    assert ds((x + y)*KD(i, j), (j, 1, 3)) == \
        Piecewise((x + y, And(S(1) <= i, i <= 3)), (0, True))
    assert ds((x + y)*KD(i, j), (j, 1, 1)) == \
        Piecewise((x + y, Eq(i, 1)), (0, True))
    assert ds((x + y)*KD(i, j), (j, 2, 2)) == \
        Piecewise((x + y, Eq(i, 2)), (0, True))
    assert ds((x + y)*KD(i, j), (j, 3, 3)) == \
        Piecewise((x + y, Eq(i, 3)), (0, True))
    assert ds((x + y)*KD(i, j), (j, 1, k)) == \
        Piecewise((x + y, And(S(1) <= i, i <= k)), (0, True))
    assert ds((x + y)*KD(i, j), (j, k, 3)) == \
        Piecewise((x + y, And(k <= i, i <= 3)), (0, True))
    assert ds((x + y)*KD(i, j), (j, k, l)) == \
        Piecewise((x + y, And(k <= i, i <= l)), (0, True))

    assert ds(KD(i, k) + KD(j, k), (k, 1, 3)) == piecewise_fold(
        Piecewise((1, And(S(1) <= i, i <= 3)), (0, True)) +
        Piecewise((1, And(S(1) <= j, j <= 3)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, 1, 1)) == piecewise_fold(
        Piecewise((1, Eq(i, 1)), (0, True)) +
        Piecewise((1, Eq(j, 1)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, 2, 2)) == piecewise_fold(
        Piecewise((1, Eq(i, 2)), (0, True)) +
        Piecewise((1, Eq(j, 2)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, 3, 3)) == piecewise_fold(
        Piecewise((1, Eq(i, 3)), (0, True)) +
        Piecewise((1, Eq(j, 3)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, 1, l)) == piecewise_fold(
        Piecewise((1, And(S(1) <= i, i <= l)), (0, True)) +
        Piecewise((1, And(S(1) <= j, j <= l)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, l, 3)) == piecewise_fold(
        Piecewise((1, And(l <= i, i <= 3)), (0, True)) +
        Piecewise((1, And(l <= j, j <= 3)), (0, True)))
    assert ds(KD(i, k) + KD(j, k), (k, l, m)) == piecewise_fold(
        Piecewise((1, And(l <= i, i <= m)), (0, True)) +
        Piecewise((1, And(l <= j, j <= m)), (0, True)))

    assert ds(x*KD(i, k) + KD(j, k), (k, 1, 3)) == piecewise_fold(
        Piecewise((x, And(S(1) <= i, i <= 3)), (0, True)) +
        Piecewise((1, And(S(1) <= j, j <= 3)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, 1, 1)) == piecewise_fold(
        Piecewise((x, Eq(i, 1)), (0, True)) +
        Piecewise((1, Eq(j, 1)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, 2, 2)) == piecewise_fold(
        Piecewise((x, Eq(i, 2)), (0, True)) +
        Piecewise((1, Eq(j, 2)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, 3, 3)) == piecewise_fold(
        Piecewise((x, Eq(i, 3)), (0, True)) +
        Piecewise((1, Eq(j, 3)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, 1, l)) == piecewise_fold(
        Piecewise((x, And(S(1) <= i, i <= l)), (0, True)) +
        Piecewise((1, And(S(1) <= j, j <= l)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, l, 3)) == piecewise_fold(
        Piecewise((x, And(l <= i, i <= 3)), (0, True)) +
        Piecewise((1, And(l <= j, j <= 3)), (0, True)))
    assert ds(x*KD(i, k) + KD(j, k), (k, l, m)) == piecewise_fold(
        Piecewise((x, And(l <= i, i <= m)), (0, True)) +
        Piecewise((1, And(l <= j, j <= m)), (0, True)))

    assert ds(x*(KD(i, k) + KD(j, k)), (k, 1, 3)) == piecewise_fold(
        Piecewise((x, And(S(1) <= i, i <= 3)), (0, True)) +
        Piecewise((x, And(S(1) <= j, j <= 3)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, 1, 1)) == piecewise_fold(
        Piecewise((x, Eq(i, 1)), (0, True)) +
        Piecewise((x, Eq(j, 1)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, 2, 2)) == piecewise_fold(
        Piecewise((x, Eq(i, 2)), (0, True)) +
        Piecewise((x, Eq(j, 2)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, 3, 3)) == piecewise_fold(
        Piecewise((x, Eq(i, 3)), (0, True)) +
        Piecewise((x, Eq(j, 3)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, 1, l)) == piecewise_fold(
        Piecewise((x, And(S(1) <= i, i <= l)), (0, True)) +
        Piecewise((x, And(S(1) <= j, j <= l)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, l, 3)) == piecewise_fold(
        Piecewise((x, And(l <= i, i <= 3)), (0, True)) +
        Piecewise((x, And(l <= j, j <= 3)), (0, True)))
    assert ds(x*(KD(i, k) + KD(j, k)), (k, l, m)) == piecewise_fold(
        Piecewise((x, And(l <= i, i <= m)), (0, True)) +
        Piecewise((x, And(l <= j, j <= m)), (0, True)))

    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, 1, 3)) == piecewise_fold(
        Piecewise((x + y, And(S(1) <= i, i <= 3)), (0, True)) +
        Piecewise((x + y, And(S(1) <= j, j <= 3)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, 1, 1)) == piecewise_fold(
        Piecewise((x + y, Eq(i, 1)), (0, True)) +
        Piecewise((x + y, Eq(j, 1)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, 2, 2)) == piecewise_fold(
        Piecewise((x + y, Eq(i, 2)), (0, True)) +
        Piecewise((x + y, Eq(j, 2)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, 3, 3)) == piecewise_fold(
        Piecewise((x + y, Eq(i, 3)), (0, True)) +
        Piecewise((x + y, Eq(j, 3)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, 1, l)) == piecewise_fold(
        Piecewise((x + y, And(S(1) <= i, i <= l)), (0, True)) +
        Piecewise((x + y, And(S(1) <= j, j <= l)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, l, 3)) == piecewise_fold(
        Piecewise((x + y, And(l <= i, i <= 3)), (0, True)) +
        Piecewise((x + y, And(l <= j, j <= 3)), (0, True)))
    assert ds((x + y)*(KD(i, k) + KD(j, k)), (k, l, m)) == piecewise_fold(
        Piecewise((x + y, And(l <= i, i <= m)), (0, True)) +
        Piecewise((x + y, And(l <= j, j <= m)), (0, True)))

    assert ds(x*y + x*KD(i, j), (j, 1, 3)) == \
        Piecewise((3*x*y + x, And(S(1) <= i, i <= 3)), (3*x*y, True))
    assert ds(x*y + x*KD(i, j), (j, 1, 1)) == \
        Piecewise((x*y + x, Eq(i, 1)), (x*y, True))
    assert ds(x*y + x*KD(i, j), (j, 2, 2)) == \
        Piecewise((x*y + x, Eq(i, 2)), (x*y, True))
    assert ds(x*y + x*KD(i, j), (j, 3, 3)) == \
        Piecewise((x*y + x, Eq(i, 3)), (x*y, True))
    assert ds(x*y + x*KD(i, j), (j, 1, k)) == \
        Piecewise((k*x*y + x, And(S(1) <= i, i <= k)), (k*x*y, True))
    assert ds(x*y + x*KD(i, j), (j, k, 3)) == \
        Piecewise(((4 - k)*x*y + x, And(k <= i, i <= 3)), ((4 - k)*x*y, True))
    assert ds(x*y + x*KD(i, j), (j, k, l)) == Piecewise(
        ((l - k + 1)*x*y + x, And(k <= i, i <= l)), ((l - k + 1)*x*y, True))

    assert ds(x*(y + KD(i, j)), (j, 1, 3)) == \
        Piecewise((3*x*y + x, And(S(1) <= i, i <= 3)), (3*x*y, True))
    assert ds(x*(y + KD(i, j)), (j, 1, 1)) == \
        Piecewise((x*y + x, Eq(i, 1)), (x*y, True))
    assert ds(x*(y + KD(i, j)), (j, 2, 2)) == \
        Piecewise((x*y + x, Eq(i, 2)), (x*y, True))
    assert ds(x*(y + KD(i, j)), (j, 3, 3)) == \
        Piecewise((x*y + x, Eq(i, 3)), (x*y, True))
    assert ds(x*(y + KD(i, j)), (j, 1, k)) == \
        Piecewise((k*x*y + x, And(S(1) <= i, i <= k)), (k*x*y, True))
    assert ds(x*(y + KD(i, j)), (j, k, 3)) == \
        Piecewise(((4 - k)*x*y + x, And(k <= i, i <= 3)), ((4 - k)*x*y, True))
    assert ds(x*(y + KD(i, j)), (j, k, l)) == Piecewise(
        ((l - k + 1)*x*y + x, And(k <= i, i <= l)), ((l - k + 1)*x*y, True))

    assert ds(x*(y + 2*KD(i, j)), (j, 1, 3)) == \
        Piecewise((3*x*y + 2*x, And(S(1) <= i, i <= 3)), (3*x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, 1, 1)) == \
        Piecewise((x*y + 2*x, Eq(i, 1)), (x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, 2, 2)) == \
        Piecewise((x*y + 2*x, Eq(i, 2)), (x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, 3, 3)) == \
        Piecewise((x*y + 2*x, Eq(i, 3)), (x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, 1, k)) == \
        Piecewise((k*x*y + 2*x, And(S(1) <= i, i <= k)), (k*x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, k, 3)) == Piecewise(
        ((4 - k)*x*y + 2*x, And(k <= i, i <= 3)), ((4 - k)*x*y, True))
    assert ds(x*(y + 2*KD(i, j)), (j, k, l)) == Piecewise(
        ((l - k + 1)*x*y + 2*x, And(k <= i, i <= l)), ((l - k + 1)*x*y, True))

    assert ds((x + y)*(y + KD(i, j)), (j, 1, 3)) == Piecewise(
        (3*(x + y)*y + x + y, And(S(1) <= i, i <= 3)), (3*(x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, 1, 1)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 1)), ((x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, 2, 2)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 2)), ((x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, 3, 3)) == \
        Piecewise(((x + y)*y + x + y, Eq(i, 3)), ((x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, 1, k)) == Piecewise(
        (k*(x + y)*y + x + y, And(S(1) <= i, i <= k)), (k*(x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, k, 3)) == Piecewise(
        ((4 - k)*(x + y)*y + x + y, And(k <= i, i <= 3)),
        ((4 - k)*(x + y)*y, True))
    assert ds((x + y)*(y + KD(i, j)), (j, k, l)) == Piecewise(
        ((l - k + 1)*(x + y)*y + x + y, And(k <= i, i <= l)),
        ((l - k + 1)*(x + y)*y, True))

    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, 1, 3)) == piecewise_fold(
        Piecewise((KD(i, k) + x, And(S(1) <= i, i <= 3)), (0, True)) +
        3*(KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, 1, 1)) == piecewise_fold(
        Piecewise((KD(i, k) + x, Eq(i, 1)), (0, True)) +
        (KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, 2, 2)) == piecewise_fold(
        Piecewise((KD(i, k) + x, Eq(i, 2)), (0, True)) +
        (KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, 3, 3)) == piecewise_fold(
        Piecewise((KD(i, k) + x, Eq(i, 3)), (0, True)) +
        (KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, 1, k)) == piecewise_fold(
        Piecewise((KD(i, k) + x, And(S(1) <= i, i <= k)), (0, True)) +
        k*(KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, k, 3)) == piecewise_fold(
        Piecewise((KD(i, k) + x, And(k <= i, i <= 3)), (0, True)) +
        (4 - k)*(KD(i, k) + x)*y)
    assert ds((x + KD(i, k))*(y + KD(i, j)), (j, k, l)) == piecewise_fold(
        Piecewise((KD(i, k) + x, And(k <= i, i <= l)), (0, True)) +
        (l - k + 1)*(KD(i, k) + x)*y)
コード例 #13
0
ファイル: exponential.py プロジェクト: cosmosZhou/sagemath
 def definition(self):
     from sympy.concrete.summations import Sum
     x = self.arg
     return exp(x) / Sum(exp(x))