Thursday, 21 May 2015

farey sequences - How to compute next/previous representable rational number?



An (approximate) non-negative rational number representation is a pair of natural numbers each not greater than some fixed limit M (and of course denominator being non-zero).




With this condition there is finite number of representable rational numbers. This means that for each such number we can name previous and next number in the set (of course except for smallest 0 and largest 1). How to compute them?



In other words lets have a set of numbers



R(M)={pq:p,qN,q0,pqM}



where MN+




and given number p1q1R(M).



I want to find numbers pSqSR(M) and pLqLR(M) (if they exist) such that



pSqS<p1q1¬pqR(M)pSqS<pq<p1q1



and smilarly




pLqL>p1q1¬pqR(M)p1q1<pq<pLqL


Answer



All this needs is a single modular inversion, with complexity O(logMloglogM) (or something like that).



We are given p/q in lowest terms, with p,qN and q>0. We want the nearest neighbours of p/q, a/b<p/q<c/d, subject to a,b,c,dN and c,d>0. Assume for the moment that p<q.



Consider c/d. It is the element after p/q in the Farey sequence Fn, which means that cqdp=1. In particular, dp+1 = 0 \pmod q, i.e. d = -1/p \pmod q. So let




r = 1/p \pmod q



Now make d as big as possible, subject to (i) d = -r \pmod q and (ii) d \leq n. Then just put c = (dp+1)/q.



Similarly, to calculate a/b, make b as big as possible subject to (i) b = +r \pmod q and (ii) b \leq n. Then put a = (bp-1)/q.



An example Suppose p/q = 28/61 and n=100. Then the inverse of 28 \pmod{61} is r=24. So d = 37 \pmod{61}, and we can take d=98, with c=(98.28+1)/61 = 45. And b = 24 \pmod{61}, so we can take b = 85, with a = (85.28-1)/61=39. So we get:



39/85 < 28/61 < 45/98




We only have to modify this slightly to handle the case p > q: instead of computing the largest d \leq n that satisfies the modulo requirement, we find the corresponding modulo requirement for c, and then look for the largest such c \leq n. You can fill in the details for yourself.


No comments:

Post a Comment

real analysis - How to find lim_{hrightarrow 0}frac{sin(ha)}{h}

How to find \lim_{h\rightarrow 0}\frac{\sin(ha)}{h} without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...