Example #1
0
def test_exp_rewrite():
    assert exp(x).rewrite(sin) == sinh(x) + cosh(x)
    assert exp(x*I).rewrite(cos) == cos(x) + I*sin(x)
    assert exp(1).rewrite(cos) == sinh(1) + cosh(1)
    assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
    assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
    assert exp(x).rewrite(tanh) == (1 + tanh(x/2))/(1 - tanh(x/2))
Example #2
0
def test_issue_1572_1364_1368():
    assert solve((sqrt(x**2 - 1) - 2)) in ([sqrt(5), -sqrt(5)],
                                           [-sqrt(5), sqrt(5)])
    assert set(solve((2**exp(y**2/x) + 2)/(x**2 + 15), y)) == set([
        -sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi)),
        sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi))])

    C1, C2 = symbols('C1 C2')
    f = Function('f')
    assert solve(C1 + C2/x**2 - exp(-f(x)), f(x)) == [log(x**2/(C1*x**2 + C2))]
    a = Symbol('a')
    E = S.Exp1
    assert solve(1 - log(a + 4*x**2), x) in (
        [-sqrt(-a + E)/2, sqrt(-a + E)/2],
        [sqrt(-a + E)/2, -sqrt(-a + E)/2]
    )
    assert solve(log(a**(-3) - x**2)/a, x) in (
        [-sqrt(-1 + a**(-3)), sqrt(-1 + a**(-3))],
        [sqrt(-1 + a**(-3)), -sqrt(-1 + a**(-3))],)
    assert solve(1 - log(a + 4*x**2), x) in (
        [-sqrt(-a + E)/2, sqrt(-a + E)/2],
        [sqrt(-a + E)/2, -sqrt(-a + E)/2],)
    assert set(solve((
        a**2 + 1) * (sin(a*x) + cos(a*x)), x)) == set([-pi/(4*a), 3*pi/(4*a)])
    assert solve(3 - (sinh(a*x) + cosh(a*x)), x) == [2*atanh(S.Half)/a]
    assert set(solve(3 - (sinh(a*x) + cosh(a*x)**2), x)) == \
        set([
            2*atanh(-1 + sqrt(2))/a,
            2*atanh(S(1)/2 + sqrt(5)/2)/a,
            2*atanh(-sqrt(2) - 1)/a,
            2*atanh(-sqrt(5)/2 + S(1)/2)/a
             ])
    assert solve(atan(x) - 1) == [tan(1)]
Example #3
0
def test_issue_6920():
    e = [cos(x) + I*sin(x), cos(x) - I*sin(x),
        cosh(x) - sinh(x), cosh(x) + sinh(x)]
    ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)]
    # wrap in f to show that the change happens wherever ei occurs
    f = Function('f')
    assert [simplify(f(ei)).args[0] for ei in e] == ok
Example #4
0
def test_ode_solutions():
    # only a few examples here, the rest will be tested in the actual dsolve tests
    assert constant_renumber(constantsimp(C1*exp(2*x) + exp(x)*(C2 + C3), [C1, C2, C3])) == \
        constant_renumber((C1*exp(x) + C2*exp(2*x)))
    assert constant_renumber(
        constantsimp(Eq(f(x), I*C1*sinh(x/3) + C2*cosh(x/3)), [C1, C2])
        ) == constant_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)))
    assert constant_renumber(constantsimp(Eq(f(x), acos((-C1)/cos(x))), [C1])) == \
        Eq(f(x), acos(C1/cos(x)))
    assert constant_renumber(
        constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), [C1])
        ) == Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0)
    assert constant_renumber(constantsimp(Eq(log(x*sqrt(2)*sqrt(1/x)*sqrt(f(x))
        /C1) + x**2/(2*f(x)**2), 0), [C1])) == \
        Eq(log(C1*sqrt(x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0)
    assert constant_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) -
        cos(f(x)/x)*exp(-f(x)/x)/2, 0), [C1])) == \
        Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)*
           exp(-f(x)/x)/2, 0)
    assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2),
        (u2, _a, x/f(x))) + log(f(x)/C1), 0), [C1])) == \
        Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) +
        log(C1*f(x)), 0)
    assert [constantsimp(i, [C1]) for i in [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x + x**2))]] == \
        [Eq(f(x), sqrt(x*(C1 + x))), Eq(f(x), -sqrt(x*(C1 + x)))]
Example #5
0
def Lorentz_Tranformation_in_Geog_Algebra():
    Print_Function()
    (alpha,beta,gamma) = symbols('alpha beta gamma')
    (x,t,xp,tp) = symbols("x t x' t'",real=True)
    (st2d,g0,g1) = Ga.build('gamma*t|x',g=[1,-1])

    from sympy import sinh,cosh

    R = cosh(alpha/2)+sinh(alpha/2)*(g0^g1)
    X = t*g0+x*g1
    Xp = tp*g0+xp*g1
    print 'R =',R

    print r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}"

    Xpp = R*Xp*R.rev()
    Xpp = Xpp.collect()
    Xpp = Xpp.trigsimp()
    print r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =",Xpp
    Xpp = Xpp.subs({sinh(alpha):gamma*beta,cosh(alpha):gamma})

    print r'%\f{\sinh}{\alpha} = \gamma\beta'
    print r'%\f{\cosh}{\alpha} = \gamma'

    print r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =",Xpp.collect()
    return
Example #6
0
def test_hyper_as_trig():
    from sympy.simplify.fu import _osborne as o, _osbornei as i, TR12

    eq = sinh(x)**2 + cosh(x)**2
    t, f = hyper_as_trig(eq)
    assert f(fu(t)) == cosh(2*x)
    e, f = hyper_as_trig(tanh(x + y))
    assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1)

    d = Dummy()
    assert o(sinh(x), d) == I*sin(x*d)
    assert o(tanh(x), d) == I*tan(x*d)
    assert o(coth(x), d) == cot(x*d)/I
    assert o(cosh(x), d) == cos(x*d)
    for func in (sinh, cosh, tanh, coth):
        h = func(pi)
        assert i(o(h, d), d) == h
    # /!\ the _osborne functions are not meant to work
    # in the o(i(trig, d), d) direction so we just check
    # that they work as they are supposed to work
    assert i(cos(x*y), y) == cosh(x)
    assert i(sin(x*y), y) == sinh(x)/I
    assert i(tan(x*y), y) == tanh(x)/I
    assert i(cot(x*y), y) == coth(x)*I
    assert i(sec(x*y), y) == 1/cosh(x)
    assert i(csc(x*y), y) == I/sinh(x)
Example #7
0
def test_cosine_transform():
    from sympy import sinh, cosh, Si, Ci

    t = symbols("t")
    w = symbols("w")
    a = symbols("a")
    f = Function("f")

    # Test unevaluated form
    assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w)
    assert inverse_cosine_transform(f(w), w, t) == InverseCosineTransform(f(w), w, t)

    assert cosine_transform(1/sqrt(t), t, w) == 1/sqrt(w)
    assert inverse_cosine_transform(1/sqrt(w), w, t) == 1/sqrt(t)

    assert cosine_transform(1/(a**2+t**2), t, w) == sqrt(2)*sqrt(pi)*(-sinh(a*w) + cosh(a*w))/(2*a)

    assert cosine_transform(t**(-a), t, w) == 2**(-a + S(1)/2)*w**(a - 1)*gamma((-a + 1)/2)/gamma(a/2)
    assert inverse_cosine_transform(2**(-a + S(1)/2)*w**(a - 1)*gamma(-a/2 + S(1)/2)/gamma(a/2), w, t) == t**(-a)

    assert cosine_transform(exp(-a*t), t, w) == sqrt(2)*a/(sqrt(pi)*(a**2 + w**2))
    assert inverse_cosine_transform(sqrt(2)*a/(sqrt(pi)*(a**2 + w**2)), w, t) == -sinh(a*t) + cosh(a*t)

    assert cosine_transform(exp(-a*sqrt(t))*cos(a*sqrt(t)), t, w) == a*(-sinh(a**2/(2*w)) + cosh(a**2/(2*w)))/(2*w**(S(3)/2))

    assert cosine_transform(1/(a+t), t, w) == -sqrt(2)*((2*Si(a*w) - pi)*sin(a*w) + 2*cos(a*w)*Ci(a*w))/(2*sqrt(pi))
    assert inverse_cosine_transform(sqrt(2)*meijerg(((S(1)/2, 0), ()), ((S(1)/2, 0, 0), (S(1)/2,)), a**2*w**2/4)/(2*pi), w, t) == 1/(a + t)

    assert cosine_transform(1/sqrt(a**2+t**2), t, w) == sqrt(2)*meijerg(((S(1)/2,), ()), ((0, 0), (S(1)/2,)), a**2*w**2/4)/(2*sqrt(pi))
    assert inverse_cosine_transform(sqrt(2)*meijerg(((S(1)/2,), ()), ((0, 0), (S(1)/2,)), a**2*w**2/4)/(2*sqrt(pi)), w, t) == 1/(t*sqrt(a**2/t**2 + 1))
Example #8
0
def Lorentz_Tranformation_in_Geometric_Algebra():
    Print_Function()
    (alpha, beta, gamma) = symbols('alpha beta gamma')
    (x, t, xp, tp) = symbols("x t x' t'")
    (g0, g1) = MV.setup('gamma*t|x', metric='[1,-1]')

    from sympy import sinh, cosh

    R = cosh(alpha/2) + sinh(alpha/2)*(g0 ^ g1)
    X = t*g0 + x*g1
    Xp = tp*g0 + xp*g1
    print('R =', R)

    print(r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}")

    Xpp = R*Xp*R.rev()
    Xpp = Xpp.collect([xp, tp])
    Xpp = Xpp.subs({2*sinh(alpha/2)*cosh(alpha/2): sinh(alpha), sinh(alpha/2)**2 + cosh(alpha/2)**2: cosh(alpha)})
    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp)
    Xpp = Xpp.subs({sinh(alpha): gamma*beta, cosh(alpha): gamma})

    print(r'%\f{\sinh}{\alpha} = \gamma\beta')
    print(r'%\f{\cosh}{\alpha} = \gamma')

    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp.collect(gamma))
    return
Example #9
0
 def _w(self, section):
     q0 = self.q0
     a = self.a
     aa = self.alpha
     bb = self.beta
     A11 = self.laminate.abd[0,0]
     B11 = self.laminate.abd[0,3]
     D11 = self.laminate.abd[3,3]
     n0 = self.n0
     x = self._x
     
     if ((section is 'left') or (section is 'right')):
         res = ((1/(4.*n0**2))*q0*
                (-4*(1/sympy.cosh((1/2.)*a*bb))*
                 sympy.sinh((1/2.)*(a-2*(-x))*bb)*
                 sympy.sinh((1/2.)*a*aa*bb)*D11+a*(a-2*(-x))*aa*n0))
         if section is 'right':
             res = res.subs(self._x, -self._x)
     elif section is 'mid':
         res = ((1/(8.*n0**2))*q0*
                (8*(-1+
                    sympy.cosh(x*bb)*sympy.cosh((1/2.)*a*(-1+aa)*bb)*
                    (1/sympy.cosh((1/2.)*a*bb)))*D11-
                    (4*x**2+a**2*(-2+aa)*aa)*n0))
     return res
Example #10
0
def test_coth_rewrite():
    x = Symbol('x')
    assert coth(x).rewrite(exp) == (exp(x) + exp(-x))/(exp(x) - exp(-x)) \
        == coth(x).rewrite('tractable')
    assert coth(x).rewrite(sinh) == -I*sinh(I*pi/2 - x)/sinh(x)
    assert coth(x).rewrite(cosh) == -I*cosh(x)/cosh(I*pi/2 - x)
    assert coth(x).rewrite(tanh) == 1/tanh(x)
