@@ -114,10 +114,15 @@ def applyfunc(self, func) -> "Poly":
114
114
def _check_type (self , other ):
115
115
return isinstance (other , int ) or (isinstance (other , Number ) and isinstance (self .coeff [0 ], Number )) or type (other ) == type (self .coeff [0 ])
116
116
117
- def _guess_zero (self ):
117
+ def _guess_ring (self ):
118
118
zero = 0 * self .coeff [0 ]
119
- one = zero ** 0
120
- return zero , one
119
+ try :
120
+ ring = type (zero )
121
+ one = ring (1 )
122
+ except :
123
+ ring = None
124
+ one = zero ** 0
125
+ return zero , one , ring
121
126
122
127
def __add__ (self , other : "Poly" ) -> "Poly" :
123
128
zero = 0 * self .coeff [0 ]
@@ -225,9 +230,9 @@ def __mod__(self, other: "Poly") -> "Poly":
225
230
226
231
def divmod (self , other : "Poly" ) -> ("Poly" , "Poly" ):
227
232
"Polynom division with remainder."
228
- zero , one = self ._guess_zero ()
233
+ zero , one , ring = self ._guess_ring ()
229
234
if isinstance (other , list ):
230
- other = self .__class__ (other )
235
+ other = self .__class__ (other , ring = ring )
231
236
elif not isinstance (other , self .__class__ ):
232
237
raise NotImplementedError (f"Cannot divide { self } and { other } ." )
233
238
if not other :
@@ -255,9 +260,9 @@ def divmod(self, other: "Poly") -> ("Poly", "Poly"):
255
260
256
261
def mod (self , other : "Poly" ) -> None :
257
262
"Reduce with respect to a given polynomial."
258
- one = self ._guess_zero ()[1 ]
263
+ one , ring = self ._guess_ring ()[1 : ]
259
264
if isinstance (other , list ):
260
- other = self .__class__ (other )
265
+ other = self .__class__ (other , ring = ring )
261
266
elif not isinstance (other , self .__class__ ):
262
267
raise NotImplementedError (f"Cannot divide { self } and { other } ." )
263
268
if not other :
@@ -284,9 +289,9 @@ def inv(self, other: "Poly" = None) -> "Poly":
284
289
"Inverse modulo a given polynomial."
285
290
if not other :
286
291
other = self .modulus
287
- zero , one = self ._guess_zero ()
292
+ zero , one , ring = self ._guess_ring ()
288
293
if isinstance (other , list ):
289
- other = self .__class__ (other )
294
+ other = self .__class__ (other , ring = ring )
290
295
elif not isinstance (other , self .__class__ ):
291
296
raise NotImplementedError (f"Cannot invert { self } modulo { other } ." )
292
297
if not other :
0 commit comments