Skip to content

Commit 070b7e5

Browse files
committed
fix: add __rdunder__ methods for arb and acb
1 parent d5f0a61 commit 070b7e5

File tree

3 files changed

+152
-116
lines changed

3 files changed

+152
-116
lines changed

src/flint/acb.pyx

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,18 @@ cdef class acb(flint_scalar):
156156
return (self.real._mpf_, self.imag._mpf_)
157157

158158
def __richcmp__(s, t, int op):
159-
cdef acb_struct sval[1]
160159
cdef acb_struct tval[1]
161160
cdef bint res
162-
cdef int stype, ttype
161+
cdef int ttype
163162
if not (op == 2 or op == 3):
164163
raise ValueError("comparing complex numbers")
165-
stype = acb_set_any_ref(sval, s)
166-
if stype == FMPZ_UNKNOWN:
167-
return NotImplemented
168164
ttype = acb_set_any_ref(tval, t)
169165
if ttype == FMPZ_UNKNOWN:
170166
return NotImplemented
171167
if op == 2:
172-
res = acb_eq(sval, tval)
168+
res = acb_eq(s.val, tval)
173169
else:
174-
res = acb_ne(sval, tval)
175-
if stype == FMPZ_TMP: acb_clear(sval)
170+
res = acb_ne(s.val, tval)
176171
if ttype == FMPZ_TMP: acb_clear(tval)
177172
return res
178173

@@ -363,92 +358,114 @@ cdef class acb(flint_scalar):
363358
return res
364359

365360
def __add__(s, t):
366-
cdef acb_struct sval[1]
367361
cdef acb_struct tval[1]
368-
cdef int stype, ttype
369-
stype = acb_set_any_ref(sval, s)
370-
if stype == FMPZ_UNKNOWN:
362+
cdef int ttype
363+
ttype = acb_set_any_ref(tval, t)
364+
if ttype == FMPZ_UNKNOWN:
371365
return NotImplemented
366+
u = acb.__new__(acb)
367+
acb_add((<acb>u).val, s.val, tval, getprec())
368+
if ttype == FMPZ_TMP: acb_clear(tval)
369+
return u
370+
371+
def __radd__(s, t):
372+
cdef acb_struct tval[1]
373+
cdef int ttype
372374
ttype = acb_set_any_ref(tval, t)
373375
if ttype == FMPZ_UNKNOWN:
374376
return NotImplemented
375377
u = acb.__new__(acb)
376-
acb_add((<acb>u).val, sval, tval, getprec())
377-
if stype == FMPZ_TMP: acb_clear(sval)
378+
acb_add((<acb>u).val, tval, s.val, getprec())
378379
if ttype == FMPZ_TMP: acb_clear(tval)
379380
return u
380381

381382
def __sub__(s, t):
382-
cdef acb_struct sval[1]
383383
cdef acb_struct tval[1]
384-
cdef int stype, ttype
385-
stype = acb_set_any_ref(sval, s)
386-
if stype == FMPZ_UNKNOWN:
384+
cdef int ttype
385+
ttype = acb_set_any_ref(tval, t)
386+
if ttype == FMPZ_UNKNOWN:
387387
return NotImplemented
388+
u = acb.__new__(acb)
389+
acb_sub((<acb>u).val, s.val, tval, getprec())
390+
if ttype == FMPZ_TMP: acb_clear(tval)
391+
return u
392+
393+
def __rsub__(s, t):
394+
cdef acb_struct tval[1]
395+
cdef int ttype
388396
ttype = acb_set_any_ref(tval, t)
389397
if ttype == FMPZ_UNKNOWN:
390398
return NotImplemented
391399
u = acb.__new__(acb)
392-
acb_sub((<acb>u).val, sval, tval, getprec())
393-
if stype == FMPZ_TMP: acb_clear(sval)
400+
acb_sub((<acb>u).val, tval, s.val, getprec())
394401
if ttype == FMPZ_TMP: acb_clear(tval)
395402
return u
396403

