From 23b261bd41c19c42adb67a517dfdfdd1f2a49b43 Mon Sep 17 00:00:00 2001 From: GDeLaurentis Date: Tue, 26 Mar 2024 14:40:22 +0000 Subject: [PATCH] Implemented PAdic.as_tuple_from_zero, following suggestion. It raises ValueError if not a p-adic integer (negative valuation). --- pyadic/padic.py | 8 ++++++++ tests/test_finite_field.py | 4 +++- tests/test_padic.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pyadic/padic.py b/pyadic/padic.py index 25424fc..1a90398 100644 --- a/pyadic/padic.py +++ b/pyadic/padic.py @@ -173,8 +173,16 @@ def k(self, value): @property def as_tuple(self): + """Tuple reprensentation of the mantissa.""" return (to_base(int(self), self.p) + tuple([0 for i in range(self.k)]))[:self.k] + @property + def as_tuple_from_zero(self): + """Tuple representation of the mantissa, shifted to start from p^0 term.""" + if self.n < 0: + raise ValueError("PAdic tuple from zero representation is only defined for PAdic integers (non-negative valuation).") + return (0, ) * self.n + self.as_tuple + def __getstate__(self): return (int(self), self.p, self.k, self.n) diff --git a/tests/test_finite_field.py b/tests/test_finite_field.py index 3fa4003..35bce4a 100644 --- a/tests/test_finite_field.py +++ b/tests/test_finite_field.py @@ -30,8 +30,10 @@ def test_same_class_instantiation_and_unary_plus(): def test_instantiation_from_padic(): assert ModP(PAdic(123, 2 ** 31 - 19, 3)) == ModP(123, (2 ** 31 - 19) ** 3) + def test_instantiation_from_string(): - assert ModP('+123', 2 ** 31 - 1) == ModP('-2147483524', 2 ** 31 - 1) == ModP(123, 2 ** 31 - 1) + assert ModP('+123', 2 ** 31 - 1) == ModP('-2147483524', 2 ** 31 - 1) == ModP(123, 2 ** 31 - 1) + def test_instantiation_with_complex(): p = 2 ** 31 - 19 diff --git a/tests/test_padic.py b/tests/test_padic.py index 00ca67c..788ec8b 100644 --- a/tests/test_padic.py +++ b/tests/test_padic.py @@ -43,6 +43,14 @@ def test_str_non_zero(): assert str(PAdic(1 + 2 * p + 3 * p ** 2, p, k)) == "1 + 2*{p} + 3*{p}^2 + O({p}^{k})".format(p=p, k=k) +def test_tuple_from_zero_representation(): + a = PAdic(64, 2, 12) + assert a.as_tuple_from_zero == (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ) + a = PAdic(Q(1, 2), 2, 12) + with pytest.raises(ValueError): + a.as_tuple_from_zero + + def test_isscalar(): assert numpy.isscalar(PAdic(3, 7, 1))