Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Help with unstable code

This basically continues from a previous post: generation of certain matrices

I'm trying to draw circles in the plane by using fractional linear transformations.

Here is my code:

K = NumberField(x^2 + 2, 's')
OK = K.ring_of_integers()
def FLT(M,z):
"""takes the fractional linear transformation/Mobius transformation of a z in CC"""
    if z == 'infinity' and M[1,0] != 0:
        return M[0,0]/M[1,0]
    elif z == 'infinity' and M[1,0] == 0:
        return 'infinity'
    elif M[1,0] != 0 and z == -M[1,1]/M[1,0]:
        return 'infinity'
    else:
        return (M[0,0]*z+M[0,1])/(M[1,0]*z + M[1,1])

Now generate circles based on 3 image points of a matrix M with entries in the ring OK via FLT:

var('a','b','r','x','y')
func_cir=(x-a)**2+(y-b)**2==r**2
circles = set()
j = 0
while j < 10:
    M = random_matrix(OK,2,algorithm = 'unimodular')
    if FLT(M,-1) != 'infinity' and FLT(M,0) != 'infinity' and FLT(M,1) != 'infinity':
    pta=[FLT(M,-1)[0],FLT(M,-1)[1]]
    ptb=[FLT(M,0)[0],FLT(M,0)[1]]
    ptc=[FLT(M,1)[0],FLT(M,1)[1]]    
    eq1 = func_cir.subs(x==pta[0]).subs(y==pta[1])
    eq2 = func_cir.subs(x==ptb[0]).subs(y==ptb[1])
    eq3 = func_cir.subs(x==ptc[0]).subs(y==ptc[1])
    sol = solve([eq1,eq2,eq3],a,b,r)
    C = circle((sol[1][0].rhs(),sol[1][1].rhs()),sol[1][2].rhs())    
    circles.add(C)
    j += 1

This second chunk of code throws the error "list index out of range" about 50% of the time. I thought that it may have been due to division by zero, but now I'm not so sure. I've also tried using a for loop with the exact same result. Thank you very much for the help!