Beispiel #1
0
def test_powdenest_polar():
    from sympy import powdenest
    x, y, z = symbols('x y z', polar=True)
    a, b, c = symbols('a b c')
    assert powdenest((x * y * z)**a) == x**a * y**a * z**a
    assert powdenest((x**a * y**b)**c) == x**(a * c) * y**(b * c)
    assert powdenest(((x**a)**b * y**c)**c) == x**(a * b * c) * y**(c**2)
Beispiel #2
0
def test_powdenest_fail_in_polys():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    assert powdenest((((x**2 * y**4)**a)**(x * y)),
                     force=True) == (x**2 * y**4)**(a * x * y)
    assert powdenest((((x**2 * y**4)**a)**(x * y))**3,
                     force=True) == (x**2 * y**4)**(3 * a * x * y)
Beispiel #3
0
def test_powdenest_polar():
    from sympy import powdenest
    x, y, z = symbols('x y z', polar=True)
    a, b, c = symbols('a b c')
    assert powdenest((x*y*z)**a) == x**a*y**a*z**a
    assert powdenest((x**a*y**b)**c) == x**(a*c)*y**(b*c)
    assert powdenest(((x**a)**b*y**c)**c) == x**(a*b*c)*y**(c**2)
Beispiel #4
0
def test_issue_19627():
    # if you use force the user must verify
    assert powdenest(sqrt(sin(x)**2), force=True) == sin(x)
    assert powdenest((x**(S.Half / y))**(2 * y), force=True) == x
    from sympy import expand_power_base
    e = 1 - a
    expr = (exp(z / e) * x**(b / e) * y**((1 - b) / e))**e
    assert powdenest(expand_power_base(expr, force=True),
                     force=True) == x**b * y**(1 - b) * exp(z)
Beispiel #5
0
def calc_gaussian_errorprop_single(equation, datadict):
    err_expr = 0
    calc = {}
    for n, data in datadict.items():
        if isinstance(data, tuple):
            if not len(data) == 2:
                raise ValueError(
                    "{} tuple does not have two elements!".format(n))
        else:
            raise ValueError("{} data is not a 2-tuple!".format(n))
        # generate derivates
        symbol = sy.Symbol(n)
        symbol_err = sy.Symbol("sigma_{}".format(n))
        symbol_der = sy.diff(equation, symbol)
        symbol_der = symbol_der * symbol_err

        calc[n] = (symbol, symbol_der, symbol_err)
        err_expr = err_expr + symbol_der**2

    err_expr = sy.simplify(sy.sqrt(err_expr))
    err_expr = sy.powdenest(err_expr, force=True)

    nominal = equation
    deviation = err_expr

    # calculate nominal + deviate values
    for n, d in datadict.items():
        nominal = nominal.subs(calc[n][0], str(d[0]))
        deviation = deviation.subs(calc[n][0], str(d[0]))
        deviation = deviation.subs(calc[n][2], str(d[1]))

    return nominal, deviation
Beispiel #6
0
 def mysimp(expr):
     return powsimp(
         powdenest(expand(
             unpolarify(
                 simplify(
                     expand(combsimp(expand_func(
                         expr.rewrite(besselj))))))),
                   polar=True))
Beispiel #7
0
def test_branch_bug():
    from sympy import powdenest, lowergamma
    # TODO gammasimp cannot prove that the factor is unity
    assert powdenest(integrate(erf(x**3), x, meijerg=True).diff(x),
           polar=True) == 2*erf(x**3)*gamma(S(2)/3)/3/gamma(S(5)/3)
    assert integrate(erf(x**3), x, meijerg=True) == \
        2*x*erf(x**3)*gamma(S(2)/3)/(3*gamma(S(5)/3)) \
        - 2*gamma(S(2)/3)*lowergamma(S(2)/3, x**6)/(3*sqrt(pi)*gamma(S(5)/3))
Beispiel #8
0
def test_branch_bug():
    from sympy import powdenest, lowergamma
    # TODO combsimp cannot prove that the factor is unity
    assert powdenest(integrate(erf(x**3), x, meijerg=True).diff(x),
           polar=True) == 2*erf(x**3)*gamma(S(2)/3)/3/gamma(S(5)/3)
    assert integrate(erf(x**3), x, meijerg=True) == \
        2*x*erf(x**3)*gamma(S(2)/3)/(3*gamma(S(5)/3)) \
        - 2*gamma(S(2)/3)*lowergamma(S(2)/3, x**6)/(3*sqrt(pi)*gamma(S(5)/3))
Beispiel #9
0
def Lame(metric, evalutation=0, contra=1):
    '''
    calculate Lame coefficients from contravariant metric tensor g^{ij}
    where Hi = sqrt(g_{ii}) = 1/sqrt(g^{ii})
    contra == 1: J = sqrt(|g_{ij}|)
    contra == 0: J = sqrt(|g^{ij}|)
    [email protected] 2020.06.17
    '''
    if contra == 1:
        # calculate Lame coefficients from cotra-variant metric tensor
        co_metric = metric.inv(method='GE')
        H = [
            sympy.powdenest(sympy.sqrt(co_metric[0, 0]), force=True),
            sympy.powdenest(sympy.sqrt(co_metric[1, 1]), force=True),
            sympy.powdenest(sympy.sqrt(co_metric[2, 2]), force=True)
        ]
    elif contra == 0:
        # calculate Lame coefficients from co-variant metric tensor
        H = [
            sympy.powdenest(sympy.sqrt(metric[0, 0]), force=True),
            sympy.powdenest(sympy.sqrt(metric[1, 1]), force=True),
            sympy.powdenest(sympy.sqrt(metric[2, 2]), force=True)
        ]

    return H
