Factory - $p$-adics

Skip to content
Back to top
View this page
Edit this page

# Factory

This file contains the constructor classes and functions for \(p\)-adic rings and fields.

AUTHORS:

- David Roe

sage.rings.padics.factory.QpCR(p, prec=None, *args, **kwds)

  A shortcut function to create capped relative \(p\)-adic fields.

  Same functionality as `Qp()`. See documentation for `Qp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: QpCR(5, 40)
      5-adic Field with capped relative precision 40

  Python

      >>> from sage.all import *
      >>> QpCR(Integer(5), Integer(40))
      5-adic Field with capped relative precision 40

sage.rings.padics.factory.QpER(p, prec=None, halt=None, secure=False, *args, **kwds)

  A shortcut function to create relaxed \(p\)-adic fields.

  See `ZpER()` for more information about this model of precision.

  EXAMPLES:

  Sage

      sage: R = QpER(2); R
      2-adic Field handled with relaxed arithmetics

  Python

      >>> from sage.all import *
      >>> R = QpER(Integer(2)); R
      2-adic Field handled with relaxed arithmetics

sage.rings.padics.factory.QpFP(p, prec=None, *args, **kwds)

  A shortcut function to create floating point \(p\)-adic fields.

  Same functionality as `Qp()`. See documentation for `Qp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: QpFP(5, 40)
      5-adic Field with floating precision 40

  Python

      >>> from sage.all import *
      >>> QpFP(Integer(5), Integer(40))
      5-adic Field with floating precision 40

sage.rings.padics.factory.QpLC(p, prec=None, *args, **kwds)

  A shortcut function to create \(p\)-adic fields with lattice precision.

  See `ZpLC()` for more information about this model of precision.

  EXAMPLES:

  Sage

      sage: R = QpLC(2)
      sage: R
      2-adic Field with lattice-cap precision

  Python

      >>> from sage.all import *
      >>> R = QpLC(Integer(2))
      >>> R
      2-adic Field with lattice-cap precision

sage.rings.padics.factory.QpLF(p, prec=None, *args, **kwds)

  A shortcut function to create \(p\)-adic fields with lattice precision.

  See `ZpLC()` for more information about this model of precision.

  EXAMPLES:

  Sage

      sage: R = QpLF(2)
      sage: R
      2-adic Field with lattice-float precision

  Python

      >>> from sage.all import *
      >>> R = QpLF(Integer(2))
      >>> R
      2-adic Field with lattice-float precision

