Esempio n. 1
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. 2
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(acot(x)) == "ArcCot[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(asinh(x)) == "ArcSinh[x]"
    assert mcode(acosh(x)) == "ArcCosh[x]"
    assert mcode(atanh(x)) == "ArcTanh[x]"
    assert mcode(acoth(x)) == "ArcCoth[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(Heaviside(x)) == "UnitStep[x]"
    assert mcode(fibonacci(x)) == "Fibonacci[x]"
    assert mcode(polylog(x, y)) == "PolyLog[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. 3
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. 4
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. 5
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)