Beispiel #10
0
def test_bessel():
    from sympy import (besselj, Heaviside, besseli, polar_lift, exp_polar,
                       powdenest)
    assert simplify(integrate(besselj(a, z)*besselj(b, z)/z, (z, 0, oo),
                     meijerg=True, conds='none')) == \
           2*sin(pi*a/2 - pi*b/2)/(pi*(a - b)*(a + b))
    assert simplify(integrate(besselj(a, z)*besselj(a, z)/z, (z, 0, oo),
                     meijerg=True, conds='none')) == 1/(2*a)

    # TODO more orthogonality integrals

    # TODO there is some improvement possible here:
    #  - the result can be simplified to besselj(y, z))
    assert powdenest(simplify(integrate(sin(z*x)*(x**2-1)**(-(y+S(1)/2)),
                              (x, 1, oo), meijerg=True, conds='none')
                              *2/((z/2)**y*sqrt(pi)*gamma(S(1)/2-y))),
                     polar=True) == \
           exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))

    # Werner Rosenheinrich
    # SOME INDEFINITE INTEGRALS OF BESSEL FUNCTIONS

    assert integrate(x*besselj(0, x), x, meijerg=True) == x*besselj(1, x)
    assert integrate(x*besseli(0, x), x, meijerg=True) == x*besseli(1, x)
    # TODO can do higher powers, but come out as high order ... should they be
    #      reduced to order 0, 1?
    assert integrate(besselj(1, x), x, meijerg=True) == -besselj(0, x)
    assert integrate(besselj(1, x)**2/x, x, meijerg=True) == \
           -(besselj(0, x)**2 + besselj(1, x)**2)/2
    # TODO more besseli when tables are extended or recursive mellin works
    assert integrate(besselj(0, x)**2/x**2, x, meijerg=True) == \
           -2*x*besselj(0, x)**2 - 2*x*besselj(1, x)**2 \
           + 2*besselj(0, x)*besselj(1, x) - besselj(0, x)**2/x
    assert integrate(besselj(0, x)*besselj(1, x), x, meijerg=True) == \
           -besselj(0, x)**2/2
    assert integrate(x**2*besselj(0, x)*besselj(1, x), x, meijerg=True) == \
           x**2*besselj(1, x)**2/2
    assert integrate(besselj(0, x)*besselj(1, x)/x, x, meijerg=True) == \
           (x*besselj(0, x)**2 + x*besselj(1, x)**2 - \
            besselj(0, x)*besselj(1, x))
    # TODO how does besselj(0, a*x)*besselj(0, b*x) work?
    # TODO how does besselj(0, x)**2*besselj(1, x)**2 work?
    # TODO sin(x)*besselj(0, x) etc come out a mess
    # TODO can x*log(x)*besselj(0, x) be done?
    # TODO how does besselj(1, x)*besselj(0, x+a) work?
    # TODO more indefinite integrals when struve functions etc are implemented

    # test a substitution
    assert integrate(besselj(1, x**2)*x, x, meijerg=True) == \
        -besselj(0, x**2)/2
Beispiel #11
0
def test_bessel():
    from sympy import (besselj, Heaviside, besseli, polar_lift, exp_polar,
                       powdenest)
    assert simplify(integrate(besselj(a, z)*besselj(b, z)/z, (z, 0, oo),
                     meijerg=True, conds='none')) == \
           2*sin(pi*a/2 - pi*b/2)/(pi*(a - b)*(a + b))
    assert simplify(integrate(besselj(a, z)*besselj(a, z)/z, (z, 0, oo),
                     meijerg=True, conds='none')) == 1/(2*a)

    # TODO more orthogonality integrals

    # TODO there is some improvement possible here:
    #  - the result can be simplified to besselj(y, z))
    assert powdenest(simplify(integrate(sin(z*x)*(x**2-1)**(-(y+S(1)/2)),
                              (x, 1, oo), meijerg=True, conds='none')
                              *2/((z/2)**y*sqrt(pi)*gamma(S(1)/2-y))),
                     polar=True) == \
           exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))

    # Werner Rosenheinrich
    # SOME INDEFINITE INTEGRALS OF BESSEL FUNCTIONS

    assert integrate(x*besselj(0, x), x, meijerg=True) == x*besselj(1, x)
    assert integrate(x*besseli(0, x), x, meijerg=True) == x*besseli(1, x)
    # TODO can do higher powers, but come out as high order ... should they be
    #      reduced to order 0, 1?
    assert integrate(besselj(1, x), x, meijerg=True) == -besselj(0, x)
    assert integrate(besselj(1, x)**2/x, x, meijerg=True) == \
           -(besselj(0, x)**2 + besselj(1, x)**2)/2
    # TODO more besseli when tables are extended or recursive mellin works
    assert integrate(besselj(0, x)**2/x**2, x, meijerg=True) == \
           -2*x*besselj(0, x)**2 - 2*x*besselj(1, x)**2 \
           + 2*besselj(0, x)*besselj(1, x) - besselj(0, x)**2/x
    assert integrate(besselj(0, x)*besselj(1, x), x, meijerg=True) == \
           -besselj(0, x)**2/2
    assert integrate(x**2*besselj(0, x)*besselj(1, x), x, meijerg=True) == \
           x**2*besselj(1, x)**2/2
    assert integrate(besselj(0, x)*besselj(1, x)/x, x, meijerg=True) == \
           (x*besselj(0, x)**2 + x*besselj(1, x)**2 - \
            besselj(0, x)*besselj(1, x))
    # TODO how does besselj(0, a*x)*besselj(0, b*x) work?
    # TODO how does besselj(0, x)**2*besselj(1, x)**2 work?
    # TODO sin(x)*besselj(0, x) etc come out a mess
    # TODO can x*log(x)*besselj(0, x) be done?
    # TODO how does besselj(1, x)*besselj(0, x+a) work?
    # TODO more indefinite integrals when struve functions etc are implemented

    # test a substitution
    assert integrate(besselj(1, x**2)*x, x, meijerg=True) == \
        -besselj(0, x**2)/2
def extra_simple(mul):
    """Simplification of pysb rates
    
    Arguments:
        mul: psyb reaction (sympy.Mul object)
    
    Returns:
        a simplified version denested of exponents. 
        TODO: make sure it fully simplifies!
    """
    return sp.powsimp((sp.expand_power_base(sp.powdenest(sp.logcombine(
        sp.expand_log(mul.simplify(), force=True), force=True),
                                                         force=True),
                                            force=True)),
                      force=True)
