@@ -58,22 +58,10 @@ def __init_subclass__(cls, *args: tuple, **kwargs: dict):
5858
5959 def _make (self , a : int , b : int ):
6060 """Construct a new value of *this* conceptual type from internal numerators a,b."""
61- # x, y = self._internal_to_basis(a, b)
62- # return self.__class__(x, y, self.ring)
63- # The above is what this method should be.
64- # However doing that would require going to the basis vector just to undo it in __init__.
65- # This code is an effort to avoid that and make this method a bit more efficient.
66- cls = type (self )
67- obj = cls .__new__ (cls )
68- obj .ring = self .ring
69- obj .a = a
70- obj .b = b
71-
72- den = self .ring .den
73- if den == 2 and ((a ^ b ) & 1 ):
74- raise ValueError ("For den=2, a and b must have the same parity" )
75-
76- return obj
61+ # TODO: This basically converts to basis values just to convert back again in __init__...
62+ # However mypyc is NOT happy about me using __new__ to bypass that.
63+ x , y = self ._internal_to_basis (a , b )
64+ return self .__class__ (x , y , self .ring )
7765
7866 @property
7967 def zero (self ) -> QuadInt :
@@ -154,27 +142,32 @@ def conjugate(self):
154142 return self ._make (self .a , - self .b )
155143
156144 # region Basis vector operations
145+ @classmethod
146+ def _internal_to_basis (cls , a : int , b : int ) -> tuple [int , int ]:
147+ """Convert from internal a and b coords to basis coords"""
148+ (n00 , n01 ), (n10 , n11 ) = cls .INTERNAL_TO_BASIS
149+ den = cls .INTERNAL_TO_BASIS_DEN
150+ x_num = n00 * a + n01 * b
151+ y_num = n10 * a + n11 * b
152+
153+ if x_num % den or y_num % den :
154+ raise ArithmeticError ("Internal coordinates do not map cleanly to declared basis" )
155+
156+ return x_num // den , y_num // den
157+
157158 @classmethod
158159 def _basis_to_internal (cls , x : int , y : int ) -> tuple [int , int ]:
159160 """Convert from basis coords to internal a and b coords"""
160161 (m00 , m01 ), (m10 , m11 ) = cls .BASIS_TO_INTERNAL
161162 return m00 * x + m01 * y , m10 * x + m11 * y
162163
163164 @property
164- def basis (self ):
165+ def basis (self ) -> tuple [ int , int ] :
165166 """The number in the basis vector"""
166- (n00 , n01 ), (n10 , n11 ) = self .INTERNAL_TO_BASIS
167- den = self .INTERNAL_TO_BASIS_DEN
168- x_num = n00 * self .a + n01 * self .b
169- y_num = n10 * self .a + n11 * self .b
170-
171- if x_num % den or y_num % den :
172- raise ArithmeticError ("Internal coordinates do not map cleanly to declared basis" )
173-
174- return x_num // den , y_num // den
167+ return self ._internal_to_basis (self .a , self .b )
175168
176169 @property
177- def basis_a (self ):
170+ def basis_a (self ) -> int :
178171 """a in the basis vector""" # noqa: D403
179172 (n00 , n01 ), _ = self .INTERNAL_TO_BASIS
180173 den = self .INTERNAL_TO_BASIS_DEN
@@ -183,7 +176,7 @@ def basis_a(self):
183176 return x_num // den
184177
185178 @property
186- def basis_b (self ):
179+ def basis_b (self ) -> int :
187180 """b in the basis vector""" # noqa: D403
188181 _ , (n10 , n11 ) = self .INTERNAL_TO_BASIS
189182 den = self .INTERNAL_TO_BASIS_DEN
0 commit comments