Jacobian Algorithm for Multidimensional Continued Fractions

Just a great example of Dynamic programming.

#!/usr/bin/env python
from math import floor
A = [ 1, 0, 0]
B = [ 0, 1 ,0]
C = [0 , 0 ,1]
p = []
q = []                                                                                     
u = [17]
v = [44]
w = [13]
i = 0
DEBUG = True
while (u[-1] != 0):
    p.append(int(floor(v[-1]/u[-1])))
    q.append(int(floor(w[-1]/u[-1])))
    A.append(q[-1]*A[-1] +p[-1]*A[-2] +A[-3])
    B.append(q[-1]*B[-1]+p[-1]*B[-2] + B[-3])
    C.append(q[-1]*C[-1]+p[-1]*C[-2] + C[-3])
    u.append(v[-1] – p[-1]*u[-1])
    v.append(w[-1] – q[-1]*u[-2])
    w.append(u[-2])
    i+=1
    if DEBUG:
        print i, u[-1], v[-1], w[-1], p[-1], q[-1], A[-1], B[-1], C[-1]

if DEBUG:
    print p, q
for k in range(i-1):
    print (p[k],q[k]),

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Reddit
  • Slashdot
  • description
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati

Comments (2)

PythonNovember 20th, 2008 at 10:25 am

1 10 13 17 2 0 1 2 0
2 3 7 10 1 1 1 3 1
3 1 1 3 2 3 5 13 4
4 0 0 1 1 3 17 44 13
[2, 1, 2, 1] [0, 1, 3, 3]
(2, 0) (1, 1) (2, 3)

PrasannaNovember 21st, 2008 at 3:30 pm

Thanks for the output :) . I’d say it’d be a lot better if it was done by some plugin in wordpress… hehehe

Leave a comment

Your comment