Beispiel #13
0
def Jacobian(metric, evaluation=0, contra=1):
    '''
    calculate Jacobian from contravariant metric tensor g^{ij}
    where J = sqrt(|g_{ij}}) = 1/sqrt(|g^{ij}|)
    contra == 1: J = sqrt(|g_{ij}|)
    contra == 0: J = sqrt(|g^{ij}|)
    [email protected] 2020.06.08
    '''
    # Jacob = sympy.powdenest(sympy.sqrt(metric[0,0]*(metric[1,1]*metric[2,2] - metric[1,2]*metric[2,1]) - metric[0,1]*(metric[1,0]*metric[2,2] - metric[1,2]*metric[2,0]) + metric[0,2]*(metric[1,0]*metric[2,1] - metric[1,1]*metric[2,0])), force=True)
    Jacob = sympy.powdenest(sympy.sqrt(metric.det()), force=True)

    if contra == 1:
        J = Jacob**-1
    else:
        J = Jacob

    return J
Beispiel #14
0
def get_s23zero_limit(exp):

    lexp = (exp.subs(s23, 0)).as_ordered_terms()
    new_exp = 0
    bar = BAR("proceesing", len(lexp))
    for term in lexp:
        dummy1 = 1
        for f in term.as_ordered_factors():
            dummy2 = 0
            for ft in f.as_ordered_terms():
                dummy2 += sp.powdenest(ft, force=True).simplify()
            dummy1 *= dummy2
        new_exp += dummy1
        bar.next()
    bar.finish()

    lexp = new_exp.as_ordered_terms()
    new_exp = sp.S(0)
    for term in lexp:
        if evaluate(term, 0, verb=False) == 0: continue
        new_exp += term.replace(sp.log(a), sp.log(sp.Abs(a)))

    return new_exp
Beispiel #15
0
def test_powdenest():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    p, q = symbols('p q', positive=True)
    i, j = symbols('i,j', integer=True)

    assert powdenest(x) == x
    assert powdenest(x + 2*(x**(2*a/3))**(3*x)) == (x + 2*(x**(2*a/3))**(3*x))
    assert powdenest((exp(2*a/3))**(3*x)) # -X-> (exp(a/3))**(6*x)
    assert powdenest((x**(2*a/3))**(3*x)) == ((x**(2*a/3))**(3*x))
    assert powdenest(exp(3*x*log(2))) == 2**(3*x)
    assert powdenest(sqrt(p**2)) == p
    i, j = symbols('i,j', integer=True)
    eq = p**(2*i)*q**(4*i)
    assert powdenest(eq) == (p*q**2)**(2*i)
    assert powdenest((x**x)**(i + j)) # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
    assert powdenest(exp(3*y*log(x))) == x**(3*y)
    assert powdenest(exp(y*(log(a) + log(b)))) == (a*b)**y
    assert powdenest(exp(3*(log(a) + log(b)))) == a**3*b**3
    assert powdenest(((x**(2*i))**(3*y))**x) == ((x**(2*i))**(3*y))**x
    assert powdenest(((x**(2*i))**(3*y))**x, force=True) == x**(6*i*x*y)
    assert powdenest(((x**(2*a/3))**(3*y/i))**x) == (((x**(2*a/3))**(3*y/i))**x)
    assert powdenest((x**(2*i)*y**(4*i))**z, force=True) == (x*y**2)**(2*i*z)
    assert powdenest((p**(2*i)*q**(4*i))**j) == (p*q**2)**(2*i*j)
    assert powdenest(((p**(2*a))**(3*y))**x) == p**(6*a*x*y)
    e = ((x**2*y**4)**a)**(x*y)
    assert powdenest(e) == e
    e = (((x**2*y**4)**a)**(x*y))**3
    assert powdenest(e) == ((x**2*y**4)**a)**(3*x*y)
    assert powdenest((((x**2*y**4)**a)**(x*y)), force=True) == (x*y**2)**(2*a*x*y)
    assert powdenest((((x**2*y**4)**a)**(x*y))**3, force=True) == (x*y**2)**(6*a*x*y)
    assert powdenest((x**2*y**6)**i) != (x*y**3)**(2*i)
    x, y = symbols('x,y', positive=True)
    assert powdenest((x**2*y**6)**i) == (x*y**3)**(2*i)
    assert powdenest((x**(2*i/3)*y**(i/2))**(2*i)) == (x**(S(4)/3)*y)**(i**2)
    assert powdenest(sqrt(x**(2*i)*y**(6*i))) == (x*y**3)**i
    # issue 2706
    assert powdenest(((gamma(x)*hyper((),(),x))*pi)**2) == (pi*gamma(x)*hyper((), (), x))**2

    assert powdenest(4**x) == 2**(2*x)
    assert powdenest((4**x)**y) == 2**(2*x*y)
    assert powdenest(4**x*y) == 2**(2*x)*y
Beispiel #16
0
def test_powdenest():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    p = symbols('p', positive=True)
    i, j = symbols('i,j', integer=1)

    assert powdenest(x) == x
    assert powdenest(x + 2*(x**(2*a/3))**(3*x)) == x + 2*(x**(a/3))**(6*x)
    assert powdenest((exp(2*a/3))**(3*x)) == (exp(a/3))**(6*x)
    assert powdenest((x**(2*a/3))**(3*x)) == (x**(a/3))**(6*x)
    assert powdenest(exp(3*x*log(2))) == 2**(3*x)
    assert powdenest(sqrt(p**2)) == p
    i, j = symbols('i,j', integer=1)
    assert powdenest((x**x)**(i + j)) # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
    assert powdenest(exp(3*y*log(x))) == x**(3*y)
    assert powdenest(exp(y*(log(a) + log(b)))) == (a*b)**y
    assert powdenest(exp(3*(log(a) + log(b)))) == a**3*b**3
    assert powdenest(((x**(2*i))**(3*y))**x) == ((x**(2*i))**(3*y))**x
    assert powdenest(((x**(2*i))**(3*y))**x, force=True) == x**(6*i*x*y)
    assert powdenest(((x**(2*a/3))**(3*y/i))**x) == ((x**(a/3))**(y/i))**(6*x)
    assert powdenest((x**(2*i)*y**(4*i))**z, force=True) == (x*y**2)**(2*i*z)
    e = ((x**2*y**4)**a)**(x*y)
    assert powdenest(e) == e
    e = (((x**2*y**4)**a)**(x*y))**3
    assert powdenest(e) == ((x**2*y**4)**a)**(3*x*y)