Example #11
0
def test_simplifications():
    x = Symbol('x')
    assert sinh(asinh(x)) == x
    assert sinh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1)
    assert sinh(atanh(x)) == x/sqrt(1 - x**2)
    assert sinh(acoth(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))

    assert cosh(asinh(x)) == sqrt(1 + x**2)
    assert cosh(acosh(x)) == x
    assert cosh(atanh(x)) == 1/sqrt(1 - x**2)
    assert cosh(acoth(x)) == x/(sqrt(x - 1) * sqrt(x + 1))

    assert tanh(asinh(x)) == x/sqrt(1 + x**2)
    assert tanh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1) / x
    assert tanh(atanh(x)) == x
    assert tanh(acoth(x)) == 1/x

    assert coth(asinh(x)) == sqrt(1 + x**2)/x
    assert coth(acosh(x)) == x/(sqrt(x - 1) * sqrt(x + 1))
    assert coth(atanh(x)) == 1/x
    assert coth(acoth(x)) == x

    assert csch(asinh(x)) == 1/x
    assert csch(acosh(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))
    assert csch(atanh(x)) == sqrt(1 - x**2)/x
    assert csch(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)

    assert sech(asinh(x)) == 1/sqrt(1 + x**2)
    assert sech(acosh(x)) == 1/x
    assert sech(atanh(x)) == sqrt(1 - x**2)
    assert sech(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)/x
Example #12
0
def test_tan_rewrite():
    neg_exp, pos_exp = exp(-x*I), exp(x*I)
    assert tan(x).rewrite(exp) == I*(neg_exp - pos_exp)/(neg_exp + pos_exp)
    assert tan(x).rewrite(sin) == 2*sin(x)**2/sin(2*x)
    assert tan(x).rewrite(cos) == -cos(x + S.Pi/2)/cos(x)
    assert tan(x).rewrite(cot) == 1/cot(x)
    assert tan(sinh(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, sinh(3)).n()
    assert tan(cosh(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cosh(3)).n()
    assert tan(tanh(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, tanh(3)).n()
    assert tan(coth(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, coth(3)).n()
    assert tan(sin(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, sin(3)).n()
    assert tan(cos(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cos(3)).n()
    assert tan(tan(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, tan(3)).n()
    assert tan(cot(x)).rewrite(
        exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cot(3)).n()
    assert tan(log(x)).rewrite(Pow) == I*(x**-I - x**I)/(x**-I + x**I)
    assert 0 == (cos(pi/15)*tan(pi/15) - sin(pi/15)).rewrite(pow)
    assert tan(pi/19).rewrite(pow) == tan(pi/19)
    assert tan(8*pi/19).rewrite(sqrt) == tan(8*pi/19)
Example #13
0
def test_issue_8368():
    assert integrate(exp(-s*x)*cosh(x), (x, 0, oo)) == \
        Piecewise(
            (   pi*Piecewise(
                    (   -s/(pi*(-s**2 + 1)),
                        Abs(s**2) < 1),
                    (   1/(pi*s*(1 - 1/s**2)),
                        Abs(s**(-2)) < 1),
                    (   meijerg(
                            ((S(1)/2,), (0, 0)),
                            ((0, S(1)/2), (0,)),
                            polar_lift(s)**2),
                        True)
                ),
                And(
                    Abs(periodic_argument(polar_lift(s)**2, oo)) < pi,
                    cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0,
                    Ne(s**2, 1))
            ),
            (
                Integral(exp(-s*x)*cosh(x), (x, 0, oo)),
                True))
    assert integrate(exp(-s*x)*sinh(x), (x, 0, oo)) == \
        Piecewise(
            (   -1/(s + 1)/2 - 1/(-s + 1)/2,
                And(
                    Ne(1/s, 1),
                    Abs(periodic_argument(s, oo)) < pi/2,
                    Abs(periodic_argument(s, oo)) <= pi/2,
                    cos(Abs(periodic_argument(s, oo)))*Abs(s) - 1 > 0)),
            (   Integral(exp(-s*x)*sinh(x), (x, 0, oo)),
                True))
Example #14
0
def test_ode_solutions():
    # only a few examples here, the rest will be tested in the actual dsolve tests
    assert ode_renumber(constantsimp(C1*exp(2*x)+exp(x)*(C2+C3), x, 3), 'C', 1, 3) == \
        ode_renumber(C1*exp(x)+C2*exp(2*x), 'C', 1, 2)
    assert ode_renumber(constantsimp(Eq(f(x),I*C1*sinh(x/3) + C2*cosh(x/3)), x, 2),
        'C', 1, 2) == ode_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2)
    assert ode_renumber(constantsimp(Eq(f(x),acos((-C1)/cos(x))), x, 1), 'C', 1, 1) == \
        Eq(f(x),acos(C1/cos(x)))
    assert ode_renumber(constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), x, 1),
        'C', 1, 1) ==  Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0)
    assert ode_renumber(constantsimp(Eq(log(x*2**Rational(1,2)*(1/x)**Rational(1,2)*f(x)\
        **Rational(1,2)/C1) + x**2/(2*f(x)**2), 0), x, 1), 'C', 1, 1) == \
        Eq(log(C1*x*(1/x)**Rational(1,2)*f(x)**Rational(1,2)) + x**2/(2*f(x)**2), 0)
    assert ode_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) - \
        cos(f(x)/x)*exp(-f(x)/x)/2, 0), x, 1), 'C', 1, 1) == \
        Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)*exp(-f(x)/x)/2, 0)
    u2 = Symbol('u2')
    _a = Symbol('_a')
    assert ode_renumber(constantsimp(Eq(-Integral(-1/((1 - u2**2)**Rational(1,2)*u2), \
        (u2, _a, x/f(x))) + log(f(x)/C1), 0), x, 1), 'C', 1, 1) == \
        Eq(-Integral(-1/(u2*(1 - u2**2)**Rational(1,2)), (u2, _a, x/f(x))) + \
        log(C1*f(x)), 0)
    assert map(lambda i: ode_renumber(constantsimp(i, x, 1), 'C', 1, 1),
        [Eq(f(x), (-C1*x + x**2)**Rational(1,2)), Eq(f(x), -(-C1*x +
        x**2)**Rational(1,2))]) == [Eq(f(x), (C1*x + x**2)**Rational(1,2)),
        Eq(f(x), -(C1*x + x**2)**Rational(1,2))]
Example #15
0
def test_manualintegrate_special():
    f, F = 4*exp(-x**2/3), 2*sqrt(3)*sqrt(pi)*erf(sqrt(3)*x/3)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = 3*exp(4*x**2), 3*sqrt(pi)*erfi(2*x)/4
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = x**(S(1)/3)*exp(-x/8), -16*uppergamma(S(4)/3, x/8)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = exp(2*x)/x, Ei(2*x)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = exp(1 + 2*x - x**2), sqrt(pi)*exp(2)*erf(x - 1)/2
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f = sin(x**2 + 4*x + 1)
    F = (sqrt(2)*sqrt(pi)*(-sin(3)*fresnelc(sqrt(2)*(2*x + 4)/(2*sqrt(pi))) +
        cos(3)*fresnels(sqrt(2)*(2*x + 4)/(2*sqrt(pi))))/2)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = cos(4*x**2), sqrt(2)*sqrt(pi)*fresnelc(2*sqrt(2)*x/sqrt(pi))/4
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = sin(3*x + 2)/x, sin(2)*Ci(3*x) + cos(2)*Si(3*x)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = sinh(3*x - 2)/x, -sinh(2)*Chi(3*x) + cosh(2)*Shi(3*x)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = 5*cos(2*x - 3)/x, 5*cos(3)*Ci(2*x) + 5*sin(3)*Si(2*x)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = cosh(x/2)/x, Chi(x/2)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = cos(x**2)/x, Ci(x**2)/2
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = 1/log(2*x + 1), li(2*x + 1)/2
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = polylog(2, 5*x)/x, polylog(3, 5*x)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = 5/sqrt(3 - 2*sin(x)**2), 5*sqrt(3)*elliptic_f(x, S(2)/3)/3
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
    f, F = sqrt(4 + 9*sin(x)**2), 2*elliptic_e(x, -S(9)/4)
    assert manualintegrate(f, x) == F and F.diff(x).equals(f)
Example #16
0
def test_ode_solutions():
    # only a few examples here, the rest will be tested in the actual dsolve tests
    assert constant_renumber(constantsimp(C1*exp(2*x)+exp(x)*(C2+C3), x, 3), 'C', 1, 3) == \
        constant_renumber(C1*exp(x)+C2*exp(2*x), 'C', 1, 2)
    assert constant_renumber(constantsimp(Eq(f(x),I*C1*sinh(x/3) + C2*cosh(x/3)), x, 2),
        'C', 1, 2) == constant_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2)
    assert constant_renumber(constantsimp(Eq(f(x),acos((-C1)/cos(x))), x, 1), 'C', 1, 1) == \
        Eq(f(x),acos(C1/cos(x)))
    assert constant_renumber(constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), x, 1),
        'C', 1, 1) ==  Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0)
    assert constant_renumber(constantsimp(Eq(log(x*sqrt(2)*sqrt(1/x)*sqrt(f(x))\
        /C1) + x**2/(2*f(x)**2), 0), x, 1), 'C', 1, 1) == \
        Eq(log(C1*x*sqrt(1/x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0)
    assert constant_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) - \
        cos(f(x)/x)*exp(-f(x)/x)/2, 0), x, 1), 'C', 1, 1) == \
        Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)*exp(-f(x)/x)/2, 0)
    u2 = Symbol('u2')
    _a = Symbol('_a')
    assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2), \
        (u2, _a, x/f(x))) + log(f(x)/C1), 0), x, 1), 'C', 1, 1) == \
        Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) + \
        log(C1*f(x)), 0)
    assert [constant_renumber(constantsimp(i, x, 1), 'C', 1, 1) for i in
        [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x +
        x**2))]] == [Eq(f(x), sqrt(C1*x + x**2)),
        Eq(f(x), -sqrt(C1*x + x**2))]
Example #17
0
def test_heurisch_hyperbolic():
    assert heurisch(sinh(x), x) == cosh(x)
    assert heurisch(cosh(x), x) == sinh(x)

    assert heurisch(x*sinh(x), x) == x*cosh(x) - sinh(x)
    assert heurisch(x*cosh(x), x) == x*sinh(x) - cosh(x)

    assert heurisch(x*asinh(x/2), x) == x**2*asinh(x/2)/2 + asinh(x/2) - x*sqrt(4+x**2)/4
Example #18
0
def test_sinh_rewrite():
    x = Symbol('x')
    assert sinh(x).rewrite(exp) == (exp(x)-exp(-x))/2
    assert sinh(x).rewrite(cosh) == -I*cosh(x+I*pi/2)
    tanh_half = tanh(S.Half*x)
    assert sinh(x).rewrite(tanh) == 2*tanh_half/(1-tanh_half**2)
    coth_half = coth(S.Half*x)
    assert sinh(x).rewrite(coth) == 2*coth_half/(coth_half**2-1)
Example #19
0
def test_sec():
    x = symbols('x', real=True)
    z = symbols('z')

    assert sec.nargs == FiniteSet(1)

    assert sec(0) == 1
    assert sec(pi) == -1
    assert sec(pi/2) == zoo
    assert sec(-pi/2) == zoo
    assert sec(pi/6) == 2*sqrt(3)/3
    assert sec(pi/3) == 2
    assert sec(5*pi/2) == zoo
    assert sec(9*pi/7) == -sec(2*pi/7)
    assert sec(I) == 1/cosh(1)
    assert sec(x*I) == 1/cosh(x)
    assert sec(-x) == sec(x)

    assert sec(x).rewrite(exp) == 1/(exp(I*x)/2 + exp(-I*x)/2)
    assert sec(x).rewrite(sin) == sec(x)
    assert sec(x).rewrite(cos) == 1/cos(x)
    assert sec(x).rewrite(tan) == (tan(x/2)**2 + 1)/(-tan(x/2)**2 + 1)
    assert sec(x).rewrite(pow) == sec(x)
    assert sec(x).rewrite(sqrt) == sec(x)

    assert sec(z).conjugate() == sec(conjugate(z))

    assert (sec(z).as_real_imag() ==
    (cos(re(z))*cosh(im(z))/(sin(re(z))**2*sinh(im(z))**2 +
                             cos(re(z))**2*cosh(im(z))**2),
     sin(re(z))*sinh(im(z))/(sin(re(z))**2*sinh(im(z))**2 +
                             cos(re(z))**2*cosh(im(z))**2)))

    assert sec(x).expand(trig=True) == 1/cos(x)
    assert sec(2*x).expand(trig=True) == 1/(2*cos(x)**2 - 1)

    assert sec(x).is_real == True
    assert sec(z).is_real == None

    assert sec(x).as_leading_term() == sec(x)

    assert sec(0).is_bounded == True
    assert sec(x).is_bounded == None
    assert sec(pi/2).is_bounded == False

    assert series(sec(x), x, x0=0, n=6) == 1 + x**2/2 + 5*x**4/24 + O(x**6)

    # https://github.com/sympy/sympy/issues/7166
    assert series(sqrt(sec(x))) == 1 + x**2/4 + 7*x**4/96 + O(x**6)

    assert sec(x).diff(x) == tan(x)*sec(x)

    # Taylor Term checks
    assert sec(z).taylor_term(4, z) == 5*z**4/24
    assert sec(z).taylor_term(6, z) == 61*z**6/720
    assert sec(z).taylor_term(5, z) == 0
