Skip to content

Commit

Permalink
Implemented PAdic.as_tuple_from_zero, following suggestion. It raises…
Browse files Browse the repository at this point in the history
… ValueError if not a p-adic integer (negative valuation).
  • Loading branch information
GDeLaurentis committed Mar 26, 2024
1 parent 557474c commit 23b261b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pyadic/padic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_finite_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_padic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 23b261b

Please sign in to comment.