This is indeed a bug!
The problem appears to be in cancel(). It creates two polynomials,
Poly(1317378933230047068160*sqrt(5) + 2945748836994210856960, sqrt(5),
domain='ZZ') Poly(120352542776360960*sqrt(5) + 269116466014453760,
sqrt(5), domain='ZZ'), and applies Euclid's algorithm on them to find
the gcd, and then polynomial division. The problem is that the
generator sqrt(5) is an algebraic, so certain polynomials in it will
be identically zero. But Poly does not know this, so somewhere in gcd
or div it comes across such a polynomial and assumes that it is not
zero, leading to the incorrect result. If you want, you may try
applying the algorithm by hand as an exercise to see where it goes
awry. You might even be able to figure out why 21 is the magic number
where it starts to go bad.
If you use expand() and then radsimp(), you get the correct result.
The only way to really fix this would be to prevent cancel, and really
Poly in general, from creating generators from algebraic numbers.
This would unfortunately break a lot of things that otherwise work
just fine (because they never come across these identically zero
polynomials in the generators).
I should also point out that if you create the polynomials using an
algebraic extension, it works correctly:
In [20]: F, G = Poly(1317378933230047068160*sqrt(5) +
2945748836994210856960, sqrt(5), extension=sqrt(5)),
Poly(120352542776360960*sqrt(5) + 269116466014453760, sqrt(5),
extension=sqrt(5))
In [21]: F.cancel(G)Out[21]: (1, Poly(1317378933230047068160, sqrt(5),
domain='QQ<sqrt(5)>'), Poly(120352542776360960, sqrt(5),
domain='QQ<sqrt(5)>'))
In [22]: 1317378933230047068160/120352542776360960
Out[22]: 10946.0
But unfortunately, this can get quite slow if you have more than one extension.
Would you mind opening an issue in our issue tracker for this:
http://code.google.com/p/sympy/issues/list.
Aaron Meurer
On Tue, Sep 18, 2012 at 10:38 AM, Oscar <
oscar.j....@gmail.com> wrote:
> Hi all,
>
> Having just discovered Binet's formula for the nth fibonacci number I
> thought
> I'd give it a try in sympy and stumbled across a possible bug.
>
> Binet's formula, although it has a number of irrational quantities, should
> always yield an integer. I found that sympy was able to compute this
> correctly
> up to the 20th Fibonacci number and that started returning irrational
> quantities when I asked it to simplify the expression.
>
> Demonstration using a fresh checkout of sympy master in a freshly created,
> otherwise empty Python 2.7.1 virtual env:
>
>>>> import sympy
>>>> n = sympy.Symbol('n')
>>>> Phi = (1 + sympy.sqrt(5)) / 2
>>>> fibn = (Phi**n - (-Phi)**(-n)) / sympy.sqrt(5)
>>>> for i in range(1, 25):
> ... print '%ith Fibonacci number:' % i, fibn.subs(n, i).simplify()
> ...
> 1th Fibonacci number: 1
> 2th Fibonacci number: 1
> 3th Fibonacci number: 2
> 4th Fibonacci number: 3
> 5th Fibonacci number: 5
> 6th Fibonacci number: 8
> 7th Fibonacci number: 13
> 8th Fibonacci number: 21
> 9th Fibonacci number: 34
> 10th Fibonacci number: 55
> 11th Fibonacci number: 89
> 12th Fibonacci number: 144
> 13th Fibonacci number: 233
> 14th Fibonacci number: 377
> 15th Fibonacci number: 610
> 16th Fibonacci number: 987
> 17th Fibonacci number: 1597
> 18th Fibonacci number: 2584
> 19th Fibonacci number: 4181
> 20th Fibonacci number: 6765
> 21th Fibonacci number: -4498 + sqrt(5) <--- should be 10946
> 22th Fibonacci number: -10108 + sqrt(5)
> 23th Fibonacci number: -6785 + sqrt(5)
> 24th Fibonacci number: sqrt(5) + 14490
>>>> fibn.subs(n, 21)
> sqrt(5)*(-1/(-sqrt(5)/2 - 1/2)**21 + (1/2 + sqrt(5)/2)**21)/5
>>>> fibn.subs(n, 21).simplify()
> -4498 + sqrt(5)
>>>> fibn.subs(n, 21).simplify().evalf()
> -4495.76393202250
>>>> fibn.subs(n, 21).evalf()
> 10946.0000000000
>
> That last line shows that the expression is correct before it is
> simplify()ed.
>
> Is this a known problem? I'd be happy to help fix this but I have little
> idea
> where to start. Is there any significance in the fact that it goes wrong at
> powers > 20?
>
> Oscar
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/sympy/-/gH8z3L4TgbUJ.
> To post to this group, send email to
sy...@googlegroups.com.
> To unsubscribe from this group, send email to
>
sympy+un...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/sympy?hl=en.