home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #23 / NN_1992_23.iso / spool / sci / math / 13321 < prev    next >
Encoding:
Text File  |  1992-10-16  |  5.0 KB  |  112 lines

  1. Newsgroups: sci.math
  2. Path: sparky!uunet!morrow.stanford.edu!CSD-NewsHost.Stanford.EDU!Sunburn.Stanford.EDU!pratt
  3. From: pratt@Sunburn.Stanford.EDU (Vaughan R. Pratt)
  4. Subject: Re: Real -> fraction algorithm needed (also: FAREY)
  5. Message-ID: <1992Oct16.200045.11492@CSD-NewsHost.Stanford.EDU>
  6. Sender: news@CSD-NewsHost.Stanford.EDU
  7. Organization: Computer Science Department,  Stanford University.
  8. References: <1992Oct16.171046.13025@cylink.COM>
  9. Date: Fri, 16 Oct 1992 20:00:45 GMT
  10. Lines: 100
  11.  
  12. Boy, talk about serendipity.  First we get someone who wants to see
  13. Farey sequences used somewhere:
  14.  
  15. In article <98687@bu.edu> rehman@math.bu.edu (Naved Rehman) writes:
  16. >are there any applications of FAREY SEQUENCE?
  17.  
  18. Then along comes a perfect customer for Farey sequences:
  19.  
  20. In article <1992Oct16.171046.13025@cylink.COM> tedh@cylink.COM (Ted Hadley) writes:
  21. >Greetings. I need an algorithm which I can code in 'C' which performs the
  22. >following task. This, I feel, is the best place to ask. If this is a common
  23. >request, please skip the details and cite a reference. Much appreciated. Here
  24. >goes:
  25. >
  26. >Part 1: General case:
  27. >
  28. >Given an real number X, compute to integers A and B such that A/B most
  29. >closely approximate X
  30.  
  31. So to make this post even more multithreaded let's suppose we want a
  32. rational approximation to pi.  Start out with the approximations 0/1
  33. and 1/0, guaranteed to be respectively lower and upper bounds on any
  34. nonnegative real.  Add their numerators and denominators to get 1/1,
  35. which being below pi becomes the new lower bound.  And so on until
  36. happy.  Formally (i.e. in C :-)
  37.  
  38.   main(){int a=0,b=1,c=1,d=0,e,f;
  39.   while (b < 1000) {
  40.     e = a+c, f = b+d;
  41.     if (e < f*3.14159265358979323846) a = e, b = f; else c = e, d = f;
  42.     printf("%d/%d ", e, f);}}
  43.  
  44. which yields 1/1 2/1 3/1 4/1 7/2 10/3 13/4 16/5 19/6 22/7 25/8 47/15
  45. 69/22 91/29 113/36 135/43 157/50 179/57 201/64 223/71 245/78 267/85
  46. 289/92 311/99 333/106 355/113 688/219 1043/332 1398/445 1753/558
  47. 2108/671 2463/784 2818/897 3173/1010.
  48.  
  49. The last of these is about 3.141584, good to 3 parts in a million (see
  50. the bit near the end of this post about expected errors and q^2).
  51.  
  52. >Part 2: First constraint:
  53. >
  54. >Repeat (1) such that the error, defined as the fractional part of A/(BX), is
  55. >within a certain bound (say +/- E) AND A and B are minimized (i.e., the
  56. >smallest possible values which produce a quotient with acceptible error).
  57.  
  58. Evidently e and f both grow monotonically, so by stopping as soon as we
  59. have a value lying within [pi-E,pi+E] we have automatically met your
  60. second condition that A and B are minimized (sorry I renamed them e and
  61. f).
  62.  
  63. >Part 3: Second contraint: (this is the preferred form)
  64. >
  65. >Given X, E, and Amax and Bmax, where Amax and Bmax are the maximum allowable
  66. >integers for A and B, respectively, compute all values of A and B for which
  67. >the error E is acceptible. For example: |A| < Amax and |B| < Bmax and some
  68. >value for E, list all possible {A, B} for which |fract(A/(BX))| < E.
  69.  
  70. What is really neat about the Farey sequence is that it generates every
  71. rational exactly once, in its reduced form, i.e. the complete Farey
  72. sequence is just the set Q of nonegative rationals, together with
  73. infinity, standardly ordered.  So to solve part 3, compute that part of
  74. the complete Farey sequence consisting of those rationals with
  75. numerators and denominators bounded as above and lying between the
  76. nearest rationals on each side of but not in [pi-E,pi+E].  (These two
  77. "outside" rationals are repeatedly replaced by progressively better
  78. approximations to pi-E below and pi+E above.) This gets you exactly
  79. those rationals that you ordered, namely those lying within your
  80. interval---the ones just outside the interval were needed only for the
  81. process, not the answer.
  82.  
  83. In thinking about all this, bear in mind that *any* consecutive pair
  84. a/b,c/d in the Farey sequence, however formed, satisfies
  85.  
  86.         |a  c|
  87.         |    |  =  -1               (*)
  88.         |b  d|
  89.  
  90. This is certainly true for the initial pair 0/1,1/0, and follows
  91. whenever e/f is interpolated since we are now looking at the
  92. determinants formed by adding the left column to the right and the
  93. right column to the left respectively.  So you can interpolate new
  94. entries in the Farey sequence in any order you like and nothing breaks
  95. and moreover no rational can ever become "locked out" by adding
  96. interpolants in an unfortunate order.
  97.  
  98. It also follows from (*) that (a,b) = (c,d) = 1, since otherwise the
  99. common factor would divide the determinant.  Moreover c/d-a/b =
  100. (bc-ad)/bd = 1/bd, telling you that consecutive finite elements of the
  101. Farey sequence up to a given bound q on the denominators are spaced at
  102. least 1/q^2 apart and at most 1/q apart (for the latter think about
  103. numbers like .000001, .499999, and .500001), giving you an idea of your
  104. prospects for rationally approximate a given real given such a bound q
  105. on the denominator.
  106.  
  107. For a *really* practical application of Farey sequences see the bottom
  108. of p.156, right column, of my paper on fast rendering of conic splines
  109. in Siggraph'85, which uses some of the above ideas.
  110. -- 
  111. Vaughan Pratt   pratt@cs.Stanford.EDU     exp(n pi sqrt(163)/3) for n=1..30
  112.