Beispiel #1
0
def test_evalf_near_integers():
    # Binet's formula
    f = lambda n: ((1 + sqrt(5))**n) / (2**n * sqrt(5))
    assert NS(f(5000) - fibonacci(5000), 10, maxn=1500) == '5.156009964e-1046'
    # Some near-integer identities from
    # http://mathworld.wolfram.com/AlmostInteger.html
    assert NS('sin(2017*2**(1/5))', 15) == '-1.00000000000000'
    assert NS('sin(2017*2**(1/5))', 20) == '-0.99999999999999997857'
    assert NS('1+sin(2017*2**(1/5))', 15) == '2.14322287389390e-17'
    assert NS('45 - 613*E/37 + 35/991', 15) == '6.03764498766326e-11'
Beispiel #2
0
def test_evalf_integer_parts():
    a = floor(log(8) / log(2) - exp(-1000), evaluate=False)
    b = floor(log(8) / log(2), evaluate=False)
    assert a.evalf() == 3
    assert b.evalf() == 3
    # equals, as a fallback, can still fail but it might succeed as here
    assert ceiling(10 * (sin(1)**2 + cos(1)**2)) == 10

    assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \
        int(11188719610782480504630258070757734324011354208865721592720336800)
    assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \
        int(11188719610782480504630258070757734324011354208865721592720336801)
    assert int(floor(GoldenRatio**999 / sqrt(5) +
                     S.Half).evalf(1000)) == fibonacci(999)
    assert int(floor(GoldenRatio**1000 / sqrt(5) +
                     S.Half).evalf(1000)) == fibonacci(1000)

    assert ceiling(x).evalf(subs={x: 3}) == 3
    assert ceiling(x).evalf(subs={x: 3 * I}) == 3.0 * I
    assert ceiling(x).evalf(subs={x: 2 + 3 * I}) == 2.0 + 3.0 * I
    assert ceiling(x).evalf(subs={x: 3.}) == 3
    assert ceiling(x).evalf(subs={x: 3. * I}) == 3.0 * I
    assert ceiling(x).evalf(subs={x: 2. + 3 * I}) == 2.0 + 3.0 * I

    assert float((floor(1.5, evaluate=False) + 1 / 9).evalf()) == 1 + 1 / 9
    assert float((floor(0.5, evaluate=False) + 20).evalf()) == 20

    # issue 19991
    n = 1169809367327212570704813632106852886389036911
    r = 744723773141314414542111064094745678855643068

    assert floor(n / (pi / 2)) == r
    assert floor(80782 * sqrt(2)) == 114242

    # issue 20076
    assert 260515 - floor(260515 / pi + 1 / 2) * pi == atan(tan(260515))
Beispiel #3
0
def test_find_simple_recurrence():
    a = Function('a')
    n = Symbol('n')
    assert find_simple_recurrence([fibonacci(k) for k in range(12)
                                   ]) == (-a(n) - a(n + 1) + a(n + 2))

    f = Function('a')
    i = Symbol('n')
    a = [1, 1, 1]
    for k in range(15):
        a.append(5 * a[-1] - 3 * a[-2] + 8 * a[-3])
    assert find_simple_recurrence(a, A=f, N=i) == (-8 * f(i) + 3 * f(i + 1) -
                                                   5 * f(i + 2) + f(i + 3))
    assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0, 1, 2, 85, 4, 5,
                                   63]) == 0
Beispiel #4
0
def test_guess_generating_function():
    x = Symbol('x')
    assert guess_generating_function(
        [fibonacci(k)
         for k in range(5, 15)])['ogf'] == ((3 * x + 5) / (-x**2 - x + 1))
    assert guess_generating_function(
        [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122,
         38919])['ogf'] == ((1 / (x**4 + 2 * x**2 - 4 * x + 1))**S.Half)
    assert guess_generating_function(
        sympify(
            "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]"
        ))['ogf'] == (x + Rational(3, 2)) / (11 * x**2 - 3 * x + 1)
    assert guess_generating_function([factorial(k) for k in range(12)],
                                     types=['egf'])['egf'] == 1 / (-x + 1)
    assert guess_generating_function([k + 1 for k in range(12)],
                                     types=['egf']) == {
                                         'egf': (x + 1) * exp(x),
                                         'lgdegf': (x + 2) / (x + 1)
                                     }
