\(p\)-adic Printing

This file contains code for printing \(p\)-adic elements.

It has been moved here to prevent code duplication and make finding the relevant code easier.

AUTHORS:

  • David Roe

sage.rings.padics.padic_printing.pAdicPrinter(ring, options={})[source]

Create a pAdicPrinter.

INPUT:

  • ring – a \(p\)-adic ring or field

  • options – dictionary, with keys in 'mode', 'pos', 'ram_name', 'unram_name', 'var_name', 'max_ram_terms', 'max_unram_terms', 'max_terse_terms', 'sep', 'alphabet'; see pAdicPrinter_class for the meanings of these keywords.

EXAMPLES:

sage: from sage.rings.padics.padic_printing import pAdicPrinter
sage: R = Zp(5)
sage: pAdicPrinter(R, {'sep': '&'})
series printer for 5-adic Ring with capped relative precision 20
>>> from sage.all import *
>>> from sage.rings.padics.padic_printing import pAdicPrinter
>>> R = Zp(Integer(5))
>>> pAdicPrinter(R, {'sep': '&'})
series printer for 5-adic Ring with capped relative precision 20
from sage.rings.padics.padic_printing import pAdicPrinter
R = Zp(5)
pAdicPrinter(R, {'sep': '&'})
class sage.rings.padics.padic_printing.pAdicPrinterDefaults(mode='series', pos=True, max_ram_terms=-1, max_unram_terms=-1, max_terse_terms=-1, sep='|', alphabet=None)[source]

Bases: SageObject

This class stores global defaults for \(p\)-adic printing.

allow_negatives(neg=None)[source]

Controls whether or not to display a balanced representation.

neg=None returns the current value.

EXAMPLES:

sage: padic_printing.allow_negatives(True)
sage: padic_printing.allow_negatives()
True
sage: Qp(29)(-1)
-1 + O(29^20)
sage: Qp(29)(-1000)
-14 - 5*29 - 29^2 + O(29^20)
sage: padic_printing.allow_negatives(False)
>>> from sage.all import *
>>> padic_printing.allow_negatives(True)
>>> padic_printing.allow_negatives()
True
>>> Qp(Integer(29))(-Integer(1))
-1 + O(29^20)
>>> Qp(Integer(29))(-Integer(1000))
-14 - 5*29 - 29^2 + O(29^20)
>>> padic_printing.allow_negatives(False)
padic_printing.allow_negatives(True)
padic_printing.allow_negatives()
Qp(29)(-1)
Qp(29)(-1000)
padic_printing.allow_negatives(False)
alphabet(alphabet=None)[source]

Controls the alphabet used to translate \(p\)-adic digits into strings (so that no separator need be used in 'digits' mode).

alphabet should be passed in as a list or tuple.

alphabet=None returns the current value.

EXAMPLES:

sage: padic_printing.alphabet("abc")
sage: padic_printing.mode('digits')
sage: repr(Qp(3)(1234))
'...bcaacab'

sage: padic_printing.mode('series')
sage: padic_printing.alphabet(('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'))
>>> from sage.all import *
>>> padic_printing.alphabet("abc")
>>> padic_printing.mode('digits')
>>> repr(Qp(Integer(3))(Integer(1234)))
'...bcaacab'

>>> padic_printing.mode('series')
>>> padic_printing.alphabet(('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'))
padic_printing.alphabet("abc")
padic_printing.mode('digits')
repr(Qp(3)(1234))
padic_printing.mode('series')
padic_printing.alphabet(('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'))
max_poly_terms(max=None)[source]

Controls the number of terms appearing when printing polynomial representations in 'terse' or 'val-unit' modes.

max=None returns the current value.

max=-1 encodes ‘no limit.’

EXAMPLES:

sage: padic_printing.max_poly_terms(3)
sage: padic_printing.max_poly_terms()
3
sage: padic_printing.mode('terse')
sage: Zq(7^5, 5, names='a')([2,3,4])^8                                      # needs sage.libs.ntl
2570 + 15808*a + 9018*a^2 + ... + O(7^5)

sage: padic_printing.max_poly_terms(-1)
sage: padic_printing.mode('series')
>>> from sage.all import *
>>> padic_printing.max_poly_terms(Integer(3))
>>> padic_printing.max_poly_terms()
3
>>> padic_printing.mode('terse')
>>> Zq(Integer(7)**Integer(5), Integer(5), names='a')([Integer(2),Integer(3),Integer(4)])**Integer(8)                                      # needs sage.libs.ntl
2570 + 15808*a + 9018*a^2 + ... + O(7^5)