Beispiel #17
0
def test_powdenest_fail_in_polys():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    assert powdenest((((x**2*y**4)**a)**(x*y)), force=True) == (x**2*y**4)**(a*x*y)
    assert powdenest((((x**2*y**4)**a)**(x*y))**3, force=True) == (x**2*y**4)**(3*a*x*y)
Beispiel #18
0
def test_inverse_mellin_transform():
    from sympy import (sin, simplify, expand_func, powsimp, Max, Min, expand,
                       powdenest, powsimp, exp_polar)
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x)
    assert IMT(s/(2*s**2 - 2), s, x, (2, oo)) \
           == (x**2/2 + S(1)/2)*Heaviside(1 - x)/(2*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) \
           == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) \
           == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x

    # test factorisation of polys
    assert simplify(expand_func(IMT(1/(s**2 + 1), s, exp(-x),
                                    (None, oo))).rewrite(sin)) \
           == sin(x)*Heaviside(1 - exp(-x))

    # test multiplicative substitution
    a, b = symbols('a b', positive=True)
    c, d = symbols('c d')
    assert IMT(b**(-s / a) * factorial(s / a) / s, s, x,
               (0, oo)) == exp(-b * x**a)
    assert IMT(factorial(a / b + s / b) / (a + s), s, x,
               (-a, oo)) == x**a * exp(-x**b)

    from sympy import expand_mul

    def simp_pows(expr):
        return expand_mul(simplify(powsimp(expr, force=True)),
                          deep=True).replace(exp_polar, exp)  # XXX ?

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1)
    assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
           == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1-beta-s)/gamma(1-s),
                         s, x, (-oo, None))) \
           == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho-s)/gamma(rho), s, x, (0, None))) \
           == (1/(x + 1))**rho
    # TODO should this simplify further?
    assert simp_pows(IMT(d**c*d**(s-1)*sin(pi*c) \
                         *gamma(s)*gamma(s+c)*gamma(1-s)*gamma(1-s-c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
           == d**c*(x/d)**c/(-d + x) - d**c/(-d + x)

    # TODO is calling simplify twice a bug?
    assert simplify(simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1-c)/2 - s) \
                                 *gamma(-c/2-s)/gamma(1-c-s),
                                 s, x, (0, -re(c)/2)))) == \
           (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s) \
                        /gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
           (b + sqrt(b**2 + x))**(a - 1)*(b**2 + b*sqrt(b**2 + x) + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s) \
                          / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
           (b + sqrt(b**2 + x))**c

    # Section 8.4.5
    assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
           log(x)**3*Heaviside(x - 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi / (s * sin(2 * pi * s)), s, x,
               (-S(1) / 2, 0)) == log(sqrt(x) + 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x)

    # TODO
    def mysimp(expr):
        from sympy import expand, logcombine, powsimp
        return expand(powsimp(logcombine(expr, force=True),
                              force=True,
                              deep=True),
                      force=True).replace(exp_polar, exp)
    assert mysimp(mysimp(IMT(pi/(s*tan(pi*s)), s, x, (-1, 0)))) == \
           log(1-x)*Heaviside(1-x) + log(x-1)*Heaviside(x-1)
    assert mysimp(IMT(pi/(s*tan(pi*s)), s, x, (0, 1))) == \
           log(1/x - 1)*Heaviside(1-x) + log(1 - 1/x)*Heaviside(x-1)

    # 8.4.14
    assert IMT(-gamma(s + S(1)/2)/(sqrt(pi)*s), s, x, (-S(1)/2, 0)) == \
           erf(sqrt(x))

    # 8.4.19
    # TODO these come out ugly
    def mysimp(expr):
        return expand(
            unpolarify(simplify(expand(expand_func(expr.rewrite(besselj))))))
    assert mysimp(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, S(3)/4))) \
           == besselj(a, 2*sqrt(x)*polar_lift(-1))*exp(-I*pi*a)
    assert mysimp(IMT(2**a*gamma(S(1)/2 - 2*s)*gamma(s + (a + 1)/2) \
                      / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-(re(a) + 1)/2, S(1)/4))) == \
           exp(-I*pi*a)*sin(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1))
    assert mysimp(IMT(2**a*gamma(a/2 + s)*gamma(S(1)/2 - 2*s) \
                      / (gamma(S(1)/2 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-re(a)/2, S(1)/4))) == \
           exp(-I*pi*a)*cos(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1))
    # TODO this comes out as an amazing mess, but surprisingly enough mysimp is
    #      effective ...
    assert powsimp(powdenest(mysimp(IMT(gamma(a + s)*gamma(S(1)/2 - s) \
                      / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                      s, x, (-re(a), S(1)/2))), polar=True)) == \
           exp(-2*I*pi*a)*besselj(a, sqrt(x)*polar_lift(-1))**2
    # NOTE the next is indeed an even function of sqrt(x), so the result is
    #      correct
    assert mysimp(IMT(gamma(s)*gamma(S(1)/2 - s) \
                      / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                      s, x, (0, S(1)/2))) == \
           besselj(-a, polar_lift(-1)*sqrt(x))*besselj(a, polar_lift(-1)*sqrt(x))
    assert mysimp(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) \
                      / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) \
                         *gamma(a/2 + b/2 - s + 1)),
                      s, x, (-(re(a) + re(b))/2, S(1)/2))) == \
           exp(-I*pi*a)*exp(-I*pi*b)*besselj(a, sqrt(x)*polar_lift(-1)) \
                                    *besselj(b, sqrt(x)*polar_lift(-1))
