Esempio n. 1
0
 def _expr_big_minus(cls, a, z, n):
     if n.is_even:
         return (1 + z)**a * exp(2 * pi * I * n * a) * cos(
             2 * a * atan(sqrt(z)))
     else:
         return (1 + z)**a * exp(
             2 * pi * I * n * a) * cos(2 * a * atan(sqrt(z)) - 2 * pi * a)
Esempio n. 2
0
def test_functional_diffgeom_ch2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    x, y = symbols('x, y', extended_real=True)
    f = Function('f')

    assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix(
        [sqrt(x0**2 + y0**2), atan2(y0, x0)]))
    assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix(
        [r0 * cos(theta0), r0 * sin(theta0)]))

    assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
        [[cos(theta0), -r0 * sin(theta0)], [sin(theta0), r0 * cos(theta0)]])

    field = f(R2.x, R2.y)
    p1_in_rect = R2_r.point([x0, y0])
    p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
    assert field.rcall(p1_in_rect) == f(x0, y0)
    assert field.rcall(p1_in_polar) == f(x0, y0)

    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])
    assert R2.x(p_r) == x0
    assert R2.x(p_p) == r0 * cos(theta0)
    assert R2.r(p_p) == r0
    assert R2.r(p_r) == sqrt(x0**2 + y0**2)
    assert R2.theta(p_r) == atan2(y0, x0)

    h = R2.x * R2.r**2 + R2.y**3
    assert h.rcall(p_r) == x0 * (x0**2 + y0**2) + y0**3
    assert h.rcall(p_p) == r0**3 * sin(theta0)**3 + r0**3 * cos(theta0)
def test_functional_diffgeom_ch2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    x, y = symbols('x, y', extended_real=True)
    f = Function('f')

    assert (R2_p.point_to_coords(R2_r.point([x0, y0])) ==
            Matrix([sqrt(x0**2 + y0**2), atan2(y0, x0)]))
    assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) ==
            Matrix([r0*cos(theta0), r0*sin(theta0)]))

    assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
        [[cos(theta0), -r0*sin(theta0)], [sin(theta0), r0*cos(theta0)]])

    field = f(R2.x, R2.y)
    p1_in_rect = R2_r.point([x0, y0])
    p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
    assert field.rcall(p1_in_rect) == f(x0, y0)
    assert field.rcall(p1_in_polar) == f(x0, y0)

    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])
    assert R2.x(p_r) == x0
    assert R2.x(p_p) == r0*cos(theta0)
    assert R2.r(p_p) == r0
    assert R2.r(p_r) == sqrt(x0**2 + y0**2)
    assert R2.theta(p_r) == atan2(y0, x0)

    h = R2.x*R2.r**2 + R2.y**3
    assert h.rcall(p_r) == x0*(x0**2 + y0**2) + y0**3
    assert h.rcall(p_p) == r0**3*sin(theta0)**3 + r0**3*cos(theta0)
Esempio n. 4
0
def test_trigintegrate_mixed():
    assert trigintegrate(sin(x)*sec(x), x) == -log(sin(x)**2 - 1)/2
    assert trigintegrate(sin(x)*csc(x), x) == x
    assert trigintegrate(sin(x)*cot(x), x) == sin(x)

    assert trigintegrate(cos(x)*sec(x), x) == x
    assert trigintegrate(cos(x)*csc(x), x) == log(cos(x)**2 - 1)/2
    assert trigintegrate(cos(x)*tan(x), x) == -cos(x)
    assert trigintegrate(cos(x)*cot(x), x) == log(cos(x) - 1)/2 \
        - log(cos(x) + 1)/2 + cos(x)
Esempio n. 5
0
def test_trigintegrate_mixed():
    assert trigintegrate(sin(x) * sec(x), x) == -log(sin(x)**2 - 1) / 2
    assert trigintegrate(sin(x) * csc(x), x) == x
    assert trigintegrate(sin(x) * cot(x), x) == sin(x)

    assert trigintegrate(cos(x) * sec(x), x) == x
    assert trigintegrate(cos(x) * csc(x), x) == log(cos(x)**2 - 1) / 2
    assert trigintegrate(cos(x) * tan(x), x) == -cos(x)
    assert trigintegrate(cos(x)*cot(x), x) == log(cos(x) - 1)/2 \
        - log(cos(x) + 1)/2 + cos(x)
Esempio n. 6
0
def test_ccode_Piecewise_deep():
    p = ccode(2*Piecewise((x, x < 1), (x + 1, x < 2), (x**2, True)))
    assert p == (
        "2*((x < 1) ? (\n"
        "   x\n"
        ")\n"
        ": ((x < 2) ? (\n"
        "   x + 1\n"
        ")\n"
        ": (\n"
        "   pow(x, 2)\n"
        ")))")
    expr = x*y*z + x**2 + y**2 + Piecewise((0, x < 0.5), (1, True)) + cos(z) - 1
    assert ccode(expr) == (
        "pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n"
        "   0\n"
        ")\n"
        ": (\n"
        "   1\n"
        ")) + cos(z) - 1")
    assert ccode(expr, assign_to='c') == (
        "c = pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n"
        "   0\n"
        ")\n"
        ": (\n"
        "   1\n"
        ")) + cos(z) - 1;")
Esempio n. 7
0
    def build_ideal(x, terms):
        """
        Build generators for our ideal. Terms is an iterable with elements of
        the form (fn, coeff), indicating that we have a generator fn(coeff*x).

        If any of the terms is trigonometric, sin(x) and cos(x) are guaranteed
        to appear in terms. Similarly for hyperbolic functions. For tan(n*x),
        sin(n*x) and cos(n*x) are guaranteed.
        """
        gens = []
        I = []
        y = Dummy('y')
        for fn, coeff in terms:
            for c, s, t, rel in ([cos, sin, tan,
                                  cos(x)**2 + sin(x)**2 - 1], [
                                      cosh, sinh, tanh,
                                      cosh(x)**2 - sinh(x)**2 - 1
                                  ]):
                if coeff == 1 and fn in [c, s]:
                    I.append(rel)
                elif fn == t:
                    I.append(t(coeff * x) * c(coeff * x) - s(coeff * x))
                elif fn in [c, s]:
                    cn = fn(coeff * y).expand(trig=True).subs(y, x)
                    I.append(fn(coeff * x) - cn)
        return list(set(I))
def test_functional_diffgeom_ch3():
    x0, y0 = symbols('x0, y0', extended_real=True)
    x, y, t = symbols('x, y, t', extended_real=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])

    s_field = f(R2.x, R2.y)
    v_field = b1(R2.x)*R2.e_x + b2(R2.y)*R2.e_y
    assert v_field.rcall(s_field).rcall(p_r).doit() == b1(
        x0)*Derivative(f(x0, y0), x0) + b2(y0)*Derivative(f(x0, y0), y0)

    assert R2.e_x(R2.r**2).rcall(p_r) == 2*x0
    v = R2.e_x + 2*R2.e_y
    s = R2.r**2 + 3*R2.x
    assert v.rcall(s).rcall(p_r).doit() == 2*x0 + 4*y0 + 3

    circ = -R2.y*R2.e_x + R2.x*R2.e_y
    series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
    series_x, series_y = zip(*series)
    assert all(term == cos(t).taylor_term(i, t)
               for i, term in enumerate(series_x))
    assert all(term == sin(t).taylor_term(i, t)
               for i, term in enumerate(series_y))