class sage.rings.padics.factory.Qp_class

  Bases: `UniqueFactory`

  A creation function for \(p\)-adic fields.

  INPUT:

  - `p` – integer; the \(p\) in \(\QQ_p\)

  - `prec` – integer (default: 20); the precision cap of the field. In the lattice capped case, `prec` can either be a pair (`relative_cap`, `absolute_cap`) or an integer (understood at relative cap). In the relaxed case, `prec` can be either a pair (`default_prec`, `halting_prec`) or an integer (understood at default precision). Except in the floating point case, individual elements keep track of their own precision. See TYPES and PRECISION below.

  - `type` – string (default: `'capped-rel'`); valid types are `'capped-rel'`, `'floating-point'`, `'lattice-cap'`, `'lattice-float'`. See TYPES and PRECISION below.

  - `print_mode` – string (default: `None`); valid modes are `'series'`, `'val-unit'`, `'terse'`, `'digits'`, and `'bars'`. See PRINTING below.

  - `names` – string or tuple (defaults to a string representation of \(p\)); what to use whenever \(p\) is printed

  - `ram_name` – string; another way to specify the name. For consistency with the `Qq` and `Zq` and extension functions.

  - `print_pos` – boolean (default: `None`); whether to only use positive integers in the representations of elements. See PRINTING below.

  - `print_sep` – string (default: `None`); the separator character used in the `'bars'` mode. See PRINTING below.

  - `print_alphabet` – tuple (default: `None`); the encoding into digits for use in the ‘digits’ mode. See PRINTING below.

  - `print_max_terms` – integer (default: `None`); the maximum number of terms shown. See PRINTING below.

  - `show_prec` – boolean or a string (default: `None`); specify how the precision is printed. See PRINTING below.

  - `check` – boolean (default: `True`); whether to check if \(p\) is prime. Non-prime input may cause seg-faults (but can also be useful for base n expansions for example).

  - `label` – string (default: `None`); used for lattice precision to create parents with different lattices

  OUTPUT: the corresponding \(p\)-adic field

  TYPES AND PRECISION:

  There are two main types of precision for a \(p\)-adic element. The first is relative precision, which gives the number of known \(p\)-adic digits:

  Sage

      sage: R = Qp(5, 20, 'capped-rel', 'series'); a = R(675); a
      2*5^2 + 5^4 + O(5^22)
      sage: a.precision_relative()
      20

  Python

      >>> from sage.all import *
      >>> R = Qp(Integer(5), Integer(20), 'capped-rel', 'series'); a = R(Integer(675)); a
      2*5^2 + 5^4 + O(5^22)
      >>> a.precision_relative()
      20

  The second type of precision is absolute precision, which gives the power of \(p\) that this element is defined modulo:

  Sage

      sage: a.precision_absolute()
      22

  [Python]

      >>> from sage.all import *
      >>> a.precision_absolute()
      22

  There are several types of \(p\)-adic fields, depending on the methods used for tracking precision. Namely, we have:

  - capped relative fields (`type='capped-rel'`)

  - capped absolute fields (`type='capped-abs'`)

  - fixed modulus fields (`type='fixed-mod'`)

  - floating point fields (`type='floating-point'`)

  - lattice precision fields (`type='lattice-cap'` or `type='lattice-float'`)

  - exact fields with relaxed arithmetics (`type='relaxed'`)

  In the capped relative case, the relative precision of an element is restricted to be at most a certain value, specified at the creation of the field. Individual elements also store their own precision, so the effect of various arithmetic operations on precision is tracked. When you cast an exact element into a capped relative field, it truncates it to the precision cap of the field.

  Sage

      sage: R = Qp(5, 5, 'capped-rel', 'series'); a = R(4006); a
      1 + 5 + 2*5^3 + 5^4 + O(5^5)
      sage: b = R(4025); b
      5^2 + 2*5^3 + 5^4 + 5^5 + O(5^7)
      sage: a + b
      1 + 5 + 5^2 + 4*5^3 + 2*5^4 + O(5^5)

  Python

      >>> from sage.all import *
      >>> R = Qp(Integer(5), Integer(5), 'capped-rel', 'series'); a = R(Integer(4006)); a
      1 + 5 + 2*5^3 + 5^4 + O(5^5)
      >>> b = R(Integer(4025)); b
      5^2 + 2*5^3 + 5^4 + 5^5 + O(5^7)
      >>> a + b
      1 + 5 + 5^2 + 4*5^3 + 2*5^4 + O(5^5)

  In the floating point case, elements do not track their precision, but the relative precision of elements is truncated during arithmetic to the precision cap of the field.

  In the lattice case, precision on elements is tracked by a global lattice that is updated after every operation, yielding better precision behavior at the cost of higher memory and runtime usage. We refer to the documentation of the function `ZpLC()` for a small demonstration of the capabilities of this precision model.

  Finally, the model for relaxed \(p\)-adics is quite different from any of the other types. In addition to storing a finite approximation, one also stores a method for increasing the precision. A quite interesting feature with relaxed \(p\)-adics is the possibility to create (in some cases) self-referent numbers, that are numbers whose \(n\)-th digit is defined by the previous ones. We refer to the documentation of the function `ZpL()` for a small demonstration of the capabilities of this precision model.

  PRINTING:

  There are many different ways to print \(p\)-adic elements. The way elements of a given field print is controlled by options passed in at the creation of the field. There are five basic printing modes (series, val-unit, terse, digits and bars), as well as various options that either hide some information in the print representation or sometimes make print representations more compact. Note that the printing options affect whether different \(p\)-adic fields are considered equal.

  1. series: elements are displayed as series in \(p\).

     Sage

         sage: R = Qp(5, print_mode='series'); a = R(70700); a
         3*5^2 + 3*5^4 + 2*5^5 + 4*5^6 + O(5^22)
         sage: b = R(-70700); b
         2*5^2 + 4*5^3 + 5^4 + 2*5^5 + 4*5^7 + 4*5^8 + 4*5^9 + 4*5^10 + 4*5^11
          + 4*5^12 + 4*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 4*5^17 + 4*5^18
          + 4*5^19 + 4*5^20 + 4*5^21 + O(5^22)

     Python

         >>> from sage.all import *
         >>> R = Qp(Integer(5), print_mode='series'); a = R(Integer(70700)); a
         3*5^2 + 3*5^4 + 2*5^5 + 4*5^6 + O(5^22)
         >>> b = R(-Integer(70700)); b
         2*5^2 + 4*5^3 + 5^4 + 2*5^5 + 4*5^7 + 4*5^8 + 4*5^9 + 4*5^10 + 4*5^11
          + 4*5^12 + 4*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 4*5^17 + 4*5^18
          + 4*5^19 + 4*5^20 + 4*5^21 + O(5^22)

  > print_pos controls whether negatives can be used in the coefficients of powers of \(p\).

  > [Sage]

  >     sage: S = Qp(5, print_mode='series', print_pos=False); a = S(70700); a
  >     -2*5^2 + 5^3 - 2*5^4 - 2*5^5 + 5^7 + O(5^22)
  >     sage: b = S(-70700); b
  >     2*5^2 - 5^3 + 2*5^4 + 2*5^5 - 5^7 + O(5^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Qp(Integer(5), print_mode='series', print_pos=False); a = S(Integer(70700)); a
  >     -2*5^2 + 5^3 - 2*5^4 - 2*5^5 + 5^7 + O(5^22)
  >     >>> b = S(-Integer(70700)); b
  >     2*5^2 - 5^3 + 2*5^4 + 2*5^5 - 5^7 + O(5^22)

  > print_max_terms limits the number of terms that appear.

  > [Sage]

  >     sage: T = Qp(5, print_mode='series', print_max_terms=4); b = R(-70700); repr(b)
  >     '2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Qp(Integer(5), print_mode='series', print_max_terms=Integer(4)); b = R(-Integer(70700)); repr(b)
  >     '2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)'

  > names affects how the prime is printed.

  > [Sage]

  >     sage: U.<p> = Qp(5); p
  >     p + O(p^21)

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Qp(Integer(5), names=('p',)); (p,) = U._first_ngens(1); p
  >     p + O(p^21)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`) or ‘bigoh’. The default is `False` for the `'floating-point'` type and `True` for all other types.

  > [Sage]

  >     sage: Qp(5)(6)
  >     1 + 5 + O(5^20)
  >     sage: Qp(5, show_prec='none')(6)
  >     1 + 5

  >     sage: QpFP(5)(6)
  >     1 + 5

  > [Python]

  >     >>> from sage.all import *
  >     >>> Qp(Integer(5))(Integer(6))
  >     1 + 5 + O(5^20)
  >     >>> Qp(Integer(5), show_prec='none')(Integer(6))
  >     1 + 5

  >     >>> QpFP(Integer(5))(Integer(6))
  >     1 + 5

  > print_sep and print_alphabet have no effect in series mode.

  > Note that print options affect equality:

  > [Sage]

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  2. val-unit: elements are displayed as p^k*u:

     Sage

         sage: R = Qp(5, print_mode='val-unit'); a = R(70700); a
         5^2 * 2828 + O(5^22)
         sage: b = R(-707/5); b
         5^-1 * 95367431639918 + O(5^19)

     Python

         >>> from sage.all import *
         >>> R = Qp(Integer(5), print_mode='val-unit'); a = R(Integer(70700)); a
         5^2 * 2828 + O(5^22)
         >>> b = R(-Integer(707)/Integer(5)); b
         5^-1 * 95367431639918 + O(5^19)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S = Qp(5, print_mode='val-unit', print_pos=False); b = S(-70700); b
  >     5^2 * (-2828) + O(5^22)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Qp(Integer(5), print_mode='val-unit', print_pos=False); b = S(-Integer(70700)); b
  >     5^2 * (-2828) + O(5^22)

  > names affects how the prime is printed.

  > Sage

  >     sage: T = Qp(5, print_mode='val-unit', names='pi'); a = T(70700); a
  >     pi^2 * 2828 + O(pi^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Qp(Integer(5), print_mode='val-unit', names='pi'); a = T(Integer(70700)); a
  >     pi^2 * 2828 + O(pi^22)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`) or ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: Qp(5, print_mode='val-unit', show_prec=False)(30)
  >     5 * 6

  > Python

  >     >>> from sage.all import *
  >     >>> Qp(Integer(5), print_mode='val-unit', show_prec=False)(Integer(30))
  >     5 * 6

  > print_max_terms, print_sep and print_alphabet have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  3. terse: elements are displayed as an integer in base 10 or the quotient of an integer by a power of \(p\) (still in base 10):

     Sage

         sage: R = Qp(5, print_mode='terse'); a = R(70700); a
         70700 + O(5^22)
         sage: b = R(-70700); b
         2384185790944925 + O(5^22)
         sage: c = R(-707/5); c
         95367431639918/5 + O(5^19)

     Python

         >>> from sage.all import *
         >>> R = Qp(Integer(5), print_mode='terse'); a = R(Integer(70700)); a
         70700 + O(5^22)
         >>> b = R(-Integer(70700)); b
         2384185790944925 + O(5^22)
         >>> c = R(-Integer(707)/Integer(5)); c
         95367431639918/5 + O(5^19)

  > The denominator, as of version 3.3, is always printed explicitly as a power of \(p\), for predictability.

  > Sage

  >     sage: d = R(707/5^2); d
  >     707/5^2 + O(5^18)

  > Python

  >     >>> from sage.all import *
  >     >>> d = R(Integer(707)/Integer(5)**Integer(2)); d
  >     707/5^2 + O(5^18)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S = Qp(5, print_mode='terse', print_pos=False); b = S(-70700); b
  >     -70700 + O(5^22)
  >     sage: c = S(-707/5); c
  >     -707/5 + O(5^19)

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Qp(Integer(5), print_mode='terse', print_pos=False); b = S(-Integer(70700)); b
  >     -70700 + O(5^22)
  >     >>> c = S(-Integer(707)/Integer(5)); c
  >     -707/5 + O(5^19)

  > name affects how the name is printed.

  > Sage

  >     sage: T.<unif> = Qp(5, print_mode='terse'); c = T(-707/5); c
  >     95367431639918/unif + O(unif^19)
  >     sage: d = T(-707/5^10); d
  >     95367431639918/unif^10 + O(unif^10)

  > Python

  >     >>> from sage.all import *
  >     >>> T = Qp(Integer(5), print_mode='terse', names=('unif',)); (unif,) = T._first_ngens(1); c = T(-Integer(707)/Integer(5)); c
  >     95367431639918/unif + O(unif^19)
  >     >>> d = T(-Integer(707)/Integer(5)**Integer(10)); d
  >     95367431639918/unif^10 + O(unif^10)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`) or ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: Qp(5, print_mode='terse', show_prec=False)(6)
  >     6

  > [Python]

  >     >>> from sage.all import *
  >     >>> Qp(Integer(5), print_mode='terse', show_prec=False)(Integer(6))
  >     6

  > print_max_terms, print_sep and print_alphabet have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  4. digits: elements are displayed as a string of base \(p\) digits

  > Restriction: you can only use the digits printing mode for small primes. Namely, \(p\) must be less than the length of the alphabet tuple (default alphabet has length 62).

  > Sage

  >     sage: R = Qp(5, print_mode='digits'); a = R(70700); repr(a)
  >     '...0000000000000004230300'
  >     sage: b = R(-70700); repr(b)
  >     '...4444444444444440214200'
  >     sage: c = R(-707/5); repr(c)
  >     '...4444444444444443413.3'
  >     sage: d = R(-707/5^2); repr(d)
  >     '...444444444444444341.33'

  > Python

  >     >>> from sage.all import *
  >     >>> R = Qp(Integer(5), print_mode='digits'); a = R(Integer(70700)); repr(a)
  >     '...0000000000000004230300'
  >     >>> b = R(-Integer(70700)); repr(b)
  >     '...4444444444444440214200'
  >     >>> c = R(-Integer(707)/Integer(5)); repr(c)
  >     '...4444444444444443413.3'
  >     >>> d = R(-Integer(707)/Integer(5)**Integer(2)); repr(d)
  >     '...444444444444444341.33'

  > Observe that the significant 0s are printed even if they are located in front of the number. On the contrary, unknown digits located after the comma appears as question marks. The precision can therefore be read in this mode as well. Here are more examples:

  > Sage

  >     sage: p = 7
  >     sage: K = Qp(p, prec=10, print_mode='digits')
  >     sage: repr(K(1))
  >     '...0000000001'
  >     sage: repr(K(p^2))
  >     '...000000000100'
  >     sage: repr(K(p^-5))
  >     '...00000.00001'
  >     sage: repr(K(p^-20))
  >     '...?.??????????0000000001'

  > [Python]

  >     >>> from sage.all import *
  >     >>> p = Integer(7)
  >     >>> K = Qp(p, prec=Integer(10), print_mode='digits')
  >     >>> repr(K(Integer(1)))
  >     '...0000000001'
  >     >>> repr(K(p**Integer(2)))
  >     '...000000000100'
  >     >>> repr(K(p**-Integer(5)))
  >     '...00000.00001'
  >     >>> repr(K(p**-Integer(20)))
  >     '...?.??????????0000000001'

  > print_max_terms limits the number of digits that are printed. Note that if the valuation of the element is very negative, more digits will be printed.

  > Sage

  >     sage: S = Qp(5, print_max_terms=4); S(-70700)
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)
  >     sage: S(-707/5^2)
  >     3*5^-2 + 3*5^-1 + 1 + 4*5 + ... + O(5^18)
  >     sage: S(-707/5^6)
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + ... + O(5^14)
  >     sage: S(-707/5^6,absprec=-2)
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + O(5^-2)
  >     sage: S(-707/5^4)
  >     3*5^-4 + 3*5^-3 + 5^-2 + 4*5^-1 + ... + O(5^16)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Qp(Integer(5), print_max_terms=Integer(4)); S(-Integer(70700))
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)
  >     >>> S(-Integer(707)/Integer(5)**Integer(2))
  >     3*5^-2 + 3*5^-1 + 1 + 4*5 + ... + O(5^18)
  >     >>> S(-Integer(707)/Integer(5)**Integer(6))
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + ... + O(5^14)
  >     >>> S(-Integer(707)/Integer(5)**Integer(6),absprec=-Integer(2))
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + O(5^-2)
  >     >>> S(-Integer(707)/Integer(5)**Integer(4))
  >     3*5^-4 + 3*5^-3 + 5^-2 + 4*5^-1 + ... + O(5^16)

  > print_alphabet controls the symbols used to substitute for digits greater than 9.

  > Defaults to (‘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’):

  > Sage

  >     sage: T = Qp(5, print_mode='digits', print_alphabet=('1','2','3','4','5')); repr(T(-70700))
  >     '...5555555555555551325311'

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Qp(Integer(5), print_mode='digits', print_alphabet=('1','2','3','4','5')); repr(T(-Integer(70700)))
  >     '...5555555555555551325311'

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘dots’ (or equivalently `True`) or ‘bigoh’. The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: repr(Zp(5, print_mode='digits', show_prec=True)(6))
  >     '...00000000000000000011'

  >     sage: repr(Zp(5, print_mode='digits', show_prec='bigoh')(6))
  >     '11 + O(5^20)'

  > Python

  >     >>> from sage.all import *
  >     >>> repr(Zp(Integer(5), print_mode='digits', show_prec=True)(Integer(6)))
  >     '...00000000000000000011'

  >     >>> repr(Zp(Integer(5), print_mode='digits', show_prec='bigoh')(Integer(6)))
  >     '11 + O(5^20)'

  > print_pos, name and print_sep have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  5. bars: elements are displayed as a string of base \(p\) digits with separators:

     Sage

         sage: R = Qp(5, print_mode='bars'); a = R(70700); repr(a)
         '...4|2|3|0|3|0|0'
         sage: b = R(-70700); repr(b)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|0|2|1|4|2|0|0'
         sage: d = R(-707/5^2); repr(d)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|3|4|1|.|3|3'

     Python

         >>> from sage.all import *
         >>> R = Qp(Integer(5), print_mode='bars'); a = R(Integer(70700)); repr(a)
         '...4|2|3|0|3|0|0'
         >>> b = R(-Integer(70700)); repr(b)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|0|2|1|4|2|0|0'
         >>> d = R(-Integer(707)/Integer(5)**Integer(2)); repr(d)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|3|4|1|.|3|3'

  > Again, note that it’s not possible to read off the precision from the representation in this mode.

  > print_pos controls whether the digits can be negative.

  > Sage

  >     sage: S = Qp(5, print_mode='bars',print_pos=False); b = S(-70700); repr(b)
  >     '...-1|0|2|2|-1|2|0|0'

  > Python

  >     >>> from sage.all import *
  >     >>> S = Qp(Integer(5), print_mode='bars',print_pos=False); b = S(-Integer(70700)); repr(b)
  >     '...-1|0|2|2|-1|2|0|0'

  > print_max_terms limits the number of digits that are printed. Note that if the valuation of the element is very negative, more digits will be printed.

  > Sage

  >     sage: T = Qp(5, print_max_terms=4); T(-70700)
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)
  >     sage: T(-707/5^2)
  >     3*5^-2 + 3*5^-1 + 1 + 4*5 + ... + O(5^18)
  >     sage: T(-707/5^6)
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + ... + O(5^14)
  >     sage: T(-707/5^6,absprec=-2)
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + O(5^-2)
  >     sage: T(-707/5^4)
  >     3*5^-4 + 3*5^-3 + 5^-2 + 4*5^-1 + ... + O(5^16)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Qp(Integer(5), print_max_terms=Integer(4)); T(-Integer(70700))
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)
  >     >>> T(-Integer(707)/Integer(5)**Integer(2))
  >     3*5^-2 + 3*5^-1 + 1 + 4*5 + ... + O(5^18)
  >     >>> T(-Integer(707)/Integer(5)**Integer(6))
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + ... + O(5^14)
  >     >>> T(-Integer(707)/Integer(5)**Integer(6),absprec=-Integer(2))
  >     3*5^-6 + 3*5^-5 + 5^-4 + 4*5^-3 + O(5^-2)
  >     >>> T(-Integer(707)/Integer(5)**Integer(4))
  >     3*5^-4 + 3*5^-3 + 5^-2 + 4*5^-1 + ... + O(5^16)

  > print_sep controls the separation character.

  > Sage

  >     sage: U = Qp(5, print_mode='bars', print_sep=']['); a = U(70700); repr(a)
  >     '...4][2][3][0][3][0][0'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Qp(Integer(5), print_mode='bars', print_sep=']['); a = U(Integer(70700)); repr(a)
  >     '...4][2][3][0][3][0][0'

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘dots’ (or equivalently `True`) or ‘bigoh’ The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: repr(Qp(5, print_mode='bars', show_prec='bigoh')(6))
  >     '...1|1 + O(5^20)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> repr(Qp(Integer(5), print_mode='bars', show_prec='bigoh')(Integer(6)))
  >     '...1|1 + O(5^20)'

  > name and print_alphabet have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  EXAMPLES:

  Sage

      sage: K = Qp(15, check=False); a = K(999); a
      9 + 6*15 + 4*15^2 + O(15^20)

  Python

      >>> from sage.all import *
      >>> K = Qp(Integer(15), check=False); a = K(Integer(999)); a
      9 + 6*15 + 4*15^2 + O(15^20)

  create_key(p, prec=None, type='capped-rel', print_mode=None, names=None, ram_name=None, print_pos=None, print_sep=None, print_alphabet=None, print_max_terms=None, show_prec=None, check=True, label=None)

    Create a key from input parameters for `Qp`.

    See the documentation for `Qp` for more information.

  create_object(version, key)

    Create an object using a given key.

    See the documentation for `Qp` for more information.

sage.rings.padics.factory.Qq(q, prec=None, type='capped-rel', modulus=None, names=None, print_mode=None, ram_name=None, res_name=None, print_pos=None, print_sep=None, print_max_ram_terms=None, print_max_unram_terms=None, print_max_terse_terms=None, show_prec=None, check=True, implementation='FLINT')

  Given a prime power \(q = p^n\), return the unique unramified extension of \(\QQ_p\) of degree \(n\).

  INPUT:

  - `q` – integer, list, tuple or `Factorization` object. If `q` is an integer, it is the prime power \(q\) in \(\QQ_q\). If `q` is a `Factorization` object, it is the factorization of the prime power \(q\). As a tuple it is the pair `(p, n)`, and as a list it is a single element list `[(p, n)]`.

  - `prec` – integer (default: 20); the precision cap of the field. Individual elements keep track of their own precision. See TYPES and PRECISION below.

  - `type` – string (default: `'capped-rel'`); valid types are `'capped-rel'`, `'floating-point'`, `'lattice-cap'` and `'lattice-float'`. See TYPES and PRECISION below.

  - `modulus` – polynomial (default: `None`); a polynomial defining an unramified extension of \(\QQ_p\). See MODULUS below.

  - `names` – string or tuple (`None` is only allowed when \(q=p\)); the name of the generator, reducing to a generator of the residue field.

  - `print_mode` – string (default: `None`); valid modes are `'series'`, `'val-unit'`, `'terse'`, and `'bars'`. See PRINTING below.

  - `ram_name` – string (defaults to string representation of \(p\) if `None`). `ram_name` controls how the prime is printed. See PRINTING below.

  - `res_name` – string (defaults to `None`, which corresponds to adding a `'0'` to the end of the name). Controls how elements of the residue field print.

  - `print_pos` – boolean (default: `None`); whether to only use positive integers in the representations of elements. See PRINTING below.

  - `print_sep` – string (default: `None`); the separator character used in the `'bars'` mode. See PRINTING below.

  - `print_max_ram_terms` – integer (default: `None`) the maximum number of powers of \(p\) shown. See PRINTING below.

  - `print_max_unram_terms` – integer (default: `None`); the maximum number of entries shown in a coefficient of \(p\). See PRINTING below.

  - `print_max_terse_terms` – integer (default: `None`); the maximum number of terms in the polynomial representation of an element (using `'terse'`). See PRINTING below.

  - `show_prec` – boolean (default: `None`); whether to show the precision for elements. See PRINTING below.

  - `check` – boolean (default: `True`); whether to check inputs

  OUTPUT: the corresponding unramified \(p\)-adic field

  TYPES AND PRECISION:

  There are two types of precision for a \(p\)-adic element. The first is relative precision, which gives the number of known \(p\)-adic digits:

  Sage

      sage: R.<a> = Qq(25, 20, 'capped-rel', print_mode='series')
      sage: b = 25*a; b
      a*5^2 + O(5^22)
      sage: b.precision_relative()
      20

  Python

      >>> from sage.all import *
      >>> R = Qq(Integer(25), Integer(20), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1)
      >>> b = Integer(25)*a; b
      a*5^2 + O(5^22)
      >>> b.precision_relative()
      20

  The second type of precision is absolute precision, which gives the power of \(p\) that this element is defined modulo:

  Sage

      sage: b.precision_absolute()
      22

  [Python]

      >>> from sage.all import *
      >>> b.precision_absolute()
      22

  There are two types of unramified \(p\)-adic fields: capped relative fields, floating point fields.

  In the capped relative case, the relative precision of an element is restricted to be at most a certain value, specified at the creation of the field. Individual elements also store their own precision, so the effect of various arithmetic operations on precision is tracked. When you cast an exact element into a capped relative field, it truncates it to the precision cap of the field.

  Sage

      sage: R.<a> = Qq(9, 5, 'capped-rel', print_mode='series')
      sage: b = (1+2*a)^4; b
      2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^5)
      sage: c = R(3249); c
      3^2 + 3^4 + 3^5 + 3^6 + O(3^7)
      sage: b + c
      2 + (2*a + 2)*3 + (2*a + 2)*3^2 + 3^4 + O(3^5)

  Python

      >>> from sage.all import *
      >>> R = Qq(Integer(9), Integer(5), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1)
      >>> b = (Integer(1)+Integer(2)*a)**Integer(4); b
      2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^5)
      >>> c = R(Integer(3249)); c
      3^2 + 3^4 + 3^5 + 3^6 + O(3^7)
      >>> b + c
      2 + (2*a + 2)*3 + (2*a + 2)*3^2 + 3^4 + O(3^5)

  In the floating point case, elements do not track their precision, but the relative precision of elements is truncated during arithmetic to the precision cap of the field.

  MODULUS:

  The modulus needs to define an unramified extension of \(\QQ_p\): when it is reduced to a polynomial over \(\GF{p}\) it should be irreducible.

  The modulus can be given in a number of forms.

  1. A polynomial.

  > The base ring can be \(\ZZ\), \(\QQ\), \(\ZZ_p\), \(\QQ_p\), \(\GF{p}\).

  > Sage

  >     sage: P.<x> = ZZ[]
  >     sage: R.<a> = Qq(27, modulus = x^3 + 2*x + 1); R.modulus()
  >     (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
  >     sage: P.<x> = QQ[]
  >     sage: S.<a> = Qq(27, modulus = x^3 + 2*x + 1)
  >     sage: P.<x> = Zp(3)[]
  >     sage: T.<a> = Qq(27, modulus = x^3 + 2*x + 1)
  >     sage: P.<x> = Qp(3)[]
  >     sage: U.<a> = Qq(27, modulus = x^3 + 2*x + 1)
  >     sage: P.<x> = GF(3)[]
  >     sage: V.<a> = Qq(27, modulus = x^3 + 2*x + 1)

  > Python

  >     >>> from sage.all import *
  >     >>> P = ZZ['x']; (x,) = P._first_ngens(1)
  >     >>> R = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = R._first_ngens(1); R.modulus()
  >     (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
  >     >>> P = QQ['x']; (x,) = P._first_ngens(1)
  >     >>> S = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = S._first_ngens(1)
  >     >>> P = Zp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> T = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = T._first_ngens(1)
  >     >>> P = Qp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> U = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = U._first_ngens(1)
  >     >>> P = GF(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> V = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = V._first_ngens(1)

  > Which form the modulus is given in has no effect on the unramified extension produced:

  > Sage

  >     sage: R == S, S == T, T == U, U == V
  >     (True, True, True, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, S == T, T == U, U == V
  >     (True, True, True, False)

  > unless the precision of the modulus differs. In the case of V, the modulus is only given to precision 1, so the resulting field has a precision cap of 1.

  > Sage

  >     sage: V.precision_cap()
  >     1
  >     sage: U.precision_cap()
  >     20
  >     sage: P.<x> = Qp(3)[]
  >     sage: modulus = x^3 + (2 + O(3^7))*x + (1 + O(3^10))
  >     sage: modulus
  >     (1 + O(3^20))*x^3 + (2 + O(3^7))*x + 1 + O(3^10)
  >     sage: W.<a> = Qq(27, modulus = modulus); W.precision_cap()
  >     7

  > Python

  >     >>> from sage.all import *
  >     >>> V.precision_cap()
  >     1
  >     >>> U.precision_cap()
  >     20
  >     >>> P = Qp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> modulus = x**Integer(3) + (Integer(2) + O(Integer(3)**Integer(7)))*x + (Integer(1) + O(Integer(3)**Integer(10)))
  >     >>> modulus
  >     (1 + O(3^20))*x^3 + (2 + O(3^7))*x + 1 + O(3^10)
  >     >>> W = Qq(Integer(27), modulus = modulus, names=('a',)); (a,) = W._first_ngens(1); W.precision_cap()
  >     7

  2. The modulus can also be given as a symbolic expression.

     [Sage]

         sage: x = var('x')
         sage: X.<a> = Qq(27, modulus = x^3 + 2*x + 1); X.modulus()
         (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
         sage: X == R
         True

     [Python]

         >>> from sage.all import *
         >>> x = var('x')
         >>> X = Qq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = X._first_ngens(1); X.modulus()
         (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
         >>> X == R
         True

  > By default, the polynomial chosen is the standard lift of the generator chosen for \(\GF{q}\).

  > Sage

  >     sage: GF(125, 'a').modulus()
  >     x^3 + 3*x + 3
  >     sage: Y.<a> = Qq(125); Y.modulus()
  >     (1 + O(5^20))*x^3 + O(5^20)*x^2 + (3 + O(5^20))*x + 3 + O(5^20)

  > Python

  >     >>> from sage.all import *
  >     >>> GF(Integer(125), 'a').modulus()
  >     x^3 + 3*x + 3
  >     >>> Y = Qq(Integer(125), names=('a',)); (a,) = Y._first_ngens(1); Y.modulus()
  >     (1 + O(5^20))*x^3 + O(5^20)*x^2 + (3 + O(5^20))*x + 3 + O(5^20)

  > However, you can choose another polynomial if desired (as long as the reduction to \(\GF{p}[x]\) is irreducible).

  > Sage

  >     sage: P.<x> = ZZ[]
  >     sage: Z.<a> = Qq(125, modulus = x^3 + 3*x^2 + x + 1); Z.modulus()
  >     (1 + O(5^20))*x^3 + (3 + O(5^20))*x^2 + (1 + O(5^20))*x + 1 + O(5^20)
  >     sage: Y == Z
  >     False

  > [Python]

  >     >>> from sage.all import *
  >     >>> P = ZZ['x']; (x,) = P._first_ngens(1)
  >     >>> Z = Qq(Integer(125), modulus = x**Integer(3) + Integer(3)*x**Integer(2) + x + Integer(1), names=('a',)); (a,) = Z._first_ngens(1); Z.modulus()
  >     (1 + O(5^20))*x^3 + (3 + O(5^20))*x^2 + (1 + O(5^20))*x + 1 + O(5^20)
  >     >>> Y == Z
  >     False

  PRINTING:

  There are many different ways to print \(p\)-adic elements. The way elements of a given field print is controlled by options passed in at the creation of the field. There are four basic printing modes (`'series'`, `'val-unit'`, `'terse'` and `'bars'`; `'digits'` is not available), as well as various options that either hide some information in the print representation or sometimes make print representations more compact. Note that the printing options affect whether different \(p\)-adic fields are considered equal.

  1. series: elements are displayed as series in \(p\).

     Sage

         sage: R.<a> = Qq(9, 20, 'capped-rel', print_mode='series'); (1+2*a)^4
         2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^20)
         sage: -3*(1+2*a)^4
         3 + a*3^2 + 3^3 + (2*a + 2)*3^4 + (2*a + 2)*3^5 + (2*a + 2)*3^6 + (2*a + 2)*3^7
          + (2*a + 2)*3^8 + (2*a + 2)*3^9 + (2*a + 2)*3^10 + (2*a + 2)*3^11
          + (2*a + 2)*3^12 + (2*a + 2)*3^13 + (2*a + 2)*3^14 + (2*a + 2)*3^15
          + (2*a + 2)*3^16 + (2*a + 2)*3^17 + (2*a + 2)*3^18 + (2*a + 2)*3^19
          + (2*a + 2)*3^20 + O(3^21)
         sage: ~(3*a+18)
         (a + 2)*3^-1 + 1 + 2*3 + (a + 1)*3^2 + 3^3 + 2*3^4 + (a + 1)*3^5 + 3^6 + 2*3^7
          + (a + 1)*3^8 + 3^9 + 2*3^10 + (a + 1)*3^11 + 3^12 + 2*3^13 + (a + 1)*3^14
          + 3^15 + 2*3^16 + (a + 1)*3^17 + 3^18 + O(3^19)

     Python

         >>> from sage.all import *
         >>> R = Qq(Integer(9), Integer(20), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); (Integer(1)+Integer(2)*a)**Integer(4)
         2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^20)
         >>> -Integer(3)*(Integer(1)+Integer(2)*a)**Integer(4)
         3 + a*3^2 + 3^3 + (2*a + 2)*3^4 + (2*a + 2)*3^5 + (2*a + 2)*3^6 + (2*a + 2)*3^7
          + (2*a + 2)*3^8 + (2*a + 2)*3^9 + (2*a + 2)*3^10 + (2*a + 2)*3^11
          + (2*a + 2)*3^12 + (2*a + 2)*3^13 + (2*a + 2)*3^14 + (2*a + 2)*3^15
          + (2*a + 2)*3^16 + (2*a + 2)*3^17 + (2*a + 2)*3^18 + (2*a + 2)*3^19
          + (2*a + 2)*3^20 + O(3^21)
         >>> ~(Integer(3)*a+Integer(18))
         (a + 2)*3^-1 + 1 + 2*3 + (a + 1)*3^2 + 3^3 + 2*3^4 + (a + 1)*3^5 + 3^6 + 2*3^7
          + (a + 1)*3^8 + 3^9 + 2*3^10 + (a + 1)*3^11 + 3^12 + 2*3^13 + (a + 1)*3^14
          + 3^15 + 2*3^16 + (a + 1)*3^17 + 3^18 + O(3^19)

  > print_pos controls whether negatives can be used in the coefficients of powers of \(p\).

  > Sage

  >     sage: S.<b> = Qq(9, print_mode='series', print_pos=False); (1+2*b)^4
  >     -1 - b*3 - 3^2 + (b + 1)*3^3 + O(3^20)
  >     sage: -3*(1+2*b)^4
  >     3 + b*3^2 + 3^3 + (-b - 1)*3^4 + O(3^21)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Qq(Integer(9), print_mode='series', print_pos=False, names=('b',)); (b,) = S._first_ngens(1); (Integer(1)+Integer(2)*b)**Integer(4)
  >     -1 - b*3 - 3^2 + (b + 1)*3^3 + O(3^20)
  >     >>> -Integer(3)*(Integer(1)+Integer(2)*b)**Integer(4)
  >     3 + b*3^2 + 3^3 + (-b - 1)*3^4 + O(3^21)

  > ram_name controls how the prime is printed.

  > Sage

  >     sage: T.<d> = Qq(9, print_mode='series', ram_name='p'); 3*(1+2*d)^4
  >     2*p + (2*d + 2)*p^2 + (2*d + 1)*p^3 + O(p^21)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Qq(Integer(9), print_mode='series', ram_name='p', names=('d',)); (d,) = T._first_ngens(1); Integer(3)*(Integer(1)+Integer(2)*d)**Integer(4)
  >     2*p + (2*d + 2)*p^2 + (2*d + 1)*p^3 + O(p^21)

  > print_max_ram_terms limits the number of powers of \(p\) that appear.

  > Sage

  >     sage: U.<e> = Qq(9, print_mode='series', print_max_ram_terms=4); repr(-3*(1+2*e)^4)
  >     '3 + e*3^2 + 3^3 + (2*e + 2)*3^4 + ... + O(3^21)'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(9), print_mode='series', print_max_ram_terms=Integer(4), names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + e*3^2 + 3^3 + (2*e + 2)*3^4 + ... + O(3^21)'

  > print_max_unram_terms limits the number of terms that appear in a coefficient of a power of \(p\).

  > Sage

  >     sage: V.<f> = Qq(128, prec = 8, print_mode='series'); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + f^4 + f^3 + f^2)*2 + (f^6 + f^5 + f^4 + f + 1)*2^2 + (f^5 + f^4 + f^2 + f + 1)*2^3 + (f^6 + f^5 + f^4 + f^3 + f^2 + f + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + f^4 + f^3 + f + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Qq(128, prec = 8, print_mode='series', print_max_unram_terms = 3); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + f^4 + ... + f^2)*2 + (f^6 + f^5 + ... + 1)*2^2 + (f^5 + f^4 + ... + 1)*2^3 + (f^6 + f^5 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Qq(128, prec = 8, print_mode='series', print_max_unram_terms = 2); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + ... + f^2)*2 + (f^6 + ... + 1)*2^2 + (f^5 + ... + 1)*2^3 + (f^6 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Qq(128, prec = 8, print_mode='series', print_max_unram_terms = 1); repr((1+f)^9)
  >     '(f^3 + ...) + (f^5 + ...)*2 + (f^6 + ...)*2^2 + (f^5 + ...)*2^3 + (f^6 + ...)*2^4 + (f^5 + ...)*2^5 + (f^6 + ...)*2^6 + (f + ...)*2^7 + O(2^8)'
  >     sage: V.<f> = Qq(128, prec = 8, print_mode='series', print_max_unram_terms = 0); repr((1+f)^9 - 1 - f^3)
  >     '(...)*2 + (...)*2^2 + (...)*2^3 + (...)*2^4 + (...)*2^5 + (...)*2^6 + (...)*2^7 + O(2^8)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> V = Qq(Integer(128), prec = Integer(8), print_mode='series', names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + f^4 + f^3 + f^2)*2 + (f^6 + f^5 + f^4 + f + 1)*2^2 + (f^5 + f^4 + f^2 + f + 1)*2^3 + (f^6 + f^5 + f^4 + f^3 + f^2 + f + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + f^4 + f^3 + f + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Qq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(3), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + f^4 + ... + f^2)*2 + (f^6 + f^5 + ... + 1)*2^2 + (f^5 + f^4 + ... + 1)*2^3 + (f^6 + f^5 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Qq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(2), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + ... + f^2)*2 + (f^6 + ... + 1)*2^2 + (f^5 + ... + 1)*2^3 + (f^6 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Qq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(1), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + ...) + (f^5 + ...)*2 + (f^6 + ...)*2^2 + (f^5 + ...)*2^3 + (f^6 + ...)*2^4 + (f^5 + ...)*2^5 + (f^6 + ...)*2^6 + (f + ...)*2^7 + O(2^8)'
  >     >>> V = Qq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(0), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9) - Integer(1) - f**Integer(3))
  >     '(...)*2 + (...)*2^2 + (...)*2^3 + (...)*2^4 + (...)*2^5 + (...)*2^6 + (...)*2^7 + O(2^8)'

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: U.<e> = Qq(9, 2, show_prec=False); repr(-3*(1+2*e)^4)
  >     '3 + e*3^2'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(9), Integer(2), show_prec=False, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + e*3^2'

  > print_sep and print_max_terse_terms have no effect.

  > Note that print options affect equality:

  > Sage

  >     sage: R == S, R == T, R == U, R == V, S == T, S == U, S == V, T == U, T == V, U == V
  >     (False, False, False, False, False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, R == V, S == T, S == U, S == V, T == U, T == V, U == V
  >     (False, False, False, False, False, False, False, False, False, False)

  2. val-unit: elements are displayed as \(p^k u\):

     Sage

         sage: R.<a> = Qq(9, 7, print_mode='val-unit'); b = (1+3*a)^9 - 1; b
         3^3 * (15 + 64*a) + O(3^7)
         sage: ~b
         3^-3 * (41 + a) + O(3)

     Python

         >>> from sage.all import *
         >>> R = Qq(Integer(9), Integer(7), print_mode='val-unit', names=('a',)); (a,) = R._first_ngens(1); b = (Integer(1)+Integer(3)*a)**Integer(9) - Integer(1); b
         3^3 * (15 + 64*a) + O(3^7)
         >>> ~b
         3^-3 * (41 + a) + O(3)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S.<a> = Qq(9, 7, print_mode='val-unit', print_pos=False)
  >     sage: b = (1+3*a)^9 - 1; b
  >     3^3 * (15 - 17*a) + O(3^7)
  >     sage: ~b
  >     3^-3 * (-40 + a) + O(3)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Qq(Integer(9), Integer(7), print_mode='val-unit', print_pos=False, names=('a',)); (a,) = S._first_ngens(1)
  >     >>> b = (Integer(1)+Integer(3)*a)**Integer(9) - Integer(1); b
  >     3^3 * (15 - 17*a) + O(3^7)
  >     >>> ~b
  >     3^-3 * (-40 + a) + O(3)

  > ram_name affects how the prime is printed.

  > Sage

  >     sage: A.<x> = Qp(next_prime(10^6), print_mode='val-unit')[]
  >     sage: T.<a> = Qq(next_prime(10^6)^3, 4, print_mode='val-unit', ram_name='p',
  >     ....:            modulus=x^3+385831*x^2+106556*x+321036)
  >     sage: b = ~(next_prime(10^6)^2*(a^2 + a - 4)); b
  >     p^-2 * (503009563508519137754940 + 704413692798200940253892*a
  >              + 968097057817740999537581*a^2) + O(p^2)
  >     sage: b * (a^2 + a - 4)
  >     p^-2 * 1 + O(p^2)

  > [Python]

  >     >>> from sage.all import *
  >     >>> A = Qp(next_prime(Integer(10)**Integer(6)), print_mode='val-unit')['x']; (x,) = A._first_ngens(1)
  >     >>> T = Qq(next_prime(Integer(10)**Integer(6))**Integer(3), Integer(4), print_mode='val-unit', ram_name='p',
  >     ...            modulus=x**Integer(3)+Integer(385831)*x**Integer(2)+Integer(106556)*x+Integer(321036), names=('a',)); (a,) = T._first_ngens(1)
  >     >>> b = ~(next_prime(Integer(10)**Integer(6))**Integer(2)*(a**Integer(2) + a - Integer(4))); b
  >     p^-2 * (503009563508519137754940 + 704413692798200940253892*a
  >              + 968097057817740999537581*a^2) + O(p^2)
  >     >>> b * (a**Integer(2) + a - Integer(4))
  >     p^-2 * 1 + O(p^2)

  > print_max_terse_terms controls how many terms of the polynomial appear in the unit part.

  > Sage

  >     sage: U.<a> = Qq(17^4, 6, print_mode='val-unit', print_max_terse_terms=3)
  >     sage: b = ~(17*(a^3-a+14)); b
  >     17^-1 * (22110411 + 11317400*a + 20656972*a^2 + ...) + O(17^5)
  >     sage: b*17*(a^3-a+14)
  >     1 + O(17^6)

  > Python

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(17)**Integer(4), Integer(6), print_mode='val-unit', print_max_terse_terms=Integer(3), names=('a',)); (a,) = U._first_ngens(1)
  >     >>> b = ~(Integer(17)*(a**Integer(3)-a+Integer(14))); b
  >     17^-1 * (22110411 + 11317400*a + 20656972*a^2 + ...) + O(17^5)
  >     >>> b*Integer(17)*(a**Integer(3)-a+Integer(14))
  >     1 + O(17^6)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: U.<e> = Qq(9, 2, print_mode='val-unit', show_prec=False); repr(-3*(1+2*e)^4)
  >     '3 * (1 + 3*e)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(9), Integer(2), print_mode='val-unit', show_prec=False, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 * (1 + 3*e)'

  > print_sep, print_max_ram_terms and print_max_unram_terms have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  3. terse: elements are displayed as a polynomial of degree less than the degree of the extension.

     Sage

         sage: R.<a> = Qq(125, print_mode='terse')
         sage: (a+5)^177
         68210977979428 + 90313850704069*a + 73948093055069*a^2 + O(5^20)
         sage: (a/5+1)^177
         68210977979428/5^177 + 90313850704069/5^177*a + 73948093055069/5^177*a^2 + O(5^-157)

     Python

         >>> from sage.all import *
         >>> R = Qq(Integer(125), print_mode='terse', names=('a',)); (a,) = R._first_ngens(1)
         >>> (a+Integer(5))**Integer(177)
         68210977979428 + 90313850704069*a + 73948093055069*a^2 + O(5^20)
         >>> (a/Integer(5)+Integer(1))**Integer(177)
         68210977979428/5^177 + 90313850704069/5^177*a + 73948093055069/5^177*a^2 + O(5^-157)

  > As of version 3.3, if coefficients of the polynomial are non-integral, they are always printed with an explicit power of \(p\) in the denominator.

  > Sage

  >     sage: 5*a + a^2/25
  >     5*a + 1/5^2*a^2 + O(5^18)

  > Python

  >     >>> from sage.all import *
  >     >>> Integer(5)*a + a**Integer(2)/Integer(25)
  >     5*a + 1/5^2*a^2 + O(5^18)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: (a-5)^6
  >     22864 + 95367431627998*a + 8349*a^2 + O(5^20)
  >     sage: S.<a> = Qq(125, print_mode='terse', print_pos=False); b = (a-5)^6; b
  >     22864 - 12627*a + 8349*a^2 + O(5^20)
  >     sage: (a - 1/5)^6
  >     -20624/5^6 + 18369/5^5*a + 1353/5^3*a^2 + O(5^14)

  > [Python]

  >     >>> from sage.all import *
  >     >>> (a-Integer(5))**Integer(6)
  >     22864 + 95367431627998*a + 8349*a^2 + O(5^20)
  >     >>> S = Qq(Integer(125), print_mode='terse', print_pos=False, names=('a',)); (a,) = S._first_ngens(1); b = (a-Integer(5))**Integer(6); b
  >     22864 - 12627*a + 8349*a^2 + O(5^20)
  >     >>> (a - Integer(1)/Integer(5))**Integer(6)
  >     -20624/5^6 + 18369/5^5*a + 1353/5^3*a^2 + O(5^14)

  > ram_name affects how the prime is printed.

  > Sage

  >     sage: T.<a> = Qq(125, print_mode='terse', ram_name='p'); (a - 1/5)^6
  >     95367431620001/p^6 + 18369/p^5*a + 1353/p^3*a^2 + O(p^14)

  > Python

  >     >>> from sage.all import *
  >     >>> T = Qq(Integer(125), print_mode='terse', ram_name='p', names=('a',)); (a,) = T._first_ngens(1); (a - Integer(1)/Integer(5))**Integer(6)
  >     95367431620001/p^6 + 18369/p^5*a + 1353/p^3*a^2 + O(p^14)

  > print_max_terse_terms controls how many terms of the polynomial are shown.

  > Sage

  >     sage: U.<a> = Qq(625, print_mode='terse', print_max_terse_terms=2); (a-1/5)^6
  >     106251/5^6 + 49994/5^5*a + ... + O(5^14)

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(625), print_mode='terse', print_max_terse_terms=Integer(2), names=('a',)); (a,) = U._first_ngens(1); (a-Integer(1)/Integer(5))**Integer(6)
  >     106251/5^6 + 49994/5^5*a + ... + O(5^14)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: U.<e> = Qq(9, 2, print_mode='terse', show_prec=False)
  >     sage: repr(-3*(1+2*e)^4)
  >     '3 + 9*e'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(9), Integer(2), print_mode='terse', show_prec=False, names=('e',)); (e,) = U._first_ngens(1)
  >     >>> repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + 9*e'

  > print_sep, print_max_ram_terms and print_max_unram_terms have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  4. digits: This print mode is not available when the residue field is not prime.

     It might make sense to have a dictionary for small fields, but this isn’t implemented.

  5. bars: elements are displayed in a similar fashion to series, but more compactly.

     Sage

         sage: R.<a> = Qq(125); (a+5)^6
         (4*a^2 + 3*a + 4) + (3*a^2 + 2*a)*5 + (a^2 + a + 1)*5^2 + (3*a + 2)*5^3
          + (3*a^2 + a + 3)*5^4 + (2*a^2 + 3*a + 2)*5^5 + O(5^20)
         sage: R.<a> = Qq(125, print_mode='bars', prec=8); repr((a+5)^6)
         '...[2, 3, 2]|[3, 1, 3]|[2, 3]|[1, 1, 1]|[0, 2, 3]|[4, 3, 4]'
         sage: repr((a-5)^6)
         '...[0, 4]|[1, 4]|[2, 0, 2]|[1, 4, 3]|[2, 3, 1]|[4, 4, 3]|[2, 4, 4]|[4, 3, 4]'

     Python

         >>> from sage.all import *
         >>> R = Qq(Integer(125), names=('a',)); (a,) = R._first_ngens(1); (a+Integer(5))**Integer(6)
         (4*a^2 + 3*a + 4) + (3*a^2 + 2*a)*5 + (a^2 + a + 1)*5^2 + (3*a + 2)*5^3
          + (3*a^2 + a + 3)*5^4 + (2*a^2 + 3*a + 2)*5^5 + O(5^20)
         >>> R = Qq(Integer(125), print_mode='bars', prec=Integer(8), names=('a',)); (a,) = R._first_ngens(1); repr((a+Integer(5))**Integer(6))
         '...[2, 3, 2]|[3, 1, 3]|[2, 3]|[1, 1, 1]|[0, 2, 3]|[4, 3, 4]'
         >>> repr((a-Integer(5))**Integer(6))
         '...[0, 4]|[1, 4]|[2, 0, 2]|[1, 4, 3]|[2, 3, 1]|[4, 4, 3]|[2, 4, 4]|[4, 3, 4]'

  > Note that elements with negative valuation are shown with a decimal point at valuation 0.

  > Sage

  >     sage: repr((a+1/5)^6)
  >     '...[3]|[4, 1, 3]|.|[1, 2, 3]|[3, 3]|[0, 0, 3]|[0, 1]|[0, 1]|[1]'
  >     sage: repr((a+1/5)^2)
  >     '...[0, 0, 1]|.|[0, 2]|[1]'

  > Python

  >     >>> from sage.all import *
  >     >>> repr((a+Integer(1)/Integer(5))**Integer(6))
  >     '...[3]|[4, 1, 3]|.|[1, 2, 3]|[3, 3]|[0, 0, 3]|[0, 1]|[0, 1]|[1]'
  >     >>> repr((a+Integer(1)/Integer(5))**Integer(2))
  >     '...[0, 0, 1]|.|[0, 2]|[1]'

  > If not enough precision is known, `'?'` is used instead.

  > Sage

  >     sage: repr((a+R(1/5,relprec=3))^7)
  >     '...|.|?|?|?|?|[0, 1, 1]|[0, 2]|[1]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> repr((a+R(Integer(1)/Integer(5),relprec=Integer(3)))**Integer(7))
  >     '...|.|?|?|?|?|[0, 1, 1]|[0, 2]|[1]'

  > Note that it’s not possible to read off the precision from the representation in this mode.

  > Sage

  >     sage: b = a + 3; repr(b)
  >     '...[3, 1]'
  >     sage: c = a + R(3, 4); repr(c)
  >     '...[3, 1]'
  >     sage: b.precision_absolute()
  >     8
  >     sage: c.precision_absolute()
  >     4

  > Python

  >     >>> from sage.all import *
  >     >>> b = a + Integer(3); repr(b)
  >     '...[3, 1]'
  >     >>> c = a + R(Integer(3), Integer(4)); repr(c)
  >     '...[3, 1]'
  >     >>> b.precision_absolute()
  >     8
  >     >>> c.precision_absolute()
  >     4

  > print_pos controls whether the digits can be negative.

  > Sage

  >     sage: S.<a> = Qq(125, print_mode='bars', print_pos=False); repr((a-5)^6)
  >     '...[1, -1, 1]|[2, 1, -2]|[2, 0, -2]|[-2, -1, 2]|[0, 0, -1]|[-2]|[-1, -2, -1]'
  >     sage: repr((a-1/5)^6)
  >     '...[0, 1, 2]|[-1, 1, 1]|.|[-2, -1, -1]|[2, 2, 1]|[0, 0, -2]|[0, -1]|[0, -1]|[1]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Qq(Integer(125), print_mode='bars', print_pos=False, names=('a',)); (a,) = S._first_ngens(1); repr((a-Integer(5))**Integer(6))
  >     '...[1, -1, 1]|[2, 1, -2]|[2, 0, -2]|[-2, -1, 2]|[0, 0, -1]|[-2]|[-1, -2, -1]'
  >     >>> repr((a-Integer(1)/Integer(5))**Integer(6))
  >     '...[0, 1, 2]|[-1, 1, 1]|.|[-2, -1, -1]|[2, 2, 1]|[0, 0, -2]|[0, -1]|[0, -1]|[1]'

  > print_max_ram_terms controls the maximum number of “digits” shown. Note that this puts a cap on the relative precision, not the absolute precision.

  > Sage

  >     sage: T.<a> = Qq(125, print_max_ram_terms=3, print_pos=False); (a-5)^6
  >     (-a^2 - 2*a - 1) - 2*5 - a^2*5^2 + ... + O(5^20)
  >     sage: 5*(a-5)^6 + 50
  >     (-a^2 - 2*a - 1)*5 - a^2*5^3 + (2*a^2 - a - 2)*5^4 + ... + O(5^21)

  > Python

  >     >>> from sage.all import *
  >     >>> T = Qq(Integer(125), print_max_ram_terms=Integer(3), print_pos=False, names=('a',)); (a,) = T._first_ngens(1); (a-Integer(5))**Integer(6)
  >     (-a^2 - 2*a - 1) - 2*5 - a^2*5^2 + ... + O(5^20)
  >     >>> Integer(5)*(a-Integer(5))**Integer(6) + Integer(50)
  >     (-a^2 - 2*a - 1)*5 - a^2*5^3 + (2*a^2 - a - 2)*5^4 + ... + O(5^21)

  > print_sep controls the separating character (`'|'` by default).

  > Sage

  >     sage: U.<a> = Qq(625, print_mode='bars', print_sep=''); b = (a+5)^6; repr(b)
  >     '...[0, 1][4, 0, 2][3, 2, 2, 3][4, 2, 2, 4][0, 3][1, 1, 3][3, 1, 4, 1]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(625), print_mode='bars', print_sep='', names=('a',)); (a,) = U._first_ngens(1); b = (a+Integer(5))**Integer(6); repr(b)
  >     '...[0, 1][4, 0, 2][3, 2, 2, 3][4, 2, 2, 4][0, 3][1, 1, 3][3, 1, 4, 1]'

  > print_max_unram_terms controls how many terms are shown in each “digit”:

  > Sage

  >     sage: with local_print_mode(U, {'max_unram_terms': 3}): repr(b)
  >     '...[0, 1][4,..., 0, 2][3,..., 2, 3][4,..., 2, 4][0, 3][1,..., 1, 3][3,..., 4, 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms': 2}): repr(b)
  >     '...[0, 1][4,..., 2][3,..., 3][4,..., 4][0, 3][1,..., 3][3,..., 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms': 1}): repr(b)
  >     '...[..., 1][..., 2][..., 3][..., 4][..., 3][..., 3][..., 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms':0}): repr(b-75*a)
  >     '...[...][...][...][...][][...][...]'

  > Python

  >     >>> from sage.all import *
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(3)}): repr(b)
  >     '...[0, 1][4,..., 0, 2][3,..., 2, 3][4,..., 2, 4][0, 3][1,..., 1, 3][3,..., 4, 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(2)}): repr(b)
  >     '...[0, 1][4,..., 2][3,..., 3][4,..., 4][0, 3][1,..., 3][3,..., 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(1)}): repr(b)
  >     '...[..., 1][..., 2][..., 3][..., 4][..., 3][..., 3][..., 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms':Integer(0)}): repr(b-Integer(75)*a)
  >     '...[...][...][...][...][][...][...]'

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘dots’ (or equivalently `True`) or ‘bigoh’ The default is `False` for the `'floating-point'` type and `True` for all other types.

  > Sage

  >     sage: U.<e> = Qq(9, 2, print_mode='bars', show_prec=True); repr(-3*(1+2*e)^4)
  >     '...[0, 1]|[1]|[]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Qq(Integer(9), Integer(2), print_mode='bars', show_prec=True, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '...[0, 1]|[1]|[]'

  > ram_name and print_max_terse_terms have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  EXAMPLES:

  Unlike for `Qp`, you can’t create `Qq(N)` when `N` is not a prime power.

  However, you can use `check=False` to pass in a pair in order to not have to factor. If you do so, you need to use names explicitly rather than the `R.<a>` syntax.

  Sage

      sage: p = next_prime(2^123)
      sage: k = Qp(p)
      sage: R.<x> = k[]
      sage: K = Qq([(p, 5)], modulus=x^5+x+4, names='a', ram_name='p',
      ....:        print_pos=False, check=False)
      sage: K.0^5
      (-a - 4) + O(p^20)

  Python

      >>> from sage.all import *
      >>> p = next_prime(Integer(2)**Integer(123))
      >>> k = Qp(p)
      >>> R = k['x']; (x,) = R._first_ngens(1)
      >>> K = Qq([(p, Integer(5))], modulus=x**Integer(5)+x+Integer(4), names='a', ram_name='p',
      ...        print_pos=False, check=False)
      >>> K.gen(0)**Integer(5)
      (-a - 4) + O(p^20)

  In tests on `sage.math.washington.edu`, the creation of `K` as above took an average of 1.58ms, while:

  Sage

      sage: K = Qq(p^5, modulus=x^5+x+4, names='a', ram_name='p',
      ....:        print_pos=False, check=True)

  [Python]

      >>> from sage.all import *
      >>> K = Qq(p**Integer(5), modulus=x**Integer(5)+x+Integer(4), names='a', ram_name='p',
      ...        print_pos=False, check=True)

  took an average of 24.5ms. Of course, with smaller primes these savings disappear.

sage.rings.padics.factory.QqCR(q, prec=None, *args, **kwds)

  A shortcut function to create capped relative unramified \(p\)-adic fields.

  Same functionality as `Qq()`. See documentation for `Qq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = QqCR(25, 40); R
      5-adic Unramified Extension Field in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = QqCR(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Field in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.QqFP(q, prec=None, *args, **kwds)

  A shortcut function to create floating point unramified \(p\)-adic fields.

  Same functionality as `Qq()`. See documentation for `Qq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = QqFP(25, 40); R
      5-adic Unramified Extension Field in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = QqFP(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Field in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.ZpCA(p, prec=None, *args, **kwds)

  A shortcut function to create capped absolute \(p\)-adic rings.

  See documentation for `Zp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: ZpCA(5, 40)
      5-adic Ring with capped absolute precision 40

  Python

      >>> from sage.all import *
      >>> ZpCA(Integer(5), Integer(40))
      5-adic Ring with capped absolute precision 40

sage.rings.padics.factory.ZpCR(p, prec=None, *args, **kwds)

  A shortcut function to create capped relative \(p\)-adic rings.

  Same functionality as `Zp()`. See documentation for `Zp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: ZpCR(5, 40)
      5-adic Ring with capped relative precision 40

  Python

      >>> from sage.all import *
      >>> ZpCR(Integer(5), Integer(40))
      5-adic Ring with capped relative precision 40

sage.rings.padics.factory.ZpER(p, prec=None, halt=None, secure=False, *args, **kwds)

  A shortcut function to create relaxed \(p\)-adic rings.

  INPUT:

  - `prec` – integer (default: 20); the default precision

  - `halt` – integer (default: twice `prec`); the halting precision

  - `secure` – boolean (default: `False`); if `False`, consider indistinguishable elements at the working precision as equal. Otherwise, raise an error.

  See documentation for `Zp()` for a description of the other input parameters.

  A SHORT INTRODUCTION TO RELAXED \(p\)-ADICS:

  The model for relaxed \(p\)-adics is quite different from any of the other types of \(p\)-adics. In addition to storing a finite approximation, one also stores a method for increasing the precision.

  Relaxed \(p\)-adic rings are created by the constructor `ZpER()`:

  Sage

      sage: R = ZpER(5, print_mode='digits'); R
      5-adic Ring handled with relaxed arithmetics

  Python

      >>> from sage.all import *
      >>> R = ZpER(Integer(5), print_mode='digits'); R
      5-adic Ring handled with relaxed arithmetics

  The precision is not capped in \(R\):

  Sage

      sage: R.precision_cap()
      +Infinity

  [Python]

      >>> from sage.all import *
      >>> R.precision_cap()
      +Infinity

  However, a default precision is fixed. This is the precision at which the elements will be printed:

  Sage

      sage: R.default_prec()
      20

  Python

      >>> from sage.all import *
      >>> R.default_prec()
      20

  A default halting precision is also set. It is the default absolute precision at which the elements will be compared. By default, it is twice the default precision:

  Sage

      sage: R.halting_prec()
      40

  [Python]

      >>> from sage.all import *
      >>> R.halting_prec()
      40

  However, both the default precision and the halting precision can be customized at the creation of the parent as follows:

  Sage

      sage: S = ZpER(5, prec=10, halt=100)
      sage: S.default_prec()
      10
      sage: S.halting_prec()
      100

  Python

      >>> from sage.all import *
      >>> S = ZpER(Integer(5), prec=Integer(10), halt=Integer(100))
      >>> S.default_prec()
      10
      >>> S.halting_prec()
      100

  One creates elements as usual:

  Sage

      sage: a = R(17/42); a
      ...00244200244200244201

      sage: R.random_element()  # random
      ...21013213133412431402

  [Python]

      >>> from sage.all import *
      >>> a = R(Integer(17)/Integer(42)); a
      ...00244200244200244201

      >>> R.random_element()  # random
      ...21013213133412431402

  Here we notice that 20 digits (that is the default precision) are printed. However, the computation model is designed in order to guarantee that more digits of \(a\) will be available on demand. This feature is reflected by the fact that, when we ask for the precision of \(a\), the software answers \(+\infty\):

  Sage

      sage: a.precision_absolute()
      +Infinity

  Python

      >>> from sage.all import *
      >>> a.precision_absolute()
      +Infinity

  Asking for more digits is achieved by the methods `at_precision_absolute()` and `at_precision_relative()`:

  Sage

      sage: a.at_precision_absolute(30)
      ...?244200244200244200244200244201

  [Python]

      >>> from sage.all import *
      >>> a.at_precision_absolute(Integer(30))
      ...?244200244200244200244200244201

  As a shortcut, one can use the bracket operator:

  Sage

      sage: a[:30]
      ...?244200244200244200244200244201

  Python

      >>> from sage.all import *
      >>> a[:Integer(30)]
      ...?244200244200244200244200244201

  Of course, standard operations are supported:

  Sage

      sage: b = R(42/17)
      sage: a + b
      ...03232011214322140002
      sage: a - b
      ...42311334324023403400
      sage: a * b
      ...00000000000000000001
      sage: a / b
      ...12442142113021233401
      sage: sqrt(a)
      ...20042333114021142101

  [Python]

      >>> from sage.all import *
      >>> b = R(Integer(42)/Integer(17))
      >>> a + b
      ...03232011214322140002
      >>> a - b
      ...42311334324023403400
      >>> a * b
      ...00000000000000000001
      >>> a / b
      ...12442142113021233401
      >>> sqrt(a)
      ...20042333114021142101

  We observe again that only 20 digits are printed but, as before, more digits are available on demand:

  Sage

      sage: sqrt(a)[:30]
      ...?142443342120042333114021142101

  Python

      >>> from sage.all import *
      >>> sqrt(a)[:Integer(30)]
      ...?142443342120042333114021142101

  Equality tests

  Checking equalities between relaxed \(p\)-adics is a bit subtle and can sometimes be puzzling at first glance.

  When the parent is created with `secure=False` (which is the default), elements are compared at the current precision, or at the default halting precision if it is higher:

  Sage

      sage: a == b
      False

      sage: a == sqrt(a)^2
      True
      sage: a == sqrt(a)^2 + 5^50
      True

  [Python]

      >>> from sage.all import *
      >>> a == b
      False

      >>> a == sqrt(a)**Integer(2)
      True
      >>> a == sqrt(a)**Integer(2) + Integer(5)**Integer(50)
      True

  In the above example, the halting precision is \(40\); it is the reason why a congruence modulo \(5^50\) is considered as an equality. However, if both sides of the equalities have been previously computed with more digits, those digits are taken into account. Hence comparing two elements at different times can produce different results:

  Sage

      sage: aa = sqrt(a)^2 + 5^50
      sage: a == aa
      True
      sage: a[:60]
      ...?244200244200244200244200244200244200244200244200244200244201
      sage: aa[:60]
      ...?244200244300244200244200244200244200244200244200244200244201
      sage: a == aa
      False

  Python

      >>> from sage.all import *
      >>> aa = sqrt(a)**Integer(2) + Integer(5)**Integer(50)
      >>> a == aa
      True
      >>> a[:Integer(60)]
      ...?244200244200244200244200244200244200244200244200244200244201
      >>> aa[:Integer(60)]
      ...?244200244300244200244200244200244200244200244200244200244201
      >>> a == aa
      False

  This annoying situation, where the output of `a == aa` may change depending on previous computations, cannot occur when the parent is created with `secure=True`. Indeed, in this case, if the equality cannot be decided, an error is raised:

  Sage

      sage: S = ZpER(5, secure=True)
      sage: u = S.random_element()
      sage: uu = u + 5^50
      sage: u == uu
      Traceback (most recent call last):
      ...
      PrecisionError: unable to decide equality; try to bound precision

      sage: u[:60] == uu
      False

  [Python]

      >>> from sage.all import *
      >>> S = ZpER(Integer(5), secure=True)
      >>> u = S.random_element()
      >>> uu = u + Integer(5)**Integer(50)
      >>> u == uu
      Traceback (most recent call last):
      ...
      PrecisionError: unable to decide equality; try to bound precision

      >>> u[:Integer(60)] == uu
      False

  Self-referent numbers

  A quite interesting feature with relaxed \(p\)-adics is the possibility to create (in some cases) self-referent numbers. Here is an example. We first declare a new variable as follows:

  Sage

      sage: x = R.unknown(); x
      ...?.0

  Python

      >>> from sage.all import *
      >>> x = R.unknown(); x
      ...?.0

  We then use the method `set()` to define \(x\) by writing down an equation it satisfies:

  Sage

      sage: x.set(1 + 5*x^2)
      True

  [Python]

      >>> from sage.all import *
      >>> x.set(Integer(1) + Integer(5)*x**Integer(2))
      True

  The variable \(x\) now contains the unique solution of the equation \(x = 1 + 5 x^2\):

  Sage

      sage: x
      ...04222412141121000211

  Python

      >>> from sage.all import *
      >>> x
      ...04222412141121000211

  This works because the \(n\)-th digit of the right hand size of the defining equation only involves the \(i\)-th digits of \(x\) with \(i < n\) (this is due to the factor \(5\)).

  As a comparison, the following does not work:

  Sage

      sage: y = R.unknown()
      sage: y.set(1 + 3*y^2)
      True
      sage: y
      ...?.0
      sage: y[:20]
      Traceback (most recent call last):
      ...
      RecursionError: definition looks circular

  [Python]

      >>> from sage.all import *
      >>> y = R.unknown()
      >>> y.set(Integer(1) + Integer(3)*y**Integer(2))
      True
      >>> y
      ...?.0
      >>> y[:Integer(20)]
      Traceback (most recent call last):
      ...
      RecursionError: definition looks circular

  Self-referent definitions also work with systems of equations:

  Sage

      sage: u = R.unknown()
      sage: v = R.unknown()
      sage: w = R.unknown()
      sage: u.set(1 + 2*v + 3*w^2 + 5*u*v*w)
      True
      sage: v.set(2 + 4*w + sqrt(1 + 5*u + 10*v + 15*w))
      True
      sage: w.set(3 + 25*(u*v + v*w + u*w))
      True
      sage: u
      ...31203130103131131433
      sage: v
      ...33441043031103114240
      sage: w
      ...30212422041102444403

  Python

      >>> from sage.all import *
      >>> u = R.unknown()
      >>> v = R.unknown()
      >>> w = R.unknown()
      >>> u.set(Integer(1) + Integer(2)*v + Integer(3)*w**Integer(2) + Integer(5)*u*v*w)
      True
      >>> v.set(Integer(2) + Integer(4)*w + sqrt(Integer(1) + Integer(5)*u + Integer(10)*v + Integer(15)*w))
      True
      >>> w.set(Integer(3) + Integer(25)*(u*v + v*w + u*w))
      True
      >>> u
      ...31203130103131131433
      >>> v
      ...33441043031103114240
      >>> w
      ...30212422041102444403

sage.rings.padics.factory.ZpFM(p, prec=None, *args, **kwds)

  A shortcut function to create fixed modulus \(p\)-adic rings.

  See documentation for `Zp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: ZpFM(5, 40)
      5-adic Ring of fixed modulus 5^40

  Python

      >>> from sage.all import *
      >>> ZpFM(Integer(5), Integer(40))
      5-adic Ring of fixed modulus 5^40

sage.rings.padics.factory.ZpFP(p, prec=None, *args, **kwds)

  A shortcut function to create floating point \(p\)-adic rings.

  Same functionality as `Zp()`. See documentation for `Zp()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: ZpFP(5, 40)
      5-adic Ring with floating precision 40

  Python

      >>> from sage.all import *
      >>> ZpFP(Integer(5), Integer(40))
      5-adic Ring with floating precision 40

sage.rings.padics.factory.ZpLC(p, prec=None, *args, **kwds)

  A shortcut function to create \(p\)-adic rings with lattice precision (precision is encoded by a lattice in a large vector space and tracked using automatic differentiation).

  See documentation for `Zp()` for a description of the input parameters.

  EXAMPLES:

  Below is a small demo of the features by this model of precision:

  Sage

      sage: R = ZpLC(3, print_mode='terse')
      sage: R
      3-adic Ring with lattice-cap precision

      sage: x = R(1,10)

  Python

      >>> from sage.all import *
      >>> R = ZpLC(Integer(3), print_mode='terse')
      >>> R
      3-adic Ring with lattice-cap precision

      >>> x = R(Integer(1),Integer(10))

  Of course, when we multiply by 3, we gain one digit of absolute precision:

  Sage

      sage: 3*x
      3 + O(3^11)

  [Python]

      >>> from sage.all import *
      >>> Integer(3)*x
      3 + O(3^11)

  The lattice precision machinery sees this even if we decompose the computation into several steps:

  Sage

      sage: y = x+x
      sage: y
      2 + O(3^10)
      sage: x + y
      3 + O(3^11)

  Python

      >>> from sage.all import *
      >>> y = x+x
      >>> y
      2 + O(3^10)
      >>> x + y
      3 + O(3^11)

  The same works for the multiplication:

  Sage

      sage: z = x^2
      sage: z
      1 + O(3^10)
      sage: x*z
      1 + O(3^11)

  [Python]

      >>> from sage.all import *
      >>> z = x**Integer(2)
      >>> z
      1 + O(3^10)
      >>> x*z
      1 + O(3^11)

  This can be more surprising when we are working with elements given at different precisions:

  Sage

      sage: R = ZpLC(2, print_mode='terse')
      sage: x = R(1,10)
      sage: y = R(1,5)
      sage: z = x+y; z
      2 + O(2^5)
      sage: t = x-y; t
      O(2^5)
      sage: z+t  # observe that z+t = 2*x
      2 + O(2^11)
      sage: z-t  # observe that z-t = 2*y
      2 + O(2^6)

      sage: x = R(28888,15)
      sage: y = R(204,10)
      sage: z = x/y; z
      242 + O(2^9)
      sage: z*y  # which is x
      28888 + O(2^15)

  Python

      >>> from sage.all import *
      >>> R = ZpLC(Integer(2), print_mode='terse')
      >>> x = R(Integer(1),Integer(10))
      >>> y = R(Integer(1),Integer(5))
      >>> z = x+y; z
      2 + O(2^5)
      >>> t = x-y; t
      O(2^5)
      >>> z+t  # observe that z+t = 2*x
      2 + O(2^11)
      >>> z-t  # observe that z-t = 2*y
      2 + O(2^6)

      >>> x = R(Integer(28888),Integer(15))
      >>> y = R(Integer(204),Integer(10))
      >>> z = x/y; z
      242 + O(2^9)
      >>> z*y  # which is x
      28888 + O(2^15)

  The SOMOS sequence is the sequence defined by the recurrence:

  \[u_n = \frac {u_{n-1} u_{n-3} + u_{n-2}^2} {u_{n-4}}\]

  It is known for its numerical instability. On the one hand, one can show that if the initial values are invertible in \(\ZZ_p\) and known at precision \(O(p^N)\) then all the next terms of the SOMOS sequence will be known at the same precision as well. On the other hand, because of the division, when we unroll the recurrence, we loose a lot of precision. Observe:

  Sage

      sage: R = Zp(2, 30, print_mode='terse')
      sage: a,b,c,d = R(1,15), R(1,15), R(1,15), R(3,15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      13 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      55 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      21975 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      6639 + O(2^13)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      7186 + O(2^13)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      569 + O(2^13)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      253 + O(2^13)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4149 + O(2^13)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      2899 + O(2^12)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      3072 + O(2^12)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      349 + O(2^12)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      619 + O(2^12)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      243 + O(2^12)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      3 + O(2^2)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      2 + O(2^2)

  [Python]

      >>> from sage.all import *
      >>> R = Zp(Integer(2), Integer(30), print_mode='terse')
      >>> a,b,c,d = R(Integer(1),Integer(15)), R(Integer(1),Integer(15)), R(Integer(1),Integer(15)), R(Integer(3),Integer(15))
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      13 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      55 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      21975 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      6639 + O(2^13)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      7186 + O(2^13)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      569 + O(2^13)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      253 + O(2^13)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4149 + O(2^13)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      2899 + O(2^12)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      3072 + O(2^12)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      349 + O(2^12)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      619 + O(2^12)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      243 + O(2^12)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      3 + O(2^2)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      2 + O(2^2)

  If instead, we use the lattice precision, everything goes well:

  Sage

      sage: R = ZpLC(2, 30, print_mode='terse')
      sage: a,b,c,d = R(1,15), R(1,15), R(1,15), R(3,15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      13 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      55 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      21975 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      23023 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      31762 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      16953 + O(2^15)
      sage: a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      16637 + O(2^15)

      sage: for _ in range(100):
      ....:     a,b,c,d = b,c,d,(b*d+c*c)/a
      sage: a
      15519 + O(2^15)
      sage: b
      32042 + O(2^15)
      sage: c
      17769 + O(2^15)
      sage: d
      20949 + O(2^15)

  Python

      >>> from sage.all import *
      >>> R = ZpLC(Integer(2), Integer(30), print_mode='terse')
      >>> a,b,c,d = R(Integer(1),Integer(15)), R(Integer(1),Integer(15)), R(Integer(1),Integer(15)), R(Integer(3),Integer(15))
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      4 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      13 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      55 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      21975 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      23023 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      31762 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      16953 + O(2^15)
      >>> a,b,c,d = b,c,d,(b*d+c*c)/a; print(d)
      16637 + O(2^15)

      >>> for _ in range(Integer(100)):
      ...     a,b,c,d = b,c,d,(b*d+c*c)/a
      >>> a
      15519 + O(2^15)
      >>> b
      32042 + O(2^15)
      >>> c
      17769 + O(2^15)
      >>> d
      20949 + O(2^15)

  ALGORITHM:

  The precision is global. It is encoded by a lattice in a huge vector space whose dimension is the number of elements having this parent. Precision is tracked using automatic differentiation techniques (see [CRV2014] and [CRV2018]).

  Concretely, this precision datum is an instance of the class `sage.rings.padic.lattice_precision.PrecisionLattice`. It is attached to the parent and is created at the same time as the parent. (It is actually a bit more subtle because two different parents may share the same instance; this happens for instance for a \(p\)-adic ring and its field of fractions.)

  This precision datum is accessible through the method `precision()`:

  Sage

      sage: R = ZpLC(5, print_mode='terse')
      sage: prec = R.precision()
      sage: prec
      Precision lattice on 0 objects

  [Python]

      >>> from sage.all import *
      >>> R = ZpLC(Integer(5), print_mode='terse')
      >>> prec = R.precision()
      >>> prec
      Precision lattice on 0 objects

  This instance knows about all elements of the parent. It is automatically updated when a new element (of this parent) is created:

  Sage

      sage: x = R(3513,10)
      sage: prec
      Precision lattice on 1 object
      sage: y = R(176,5)
      sage: prec
      Precision lattice on 2 objects
      sage: z = R.random_element()
      sage: prec
      Precision lattice on 3 objects

  Python

      >>> from sage.all import *
      >>> x = R(Integer(3513),Integer(10))
      >>> prec
      Precision lattice on 1 object
      >>> y = R(Integer(176),Integer(5))
      >>> prec
      Precision lattice on 2 objects
      >>> z = R.random_element()
      >>> prec
      Precision lattice on 3 objects

  The method `tracked_elements()` provides the list of all tracked elements:

  Sage

      sage: prec.tracked_elements()
      [3513 + O(5^10), 176 + O(5^5), ...]

  [Python]

      >>> from sage.all import *
      >>> prec.tracked_elements()
      [3513 + O(5^10), 176 + O(5^5), ...]

  Similarly, when a variable is collected by the garbage collector, the precision lattice is updated. Note however that the update might be delayed. We can force it with the method `del_elements()`:

  Sage

      sage: z = 0
      sage: prec  # random output, could be 2 objects if the garbage collector is fast
      Precision lattice on 3 objects
      sage: prec.del_elements()
      sage: prec
      Precision lattice on 2 objects

  Python

      >>> from sage.all import *
      >>> z = Integer(0)
      >>> prec  # random output, could be 2 objects if the garbage collector is fast
      Precision lattice on 3 objects
      >>> prec.del_elements()
      >>> prec
      Precision lattice on 2 objects

  The method `precision_lattice()` returns (a matrix defining) the lattice that models the precision. Here we have:

  Sage

      sage: prec.precision_lattice()
      [9765625       0]
      [      0    3125]

  [Python]

      >>> from sage.all import *
      >>> prec.precision_lattice()
      [9765625       0]
      [      0    3125]

  Observe that \(5^10 = 9765625\) and \(5^5 = 3125\). The above matrix then reflects the precision on \(x\) and \(y\).

  Now, observe how the precision lattice changes while performing computations:

  Sage

      sage: x, y = 3*x+2*y, 2*(x-y)
      sage: prec.del_elements()
      sage: prec.precision_lattice()
      [    3125 48825000]
      [       0 48828125]

  Python

      >>> from sage.all import *
      >>> x, y = Integer(3)*x+Integer(2)*y, Integer(2)*(x-y)
      >>> prec.del_elements()
      >>> prec.precision_lattice()
      [    3125 48825000]
      [       0 48828125]

  The matrix we get is no longer diagonal, meaning that some digits of precision are diffused among the two new elements \(x\) and \(y\). They nevertheless show up when we compute for instance \(x+y\):

  Sage

      sage: x
      1516 + O(5^5)
      sage: y
      424 + O(5^5)
      sage: x+y
      17565 + O(5^11)

  [Python]

      >>> from sage.all import *
      >>> x
      1516 + O(5^5)
      >>> y
      424 + O(5^5)
      >>> x+y
      17565 + O(5^11)

  These diffused digits of precision (which are tracked but do not appear on the printing) allow to be always sharp on precision.

  NOTE:

  Each elementary operation requires significant manipulations on the precision lattice and therefore is costly. Precisely:

  - The creation of a new element has a cost \(O(n)\) where \(n\) is the number of tracked elements.

  - The destruction of one element has a cost \(O(m^2)\) where \(m\) is the distance between the destroyed element and the last one. Fortunately, it seems that \(m\) tends to be small in general (the dynamics of the list of tracked elements is rather close to that of a stack).

  It is nevertheless still possible to manipulate several hundred variables (e.g. square matrices of size 5 or polynomials of degree 20).

  The class `PrecisionLattice` provides several features for introspection, especially concerning timings. See `history()` and `timings()` for details.

  See also

  `ZpLF()`

sage.rings.padics.factory.ZpLF(p, prec=None, *args, **kwds)

  A shortcut function to create \(p\)-adic rings where precision is encoded by a module in a large vector space.

  See documentation for `Zp()` for a description of the input parameters.

  Note

  The precision is tracked using automatic differentiation techniques (see [CRV2018] and [CRV2014]). Floating point \(p\)-adic numbers are used for the computation of the differential (which is then not exact).

  EXAMPLES:

  Sage

      sage: R = ZpLF(5, 40)
      sage: R
      5-adic Ring with lattice-float precision

  Python

      >>> from sage.all import *
      >>> R = ZpLF(Integer(5), Integer(40))
      >>> R
      5-adic Ring with lattice-float precision

  See also

  `ZpLC()`

class sage.rings.padics.factory.Zp_class

  Bases: `UniqueFactory`

  A creation function for \(p\)-adic rings.

  INPUT:

  - `p` – integer; the \(p\) in \(\ZZ_p\)

  - `prec` – integer (default: 20); the precision cap of the ring. In the lattice capped case, `prec` can either be a pair (`relative_cap`, `absolute_cap`) or an integer (understood as relative cap). In the relaxed case, `prec` can be either a pair (`default_prec`, `halting_prec`) or an integer (understood as default precision). Except for the fixed modulus and floating point cases, individual elements keep track of their own precision. See TYPES and PRECISION below.

  - `type` – string (default: `'capped-rel'`); valid types are `'capped-rel'`, `'capped-abs'`, `'fixed-mod'`, `'floating-point'`, `'lattice-cap'`, `'lattice-float'`, `'relaxed'`. See TYPES and PRECISION below.

  - `print_mode` – string (default: `None`); valid modes are `'series'`, `'val-unit'`, `'terse'`, `'digits'`, and `'bars'`. See PRINTING below.

  - `names` – string or tuple (defaults to a string representation of \(p\)); what to use whenever \(p\) is printed

  - `print_pos` – boolean (default: `None`); whether to only use positive integers in the representations of elements. See PRINTING below.

  - `print_sep` – string (default: `None`); the separator character used in the `'bars'` mode. See PRINTING below.

  - `print_alphabet` – tuple (default: `None`); the encoding into digits for use in the `'digits'` mode. See PRINTING below.

  - `print_max_terms` – integer (default: `None`); the maximum number of terms shown. See PRINTING below.

  - `show_prec` – boolean (default: `None`) whether to show the precision for elements. See PRINTING below.

  - `check` – boolean (default: `True`) whether to check if \(p\) is prime. Non-prime input may cause seg-faults (but can also be useful for base \(n\) expansions for example)

  - `label` – string (default: `None`); used for lattice precision to create parents with different lattices

  OUTPUT: the corresponding \(p\)-adic ring

  TYPES AND PRECISION:

  There are two main types of precision. The first is relative precision; it gives the number of known \(p\)-adic digits:

  Sage

      sage: R = Zp(5, 20, 'capped-rel', 'series'); a = R(675); a
      2*5^2 + 5^4 + O(5^22)
      sage: a.precision_relative()
      20

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5), Integer(20), 'capped-rel', 'series'); a = R(Integer(675)); a
      2*5^2 + 5^4 + O(5^22)
      >>> a.precision_relative()
      20

  The second type of precision is absolute precision, which gives the power of \(p\) that this element is defined modulo:

  Sage

      sage: a.precision_absolute()
      22

  [Python]

      >>> from sage.all import *
      >>> a.precision_absolute()
      22

  There are several types of \(p\)-adic rings, depending on the methods used for tracking precision. Namely, we have:

  - capped relative rings (`type='capped-rel'`)

  - capped absolute rings (`type='capped-abs'`)

  - fixed modulus rings (`type='fixed-mod'`)

  - floating point rings (`type='floating-point'`)

  - lattice precision rings (`type='lattice-cap'` or `type='lattice-float'`)

  - exact fields with relaxed arithmetics (`type='relaxed'`)

  In the capped relative case, the relative precision of an element is restricted to be at most a certain value, specified at the creation of the field. Individual elements also store their own precision, so the effect of various arithmetic operations on precision is tracked. When you cast an exact element into a capped relative field, it truncates it to the precision cap of the field.

  Sage

      sage: R = Zp(5, 5, 'capped-rel', 'series'); a = R(4006); a
      1 + 5 + 2*5^3 + 5^4 + O(5^5)
      sage: b = R(4025); b
      5^2 + 2*5^3 + 5^4 + 5^5 + O(5^7)
      sage: a + b
      1 + 5 + 5^2 + 4*5^3 + 2*5^4 + O(5^5)

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5), Integer(5), 'capped-rel', 'series'); a = R(Integer(4006)); a
      1 + 5 + 2*5^3 + 5^4 + O(5^5)
      >>> b = R(Integer(4025)); b
      5^2 + 2*5^3 + 5^4 + 5^5 + O(5^7)
      >>> a + b
      1 + 5 + 5^2 + 4*5^3 + 2*5^4 + O(5^5)

  In the capped absolute type, instead of having a cap on the relative precision of an element there is instead a cap on the absolute precision. Elements still store their own precisions, and as with the capped relative case, exact elements are truncated when cast into the ring.

  Sage

      sage: R = Zp(5, 5, 'capped-abs', 'series'); a = R(4005); a
      5 + 2*5^3 + 5^4 + O(5^5)
      sage: b = R(4025); b
      5^2 + 2*5^3 + 5^4 + O(5^5)
      sage: a * b
      5^3 + 2*5^4 + O(5^5)
      sage: (a * b) // 5^3
      1 + 2*5 + O(5^2)

  [Python]

      >>> from sage.all import *
      >>> R = Zp(Integer(5), Integer(5), 'capped-abs', 'series'); a = R(Integer(4005)); a
      5 + 2*5^3 + 5^4 + O(5^5)
      >>> b = R(Integer(4025)); b
      5^2 + 2*5^3 + 5^4 + O(5^5)
      >>> a * b
      5^3 + 2*5^4 + O(5^5)
      >>> (a * b) // Integer(5)**Integer(3)
      1 + 2*5 + O(5^2)

  The fixed modulus type is the leanest of the \(p\)-adic rings: it is basically just a wrapper around \(\ZZ / p^n \ZZ\) providing a unified interface with the rest of the \(p\)-adics. This is the type you should use if your sole interest is speed. It does not track precision of elements.

  Sage

      sage: R = Zp(5,5,'fixed-mod','series'); a = R(4005); a
      5 + 2*5^3 + 5^4
      sage: a // 5
      1 + 2*5^2 + 5^3

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5),Integer(5),'fixed-mod','series'); a = R(Integer(4005)); a
      5 + 2*5^3 + 5^4
      >>> a // Integer(5)
      1 + 2*5^2 + 5^3

  The floating point case is similar to the fixed modulus type in that elements do not track their own precision. However, relative precision is truncated with each operation rather than absolute precision.

  On the contrary, the lattice type tracks precision using lattices and automatic differentiation. It is rather slow but provides sharp (often optimal) results regarding precision. We refer to the documentation of the function `ZpLC()` for a small demonstration of the capabilities of this precision model.

  Finally, the model for relaxed \(p\)-adics is quite different from any of the other types. In addition to storing a finite approximation, one also stores a method for increasing the precision. A quite interesting feature with relaxed \(p\)-adics is the possibility to create (in some cases) self-referent numbers, that are numbers whose \(n\)-th digit is defined by the previous ones. We refer to the documentation of the function `ZpL()` for a small demonstration of the capabilities of this precision model.

  PRINTING:

  There are many different ways to print \(p\)-adic elements. The way elements of a given ring print is controlled by options passed in at the creation of the ring. There are five basic printing modes (`'series'`, `'val-unit'`, `'terse'`, `'digits'` and `'bars'`), as well as various options that either hide some information in the print representation or sometimes make print representations more compact. Note that the printing options affect whether different \(p\)-adic fields are considered equal.

  1. series: elements are displayed as series in \(p\).

     Sage

         sage: R = Zp(5, print_mode='series'); a = R(70700); a
         3*5^2 + 3*5^4 + 2*5^5 + 4*5^6 + O(5^22)
         sage: b = R(-70700); b
         2*5^2 + 4*5^3 + 5^4 + 2*5^5 + 4*5^7 + 4*5^8 + 4*5^9 + 4*5^10 + 4*5^11
          + 4*5^12 + 4*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 4*5^17 + 4*5^18
          + 4*5^19 + 4*5^20 + 4*5^21 + O(5^22)

     Python

         >>> from sage.all import *
         >>> R = Zp(Integer(5), print_mode='series'); a = R(Integer(70700)); a
         3*5^2 + 3*5^4 + 2*5^5 + 4*5^6 + O(5^22)
         >>> b = R(-Integer(70700)); b
         2*5^2 + 4*5^3 + 5^4 + 2*5^5 + 4*5^7 + 4*5^8 + 4*5^9 + 4*5^10 + 4*5^11
          + 4*5^12 + 4*5^13 + 4*5^14 + 4*5^15 + 4*5^16 + 4*5^17 + 4*5^18
          + 4*5^19 + 4*5^20 + 4*5^21 + O(5^22)

  > print_pos controls whether negatives can be used in the coefficients of powers of \(p\).

  > [Sage]

  >     sage: S = Zp(5, print_mode='series', print_pos=False); a = S(70700); a
  >     -2*5^2 + 5^3 - 2*5^4 - 2*5^5 + 5^7 + O(5^22)
  >     sage: b = S(-70700); b
  >     2*5^2 - 5^3 + 2*5^4 + 2*5^5 - 5^7 + O(5^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Zp(Integer(5), print_mode='series', print_pos=False); a = S(Integer(70700)); a
  >     -2*5^2 + 5^3 - 2*5^4 - 2*5^5 + 5^7 + O(5^22)
  >     >>> b = S(-Integer(70700)); b
  >     2*5^2 - 5^3 + 2*5^4 + 2*5^5 - 5^7 + O(5^22)

  > print_max_terms limits the number of terms that appear.

  > [Sage]

  >     sage: T = Zp(5, print_mode='series', print_max_terms=4); b = R(-70700); b
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Zp(Integer(5), print_mode='series', print_max_terms=Integer(4)); b = R(-Integer(70700)); b
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > names affects how the prime is printed.

  > [Sage]

  >     sage: U.<p> = Zp(5); p
  >     p + O(p^21)

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Zp(Integer(5), names=('p',)); (p,) = U._first_ngens(1); p
  >     p + O(p^21)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > [Sage]

  >     sage: Zp(5, show_prec=False)(6)
  >     1 + 5

  > [Python]

  >     >>> from sage.all import *
  >     >>> Zp(Integer(5), show_prec=False)(Integer(6))
  >     1 + 5

  > print_sep and print_alphabet have no effect.

  > Note that print options affect equality:

  > [Sage]

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  2. val-unit: elements are displayed as \(p^k u\):

     Sage

         sage: R = Zp(5, print_mode='val-unit'); a = R(70700); a
         5^2 * 2828 + O(5^22)
         sage: b = R(-707*5); b
         5 * 95367431639918 + O(5^21)

     Python

         >>> from sage.all import *
         >>> R = Zp(Integer(5), print_mode='val-unit'); a = R(Integer(70700)); a
         5^2 * 2828 + O(5^22)
         >>> b = R(-Integer(707)*Integer(5)); b
         5 * 95367431639918 + O(5^21)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S = Zp(5, print_mode='val-unit', print_pos=False); b = S(-70700); b
  >     5^2 * (-2828) + O(5^22)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Zp(Integer(5), print_mode='val-unit', print_pos=False); b = S(-Integer(70700)); b
  >     5^2 * (-2828) + O(5^22)

  > names affects how the prime is printed.

  > Sage

  >     sage: T = Zp(5, print_mode='val-unit', names='pi'); a = T(70700); a
  >     pi^2 * 2828 + O(pi^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Zp(Integer(5), print_mode='val-unit', names='pi'); a = T(Integer(70700)); a
  >     pi^2 * 2828 + O(pi^22)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: Zp(5, print_mode='val-unit', show_prec=False)(30)
  >     5 * 6

  > Python

  >     >>> from sage.all import *
  >     >>> Zp(Integer(5), print_mode='val-unit', show_prec=False)(Integer(30))
  >     5 * 6

  > print_max_terms, print_sep and print_alphabet have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  3. terse: elements are displayed as an integer in base 10:

     Sage

         sage: R = Zp(5, print_mode='terse'); a = R(70700); a
         70700 + O(5^22)
         sage: b = R(-70700); b
         2384185790944925 + O(5^22)

     Python

         >>> from sage.all import *
         >>> R = Zp(Integer(5), print_mode='terse'); a = R(Integer(70700)); a
         70700 + O(5^22)
         >>> b = R(-Integer(70700)); b
         2384185790944925 + O(5^22)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S = Zp(5, print_mode='terse', print_pos=False); b = S(-70700); b
  >     -70700 + O(5^22)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Zp(Integer(5), print_mode='terse', print_pos=False); b = S(-Integer(70700)); b
  >     -70700 + O(5^22)

  > name affects how the name is printed. Note that this interacts with the choice of shorter string for denominators.

  > Sage

  >     sage: T.<unif> = Zp(5, print_mode='terse'); c = T(-707); c
  >     95367431639918 + O(unif^20)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Zp(Integer(5), print_mode='terse', names=('unif',)); (unif,) = T._first_ngens(1); c = T(-Integer(707)); c
  >     95367431639918 + O(unif^20)

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘bigoh’ (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: Zp(5, print_mode='terse', show_prec=False)(30)
  >     30

  > Python

  >     >>> from sage.all import *
  >     >>> Zp(Integer(5), print_mode='terse', show_prec=False)(Integer(30))
  >     30

  > print_max_terms, print_sep and print_alphabet have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  4. digits: elements are displayed as a string of base \(p\) digits

  > Restriction: you can only use the digits printing mode for small primes. Namely, \(p\) must be less than the length of the alphabet tuple (default alphabet has length 62).

  > Sage

  >     sage: R = Zp(5, print_mode='digits'); a = R(70700); repr(a)
  >     '...4230300'
  >     sage: b = R(-70700); repr(b)
  >     '...4444444444444440214200'

  > Python

  >     >>> from sage.all import *
  >     >>> R = Zp(Integer(5), print_mode='digits'); a = R(Integer(70700)); repr(a)
  >     '...4230300'
  >     >>> b = R(-Integer(70700)); repr(b)
  >     '...4444444444444440214200'

  > Note that it’s not possible to read off the precision from the representation in this mode.

  > print_max_terms limits the number of digits that are printed.

  > Sage

  >     sage: S = Zp(5, print_max_terms=4); S(-70700)
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Zp(Integer(5), print_max_terms=Integer(4)); S(-Integer(70700))
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > print_alphabet controls the symbols used to substitute for digits greater than 9. Defaults to (‘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’):

  > Sage

  >     sage: T = Zp(5, print_mode='digits', print_alphabet=('1','2','3','4','5'))
  >     sage: repr(T(-70700))
  >     '...5555555555555551325311'

  > Python

  >     >>> from sage.all import *
  >     >>> T = Zp(Integer(5), print_mode='digits', print_alphabet=('1','2','3','4','5'))
  >     >>> repr(T(-Integer(70700)))
  >     '...5555555555555551325311'

  > show_prec determines how the precision is printed. It can be either `'none'` (or equivalently `False`), `'dots'` (or equivalently `True`) or `'bigoh'`. The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: repr(Zp(5, 2, print_mode='digits', show_prec=True)(6))
  >     '...11'
  >     sage: repr(Zp(5, 2, print_mode='digits', show_prec='bigoh')(6))
  >     '11 + O(5^2)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> repr(Zp(Integer(5), Integer(2), print_mode='digits', show_prec=True)(Integer(6)))
  >     '...11'
  >     >>> repr(Zp(Integer(5), Integer(2), print_mode='digits', show_prec='bigoh')(Integer(6)))
  >     '11 + O(5^2)'

  > print_pos, name and print_sep have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, S == T
  >     (False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, S == T
  >     (False, False, False)

  5. bars: elements are displayed as a string of base \(p\) digits with separators:

     Sage

         sage: R = Zp(5, print_mode='bars'); a = R(70700); repr(a)
         '...4|2|3|0|3|0|0'
         sage: b = R(-70700); repr(b)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|0|2|1|4|2|0|0'

     Python

         >>> from sage.all import *
         >>> R = Zp(Integer(5), print_mode='bars'); a = R(Integer(70700)); repr(a)
         '...4|2|3|0|3|0|0'
         >>> b = R(-Integer(70700)); repr(b)
         '...4|4|4|4|4|4|4|4|4|4|4|4|4|4|4|0|2|1|4|2|0|0'

  > Again, note that it’s not possible to read off the precision from the representation in this mode.

  > print_pos controls whether the digits can be negative.

  > Sage

  >     sage: S = Zp(5, print_mode='bars', print_pos=False); b = S(-70700); repr(b)
  >     '...-1|0|2|2|-1|2|0|0'

  > Python

  >     >>> from sage.all import *
  >     >>> S = Zp(Integer(5), print_mode='bars', print_pos=False); b = S(-Integer(70700)); repr(b)
  >     '...-1|0|2|2|-1|2|0|0'

  > print_max_terms limits the number of digits that are printed.

  > Sage

  >     sage: T = Zp(5, print_max_terms=4); T(-70700)
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Zp(Integer(5), print_max_terms=Integer(4)); T(-Integer(70700))
  >     2*5^2 + 4*5^3 + 5^4 + 2*5^5 + ... + O(5^22)

  > print_sep controls the separation character.

  > Sage

  >     sage: U = Zp(5, print_mode='bars', print_sep=']['); a = U(70700); repr(a)
  >     '...4][2][3][0][3][0][0'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Zp(Integer(5), print_mode='bars', print_sep=']['); a = U(Integer(70700)); repr(a)
  >     '...4][2][3][0][3][0][0'

  > show_prec determines how the precision is printed. It can be either ‘none’ (or equivalently `False`), ‘dots’ (or equivalently `True`) or ‘bigoh’. The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: repr(Zp(5, 2, print_mode='bars', show_prec=True)(6))
  >     '...1|1'
  >     sage: repr(Zp(5, 2, print_mode='bars', show_prec=False)(6))
  >     '1|1'

  > [Python]

  >     >>> from sage.all import *
  >     >>> repr(Zp(Integer(5), Integer(2), print_mode='bars', show_prec=True)(Integer(6)))
  >     '...1|1'
  >     >>> repr(Zp(Integer(5), Integer(2), print_mode='bars', show_prec=False)(Integer(6)))
  >     '1|1'

  > name and print_alphabet have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  EXAMPLES:

  We allow non-prime \(p\), but only if `check=False`. Note that some features will not work.

  Sage

      sage: K = Zp(15, check=False); a = K(999); a
      9 + 6*15 + 4*15^2 + O(15^20)

  Python

      >>> from sage.all import *
      >>> K = Zp(Integer(15), check=False); a = K(Integer(999)); a
      9 + 6*15 + 4*15^2 + O(15^20)

  We create rings with various parameters:

  Sage

      sage: Zp(7)
      7-adic Ring with capped relative precision 20
      sage: Zp(9)
      Traceback (most recent call last):
      ...
      ValueError: p must be prime
      sage: Zp(17, 5)
      17-adic Ring with capped relative precision 5
      sage: Zp(17, 5)(-1)
      16 + 16*17 + 16*17^2 + 16*17^3 + 16*17^4 + O(17^5)

  [Python]

      >>> from sage.all import *
      >>> Zp(Integer(7))
      7-adic Ring with capped relative precision 20
      >>> Zp(Integer(9))
      Traceback (most recent call last):
      ...
      ValueError: p must be prime
      >>> Zp(Integer(17), Integer(5))
      17-adic Ring with capped relative precision 5
      >>> Zp(Integer(17), Integer(5))(-Integer(1))
      16 + 16*17 + 16*17^2 + 16*17^3 + 16*17^4 + O(17^5)

  It works even with a fairly huge cap:

  Sage

      sage: Zp(next_prime(10^50), 100000)
      100000000000000000000000000000000000000000000000151-adic Ring
       with capped relative precision 100000

  Python

      >>> from sage.all import *
      >>> Zp(next_prime(Integer(10)**Integer(50)), Integer(100000))
      100000000000000000000000000000000000000000000000151-adic Ring
       with capped relative precision 100000

  We create each type of ring:

  Sage

      sage: Zp(7, 20, 'capped-rel')
      7-adic Ring with capped relative precision 20
      sage: Zp(7, 20, 'fixed-mod')
      7-adic Ring of fixed modulus 7^20
      sage: Zp(7, 20, 'capped-abs')
      7-adic Ring with capped absolute precision 20

  [Python]

      >>> from sage.all import *
      >>> Zp(Integer(7), Integer(20), 'capped-rel')
      7-adic Ring with capped relative precision 20
      >>> Zp(Integer(7), Integer(20), 'fixed-mod')
      7-adic Ring of fixed modulus 7^20
      >>> Zp(Integer(7), Integer(20), 'capped-abs')
      7-adic Ring with capped absolute precision 20

  We create a capped relative ring with each print mode:

  Sage

      sage: k = Zp(7, 8, print_mode='series'); k
      7-adic Ring with capped relative precision 8
      sage: k(7*(19))
      5*7 + 2*7^2 + O(7^9)
      sage: k(7*(-19))
      2*7 + 4*7^2 + 6*7^3 + 6*7^4 + 6*7^5 + 6*7^6 + 6*7^7 + 6*7^8 + O(7^9)

  Python

      >>> from sage.all import *
      >>> k = Zp(Integer(7), Integer(8), print_mode='series'); k
      7-adic Ring with capped relative precision 8
      >>> k(Integer(7)*(Integer(19)))
      5*7 + 2*7^2 + O(7^9)
      >>> k(Integer(7)*(-Integer(19)))
      2*7 + 4*7^2 + 6*7^3 + 6*7^4 + 6*7^5 + 6*7^6 + 6*7^7 + 6*7^8 + O(7^9)

  Sage

      sage: k = Zp(7, print_mode='val-unit'); k
      7-adic Ring with capped relative precision 20
      sage: k(7*(19))
      7 * 19 + O(7^21)
      sage: k(7*(-19))
      7 * 79792266297611982 + O(7^21)

  [Python]

      >>> from sage.all import *
      >>> k = Zp(Integer(7), print_mode='val-unit'); k
      7-adic Ring with capped relative precision 20
      >>> k(Integer(7)*(Integer(19)))
      7 * 19 + O(7^21)
      >>> k(Integer(7)*(-Integer(19)))
      7 * 79792266297611982 + O(7^21)

  Sage

      sage: k = Zp(7, print_mode='terse'); k
      7-adic Ring with capped relative precision 20
      sage: k(7*(19))
      133 + O(7^21)
      sage: k(7*(-19))
      558545864083283874 + O(7^21)

  Python

      >>> from sage.all import *
      >>> k = Zp(Integer(7), print_mode='terse'); k
      7-adic Ring with capped relative precision 20
      >>> k(Integer(7)*(Integer(19)))
      133 + O(7^21)
      >>> k(Integer(7)*(-Integer(19)))
      558545864083283874 + O(7^21)

  Note that \(p\)-adic rings are cached (via weak references):

  Sage

      sage: a = Zp(7); b = Zp(7)
      sage: a is b
      True

  [Python]

      >>> from sage.all import *
      >>> a = Zp(Integer(7)); b = Zp(Integer(7))
      >>> a is b
      True

  We create some elements in various rings:

  Sage

      sage: R = Zp(5); a = R(4); a
      4 + O(5^20)
      sage: S = Zp(5, 10, type = 'capped-abs'); b = S(2); b
      2 + O(5^10)
      sage: a + b
      1 + 5 + O(5^10)

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5)); a = R(Integer(4)); a
      4 + O(5^20)
      >>> S = Zp(Integer(5), Integer(10), type = 'capped-abs'); b = S(Integer(2)); b
      2 + O(5^10)
      >>> a + b
      1 + 5 + O(5^10)

  create_key(p, prec=None, type='capped-rel', print_mode=None, names=None, ram_name=None, print_pos=None, print_sep=None, print_alphabet=None, print_max_terms=None, show_prec=None, check=True, label=None)

    Create a key from input parameters for `Zp`.

    See the documentation for `Zp()` for more information.

  create_object(version, key)

    Create an object using a given key.

    See the documentation for `Zp()` for more information.

sage.rings.padics.factory.Zq(q, prec=None, type='capped-rel', modulus=None, names=None, print_mode=None, ram_name=None, res_name=None, print_pos=None, print_sep=None, print_max_ram_terms=None, print_max_unram_terms=None, print_max_terse_terms=None, show_prec=None, check=True, implementation='FLINT')

  Given a prime power \(q = p^n\), return the unique unramified extension of \(\ZZ_p\) of degree \(n\).

  INPUT:

  - `q` – integer, list or tuple: the prime power in \(\QQ_q\). Or a `Factorization` object, single element list `[(p, n)]` where `p` is a prime and `n` a positive integer, or the pair `(p, n)`.

  - `prec` – integer (default: 20); the precision cap of the field. Individual elements keep track of their own precision. See TYPES and PRECISION below.

  - `type` – string (default: `'capped-rel'`); valid types are `'capped-abs'`, `'capped-rel'`, `'fixed-mod'`, and `'floating-point'`. See TYPES and PRECISION below.

  - `modulus` – polynomial (default: `None`); a polynomial defining an unramified extension of \(\ZZ_p\). See MODULUS below.

  - `names` – string or tuple (`None` is only allowed when \(q=p\)); the name of the generator, reducing to a generator of the residue field

  - `print_mode` – string (default: `None`); valid modes are `'series'`, `'val-unit'`, `'terse'`, and `'bars'`. See PRINTING below.

  - `ram_name` – string (defaults to string representation of \(p\) if `None`); `ram_name` controls how the prime is printed. See PRINTING below.

  - `res_name` – string (defaults to `None`, which corresponds to adding a `'0'` to the end of the name); controls how elements of the residue field print.

  - `print_pos` – boolean (default: `None`); whether to only use positive integers in the representations of elements. See PRINTING below.

  - `print_sep` – string (default: `None`); the separator character used in the `'bars'` mode. See PRINTING below.

  - `print_max_ram_terms` – integer (default: `None`); the maximum number of powers of \(p\) shown. See PRINTING below.

  - `print_max_unram_terms` – integer (default: `None`) the maximum number of entries shown in a coefficient of \(p\). See PRINTING below.

  - `print_max_terse_terms` – integer (default: `None`); the maximum number of terms in the polynomial representation of an element (using `'terse'`). See PRINTING below.

  - `show_prec` – boolean (default: `None`); whether to show the precision for elements. See PRINTING below.

  - `check` – boolean (default: `True`) whether to check inputs

  - `implementation` – string (default: `'FLINT'`); which implementation to use. `'NTL'` is the other option.

  OUTPUT: the corresponding unramified \(p\)-adic ring

  TYPES AND PRECISION:

  There are two types of precision for a \(p\)-adic element. The first is relative precision (default), which gives the number of known \(p\)-adic digits:

  Sage

      sage: R.<a> = Zq(25, 20, 'capped-rel', print_mode='series'); b = 25*a; b
      a*5^2 + O(5^22)
      sage: b.precision_relative()
      20

  Python

      >>> from sage.all import *
      >>> R = Zq(Integer(25), Integer(20), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); b = Integer(25)*a; b
      a*5^2 + O(5^22)
      >>> b.precision_relative()
      20

  The second type of precision is absolute precision, which gives the power of \(p\) that this element is defined modulo:

  Sage

      sage: b.precision_absolute()
      22

  [Python]

      >>> from sage.all import *
      >>> b.precision_absolute()
      22

  There are many types of \(p\)-adic rings: capped relative rings (`type='capped-rel'`), capped absolute rings (`type='capped-abs'`), fixed modulus rings (`type='fixed-mod'`), and floating point rings (`type='floating-point'`).

  In the capped relative case, the relative precision of an element is restricted to be at most a certain value, specified at the creation of the field. Individual elements also store their own precision, so the effect of various arithmetic operations on precision is tracked. When you cast an exact element into a capped relative field, it truncates it to the precision cap of the field.

  Sage

      sage: R.<a> = Zq(9, 5, 'capped-rel', print_mode='series'); b = (1+2*a)^4; b
      2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^5)
      sage: c = R(3249); c
      3^2 + 3^4 + 3^5 + 3^6 + O(3^7)
      sage: b + c
      2 + (2*a + 2)*3 + (2*a + 2)*3^2 + 3^4 + O(3^5)

  Python

      >>> from sage.all import *
      >>> R = Zq(Integer(9), Integer(5), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); b = (Integer(1)+Integer(2)*a)**Integer(4); b
      2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^5)
      >>> c = R(Integer(3249)); c
      3^2 + 3^4 + 3^5 + 3^6 + O(3^7)
      >>> b + c
      2 + (2*a + 2)*3 + (2*a + 2)*3^2 + 3^4 + O(3^5)

  One can invert non-units: the result is in the fraction field.

  Sage

      sage: d = ~(3*b+c); d
      2*3^-1 + (a + 1) + (a + 1)*3 + a*3^3 + O(3^4)
      sage: d.parent()
      3-adic Unramified Extension Field in a defined by x^2 + 2*x + 2

  [Python]

      >>> from sage.all import *
      >>> d = ~(Integer(3)*b+c); d
      2*3^-1 + (a + 1) + (a + 1)*3 + a*3^3 + O(3^4)
      >>> d.parent()
      3-adic Unramified Extension Field in a defined by x^2 + 2*x + 2

  The capped absolute case is the same as the capped relative case, except that the cap is on the absolute precision rather than the relative precision.

  Sage

      sage: R.<a> = Zq(9, 5, 'capped-abs', print_mode='series'); b = 3*(1+2*a)^4; b
      2*3 + (2*a + 2)*3^2 + (2*a + 1)*3^3 + O(3^5)
      sage: c = R(3249); c
      3^2 + 3^4 + O(3^5)
      sage: b*c
      2*3^3 + (2*a + 2)*3^4 + O(3^5)
      sage: b*c >> 1
      2*3^2 + (2*a + 2)*3^3 + O(3^4)

  Python

      >>> from sage.all import *
      >>> R = Zq(Integer(9), Integer(5), 'capped-abs', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); b = Integer(3)*(Integer(1)+Integer(2)*a)**Integer(4); b
      2*3 + (2*a + 2)*3^2 + (2*a + 1)*3^3 + O(3^5)
      >>> c = R(Integer(3249)); c
      3^2 + 3^4 + O(3^5)
      >>> b*c
      2*3^3 + (2*a + 2)*3^4 + O(3^5)
      >>> b*c >> Integer(1)
      2*3^2 + (2*a + 2)*3^3 + O(3^4)

  The fixed modulus case is like the capped absolute, except that individual elements don’t track their precision.

  Sage

      sage: R.<a> = Zq(9, 5, 'fixed-mod', print_mode='series'); b = 3*(1+2*a)^4; b
      2*3 + (2*a + 2)*3^2 + (2*a + 1)*3^3
      sage: c = R(3249); c
      3^2 + 3^4
      sage: b*c
      2*3^3 + (2*a + 2)*3^4
      sage: b*c >> 1
      2*3^2 + (2*a + 2)*3^3

  [Python]

      >>> from sage.all import *
      >>> R = Zq(Integer(9), Integer(5), 'fixed-mod', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); b = Integer(3)*(Integer(1)+Integer(2)*a)**Integer(4); b
      2*3 + (2*a + 2)*3^2 + (2*a + 1)*3^3
      >>> c = R(Integer(3249)); c
      3^2 + 3^4
      >>> b*c
      2*3^3 + (2*a + 2)*3^4
      >>> b*c >> Integer(1)
      2*3^2 + (2*a + 2)*3^3

  The floating point case is similar to the fixed modulus type in that elements do not track their own precision. However, relative precision is truncated with each operation rather than absolute precision.

  MODULUS:

  The modulus needs to define an unramified extension of \(\ZZ_p\): when it is reduced to a polynomial over \(\GF{p}\) it should be irreducible.

  The modulus can be given in a number of forms.

  1. A polynomial.

  > The base ring can be \(\ZZ\), \(\QQ\), \(\ZZ_p\), \(\GF{p}\), or anything that can be converted to \(\ZZ_p\).

  > Sage

  >     sage: P.<x> = ZZ[]
  >     sage: R.<a> = Zq(27, modulus = x^3 + 2*x + 1); R.modulus()
  >     (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
  >     sage: P.<x> = QQ[]
  >     sage: S.<a> = Zq(27, modulus = x^3 + 2/7*x + 1)
  >     sage: P.<x> = Zp(3)[]
  >     sage: T.<a> = Zq(27, modulus = x^3 + 2*x + 1)
  >     sage: P.<x> = Qp(3)[]
  >     sage: U.<a> = Zq(27, modulus = x^3 + 2*x + 1)
  >     sage: P.<x> = GF(3)[]
  >     sage: V.<a> = Zq(27, modulus = x^3 + 2*x + 1)

  > Python

  >     >>> from sage.all import *
  >     >>> P = ZZ['x']; (x,) = P._first_ngens(1)
  >     >>> R = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = R._first_ngens(1); R.modulus()
  >     (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
  >     >>> P = QQ['x']; (x,) = P._first_ngens(1)
  >     >>> S = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)/Integer(7)*x + Integer(1), names=('a',)); (a,) = S._first_ngens(1)
  >     >>> P = Zp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> T = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = T._first_ngens(1)
  >     >>> P = Qp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> U = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = U._first_ngens(1)
  >     >>> P = GF(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> V = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = V._first_ngens(1)

  > Which form the modulus is given in has no effect on the unramified extension produced:

  > Sage

  >     sage: R == S, R == T, T == U, U == V
  >     (False, True, True, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, T == U, U == V
  >     (False, True, True, False)

  > unless the modulus is different, or the precision of the modulus differs. In the case of `V`, the modulus is only given to precision `1`, so the resulting field has a precision cap of `1`.

  > Sage

  >     sage: V.precision_cap()
  >     1
  >     sage: U.precision_cap()
  >     20
  >     sage: P.<x> = Zp(3)[]
  >     sage: modulus = x^3 + (2 + O(3^7))*x + (1 + O(3^10))
  >     sage: modulus
  >     (1 + O(3^20))*x^3 + (2 + O(3^7))*x + 1 + O(3^10)
  >     sage: W.<a> = Zq(27, modulus = modulus); W.precision_cap()
  >     7

  > Python

  >     >>> from sage.all import *
  >     >>> V.precision_cap()
  >     1
  >     >>> U.precision_cap()
  >     20
  >     >>> P = Zp(Integer(3))['x']; (x,) = P._first_ngens(1)
  >     >>> modulus = x**Integer(3) + (Integer(2) + O(Integer(3)**Integer(7)))*x + (Integer(1) + O(Integer(3)**Integer(10)))
  >     >>> modulus
  >     (1 + O(3^20))*x^3 + (2 + O(3^7))*x + 1 + O(3^10)
  >     >>> W = Zq(Integer(27), modulus = modulus, names=('a',)); (a,) = W._first_ngens(1); W.precision_cap()
  >     7

  2. The modulus can also be given as a symbolic expression.

     Sage

         sage: x = var('x')
         sage: X.<a> = Zq(27, modulus = x^3 + 2*x + 1); X.modulus()
         (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
         sage: X == R
         True

     Python

         >>> from sage.all import *
         >>> x = var('x')
         >>> X = Zq(Integer(27), modulus = x**Integer(3) + Integer(2)*x + Integer(1), names=('a',)); (a,) = X._first_ngens(1); X.modulus()
         (1 + O(3^20))*x^3 + O(3^20)*x^2 + (2 + O(3^20))*x + 1 + O(3^20)
         >>> X == R
         True

  > By default, the polynomial chosen is the standard lift of the generator chosen for \(\GF{q}\).

  > Sage

  >     sage: GF(125, 'a').modulus()
  >     x^3 + 3*x + 3
  >     sage: Y.<a> = Zq(125); Y.modulus()
  >     (1 + O(5^20))*x^3 + O(5^20)*x^2 + (3 + O(5^20))*x + 3 + O(5^20)

  > Python

  >     >>> from sage.all import *
  >     >>> GF(Integer(125), 'a').modulus()
  >     x^3 + 3*x + 3
  >     >>> Y = Zq(Integer(125), names=('a',)); (a,) = Y._first_ngens(1); Y.modulus()
  >     (1 + O(5^20))*x^3 + O(5^20)*x^2 + (3 + O(5^20))*x + 3 + O(5^20)

  > However, you can choose another polynomial if desired (as long as the reduction to \(\GF{p}[x]\) is irreducible).

  > Sage

  >     sage: P.<x> = ZZ[]
  >     sage: Z.<a> = Zq(125, modulus = x^3 + 3*x^2 + x + 1); Z.modulus()
  >     (1 + O(5^20))*x^3 + (3 + O(5^20))*x^2 + (1 + O(5^20))*x + 1 + O(5^20)
  >     sage: Y == Z
  >     False

  > [Python]

  >     >>> from sage.all import *
  >     >>> P = ZZ['x']; (x,) = P._first_ngens(1)
  >     >>> Z = Zq(Integer(125), modulus = x**Integer(3) + Integer(3)*x**Integer(2) + x + Integer(1), names=('a',)); (a,) = Z._first_ngens(1); Z.modulus()
  >     (1 + O(5^20))*x^3 + (3 + O(5^20))*x^2 + (1 + O(5^20))*x + 1 + O(5^20)
  >     >>> Y == Z
  >     False

  PRINTING:

  There are many different ways to print \(p\)-adic elements. The way elements of a given field print is controlled by options passed in at the creation of the field. There are four basic printing modes (`'series'`, `'val-unit'`, `'terse'` and `'bars'`; `'digits'` is not available), as well as various options that either hide some information in the print representation or sometimes make print representations more compact. Note that the printing options affect whether different \(p\)-adic fields are considered equal.

  1. series: elements are displayed as series in \(p\).

     Sage

         sage: R.<a> = Zq(9, 20, 'capped-rel', print_mode='series'); (1+2*a)^4
         2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^20)
         sage: -3*(1+2*a)^4
         3 + a*3^2 + 3^3 + (2*a + 2)*3^4 + (2*a + 2)*3^5 + (2*a + 2)*3^6 + (2*a + 2)*3^7
           + (2*a + 2)*3^8 + (2*a + 2)*3^9 + (2*a + 2)*3^10 + (2*a + 2)*3^11 + (2*a + 2)*3^12
           + (2*a + 2)*3^13 + (2*a + 2)*3^14 + (2*a + 2)*3^15 + (2*a + 2)*3^16
           + (2*a + 2)*3^17 + (2*a + 2)*3^18 + (2*a + 2)*3^19 + (2*a + 2)*3^20 + O(3^21)
         sage: b = ~(3*a+18); b
         (a + 2)*3^-1 + 1 + 2*3 + (a + 1)*3^2 + 3^3 + 2*3^4 + (a + 1)*3^5 + 3^6 + 2*3^7
           + (a + 1)*3^8 + 3^9 + 2*3^10 + (a + 1)*3^11 + 3^12 + 2*3^13 + (a + 1)*3^14
           + 3^15 + 2*3^16 + (a + 1)*3^17 + 3^18 + O(3^19)
         sage: b.parent() is R.fraction_field()
         True

     Python

         >>> from sage.all import *
         >>> R = Zq(Integer(9), Integer(20), 'capped-rel', print_mode='series', names=('a',)); (a,) = R._first_ngens(1); (Integer(1)+Integer(2)*a)**Integer(4)
         2 + (2*a + 2)*3 + (2*a + 1)*3^2 + O(3^20)
         >>> -Integer(3)*(Integer(1)+Integer(2)*a)**Integer(4)
         3 + a*3^2 + 3^3 + (2*a + 2)*3^4 + (2*a + 2)*3^5 + (2*a + 2)*3^6 + (2*a + 2)*3^7
           + (2*a + 2)*3^8 + (2*a + 2)*3^9 + (2*a + 2)*3^10 + (2*a + 2)*3^11 + (2*a + 2)*3^12
           + (2*a + 2)*3^13 + (2*a + 2)*3^14 + (2*a + 2)*3^15 + (2*a + 2)*3^16
           + (2*a + 2)*3^17 + (2*a + 2)*3^18 + (2*a + 2)*3^19 + (2*a + 2)*3^20 + O(3^21)
         >>> b = ~(Integer(3)*a+Integer(18)); b
         (a + 2)*3^-1 + 1 + 2*3 + (a + 1)*3^2 + 3^3 + 2*3^4 + (a + 1)*3^5 + 3^6 + 2*3^7
           + (a + 1)*3^8 + 3^9 + 2*3^10 + (a + 1)*3^11 + 3^12 + 2*3^13 + (a + 1)*3^14
           + 3^15 + 2*3^16 + (a + 1)*3^17 + 3^18 + O(3^19)
         >>> b.parent() is R.fraction_field()
         True

  > print_pos controls whether negatives can be used in the coefficients of powers of \(p\).

  > Sage

  >     sage: S.<b> = Zq(9, print_mode='series', print_pos=False); (1+2*b)^4
  >     -1 - b*3 - 3^2 + (b + 1)*3^3 + O(3^20)
  >     sage: -3*(1+2*b)^4
  >     3 + b*3^2 + 3^3 + (-b - 1)*3^4 + O(3^21)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Zq(Integer(9), print_mode='series', print_pos=False, names=('b',)); (b,) = S._first_ngens(1); (Integer(1)+Integer(2)*b)**Integer(4)
  >     -1 - b*3 - 3^2 + (b + 1)*3^3 + O(3^20)
  >     >>> -Integer(3)*(Integer(1)+Integer(2)*b)**Integer(4)
  >     3 + b*3^2 + 3^3 + (-b - 1)*3^4 + O(3^21)

  > ram_name controls how the prime is printed.

  > Sage

  >     sage: T.<d> = Zq(9, print_mode='series', ram_name='p'); 3*(1+2*d)^4
  >     2*p + (2*d + 2)*p^2 + (2*d + 1)*p^3 + O(p^21)

  > [Python]

  >     >>> from sage.all import *
  >     >>> T = Zq(Integer(9), print_mode='series', ram_name='p', names=('d',)); (d,) = T._first_ngens(1); Integer(3)*(Integer(1)+Integer(2)*d)**Integer(4)
  >     2*p + (2*d + 2)*p^2 + (2*d + 1)*p^3 + O(p^21)

  > print_max_ram_terms limits the number of powers of \(p\) that appear.

  > Sage

  >     sage: U.<e> = Zq(9, print_mode='series', print_max_ram_terms=4); repr(-3*(1+2*e)^4)
  >     '3 + e*3^2 + 3^3 + (2*e + 2)*3^4 + ... + O(3^21)'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(9), print_mode='series', print_max_ram_terms=Integer(4), names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + e*3^2 + 3^3 + (2*e + 2)*3^4 + ... + O(3^21)'

  > print_max_unram_terms limits the number of terms that appear in a coefficient of a power of \(p\).

  > Sage

  >     sage: V.<f> = Zq(128, prec = 8, print_mode='series'); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + f^4 + f^3 + f^2)*2 + (f^6 + f^5 + f^4 + f + 1)*2^2 + (f^5 + f^4 + f^2 + f + 1)*2^3 + (f^6 + f^5 + f^4 + f^3 + f^2 + f + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + f^4 + f^3 + f + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Zq(128, prec = 8, print_mode='series', print_max_unram_terms = 3); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + f^4 + ... + f^2)*2 + (f^6 + f^5 + ... + 1)*2^2 + (f^5 + f^4 + ... + 1)*2^3 + (f^6 + f^5 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Zq(128, prec = 8, print_mode='series', print_max_unram_terms = 2); repr((1+f)^9)
  >     '(f^3 + 1) + (f^5 + ... + f^2)*2 + (f^6 + ... + 1)*2^2 + (f^5 + ... + 1)*2^3 + (f^6 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     sage: V.<f> = Zq(128, prec = 8, print_mode='series', print_max_unram_terms = 1); repr((1+f)^9)
  >     '(f^3 + ...) + (f^5 + ...)*2 + (f^6 + ...)*2^2 + (f^5 + ...)*2^3 + (f^6 + ...)*2^4 + (f^5 + ...)*2^5 + (f^6 + ...)*2^6 + (f + ...)*2^7 + O(2^8)'
  >     sage: V.<f> = Zq(128, prec = 8, print_mode='series', print_max_unram_terms = 0); repr((1+f)^9 - 1 - f^3)
  >     '(...)*2 + (...)*2^2 + (...)*2^3 + (...)*2^4 + (...)*2^5 + (...)*2^6 + (...)*2^7 + O(2^8)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> V = Zq(Integer(128), prec = Integer(8), print_mode='series', names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + f^4 + f^3 + f^2)*2 + (f^6 + f^5 + f^4 + f + 1)*2^2 + (f^5 + f^4 + f^2 + f + 1)*2^3 + (f^6 + f^5 + f^4 + f^3 + f^2 + f + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + f^4 + f^3 + f + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Zq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(3), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + f^4 + ... + f^2)*2 + (f^6 + f^5 + ... + 1)*2^2 + (f^5 + f^4 + ... + 1)*2^3 + (f^6 + f^5 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + f^5 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Zq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(2), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + 1) + (f^5 + ... + f^2)*2 + (f^6 + ... + 1)*2^2 + (f^5 + ... + 1)*2^3 + (f^6 + ... + 1)*2^4 + (f^5 + f^4)*2^5 + (f^6 + ... + 1)*2^6 + (f + 1)*2^7 + O(2^8)'
  >     >>> V = Zq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(1), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9))
  >     '(f^3 + ...) + (f^5 + ...)*2 + (f^6 + ...)*2^2 + (f^5 + ...)*2^3 + (f^6 + ...)*2^4 + (f^5 + ...)*2^5 + (f^6 + ...)*2^6 + (f + ...)*2^7 + O(2^8)'
  >     >>> V = Zq(Integer(128), prec = Integer(8), print_mode='series', print_max_unram_terms = Integer(0), names=('f',)); (f,) = V._first_ngens(1); repr((Integer(1)+f)**Integer(9) - Integer(1) - f**Integer(3))
  >     '(...)*2 + (...)*2^2 + (...)*2^3 + (...)*2^4 + (...)*2^5 + (...)*2^6 + (...)*2^7 + O(2^8)'

  > show_prec determines how the precision is printed. It can be either `'none'` (or equivalently `False`), `'bigoh'` (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: U.<e> = Zq(9, 2, show_prec=False); repr(-3*(1+2*e)^4)
  >     '3 + e*3^2'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(9), Integer(2), show_prec=False, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + e*3^2'

  > print_sep and print_max_terse_terms have no effect.

  > Note that print options affect equality:

  > Sage

  >     sage: R == S, R == T, R == U, R == V, S == T, S == U, S == V, T == U, T == V, U == V
  >     (False, False, False, False, False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, R == V, S == T, S == U, S == V, T == U, T == V, U == V
  >     (False, False, False, False, False, False, False, False, False, False)

  2. val-unit: elements are displayed as \(p^k u\):

     Sage

         sage: R.<a> = Zq(9, 7, print_mode='val-unit'); b = (1+3*a)^9 - 1; b
         3^3 * (15 + 64*a) + O(3^7)
         sage: ~b
         3^-3 * (41 + a) + O(3)

     Python

         >>> from sage.all import *
         >>> R = Zq(Integer(9), Integer(7), print_mode='val-unit', names=('a',)); (a,) = R._first_ngens(1); b = (Integer(1)+Integer(3)*a)**Integer(9) - Integer(1); b
         3^3 * (15 + 64*a) + O(3^7)
         >>> ~b
         3^-3 * (41 + a) + O(3)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: S.<a> = Zq(9, 7, print_mode='val-unit', print_pos=False); b = (1+3*a)^9 - 1; b
  >     3^3 * (15 - 17*a) + O(3^7)
  >     sage: ~b
  >     3^-3 * (-40 + a) + O(3)

  > Python

  >     >>> from sage.all import *
  >     >>> S = Zq(Integer(9), Integer(7), print_mode='val-unit', print_pos=False, names=('a',)); (a,) = S._first_ngens(1); b = (Integer(1)+Integer(3)*a)**Integer(9) - Integer(1); b
  >     3^3 * (15 - 17*a) + O(3^7)
  >     >>> ~b
  >     3^-3 * (-40 + a) + O(3)

  > ram_name affects how the prime is printed.

  > Sage

  >     sage: A.<x> = Zp(next_prime(10^6), print_mode='val-unit')[]
  >     sage: T.<a> = Zq(next_prime(10^6)^3, 4, print_mode='val-unit', ram_name='p',
  >     ....:            modulus=x^3+385831*x^2+106556*x+321036)
  >     sage: b = next_prime(10^6)^2*(a^2 + a - 4)^4; b
  >     p^2 * (87996187118837557387483 + 246348888344392418464080*a + 1353538653775332610349*a^2)
  >      + O(p^6)
  >     sage: b * (a^2 + a - 4)^-4
  >     p^2 * 1 + O(p^6)

  > [Python]

  >     >>> from sage.all import *
  >     >>> A = Zp(next_prime(Integer(10)**Integer(6)), print_mode='val-unit')['x']; (x,) = A._first_ngens(1)
  >     >>> T = Zq(next_prime(Integer(10)**Integer(6))**Integer(3), Integer(4), print_mode='val-unit', ram_name='p',
  >     ...            modulus=x**Integer(3)+Integer(385831)*x**Integer(2)+Integer(106556)*x+Integer(321036), names=('a',)); (a,) = T._first_ngens(1)
  >     >>> b = next_prime(Integer(10)**Integer(6))**Integer(2)*(a**Integer(2) + a - Integer(4))**Integer(4); b
  >     p^2 * (87996187118837557387483 + 246348888344392418464080*a + 1353538653775332610349*a^2)
  >      + O(p^6)
  >     >>> b * (a**Integer(2) + a - Integer(4))**-Integer(4)
  >     p^2 * 1 + O(p^6)

  > print_max_terse_terms controls how many terms of the polynomial appear in the unit part.

  > Sage

  >     sage: U.<a> = Zq(17^4, 6, print_mode='val-unit', print_max_terse_terms=3)
  >     sage: b = 17*(a^3-a+14)^6; b
  >     17 * (12131797 + 12076378*a + 10809706*a^2 + ...) + O(17^7)

  > Python

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(17)**Integer(4), Integer(6), print_mode='val-unit', print_max_terse_terms=Integer(3), names=('a',)); (a,) = U._first_ngens(1)
  >     >>> b = Integer(17)*(a**Integer(3)-a+Integer(14))**Integer(6); b
  >     17 * (12131797 + 12076378*a + 10809706*a^2 + ...) + O(17^7)

  > show_prec determines how the precision is printed. It can be either `'none'` (or equivalently `False`), `'bigoh'` (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: U.<e> = Zq(9, 2, print_mode='val-unit', show_prec=False); repr(-3*(1+2*e)^4)
  >     '3 * (1 + 3*e)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(9), Integer(2), print_mode='val-unit', show_prec=False, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 * (1 + 3*e)'

  > print_sep, print_max_ram_terms and print_max_unram_terms have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  3. terse: elements are displayed as a polynomial of degree less than the degree of the extension.

     Sage

         sage: R.<a> = Zq(125, print_mode='terse')
         sage: (a+5)^177
         68210977979428 + 90313850704069*a + 73948093055069*a^2 + O(5^20)
         sage: (a/5+1)^177
         68210977979428/5^177 + 90313850704069/5^177*a + 73948093055069/5^177*a^2 + O(5^-157)

     Python

         >>> from sage.all import *
         >>> R = Zq(Integer(125), print_mode='terse', names=('a',)); (a,) = R._first_ngens(1)
         >>> (a+Integer(5))**Integer(177)
         68210977979428 + 90313850704069*a + 73948093055069*a^2 + O(5^20)
         >>> (a/Integer(5)+Integer(1))**Integer(177)
         68210977979428/5^177 + 90313850704069/5^177*a + 73948093055069/5^177*a^2 + O(5^-157)

  > Note that in this last computation, you get one fewer \(p\)-adic digit than one might expect. This is because `R` is capped absolute, and thus 5 is cast in with relative precision 19.

  > As of version 3.3, if coefficients of the polynomial are non-integral, they are always printed with an explicit power of \(p\) in the denominator.

  > Sage

  >     sage: 5*a + a^2/25
  >     5*a + 1/5^2*a^2 + O(5^18)

  > Python

  >     >>> from sage.all import *
  >     >>> Integer(5)*a + a**Integer(2)/Integer(25)
  >     5*a + 1/5^2*a^2 + O(5^18)

  > print_pos controls whether to use a balanced representation or not.

  > Sage

  >     sage: (a-5)^6
  >     22864 + 95367431627998*a + 8349*a^2 + O(5^20)
  >     sage: S.<a> = Zq(125, print_mode='terse', print_pos=False); b = (a-5)^6; b
  >     22864 - 12627*a + 8349*a^2 + O(5^20)
  >     sage: (a - 1/5)^6
  >     -20624/5^6 + 18369/5^5*a + 1353/5^3*a^2 + O(5^14)

  > [Python]

  >     >>> from sage.all import *
  >     >>> (a-Integer(5))**Integer(6)
  >     22864 + 95367431627998*a + 8349*a^2 + O(5^20)
  >     >>> S = Zq(Integer(125), print_mode='terse', print_pos=False, names=('a',)); (a,) = S._first_ngens(1); b = (a-Integer(5))**Integer(6); b
  >     22864 - 12627*a + 8349*a^2 + O(5^20)
  >     >>> (a - Integer(1)/Integer(5))**Integer(6)
  >     -20624/5^6 + 18369/5^5*a + 1353/5^3*a^2 + O(5^14)

  > ram_name affects how the prime is printed.

  > Sage

  >     sage: T.<a> = Zq(125, print_mode='terse', ram_name='p'); (a - 1/5)^6
  >     95367431620001/p^6 + 18369/p^5*a + 1353/p^3*a^2 + O(p^14)

  > Python

  >     >>> from sage.all import *
  >     >>> T = Zq(Integer(125), print_mode='terse', ram_name='p', names=('a',)); (a,) = T._first_ngens(1); (a - Integer(1)/Integer(5))**Integer(6)
  >     95367431620001/p^6 + 18369/p^5*a + 1353/p^3*a^2 + O(p^14)

  > print_max_terse_terms controls how many terms of the polynomial are shown.

  > Sage

  >     sage: U.<a> = Zq(625, print_mode='terse', print_max_terse_terms=2); (a-1/5)^6
  >     106251/5^6 + 49994/5^5*a + ... + O(5^14)

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(625), print_mode='terse', print_max_terse_terms=Integer(2), names=('a',)); (a,) = U._first_ngens(1); (a-Integer(1)/Integer(5))**Integer(6)
  >     106251/5^6 + 49994/5^5*a + ... + O(5^14)

  > show_prec determines how the precision is printed. It can be either `'none'` (or equivalently `False`), `'bigoh'` (or equivalently `True`). The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: U.<e> = Zq(9, 2, print_mode='terse', show_prec=False); repr(-3*(1+2*e)^4)
  >     '3 + 9*e'

  > Python

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(9), Integer(2), print_mode='terse', show_prec=False, names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '3 + 9*e'

  > print_sep, print_max_ram_terms and print_max_unram_terms have no effect.

  > Equality again depends on the printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > [Python]

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  4. digits: This print mode is not available when the residue field is not prime. It might make sense to have a dictionary for small fields, but this isn’t implemented.

  5. bars: elements are displayed in a similar fashion to series, but more compactly.

     Sage

         sage: R.<a> = Zq(125); (a+5)^6
         (4*a^2 + 3*a + 4) + (3*a^2 + 2*a)*5 + (a^2 + a + 1)*5^2 + (3*a + 2)*5^3
          + (3*a^2 + a + 3)*5^4 + (2*a^2 + 3*a + 2)*5^5 + O(5^20)
         sage: R.<a> = Zq(125, print_mode='bars', prec=8); repr((a+5)^6)
         '...[2, 3, 2]|[3, 1, 3]|[2, 3]|[1, 1, 1]|[0, 2, 3]|[4, 3, 4]'
         sage: repr((a-5)^6)
         '...[0, 4]|[1, 4]|[2, 0, 2]|[1, 4, 3]|[2, 3, 1]|[4, 4, 3]|[2, 4, 4]|[4, 3, 4]'

     Python

         >>> from sage.all import *
         >>> R = Zq(Integer(125), names=('a',)); (a,) = R._first_ngens(1); (a+Integer(5))**Integer(6)
         (4*a^2 + 3*a + 4) + (3*a^2 + 2*a)*5 + (a^2 + a + 1)*5^2 + (3*a + 2)*5^3
          + (3*a^2 + a + 3)*5^4 + (2*a^2 + 3*a + 2)*5^5 + O(5^20)
         >>> R = Zq(Integer(125), print_mode='bars', prec=Integer(8), names=('a',)); (a,) = R._first_ngens(1); repr((a+Integer(5))**Integer(6))
         '...[2, 3, 2]|[3, 1, 3]|[2, 3]|[1, 1, 1]|[0, 2, 3]|[4, 3, 4]'
         >>> repr((a-Integer(5))**Integer(6))
         '...[0, 4]|[1, 4]|[2, 0, 2]|[1, 4, 3]|[2, 3, 1]|[4, 4, 3]|[2, 4, 4]|[4, 3, 4]'

  > Note that it’s not possible to read off the precision from the representation in this mode.

  > Sage

  >     sage: b = a + 3; repr(b)
  >     '...[3, 1]'
  >     sage: c = a + R(3, 4); repr(c)
  >     '...[3, 1]'
  >     sage: b.precision_absolute()
  >     8
  >     sage: c.precision_absolute()
  >     4

  > Python

  >     >>> from sage.all import *
  >     >>> b = a + Integer(3); repr(b)
  >     '...[3, 1]'
  >     >>> c = a + R(Integer(3), Integer(4)); repr(c)
  >     '...[3, 1]'
  >     >>> b.precision_absolute()
  >     8
  >     >>> c.precision_absolute()
  >     4

  > print_pos controls whether the digits can be negative.

  > Sage

  >     sage: S.<a> = Zq(125, print_mode='bars', print_pos=False); repr((a-5)^6)
  >     '...[1, -1, 1]|[2, 1, -2]|[2, 0, -2]|[-2, -1, 2]|[0, 0, -1]|[-2]|[-1, -2, -1]'
  >     sage: repr((a-1/5)^6)
  >     '...[0, 1, 2]|[-1, 1, 1]|.|[-2, -1, -1]|[2, 2, 1]|[0, 0, -2]|[0, -1]|[0, -1]|[1]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> S = Zq(Integer(125), print_mode='bars', print_pos=False, names=('a',)); (a,) = S._first_ngens(1); repr((a-Integer(5))**Integer(6))
  >     '...[1, -1, 1]|[2, 1, -2]|[2, 0, -2]|[-2, -1, 2]|[0, 0, -1]|[-2]|[-1, -2, -1]'
  >     >>> repr((a-Integer(1)/Integer(5))**Integer(6))
  >     '...[0, 1, 2]|[-1, 1, 1]|.|[-2, -1, -1]|[2, 2, 1]|[0, 0, -2]|[0, -1]|[0, -1]|[1]'

  > print_max_ram_terms controls the maximum number of “digits” shown. Note that this puts a cap on the relative precision, not the absolute precision.

  > Sage

  >     sage: T.<a> = Zq(125, print_max_ram_terms=3, print_pos=False); (a-5)^6
  >     (-a^2 - 2*a - 1) - 2*5 - a^2*5^2 + ... + O(5^20)
  >     sage: 5*(a-5)^6 + 50
  >     (-a^2 - 2*a - 1)*5 - a^2*5^3 + (2*a^2 - a - 2)*5^4 + ... + O(5^21)
  >     sage: (a-1/5)^6
  >     5^-6 - a*5^-5 - a*5^-4 + ... + O(5^14)

  > Python

  >     >>> from sage.all import *
  >     >>> T = Zq(Integer(125), print_max_ram_terms=Integer(3), print_pos=False, names=('a',)); (a,) = T._first_ngens(1); (a-Integer(5))**Integer(6)
  >     (-a^2 - 2*a - 1) - 2*5 - a^2*5^2 + ... + O(5^20)
  >     >>> Integer(5)*(a-Integer(5))**Integer(6) + Integer(50)
  >     (-a^2 - 2*a - 1)*5 - a^2*5^3 + (2*a^2 - a - 2)*5^4 + ... + O(5^21)
  >     >>> (a-Integer(1)/Integer(5))**Integer(6)
  >     5^-6 - a*5^-5 - a*5^-4 + ... + O(5^14)

  > print_sep controls the separating character (`'|'` by default).

  > Sage

  >     sage: U.<a> = Zq(625, print_mode='bars', print_sep=''); b = (a+5)^6; repr(b)
  >     '...[0, 1][4, 0, 2][3, 2, 2, 3][4, 2, 2, 4][0, 3][1, 1, 3][3, 1, 4, 1]'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(625), print_mode='bars', print_sep='', names=('a',)); (a,) = U._first_ngens(1); b = (a+Integer(5))**Integer(6); repr(b)
  >     '...[0, 1][4, 0, 2][3, 2, 2, 3][4, 2, 2, 4][0, 3][1, 1, 3][3, 1, 4, 1]'

  > print_max_unram_terms controls how many terms are shown in each `'digit'`:

  > Sage

  >     sage: with local_print_mode(U, {'max_unram_terms': 3}): repr(b)
  >     '...[0, 1][4,..., 0, 2][3,..., 2, 3][4,..., 2, 4][0, 3][1,..., 1, 3][3,..., 4, 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms': 2}): repr(b)
  >     '...[0, 1][4,..., 2][3,..., 3][4,..., 4][0, 3][1,..., 3][3,..., 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms': 1}): repr(b)
  >     '...[..., 1][..., 2][..., 3][..., 4][..., 3][..., 3][..., 1]'
  >     sage: with local_print_mode(U, {'max_unram_terms':0}): repr(b-75*a)
  >     '...[...][...][...][...][][...][...]'

  > Python

  >     >>> from sage.all import *
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(3)}): repr(b)
  >     '...[0, 1][4,..., 0, 2][3,..., 2, 3][4,..., 2, 4][0, 3][1,..., 1, 3][3,..., 4, 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(2)}): repr(b)
  >     '...[0, 1][4,..., 2][3,..., 3][4,..., 4][0, 3][1,..., 3][3,..., 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms': Integer(1)}): repr(b)
  >     '...[..., 1][..., 2][..., 3][..., 4][..., 3][..., 3][..., 1]'
  >     >>> with local_print_mode(U, {'max_unram_terms':Integer(0)}): repr(b-Integer(75)*a)
  >     '...[...][...][...][...][][...][...]'

  > show_prec determines how the precision is printed. It can be either `'none'` (or equivalently `False`), `'dots'` (or equivalently `True`) or `'bigoh'`. The default is `False` for the `'floating-point'` and `'fixed-mod'` types and `True` for all other types.

  > Sage

  >     sage: U.<e> = Zq(9, 2, print_mode='bars', show_prec='bigoh'); repr(-3*(1+2*e)^4)
  >     '[0, 1]|[1]|[] + O(3^3)'

  > [Python]

  >     >>> from sage.all import *
  >     >>> U = Zq(Integer(9), Integer(2), print_mode='bars', show_prec='bigoh', names=('e',)); (e,) = U._first_ngens(1); repr(-Integer(3)*(Integer(1)+Integer(2)*e)**Integer(4))
  >     '[0, 1]|[1]|[] + O(3^3)'

  > ram_name and print_max_terse_terms have no effect.

  > Equality depends on printing options:

  > Sage

  >     sage: R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  > Python

  >     >>> from sage.all import *
  >     >>> R == S, R == T, R == U, S == T, S == U, T == U
  >     (False, False, False, False, False, False)

  EXAMPLES:

  Unlike for `Zp()`, you can’t create `Zq(N)` when `N` is not a prime power.

  However, you can use `check=False` to pass in a pair in order to not have to factor. If you do so, you need to use names explicitly rather than the `R.<a>` syntax.

  Sage

      sage: p = next_prime(2^123)
      sage: k = Zp(p)
      sage: R.<x> = k[]
      sage: K = Zq([(p, 5)], modulus=x^5+x+4, names='a', ram_name='p',
      ....:        print_pos=False, check=False)
      sage: K.0^5
      (-a - 4) + O(p^20)

  Python

      >>> from sage.all import *
      >>> p = next_prime(Integer(2)**Integer(123))
      >>> k = Zp(p)
      >>> R = k['x']; (x,) = R._first_ngens(1)
      >>> K = Zq([(p, Integer(5))], modulus=x**Integer(5)+x+Integer(4), names='a', ram_name='p',
      ...        print_pos=False, check=False)
      >>> K.gen(0)**Integer(5)
      (-a - 4) + O(p^20)

  In tests on sage.math, the creation of `K` as above took an average of 1.58ms, while:

  Sage

      sage: K = Zq(p^5, modulus=x^5+x+4, names='a', ram_name='p',
      ....:        print_pos=False, check=True)

  [Python]

      >>> from sage.all import *
      >>> K = Zq(p**Integer(5), modulus=x**Integer(5)+x+Integer(4), names='a', ram_name='p',
      ...        print_pos=False, check=True)

  took an average of 24.5ms. Of course, with smaller primes these savings disappear.

sage.rings.padics.factory.ZqCA(q, prec=None, *args, **kwds)

  A shortcut function to create capped absolute unramified \(p\)-adic rings.

  See documentation for `Zq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = ZqCA(25, 40); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = ZqCA(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.ZqCR(q, prec=None, *args, **kwds)

  A shortcut function to create capped relative unramified \(p\)-adic rings.

  Same functionality as `Zq()`. See documentation for `Zq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = ZqCR(25, 40); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = ZqCR(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.ZqFM(q, prec=None, *args, **kwds)

  A shortcut function to create fixed modulus unramified \(p\)-adic rings.

  See documentation for `Zq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = ZqFM(25, 40); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = ZqFM(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.ZqFP(q, prec=None, *args, **kwds)

  A shortcut function to create floating point unramified \(p\)-adic rings.

  Same functionality as `Zq()`. See documentation for `Zq()` for a description of the input parameters.

  EXAMPLES:

  Sage

      sage: R.<a> = ZqFP(25, 40); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

  Python

      >>> from sage.all import *
      >>> R = ZqFP(Integer(25), Integer(40), names=('a',)); (a,) = R._first_ngens(1); R
      5-adic Unramified Extension Ring in a defined by x^2 + 4*x + 2

sage.rings.padics.factory.get_key_base(p, prec, type, print_mode, names, ram_name, print_pos, print_sep, print_alphabet, print_max_terms, show_prec, check, valid_types, label=None)

  This implements `create_key` for `Zp` and `Qp`: moving it here prevents code duplication.

  It fills in unspecified values and checks for contradictions in the input. It also standardizes irrelevant options so that duplicate parents are not created.

  EXAMPLES:

  Sage

      sage: from sage.rings.padics.factory import get_key_base
      sage: get_key_base(11, 5, 'capped-rel', None, None, None, None, ':', None, None, False, True, ['capped-rel'])
      (11, 5, 'capped-rel', 'series', '11', True, '|', (), -1, 'none', None)
      sage: get_key_base(12, 5, 'capped-rel', 'digits', None, None, None, None, None, None, True, False, ['capped-rel'])
      (12,
       5,
       'capped-rel',
       'digits',
       '12',
       True,
       '|',
       ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'),
       -1,
       'dots',
       None)

  Python

      >>> from sage.all import *
      >>> from sage.rings.padics.factory import get_key_base
      >>> get_key_base(Integer(11), Integer(5), 'capped-rel', None, None, None, None, ':', None, None, False, True, ['capped-rel'])
      (11, 5, 'capped-rel', 'series', '11', True, '|', (), -1, 'none', None)
      >>> get_key_base(Integer(12), Integer(5), 'capped-rel', 'digits', None, None, None, None, None, None, True, False, ['capped-rel'])
      (12,
       5,
       'capped-rel',
       'digits',
       '12',
       True,
       '|',
       ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'),
       -1,
       'dots',
       None)

sage.rings.padics.factory.is_eisenstein(poly)

  Return `True` iff this monic polynomial is Eisenstein.

  A polynomial is Eisenstein if it is monic, the constant term has valuation 1 and all other terms have positive valuation.

  EXAMPLES:

  Sage

      sage: R = Zp(5)
      sage: S.<x> = R[]
      sage: from sage.rings.padics.factory import is_eisenstein
      sage: f = x^4 - 75*x + 15
      sage: is_eisenstein(f)
      True
      sage: g = x^4 + 75
      sage: is_eisenstein(g)
      False
      sage: h = x^7 + 27*x -15
      sage: is_eisenstein(h)
      False

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5))
      >>> S = R['x']; (x,) = S._first_ngens(1)
      >>> from sage.rings.padics.factory import is_eisenstein
      >>> f = x**Integer(4) - Integer(75)*x + Integer(15)
      >>> is_eisenstein(f)
      True
      >>> g = x**Integer(4) + Integer(75)
      >>> is_eisenstein(g)
      False
      >>> h = x**Integer(7) + Integer(27)*x -Integer(15)
      >>> is_eisenstein(h)
      False

sage.rings.padics.factory.is_unramified(poly)

  Return `True` iff this monic polynomial is unramified.

  A polynomial is unramified if its reduction modulo the maximal ideal is irreducible.

  EXAMPLES:

  Sage

      sage: R = Zp(5)
      sage: S.<x> = R[]
      sage: from sage.rings.padics.factory import is_unramified
      sage: f = x^4 + 14*x + 9
      sage: is_unramified(f)
      True
      sage: g = x^6 + 17*x + 6
      sage: is_unramified(g)
      False

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5))
      >>> S = R['x']; (x,) = S._first_ngens(1)
      >>> from sage.rings.padics.factory import is_unramified
      >>> f = x**Integer(4) + Integer(14)*x + Integer(9)
      >>> is_unramified(f)
      True
      >>> g = x**Integer(6) + Integer(17)*x + Integer(6)
      >>> is_unramified(g)
      False