Beispiel #19
0
def test_powdenest_polar():
    x, y, z = symbols("x y z", polar=True)
    a, b, c = symbols("a b c")
    assert powdenest((x * y * z)**a) == x**a * y**a * z**a
    assert powdenest((x**a * y**b)**c) == x**(a * c) * y**(b * c)
    assert powdenest(((x**a)**b * y**c)**c) == x**(a * b * c) * y**(c**2)
Beispiel #20
0
                                               4 * x)
print(sympy.apart(expr))
# 三角化简 trigsimp
expr = sympy.sin(x) / sympy.cos(x)
print(sympy.trigsimp(expr))
# 三角展开 expand_trig
expr = sympy.sin(x + y)
print(sympy.expand_trig(expr))
# 指数化简 powsimp / 指数展开 expand_power_exp
a, b = sympy.symbols('a b')
expr = x**a * x**b
print(sympy.powsimp(expr))
# 化简指数的指数 powdenest
# 必须满足条件 底数 positive=True
x = sympy.symbols('x', positive=True)
expr = (x**a)**b
print(sympy.powdenest(expr))
# 对数展开 expand_log / 对数合并 logcombine
# 需要指出 log ln 在 sympy 中都是自然对数
# symbol 也需要满足条件
x, y = sympy.symbols('x y', positive=True)
n = sympy.symbols('n', real=True)
print(sympy.expand_log(sympy.log(x**n)))
print(sympy.expand_log(sympy.log(x * y)))

# more:
# series-泰勒展开函数
# diff-求导
# pprint-将公式用更好看的格式打印出来
sympy.pprint(x / (y + 1))
Beispiel #21
0
    def test_integrate_pole_part(self):
        for j in (0, 1):
            for i, I_j_before in enumerate([
                    Product(self.monomial_product1, self.regulator_poles,
                            self.cal_I),
                    Product(self.monomial_product2, self.regulator_poles,
                            self.cal_I)
            ]):
                I_j_after = integrate_pole_part(I_j_before, j)
                I_j_pole_part = Sum(*I_j_after[:-1])
                I_j_numerically_integrable_part = I_j_after[-1]

                if j == 0:
                    expected_pole_part = sympify_expression('''
                                                         (  1/(-2 + 0 + 1 - eps0 - 3*eps1) * (A + B*x0 + C*x0**2*x1 + D*x1) +
                                                            1/(-2 + 1 + 1 - eps0 - 3*eps1) * (B + 2*C*x0*x1)  ) * (x1**2)**(-2 - eps0 - 3*eps1)
                                                    ''').subs('x0', 0)

                    expected_numerical_integrand = (
                        sympify_expression(str(self.cal_I)) -
                        sympify_expression('A + D*x1 + x0*B')
                    ) * sympify_expression(str(self.exponentiated_monomial1))

                    if i == 0:
                        for summand in I_j_pole_part.summands:
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[0].expolist,
                                [(0, 2, 0, 0)])
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[0].coeffs, [1])
                            self.assertEqual(
                                sympify_expression(
                                    str(summand.factors[0].factors[0].exponent)
                                ) - sympify_expression('-2 - eps0 - 3*eps1'),
                                0)
                    elif i == 1:
                        for summand in I_j_pole_part.summands:
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[0].expolist,
                                [(0, 2, 0, 0)])
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[1].expolist,
                                [(0, 4, 0, 0)])
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[0].coeffs, [1])
                            np.testing.assert_array_equal(
                                summand.factors[0].factors[1].coeffs, [1])
                            self.assertEqual(
                                sympify_expression(
                                    str(summand.factors[0].factors[0].exponent)
                                ) - sympify_expression(-2), 0)
                            self.assertEqual(
                                sympify_expression(
                                    str(summand.factors[0].factors[1].exponent)
                                ) - sympify_expression('- eps0 - 3*eps1') / 2,
                                0)
                    else:
                        raise IndexError('`i` should only run over `(0,1)`!')

                elif j == 1:
                    expected_pole_part = sympify_expression('''
                                                         (  1/( 2*(-2 - eps0 - 3*eps1) + 0 + 1 ) * (A + B*x0 + C*x0**2*x1 + D*x1) +
                                                            1/( 2*(-2 - eps0 - 3*eps1) + 1 + 1 ) * (C*x0**2 + D) +
                                                            0  ) * x0**(-2 - eps0 - 3*eps1)
                                                    ''').subs('x1', 0)

                    expected_numerical_integrand = (
                        sympify_expression(str(self.cal_I)) -
                        sympify_expression('A + B*x0 + C*x0**2*x1 + D*x1')
                    ) * sympify_expression(str(self.exponentiated_monomial1))

                else:
                    raise IndexError('`j` should only run over `(0,1)`!')

                self.assertEqual(
                    sp.powdenest((sympify_expression(str(I_j_pole_part)) -
                                  expected_pole_part),
                                 force=True).simplify(), 0)
                for summand in I_j_pole_part.summands:
                    self.assertEqual(type(summand), Product)
                    self.assertEqual(type(summand.factors[0]), Product)
                    for factor in summand.factors[0].factors:
                        self.assertEqual(type(factor), ExponentiatedPolynomial)

                self.assertEqual(type(I_j_numerically_integrable_part),
                                 Product)
                self.assertEqual(
                    type(I_j_numerically_integrable_part.factors[0]), Product)
                for factor in I_j_numerically_integrable_part.factors[
                        0].factors:
                    self.assertEqual(type(factor), ExponentiatedPolynomial)
                should_be_zero = (
                    sympify_expression(I_j_numerically_integrable_part) -
                    expected_numerical_integrand).simplify()
                if i == 1:
                    # need some simplifications that are not generally true for all complex numbers
                    # see http://docs.sympy.org/dev/tutorial/simplification.html#powers for a discussion
                    should_be_zero = sp.expand_power_base(should_be_zero,
                                                          force=True)
                    should_be_zero = sp.powdenest(should_be_zero, force=True)
                self.assertEqual(should_be_zero, 0)
