Attachment 'tamer.sage'

Download

   1 #*****************************************************************************
   2 #       Copyright (C) 2009 Xavier Provencal <provencal at gmail.com>
   3 #                     2009 Sebastien Labbe <slabqc at gmail.com >
   4 #
   5 #  Distributed under the terms of the GNU General Public License (GPL)
   6 #                  http://www.gnu.org/licenses/
   7 #*****************************************************************************
   8 # Parameters that define the image:
   9 step  = n(0.01,digits=10)
  10 lion_speed = 4
  11 
  12 def to_polar(v) :
  13     if (v.norm() == 0) :
  14         return vector([0,0])
  15     x = v[0]
  16     y = v[1]
  17     if (x == 0 and y > 0) :
  18         angle = pi/2
  19     elif (x == 0 and y < 0) :
  20         angle = 3*pi/2
  21     elif x > 0 :
  22         angle = atan(y/x)
  23     elif x < 0 :
  24         angle = pi+atan(y/x)
  25     else :
  26         raise TypeError
  27     return vector([sqrt(x^2+y^2),angle])
  28 
  29 def to_carte(v) :
  30     rho = v[0]
  31     theta = v[1]
  32     return vector([rho*cos(theta), rho*sin(theta)]);
  33 
  34 def monMod(x,p) :
  35     nb = floor(x / p)
  36     return x - nb*p
  37 
  38 @CachedFunction
  39 def dompteur_pos(n) :
  40     r"""
  41     Return the position of the dompteur in cartesian coordinates.
  42 
  43     EXAMPLES::
  44 
  45         sage: dompteur_pos(0)
  46         (0, 0)
  47         sage: dompteur_pos(1)
  48         (-0.01000000000, 0)
  49         sage: dompteur_pos(2)
  50         (-0.01999215862, -0.0003959371065)
  51         sage: dompteur_pos(3)
  52         (-0.02996111130, -0.001183326674)
  53     """
  54     if (n == 0) :
  55         return vector([0,0])
  56     v = dompteur_pos(n-1) - to_carte(lion_polar_pos(n-1))
  57     return dompteur_pos(n-1) + step * v/v.norm();
  58 
  59 @CachedFunction
  60 def lion_polar_pos(n) :
  61     r"""
  62     Return the lion's position in polar coordinates.
  63 
  64     EXAMPLES::
  65 
  66         sage: lion_polar_pos(0)
  67         (1, 0)
  68         sage: lion_polar_pos(1)
  69         (1.000000000, 0.04000000000)
  70         sage: lion_polar_pos(1)
  71         (1.000000000, 0.04000000000)
  72         sage: lion_polar_pos(2)
  73         (1.000000000, 0.08000000000)
  74         sage: lion_polar_pos(3)
  75         (1.000000000, 0.1200000000)
  76     """
  77     if (n == 0) :
  78         return vector([1,0])
  79     angle_L = lion_polar_pos(n-1)[1]
  80     angle_D = to_polar(dompteur_pos(n-1))[1]
  81     diff = monMod(angle_L - angle_D,2*pi) 
  82     if 0 < diff < pi :
  83         return lion_polar_pos(n-1) - lion_speed*step*vector([0,1])
  84     else :
  85         return lion_polar_pos(n-1) + lion_speed*step*vector([0,1])
  86 
  87 
  88 def initialize_up_to(n=1000) :
  89     r"""
  90     Because ``dompteur_pos`` and ``lion_polar_pos`` are defined recursively
  91     on can get a ``RuntimeError: maximum recursion depth exceeded`` if both
  92     of these functions are not evaluated succesively. Here, we compute by
  93     step of 100.
  94 
  95     I'm sure that there is a better solution like changing the default
  96     value of the maximal recurxsion depth.
  97     """
  98     for i in range(0,n,100):
  99         lion_polar_pos(i)
 100 
 101 @CachedFunction
 102 def dessine(n) :
 103     r"""
 104     Returns a plot of the position of the lion and dompteur (tamer) at the n-th
 105     step.
 106 
 107     EXAMPLES::
 108 
 109         sage: dessine(10)
 110         sage: dessine(100)
 111         sage: for i in range(0,1000,100): dessine(i)
 112     """
 113     r = circle((0,0),1)
 114 
 115     rho,teta = lion_polar_pos(n)
 116     lion_text_pos = to_carte((rho-0.1,teta))
 117     lion_pos = to_carte((rho,teta))
 118     r += point(lion_pos,color="red",pointsize=25)
 119     r += text('Lion',lion_text_pos,rgbcolor='red')
 120 
 121     r += line(map(dompteur_pos,range(n+1)),color="blue",thickness=2)
 122     x,y = dompteur_pos(n)
 123     r += point((x,y),color="blue",pointsize=25)
 124     r += text('Tamer',(x,y+0.1),color='blue')
 125 
 126     r += text('Relative speed: Lion/Tamer = %s'%lion_speed ,(-1,1.2),color='purple',horizontal_alignment='left')
 127     r += text('Tamer step = %s times the radius'%N(step,digits=2),(-1,1.1),color='purple',horizontal_alignment='left')
 128     r += text('%s-th step'%n,(-1,1),color='purple',horizontal_alignment='left')
 129 
 130     r.set_aspect_ratio(1)
 131     r.axes(False)
 132     return r
 133 
 134 def anime(l) :
 135     r"""
 136     Returns an animation of the tamer and lion from start to stop by step.
 137 
 138     EXAMPLES::
 139 
 140         sage: l = range(0,100,10)
 141         sage: anime(l)
 142         Animation with 10 frames
 143     """
 144     initialize_up_to(l[-1])
 145     return animate(map(dessine, l))

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.
  • [get | view] (2010-03-03 21:11:38, 84.3 KB) [[attachment:fibotile.gif]]
  • [get | view] (2010-02-28 14:48:03, 11.3 KB) [[attachment:hanoi.gif]]
  • [get | view] (2010-11-16 13:57:47, 87.7 KB) [[attachment:pencil.gif]]
  • [get | view] (2009-12-09 17:00:14, 1454.7 KB) [[attachment:tamer.gif]]
  • [get | view] (2009-12-09 17:14:41, 4.1 KB) [[attachment:tamer.sage]]
  • [get | view] (2009-07-24 19:41:40, 572.3 KB) [[attachment:witch.gif]]
 All files | Selected Files: delete move to page copy to page

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