sage.rings.padics.factory.krasner_check(poly, prec)

  Return `True` iff `poly` determines a unique isomorphism class of extensions at precision `prec`.

  Currently just returns `True` (thus allowing extensions that are not defined to high enough precision in order to specify them up to isomorphism). This will change in the future.

  EXAMPLES:

  Sage

      sage: from sage.rings.padics.factory import krasner_check
      sage: krasner_check(1,2)  # this is a stupid example.
      True

  Python

      >>> from sage.all import *
      >>> from sage.rings.padics.factory import krasner_check
      >>> krasner_check(Integer(1),Integer(2))  # this is a stupid example.
      True

class sage.rings.padics.factory.pAdicExtension_class

  Bases: `UniqueFactory`

  A class for creating extensions of \(p\)-adic rings and fields.

  EXAMPLES:

  Sage

      sage: R = Zp(5,3)
      sage: S.<x> = ZZ[]
      sage: W.<w> = pAdicExtension(R, x^4 - 15); W
      5-adic Eisenstein Extension Ring in w defined by x^4 - 15
      sage: W.precision_cap()
      12

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5),Integer(3))
      >>> S = ZZ['x']; (x,) = S._first_ngens(1)
      >>> W = pAdicExtension(R, x**Integer(4) - Integer(15), names=('w',)); (w,) = W._first_ngens(1); W
      5-adic Eisenstein Extension Ring in w defined by x^4 - 15
      >>> W.precision_cap()
      12

  create_key_and_extra_args(base, modulus, prec=None, print_mode=None, names=None, var_name=None, res_name=None, unram_name=None, ram_name=None, print_pos=None, print_sep=None, print_alphabet=None, print_max_ram_terms=None, print_max_unram_terms=None, print_max_terse_terms=None, show_prec=None, check=True, unram=False, implementation='FLINT')

    Create a key from input parameters for `pAdicExtension`.

    See the documentation for `Qq()` for more information.

  create_object(version, key, approx_modulus=None, shift_seed=None)

    Create an object using a given key.

    See the documentation for `pAdicExtension` for more information.