Beispiel #22
0
def test_powdenest():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    p, q = symbols('p q', positive=True)
    i, j = symbols('i,j', integer=True)

    assert powdenest(x) == x
    assert powdenest(x + 2 * (x**(a * Rational(2, 3)))**(3 * x)) == (
        x + 2 * (x**(a * Rational(2, 3)))**(3 * x))
    assert powdenest(
        (exp(a * Rational(2, 3)))**(3 * x))  # -X-> (exp(a/3))**(6*x)
    assert powdenest(
        (x**(a * Rational(2, 3)))**(3 * x)) == ((x**(a * Rational(2, 3)))**(3 *
                                                                            x))
    assert powdenest(exp(3 * x * log(2))) == 2**(3 * x)
    assert powdenest(sqrt(p**2)) == p
    eq = p**(2 * i) * q**(4 * i)
    assert powdenest(eq) == (p * q**2)**(2 * i)
    # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
    assert powdenest((x**x)**(i + j))
    assert powdenest(exp(3 * y * log(x))) == x**(3 * y)
    assert powdenest(exp(y * (log(a) + log(b)))) == (a * b)**y
    assert powdenest(exp(3 * (log(a) + log(b)))) == a**3 * b**3
    assert powdenest(((x**(2 * i))**(3 * y))**x) == ((x**(2 * i))**(3 * y))**x
    assert powdenest(((x**(2 * i))**(3 * y))**x,
                     force=True) == x**(6 * i * x * y)
    assert powdenest(((x**(a*Rational(2, 3)))**(3*y/i))**x) == \
        (((x**(a*Rational(2, 3)))**(3*y/i))**x)
    assert powdenest((x**(2 * i) * y**(4 * i))**z,
                     force=True) == (x * y**2)**(2 * i * z)
    assert powdenest((p**(2 * i) * q**(4 * i))**j) == (p * q**2)**(2 * i * j)
    e = ((p**(2 * a))**(3 * y))**x
    assert powdenest(e) == e
    e = ((x**2 * y**4)**a)**(x * y)
    assert powdenest(e) == e
    e = (((x**2 * y**4)**a)**(x * y))**3
    assert powdenest(e) == ((x**2 * y**4)**a)**(3 * x * y)
    assert powdenest((((x**2*y**4)**a)**(x*y)), force=True) == \
        (x*y**2)**(2*a*x*y)
    assert powdenest((((x**2*y**4)**a)**(x*y))**3, force=True) == \
        (x*y**2)**(6*a*x*y)
    assert powdenest((x**2 * y**6)**i) != (x * y**3)**(2 * i)
    x, y = symbols('x,y', positive=True)
    assert powdenest((x**2 * y**6)**i) == (x * y**3)**(2 * i)

    assert powdenest((x**(i * Rational(2, 3)) *
                      y**(i / 2))**(2 * i)) == (x**Rational(4, 3) * y)**(i**2)
    assert powdenest(sqrt(x**(2 * i) * y**(6 * i))) == (x * y**3)**i

    assert powdenest(4**x) == 2**(2 * x)
    assert powdenest((4**x)**y) == 2**(2 * x * y)
    assert powdenest(4**x * y) == 2**(2 * x) * y
Beispiel #23
0
 def mysimp(expr):
     return powsimp(powdenest(expand(unpolarify(simplify(expand(combsimp(expand_func(expr.rewrite(besselj))))))), polar=True))
Beispiel #24
0
def test_powdenest():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    p, q = symbols('p q', positive=True)
    i, j = symbols('i,j', integer=True)

    assert powdenest(x) == x
    assert powdenest(x + 2 * (x**(2 * a / 3))**
                     (3 * x)) == x + 2 * (x**(a / 3))**(6 * x)
    assert powdenest((exp(2 * a / 3))**(3 * x)) == (exp(a / 3))**(6 * x)
    assert powdenest((x**(2 * a / 3))**(3 * x)) == (x**(a / 3))**(6 * x)
    assert powdenest(exp(3 * x * log(2))) == 2**(3 * x)
    assert powdenest(sqrt(p**2)) == p
    i, j = symbols('i,j', integer=True)
    eq = p**(2 * i) * q**(4 * i)
    assert powdenest(eq) == (p * q**2)**(2 * i)
    assert powdenest(
        (x**x)**(i + j))  # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
    assert powdenest(exp(3 * y * log(x))) == x**(3 * y)
    assert powdenest(exp(y * (log(a) + log(b)))) == (a * b)**y
    assert powdenest(exp(3 * (log(a) + log(b)))) == a**3 * b**3
    assert powdenest(((x**(2 * i))**(3 * y))**x) == ((x**(2 * i))**(3 * y))**x
    assert powdenest(((x**(2 * i))**(3 * y))**x,
                     force=True) == x**(6 * i * x * y)
    assert powdenest(
        ((x**(2 * a / 3))**(3 * y / i))**x) == ((x**(a / 3))**(y / i))**(6 * x)
    assert powdenest((x**(2 * i) * y**(4 * i))**z,
                     force=True) == (x * y**2)**(2 * i * z)
    assert powdenest((p**(2 * i) * q**(4 * i))**j) == (p * q**2)**(2 * i * j)
    assert powdenest(((p**(2 * a))**(3 * y))**x) == p**(6 * a * x * y)
    e = ((x**2 * y**4)**a)**(x * y)
    assert powdenest(e) == e
    e = (((x**2 * y**4)**a)**(x * y))**3
    assert powdenest(e) == ((x**2 * y**4)**a)**(3 * x * y)
    assert powdenest((((x**2 * y**4)**a)**(x * y)),
                     force=True) == (x * y**2)**(2 * a * x * y)
    assert powdenest((((x**2 * y**4)**a)**(x * y))**3,
                     force=True) == (x * y**2)**(6 * a * x * y)
    assert powdenest((x**2 * y**6)**i) != (x * y**3)**(2 * i)
    x, y = symbols('x,y', positive=True)
    assert powdenest((x**2 * y**6)**i) == (x * y**3)**(2 * i)
    assert powdenest(
        (x**(2 * i / 3) * y**(i / 2))**(2 * i)) == (x**(S(4) / 3) * y)**(i**2)
    assert powdenest(sqrt(x**(2 * i) * y**(6 * i))) == (x * y**3)**i
    # issue 2706
    assert powdenest(((gamma(x) * hyper(
        (), (), x)) * pi)**2) == (pi * gamma(x) * hyper((), (), x))**2

    assert powdenest(4**x) == 2**(2 * x)
    assert powdenest((4**x)**y) == 2**(2 * x * y)
    assert powdenest(4**x * y) == 2**(2 * x) * y