Example #20
0
def test_csc():
    x = symbols('x', real=True)
    z = symbols('z')

    # https://github.com/sympy/sympy/issues/6707
    cosecant = csc('x')
    alternate = 1/sin('x')
    assert cosecant.equals(alternate) == True
    assert alternate.equals(cosecant) == True

    assert csc.nargs == FiniteSet(1)

    assert csc(0) == zoo
    assert csc(pi) == zoo

    assert csc(pi/2) == 1
    assert csc(-pi/2) == -1
    assert csc(pi/6) == 2
    assert csc(pi/3) == 2*sqrt(3)/3
    assert csc(5*pi/2) == 1
    assert csc(9*pi/7) == -csc(2*pi/7)
    assert csc(I) == -I/sinh(1)
    assert csc(x*I) == -I/sinh(x)
    assert csc(-x) == -csc(x)

    assert csc(x).rewrite(exp) == 2*I/(exp(I*x) - exp(-I*x))
    assert csc(x).rewrite(sin) == 1/sin(x)
    assert csc(x).rewrite(cos) == csc(x)
    assert csc(x).rewrite(tan) == (tan(x/2)**2 + 1)/(2*tan(x/2))

    assert csc(z).conjugate() == csc(conjugate(z))

    assert (csc(z).as_real_imag() ==
            (sin(re(z))*cosh(im(z))/(sin(re(z))**2*cosh(im(z))**2 +
                                     cos(re(z))**2*sinh(im(z))**2),
             -cos(re(z))*sinh(im(z))/(sin(re(z))**2*cosh(im(z))**2 +
                          cos(re(z))**2*sinh(im(z))**2)))

    assert csc(x).expand(trig=True) == 1/sin(x)
    assert csc(2*x).expand(trig=True) == 1/(2*sin(x)*cos(x))

    assert csc(x).is_real == True
    assert csc(z).is_real == None

    assert csc(x).as_leading_term() == csc(x)

    assert csc(0).is_bounded == False
    assert csc(x).is_bounded == None
    assert csc(pi/2).is_bounded == True

    assert series(csc(x), x, x0=pi/2, n=6) == \
        1 + (x - pi/2)**2/2 + 5*(x - pi/2)**4/24 + O((x - pi/2)**6, (x, pi/2))
    assert series(csc(x), x, x0=0, n=6) == \
            1/x + x/6 + 7*x**3/360 + 31*x**5/15120 + O(x**6)

    assert csc(x).diff(x) == -cot(x)*csc(x)
Example #21
0
def test_derivs():
    x = Symbol('x')
    assert coth(x).diff(x) == -sinh(x)**(-2)
    assert sinh(x).diff(x) == cosh(x)
    assert cosh(x).diff(x) == sinh(x)
    assert tanh(x).diff(x) == -tanh(x)**2 + 1
    assert acoth(x).diff(x) == 1/(-x**2 + 1)
    assert asinh(x).diff(x) == 1/sqrt(x**2 + 1)
    assert acosh(x).diff(x) == 1/sqrt(x**2 - 1)
    assert atanh(x).diff(x) == 1/(-x**2 + 1)
Example #22
0
def test_issue_8368_7173():
    LT = laplace_transform
    # hyperbolic
    assert LT(sinh(x), x, s) == (1 / (s ** 2 - 1), 1, True)
    assert LT(cosh(x), x, s) == (s / (s ** 2 - 1), 1, True)
    assert LT(sinh(x + 3), x, s) == ((-s + (s + 1) * exp(6) + 1) * exp(-3) / (s - 1) / (s + 1) / 2, 1, True)
    assert LT(sinh(x) * cosh(x), x, s) == (1 / (s ** 2 - 4), 2, Ne(s / 2, 1))

    # trig (make sure they are not being rewritten in terms of exp)
    assert LT(cos(x + 3), x, s) == ((s * cos(3) - sin(3)) / (s ** 2 + 1), 0, True)
Example #23
0
def test_sec():
    x = symbols('x', real=True)
    z = symbols('z')

    assert sec.nargs == 1

    assert sec(0) == 1
    assert sec(pi) == -1
    assert sec(pi/2) == oo
    assert sec(-pi/2) == oo
    assert sec(pi/6) == 2*sqrt(3)/3
    assert sec(pi/3) == 2
    assert sec(5*pi/2) == oo
    assert sec(9*pi/7) == -sec(2*pi/7)
    assert sec(I) == 1/cosh(1)
    assert sec(x*I) == 1/cosh(x)
    assert sec(-x) == sec(x)

    assert sec(x).rewrite(exp) == 1/(exp(I*x)/2 + exp(-I*x)/2)
    assert sec(x).rewrite(sin) == sec(x)
    assert sec(x).rewrite(cos) == 1/cos(x)
    assert sec(x).rewrite(tan) == (tan(x/2)**2 + 1)/(-tan(x/2)**2 + 1)
    assert sec(x).rewrite(pow) == sec(x)
    assert sec(x).rewrite(sqrt) == sec(x)

    assert sec(z).conjugate() == sec(conjugate(z))

    assert (sec(z).as_real_imag() ==
    (cos(re(z))*cosh(im(z))/(sin(re(z))**2*sinh(im(z))**2 +
                             cos(re(z))**2*cosh(im(z))**2),
     sin(re(z))*sinh(im(z))/(sin(re(z))**2*sinh(im(z))**2 +
                             cos(re(z))**2*cosh(im(z))**2)))

    assert sec(x).expand(trig=True) == 1/cos(x)
    assert sec(2*x).expand(trig=True) == 1/(2*cos(x)**2 - 1)

    assert sec(x).is_real == True
    assert sec(z).is_real == None

    assert sec(x).as_leading_term() == sec(x)

    assert sec(0).is_bounded == True
    assert sec(x).is_bounded == None
    assert sec(pi/2).is_bounded == False

    assert series(sec(x), x, x0=0, n=6) == 1 + x**2/2 + 5*x**4/24 + O(x**6)

    # https://code.google.com/p/sympy/issues/detail?id=4067
    assert series(sqrt(sec(x))) == 1 + x**2/4 + 7*x**4/96 + O(x**6)

    # https://code.google.com/p/sympy/issues/detail?id=4068
    assert (series(sqrt(sec(x)), x, x0=pi*3/2, n=4) ==
            1/sqrt(x) +x**(S(3)/2)/12 + x**(S(7)/2)/160 + O(x**4))

    assert sec(x).diff(x) == tan(x)*sec(x)
Example #24
0
def test_real_imag():
    a,b = symbols('a,b', real=True)
    z = a+b*I
    for deep in [True, False]:
        assert sin(z).as_real_imag(deep=deep) == (sin(a)*cosh(b), cos(a)*sinh(b))
        assert cos(z).as_real_imag(deep=deep) == (cos(a)*cosh(b), -sin(a)*sinh(b))
        assert tan(z).as_real_imag(deep=deep) == (sin(a)*cos(a)/(cos(a)**2+sinh(b)**2), sinh(b)*cosh(b)/(cos(a)**2+sinh(b)**2))
        assert cot(z).as_real_imag(deep=deep) == (sin(a)*cos(a)/(sin(a)**2+sinh(b)**2), -sinh(b)*cosh(b)/(sin(a)**2+sinh(b)**2))
        assert sin(a).as_real_imag(deep=deep) == (sin(a), 0)
        assert cos(a).as_real_imag(deep=deep) == (cos(a), 0)
        assert tan(a).as_real_imag(deep=deep) == (tan(a), 0)
        assert cot(a).as_real_imag(deep=deep) == (cot(a), 0)