sage.rings.padics.factory.split(poly, prec)

  Given a polynomial `poly` and a desired precision `prec`, computes `upoly` and epoly so that the extension defined by `poly` is isomorphic to the extension defined by first taking an extension by the unramified polynomial `upoly`, and then an extension by the Eisenstein polynomial `epoly`.

  We need better \(p\)-adic factoring in Sage before this function can be implemented.

  EXAMPLES:

  Sage

      sage: k = Qp(13)
      sage: x = polygen(k)
      sage: f = x^2 + 1
      sage: sage.rings.padics.factory.split(f, 10)
      Traceback (most recent call last):
      ...
      NotImplementedError: Extensions by general polynomials not yet supported.
      Please use an unramified or Eisenstein polynomial.

  Python

      >>> from sage.all import *
      >>> k = Qp(Integer(13))
      >>> x = polygen(k)
      >>> f = x**Integer(2) + Integer(1)
      >>> sage.rings.padics.factory.split(f, Integer(10))
      Traceback (most recent call last):
      ...
      NotImplementedError: Extensions by general polynomials not yet supported.
      Please use an unramified or Eisenstein polynomial.

sage.rings.padics.factory.truncate_to_prec(poly, R, absprec)

  Truncates the unused precision off of a polynomial.

  EXAMPLES:

  Sage

      sage: R = Zp(5)
      sage: S.<x> = R[]
      sage: from sage.rings.padics.factory import truncate_to_prec
      sage: f = x^4 + (3+O(5^6))*x^3 + O(5^4)
      sage: truncate_to_prec(f, R, 5)
      (1 + O(5^5))*x^4 + (3 + O(5^5))*x^3 + O(5^5)*x^2 + O(5^5)*x + O(5^4)

  Python

      >>> from sage.all import *
      >>> R = Zp(Integer(5))
      >>> S = R['x']; (x,) = S._first_ngens(1)
      >>> from sage.rings.padics.factory import truncate_to_prec
      >>> f = x**Integer(4) + (Integer(3)+O(Integer(5)**Integer(6)))*x**Integer(3) + O(Integer(5)**Integer(4))
      >>> truncate_to_prec(f, R, Integer(5))
      (1 + O(5^5))*x^4 + (3 + O(5^5))*x^3 + O(5^5)*x^2 + O(5^5)*x + O(5^4)