Esempio n. 9
0
def test_ccode_Piecewise_deep():
    p = ccode(2 * Piecewise((x, x < 1), (x + 1, x < 2), (x**2, True)))
    assert p == ("2*((x < 1) ? (\n"
                 "   x\n"
                 ")\n"
                 ": ((x < 2) ? (\n"
                 "   x + 1\n"
                 ")\n"
                 ": (\n"
                 "   pow(x, 2)\n"
                 ")))")
    expr = x * y * z + x**2 + y**2 + Piecewise((0, x < 0.5),
                                               (1, True)) + cos(z) - 1
    assert ccode(expr) == ("pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n"
                           "   0\n"
                           ")\n"
                           ": (\n"
                           "   1\n"
                           ")) + cos(z) - 1")
    assert ccode(expr, assign_to='c') == (
        "c = pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n"
        "   0\n"
        ")\n"
        ": (\n"
        "   1\n"
        ")) + cos(z) - 1;")
Esempio n. 10
0
def test_functional_diffgeom_ch3():
    x0, y0 = symbols('x0, y0', extended_real=True)
    x, y, t = symbols('x, y, t', extended_real=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])

    s_field = f(R2.x, R2.y)
    v_field = b1(R2.x) * R2.e_x + b2(R2.y) * R2.e_y
    assert v_field.rcall(s_field).rcall(p_r).doit() == b1(x0) * Derivative(
        f(x0, y0), x0) + b2(y0) * Derivative(f(x0, y0), y0)

    assert R2.e_x(R2.r**2).rcall(p_r) == 2 * x0
    v = R2.e_x + 2 * R2.e_y
    s = R2.r**2 + 3 * R2.x
    assert v.rcall(s).rcall(p_r).doit() == 2 * x0 + 4 * y0 + 3

    circ = -R2.y * R2.e_x + R2.x * R2.e_y
    series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
    series_x, series_y = zip(*series)
    assert all(term == cos(t).taylor_term(i, t)
               for i, term in enumerate(series_x))
    assert all(term == sin(t).taylor_term(i, t)
               for i, term in enumerate(series_y))
Esempio n. 11
0
def test_coords():
    r, theta = symbols('r, theta')
    m = Manifold('M', 2)
    patch = Patch('P', m)
    rect = CoordSystem('rect', patch)
    polar = CoordSystem('polar', patch)
    polar.connect_to(rect, [r, theta], [r*cos(theta), r*sin(theta)])
    polar.coord_tuple_transform_to(rect, [0, 2]) == Matrix([[0], [0]])
Esempio n. 12
0
def test_coords():
    r, theta = symbols('r, theta')
    m = Manifold('M', 2)
    patch = Patch('P', m)
    rect = CoordSystem('rect', patch)
    polar = CoordSystem('polar', patch)
    polar.connect_to(rect, [r, theta], [r * cos(theta), r * sin(theta)])
    polar.coord_tuple_transform_to(rect, [0, 2]) == Matrix([[0], [0]])
Esempio n. 13
0
def _sin_pow_integrate(n, x):
    if n > 0:
        if n == 1:
            # Recursion break
            return -cos(x)

        #  n > 0
        #   /                                                 /
        #  |                                                 |
        #  |    n           -1               n-1     n - 1   |     n-2
        #  | sin (x) dx =  ______ cos (x) sin (x) + _______  |  sin (x) dx
        #  |                                                 |
        #  |                 n                         n     |
        # /                                                 /
        #

        return (Rational(-1, n) * cos(x) * sin(x)**(n - 1) +
                Rational(n - 1, n) * _sin_pow_integrate(n - 2, x))

    if n < 0:
        if n == -1:
            # Make sure this does not come back here again.
            # Recursion breaks here or at n==0.
            return trigintegrate(1 / sin(x), x)

        #  n < 0
        #   /                                                 /
        #  |                                                 |
        #  |    n            1               n+1     n + 2   |     n+2
        #  | sin (x) dx = _______ cos (x) sin (x) + _______  |  sin (x) dx
        #  |                                                 |
        #  |               n + 1                     n + 1   |
        # /                                                 /
        #

        return (Rational(1, n + 1) * cos(x) * sin(x)**(n + 1) +
                Rational(n + 2, n + 1) * _sin_pow_integrate(n + 2, x))

    else:
        # n == 0
        # Recursion break.
        return x
Esempio n. 14
0
def test_ccode_functions():
    assert ccode(sin(x)**cos(x)) == "pow(sin(x), cos(x))"

    assert ccode(elliptic_e(x)) == ("// Not supported in C:\n"
                                    "// elliptic_e\nelliptic_e(x)")

    n = symbols('n', integer=True)
    assert ccode(Abs(n)) == '// Not supported in C:\n// Abs\nAbs(n)'
    assert ccode(Abs(x)) == 'fabs(x)'

    pytest.raises(TypeError, lambda: ccode(sin(x), assign_to=1))
Esempio n. 15
0
def test_ccode_sign():

    expr = sign(x) * y
    assert ccode(expr) == 'y*(((x) > 0) - ((x) < 0))'
    assert ccode(expr, 'z') == 'z = y*(((x) > 0) - ((x) < 0));'

    assert ccode(sign(2 * x + x**2) * x + x**2) == \
        'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))'

    expr = sign(cos(x))
    assert ccode(expr) == '(((cos(x)) > 0) - ((cos(x)) < 0))'
Esempio n. 16
0
def test_Function():
    assert mcode(f(x, y, z)) == "f[x, y, z]"
    assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]"
    assert mcode(sign(x)) == "Sign[x]"

    assert mcode(atanh(x), user_functions={"atanh": "ArcTanh"}) == "ArcTanh[x]"

    assert (mcode(meijerg(((1, 1), (3, 4)), ((1, ), ()),
                          x)) == "MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]")
    assert (mcode(hyper((1, 2, 3), (3, 4),
                        x)) == "HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]")

    assert mcode(Min(x, y)) == "Min[x, y]"
    assert mcode(Max(x, y)) == "Max[x, y]"
    assert mcode(Max(x, 2)) == "Max[2, x]"  # issue sympy/sympy#15344

    assert mcode(binomial(x, y)) == "Binomial[x, y]"

    assert mcode(log(x)) == "Log[x]"
    assert mcode(tan(x)) == "Tan[x]"
    assert mcode(cot(x)) == "Cot[x]"
    assert mcode(asin(x)) == "ArcSin[x]"
    assert mcode(acos(x)) == "ArcCos[x]"
    assert mcode(atan(x)) == "ArcTan[x]"
    assert mcode(sinh(x)) == "Sinh[x]"
    assert mcode(cosh(x)) == "Cosh[x]"
    assert mcode(tanh(x)) == "Tanh[x]"
    assert mcode(coth(x)) == "Coth[x]"
    assert mcode(sech(x)) == "Sech[x]"
    assert mcode(csch(x)) == "Csch[x]"
    assert mcode(erfc(x)) == "Erfc[x]"
    assert mcode(conjugate(x)) == "Conjugate[x]"
    assert mcode(re(x)) == "Re[x]"
    assert mcode(im(x)) == "Im[x]"
    assert mcode(polygamma(x, y)) == "PolyGamma[x, y]"

    class myfunc1(Function):
        @classmethod
        def eval(cls, x):
            pass

    class myfunc2(Function):
        @classmethod
        def eval(cls, x, y):
            pass

    pytest.raises(
        ValueError,
        lambda: mcode(myfunc1(x), user_functions={"myfunc1": ["Myfunc1"]}))
    assert mcode(myfunc1(x), user_functions={"myfunc1":
                                             "Myfunc1"}) == "Myfunc1[x]"
    assert mcode(myfunc2(x, y),
                 user_functions={"myfunc2": [(lambda *x: False, "Myfunc2")]
                                 }) == "myfunc2[x, y]"