Beispiel #5
0
def test_approximants():
    x, t = symbols("x,t")
    g = [lucas(k) for k in range(16)]
    assert [e for e in approximants(g)] == ([
        2, -4 / (x - 2), (5 * x - 2) / (3 * x - 1), (x - 2) / (x**2 + x - 1)
    ])
    g = [lucas(k) + fibonacci(k + 2) for k in range(16)]
    assert [e for e in approximants(g)] == ([
        3, -3 / (x - 1), (3 * x - 3) / (2 * x - 1), -3 / (x**2 + x - 1)
    ])
    g = [lucas(k)**2 for k in range(16)]
    assert [e for e in approximants(g)] == ([
        4, -16 / (x - 4),
        (35 * x - 4) / (9 * x - 1), (37 * x - 28) / (13 * x**2 + 11 * x - 7),
        (50 * x**2 + 63 * x - 52) / (37 * x**2 + 19 * x - 13),
        (-x**2 - 7 * x + 4) / (x**3 - 2 * x**2 - 2 * x + 1)
    ])
    p = [sum(binomial(k, i) * x**i for i in range(k + 1)) for k in range(16)]
    y = approximants(p, t, simplify=True)
    assert next(y) == 1
    assert next(y) == -1 / (t * (x + 1) - 1)
Beispiel #6
0
def test_find_linear_recurrence():
    assert sequence((0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), \
    (n, 0, 10)).find_linear_recurrence(11) == [1, 1]
    assert sequence((1, 2, 4, 7, 28, 128, 582, 2745, 13021, 61699, 292521, \
    1387138), (n, 0, 11)).find_linear_recurrence(12) == [5, -2, 6, -11]
    assert sequence(x*n**3+y*n, (n, 0, oo)).find_linear_recurrence(10) \
    == [4, -6, 4, -1]
    assert sequence(x**n, (n,0,20)).find_linear_recurrence(21) == [x]
    assert sequence((1,2,3)).find_linear_recurrence(10, 5) == [0, 0, 1]
    assert sequence(((1 + sqrt(5))/2)**n + \
    (-(1 + sqrt(5))/2)**(-n)).find_linear_recurrence(10) == [1, 1]
    assert sequence(x*((1 + sqrt(5))/2)**n + y*(-(1 + sqrt(5))/2)**(-n), \
    (n,0,oo)).find_linear_recurrence(10) == [1, 1]
    assert sequence((1,2,3,4,6),(n, 0, 4)).find_linear_recurrence(5) == []
    assert sequence((2,3,4,5,6,79),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
    == ([], None)
    assert sequence((2,3,4,5,8,30),(n, 0, 5)).find_linear_recurrence(6,gfvar=x) \
    == ([Rational(19, 2), -20, Rational(27, 2)], (-31*x**2 + 32*x - 4)/(27*x**3 - 40*x**2 + 19*x -2))
    assert sequence(fibonacci(n)).find_linear_recurrence(30,gfvar=x) \
    == ([1, 1], -x/(x**2 + x - 1))
    assert sequence(tribonacci(n)).find_linear_recurrence(30,gfvar=x) \
    ==  ([1, 1, 1], -x/(x**3 + x**2 + x - 1))
Beispiel #7
0
def test_sympy__functions__combinatorial__numbers__fibonacci():
    from sympy.functions.combinatorial.numbers import fibonacci
    assert _test_args(fibonacci(x))
Beispiel #8
0
def test_issue_10382():
    n = Symbol('n', integer=True)
    assert limit(fibonacci(n+1)/fibonacci(n), n, oo) == S.GoldenRatio
Beispiel #9
0
def test_sympy__functions__combinatorial__numbers__fibonacci():
    from sympy.functions.combinatorial.numbers import fibonacci
    assert _test_args(fibonacci(x))
Beispiel #10
0
def fibonacci(n: Union[int, float]) -> Union[int, float]:
    try:
        import sympy.functions.combinatorial.numbers as ns
    except ModuleNotFoundError:
        raise Exception("Install sympy to use number-theoretic functions!")
    return ns.fibonacci(n)
Beispiel #11
0
def test_linrec():
    assert linrec(coeffs=[1, 1], init=[1, 1], n=20) == 10946
    assert linrec(coeffs=[1, 2, 3, 4, 5], init=[1, 1, 0, 2], n=10) == 1040
    assert linrec(coeffs=[0, 0, 11, 13], init=[23, 27], n=25) == 59628567384
    assert linrec(coeffs=[0, 0, 1, 1, 2], init=[1, 5, 3], n=15) == 165
    assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=70) == \
        56889923441670659718376223533331214868804815612050381493741233489928913241
    assert linrec(coeffs=[0]*55 + [1, 1, 2, 3], init=[0]*50 + [1, 2, 3], n=4000) == \
        702633573874937994980598979769135096432444135301118916539

    assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**4)
    assert linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4], n=10**5)

    assert all(
        linrec(coeffs=[1, 1], init=[0, 1], n=n) == fibonacci(n)
        for n in range(95, 115))

    assert all(
        linrec(coeffs=[1, 1], init=[1, 1], n=n) == fibonacci(n + 1)
        for n in range(595, 615))

    a = [
        S.Half,
        Rational(3, 4),
        Rational(5, 6), 7,
        Rational(8, 9),
        Rational(3, 5)
    ]
    b = [1, 2, 8, Rational(5, 7), Rational(3, 7), Rational(2, 9), 6]
    x, y, z = symbols('x y z')

    assert linrec(coeffs=a[:5], init=b[:4], n=80) == \
        Rational(1726244235456268979436592226626304376013002142588105090705187189,
            1960143456748895967474334873705475211264)

    assert linrec(coeffs=a[:4], init=b[:4], n=50) == \
        Rational(368949940033050147080268092104304441, 504857282956046106624)

    assert linrec(coeffs=a[3:], init=b[:3], n=35) == \
        Rational(97409272177295731943657945116791049305244422833125109,
            814315512679031689453125)

    assert linrec(coeffs=[0]*60 + [Rational(2, 3), Rational(4, 5)], init=b, n=3000) == \
        Rational(26777668739896791448594650497024, 48084516708184142230517578125)

    raises(TypeError,
           lambda: linrec(coeffs=[11, 13, 15, 17], init=[1, 2, 3, 4, 5], n=1))
    raises(TypeError, lambda: linrec(coeffs=a[:4], init=b[:5], n=10000))
    raises(ValueError, lambda: linrec(coeffs=a[:4], init=b[:4], n=-10000))
    raises(TypeError, lambda: linrec(x, b, n=10000))
    raises(TypeError, lambda: linrec(a, y, n=10000))

    assert linrec(coeffs=[x, y, z], init=[1, 1, 1], n=4) == \
        x**2  + x*y + x*z + y + z
    assert linrec(coeffs=[1, 2, 1], init=[x, y, z], n=20) == \
        269542*x + 664575*y + 578949*z
    assert linrec(coeffs=[0, 3, 1, 2], init=[x, y], n=30) == \
        58516436*x + 56372788*y
    assert linrec(coeffs=[0]*50 + [1, 2, 3], init=[x, y, z], n=1000) == \
        11477135884896*x + 25999077948732*y + 41975630244216*z
    assert linrec(coeffs=[], init=[1, 1], n=20) == 0
    assert linrec(coeffs=[x, y, z], init=[1, 2, 3], n=2) == 3
Beispiel #12
0
def test_guess_generating_function_rational():
    x = Symbol('x')
    assert guess_generating_function_rational([
        fibonacci(k) for k in range(5, 15)
    ]) == ((3 * x + 5) / (-x**2 - x + 1))
Beispiel #13
0
def test_find_simple_recurrence_vector():
    assert find_simple_recurrence_vector([fibonacci(k)
                                          for k in range(12)]) == [1, -1, -1]