Example #25
0
def test_sin_rewrite():
    assert sin(x).rewrite(exp) == -I * (exp(I * x) - exp(-I * x)) / 2
    assert sin(x).rewrite(tan) == 2 * tan(x / 2) / (1 + tan(x / 2) ** 2)
    assert sin(x).rewrite(cot) == 2 * cot(x / 2) / (1 + cot(x / 2) ** 2)
    assert sin(sinh(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, sinh(3)).n()
    assert sin(cosh(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cosh(3)).n()
    assert sin(tanh(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, tanh(3)).n()
    assert sin(coth(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, coth(3)).n()
    assert sin(sin(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, sin(3)).n()
    assert sin(cos(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cos(3)).n()
    assert sin(tan(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, tan(3)).n()
    assert sin(cot(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cot(3)).n()
    assert sin(log(x)).rewrite(Pow) == I * x ** -I / 2 - I * x ** I / 2
Example #26
0
def test_simplifications():
    x = Symbol("x")
    assert sinh(asinh(x)) == x
    assert sinh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1)
    assert sinh(atanh(x)) == x / sqrt(1 - x ** 2)

    assert cosh(asinh(x)) == sqrt(1 + x ** 2)
    assert cosh(acosh(x)) == x
    assert cosh(atanh(x)) == 1 / sqrt(1 - x ** 2)

    assert tanh(asinh(x)) == x / sqrt(1 + x ** 2)
    assert tanh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1) / x
    assert tanh(atanh(x)) == x
Example #27
0
def test_cos_rewrite():
    assert cos(x).rewrite(exp) == exp(I * x) / 2 + exp(-I * x) / 2
    assert cos(x).rewrite(tan) == (1 - tan(x / 2) ** 2) / (1 + tan(x / 2) ** 2)
    assert cos(x).rewrite(cot) == -(1 - cot(x / 2) ** 2) / (1 + cot(x / 2) ** 2)
    assert cos(sinh(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, sinh(3)).n()
    assert cos(cosh(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cosh(3)).n()
    assert cos(tanh(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tanh(3)).n()
    assert cos(coth(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, coth(3)).n()
    assert cos(sin(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, sin(3)).n()
    assert cos(cos(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cos(3)).n()
    assert cos(tan(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tan(3)).n()
    assert cos(cot(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cot(3)).n()
    assert cos(log(x)).rewrite(Pow) == x ** I / 2 + x ** -I / 2
Example #28
0
def test_trigsimp1a():
    assert trigsimp(sin(2)**2*cos(3)*exp(2)/cos(2)**2) == tan(2)**2*cos(3)*exp(2)
    assert trigsimp(tan(2)**2*cos(3)*exp(2)*cos(2)**2) == sin(2)**2*cos(3)*exp(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*sin(2)) == cos(3)*exp(2)*cos(2)
    assert trigsimp(tan(2)*cos(3)*exp(2)/sin(2)) == cos(3)*exp(2)/cos(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)/cos(2)) == cos(3)*exp(2)/sin(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*tan(2)) == cos(3)*exp(2)
    assert trigsimp(sinh(2)*cos(3)*exp(2)/cosh(2)) == tanh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)*cosh(2)) == sinh(2)*cos(3)*exp(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*sinh(2)) == cosh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)/sinh(2)) == cos(3)*exp(2)/cosh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)/cosh(2)) == cos(3)*exp(2)/sinh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*tanh(2)) == cos(3)*exp(2)
Example #29
0
def test_expint():
    """ Test various exponential integrals. """
    from sympy import (expint, unpolarify, Symbol, Ci, Si, Shi, Chi,
                       sin, cos, sinh, cosh, Ei)
    assert simplify(unpolarify(integrate(exp(-z*x)/x**y, (x, 1, oo),
                meijerg=True, conds='none'
                ).rewrite(expint).expand(func=True))) == expint(y, z)

    assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(1, z)
    assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(2, z).rewrite(Ei).rewrite(expint)
    assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(3, z).rewrite(Ei).rewrite(expint).expand()

    t = Symbol('t', positive=True)
    assert integrate(-cos(x)/x, (x, t, oo), meijerg=True).expand() == Ci(t)
    assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \
        Si(t) - pi/2
    assert integrate(sin(x)/x, (x, 0, z), meijerg=True) == Si(z)
    assert integrate(sinh(x)/x, (x, 0, z), meijerg=True) == Shi(z)
    assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \
        I*pi - expint(1, x)
    assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \
        == expint(1, x) - exp(-x)/x - I*pi

    u = Symbol('u', polar=True)
    assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Ci(u)
    assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Chi(u)

    assert integrate(expint(1, x), x, meijerg=True
            ).rewrite(expint).expand() == x*expint(1, x) - exp(-x)
    assert integrate(expint(2, x), x, meijerg=True
            ).rewrite(expint).expand() == \
        -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2
    assert simplify(unpolarify(integrate(expint(y, x), x,
                 meijerg=True).rewrite(expint).expand(func=True))) == \
        -expint(y + 1, x)

    assert integrate(Si(x), x, meijerg=True) == x*Si(x) + cos(x)
    assert integrate(Ci(u), u, meijerg=True).expand() == u*Ci(u) - sin(u)
    assert integrate(Shi(x), x, meijerg=True) == x*Shi(x) - cosh(x)
    assert integrate(Chi(u), u, meijerg=True).expand() == u*Chi(u) - sinh(u)

    assert integrate(Si(x)*exp(-x), (x, 0, oo), meijerg=True) == pi/4
    assert integrate(expint(1, x)*sin(x), (x, 0, oo), meijerg=True) == log(2)/2
Example #30
0
def test_derivs():
    x = Symbol('x')
    assert coth(x).diff(x) == -sinh(x)**(-2)
    assert sinh(x).diff(x) == cosh(x)
    assert cosh(x).diff(x) == sinh(x)
    assert tanh(x).diff(x) == -tanh(x)**2 + 1
    assert csch(x).diff(x) == -coth(x)*csch(x)
    assert sech(x).diff(x) == -tanh(x)*sech(x)
    assert acoth(x).diff(x) == 1/(-x**2 + 1)
    assert asinh(x).diff(x) == 1/sqrt(x**2 + 1)
    assert acosh(x).diff(x) == 1/sqrt(x**2 - 1)
    assert atanh(x).diff(x) == 1/(-x**2 + 1)
    assert asech(x).diff(x) == -1/(x*sqrt(1 - x**2))
    assert acsch(x).diff(x) == -1/(x**2*sqrt(1 + x**(-2)))
Example #31
0
def test_intrinsic_math_codegen():
    # not included: log10
    from sympy import (acos, asin, atan, ceiling, cos, cosh, floor, log, ln,
                       sin, sinh, sqrt, tan, tanh, Abs)
    x = symbols('x')
    name_expr = [
        ("test_abs", Abs(x)),
        ("test_acos", acos(x)),
        ("test_asin", asin(x)),
        ("test_atan", atan(x)),
        # ("test_ceil", ceiling(x)),
        ("test_cos", cos(x)),
        ("test_cosh", cosh(x)),
        # ("test_floor", floor(x)),
        ("test_log", log(x)),
        ("test_ln", ln(x)),
        ("test_sin", sin(x)),
        ("test_sinh", sinh(x)),
        ("test_sqrt", sqrt(x)),
        ("test_tan", tan(x)),
        ("test_tanh", tanh(x)),
    ]
    result = codegen(name_expr, "F95", "file", header=False, empty=False)
    assert result[0][0] == "file.f90"
    expected = ('REAL*8 function test_abs(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_abs = Abs(x)\n'
                'end function\n'
                'REAL*8 function test_acos(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_acos = acos(x)\n'
                'end function\n'
                'REAL*8 function test_asin(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_asin = asin(x)\n'
                'end function\n'
                'REAL*8 function test_atan(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_atan = atan(x)\n'
                'end function\n'
                'REAL*8 function test_cos(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_cos = cos(x)\n'
                'end function\n'
                'REAL*8 function test_cosh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_cosh = cosh(x)\n'
                'end function\n'
                'REAL*8 function test_log(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_log = log(x)\n'
                'end function\n'
                'REAL*8 function test_ln(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_ln = log(x)\n'
                'end function\n'
                'REAL*8 function test_sin(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_sin = sin(x)\n'
                'end function\n'
                'REAL*8 function test_sinh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_sinh = sinh(x)\n'
                'end function\n'
                'REAL*8 function test_sqrt(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_sqrt = sqrt(x)\n'
                'end function\n'
                'REAL*8 function test_tan(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_tan = tan(x)\n'
                'end function\n'
                'REAL*8 function test_tanh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'test_tanh = tanh(x)\n'
                'end function\n')
    assert result[0][1] == expected

    assert result[1][0] == "file.h"
    expected = ('interface\n'
                'REAL*8 function test_abs(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_acos(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_asin(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_atan(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_cos(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_cosh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_log(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_ln(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_sin(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_sinh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_sqrt(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_tan(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n'
                'interface\n'
                'REAL*8 function test_tanh(x)\n'
                'implicit none\n'
                'REAL*8, intent(in) :: x\n'
                'end function\n'
                'end interface\n')
    assert result[1][1] == expected
Example #32
0
def test_coth_rewrite():
    x = Symbol('x')
    assert coth(x).rewrite(exp) == (exp(x) + exp(-x)) / (exp(x) - exp(-x))
    assert coth(x).rewrite(sinh) == -I * sinh(I * pi / 2 - x) / sinh(x)
    assert coth(x).rewrite(cosh) == -I * cosh(x) / cosh(I * pi / 2 - x)
    assert coth(x).rewrite(tanh) == 1 / tanh(x)
Example #33
0
def test_sinh():
    x, y = symbols('xy')

    k = Symbol('k', integer=True)

    assert sinh(nan) == nan

    assert sinh(oo) == oo
    assert sinh(-oo) == -oo

    assert sinh(0) == 0

    assert sinh(1) == sinh(1)
    assert sinh(-1) == -sinh(1)

    assert sinh(x) == sinh(x)
    assert sinh(-x) == -sinh(x)

    assert sinh(pi) == sinh(pi)
    assert sinh(-pi) == -sinh(pi)

    assert sinh(2**1024 * E) == sinh(2**1024 * E)
    assert sinh(-2**1024 * E) == -sinh(2**1024 * E)

    assert sinh(pi * I) == 0
    assert sinh(-pi * I) == 0
    assert sinh(2 * pi * I) == 0
    assert sinh(-2 * pi * I) == 0
    assert sinh(-3 * 10**73 * pi * I) == 0
    assert sinh(7 * 10**103 * pi * I) == 0

    assert sinh(pi * I / 2) == I
    assert sinh(-pi * I / 2) == -I
    assert sinh(5 * pi * I / 2) == I
    assert sinh(7 * pi * I / 2) == -I

    assert sinh(pi * I / 3) == S.Half * sqrt(3) * I
    assert sinh(-2 * pi * I / 3) == -S.Half * sqrt(3) * I

    assert sinh(pi * I / 4) == S.Half * sqrt(2) * I
    assert sinh(-pi * I / 4) == -S.Half * sqrt(2) * I
    assert sinh(17 * pi * I / 4) == S.Half * sqrt(2) * I
    assert sinh(-3 * pi * I / 4) == -S.Half * sqrt(2) * I

    assert sinh(pi * I / 6) == S.Half * I
    assert sinh(-pi * I / 6) == -S.Half * I
    assert sinh(7 * pi * I / 6) == -S.Half * I
    assert sinh(-5 * pi * I / 6) == -S.Half * I

    assert sinh(pi * I / 105) == sin(pi / 105) * I
    assert sinh(-pi * I / 105) == -sin(pi / 105) * I

    assert sinh(2 + 3 * I) == sinh(2 + 3 * I)

    assert sinh(x * I) == sin(x) * I

    assert sinh(k * pi * I) == 0
    assert sinh(17 * k * pi * I) == 0

    assert sinh(k * pi * I / 2) == sin(k * pi / 2) * I
def test_sec():
    x = symbols('x', real=True)
    z = symbols('z')

    assert sec.nargs == FiniteSet(1)

    assert sec(0) == 1
    assert sec(pi) == -1
    assert sec(pi / 2) == zoo
    assert sec(-pi / 2) == zoo
    assert sec(pi / 6) == 2 * sqrt(3) / 3
    assert sec(pi / 3) == 2
    assert sec(5 * pi / 2) == zoo
    assert sec(9 * pi / 7) == -sec(2 * pi / 7)
    assert sec(I) == 1 / cosh(1)
    assert sec(x * I) == 1 / cosh(x)
    assert sec(-x) == sec(x)

    assert sec(asec(x)) == x

    assert sec(x).rewrite(exp) == 1 / (exp(I * x) / 2 + exp(-I * x) / 2)
    assert sec(x).rewrite(sin) == sec(x)
    assert sec(x).rewrite(cos) == 1 / cos(x)
    assert sec(x).rewrite(tan) == (tan(x / 2)**2 + 1) / (-tan(x / 2)**2 + 1)
    assert sec(x).rewrite(pow) == sec(x)
    assert sec(x).rewrite(sqrt) == sec(x)
    assert sec(z).rewrite(cot) == (cot(z / 2)**2 + 1) / (cot(z / 2)**2 - 1)

    assert sec(z).conjugate() == sec(conjugate(z))

    assert (sec(z).as_real_imag() == (
        cos(re(z)) * cosh(im(z)) /
        (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2),
        sin(re(z)) * sinh(im(z)) /
        (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2)))

    assert sec(x).expand(trig=True) == 1 / cos(x)
    assert sec(2 * x).expand(trig=True) == 1 / (2 * cos(x)**2 - 1)

    assert sec(x).is_real == True
    assert sec(z).is_real == None

    assert sec(a).is_algebraic is None
    assert sec(na).is_algebraic is False

    assert sec(x).as_leading_term() == sec(x)

    assert sec(0).is_finite == True
    assert sec(x).is_finite == None
    assert sec(pi / 2).is_finite == False

    assert series(sec(x), x, x0=0,
                  n=6) == 1 + x**2 / 2 + 5 * x**4 / 24 + O(x**6)

    # https://github.com/sympy/sympy/issues/7166
    assert series(sqrt(sec(x))) == 1 + x**2 / 4 + 7 * x**4 / 96 + O(x**6)

    # https://github.com/sympy/sympy/issues/7167
    assert (series(sqrt(sec(x)), x, x0=pi * 3 / 2,
                   n=4) == 1 / sqrt(x - 3 * pi / 2) +
            (x - 3 * pi / 2)**(S(3) / 2) / 12 +
            (x - 3 * pi / 2)**(S(7) / 2) / 160 + O((x - 3 * pi / 2)**4,
                                                   (x, 3 * pi / 2)))

    assert sec(x).diff(x) == tan(x) * sec(x)

    # Taylor Term checks
    assert sec(z).taylor_term(4, z) == 5 * z**4 / 24
    assert sec(z).taylor_term(6, z) == 61 * z**6 / 720
    assert sec(z).taylor_term(5, z) == 0
Example #35
0
 def Zhfun_exact(self, j):
     kj = j * self.dk
     return sp.sinh(kj * (self.zswd + self.d)) / sp.cosh(kj * self.d)
def test_csc():
    x = symbols('x', real=True)
    z = symbols('z')

    # https://github.com/sympy/sympy/issues/6707
    cosecant = csc('x')
    alternate = 1 / sin('x')
    assert cosecant.equals(alternate) == True
    assert alternate.equals(cosecant) == True

    assert csc.nargs == FiniteSet(1)

    assert csc(0) == zoo
    assert csc(pi) == zoo

    assert csc(pi / 2) == 1
    assert csc(-pi / 2) == -1
    assert csc(pi / 6) == 2
    assert csc(pi / 3) == 2 * sqrt(3) / 3
    assert csc(5 * pi / 2) == 1
    assert csc(9 * pi / 7) == -csc(2 * pi / 7)
    assert csc(I) == -I / sinh(1)
    assert csc(x * I) == -I / sinh(x)
    assert csc(-x) == -csc(x)

    assert csc(acsc(x)) == x

    assert csc(x).rewrite(exp) == 2 * I / (exp(I * x) - exp(-I * x))
    assert csc(x).rewrite(sin) == 1 / sin(x)
    assert csc(x).rewrite(cos) == csc(x)
    assert csc(x).rewrite(tan) == (tan(x / 2)**2 + 1) / (2 * tan(x / 2))
    assert csc(x).rewrite(cot) == (cot(x / 2)**2 + 1) / (2 * cot(x / 2))

    assert csc(z).conjugate() == csc(conjugate(z))

    assert (csc(z).as_real_imag() == (
        sin(re(z)) * cosh(im(z)) /
        (sin(re(z))**2 * cosh(im(z))**2 + cos(re(z))**2 * sinh(im(z))**2),
        -cos(re(z)) * sinh(im(z)) /
        (sin(re(z))**2 * cosh(im(z))**2 + cos(re(z))**2 * sinh(im(z))**2)))

    assert csc(x).expand(trig=True) == 1 / sin(x)
    assert csc(2 * x).expand(trig=True) == 1 / (2 * sin(x) * cos(x))

    assert csc(x).is_real == True
    assert csc(z).is_real == None

    assert csc(a).is_algebraic is None
    assert csc(na).is_algebraic is False

    assert csc(x).as_leading_term() == csc(x)

    assert csc(0).is_finite == False
    assert csc(x).is_finite == None
    assert csc(pi / 2).is_finite == True

    assert series(csc(x), x, x0=pi/2, n=6) == \
        1 + (x - pi/2)**2/2 + 5*(x - pi/2)**4/24 + O((x - pi/2)**6, (x, pi/2))
    assert series(csc(x), x, x0=0, n=6) == \
            1/x + x/6 + 7*x**3/360 + 31*x**5/15120 + O(x**6)

    assert csc(x).diff(x) == -cot(x) * csc(x)

    assert csc(x).taylor_term(2, x) == 0
    assert csc(x).taylor_term(3, x) == 7 * x**3 / 360
    assert csc(x).taylor_term(5, x) == 31 * x**5 / 15120
Example #37
0
def transform_coords(coord_sys, coords, a=1, b=1, c=1):
    """
    Return transformation for predefined coordinate systems.

    Parameters
    -------
    coord_sys : string
        Coordinate system.
    coords : Tuple (3)
        Coordinates for the new reference system.
    a : SymPy expression, optional
        Additional parameter for some coordinate systems.
    b : SymPy expression, optional
        Additional parameter for some coordinate systems.
    c : SymPy expression, optional
        Additional parameter for some coordinate systems.

    Returns
    -------
    h_vec : Tuple (3)
        Scale coefficients.

    References
    ----------
    .. [ORTHO] Wikipedia contributors, 'Orthogonal coordinates',
        Wikipedia, The Free Encyclopedia, 2019
    """
    if not isinstance(coord_sys, str):
        raise TypeError("The coordinate system should be defined by a string")
    u, v, w = coords
    r_dict = {
        "cartesian": (u, v, w),
        "cylindrical": (u * cos(v), u * sin(v), w),
        "spherical": (u * sin(v) * cos(w), u * sin(v) * sin(w), u * sin(v)),
        "parabolic_cylindrical": ((u**2 - v**2) / 2, u * v, w),
        "parabolic": (u * v * cos(w), u * v * sin(w), (u**2 - v**2) / 2),
        "paraboloidal":
        (sqrt((a**2 - u) * (a**2 - v) * (a**2 - w) / (b**2 - a**2)),
         sqrt((b**2 - u) * (b**2 - v) * (b**2 - w) / (a**2 - b**2)),
         S(1) / 2 * (a**2 + b**2 - u - v - w)),
        "elliptic_cylindrical":
        (a * cosh(u) * cos(v), a * sinh(u) * sin(v), w),
        "oblate_spheroidal":
        (a * cosh(u) * cos(v) * cos(w), a * cosh(u) * cos(v) * sin(w),
         a * sinh(u) * sin(v)),
        "prolate_spheroidal":
        (a * sinh(u) * sin(v) * cos(w), a * sinh(u) * sin(v) * sin(w),
         a * cosh(u) * cos(v)),
        "ellipsoidal": (sqrt((a**2 + u) * (a**2 + v) * (a**2 + w) /
                             ((a**2 - b**2) * (a**2 - c**2))),
                        sqrt((b**2 + u) * (b**2 + v) * (b**2 + w) /
                             ((b**2 - a**2) * (b**2 - c**2))),
                        sqrt((c**2 + u) * (c**2 + v) * (c**2 + w) /
                             ((c**2 - b**2) * (c**2 - a**2)))),
        "bipolar_cylindrical": (a * sinh(v) / (cosh(v) - cos(u)),
                                a * sin(u) / (cosh(v) - cos(u)), w),
        "toroidal": (a * sinh(v) * cos(w) / (cosh(v) - cos(u)),
                     a * sinh(v) * sin(w) / (cosh(v) - cos(u)),
                     a * sin(u) / (cosh(v) - cos(u))),
        "bispherical": (a * sin(u) * cos(w) / (cosh(v) - cos(u)),
                        a * sin(u) * sin(w) / (cosh(v) - cos(u)),
                        a * sinh(v) / (cosh(v) - cos(u))),
        "conical": (u * v * w / (a * b), u / a * sqrt(
            (v**2 - a**2) * (w**2 - a**2) / (a**2 - b**2)), u / b * sqrt(
                (v**2 - b**2) * (w**2 - b**2) / (a**2 - b**2)))
    }
    if coord_sys not in r_dict.keys():
        msg = "System coordinate not available.\n\nAvailable options are:\n"
        raise ValueError(msg + ", ".join(r_dict.keys()))
    return r_dict[coord_sys]
Example #38
0
import math

# Based on https://foggyhazel.wordpress.com/2018/02/12/catenary-passing-through-2-points/

dx, dy, b = sympy.symbols("dx dy b")

start_x = 100
start_y = 200
end_x = 500
end_y = 500

vals = [(dx, end_x - start_x), (dy, end_y - start_y)]

s = 2.0 * sympy.sqrt(dx ** 2 + dy ** 2)

f = 1 / sympy.sqrt(2 * b**2 * sympy.sinh(1 / (2 * b**2)) - 1) - 1 / sympy.sqrt(sympy.sqrt(s**2 - dy**2) / dx - 1)
df = f.diff(b)

print (f"f(x): {f}")
print (f"df(x): {df}")

print (sympy.simplify(f))
print (sympy.trigsimp(f))

def solve(f):
    return (f.subs(vals).subs(b, x)).evalf()

x = 10
for i in range(10):
    print (f"Iteration {i}, x={x} f(x)={solve(f)} df(x)={solve(df)}")
    x = x - solve(f / df)
def test_sinh_series():
    x = Symbol('x')
    assert sinh(x).series(x, 0, 10) == \
        x + x**3/6 + x**5/120 + x**7/5040 + x**9/362880 + O(x**10)
Example #40
0
def test_expand():
    from sympy import besselsimp, Symbol, exp, exp_polar, I

    assert expand_func(besselj(S.Half, z).rewrite(jn)) == \
        sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(bessely(S.Half, z).rewrite(yn)) == \
        -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))

    # XXX: teach sin/cos to work around arguments like
    # x*exp_polar(I*pi*n/2).  Then change besselsimp -> expand_func
    assert besselsimp(besselj(S.Half,
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(-1, 2),
                              z)) == sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besselj(Rational(-5, 2), z)) == \
        -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(bessely(S.Half, z)) == \
        -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z))
    assert besselsimp(bessely(Rational(-1, 2),
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(bessely(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(bessely(Rational(-5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besseli(S.Half,
                              z)) == sqrt(2) * sinh(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besseli(Rational(-1, 2), z)) == \
        sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besseli(Rational(-5, 2), z)) == \
        sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besselk(S.Half, z)) == \
        besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z))
    assert besselsimp(besselk(Rational(5, 2), z)) == \
        besselsimp(besselk(Rational(-5, 2), z)) == \
        sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2))

    def check(eq, ans):
        return tn(eq, ans) and eq == ans

    rn = randcplx(a=1, b=0, d=0, c=2)

    for besselx in [besselj, bessely, besseli, besselk]:
        ri = S(2 * randint(-11, 10) + 1) / 2  # half integer in [-21/2, 21/2]
        assert tn(besselsimp(besselx(ri, z)), besselx(ri, z))

    assert check(expand_func(besseli(rn, x)),
                 besseli(rn - 2, x) - 2 * (rn - 1) * besseli(rn - 1, x) / x)
    assert check(expand_func(besseli(-rn, x)),
                 besseli(-rn + 2, x) + 2 * (-rn + 1) * besseli(-rn + 1, x) / x)

    assert check(expand_func(besselj(rn, x)),
                 -besselj(rn - 2, x) + 2 * (rn - 1) * besselj(rn - 1, x) / x)
    assert check(
        expand_func(besselj(-rn, x)),
        -besselj(-rn + 2, x) + 2 * (-rn + 1) * besselj(-rn + 1, x) / x)

    assert check(expand_func(besselk(rn, x)),
                 besselk(rn - 2, x) + 2 * (rn - 1) * besselk(rn - 1, x) / x)
    assert check(expand_func(besselk(-rn, x)),
                 besselk(-rn + 2, x) - 2 * (-rn + 1) * besselk(-rn + 1, x) / x)

    assert check(expand_func(bessely(rn, x)),
                 -bessely(rn - 2, x) + 2 * (rn - 1) * bessely(rn - 1, x) / x)
    assert check(
        expand_func(bessely(-rn, x)),
        -bessely(-rn + 2, x) + 2 * (-rn + 1) * bessely(-rn + 1, x) / x)

    n = Symbol('n', integer=True, positive=True)

    assert expand_func(besseli(n + 2, z)) == \
        besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z
    assert expand_func(besselj(n + 2, z)) == \
        -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z
    assert expand_func(besselk(n + 2, z)) == \
        besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z
    assert expand_func(bessely(n + 2, z)) == \
        -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z

    assert expand_func(besseli(n + S.Half, z).rewrite(jn)) == \
        (sqrt(2)*sqrt(z)*exp(-I*pi*(n + S.Half)/2) *
         exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi))
    assert expand_func(besselj(n + S.Half, z).rewrite(jn)) == \
        sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi)

    r = Symbol('r', real=True)
    p = Symbol('p', positive=True)
    i = Symbol('i', integer=True)

    for besselx in [besselj, bessely, besseli, besselk]:
        assert besselx(i, p).is_extended_real is True
        assert besselx(i, x).is_extended_real is None
        assert besselx(x, z).is_extended_real is None

    for besselx in [besselj, besseli]:
        assert besselx(i, r).is_extended_real is True
    for besselx in [bessely, besselk]:
        assert besselx(i, r).is_extended_real is None