>>> padic_printing.max_poly_terms(-Integer(1))
>>> padic_printing.mode('series')
padic_printing.max_poly_terms(3)
padic_printing.max_poly_terms()
padic_printing.mode('terse')
Zq(7^5, 5, names='a')([2,3,4])^8                                      # needs sage.libs.ntl
padic_printing.max_poly_terms(-1)
padic_printing.mode('series')
max_series_terms(max=None)[source]

Controls the maximum number of terms shown when printing in 'series', 'digits' or 'bars' mode.

max=None returns the current value.

max=-1 encodes ‘no limit.’

EXAMPLES:

sage: padic_printing.max_series_terms(2)
sage: padic_printing.max_series_terms()
2
sage: Qp(31)(1000)
8 + 31 + ... + O(31^20)
sage: padic_printing.max_series_terms(-1)
sage: Qp(37)(100000)
26 + 37 + 36*37^2 + 37^3 + O(37^20)
>>> from sage.all import *
>>> padic_printing.max_series_terms(Integer(2))
>>> padic_printing.max_series_terms()
2
>>> Qp(Integer(31))(Integer(1000))
8 + 31 + ... + O(31^20)
>>> padic_printing.max_series_terms(-Integer(1))
>>> Qp(Integer(37))(Integer(100000))
26 + 37 + 36*37^2 + 37^3 + O(37^20)
padic_printing.max_series_terms(2)
padic_printing.max_series_terms()
Qp(31)(1000)
padic_printing.max_series_terms(-1)
Qp(37)(100000)
max_unram_terms(max=None)[source]

For rings with non-prime residue fields, controls how many terms appear in the coefficient of each pi^n when printing in 'series' or 'bar' modes.

max=None returns the current value.

max=-1 encodes ‘no limit.’

EXAMPLES:

sage: padic_printing.max_unram_terms(2)
sage: padic_printing.max_unram_terms()
2
sage: Zq(5^6, 5, names='a')([1,2,3,-1])^17                                  # needs sage.libs.ntl
(3*a^4 + ... + 3) + (a^5 + ... + a)*5 + (3*a^3 + ... + 2)*5^2 + (3*a^5 + ... + 2)*5^3 + (4*a^5 + ... + 4)*5^4 + O(5^5)

sage: padic_printing.max_unram_terms(-1)
>>> from sage.all import *
>>> padic_printing.max_unram_terms(Integer(2))
>>> padic_printing.max_unram_terms()
2
>>> Zq(Integer(5)**Integer(6), Integer(5), names='a')([Integer(1),Integer(2),Integer(3),-Integer(1)])**Integer(17)                                  # needs sage.libs.ntl
(3*a^4 + ... + 3) + (a^5 + ... + a)*5 + (3*a^3 + ... + 2)*5^2 + (3*a^5 + ... + 2)*5^3 + (4*a^5 + ... + 4)*5^4 + O(5^5)

>>> padic_printing.max_unram_terms(-Integer(1))
padic_printing.max_unram_terms(2)
padic_printing.max_unram_terms()
Zq(5^6, 5, names='a')([1,2,3,-1])^17                                  # needs sage.libs.ntl
padic_printing.max_unram_terms(-1)
mode(mode=None)[source]

Set the default printing mode.

mode=None returns the current value.

The allowed values for mode are: 'val-unit', 'series', 'terse', 'digits' and 'bars'.

EXAMPLES:

sage: padic_printing.mode('terse')
sage: padic_printing.mode()
'terse'
sage: Qp(7)(100)
100 + O(7^20)
sage: padic_printing.mode('series')
sage: Qp(11)(100)
1 + 9*11 + O(11^20)
sage: padic_printing.mode('val-unit')
sage: Qp(13)(130)
13 * 10 + O(13^21)
sage: padic_printing.mode('digits')
sage: repr(Qp(17)(100))
'...5F'
sage: repr(Qp(17)(1000))
'...37E'
sage: padic_printing.mode('bars')
sage: repr(Qp(19)(1000))
'...2|14|12'

sage: padic_printing.mode('series')
>>> from sage.all import *
>>> padic_printing.mode('terse')
>>> padic_printing.mode()
'terse'
>>> Qp(Integer(7))(Integer(100))
100 + O(7^20)
>>> padic_printing.mode('series')
>>> Qp(Integer(11))(Integer(100))
1 + 9*11 + O(11^20)
>>> padic_printing.mode('val-unit')
>>> Qp(Integer(13))(Integer(130))
13 * 10 + O(13^21)
>>> padic_printing.mode('digits')
>>> repr(Qp(Integer(17))(Integer(100)))
'...5F'
>>> repr(Qp(Integer(17))(Integer(1000)))
'...37E'
>>> padic_printing.mode('bars')
>>> repr(Qp(Integer(19))(Integer(1000)))
'...2|14|12'

