I'm working with Sagemath for an in-depth project for the first time. I'm experiencing what I perceive to be some very strange behavior and I'm totally stumped on resolving it. I have a class that is encoding some geometric objects. It includes the following:
def __init__(self, A, B, C):
self.A = A
self.B = B
self.C = C
if A != 0:
self.center = -conjugate(B)
self.radius = abs(sqrt(abs(self.center)^2 - C))
elif A == 0:
self.center = None
self.radius = None
This works perfectly fine for relatively simple inputs. The strange thing is that when self.center and/or C get sufficiently complicated, I get an error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-34-f3d291115bf1> in <module>()
2 print(c1)
3 m1 = c1.mobToRealLine()
----> 4 cccc = m1.imageCircle(c1)
5 print(cccc)
<ipython-input-33-df6c59aba5fd> in imageCircle(self, circle)
52 print("new A, B, C = {}, {}, {}".format(newA, newB, newC))
53
---> 54 return Circle(newA, newB, newC)
55
56 class Circle:
<ipython-input-33-df6c59aba5fd> in __init__(self, A, B, C)
64 if A != Integer(0):
65 self.center = -conjugate(B)
---> 66 self.radius = abs(sqrt(abs(self.center)**Integer(2) - C))
67 elif A == Integer(0):
68 self.center = None
/Applications/SageMath/local/lib/python2.7/site-packages/sage/functions/other.pyc in sqrt(x, *args, **kwds)
869 return sqrt(x)
870 try:
--> 871 return x.sqrt(*args, **kwds)
872 # The following includes TypeError to catch cases where sqrt
873 # is called with a "prec" keyword, for example, but the sqrt
/Applications/SageMath/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.sqrt (build/cythonized/sage/symbolic/expression.cpp:44675)()
8075 """
8076 return new_Expression_from_GEx(self._parent,
-> 8077 g_hold2_wrapper(g_power_construct, self._gobj, g_ex1_2, hold))
8078
8079 def sin(self, hold=False):
RuntimeError: unsupported type in numeric::lcm
The error occurs when it's trying to initialize a the class with
A, B, C, = 1/2*abs(sqrt(16) - I - 1)^2/sqrt(16) + (1/2*I + 1/2)*(sqrt(16) + I - 1)/sqrt(16) - (1/2*I - 1/2)*(sqrt(16) - I - 1)/sqrt(16) - 7/sqrt(16),1/2*I*(sqrt(16) - I + 1)*(sqrt(16) - I - 1)/sqrt(16) + (1/2*I - 1/2)*(sqrt(16) - I + 1)/sqrt(16) - (1/2*I + 1/2)*(sqrt(16) - I - 1)/sqrt(16) + 7*I/sqrt(16),1/2*abs(sqrt(16) + I + 1)^2/sqrt(16) + (1/2*I - 1/2)*(sqrt(16) + I + 1)/sqrt(16) - (1/2*I + 1/2)*(sqrt(16) - I + 1)/sqrt(16) - 7/sqrt(16)
If I (by hand) initialize the class with these values of A, B, and C, it works just fine. If, on the other hand, I use the "imagecircle" method that I've written (which is implemented as follows. prints are there for debugging), it fails, despite the fact that the A, B, and C given by the imagecircle method are exactly as above.
def imageCircle(self, circle):
"""Return the image of circle under self."""
A, B, C = circle.A, circle.B, circle.C
cA, cB, cC = conjugate(A), conjugate(B), conjugate(C)
a, b, c, d = self.a, self.b, self.c, self.d
ca, cb, cc, cd = conjugate(a), conjugate(b), conjugate(c), conjugate(d)
newA = A*abs(d)^2 - cc*d*B - c*cd*cB + C*abs(c)^2
newB = -A*cb*d + ca*d*B + cb*c*cB - ca*c*C
newC = A*abs(b)^2 - ca*b*B - a*cb*cB + abs(a)^2*C
print("new A, B, C = {}, {}, {}".format(newA, newB, newC))
return Circle(newA, newB, newC) #leads to error when running __init__ in class Circle