Skip to content

Commit be94896

Browse files
author
Release Manager
committed
Trac #32990: pickling support for real balls and related objects
URL: https://trac.sagemath.org/32990 Reported by: mmezzarobba Ticket author(s): Marc Mezzarobba Reviewer(s): Matthias Koeppe
2 parents 81cc1a8 + fb82012 commit be94896

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/sage/libs/arb/arb.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ cdef extern from "arb_wrap.h":
4242
void arb_printd(const arb_t x, long digits)
4343
void arb_printn(const arb_t x, long digits, unsigned long flags)
4444

45+
char *arb_dump_str(const arb_t x)
46+
bint arb_load_str(arb_t x, const char *str)
47+
4548
# void arb_randtest(arb_t x, flint_rand_t state, long prec, long mag_bits)
4649
# void arb_randtest_exact(arb_t x, flint_rand_t state, long prec, long mag_bits)
4750
# void arb_randtest_precise(arb_t x, flint_rand_t state, long prec, long mag_bits)

src/sage/matrix/matrix_complex_ball_dense.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ TESTS::
1818
sage: pol(mat)
1919
[8.000000000000000 11.00000000000000]
2020
[22.00000000000000 41.00000000000000]
21+
22+
sage: mat = matrix(ComplexBallField(20), 2, 2, list(range(4)))*i/3
23+
sage: loads(dumps(mat)).identical(mat)
24+
True
2125
"""
2226

2327
#*****************************************************************************

src/sage/rings/complex_arb.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ class ComplexBallField(UniqueRepresentation, sage.rings.abc.ComplexBallField):
331331
332332
sage: ComplexBallField().is_finite()
333333
False
334+
335+
sage: loads(dumps(ComplexBallField(60))) is ComplexBallField(60)
336+
True
334337
"""
335338
Element = ComplexBall
336339

@@ -1487,6 +1490,18 @@ cdef class ComplexBall(RingElement):
14871490
return "{} + {}*I".format(self.real()._repr_(),
14881491
self.imag()._repr_())
14891492

1493+
def __reduce__(self):
1494+
r"""
1495+
Serialize a ComplexBall.
1496+
1497+
TESTS::
1498+
1499+
sage: [loads(dumps(b)).identical(b) for b in
1500+
....: [ComplexBallField(60)(1/3 + i*pi), CBF(NaN)]]
1501+
[True, True]
1502+
"""
1503+
return self.__class__, (self._parent, self.real(), self.imag())
1504+
14901505
def _is_atomic(self):
14911506
r"""
14921507
Declare that complex balls print atomically in some cases.

src/sage/rings/polynomial/polynomial_complex_arb.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ cdef class Polynomial_complex_arb(Polynomial):
184184
ball = Coeff(x)
185185
acb_poly_set_coeff_acb(self.__poly, 0, ball.value)
186186

187+
def __reduce__(self):
188+
r"""
189+
Serialize a polynomial for pickling.
190+
191+
TESTS::
192+
193+
sage: Pol.<x> = ComplexBallField(42)[]
194+
sage: pol = (x + i)/3
195+
sage: pol2 = loads(dumps(pol))
196+
sage: pol.degree() == pol2.degree()
197+
True
198+
sage: all(a.identical(b) for (a, b) in zip(pol, pol2))
199+
True
200+
"""
201+
return (self.__class__,
202+
(self.parent(), self.list(), False, self.is_gen()))
203+
187204
# Access
188205

189206
def degree(self):

src/sage/rings/real_arb.pyx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ class RealBallField(UniqueRepresentation, sage.rings.abc.RealBallField):
369369
370370
sage: RealBallField().is_finite()
371371
False
372+
373+
sage: loads(dumps(RealBallField(60))) is RealBallField(60)
374+
True
372375
"""
373376
Element = RealBall
374377

@@ -1148,6 +1151,30 @@ cdef inline bint _do_sig(long prec):
11481151
cdef inline long prec(RealBall ball):
11491152
return ball._parent._prec
11501153

1154+
def create_RealBall(parent, serialized):
1155+
r"""
1156+
Create a RealBall from a serialized representation.
1157+
1158+
TESTS::
1159+
1160+
sage: from sage.rings.real_arb import create_RealBall
1161+
sage: create_RealBall(RBF, b'15555555555555 -36 1 -36')
1162+
[0.3333333333333333 +/- 7.04e-17]
1163+
sage: create_RealBall(RBF, b'foo')
1164+
Traceback (most recent call last):
1165+
...
1166+
ValueError: incorrect format
1167+
"""
1168+
cdef RealBall res = RealBall.__new__(RealBall)
1169+
res._parent = parent
1170+
sig_on()
1171+
cdef bint error = arb_load_str(res.value, serialized)
1172+
sig_off()
1173+
if error:
1174+
raise ValueError("incorrect format")
1175+
else:
1176+
return res
1177+
11511178
cdef class RealBall(RingElement):
11521179
"""
11531180
Hold one ``arb_t`` of the `Arb library
@@ -1494,6 +1521,26 @@ cdef class RealBall(RingElement):
14941521

14951522
return py_string
14961523

1524+
def __reduce__(self):
1525+
r"""
1526+
Serialize a RealBall.
1527+
1528+
TESTS::
1529+
1530+
sage: [loads(dumps(b)).identical(b) for b in
1531+
....: [RealBallField(60).pi(), RBF(infinity), RBF(NaN)]]
1532+
[True, True, True]
1533+
"""
1534+
cdef bytes py_val
1535+
sig_on()
1536+
cdef char* c_val = arb_dump_str(self.value)
1537+
sig_off()
1538+
try:
1539+
py_val = <bytes> c_val
1540+
finally:
1541+
flint_free(c_val)
1542+
return create_RealBall, (self._parent, py_val)
1543+
14971544
# Conversions
14981545

14991546
cpdef RealIntervalFieldElement _real_mpfi_(self, RealIntervalField_class parent):

0 commit comments

Comments
 (0)