Esempio n. 17
0
def test_ccode_sign():

    expr = sign(x) * y
    assert ccode(expr) == 'y*(((x) > 0) - ((x) < 0))'
    assert ccode(expr, 'z') == 'z = y*(((x) > 0) - ((x) < 0));'

    assert ccode(sign(2 * x + x**2) * x + x**2) == \
        'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))'

    expr = sign(cos(x))
    assert ccode(expr) == '(((cos(x)) > 0) - ((cos(x)) < 0))'
Esempio n. 18
0
def test_ccode_functions():
    assert ccode(sin(x) ** cos(x)) == "pow(sin(x), cos(x))"

    assert ccode(elliptic_e(x)) == ("// Not supported in C:\n"
                                    "// elliptic_e\nelliptic_e(x)")

    n = symbols('n', integer=True)
    assert ccode(Abs(n)) == '// Not supported in C:\n// Abs\nAbs(n)'
    assert ccode(Abs(x)) == 'fabs(x)'

    pytest.raises(TypeError, lambda: ccode(sin(x), assign_to=1))
def test_functional_diffgeom_ch4():
    x0, y0, theta0 = symbols('x0, y0, theta0', extended_real=True)
    x, y, r, theta = symbols('x, y, r, theta', extended_real=True)
    r0 = symbols('r0', positive=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])

    f_field = b1(R2.x, R2.y)*R2.dx + b2(R2.x, R2.y)*R2.dy
    assert f_field.rcall(R2.e_x).rcall(p_r) == b1(x0, y0)
    assert f_field.rcall(R2.e_y).rcall(p_r) == b2(x0, y0)

    s_field_r = f(R2.x, R2.y)
    df = Differential(s_field_r)
    assert df(R2.e_x).rcall(p_r).doit() == Derivative(f(x0, y0), x0)
    assert df(R2.e_y).rcall(p_r).doit() == Derivative(f(x0, y0), y0)

    s_field_p = f(R2.r, R2.theta)
    df = Differential(s_field_p)
    assert trigsimp(df(R2.e_x).rcall(p_p).doit()) == (
        cos(theta0)*Derivative(f(r0, theta0), r0) -
        sin(theta0)*Derivative(f(r0, theta0), theta0)/r0)
    assert trigsimp(df(R2.e_y).rcall(p_p).doit()) == (
        sin(theta0)*Derivative(f(r0, theta0), r0) +
        cos(theta0)*Derivative(f(r0, theta0), theta0)/r0)

    assert R2.dx(R2.e_x).rcall(p_r) == 1
    assert R2.dx(R2.e_x) == 1
    assert R2.dx(R2.e_y).rcall(p_r) == 0
    assert R2.dx(R2.e_y) == 0

    circ = -R2.y*R2.e_x + R2.x*R2.e_y
    assert R2.dx(circ).rcall(p_r).doit() == -y0
    assert R2.dy(circ).rcall(p_r) == x0
    assert R2.dr(circ).rcall(p_r) == 0
    assert simplify(R2.dtheta(circ).rcall(p_r)) == 1

    assert (circ - R2.e_theta).rcall(s_field_r).rcall(p_r) == 0
Esempio n. 20
0
def _cos_pow_integrate(n, x):
    if n > 0:
        if n == 1:
            # Recursion break.
            return sin(x)

        #  n > 0
        #   /                                                 /
        #  |                                                 |
        #  |    n            1               n-1     n - 1   |     n-2
        #  | sin (x) dx =  ______ sin (x) cos (x) + _______  |  cos (x) dx
        #  |                                                 |
        #  |                 n                         n     |
        # /                                                 /
        #

        return (Rational(1, n) * sin(x) * cos(x)**(n - 1) +
                Rational(n - 1, n) * _cos_pow_integrate(n - 2, x))

    if n < 0:
        if n == -1:
            # Recursion break
            return trigintegrate(1 / cos(x), x)

        #  n < 0
        #   /                                                 /
        #  |                                                 |
        #  |    n            -1              n+1     n + 2   |     n+2
        #  | cos (x) dx = _______ sin (x) cos (x) + _______  |  cos (x) dx
        #  |                                                 |
        #  |               n + 1                     n + 1   |
        # /                                                 /
        #

        return (Rational(-1, n + 1) * sin(x) * cos(x)**(n + 1) +
                Rational(n + 2, n + 1) * _cos_pow_integrate(n + 2, x))
    else:
        # n == 0
        # Recursion Break.
        return x
Esempio n. 21
0
def test_functional_diffgeom_ch4():
    x0, y0, theta0 = symbols('x0, y0, theta0', extended_real=True)
    x, y, r, theta = symbols('x, y, r, theta', extended_real=True)
    r0 = symbols('r0', positive=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])

    f_field = b1(R2.x, R2.y) * R2.dx + b2(R2.x, R2.y) * R2.dy
    assert f_field.rcall(R2.e_x).rcall(p_r) == b1(x0, y0)
    assert f_field.rcall(R2.e_y).rcall(p_r) == b2(x0, y0)

    s_field_r = f(R2.x, R2.y)
    df = Differential(s_field_r)
    assert df(R2.e_x).rcall(p_r).doit() == Derivative(f(x0, y0), x0)
    assert df(R2.e_y).rcall(p_r).doit() == Derivative(f(x0, y0), y0)

    s_field_p = f(R2.r, R2.theta)
    df = Differential(s_field_p)
    assert trigsimp(df(R2.e_x).rcall(p_p).doit()) == (
        cos(theta0) * Derivative(f(r0, theta0), r0) -
        sin(theta0) * Derivative(f(r0, theta0), theta0) / r0)
    assert trigsimp(df(R2.e_y).rcall(p_p).doit()) == (
        sin(theta0) * Derivative(f(r0, theta0), r0) +
        cos(theta0) * Derivative(f(r0, theta0), theta0) / r0)

    assert R2.dx(R2.e_x).rcall(p_r) == 1
    assert R2.dx(R2.e_x) == 1
    assert R2.dx(R2.e_y).rcall(p_r) == 0
    assert R2.dx(R2.e_y) == 0

    circ = -R2.y * R2.e_x + R2.x * R2.e_y
    assert R2.dx(circ).rcall(p_r).doit() == -y0
    assert R2.dy(circ).rcall(p_r) == x0
    assert R2.dr(circ).rcall(p_r) == 0
    assert simplify(R2.dtheta(circ).rcall(p_r)) == 1

    assert (circ - R2.e_theta).rcall(s_field_r).rcall(p_r) == 0
Esempio n. 22
0
def test_fibonacci():
    assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3]
    assert fibonacci(100) == 354224848179261915075
    assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7]
    assert lucas(100) == 792070839848372253127

    assert fibonacci(1, x) == 1
    assert fibonacci(2, x) == x
    assert fibonacci(3, x) == x**2 + 1
    assert fibonacci(4, x) == x**3 + 2 * x

    assert fibonacci(x).rewrite(sqrt) == (
        S.GoldenRatio**x - cos(S.Pi * x) / S.GoldenRatio**x) / sqrt(5)
    assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt)