397404
def __mul__(s, t):
398-
cdef acb_struct sval[1]
399405
cdef acb_struct tval[1]
400-
cdef int stype, ttype
401-
stype = acb_set_any_ref(sval, s)
402-
if stype == FMPZ_UNKNOWN:
403-
return NotImplemented
406+
cdef int ttype
404407
ttype = acb_set_any_ref(tval, t)
405408
if ttype == FMPZ_UNKNOWN:
406409
return NotImplemented
407410
u = acb.__new__(acb)
408-
acb_mul((<acb>u).val, sval, tval, getprec())
409-
if stype == FMPZ_TMP: acb_clear(sval)
411+
acb_mul((<acb>u).val, s.val, tval, getprec())
410412
if ttype == FMPZ_TMP: acb_clear(tval)
411413
return u
412414

413-
# important: must not be cdef because of cython magic
414-
@staticmethod
415-
def _div_(s, t):
416-
cdef acb_struct sval[1]
415+
def __rmul__(s, t):
417416
cdef acb_struct tval[1]
418-
cdef int stype, ttype
419-
stype = acb_set_any_ref(sval, s)
420-
if stype == FMPZ_UNKNOWN:
421-
return NotImplemented
417+
cdef int ttype
422418
ttype = acb_set_any_ref(tval, t)
423419
if ttype == FMPZ_UNKNOWN:
424420
return NotImplemented
425421
u = acb.__new__(acb)
426-
acb_div((<acb>u).val, sval, tval, getprec())
427-
if stype == FMPZ_TMP: acb_clear(sval)
422+
acb_mul((<acb>u).val, tval, s.val, getprec())
428423
if ttype == FMPZ_TMP: acb_clear(tval)
429424
return u
430425

431426
def __truediv__(s, t):
432-
return acb._div_(s, t)
427+
cdef acb_struct tval[1]
428+
cdef int ttype
429+
ttype = acb_set_any_ref(tval, t)
430+
if ttype == FMPZ_UNKNOWN:
431+
return NotImplemented
432+
u = acb.__new__(acb)
433+
acb_div((<acb>u).val, s.val, tval, getprec())
434+
if ttype == FMPZ_TMP: acb_clear(tval)
435+
return u
433436

434-
def __div__(s, t):
435-
return acb._div_(s, t)
437+
def __rtruediv__(s, t):
438+
cdef acb_struct tval[1]
439+
cdef int ttype
440+
ttype = acb_set_any_ref(tval, t)
441+
if ttype == FMPZ_UNKNOWN:
442+
return NotImplemented
443+
u = acb.__new__(acb)
444+
acb_div((<acb>u).val, tval, s.val, getprec())
445+
if ttype == FMPZ_TMP: acb_clear(tval)
446+
return u
436447

437448
def __pow__(s, t, u):
438-
cdef acb_struct sval[1]
439449
cdef acb_struct tval[1]
440-
cdef int stype, ttype
450+
cdef int ttype
441451
if u is not None:
442452
raise ValueError("modular exponentiation of complex number")
443-
stype = acb_set_any_ref(sval, s)
444-
if stype == FMPZ_UNKNOWN:
453+
ttype = acb_set_any_ref(tval, t)
454+
if ttype == FMPZ_UNKNOWN:
445455
return NotImplemented
456+
u = acb.__new__(acb)
457+
acb_pow((<acb>u).val, s.val, tval, getprec())
458+
if ttype == FMPZ_TMP: acb_clear(tval)
459+
return u
460+
461+
def __rpow__(s, t):
462+
cdef acb_struct tval[1]
463+
cdef int ttype
446464
ttype = acb_set_any_ref(tval, t)
447465
if ttype == FMPZ_UNKNOWN:
448466
return NotImplemented
449467
u = acb.__new__(acb)
450-
acb_pow((<acb>u).val, sval, tval, getprec())
451-
if stype == FMPZ_TMP: acb_clear(sval)
468+
acb_pow((<acb>u).val, tval, s.val, getprec())
452469
if ttype == FMPZ_TMP: acb_clear(tval)
453470
return u
454471

