Attachment 'ChristoffelWord.py'

Download

   1 from JyModule import *
   2 
   3 xmax = 10
   4 ymax = 8
   5 def draw():
   6     beginpage()
   7 
   8     scale(50)
   9     translate(1,1)
  10 
  11     #grid
  12     newpath()
  13     for i in range(xmax):
  14         moveto(i,0)
  15         lineto(i,ymax)
  16     
  17     for i in range(ymax):
  18         moveto(0,i)
  19         lineto(xmax,i)
  20     stroke(0.5)
  21 
  22     #vecteur
  23     dx = int(upperright.x)
  24     dy = int(upperright.y)
  25     newpath()
  26     moveto(0,0)
  27     lineto(dx,dy)
  28     setlinewidth(3)
  29     stroke([0,0,1])
  30 
  31     #path
  32     dir={'a':(1,0),'b':(0,1),'A':(-1,0),'B':(0,-1)}
  33     w = ChristoffelWord(dy,dx)
  34     newpath()
  35     moveto(0,0)
  36     for a in w:
  37         rlineto(dir[a])
  38     setlinewidth(3)
  39     stroke([1,0,0])
  40 
  41     #origine
  42     newpath()
  43     circle(0,0,0.08)
  44     fill(1,0,0)
  45     stroke(0)
  46 
  47     #is christoffel
  48     if isChristoffel(dx,dy):
  49         newpath()
  50         moveto(xmax/2,-.5)
  51         setfont(12)
  52         show('is Christoffel!!!')
  53 
  54     #the word
  55     newpath()
  56     moveto(0,-.5)
  57     setfont(12)
  58     show('Word : '+w)
  59 
  60     #Coordinates
  61     newpath()
  62     moveto(dx + .1,dy + .2)
  63     setfont(12)
  64     show('( %s, %s )'%(dx,dy))
  65 
  66     #moveable point
  67     newpath()
  68     placemoveable(upperright)
  69 
  70     endpage()
  71 
  72 def move(point, px, py):
  73     x = max(0, int(px))
  74     y = max(0, int(py))
  75     point.setpoint(x, y)
  76     
  77 
  78 upperright = Moveablepoint(1, 1, move)
  79 addmoveable(upperright)
  80 
  81 openframe((xmax+2)*50, (ymax+2)*50, draw)
  82 
  83 def isChristoffel(p,q):
  84     return GCD(p,q) == 1
  85 
  86 def ChristoffelWord(p, q):
  87     if p<0:
  88         return ChristoffelWord(-p,q).replace('A','a')
  89     elif q<0:
  90         return ChristoffelWord(p,-q).replace('B','b')
  91     
  92     # Compute the Christoffel word
  93     w = '' 
  94     u = 0
  95     if p == 0:
  96         w = 'a'*q
  97     else:
  98         for i in range(p + q):
  99             v = (u+p) % (p+q)
 100             if u < v:
 101                 new_letter = 'a'
 102             else:
 103                 new_letter = 'b'
 104             w += new_letter
 105             u = v
 106     return w
 107 
 108 def GCD(x, y):
 109 # The greatest common denominator (GCD) is the largest positive integer
 110 # that divides into both numbers without a remainder.
 111 # Examples: GCD(256,64)=64, GCD(12,8)=4, GCD(5,3)=1
 112 
 113  # Work With absolute values (positive integers)
 114     if x < 0 : x = -x
 115     if y < 0 : y = -y
 116 
 117     if x + y > 0 :
 118         g = y
 119         # Iterate Until x = 0
 120         while x > 0:
 121              g = x
 122              x = y % x
 123              y = g
 124         return g
 125     else:
 126   # Error, both parameters zero
 127         return 0

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.