Esempio n. 23
0
def test_Function():
    assert mcode(f(x, y, z)) == "f[x, y, z]"
    assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]"
    assert mcode(sign(x)) == "Sign[x]"

    assert mcode(atanh(x), user_functions={"atanh": "ArcTanh"}) == "ArcTanh[x]"

    assert (mcode(meijerg(((1, 1), (3, 4)), ((1, ), ()),
                          x)) == "MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]")
    assert (mcode(hyper((1, 2, 3), (3, 4),
                        x)) == "HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]")

    assert mcode(Min(x, y)) == "Min[x, y]"
    assert mcode(Max(x, y)) == "Max[x, y]"
Esempio n. 24
0
def test_Max():
    from diofant.abc import x, y, z
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    nn = Symbol('nn', nonnegative=True)
    nn_ = Symbol('nn_', nonnegative=True)
    p = Symbol('p', positive=True)
    p_ = Symbol('p_', positive=True)
    np = Symbol('np', nonpositive=True)
    np_ = Symbol('np_', nonpositive=True)
    r = Symbol('r', extended_real=True)

    assert Max(5, 4) == 5

    # lists

    pytest.raises(ValueError, lambda: Max())
    assert Max(x, y) == Max(y, x)
    assert Max(x, y, z) == Max(z, y, x)
    assert Max(x, Max(y, z)) == Max(z, y, x)
    assert Max(x, Min(y, oo)) == Max(x, y)
    assert Max(n, -oo, n_, p, 2) == Max(p, 2)
    assert Max(n, -oo, n_, p) == p
    assert Max(2, x, p, n, -oo, S.NegativeInfinity, n_, p, 2) == Max(2, x, p)
    assert Max(0, x, 1, y) == Max(1, x, y)
    assert Max(r, r + 1, r - 1) == 1 + r
    assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000)
    assert Max(cos(x), sin(x)) == Max(sin(x), cos(x))
    assert Max(cos(x), sin(x)).subs(x, 1) == sin(1)
    assert Max(cos(x), sin(x)).subs(x, Rational(1, 2)) == cos(Rational(1, 2))
    pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs(x, I))
    pytest.raises(ValueError, lambda: Max(I))
    pytest.raises(ValueError, lambda: Max(I, x))
    pytest.raises(ValueError, lambda: Max(S.ComplexInfinity, 1))
    # interesting:
    # Max(n, -oo, n_,  p, 2) == Max(p, 2)
    # True
    # Max(n, -oo, n_,  p, 1000) == Max(p, 1000)
    # False

    assert Max(1, x).diff(x) == Heaviside(x - 1)
    assert Max(x, 1).diff(x) == Heaviside(x - 1)
    assert Max(x**2, 1 + x, 1).diff(x) == \
        2*x*Heaviside(x**2 - Max(1, x + 1)) \
        + Heaviside(x - Max(1, x**2) + 1)

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Max(a, b) should be real
    assert Max(a, b).is_extended_real

    # issue sympy/sympy#7233
    e = Max(0, x)
    assert e.evalf == e.n
    assert e.n().args == (0, x)
Esempio n. 25
0
def test_fibonacci():
    assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3]
    assert fibonacci(100) == 354224848179261915075
    assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7]
    assert lucas(100) == 792070839848372253127
    assert lucas(x) == lucas(x, evaluate=False)

    assert fibonacci(1, x) == 1
    assert fibonacci(2, x) == x
    assert fibonacci(3, x) == x**2 + 1
    assert fibonacci(4, x) == x**3 + 2*x

    assert fibonacci(x).rewrite(sqrt) == (GoldenRatio**x - cos(pi*x)/GoldenRatio**x)/sqrt(5)
    assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt)

    pytest.raises(ValueError, lambda: fibonacci(-2, x))

    n = Symbol('n', integer=True)
    assert isinstance(fibonacci(n, x).rewrite(sqrt), fibonacci)
Esempio n. 26
0
def test_fibonacci():
    assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3]
    assert fibonacci(100) == 354224848179261915075
    assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7]
    assert lucas(100) == 792070839848372253127

    assert fibonacci(1, x) == 1
    assert fibonacci(2, x) == x
    assert fibonacci(3, x) == x**2 + 1
    assert fibonacci(4, x) == x**3 + 2 * x

    assert fibonacci(x).rewrite(sqrt) == (
        S.GoldenRatio**x - cos(S.Pi * x) / S.GoldenRatio**x) / sqrt(5)
    assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt)

    pytest.raises(ValueError, lambda: fibonacci(-2, x))

    n = Symbol('n', integer=True)
    assert fibonacci(n, x).rewrite(sqrt).func is fibonacci
Esempio n. 27
0
def test_Max():
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    p = Symbol('p', positive=True)
    r = Symbol('r', extended_real=True)

    assert Max(5, 4) == 5

    # lists

    pytest.raises(ValueError, lambda: Max())
    assert Max(x, y) == Max(y, x)
    assert Max(x, y, z) == Max(z, y, x)
    assert Max(x, Max(y, z)) == Max(z, y, x)
    assert Max(x, Min(y, oo)) == Max(x, y)
    assert Max(n, -oo, n_, p, 2) == Max(p, 2)
    assert Max(n, -oo, n_, p) == p
    assert Max(2, x, p, n, -oo, -oo, n_, p, 2) == Max(2, x, p)
    assert Max(0, x, 1, y) == Max(1, x, y)
    assert Max(r, r + 1, r - 1) == 1 + r
    assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000)
    assert Max(cos(x), sin(x)) == Max(sin(x), cos(x))
    assert Max(cos(x), sin(x)).subs({x: 1}) == sin(1)
    assert Max(cos(x), sin(x)).subs({x: Rational(1, 2)}) == cos(Rational(1, 2))
    pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs({x: I}))
    pytest.raises(ValueError, lambda: Max(I))
    pytest.raises(ValueError, lambda: Max(I, x))
    pytest.raises(ValueError, lambda: Max(zoo, 1))
    # interesting:
    # Max(n, -oo, n_,  p, 2) == Max(p, 2)
    # True
    # Max(n, -oo, n_,  p, 1000) == Max(p, 1000)
    # False

    assert Max(1, x).diff(x) == Heaviside(x - 1)
    assert Max(x, 1).diff(x) == Heaviside(x - 1)
    assert Max(x**2, 1 + x, 1).diff(x) == \
        2*x*Heaviside(x**2 - Max(1, x + 1)) \
        + Heaviside(x - Max(1, x**2) + 1)

    pytest.raises(ArgumentIndexError, lambda: Max(1, x).fdiff(3))

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Max(a, b) should be real
    assert Max(a, b).is_extended_real

    # issue sympy/sympy#7233
    e = Max(0, x)
    assert e.evalf == e.n
    assert e.evalf(strict=False).args == (0, x)