Example #41
0
def test_issue_8901():
    assert integrate(sinh(1.0 * x)) == 1.0 * cosh(1.0 * x)
    assert integrate(tanh(1.0 * x)) == 1.0 * x - 1.0 * log(tanh(1.0 * x) + 1)
    assert integrate(tanh(x)) == x - log(tanh(x) + 1)
Example #42
0
sympy_mappings = {
    'div': lambda x, y: x / y,
    'mult': lambda x, y: x * y,
    'sqrtm': lambda x: sympy.sqrt(abs(x)),
    'square': lambda x: x**2,
    'cube': lambda x: x**3,
    'plus': lambda x, y: x + y,
    'sub': lambda x, y: x - y,
    'neg': lambda x: -x,
    'pow': lambda x, y: sympy.sign(x) * abs(x)**y,
    'cos': lambda x: sympy.cos(x),
    'sin': lambda x: sympy.sin(x),
    'tan': lambda x: sympy.tan(x),
    'cosh': lambda x: sympy.cosh(x),
    'sinh': lambda x: sympy.sinh(x),
    'tanh': lambda x: sympy.tanh(x),
    'exp': lambda x: sympy.exp(x),
    'acos': lambda x: sympy.acos(x),
    'asin': lambda x: sympy.asin(x),
    'atan': lambda x: sympy.atan(x),
    'acosh': lambda x: sympy.acosh(x),
    'asinh': lambda x: sympy.asinh(x),
    'atanh': lambda x: sympy.atanh(x),
    'abs': lambda x: abs(x),
    'mod': lambda x, y: sympy.Mod(x, y),
    'erf': lambda x: sympy.erf(x),
    'erfc': lambda x: sympy.erfc(x),
    'logm': lambda x: sympy.log(abs(x)),
    'logm10': lambda x: sympy.log10(abs(x)),
    'logm2': lambda x: sympy.log2(abs(x)),
Example #43
0
def test_moment_generating_function():
    t = symbols('t', positive=True)

    # Symbolic tests
    a, b, c = symbols('a b c')

    mgf = moment_generating_function(Beta('x', a, b))(t)
    assert mgf == hyper((a,), (a + b,), t)

    mgf = moment_generating_function(Chi('x', a))(t)
    assert mgf == sqrt(2)*t*gamma(a/2 + S.Half)*\
        hyper((a/2 + S.Half,), (Rational(3, 2),), t**2/2)/gamma(a/2) +\
        hyper((a/2,), (S.Half,), t**2/2)

    mgf = moment_generating_function(ChiSquared('x', a))(t)
    assert mgf == (1 - 2*t)**(-a/2)

    mgf = moment_generating_function(Erlang('x', a, b))(t)
    assert mgf == (1 - t/b)**(-a)

    mgf = moment_generating_function(ExGaussian("x", a, b, c))(t)
    assert mgf == exp(a*t + b**2*t**2/2)/(1 - t/c)

    mgf = moment_generating_function(Exponential('x', a))(t)
    assert mgf == a/(a - t)

    mgf = moment_generating_function(Gamma('x', a, b))(t)
    assert mgf == (-b*t + 1)**(-a)

    mgf = moment_generating_function(Gumbel('x', a, b))(t)
    assert mgf == exp(b*t)*gamma(-a*t + 1)

    mgf = moment_generating_function(Gompertz('x', a, b))(t)
    assert mgf == b*exp(b)*expint(t/a, b)

    mgf = moment_generating_function(Laplace('x', a, b))(t)
    assert mgf == exp(a*t)/(-b**2*t**2 + 1)

    mgf = moment_generating_function(Logistic('x', a, b))(t)
    assert mgf == exp(a*t)*beta(-b*t + 1, b*t + 1)

    mgf = moment_generating_function(Normal('x', a, b))(t)
    assert mgf == exp(a*t + b**2*t**2/2)

    mgf = moment_generating_function(Pareto('x', a, b))(t)
    assert mgf == b*(-a*t)**b*uppergamma(-b, -a*t)

    mgf = moment_generating_function(QuadraticU('x', a, b))(t)
    assert str(mgf) == ("(3*(t*(-4*b + (a + b)**2) + 4)*exp(b*t) - "
    "3*(t*(a**2 + 2*a*(b - 2) + b**2) + 4)*exp(a*t))/(t**2*(a - b)**3)")

    mgf = moment_generating_function(RaisedCosine('x', a, b))(t)
    assert mgf == pi**2*exp(a*t)*sinh(b*t)/(b*t*(b**2*t**2 + pi**2))

    mgf = moment_generating_function(Rayleigh('x', a))(t)
    assert mgf == sqrt(2)*sqrt(pi)*a*t*(erf(sqrt(2)*a*t/2) + 1)\
        *exp(a**2*t**2/2)/2 + 1

    mgf = moment_generating_function(Triangular('x', a, b, c))(t)
    assert str(mgf) == ("(-2*(-a + b)*exp(c*t) + 2*(-a + c)*exp(b*t) + "
    "2*(b - c)*exp(a*t))/(t**2*(-a + b)*(-a + c)*(b - c))")

    mgf = moment_generating_function(Uniform('x', a, b))(t)
    assert mgf == (-exp(a*t) + exp(b*t))/(t*(-a + b))

    mgf = moment_generating_function(UniformSum('x', a))(t)
    assert mgf == ((exp(t) - 1)/t)**a

    mgf = moment_generating_function(WignerSemicircle('x', a))(t)
    assert mgf == 2*besseli(1, a*t)/(a*t)

    # Numeric tests

    mgf = moment_generating_function(Beta('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == hyper((2,), (3,), 1)/2

    mgf = moment_generating_function(Chi('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == sqrt(2)*hyper((1,), (Rational(3, 2),), S.Half
    )/sqrt(pi) + hyper((Rational(3, 2),), (Rational(3, 2),), S.Half) + 2*sqrt(2)*hyper((2,),
    (Rational(5, 2),), S.Half)/(3*sqrt(pi))

    mgf = moment_generating_function(ChiSquared('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == I

    mgf = moment_generating_function(Erlang('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(ExGaussian("x", 0, 1, 1))(t)
    assert mgf.diff(t).subs(t, 2) == -exp(2)

    mgf = moment_generating_function(Exponential('x', 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Gamma('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Gumbel('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == EulerGamma + 1

    mgf = moment_generating_function(Gompertz('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == -e*meijerg(((), (1, 1)),
    ((0, 0, 0), ()), 1)

    mgf = moment_generating_function(Laplace('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Logistic('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == beta(1, 1)

    mgf = moment_generating_function(Normal('x', 0, 1))(t)
    assert mgf.diff(t).subs(t, 1) == exp(S.Half)

    mgf = moment_generating_function(Pareto('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == expint(1, 0)

    mgf = moment_generating_function(QuadraticU('x', 1, 2))(t)
    assert mgf.diff(t).subs(t, 1) == -12*e - 3*exp(2)

    mgf = moment_generating_function(RaisedCosine('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == -2*e*pi**2*sinh(1)/\
    (1 + pi**2)**2 + e*pi**2*cosh(1)/(1 + pi**2)

    mgf = moment_generating_function(Rayleigh('x', 1))(t)
    assert mgf.diff(t).subs(t, 0) == sqrt(2)*sqrt(pi)/2

    mgf = moment_generating_function(Triangular('x', 1, 3, 2))(t)
    assert mgf.diff(t).subs(t, 1) == -e + exp(3)

    mgf = moment_generating_function(Uniform('x', 0, 1))(t)
    assert mgf.diff(t).subs(t, 1) == 1

    mgf = moment_generating_function(UniformSum('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == 1

    mgf = moment_generating_function(WignerSemicircle('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == -2*besseli(1, 1) + besseli(2, 1) +\
        besseli(0, 1)
Example #44
0
def test_issue_4941():
    if ON_TRAVIS:
        skip("Too slow for travis.")
    assert not integrate(sqrt(1 + sinh(x / 20)**2), (x, -25, 25)).has(Integral)
Example #45
0
def test_sin():
    x, y = symbols('x y')

    assert sin.nargs == FiniteSet(1)
    assert sin(nan) == nan

    assert sin(oo * I) == oo * I
    assert sin(-oo * I) == -oo * I
    assert sin(oo).args[0] == oo

    assert sin(0) == 0

    assert sin(asin(x)) == x
    assert sin(atan(x)) == x / sqrt(1 + x**2)
    assert sin(acos(x)) == sqrt(1 - x**2)
    assert sin(acot(x)) == 1 / (sqrt(1 + 1 / x**2) * x)
    assert sin(atan2(y, x)) == y / sqrt(x**2 + y**2)

    assert sin(pi * I) == sinh(pi) * I
    assert sin(-pi * I) == -sinh(pi) * I
    assert sin(-2 * I) == -sinh(2) * I

    assert sin(pi) == 0
    assert sin(-pi) == 0
    assert sin(2 * pi) == 0
    assert sin(-2 * pi) == 0
    assert sin(-3 * 10**73 * pi) == 0
    assert sin(7 * 10**103 * pi) == 0

    assert sin(pi / 2) == 1
    assert sin(-pi / 2) == -1
    assert sin(5 * pi / 2) == 1
    assert sin(7 * pi / 2) == -1

    n = symbols('n', integer=True)
    assert sin(pi * n / 2) == (-1)**(n / 2 - S.Half)

    assert sin(pi / 3) == S.Half * sqrt(3)
    assert sin(-2 * pi / 3) == -S.Half * sqrt(3)

    assert sin(pi / 4) == S.Half * sqrt(2)
    assert sin(-pi / 4) == -S.Half * sqrt(2)
    assert sin(17 * pi / 4) == S.Half * sqrt(2)
    assert sin(-3 * pi / 4) == -S.Half * sqrt(2)

    assert sin(pi / 6) == S.Half
    assert sin(-pi / 6) == -S.Half
    assert sin(7 * pi / 6) == -S.Half
    assert sin(-5 * pi / 6) == -S.Half

    assert sin(1 * pi / 5) == sqrt((5 - sqrt(5)) / 8)
    assert sin(2 * pi / 5) == sqrt((5 + sqrt(5)) / 8)
    assert sin(3 * pi / 5) == sin(2 * pi / 5)
    assert sin(4 * pi / 5) == sin(1 * pi / 5)
    assert sin(6 * pi / 5) == -sin(1 * pi / 5)
    assert sin(8 * pi / 5) == -sin(2 * pi / 5)

    assert sin(-1273 * pi / 5) == -sin(2 * pi / 5)

    assert sin(pi / 8) == sqrt((2 - sqrt(2)) / 4)

    assert sin(104 * pi / 105) == sin(pi / 105)
    assert sin(106 * pi / 105) == -sin(pi / 105)

    assert sin(-104 * pi / 105) == -sin(pi / 105)
    assert sin(-106 * pi / 105) == sin(pi / 105)

    assert sin(x * I) == sinh(x) * I

    assert sin(k * pi) == 0
    assert sin(17 * k * pi) == 0

    assert sin(k * pi * I) == sinh(k * pi) * I

    assert sin(r).is_real is True

    assert isinstance(sin(re(x) - im(y)), sin) is True
    assert isinstance(sin(-re(x) + im(y)), sin) is False

    for d in list(range(1, 22)) + [60, 85]:
        for n in xrange(0, d * 2 + 1):
            x = n * pi / d
            e = abs(float(sin(x)) - sin(float(x)))
            assert e < 1e-12
Example #46
0
def test_inverse_laplace_transform():
    from sympy import sinh, cosh, besselj, besseli, simplify, factor_terms,\
        DiracDelta
    ILT = inverse_laplace_transform
    a, b, c, = symbols('a b c', positive=True)
    t = symbols('t')

    def simp_hyp(expr):
        return factor_terms(expand_mul(expr)).rewrite(sin)

    assert ILT(1, s, t) == DiracDelta(t)
    assert ILT(1 / s, s, t) == Heaviside(t)
    assert ILT(a / (a + s), s, t) == a * exp(-a * t) * Heaviside(t)
    assert ILT(s / (a + s), s,
               t) == -a * exp(-a * t) * Heaviside(t) + DiracDelta(t)
    assert ILT((a + s)**(-2), s, t) == t * exp(-a * t) * Heaviside(t)
    assert ILT((a + s)**(-5), s, t) == t**4 * exp(-a * t) * Heaviside(t) / 24
    assert ILT(a / (a**2 + s**2), s, t) == sin(a * t) * Heaviside(t)
    assert ILT(s / (s**2 + a**2), s, t) == cos(a * t) * Heaviside(t)
    assert ILT(b / (b**2 + (a + s)**2), s,
               t) == exp(-a * t) * sin(b * t) * Heaviside(t)
    assert ILT(b*s/(b**2 + (a + s)**2), s, t) +\
        (a*sin(b*t) - b*cos(b*t))*exp(-a*t)*Heaviside(t) == 0
    assert ILT(exp(-a * s) / s, s, t) == Heaviside(-a + t)
    assert ILT(exp(-a * s) / (b + s), s,
               t) == exp(b * (a - t)) * Heaviside(-a + t)
    assert ILT((b + s)/(a**2 + (b + s)**2), s, t) == \
        exp(-b*t)*cos(a*t)*Heaviside(t)
    assert ILT(exp(-a*s)/s**b, s, t) == \
        (-a + t)**(b - 1)*Heaviside(-a + t)/gamma(b)
    assert ILT(exp(-a*s)/sqrt(s**2 + 1), s, t) == \
        Heaviside(-a + t)*besselj(0, a - t)
    assert ILT(1 / (s * sqrt(s + 1)), s, t) == Heaviside(t) * erf(sqrt(t))
    assert ILT(1 / (s**2 * (s**2 + 1)), s, t) == (t - sin(t)) * Heaviside(t)
    assert ILT(s**2 / (s**2 + 1), s,
               t) == -sin(t) * Heaviside(t) + DiracDelta(t)
    assert ILT(1 - 1 / (s**2 + 1), s,
               t) == -sin(t) * Heaviside(t) + DiracDelta(t)
    assert ILT(1 / s**2, s, t) == t * Heaviside(t)
    assert ILT(1 / s**5, s, t) == t**4 * Heaviside(t) / 24
    assert simp_hyp(ILT(a / (s**2 - a**2), s, t)) == sinh(a * t) * Heaviside(t)
    assert simp_hyp(ILT(s / (s**2 - a**2), s, t)) == cosh(a * t) * Heaviside(t)
    # TODO sinh/cosh shifted come out a mess. also delayed trig is a mess
    # TODO should this simplify further?
    assert ILT(exp(-a*s)/s**b, s, t) == \
        (t - a)**(b - 1)*Heaviside(t - a)/gamma(b)
    assert ILT(exp(-a*s)/sqrt(1 + s**2), s, t) == \
        Heaviside(t - a)*besselj(0, a - t)  # note: besselj(0, x) is even
    # XXX ILT turns these branch factor into trig functions ...
    assert simplify(ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2),
                    s, t).rewrite(exp)) == \
        Heaviside(t)*besseli(b, a*t)
    assert ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2),
               s, t).rewrite(exp) == \
        Heaviside(t)*besselj(b, a*t)

    assert ILT(1 / (s * sqrt(s + 1)), s, t) == Heaviside(t) * erf(sqrt(t))
    # TODO can we make erf(t) work?

    assert ILT(1 / (s**2 * (s**2 + 1)), s, t) == (t - sin(t)) * Heaviside(t)

    assert ILT( (s * eye(2) - Matrix([[1, 0], [0, 2]])).inv(), s, t) ==\
        Matrix([[exp(t)*Heaviside(t), 0], [0, exp(2*t)*Heaviside(t)]])
def test_complex():
    a, b = symbols('a,b', real=True)
    z = a + b*I
    for func in [sinh, cosh, tanh, coth, sech, csch]:
        assert func(z).conjugate() == func(a - b*I)
    for deep in [True, False]:
        assert sinh(z).expand(
            complex=True, deep=deep) == sinh(a)*cos(b) + I*cosh(a)*sin(b)
        assert cosh(z).expand(
            complex=True, deep=deep) == cosh(a)*cos(b) + I*sinh(a)*sin(b)
        assert tanh(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
            a)/(cos(b)**2 + sinh(a)**2) + I*sin(b)*cos(b)/(cos(b)**2 + sinh(a)**2)
        assert coth(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
            a)/(sin(b)**2 + sinh(a)**2) - I*sin(b)*cos(b)/(sin(b)**2 + sinh(a)**2)
        assert csch(z).expand(complex=True, deep=deep) == cos(b) * sinh(a) / (sin(b)**2\
            *cosh(a)**2 + cos(b)**2 * sinh(a)**2) - I*sin(b) * cosh(a) / (sin(b)**2\
            *cosh(a)**2 + cos(b)**2 * sinh(a)**2)
        assert sech(z).expand(complex=True, deep=deep) == cos(b) * cosh(a) / (sin(b)**2\
            *sinh(a)**2 + cos(b)**2 * cosh(a)**2) - I*sin(b) * sinh(a) / (sin(b)**2\
            *sinh(a)**2 + cos(b)**2 * cosh(a)**2)
Example #48
0
def test_K5():
    x, y = symbols('x, y', real=True)
    assert tan(x + I * y).expand(
        complex=True) == sin(x) * cos(x) / (cos(x)**2 + sinh(y)**2) + I * sinh(
            y) * cosh(y) / (cos(x)**2 + sinh(y)**2)
Example #49
0
def test_is_function_class_equation():
    from sympy.abc import x, a
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x), x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x) - 1, x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x) + sin(x), x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x) + sin(x) - a, x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       sin(x)*tan(x) + sin(x), x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       sin(x)*tan(x + a) + sin(x), x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       sin(x)*tan(x*a) + sin(x), x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       a*tan(x) - 1, x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x)**2 + sin(x) - 1, x) is True
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x**2), x) is False
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x**2) + sin(x), x) is False
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(x)**sin(x), x) is False
    assert _is_function_class_equation(C.TrigonometricFunction,
                                       tan(sin(x)) + sin(x), x) is False
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x), x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x) - 1, x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x) + sinh(x), x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x) + sinh(x) - a, x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       sinh(x)*tanh(x) + sinh(x), x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       sinh(x)*tanh(x + a) + sinh(x), x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       sinh(x)*tanh(x*a) + sinh(x), x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       a*tanh(x) - 1, x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x)**2 + sinh(x) - 1, x) is True
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x**2), x) is False
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x**2) + sinh(x), x) is False
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(x)**sinh(x), x) is False
    assert _is_function_class_equation(C.HyperbolicFunction,
                                       tanh(sinh(x)) + sinh(x), x) is False
