Skip to content

Commit 7a3076c

Browse files
author
Jake Moss
committed
cdef'd operands POC
1 parent 162ded1 commit 7a3076c

File tree

3 files changed

+112
-65
lines changed

3 files changed

+112
-65
lines changed

src/flint/flint_base/flint_base.pxd

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,30 @@ cdef class flint_mpoly_context(flint_elem):
1414
cdef const char ** c_names
1515

1616
cdef class flint_mpoly(flint_elem):
17-
pass
17+
cdef _add_scalar_(self, other)
18+
cdef _sub_scalar_(self, other)
19+
cdef _mul_scalar_(self, other)
20+
cdef _pow_(self, other)
21+
22+
cdef _add_mpoly_(self, other)
23+
cdef _sub_mpoly_(self, other)
24+
cdef _mul_mpoly_(self, other)
25+
26+
cdef _divmod_mpoly_(self, other)
27+
cdef _floordiv_mpoly_(self, other)
28+
cdef _truediv_mpoly_(self, other)
29+
cdef _mod_mpoly_(self, other)
30+
31+
cdef _rtruediv_mpoly_(self, other)
32+
33+
cdef _iadd_scalar_(self, other)
34+
cdef _isub_scalar_(self, other)
35+
cdef _imul_scalar_(self, other)
36+
37+
cdef _iadd_mpoly_(self, other)
38+
cdef _isub_mpoly_(self, other)
39+
cdef _imul_mpoly_(self, other)
40+
1841

1942
cdef class flint_mat(flint_elem):
2043
pass

src/flint/flint_base/flint_base.pyx

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,52 +405,58 @@ cdef class flint_mpoly(flint_elem):
405405
if not other:
406406
raise ZeroDivisionError("nmod_mpoly division by zero")
407407

408-
def _add_scalar_(self, other):
408+
cdef _add_scalar_(self, other):
409409
return NotImplemented
410410

411-
def _add_mpoly_(self, other):
411+
cdef _sub_scalar_(self, other):
412412
return NotImplemented
413413

414-
def _iadd_scalar_(self, other):
414+
cdef _mul_scalar_(self, other):
415415
return NotImplemented
416416

417-
def _iadd_mpoly_(self, other):
417+
cdef _pow_(self, other):
418418
return NotImplemented
419419

420-
def _sub_scalar_(self, other):
420+
cdef _add_mpoly_(self, other):
421421
return NotImplemented
422422

423-
def _sub_mpoly_(self, other):
423+
cdef _sub_mpoly_(self, other):
424424
return NotImplemented
425425

426-
def _isub_scalar_(self, other):
426+
cdef _mul_mpoly_(self, other):
427427
return NotImplemented
428428

429-
def _isub_mpoly_(self, other):
429+
cdef _divmod_mpoly_(self, other):
430430
return NotImplemented
431431

432-
def _mul_scalar_(self, other):
432+
cdef _floordiv_mpoly_(self, other):
433433
return NotImplemented
434434

435-
def _imul_mpoly_(self, other):
435+
cdef _truediv_mpoly_(self, other):
436436
return NotImplemented
437437

438-
def _imul_scalar_(self, other):
438+
cdef _rtruediv_mpoly_(self, other):
439439
return NotImplemented
440440

441-
def _mul_mpoly_(self, other):
441+
cdef _mod_mpoly_(self, other):
442442
return NotImplemented
443443

444-
def _pow_(self, other):
444+
cdef _iadd_scalar_(self, other):
445445
return NotImplemented
446446

447-
def _divmod_mpoly_(self, other):
447+
cdef _isub_scalar_(self, other):
448448
return NotImplemented
449449

450-
def _floordiv_mpoly_(self, other):
450+
cdef _imul_scalar_(self, other):
451451
return NotImplemented
452452

453-
def _truediv_mpoly_(self, other):
453+
cdef _iadd_mpoly_(self, other):
454+
return NotImplemented
455+
456+
cdef _isub_mpoly_(self, other):
457+
return NotImplemented
458+
459+
cdef _imul_mpoly_(self, other):
454460
return NotImplemented
455461

456462
def __add__(self, other):
@@ -628,7 +634,8 @@ cdef class flint_mpoly(flint_elem):
628634

629635
other = self.context().scalar_as_mpoly(other)
630636
other._division_check(self)
631-
return other._truediv_mpoly_(self)
637+
# return other._truediv_mpoly_(self)
638+
return self._rtruediv_mpoly_(other)
632639

633640
def __floordiv__(self, other):
634641
if typecheck(other, type(self)):