Esempio n. 28
0
def test_Max():
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    p = Symbol('p', positive=True)
    r = Symbol('r', extended_real=True)

    assert Max(5, 4) == 5

    # lists

    pytest.raises(ValueError, lambda: Max())
    assert Max(x, y) == Max(y, x)
    assert Max(x, y, z) == Max(z, y, x)
    assert Max(x, Max(y, z)) == Max(z, y, x)
    assert Max(x, Min(y, oo)) == Max(x, y)
    assert Max(n, -oo, n_, p, 2) == Max(p, 2)
    assert Max(n, -oo, n_, p) == p
    assert Max(2, x, p, n, -oo, -oo, n_, p, 2) == Max(2, x, p)
    assert Max(0, x, 1, y) == Max(1, x, y)
    assert Max(r, r + 1, r - 1) == 1 + r
    assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000)
    assert Max(cos(x), sin(x)) == Max(sin(x), cos(x))
    assert Max(cos(x), sin(x)).subs({x: 1}) == sin(1)
    assert Max(cos(x), sin(x)).subs({x: Rational(1, 2)}) == cos(Rational(1, 2))
    pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs({x: I}))
    pytest.raises(ValueError, lambda: Max(I))
    pytest.raises(ValueError, lambda: Max(I, x))
    pytest.raises(ValueError, lambda: Max(zoo, 1))
    # interesting:
    # Max(n, -oo, n_,  p, 2) == Max(p, 2)
    # True
    # Max(n, -oo, n_,  p, 1000) == Max(p, 1000)
    # False

    assert Max(1, x).diff(x) == Heaviside(x - 1)
    assert Max(x, 1).diff(x) == Heaviside(x - 1)
    assert Max(x**2, 1 + x, 1).diff(x) == \
        2*x*Heaviside(x**2 - Max(1, x + 1)) \
        + Heaviside(x - Max(1, x**2) + 1)

    pytest.raises(ArgumentIndexError, lambda: Max(1, x).fdiff(3))

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Max(a, b) should be real
    assert Max(a, b).is_extended_real

    # issue sympy/sympy#7233
    e = Max(0, x)
    assert e.evalf == e.n
    assert e.evalf(strict=False).args == (0, x)
Esempio n. 29
0
def rotate(th):
    """Return the matrix to rotate a 2-D point about the origin by ``angle``.

    The angle is measured in radians. To Point a point about a point other
    then the origin, translate the Point, do the rotation, and
    translate it back:

    >>> from diofant.geometry.entity import rotate, translate
    >>> from diofant import Point, pi
    >>> rot_about_11 = translate(-1, -1)*rotate(pi/2)*translate(1, 1)
    >>> Point(1, 1).transform(rot_about_11)
    Point2D(1, 1)
    >>> Point(0, 0).transform(rot_about_11)
    Point2D(2, 0)
    """
    s = sin(th)
    rv = eye(3) * cos(th)
    rv[0, 1] = s
    rv[1, 0] = -s
    rv[2, 2] = 1
    return rv
Esempio n. 30
0
def test_Matrix_printing():
    # Test returning a Matrix
    mat = Matrix([x*y, Piecewise((2 + x, y > 0), (y, True)), sin(z)])
    A = MatrixSymbol('A', 3, 1)
    assert ccode(mat, A) == (
        "A[0] = x*y;\n"
        "if (y > 0) {\n"
        "   A[1] = x + 2;\n"
        "}\n"
        "else {\n"
        "   A[1] = y;\n"
        "}\n"
        "A[2] = sin(z);")
    # Test using MatrixElements in expressions
    expr = Piecewise((2*A[2, 0], x > 0), (A[2, 0], True)) + sin(A[1, 0]) + A[0, 0]
    assert ccode(expr) == (
        "((x > 0) ? (\n"
        "   2*A[2]\n"
        ")\n"
        ": (\n"
        "   A[2]\n"
        ")) + sin(A[1]) + A[0]")
    # Test using MatrixElements in a Matrix
    q = MatrixSymbol('q', 5, 1)
    M = MatrixSymbol('M', 3, 3)
    m = Matrix([[sin(q[1, 0]), 0, cos(q[2, 0])],
                [q[1, 0] + q[2, 0], q[3, 0], 5],
                [2*q[4, 0]/q[1, 0], sqrt(q[0, 0]) + 4, 0]])
    assert ccode(m, M) == (
        "M[0] = sin(q[1]);\n"
        "M[1] = 0;\n"
        "M[2] = cos(q[2]);\n"
        "M[3] = q[1] + q[2];\n"
        "M[4] = q[3];\n"
        "M[5] = 5;\n"
        "M[6] = 2*q[4]*1.0/q[1];\n"
        "M[7] = 4 + sqrt(q[0]);\n"
        "M[8] = 0;")
Esempio n. 31
0
def test_Matrix_printing():
    # Test returning a Matrix
    mat = Matrix([x*y, Piecewise((2 + x, y > 0), (y, True)), sin(z)])
    A = MatrixSymbol('A', 3, 1)
    assert jscode(mat, A) == (
        "A[0] = x*y;\n"
        "if (y > 0) {\n"
        "   A[1] = x + 2;\n"
        "}\n"
        "else {\n"
        "   A[1] = y;\n"
        "}\n"
        "A[2] = Math.sin(z);")
    # Test using MatrixElements in expressions
    expr = Piecewise((2*A[2, 0], x > 0), (A[2, 0], True)) + sin(A[1, 0]) + A[0, 0]
    assert jscode(expr) == (
        "((x > 0) ? (\n"
        "   2*A[2]\n"
        ")\n"
        ": (\n"
        "   A[2]\n"
        ")) + Math.sin(A[1]) + A[0]")
    # Test using MatrixElements in a Matrix
    q = MatrixSymbol('q', 5, 1)
    M = MatrixSymbol('M', 3, 3)
    m = Matrix([[sin(q[1, 0]), 0, cos(q[2, 0])],
                [q[1, 0] + q[2, 0], q[3, 0], 5],
                [2*q[4, 0]/q[1, 0], sqrt(q[0, 0]) + 4, 0]])
    assert jscode(m, M) == (
        "M[0] = Math.sin(q[1]);\n"
        "M[1] = 0;\n"
        "M[2] = Math.cos(q[2]);\n"
        "M[3] = q[1] + q[2];\n"
        "M[4] = q[3];\n"
        "M[5] = 5;\n"
        "M[6] = 2*q[4]*1/q[1];\n"
        "M[7] = 4 + Math.sqrt(q[0]);\n"
        "M[8] = 0;")