Beispiel #25
0
 def cancel_unidades(self, unidades):
     s_unidades = str(powdenest(unidades, force=True).cancel())
     #         if s_unidades=='1':
     #             s_unidades = ''
     return s_unidades
Beispiel #26
0
def test_H1():
    assert simplify(2 * 2**n) == simplify(2**(n + 1))
    assert powdenest(2 * 2**n) == simplify(2**(n + 1))
Beispiel #27
0
def test_inverse_mellin_transform():
    from sympy import (sin, simplify, expand_func, powsimp, Max, Min, expand,
                       powdenest, powsimp, exp_polar)
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1/x)
    assert IMT(s/(2*s**2 - 2), s, x, (2, oo)) \
           == (x**2/2 + S(1)/2)*Heaviside(1 - x)/(2*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) \
           == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) \
           == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s-1), s, x, (1, oo)) == (x + 1)*exp(-x)/x

    # test factorisation of polys
    assert simplify(expand_func(IMT(1/(s**2 + 1), s, exp(-x),
                                    (None, oo))).rewrite(sin)) \
           == sin(x)*Heaviside(1 - exp(-x))

    # test multiplicative substitution
    a, b = symbols('a b', positive=True)
    c, d = symbols('c d')
    assert IMT(b**(-s/a)*factorial(s/a)/s, s, x, (0, oo)) == exp(-b*x**a)
    assert IMT(factorial(a/b + s/b)/(a+ s), s, x, (-a, oo)) == x**a*exp(-x**b)

    from sympy import expand_mul
    def simp_pows(expr): return expand_mul(simplify(powsimp(expr, force=True)), deep=True).replace(exp_polar, exp) # XXX ?

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    assert IMT(-1/(nu + s), s, x, (-oo, None)) == x**nu*Heaviside(x - 1)
    assert IMT(1/(nu + s), s, x, (None, oo)) == x**nu*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
           == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1-beta-s)/gamma(1-s),
                         s, x, (-oo, None))) \
           == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho-s)/gamma(rho), s, x, (0, None))) \
           == (1/(x + 1))**rho
    # TODO should this simplify further?
    assert simp_pows(IMT(d**c*d**(s-1)*sin(pi*c) \
                         *gamma(s)*gamma(s+c)*gamma(1-s)*gamma(1-s-c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
           == d**c*(x/d)**c/(-d + x) - d**c/(-d + x)

    # TODO is calling simplify twice a bug?
    assert simplify(simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1-c)/2 - s) \
                                 *gamma(-c/2-s)/gamma(1-c-s),
                                 s, x, (0, -re(c)/2)))) == \
           (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s) \
                        /gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
           (b + sqrt(b**2 + x))**(a - 1)*(b**2 + b*sqrt(b**2 + x) + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s) \
                          / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
           (b + sqrt(b**2 + x))**c

    # Section 8.4.5
    assert IMT(24/s**5, s, x, (0, oo)) == log(x)**4*Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
           log(x)**3*Heaviside(x - 1)
    assert IMT(pi/(s*sin(pi*s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi/(s*sin(pi*s/2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi/(s*sin(2*pi*s)), s, x, (-S(1)/2, 0)) == log(sqrt(x) + 1)
    assert IMT(pi/(s*sin(pi*s)), s, x, (0, 1)) == log(1 + 1/x)

    # TODO
    def mysimp(expr):
        from sympy import expand, logcombine, powsimp
        return expand(powsimp(logcombine(expr, force=True), force=True, deep=True),
                      force=True).replace(exp_polar, exp)
    assert mysimp(mysimp(IMT(pi/(s*tan(pi*s)), s, x, (-1, 0)))) == \
           log(1-x)*Heaviside(1-x) + log(x-1)*Heaviside(x-1)
    assert mysimp(IMT(pi/(s*tan(pi*s)), s, x, (0, 1))) == \
           log(1/x - 1)*Heaviside(1-x) + log(1 - 1/x)*Heaviside(x-1)

    # 8.4.14
    assert IMT(-gamma(s + S(1)/2)/(sqrt(pi)*s), s, x, (-S(1)/2, 0)) == \
           erf(sqrt(x))

    # 8.4.19
    # TODO these come out ugly
    def mysimp(expr):
        return expand(unpolarify(simplify(expand(expand_func(expr.rewrite(besselj))))))
    assert mysimp(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, S(3)/4))) \
           == besselj(a, 2*sqrt(x)*polar_lift(-1))*exp(-I*pi*a)
    assert mysimp(IMT(2**a*gamma(S(1)/2 - 2*s)*gamma(s + (a + 1)/2) \
                      / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-(re(a) + 1)/2, S(1)/4))) == \
           exp(-I*pi*a)*sin(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1))
    assert mysimp(IMT(2**a*gamma(a/2 + s)*gamma(S(1)/2 - 2*s) \
                      / (gamma(S(1)/2 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-re(a)/2, S(1)/4))) == \
           exp(-I*pi*a)*cos(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1))
    # TODO this comes out as an amazing mess, but surprisingly enough mysimp is
    #      effective ...
    assert powsimp(powdenest(mysimp(IMT(gamma(a + s)*gamma(S(1)/2 - s) \
                      / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                      s, x, (-re(a), S(1)/2))), polar=True)) == \
           exp(-2*I*pi*a)*besselj(a, sqrt(x)*polar_lift(-1))**2
    # NOTE the next is indeed an even function of sqrt(x), so the result is
    #      correct
    assert mysimp(IMT(gamma(s)*gamma(S(1)/2 - s) \
                      / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                      s, x, (0, S(1)/2))) == \
           besselj(-a, polar_lift(-1)*sqrt(x))*besselj(a, polar_lift(-1)*sqrt(x))
    assert mysimp(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) \
                      / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) \
                         *gamma(a/2 + b/2 - s + 1)),
                      s, x, (-(re(a) + re(b))/2, S(1)/2))) == \
           exp(-I*pi*a)*exp(-I*pi*b)*besselj(a, sqrt(x)*polar_lift(-1)) \
                                    *besselj(b, sqrt(x)*polar_lift(-1))