Example #50
0
def test_exp_rewrite():
    x = symbols('x')
    assert exp(x).rewrite(sin) == sinh(x) + cosh(x)
    assert exp(x * I).rewrite(cos) == cos(x) + I * sin(x)
    assert exp(1).rewrite(cos) == sinh(1) + cosh(1)
    assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
Example #51
0
    def test_static_cases(self):

        test_cases = [
            # Linear algebra
            1 * x,
            Eq(8 * x, 0),
            Eq(10 * x, 5),
            Eq(x - 4 * x, -21),
            Eq(3 * x, 21),
            Eq(2 + 5, x),
            Eq(3 * (x - 4), x),
            Eq(12, 2 * x),
            Eq(3 * (x - 4) / x, 1),
            Eq(12 / x, 2),
            0.5 * x + 0.1,
            Rational(1, 10) + Rational(1, 2) * x,
            Add(x, 2 * x, -3 * x, evaluate=False),
            # Trigonometry
            sin(x),
            cos(x),
            tan(x),
            csc(x),
            sec(x),
            cot(x),
            1 - sin(x)**2,
            cos(x)**2,
            Eq(sin(x)**2, x),
            Eq(-x, 1 - cos(x)**2 - 2 * x),
            sin(2 * x),
            2 * sin(x) * cos(x),
            Eq(1 - sin(2 * x), 0),
            Eq(sin(x)**2, 2 * sin(x) * cos(x) - cos(x)**2),
            cos(x * y),
            acos(x),
            Pow(cos(x), 1, evaluate=False),
            Pow(cos(x), 0, evaluate=False),
            # Hyperbolic functions
            sinh(x),
            cosh(x),
            tanh(x),
            #csch(x),
            #sech(x),
            #coth(x),
            # Exponential
            E,
            sqrt(E),
            E**x,
            exp(x),
            exp(ln(x), evaluate=False),
            # Imaginary/Complex
            I,
            3 + 8 * I,
            re(x + I * y, evaluate=False),
            im(x + y * I, evaluate=False),
            re(1 - I, evaluate=False),
            im(1 - I, evaluate=False),
            E**(I * theta),
            # Infinity
            oo,
            Add(oo, oo, evaluate=False),
            Add(oo, Mul(3, oo, evaluate=False), evaluate=False),
            -oo,
            Add(-oo, -oo, evaluate=False),
            Mul(-2, oo, evaluate=False),
            Mul(-2, -oo, evaluate=False),
            Mul(-2, -oo, -5, evaluate=False),
            # Calculus
            Limit(x**2, x, theta),
            # Miscellaneous
            Abs(x),
            parse_expr('n!'),
            parse_expr('n!!'),
            factorial2(2 * x + y),
            pi,
            Pow(0, 0, evaluate=False),
        ]

        failed_tests = []
        for expr in test_cases:
            try:
                s_l_expr = latex(expr)
                l_expr = process_sympy(s_l_expr)
                equiv = equivalent(expr, l_expr)
                e = None
            except Exception as e:
                # Parsing failed
                l_expr = None
                equiv = False
            if not equiv:
                print '%s %s' % (s_l_expr, 'PASSED' if equiv else 'FAILED')
                failed_tests.append((s_l_expr, l_expr))
                print 'sympy: %s\nlatex: %s' % (expr, l_expr)
                if e is not None:
                    print e
                print

        if failed_tests:
            print len(failed_tests), 'failed test cases'
        assert len(failed_tests) == 0