@@ -2560,4 +2577,3 @@ cdef class acb(flint_scalar):
25602577
acb_hypgeom_coulomb(NULL, (<acb>G).val, NULL, NULL,
25612578
(<acb>l).val, (<acb>eta).val, (<acb>self).val, getprec())
25622579
return G
2563-

src/flint/arb.pyx

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,6 @@ cdef any_as_arb_or_notimplemented(x):
116116
return NotImplemented
117117
return t
118118

119-
cdef _arb_div_(s, t):
120-
cdef arb_struct sval[1]
121-
cdef arb_struct tval[1]
122-
cdef int stype, ttype
123-
stype = arb_set_any_ref(sval, s)
124-
if stype == FMPZ_UNKNOWN:
125-
return NotImplemented
126-
ttype = arb_set_any_ref(tval, t)
127-
if ttype == FMPZ_UNKNOWN:
128-
return NotImplemented
129-
u = arb.__new__(arb)
130-
arb_div((<arb>u).val, sval, tval, getprec())
131-
if stype == FMPZ_TMP: arb_clear(sval)
132-
if ttype == FMPZ_TMP: arb_clear(tval)
133-
return u
134-
135119
cdef class arb(flint_scalar):
136120
ur"""
137121
Represents a real number `x` by a midpoint `m` and a radius `r`
@@ -550,74 +534,116 @@ cdef class arb(flint_scalar):
550534
return res
551535

552536
def __add__(s, t):
553-
cdef arb_struct sval[1]
554537
cdef arb_struct tval[1]
555-
cdef int stype, ttype
556-
stype = arb_set_any_ref(sval, s)
557-
if stype == FMPZ_UNKNOWN:
538+
cdef int ttype
539+
ttype = arb_set_any_ref(tval, t)
540+
if ttype == FMPZ_UNKNOWN:
558541
return NotImplemented
542+
u = arb.__new__(arb)
543+
arb_add((<arb>u).val, s.val, tval, getprec())
544+
if ttype == FMPZ_TMP: arb_clear(tval)
545+
return u
546+
547+
def __radd__(s, t):
548+
cdef arb_struct tval[1]
549+
cdef int ttype
559550
ttype = arb_set_any_ref(tval, t)
560551
if ttype == FMPZ_UNKNOWN:
561552
return NotImplemented
562553
u = arb.__new__(arb)
563-
arb_add((<arb>u).val, sval, tval, getprec())
564-
if stype == FMPZ_TMP: arb_clear(sval)
554+
arb_add((<arb>u).val, tval, s.val, getprec())
565555
if ttype == FMPZ_TMP: arb_clear(tval)
566556
return u
567557

568558
def __sub__(s, t):
569-
cdef arb_struct sval[1]
570559
cdef arb_struct tval[1]
571-
cdef int stype, ttype
572-
stype = arb_set_any_ref(sval, s)
573-
if stype == FMPZ_UNKNOWN:
560+
cdef int ttype
561+
ttype = arb_set_any_ref(tval, t)
562+
if ttype == FMPZ_UNKNOWN:
574563
return NotImplemented
564+
u = arb.__new__(arb)
565+
arb_sub((<arb>u).val, s.val, tval, getprec())
566+
if ttype == FMPZ_TMP: arb_clear(tval)
567+
return u
568+
569+
def __rsub__(s, t):
570+
cdef arb_struct tval[1]
571+
cdef int ttype
575572
ttype = arb_set_any_ref(tval, t)
576573
if ttype == FMPZ_UNKNOWN:
577574
return NotImplemented
578575
u = arb.__new__(arb)
579-
arb_sub((<arb>u).val, sval, tval, getprec())
580-
if stype == FMPZ_TMP: arb_clear(sval)
576+
arb_sub((<arb>u).val, tval, s.val, getprec())
581577
if ttype == FMPZ_TMP: arb_clear(tval)
582578
return u
583579

584580
def __mul__(s, t):
585-
cdef arb_struct sval[1]
586581
cdef arb_struct tval[1]
587-
cdef int stype, ttype
588-
stype = arb_set_any_ref(sval, s)
589-
if stype == FMPZ_UNKNOWN:
582+
cdef int ttype
583+
ttype = arb_set_any_ref(tval, t)
584+
if ttype == FMPZ_UNKNOWN:
590585
return NotImplemented
586+
u = arb.__new__(arb)
587+
arb_mul((<arb>u).val, s.val, tval, getprec())
588+
if ttype == FMPZ_TMP: arb_clear(tval)
589+
return u
590+
591+
def __rmul__(s, t):
592+
cdef arb_struct tval[1]
593+
cdef int ttype
591594
ttype = arb_set_any_ref(tval, t)
592595
if ttype == FMPZ_UNKNOWN:
593596
return NotImplemented
594597
u = arb.__new__(arb)
595-
arb_mul((<arb>u).val, sval, tval, getprec())
596-
if stype == FMPZ_TMP: arb_clear(sval)
598+
arb_mul((<arb>u).val, tval, s.val, getprec())
597599
if ttype == FMPZ_TMP: arb_clear(tval)
598600
return u
599601

600602
def __truediv__(s, t):
601-
return _arb_div_(s, t)
603+
cdef arb_struct tval[1]
604+
cdef int ttype
605+
ttype = arb_set_any_ref(tval, t)
606+
if ttype == FMPZ_UNKNOWN:
607+
return NotImplemented
608+
u = arb.__new__(arb)
609+
arb_div((<arb>u).val, s.val, tval, getprec())
610+
if ttype == FMPZ_TMP: arb_clear(tval)
611+
return u
602612

603-
def __div__(s, t):
604-
return _arb_div_(s, t)
613+
def __rtruediv__(s, t):
614+
cdef arb_struct tval[1]
615+
cdef int ttype
616+
ttype = arb_set_any_ref(tval, t)
617+
if ttype == FMPZ_UNKNOWN:
618+
return NotImplemented
619+
u = arb.__new__(arb)
620+
arb_div((<arb>u).val, tval, s.val, getprec())
621+
if ttype == FMPZ_TMP: arb_clear(tval)
622+
return u
605623

606624
def __pow__(s, t, modulus):
607-
cdef arb_struct sval[1]
608625
cdef arb_struct tval[1]
609-
cdef int stype, ttype
626+
cdef int ttype
610627
if modulus is not None:
611628
raise TypeError("three-argument pow() not supported by arb type")
612-
stype = arb_set_any_ref(sval, s)
613-
if stype == FMPZ_UNKNOWN:
629+
ttype = arb_set_any_ref(tval, t)
630+
if ttype == FMPZ_UNKNOWN:
614631
return NotImplemented
632+
u = arb.__new__(arb)
633+
arb_pow((<arb>u).val, s.val, tval, getprec())
634+
if ttype == FMPZ_TMP: arb_clear(tval)
635+
return u
636+
637+
def __rpow__(s, t, modulus):
638+
cdef arb_struct tval[1]
639+
cdef int ttype
640+
if modulus is not None:
641+
raise TypeError("three-argument pow() not supported by arb type")
615642
ttype = arb_set_any_ref(tval, t)
616643
if ttype == FMPZ_UNKNOWN:
617644
return NotImplemented
618645
u = arb.__new__(arb)
619-
arb_pow((<arb>u).val, sval, tval, getprec())
620-
if stype == FMPZ_TMP: arb_clear(sval)
646+
arb_pow((<arb>u).val, tval, s.val, getprec())
621647
if ttype == FMPZ_TMP: arb_clear(tval)
622648
return u
623649

@@ -2421,4 +2447,3 @@ cdef class arb(flint_scalar):
24212447
arb_hypgeom_coulomb(NULL, (<arb>G).val,
24222448
(<arb>l).val, (<arb>eta).val, (<arb>self).val, getprec())
24232449
return G
2424-

0 commit comments

Comments
 (0)