src/flint/types/fmpz_mpoly.pyx

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -326,77 +326,112 @@ cdef class fmpz_mpoly(flint_mpoly):
326326
fmpz_mpoly_neg(res.val, (<fmpz_mpoly>self).val, res.ctx.val)
327327
return res
328328

329-
def _add_scalar_(self, other: fmpz):
330-
cdef fmpz_mpoly res
329+
cdef _add_scalar_(self, arg):
330+
cdef:
331+
fmpz_mpoly res
332+
fmpz other = <fmpz>arg
331333
res = create_fmpz_mpoly(self.ctx)
332334
fmpz_mpoly_add_fmpz(res.val, self.val, other.val, self.ctx.val)
333335
return res
334336

335-
def _add_mpoly_(self, other: fmpz_mpoly):
336-
cdef fmpz_mpoly res
337+
cdef _sub_scalar_(self, arg):
338+
cdef:
339+
fmpz_mpoly res
340+
fmpz other = <fmpz>arg
337341
res = create_fmpz_mpoly(self.ctx)
338-
fmpz_mpoly_add(res.val, self.val, other.val, res.ctx.val)
342+
fmpz_mpoly_sub_fmpz(res.val, self.val, other.val, self.ctx.val)
339343
return res
340344

341-
def _sub_scalar_(self, other: fmpz):
342-
cdef fmpz_mpoly res
345+
cdef _mul_scalar_(self, arg):
346+
cdef:
347+
fmpz_mpoly res
348+
fmpz other = <fmpz>arg
343349
res = create_fmpz_mpoly(self.ctx)
344-
fmpz_mpoly_sub_fmpz(res.val, self.val, other.val, self.ctx.val)
350+
fmpz_mpoly_scalar_mul_fmpz(res.val, self.val, other.val, self.ctx.val)
345351
return res
346352

347-
def _sub_mpoly_(self, other: fmpz_mpoly):
348-
cdef fmpz_mpoly res
353+
cdef _pow_(self, arg):
354+
cdef:
355+
fmpz_mpoly res
356+
fmpz other = <fmpz>arg
349357
res = create_fmpz_mpoly(self.ctx)
350-
fmpz_mpoly_sub(res.val, self.val, other.val, res.ctx.val)
358+
if fmpz_mpoly_pow_fmpz(res.val, self.val, other.val, res.ctx.val) == 0:
359+
raise ValueError("unreasonably large polynomial") # pragma: no cover
351360
return res
352361

353-
def _mul_scalar_(self, other: fmpz):
354-
cdef fmpz_mpoly res
362+
cdef _add_mpoly_(self, arg):
363+
cdef fmpz_mpoly res, other = <fmpz_mpoly>arg
355364
res = create_fmpz_mpoly(self.ctx)
356-
fmpz_mpoly_scalar_mul_fmpz(res.val, self.val, other.val, self.ctx.val)
365+
fmpz_mpoly_add(res.val, self.val, other.val, res.ctx.val)
357366
return res
358367

359-
def _mul_mpoly_(self, other: fmpz_mpoly):
360-
cdef fmpz_mpoly res
368+
cdef _sub_mpoly_(self, arg):
369+
cdef fmpz_mpoly res, other = <fmpz_mpoly>arg
361370
res = create_fmpz_mpoly(self.ctx)
362-
fmpz_mpoly_mul(res.val, self.val, other.val, res.ctx.val)
371+
fmpz_mpoly_sub(res.val, self.val, other.val, res.ctx.val)
363372
return res
364373

365-
def _pow_(self, other: fmpz):
366-
cdef fmpz_mpoly res
374+
cdef _mul_mpoly_(self, arg):
375+
cdef fmpz_mpoly res, other = <fmpz_mpoly>arg
367376
res = create_fmpz_mpoly(self.ctx)
368-
if fmpz_mpoly_pow_fmpz(res.val, self.val, other.val, res.ctx.val) == 0:
369-
raise ValueError("unreasonably large polynomial") # pragma: no cover
377+
fmpz_mpoly_mul(res.val, self.val, other.val, res.ctx.val)
370378
return res
371379

372-
def _divmod_mpoly_(self, other: fmpz_mpoly):
373-
cdef fmpz_mpoly quotient, remainder
380+
cdef _divmod_mpoly_(self, arg):
381+
cdef fmpz_mpoly quotient, remainder, other = <fmpz_mpoly>arg
374382
quotient = create_fmpz_mpoly(self.ctx)
375383
remainder = create_fmpz_mpoly(self.ctx)
376384
fmpz_mpoly_divrem(quotient.val, remainder.val, self.val, other.val, self.ctx.val)
377385
return (quotient, remainder)
378386