Esempio n. 32
0
def test_Min():
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    nn = Symbol('nn', nonnegative=True)
    nn_ = Symbol('nn_', nonnegative=True)
    p = Symbol('p', positive=True)
    p_ = Symbol('p_', positive=True)
    np = Symbol('np', nonpositive=True)
    np_ = Symbol('np_', nonpositive=True)

    assert Min(5, 4) == 4
    assert Min(-oo, -oo) == -oo
    assert Min(-oo, n) == -oo
    assert Min(n, -oo) == -oo
    assert Min(-oo, np) == -oo
    assert Min(np, -oo) == -oo
    assert Min(-oo, 0) == -oo
    assert Min(0, -oo) == -oo
    assert Min(-oo, nn) == -oo
    assert Min(nn, -oo) == -oo
    assert Min(-oo, p) == -oo
    assert Min(p, -oo) == -oo
    assert Min(-oo, oo) == -oo
    assert Min(oo, -oo) == -oo
    assert Min(n, n) == n
    assert Min(n, np) == Min(n, np)
    assert Min(np, n) == Min(np, n)
    assert Min(n, 0) == n
    assert Min(0, n) == n
    assert Min(n, nn) == n
    assert Min(nn, n) == n
    assert Min(n, p) == n
    assert Min(p, n) == n
    assert Min(n, oo) == n
    assert Min(oo, n) == n
    assert Min(np, np) == np
    assert Min(np, 0) == np
    assert Min(0, np) == np
    assert Min(np, nn) == np
    assert Min(nn, np) == np
    assert Min(np, p) == np
    assert Min(p, np) == np
    assert Min(np, oo) == np
    assert Min(oo, np) == np
    assert Min(0, 0) == 0
    assert Min(0, nn) == 0
    assert Min(nn, 0) == 0
    assert Min(0, p) == 0
    assert Min(p, 0) == 0
    assert Min(0, oo) == 0
    assert Min(oo, 0) == 0
    assert Min(nn, nn) == nn
    assert Min(nn, p) == Min(nn, p)
    assert Min(p, nn) == Min(p, nn)
    assert Min(nn, oo) == nn
    assert Min(oo, nn) == nn
    assert Min(p, p) == p
    assert Min(p, oo) == p
    assert Min(oo, p) == p
    assert Min(oo, oo) == oo

    assert isinstance(Min(n, n_), Min)
    assert isinstance(Min(nn, nn_), Min)
    assert isinstance(Min(np, np_), Min)
    assert isinstance(Min(p, p_), Min)

    # lists
    pytest.raises(ValueError, lambda: Min())
    assert Min(x, y) == Min(y, x)
    assert Min(x, y, z) == Min(z, y, x)
    assert Min(x, Min(y, z)) == Min(z, y, x)
    assert Min(x, Max(y, -oo)) == Min(x, y)
    assert Min(p, oo, n, p, p, p_) == n
    assert Min(p_, n_, p) == n_
    assert Min(n, oo, -7, p, p, 2) == Min(n, -7)
    assert Min(2, x, p, n, oo, n_, p, 2, -2, -2) == Min(-2, x, n, n_)
    assert Min(0, x, 1, y) == Min(0, x, y)
    assert Min(1000, 100, -100, x, p, n) == Min(n, x, -100)
    assert Min(cos(x), sin(x)) == Min(cos(x), sin(x))
    assert Min(cos(x), sin(x)).subs({x: 1}) == cos(1)
    assert Min(cos(x), sin(x)).subs({x: Rational(1, 2)}) == sin(Rational(1, 2))
    pytest.raises(ValueError, lambda: Min(cos(x), sin(x)).subs({x: I}))
    pytest.raises(ValueError, lambda: Min(I))
    pytest.raises(ValueError, lambda: Min(I, x))
    pytest.raises(ValueError, lambda: Min(zoo, x))

    assert Min(1, x).diff(x) == Heaviside(1 - x)
    assert Min(x, 1).diff(x) == Heaviside(1 - x)
    assert Min(0, -x, 1 - 2*x).diff(x) == -Heaviside(x + Min(0, -2*x + 1)) \
        - 2*Heaviside(2*x + Min(0, -x) - 1)

    pytest.raises(ArgumentIndexError, lambda: Min(1, x).fdiff(3))

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Min(a, b) should be real
    assert Min(a, b).is_extended_real

    # issue sympy/sympy#7619
    f = Function('f')
    assert Min(1, 2*Min(f(1), 2))  # doesn't fail

    # issue sympy/sympy#7233
    e = Min(0, x)
    assert e.evalf == e.n
    assert e.evalf(strict=False).args == (0, x)
Esempio n. 33
0
def test_Function():
    assert mcode(sin(x)**cos(x)) == "sin(x).^cos(x)"
    assert mcode(abs(x)) == "abs(x)"
    assert mcode(ceiling(x)) == "ceil(x)"
Esempio n. 34
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe/qe)
    Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + Rational(13944145, 4720968))

    Heeo = harmonic(ne + pe/qo)
    Aeeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13)
            + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13)
            - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13)
            + Rational(2422020029, 702257080))

    Heoe = harmonic(ne + po/qe)
    Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
            + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
            + Rational(11818877030, 4286604231) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8)))

    Heoo = harmonic(ne + po/qo)
    Aeoo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13)
            + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13)
            - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2
            - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(11669332571, 3628714320))

    Hoee = harmonic(no + pe/qe)
    Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + Rational(779405, 277704))

    Hoeo = harmonic(no + pe/qo)
    Aoeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13)
            + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13)
            - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2
            - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(53857323, 16331560))

    Hooe = harmonic(no + po/qe)
    Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
            + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
            + Rational(486853480, 186374097) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8)))

    Hooo = harmonic(no + po/qo)
    Aooo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13)
            + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13)
            - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2
            - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(383693479, 125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e/a) == 1
        assert h.evalf() == a.evalf()
Esempio n. 35
0
def test_Function():
    assert mcode(f(x, y, z)) == "f[x, y, z]"
    assert mcode(sin(x) ** cos(x)) == "Sin[x]^Cos[x]"
    assert mcode(sign(x)) == "Sign[x]"

    assert mcode(atanh(x), user_functions={"atanh": "ArcTanh"}) == "ArcTanh[x]"

    assert (mcode(meijerg(((1, 1), (3, 4)), ((1,), ()), x)) ==
            "MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]")
    assert (mcode(hyper((1, 2, 3), (3, 4), x)) ==
            "HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]")

    assert mcode(Min(x, y)) == "Min[x, y]"
    assert mcode(Max(x, y)) == "Max[x, y]"
    assert mcode(Max(x, 2)) == "Max[2, x]"  # issue sympy/sympy#15344

    assert mcode(binomial(x, y)) == "Binomial[x, y]"

    assert mcode(log(x)) == "Log[x]"
    assert mcode(tan(x)) == "Tan[x]"
    assert mcode(cot(x)) == "Cot[x]"
    assert mcode(asin(x)) == "ArcSin[x]"
    assert mcode(acos(x)) == "ArcCos[x]"
    assert mcode(atan(x)) == "ArcTan[x]"
    assert mcode(sinh(x)) == "Sinh[x]"
    assert mcode(cosh(x)) == "Cosh[x]"
    assert mcode(tanh(x)) == "Tanh[x]"
    assert mcode(coth(x)) == "Coth[x]"
    assert mcode(sech(x)) == "Sech[x]"
    assert mcode(csch(x)) == "Csch[x]"
    assert mcode(erfc(x)) == "Erfc[x]"
    assert mcode(conjugate(x)) == "Conjugate[x]"
    assert mcode(re(x)) == "Re[x]"
    assert mcode(im(x)) == "Im[x]"
    assert mcode(polygamma(x, y)) == "PolyGamma[x, y]"
    assert mcode(factorial(x)) == "Factorial[x]"
    assert mcode(factorial2(x)) == "Factorial2[x]"
    assert mcode(rf(x, y)) == "Pochhammer[x, y]"
    assert mcode(gamma(x)) == "Gamma[x]"
    assert mcode(zeta(x)) == "Zeta[x]"
    assert mcode(asinh(x)) == "ArcSinh[x]"
    assert mcode(Heaviside(x)) == "UnitStep[x]"
    assert mcode(fibonacci(x)) == "Fibonacci[x]"
    assert mcode(polylog(x, y)) == "PolyLog[x, y]"
    assert mcode(atanh(x)) == "ArcTanh[x]"

    class myfunc1(Function):
        @classmethod
        def eval(cls, x):
            pass

    class myfunc2(Function):
        @classmethod
        def eval(cls, x, y):
            pass

    pytest.raises(ValueError,
                  lambda: mcode(myfunc1(x),
                                user_functions={"myfunc1": ["Myfunc1"]}))
    assert mcode(myfunc1(x),
                 user_functions={"myfunc1": "Myfunc1"}) == "Myfunc1[x]"
    assert mcode(myfunc2(x, y),
                 user_functions={"myfunc2": [(lambda *x: False,
                                              "Myfunc2")]}) == "myfunc2[x, y]"