Example #52
0
def main():
    Format()
    (g3d, ex, ey, ez) = Ga.build('e*x|y|z')
    A = g3d.mv('A', 'mv')
    print(r'\bm{A} =', A)
    A.Fmt(2, r'\bm{A}')
    A.Fmt(3, r'\bm{A}')

    X = (x, y, z) = symbols('x y z', real=True)
    o3d = Ga('e_x e_y e_z', g=[1, 1, 1], coords=X)
    (ex, ey, ez) = o3d.mv()

    f = o3d.mv('f', 'scalar', f=True)
    A = o3d.mv('A', 'vector', f=True)
    B = o3d.mv('B', 'bivector', f=True)

    print(r'\bm{A} =', A)
    print(r'\bm{B} =', B)

    print('grad*f =', o3d.grad * f)
    print(r'grad|\bm{A} =', o3d.grad | A)
    print(r'grad*\bm{A} =', o3d.grad * A)

    print(r'-I*(grad^\bm{A}) =', -o3d.i * (o3d.grad ^ A))
    print(r'grad*\bm{B} =', o3d.grad * B)
    print(r'grad^\bm{B} =', o3d.grad ^ B)
    print(r'grad|\bm{B} =', o3d.grad | B)

    g4d = Ga('a b c d')

    (a, b, c, d) = g4d.mv()

    print('g_{ij} =', g4d.g)

    print('\\bm{a|(b*c)} =', a | (b * c))
    print('\\bm{a|(b^c)} =', a | (b ^ c))
    print('\\bm{a|(b^c^d)} =', a | (b ^ c ^ d))
    print('\\bm{a|(b^c)+c|(a^b)+b|(c^a)} =',
          (a | (b ^ c)) + (c | (a ^ b)) + (b | (c ^ a)))
    print('\\bm{a*(b^c)-b*(a^c)+c*(a^b)} =',
          a * (b ^ c) - b * (a ^ c) + c * (a ^ b))
    print(
        '\\bm{a*(b^c^d)-b*(a^c^d)+c*(a^b^d)-d*(a^b^c)} =',
        a * (b ^ c ^ d) - b * (a ^ c ^ d) + c * (a ^ b ^ d) - d * (a ^ b ^ c))
    print('\\bm{(a^b)|(c^d)} =', (a ^ b) | (c ^ d))
    print('\\bm{((a^b)|c)|d} =', ((a ^ b) | c) | d)
    print('\\bm{(a^b)\\times (c^d)} =', com(a ^ b, c ^ d))

    g = '1 # #,'+ \
         '# 1 #,'+ \
         '# # 1'

    ng3d = Ga('e1 e2 e3', g=g)
    (e1, e2, e3) = ng3d.mv()

    E = e1 ^ e2 ^ e3
    Esq = (E * E).scalar()
    print('E =', E)
    print('%E^{2} =', Esq)
    Esq_inv = 1 / Esq

    E1 = (e2 ^ e3) * E
    E2 = (-1) * (e1 ^ e3) * E
    E3 = (e1 ^ e2) * E

    print('E1 = (e2^e3)*E =', E1)
    print('E2 =-(e1^e3)*E =', E2)
    print('E3 = (e1^e2)*E =', E3)

    print('E1|e2 =', (E1 | e2).expand())
    print('E1|e3 =', (E1 | e3).expand())
    print('E2|e1 =', (E2 | e1).expand())
    print('E2|e3 =', (E2 | e3).expand())
    print('E3|e1 =', (E3 | e1).expand())
    print('E3|e2 =', (E3 | e2).expand())
    w = ((E1 | e1).expand()).scalar()
    Esq = expand(Esq)
    print('%(E1\\cdot e1)/E^{2} =', simplify(w / Esq))
    w = ((E2 | e2).expand()).scalar()
    print('%(E2\\cdot e2)/E^{2} =', simplify(w / Esq))
    w = ((E3 | e3).expand()).scalar()
    print('%(E3\\cdot e3)/E^{2} =', simplify(w / Esq))

    X = (r, th, phi) = symbols('r theta phi')
    s3d = Ga('e_r e_theta e_phi',
             g=[1, r**2, r**2 * sin(th)**2],
             coords=X,
             norm=True)
    (er, eth, ephi) = s3d.mv()

    f = s3d.mv('f', 'scalar', f=True)
    A = s3d.mv('A', 'vector', f=True)
    B = s3d.mv('B', 'bivector', f=True)

    print('A =', A)
    print('B =', B)

    print('grad*f =', s3d.grad * f)
    print('grad|A =', s3d.grad | A)
    print('-I*(grad^A) =', -s3d.i * (s3d.grad ^ A))
    print('grad^B =', s3d.grad ^ B)

    coords = symbols('t x y z')
    m4d = Ga('gamma*t|x|y|z', g=[1, -1, -1, -1], coords=coords)
    (g0, g1, g2, g3) = m4d.mv()
    I = m4d.i

    B = m4d.mv('B', 'vector', f=True)
    E = m4d.mv('E', 'vector', f=True)
    B.set_coef(1, 0, 0)
    E.set_coef(1, 0, 0)
    B *= g0
    E *= g0
    J = m4d.mv('J', 'vector', f=True)
    F = E + I * B

    print('B = \\bm{B\\gamma_{t}} =', B)
    print('E = \\bm{E\\gamma_{t}} =', E)
    print('F = E+IB =', F)
    print('J =', J)
    gradF = m4d.grad * F
    gradF.Fmt(3, 'grad*F')

    print('grad*F = J')
    (gradF.get_grade(1) - J).Fmt(3, '%\\grade{\\nabla F}_{1} -J = 0')
    (gradF.get_grade(3)).Fmt(3, '%\\grade{\\nabla F}_{3} = 0')

    (alpha, beta, gamma) = symbols('alpha beta gamma')

    (x, t, xp, tp) = symbols("x t x' t'")
    m2d = Ga('gamma*t|x', g=[1, -1])
    (g0, g1) = m2d.mv()

    R = cosh(alpha / 2) + sinh(alpha / 2) * (g0 ^ g1)
    X = t * g0 + x * g1
    Xp = tp * g0 + xp * g1
    print('R =', R)

    print(
        r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}"
    )

    Xpp = R * Xp * R.rev()
    Xpp = Xpp.collect()
    Xpp = Xpp.trigsimp()
    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp)
    Xpp = Xpp.subs({sinh(alpha): gamma * beta, cosh(alpha): gamma})

    print(r'%\f{\sinh}{\alpha} = \gamma\beta')
    print(r'%\f{\cosh}{\alpha} = \gamma')

    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp.collect())

    coords = symbols('t x y z')
    m4d = Ga('gamma*t|x|y|z', g=[1, -1, -1, -1], coords=coords)
    (g0, g1, g2, g3) = m4d.mv()
    I = m4d.i
    (m, e) = symbols('m e')

    psi = m4d.mv('psi', 'spinor', f=True)
    A = m4d.mv('A', 'vector', f=True)
    sig_z = g3 * g0
    print('\\bm{A} =', A)
    print('\\bm{\\psi} =', psi)

    dirac_eq = (m4d.grad * psi) * I * sig_z - e * A * psi - m * psi * g0
    dirac_eq.simplify()

    dirac_eq.Fmt(
        3,
        r'\nabla \bm{\psi} I \sigma_{z}-e\bm{A}\bm{\psi}-m\bm{\psi}\gamma_{t} = 0'
    )

    xpdf()
    return