379-
def _floordiv_mpoly_(self, other: fmpz_mpoly):
380-
cdef fmpz_mpoly quotient
387+
cdef _floordiv_mpoly_(self, arg):
388+
cdef fmpz_mpoly quotient, other = <fmpz_mpoly>arg
381389
quotient = create_fmpz_mpoly(self.ctx)
382390
fmpz_mpoly_div(quotient.val, self.val, other.val, self.ctx.val)
383391
return quotient
384392

385-
def _truediv_mpoly_(self, other: fmpz_mpoly):
386-
cdef fmpz_mpoly quotient
393+
cdef _truediv_mpoly_(self, arg):
394+
cdef fmpz_mpoly quotient, other = <fmpz_mpoly>arg
387395
quotient = create_fmpz_mpoly(self.ctx)
388396
if fmpz_mpoly_divides(quotient.val, self.val, other.val, self.ctx.val):
389397
return quotient
390398
else:
391399
raise DomainError("fmpz_mpoly division is not exact")
392400

393-
def _mod_mpoly_(self, other: fmpz_mpoly):
394-
cdef fmpz_mpoly quotient, remainder
401+
cdef _rtruediv_mpoly_(self, arg):
402+
return (<fmpz_mpoly>arg)._truediv_mpoly_(self)
403+
404+
cdef _mod_mpoly_(self, arg):
405+
cdef fmpz_mpoly quotient, remainder, other = <fmpz_mpoly>arg
395406
quotient = create_fmpz_mpoly(self.ctx)
396407
remainder = create_fmpz_mpoly(self.ctx)
397408
fmpz_mpoly_divrem(quotient.val, remainder.val, self.val, other.val, self.ctx.val)
398409
return remainder
399410

411+
cdef _iadd_scalar_(self, arg):
412+
cdef fmpz other = <fmpz>arg
413+
fmpz_mpoly_add_fmpz(self.val, self.val, other.val, self.ctx.val)
414+
415+
cdef _isub_scalar_(self, arg):
416+
cdef fmpz other = <fmpz>arg
417+
fmpz_mpoly_sub_fmpz(self.val, self.val, other.val, self.ctx.val)
418+
419+
cdef _imul_scalar_(self, arg):
420+
cdef fmpz other = <fmpz>arg
421+
fmpz_mpoly_scalar_mul_fmpz(self.val, self.val, other.val, self.ctx.val)
422+
423+
cdef _iadd_mpoly_(self, arg):
424+
cdef fmpz_mpoly other = <fmpz_mpoly>arg
425+
fmpz_mpoly_add(self.val, self.val, other.val, self.ctx.val)
426+
427+
cdef _isub_mpoly_(self, arg):
428+
cdef fmpz_mpoly other = <fmpz_mpoly>arg
429+
fmpz_mpoly_sub(self.val, self.val, other.val, self.ctx.val)
430+
431+
cdef _imul_mpoly_(self, arg):
432+
cdef fmpz_mpoly other = <fmpz_mpoly>arg
433+
fmpz_mpoly_mul(self.val, self.val, other.val, self.ctx.val)
434+
400435
def __call__(self, *args) -> fmpz:
401436
cdef:
402437
fmpz_vec V
@@ -412,24 +447,6 @@ cdef class fmpz_mpoly(flint_mpoly):
412447
raise ValueError("unreasonably large polynomial") # pragma: no cover
413448
return vres
414449

415-
def _iadd_scalar_(self, other: fmpz):
416-
fmpz_mpoly_add_fmpz(self.val, self.val, other.val, self.ctx.val)
417-
418-
def _iadd_mpoly_(self, other: fmpz_mpoly):
419-
fmpz_mpoly_add(self.val, self.val, other.val, self.ctx.val)
420-
421-
def _isub_scalar_(self, other: fmpz):
422-
fmpz_mpoly_sub_fmpz(self.val, self.val, other.val, self.ctx.val)
423-
424-
def _isub_mpoly_(self, other: fmpz_mpoly):
425-
fmpz_mpoly_sub(self.val, self.val, other.val, self.ctx.val)
426-
427-
def _imul_scalar_(self, other: fmpz):
428-
fmpz_mpoly_scalar_mul_fmpz(self.val, self.val, other.val, self.ctx.val)
429-
430-
def _imul_mpoly_(self, other: fmpz_mpoly):
431-
fmpz_mpoly_mul(self.val, self.val, other.val, self.ctx.val)
432-
433450
def monoms(self):
434451
"""
435452
Return the exponent vectors of each term as a tuple of fmpz.

0 commit comments

Comments
 (0)