Beispiel #28
0
def _simplify(expr, doit):
    from sympy import powdenest, powsimp
    if doit:
        return simplify(powdenest(expr, polar=True))
    return expr
Beispiel #29
0
def test_issue_5805():
    arg = ((gamma(x) * hyper((), (), x)) * pi)**2
    assert powdenest(arg) == (pi * gamma(x) * hyper((), (), x))**2
    assert arg.is_positive is None
Beispiel #30
0
def _simplify(expr, doit):
    from sympy import powdenest, powsimp
    if doit:
        return simplify(powdenest(expr, polar=True))
    return expr
def Library_SympyExpressionSimplify(
    SympyExpression=None,
    ShowAttemptErrors=None,
    CheckArguments=True,
    PrintExtra=False,
):

    Result = None

    if ShowAttemptErrors is None:
        ShowAttemptErrors = False

    if (CheckArguments):
        ArgumentErrorMessage = ""

        if (len(ArgumentErrorMessage) > 0):
            if (PrintExtra):
                print("ArgumentErrorMessage:\n", ArgumentErrorMessage)
            raise Exception(ArgumentErrorMessage)

    try:
        Result = SympyExpression

        #sin = copy.deepcopy(sympy.sin)
        #cos = copy.deepcopy(sympy.cos)
        #exp = copy.deepcopy(sympy.exp)

        #make a copy of input
        #Result = copy.deepcopy(SympyExpression)
        #if(PrintExtra): Library_SympyExpressionPrintInfo.Main(Result)

        #First do the basic built-in simplify
        Result = Result.simplify().cancel().expand()  #.logcombine()

        #Second -> expand everything in terms of exponents
        Result = Result.expand()

        #Try to simplify and gather exponents:
        Result = sympy.powsimp(Result, deep=True, force=True, combine='all')
        Result = sympy.powdenest(Result, force=True)

        #Force Trig Equalities, by plugging in the exp:
        Result = Result.rewrite(sympy.sin,
                                sympy.exp).rewrite(sympy.cos, sympy.exp)
        if (PrintExtra): Library_SympyExpressionPrintInfo(Result)

        #Simplify the Exponentials and trig functions:
        Result = Result.trigsimp()
        if (PrintExtra): Library_SympyExpressionPrintInfo(Result)

        #Force consts to be evaluated:
        Result = Result.evalf()
        if (PrintExtra): Library_SympyExpressionPrintInfo(Result)

        #Do the built in symplifies one more time:
        Result = Result.simplify().cancel().expand()  #.logcombine()
        if (PrintExtra): Library_SympyExpressionPrintInfo(Result)

        #Make another copy
        #FinalResult = copy.deepcopy(Result)
        #if(PrintExtra):Library_SympyExpressionPrintInfo.Main(FinalResult)
    except Exception as ExceptionObject:
        if ShowAttemptErrors:
            Library_PrintExceptionObject(ExceptionObject)
        Result = SympyExpression

    return Result
Beispiel #32
0
def test_powdenest():
    from sympy import powdenest
    from sympy.abc import x, y, z, a, b
    p = symbols('p', positive=True)
    i, j = symbols('i,j', integer=1)

    assert powdenest(x) == x
    assert powdenest(x + 2 * (x**(2 * a / 3))**
                     (3 * x)) == x + 2 * (x**(a / 3))**(6 * x)
    assert powdenest((exp(2 * a / 3))**(3 * x)) == (exp(a / 3))**(6 * x)
    assert powdenest((x**(2 * a / 3))**(3 * x)) == (x**(a / 3))**(6 * x)
    assert powdenest(exp(3 * x * log(2))) == 2**(3 * x)
    assert powdenest(sqrt(p**2)) == p
    i, j = symbols('i,j', integer=1)
    assert powdenest(
        (x**x)**(i + j))  # -X-> (x**x)**i*(x**x)**j == x**(x*(i + j))
    assert powdenest(exp(3 * y * log(x))) == x**(3 * y)
    assert powdenest(exp(y * (log(a) + log(b)))) == (a * b)**y
    assert powdenest(exp(3 * (log(a) + log(b)))) == a**3 * b**3
    assert powdenest(((x**(2 * i))**(3 * y))**x) == ((x**(2 * i))**(3 * y))**x
    assert powdenest(((x**(2 * i))**(3 * y))**x,
                     force=True) == x**(6 * i * x * y)
    assert powdenest(
        ((x**(2 * a / 3))**(3 * y / i))**x) == ((x**(a / 3))**(y / i))**(6 * x)
    assert powdenest((x**(2 * i) * y**(4 * i))**z,
                     force=True) == (x * y**2)**(2 * i * z)
    e = ((x**2 * y**4)**a)**(x * y)
    assert powdenest(e) == e
    e = (((x**2 * y**4)**a)**(x * y))**3
    assert powdenest(e) == ((x**2 * y**4)**a)**(3 * x * y)
Beispiel #33
0
def _simplify(expr, doit):
    from sympy import powdenest, piecewise_fold
    if doit:
        return simplify(powdenest(piecewise_fold(expr), polar=True))
    return expr
Beispiel #34
0
def test_issue_2706():
    arg = ((gamma(x)*hyper((),(),x))*pi)**2
    assert powdenest(arg) == (pi*gamma(x)*hyper((), (), x))**2
    assert arg.is_positive is None
Beispiel #35
0
def test_H1():
    assert simplify(2*2**n) == simplify(2**(n + 1))
    assert powdenest(2*2**n) == simplify(2**(n + 1))
Beispiel #36
0
def _simplify(expr, doit):
    from sympy import powdenest, piecewise_fold
    if doit:
        return simplify(powdenest(piecewise_fold(expr), polar=True))
    return expr