Example #53
0
def main():
    Format()

    (ex, ey, ez) = MV.setup('e*x|y|z')
    A = MV('A', 'mv')
    print(r'\bm{A} =', A)
    A.Fmt(2, r'\bm{A}')
    A.Fmt(3, r'\bm{A}')

    X = (x, y, z) = symbols('x y z')
    (ex, ey, ez, grad) = MV.setup('e_x e_y e_z', metric='[1,1,1]', coords=X)

    f = MV('f', 'scalar', fct=True)
    A = MV('A', 'vector', fct=True)
    B = MV('B', 'grade2', fct=True)

    print(r'\bm{A} =', A)
    print(r'\bm{B} =', B)

    print('grad*f =', grad * f)
    print(r'grad|\bm{A} =', grad | A)
    print(r'grad*\bm{A} =', grad * A)

    print(r'-I*(grad^\bm{A}) =', -MV.I * (grad ^ A))
    print(r'grad*\bm{B} =', grad * B)
    print(r'grad^\bm{B} =', grad ^ B)
    print(r'grad|\bm{B} =', grad | B)

    (a, b, c, d) = MV.setup('a b c d')

    print('g_{ij} =', MV.metric)

    print('\\bm{a|(b*c)} =', a | (b * c))
    print('\\bm{a|(b^c)} =', a | (b ^ c))
    print('\\bm{a|(b^c^d)} =', a | (b ^ c ^ d))
    print('\\bm{a|(b^c)+c|(a^b)+b|(c^a)} =',
          (a | (b ^ c)) + (c | (a ^ b)) + (b | (c ^ a)))
    print('\\bm{a*(b^c)-b*(a^c)+c*(a^b)} =',
          a * (b ^ c) - b * (a ^ c) + c * (a ^ b))
    print(
        '\\bm{a*(b^c^d)-b*(a^c^d)+c*(a^b^d)-d*(a^b^c)} =',
        a * (b ^ c ^ d) - b * (a ^ c ^ d) + c * (a ^ b ^ d) - d * (a ^ b ^ c))
    print('\\bm{(a^b)|(c^d)} =', (a ^ b) | (c ^ d))
    print('\\bm{((a^b)|c)|d} =', ((a ^ b) | c) | d)
    print('\\bm{(a^b)\\times (c^d)} =', Com(a ^ b, c ^ d))

    metric = '1 # #,'+ \
             '# 1 #,'+ \
             '# # 1'

    (e1, e2, e3) = MV.setup('e1 e2 e3', metric)

    E = e1 ^ e2 ^ e3
    Esq = (E * E).scalar()
    print('E =', E)
    print('%E^{2} =', Esq)
    Esq_inv = 1 / Esq

    E1 = (e2 ^ e3) * E
    E2 = (-1) * (e1 ^ e3) * E
    E3 = (e1 ^ e2) * E

    print('E1 = (e2^e3)*E =', E1)
    print('E2 =-(e1^e3)*E =', E2)
    print('E3 = (e1^e2)*E =', E3)

    print('E1|e2 =', (E1 | e2).expand())
    print('E1|e3 =', (E1 | e3).expand())
    print('E2|e1 =', (E2 | e1).expand())
    print('E2|e3 =', (E2 | e3).expand())
    print('E3|e1 =', (E3 | e1).expand())
    print('E3|e2 =', (E3 | e2).expand())
    w = ((E1 | e1).expand()).scalar()
    Esq = expand(Esq)
    print('%(E1\\cdot e1)/E^{2} =', simplify(w / Esq))
    w = ((E2 | e2).expand()).scalar()
    print('%(E2\\cdot e2)/E^{2} =', simplify(w / Esq))
    w = ((E3 | e3).expand()).scalar()
    print('%(E3\\cdot e3)/E^{2} =', simplify(w / Esq))

    X = (r, th, phi) = symbols('r theta phi')
    curv = [[r * cos(phi) * sin(th), r * sin(phi) * sin(th), r * cos(th)],
            [1, r, r * sin(th)]]
    (er, eth, ephi, grad) = MV.setup('e_r e_theta e_phi',
                                     metric='[1,1,1]',
                                     coords=X,
                                     curv=curv)

    f = MV('f', 'scalar', fct=True)
    A = MV('A', 'vector', fct=True)
    B = MV('B', 'grade2', fct=True)

    print('A =', A)
    print('B =', B)

    print('grad*f =', grad * f)
    print('grad|A =', grad | A)
    print('-I*(grad^A) =', -MV.I * (grad ^ A))
    print('grad^B =', grad ^ B)

    vars = symbols('t x y z')
    (g0, g1, g2, g3, grad) = MV.setup('gamma*t|x|y|z',
                                      metric='[1,-1,-1,-1]',
                                      coords=vars)
    I = MV.I

    B = MV('B', 'vector', fct=True)
    E = MV('E', 'vector', fct=True)
    B.set_coef(1, 0, 0)
    E.set_coef(1, 0, 0)
    B *= g0
    E *= g0
    J = MV('J', 'vector', fct=True)
    F = E + I * B

    print('B = \\bm{B\\gamma_{t}} =', B)
    print('E = \\bm{E\\gamma_{t}} =', E)
    print('F = E+IB =', F)
    print('J =', J)
    gradF = grad * F
    gradF.Fmt(3, 'grad*F')

    print('grad*F = J')
    (gradF.grade(1) - J).Fmt(3, '%\\grade{\\nabla F}_{1} -J = 0')
    (gradF.grade(3)).Fmt(3, '%\\grade{\\nabla F}_{3} = 0')

    (alpha, beta, gamma) = symbols('alpha beta gamma')

    (x, t, xp, tp) = symbols("x t x' t'")
    (g0, g1) = MV.setup('gamma*t|x', metric='[1,-1]')

    R = cosh(alpha / 2) + sinh(alpha / 2) * (g0 ^ g1)
    X = t * g0 + x * g1
    Xp = tp * g0 + xp * g1
    print('R =', R)

    print(
        r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}"
    )

    Xpp = R * Xp * R.rev()
    Xpp = Xpp.collect()
    Xpp = Xpp.subs({
        2 * sinh(alpha / 2) * cosh(alpha / 2): sinh(alpha),
        sinh(alpha / 2)**2 + cosh(alpha / 2)**2: cosh(alpha)
    })
    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp)
    Xpp = Xpp.subs({sinh(alpha): gamma * beta, cosh(alpha): gamma})

    print(r'%\f{\sinh}{\alpha} = \gamma\beta')
    print(r'%\f{\cosh}{\alpha} = \gamma')

    print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp.collect())

    vars = symbols('t x y z')
    (g0, g1, g2, g3, grad) = MV.setup('gamma*t|x|y|z',
                                      metric='[1,-1,-1,-1]',
                                      coords=vars)
    I = MV.I
    (m, e) = symbols('m e')

    psi = MV('psi', 'spinor', fct=True)
    A = MV('A', 'vector', fct=True)
    sig_z = g3 * g0
    print('\\bm{A} =', A)
    print('\\bm{\\psi} =', psi)

    dirac_eq = (grad * psi) * I * sig_z - e * A * psi - m * psi * g0
    dirac_eq.simplify()

    dirac_eq.Fmt(
        3,
        r'\nabla \bm{\psi} I \sigma_{z}-e\bm{A}\bm{\psi}-m\bm{\psi}\gamma_{t} = 0'
    )

    xdvi()
    return