Esempio n. 36
0
 def _expr_small(cls, a, z):
     return cos(2 * a * asin(sqrt(z)))
Esempio n. 37
0
def test_jscode_functions():
    assert jscode(sin(x) ** cos(x)) == "Math.pow(Math.sin(x), Math.cos(x))"
Esempio n. 38
0
def test_Min():
    n = Symbol('n', negative=True)
    n_ = Symbol('n_', negative=True)
    nn = Symbol('nn', nonnegative=True)
    nn_ = Symbol('nn_', nonnegative=True)
    p = Symbol('p', positive=True)
    p_ = Symbol('p_', positive=True)
    np = Symbol('np', nonpositive=True)
    np_ = Symbol('np_', nonpositive=True)

    assert Min(5, 4) == 4
    assert Min(-oo, -oo) == -oo
    assert Min(-oo, n) == -oo
    assert Min(n, -oo) == -oo
    assert Min(-oo, np) == -oo
    assert Min(np, -oo) == -oo
    assert Min(-oo, 0) == -oo
    assert Min(0, -oo) == -oo
    assert Min(-oo, nn) == -oo
    assert Min(nn, -oo) == -oo
    assert Min(-oo, p) == -oo
    assert Min(p, -oo) == -oo
    assert Min(-oo, oo) == -oo
    assert Min(oo, -oo) == -oo
    assert Min(n, n) == n
    assert Min(n, np) == Min(n, np)
    assert Min(np, n) == Min(np, n)
    assert Min(n, 0) == n
    assert Min(0, n) == n
    assert Min(n, nn) == n
    assert Min(nn, n) == n
    assert Min(n, p) == n
    assert Min(p, n) == n
    assert Min(n, oo) == n
    assert Min(oo, n) == n
    assert Min(np, np) == np
    assert Min(np, 0) == np
    assert Min(0, np) == np
    assert Min(np, nn) == np
    assert Min(nn, np) == np
    assert Min(np, p) == np
    assert Min(p, np) == np
    assert Min(np, oo) == np
    assert Min(oo, np) == np
    assert Min(0, 0) == 0
    assert Min(0, nn) == 0
    assert Min(nn, 0) == 0
    assert Min(0, p) == 0
    assert Min(p, 0) == 0
    assert Min(0, oo) == 0
    assert Min(oo, 0) == 0
    assert Min(nn, nn) == nn
    assert Min(nn, p) == Min(nn, p)
    assert Min(p, nn) == Min(p, nn)
    assert Min(nn, oo) == nn
    assert Min(oo, nn) == nn
    assert Min(p, p) == p
    assert Min(p, oo) == p
    assert Min(oo, p) == p
    assert Min(oo, oo) == oo

    assert isinstance(Min(n, n_), Min)
    assert isinstance(Min(nn, nn_), Min)
    assert isinstance(Min(np, np_), Min)
    assert isinstance(Min(p, p_), Min)

    # lists
    pytest.raises(ValueError, lambda: Min())
    assert Min(x, y) == Min(y, x)
    assert Min(x, y, z) == Min(z, y, x)
    assert Min(x, Min(y, z)) == Min(z, y, x)
    assert Min(x, Max(y, -oo)) == Min(x, y)
    assert Min(p, oo, n, p, p, p_) == n
    assert Min(p_, n_, p) == n_
    assert Min(n, oo, -7, p, p, 2) == Min(n, -7)
    assert Min(2, x, p, n, oo, n_, p, 2, -2, -2) == Min(-2, x, n, n_)
    assert Min(0, x, 1, y) == Min(0, x, y)
    assert Min(1000, 100, -100, x, p, n) == Min(n, x, -100)
    assert Min(cos(x), sin(x)) == Min(cos(x), sin(x))
    assert Min(cos(x), sin(x)).subs({x: 1}) == cos(1)
    assert Min(cos(x), sin(x)).subs({x: Rational(1, 2)}) == sin(Rational(1, 2))
    pytest.raises(ValueError, lambda: Min(cos(x), sin(x)).subs({x: I}))
    pytest.raises(ValueError, lambda: Min(I))
    pytest.raises(ValueError, lambda: Min(I, x))
    pytest.raises(ValueError, lambda: Min(zoo, x))

    assert Min(1, x).diff(x) == Heaviside(1 - x)
    assert Min(x, 1).diff(x) == Heaviside(1 - x)
    assert Min(0, -x, 1 - 2*x).diff(x) == -Heaviside(x + Min(0, -2*x + 1)) \
        - 2*Heaviside(2*x + Min(0, -x) - 1)

    pytest.raises(ArgumentIndexError, lambda: Min(1, x).fdiff(3))

    a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True)
    # a and b are both real, Min(a, b) should be real
    assert Min(a, b).is_extended_real

    # issue sympy/sympy#7619
    f = Function('f')
    assert Min(1, 2 * Min(f(1), 2))  # doesn't fail

    # issue sympy/sympy#7233
    e = Min(0, x)
    assert e.evalf == e.n
    assert e.evalf(strict=False).args == (0, x)
Esempio n. 39
0
def test_trigintegrate_symbolic():
    n = Symbol('n', integer=True)
    assert trigintegrate(cos(x)**n, x) is None
    assert trigintegrate(sin(x)**n, x) is None
    assert trigintegrate(cot(x)**n, x) is None
Esempio n. 40
0
def test_trigintegrate_odd():
    assert trigintegrate(Rational(1), x) == x
    assert trigintegrate(x, x) is None
    assert trigintegrate(x**2, x) is None

    assert trigintegrate(sin(x), x) == -cos(x)
    assert trigintegrate(cos(x), x) == sin(x)

    assert trigintegrate(sin(3 * x), x) == -cos(3 * x) / 3
    assert trigintegrate(cos(3 * x), x) == sin(3 * x) / 3

    y = Symbol('y')
    assert trigintegrate(sin(y*x), x) == \
        Piecewise((0, Eq(y, 0)), (-cos(y*x)/y, True))
    assert trigintegrate(cos(y*x), x) == \
        Piecewise((x, Eq(y, 0)), (sin(y*x)/y, True))
    assert trigintegrate(sin(y*x)**2, x) == \
        Piecewise((0, Eq(y, 0)), ((x*y/2 - sin(x*y)*cos(x*y)/2)/y, True))
    assert trigintegrate(sin(y*x)*cos(y*x), x) == \
        Piecewise((0, Eq(y, 0)), (sin(x*y)**2/(2*y), True))
    assert trigintegrate(cos(y*x)**2, x) == \
        Piecewise((x, Eq(y, 0)), ((x*y/2 + sin(x*y)*cos(x*y)/2)/y, True))

    y = Symbol('y', positive=True)
    # TODO: remove conds='none' below. For this to work we would have to rule
    #       out (e.g. by trying solve) the condition y = 0, incompatible with
    #       y.is_positive being True.
    assert trigintegrate(sin(y * x), x, conds='none') == -cos(y * x) / y
    assert trigintegrate(cos(y * x), x, conds='none') == sin(y * x) / y

    assert trigintegrate(sin(x) * cos(x), x) == sin(x)**2 / 2
    assert trigintegrate(sin(x) * cos(x)**2, x) == -cos(x)**3 / 3
    assert trigintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3

    # check if it selects right function to substitute,
    # so the result is kept simple
    assert trigintegrate(sin(x)**7 * cos(x), x) == sin(x)**8 / 8
    assert trigintegrate(sin(x) * cos(x)**7, x) == -cos(x)**8 / 8

    assert trigintegrate(sin(x)**7 * cos(x)**3, x) == \
        -sin(x)**10/10 + sin(x)**8/8
    assert trigintegrate(sin(x)**3 * cos(x)**7, x) == \
        cos(x)**10/10 - cos(x)**8/8
