11cdef arb_mat_coerce_operands(x, y):
2- if typecheck(x, arb_mat):
3- if isinstance (y, (fmpz_mat, fmpq_mat)):
4- return x, arb_mat(y)
5- if isinstance (y, (int , long , float , fmpz, fmpq, arb)):
6- return x, arb_mat(x.nrows(), x.ncols(), y)
7- if isinstance (y, (complex , acb)):
8- return acb_mat(x), acb_mat(x.nrows(), x.ncols(), y)
9- elif typecheck(y, arb_mat):
10- if isinstance (x, (fmpz_mat, fmpq_mat)):
11- return arb_mat(x), y
12- if isinstance (y, (int , long , float , fmpz, fmpq, arb)):
13- return arb_mat(y.nrows(), y.ncols(), x), y
14- if isinstance (y, (complex , acb)):
15- return acb_mat(y.nrows(), y.ncols(), x), acb_mat(y)
2+ if isinstance (y, (fmpz_mat, fmpq_mat)):
3+ return x, arb_mat(y)
4+ if isinstance (y, (int , long , float , fmpz, fmpq, arb)):
5+ return x, arb_mat(x.nrows(), x.ncols(), y)
6+ if isinstance (y, (complex , acb)):
7+ return acb_mat(x), acb_mat(x.nrows(), x.ncols(), y)
168 return NotImplemented , NotImplemented
179
1810cdef arb_mat_coerce_scalar(x, y):
@@ -216,35 +208,49 @@ cdef class arb_mat(flint_mat):
216208
217209 def __add__ (s , t ):
218210 cdef long m, n
219- if type (s) is type (t):
220- m = (< arb_mat> s).nrows()
221- n = (< arb_mat> s).ncols()
222- if m != (< arb_mat> t).nrows() or n != (< arb_mat> t).ncols():
223- raise ValueError (" incompatible shapes for matrix addition" )
224- u = arb_mat.__new__ (arb_mat)
225- arb_mat_init((< arb_mat> u).val, m, n)
226- arb_mat_add((< arb_mat> u).val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
227- return u
211+ if not isinstance (t, arb_mat):
212+ s, t = arb_mat_coerce_operands(s, t)
213+ if s is NotImplemented :
214+ return s
215+ return s + t
216+
217+ m = (< arb_mat> s).nrows()
218+ n = (< arb_mat> s).ncols()
219+ if m != (< arb_mat> t).nrows() or n != (< arb_mat> t).ncols():
220+ raise ValueError (" incompatible shapes for matrix addition" )
221+ u = arb_mat.__new__ (arb_mat)
222+ arb_mat_init((< arb_mat> u).val, m, n)
223+ arb_mat_add((< arb_mat> u).val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
224+ return u
225+
226+ def __radd__ (s , t ):
228227 s, t = arb_mat_coerce_operands(s, t)
229228 if s is NotImplemented :
230229 return s
231- return s + t
230+ return t + s
232231
233232 def __sub__ (s , t ):
234233 cdef long m, n
235- if type (s) is type (t):
236- m = (< arb_mat> s).nrows()
237- n = (< arb_mat> s).ncols()
238- if m != (< arb_mat> t).nrows() or n != (< arb_mat> t).ncols():
239- raise ValueError (" incompatible shapes for matrix addition" )
240- u = arb_mat.__new__ (arb_mat)
241- arb_mat_init((< arb_mat> u).val, m, n)
242- arb_mat_sub((< arb_mat> u).val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
243- return u
234+ if not isinstance (t, arb_mat):
235+ s, t = arb_mat_coerce_operands(s, t)
236+ if s is NotImplemented :
237+ return s
238+ return s - t
239+
240+ m = (< arb_mat> s).nrows()
241+ n = (< arb_mat> s).ncols()
242+ if m != (< arb_mat> t).nrows() or n != (< arb_mat> t).ncols():
243+ raise ValueError (" incompatible shapes for matrix addition" )
244+ u = arb_mat.__new__ (arb_mat)
245+ arb_mat_init((< arb_mat> u).val, m, n)
246+ arb_mat_sub((< arb_mat> u).val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
247+ return u
248+
249+ def __rsub__ (s , t ):
244250 s, t = arb_mat_coerce_operands(s, t)
245251 if s is NotImplemented :
246252 return s
247- return s - t
253+ return t - s
248254
249255 def _scalar_mul_ (s , arb t ):
250256 cdef arb_mat u
@@ -255,25 +261,31 @@ cdef class arb_mat(flint_mat):
255261
256262 def __mul__ (s , t ):
257263 cdef arb_mat u
258- if type (s) is type (t):
259- if arb_mat_ncols((< arb_mat> s).val) != arb_mat_nrows((< arb_mat> t).val):
260- raise ValueError (" incompatible shapes for matrix multiplication" )
261- u = arb_mat.__new__ (arb_mat)
262- arb_mat_init(u.val, arb_mat_nrows((< arb_mat> s).val), arb_mat_ncols((< arb_mat> t).val))
263- arb_mat_mul(u.val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
264- return u
265- if typecheck(s, arb_mat):
264+ if not isinstance (t, arb_mat):
266265 c, d = arb_mat_coerce_scalar(s, t)
267266 if c is not NotImplemented :
268267 return c._scalar_mul_(d)
269- else :
270- d, c = arb_mat_coerce_scalar(t, s)
271- if d is not NotImplemented :
272- return d._scalar_mul_(c)
268+ s, t = arb_mat_coerce_operands(s, t)
269+ if s is NotImplemented :
270+ return s
271+ return s * t
272+
273+ if arb_mat_ncols((< arb_mat> s).val) != arb_mat_nrows((< arb_mat> t).val):
274+ raise ValueError (" incompatible shapes for matrix multiplication" )
275+ u = arb_mat.__new__ (arb_mat)
276+ arb_mat_init(u.val, arb_mat_nrows((< arb_mat> s).val), arb_mat_ncols((< arb_mat> t).val))
277+ arb_mat_mul(u.val, (< arb_mat> s).val, (< arb_mat> t).val, getprec())
278+ return u
279+
280+ def __rmul__ (s , t ):
281+ cdef arb_mat u
282+ c, d = arb_mat_coerce_scalar(s, t)
283+ if c is not NotImplemented :
284+ return c._scalar_mul_(d)
273285 s, t = arb_mat_coerce_operands(s, t)
274286 if s is NotImplemented :
275287 return s
276- return s * t
288+ return t * s
277289
278290 def _scalar_div_ (s , arb t ):
279291 cdef arb_mat u
@@ -282,28 +294,17 @@ cdef class arb_mat(flint_mat):
282294 arb_mat_scalar_div_arb(u.val, s.val, t.val, getprec())
283295 return u
284296
285- @staticmethod
286- def _div_ (s , t ):
287- cdef arb_mat u
288- if typecheck(s, arb_mat):
289- s, t = arb_mat_coerce_scalar(s, t)
290- if s is NotImplemented :
291- return s
292- return s._scalar_div_(t)
293- return NotImplemented
294-
295297 def __truediv__ (s , t ):
296- return arb_mat._div_(s, t)
297-
298- def __div__ (s , t ):
299- return arb_mat._div_(s, t)
298+ cdef arb_mat u
299+ s, t = arb_mat_coerce_scalar(s, t)
300+ if s is NotImplemented :
301+ return s
302+ return s._scalar_div_(t)
300303
301304 def __pow__ (s , e , m ):
302305 cdef arb_mat u
303306 cdef ulong exp
304307 cdef long n
305- if not typecheck(s, arb_mat):
306- return NotImplemented
307308 exp = e
308309 n = arb_mat_nrows((< arb_mat> s).val)
309310 if n != arb_mat_ncols((< arb_mat> s).val):
0 commit comments