>>> padic_printing.mode('series')
padic_printing.mode('terse')
padic_printing.mode()
Qp(7)(100)
padic_printing.mode('series')
Qp(11)(100)
padic_printing.mode('val-unit')
Qp(13)(130)
padic_printing.mode('digits')
repr(Qp(17)(100))
repr(Qp(17)(1000))
padic_printing.mode('bars')
repr(Qp(19)(1000))
padic_printing.mode('series')
sep(sep=None)[source]

Controls the separator used in 'bars' mode.

sep=None returns the current value.

EXAMPLES:

sage: padic_printing.sep('][')
sage: padic_printing.sep()
']['
sage: padic_printing.mode('bars')
sage: repr(Qp(61)(-1))
'...60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60'

sage: padic_printing.sep('|')
sage: padic_printing.mode('series')
>>> from sage.all import *
>>> padic_printing.sep('][')
>>> padic_printing.sep()
']['
>>> padic_printing.mode('bars')
>>> repr(Qp(Integer(61))(-Integer(1)))
'...60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60][60'

>>> padic_printing.sep('|')
>>> padic_printing.mode('series')
padic_printing.sep('][')
padic_printing.sep()
padic_printing.mode('bars')
repr(Qp(61)(-1))
padic_printing.sep('|')
padic_printing.mode('series')
class sage.rings.padics.padic_printing.pAdicPrinter_class[source]

Bases: SageObject

This class stores the printing options for a specific \(p\)-adic ring or field, and uses these to compute the representations of elements.

dict()[source]

Return a dictionary storing all of self’s printing options.

EXAMPLES:

sage: D = Zp(5)._printer.dict(); D['sep']
'|'
>>> from sage.all import *
>>> D = Zp(Integer(5))._printer.dict(); D['sep']
'|'
D = Zp(5)._printer.dict(); D['sep']
repr_gen(elt, do_latex, pos=None, mode=None, ram_name=None)[source]

The entry point for printing an element.

INPUT:

  • elt – a \(p\)-adic element of the appropriate ring to print

  • do_latex – whether to return a latex representation or a normal one

EXAMPLES:

sage: R = Zp(5,5); P = R._printer; a = R(-5); a
4*5 + 4*5^2 + 4*5^3 + 4*5^4 + 4*5^5 + O(5^6)
sage: P.repr_gen(a, False, pos=False)
'-5 + O(5^6)'
sage: P.repr_gen(a, False, ram_name='p')
'4*p + 4*p^2 + 4*p^3 + 4*p^4 + 4*p^5 + O(p^6)'
>>> from sage.all import *
>>> R = Zp(Integer(5),Integer(5)); P = R._printer; a = R(-Integer(5)); a
4*5 + 4*5^2 + 4*5^3 + 4*5^4 + 4*5^5 + O(5^6)
>>> P.repr_gen(a, False, pos=False)
'-5 + O(5^6)'
>>> P.repr_gen(a, False, ram_name='p')
'4*p + 4*p^2 + 4*p^3 + 4*p^4 + 4*p^5 + O(p^6)'
R = Zp(5,5); P = R._printer; a = R(-5); a
P.repr_gen(a, False, pos=False)
P.repr_gen(a, False, ram_name='p')
richcmp_modes(other, op)[source]

Return a comparison of the printing modes of self and other.

Return 0 if and only if all relevant modes are equal (max_unram_terms is irrelevant if the ring is totally ramified over the base, for example). This does not check if the rings are equal (to prevent infinite recursion in the comparison functions of \(p\)-adic rings), but it does check if the primes are the same (since the prime affects whether pos is relevant).

EXAMPLES:

sage: R = Qp(7, print_mode='digits', print_pos=True)
sage: S = Qp(7, print_mode='digits', print_pos=False)
sage: R._printer == S._printer
True
sage: R = Qp(7)
sage: S = Qp(7, print_mode='val-unit')
sage: R == S
False
sage: R._printer < S._printer
True
>>> from sage.all import *
>>> R = Qp(Integer(7), print_mode='digits', print_pos=True)
>>> S = Qp(Integer(7), print_mode='digits', print_pos=False)
>>> R._printer == S._printer
True
>>> R = Qp(Integer(7))
>>> S = Qp(Integer(7), print_mode='val-unit')
>>> R == S
False
>>> R._printer < S._printer
True
R = Qp(7, print_mode='digits', print_pos=True)
S = Qp(7, print_mode='digits', print_pos=False)
R._printer == S._printer
R = Qp(7)
S = Qp(7, print_mode='val-unit')
R == S
R._printer < S._printer