Esempio n. 41
0
def test_trigintegrate_symbolic():
    n = Symbol('n', integer=True)
    assert trigintegrate(cos(x)**n, x) is None
    assert trigintegrate(sin(x)**n, x) is None
    assert trigintegrate(cot(x)**n, x) is None
Esempio n. 42
0
def test_trigintegrate_even():
    assert trigintegrate(sin(x)**2, x) == x / 2 - cos(x) * sin(x) / 2
    assert trigintegrate(cos(x)**2, x) == x / 2 + cos(x) * sin(x) / 2

    assert trigintegrate(sin(3 * x)**2,
                         x) == x / 2 - cos(3 * x) * sin(3 * x) / 6
    assert trigintegrate(cos(3 * x)**2,
                         x) == x / 2 + cos(3 * x) * sin(3 * x) / 6
    assert trigintegrate(sin(x)**2 * cos(x)**2, x) == \
        x/8 - sin(2*x)*cos(2*x)/16

    assert trigintegrate(sin(x)**4 * cos(x)**2, x) == \
        x/16 - sin(x) * cos(x)/16 - sin(x)**3*cos(x)/24 + \
        sin(x)**5*cos(x)/6

    assert trigintegrate(sin(x)**2 * cos(x)**4, x) == \
        x/16 + cos(x) * sin(x)/16 + cos(x)**3*sin(x)/24 - \
        cos(x)**5*sin(x)/6

    assert trigintegrate(sin(x)**(-4), x) == -2*cos(x)/(3*sin(x)) \
        - cos(x)/(3*sin(x)**3)

    assert trigintegrate(cos(x)**(-6), x) == sin(x)/(5*cos(x)**5) \
        + 4*sin(x)/(15*cos(x)**3) + 8*sin(x)/(15*cos(x))
Esempio n. 43
0
def test_matmul_simplify():
    A = MatrixSymbol('A', 1, 1)
    assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \
        MatMul(A, ImmutableMatrix([[1]]))
Esempio n. 44
0
def test_ccode_functions():
    assert ccode(sin(x)**cos(x)) == "pow(sin(x), cos(x))"

    assert ccode(elliptic_e(x)) == ("// Not supported in C:\n"
                                    "// elliptic_e\nelliptic_e(x)")
Esempio n. 45
0
def test_Function():
    assert mcode(sin(x) ** cos(x)) == "sin(x).^cos(x)"
    assert mcode(abs(x)) == "abs(x)"
    assert mcode(ceiling(x)) == "ceil(x)"
Esempio n. 46
0
def test_trigintegrate_odd():
    assert trigintegrate(Integer(1), x) == x
    assert trigintegrate(x, x) is None
    assert trigintegrate(x**2, x) is None

    assert trigintegrate(sin(x), x) == -cos(x)
    assert trigintegrate(cos(x), x) == sin(x)

    assert trigintegrate(sin(3*x), x) == -cos(3*x)/3
    assert trigintegrate(cos(3*x), x) == sin(3*x)/3

    y = Symbol('y')
    assert trigintegrate(sin(y*x), x) == \
        Piecewise((0, Eq(y, 0)), (-cos(y*x)/y, True))
    assert trigintegrate(cos(y*x), x) == \
        Piecewise((x, Eq(y, 0)), (sin(y*x)/y, True))
    assert trigintegrate(sin(y*x)**2, x) == \
        Piecewise((0, Eq(y, 0)), ((x*y/2 - sin(x*y)*cos(x*y)/2)/y, True))
    assert trigintegrate(sin(y*x)*cos(y*x), x) == \
        Piecewise((0, Eq(y, 0)), (sin(x*y)**2/(2*y), True))
    assert trigintegrate(cos(y*x)**2, x) == \
        Piecewise((x, Eq(y, 0)), ((x*y/2 + sin(x*y)*cos(x*y)/2)/y, True))

    y = Symbol('y', positive=True)
    # TODO: remove conds='none' below. For this to work we would have to rule
    #       out (e.g. by trying solve) the condition y = 0, incompatible with
    #       y.is_positive being True.
    assert trigintegrate(sin(y*x), x, conds='none') == -cos(y*x)/y
    assert trigintegrate(cos(y*x), x, conds='none') == sin(y*x)/y

    assert trigintegrate(sin(x)*cos(x), x) == sin(x)**2/2
    assert trigintegrate(sin(x)*cos(x)**2, x) == -cos(x)**3/3
    assert trigintegrate(sin(x)**2*cos(x), x) == sin(x)**3/3

    # check if it selects right function to substitute,
    # so the result is kept simple
    assert trigintegrate(sin(x)**7 * cos(x), x) == sin(x)**8/8
    assert trigintegrate(sin(x) * cos(x)**7, x) == -cos(x)**8/8

    assert trigintegrate(sin(x)**7 * cos(x)**3, x) == \
        -sin(x)**10/10 + sin(x)**8/8
    assert trigintegrate(sin(x)**3 * cos(x)**7, x) == \
        cos(x)**10/10 - cos(x)**8/8
Esempio n. 47
0
 def _expr_small_minus(cls, a, z):
     return (1 + z)**a * cos(2 * a * atan(sqrt(z)))
Esempio n. 48
0
def test_matmul_simplify():
    A = MatrixSymbol('A', 1, 1)
    assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \
        MatMul(A, ImmutableMatrix([[1]]))
Esempio n. 49
0
def test_trigintegrate_even():
    assert trigintegrate(sin(x)**2, x) == x/2 - cos(x)*sin(x)/2
    assert trigintegrate(cos(x)**2, x) == x/2 + cos(x)*sin(x)/2

    assert trigintegrate(sin(3*x)**2, x) == x/2 - cos(3*x)*sin(3*x)/6
    assert trigintegrate(cos(3*x)**2, x) == x/2 + cos(3*x)*sin(3*x)/6
    assert trigintegrate(sin(x)**2 * cos(x)**2, x) == \
        x/8 - sin(2*x)*cos(2*x)/16

    assert trigintegrate(sin(x)**4 * cos(x)**2, x) == \
        x/16 - sin(x) * cos(x)/16 - sin(x)**3*cos(x)/24 + \
        sin(x)**5*cos(x)/6

    assert trigintegrate(sin(x)**2 * cos(x)**4, x) == \
        x/16 + cos(x) * sin(x)/16 + cos(x)**3*sin(x)/24 - \
        cos(x)**5*sin(x)/6

    assert trigintegrate(sin(x)**(-4), x) == -2*cos(x)/(3*sin(x)) \
        - cos(x)/(3*sin(x)**3)

    assert trigintegrate(cos(x)**(-6), x) == sin(x)/(5*cos(x)**5) \
        + 4*sin(x)/(15*cos(x)**3) + 8*sin(x)/(15*cos(x))