sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.33k stars 453 forks source link

Issues in using random_element and random_matrix for lattice padic fields #30694

Open 333767a0-8bb0-4499-a032-33e52572d678 opened 3 years ago

333767a0-8bb0-4499-a032-33e52572d678 commented 3 years ago

Using version 9.2 of Sage, it seems that:

F = Qp(3, type='lattice-float')
F.random_element()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element._pow_ (build/cythonized/sage/structure/element.c:14697)()
   2093         try:
-> 2094             python_op = (<object>self)._pow_
   2095         except AttributeError:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4703)()
    492         """
--> 493         return self.getattr_from_category(name)
    494 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.getattr_from_category (build/cythonized/sage/structure/element.c:4815)()
    505             cls = P._abstract_element_class
--> 506         return getattr_from_other_class(self, cls, name)
    507 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/cpython/getattr.pyx in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:2620)()
    371         dummy_error_message.name = name
--> 372         raise AttributeError(dummy_error_message)
    373     attribute = <object>attr

AttributeError: 'InfinityRing_class_with_category' object has no attribute '__custom_name'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-839c07c9da3f> in <module>
----> 1 F.random_element()

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/padic_base_leaves.py in random_element(self, prec, integral)
   1092             prec = self._prec_cap_absolute - val
   1093         p = self.prime()
-> 1094         x = ZZ.random_element(p**prec)
   1095         relcap = x.valuation(p) + self._prec_cap_relative
   1096         if relcap < prec:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/integer.pyx in sage.rings.integer.Integer.__pow__ (build/cythonized/sage/rings/integer.c:15196)()
   2204             return (<Integer>left)._pow_(right)
   2205         elif isinstance(left, Element):
-> 2206             return coercion_model.bin_op(left, right, operator.pow)
   2207         # left is a non-Element: do the powering with a Python int
   2208         return left ** int(right)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:10267)()
   1202             self._record_exception()
   1203         else:
-> 1204             return PyObject_CallObject(op, xy)
   1205 
   1206         if op is mul:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__pow__ (build/cythonized/sage/structure/element.c:14422)()
   2053         cdef int cl = classify_elements(left, right)
   2054         if HAVE_SAME_PARENT(cl):
-> 2055             return (<Element>left)._pow_(right)
   2056         if BOTH_ARE_ELEMENT(cl):
   2057             return coercion_model.bin_op(left, right, pow)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element._pow_ (build/cythonized/sage/structure/element.c:14769)()
   2094             python_op = (<object>self)._pow_
   2095         except AttributeError:
-> 2096             raise bin_op_exception('^', self, other)
   2097         else:
   2098             return python_op(other)

TypeError: unsupported operand parent(s) for ^: 'The Infinity Ring' and 'The Infinity Ring'
G = Qp(11, 1, type='lattice-cap')
G.random_element()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-f92c3aada714> in <module>
----> 1 G.random_element()

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/padic_base_leaves.py in random_element(self, prec, integral)
   1092             prec = self._prec_cap_absolute - val
   1093         p = self.prime()
-> 1094         x = ZZ.random_element(p**prec)
   1095         relcap = x.valuation(p) + self._prec_cap_relative
   1096         if relcap < prec:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/integer_ring.pyx in sage.rings.integer_ring.IntegerRing_class.random_element (build/cythonized/sage/rings/integer_ring.c:6528)()
    716         if x is not None and y is not None and x >= y:
    717             raise TypeError("x must be < y")
--> 718         self._randomize_mpz(z.value, x, y, distribution)
    719         return z
    720 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/integer_ring.pyx in sage.rings.integer_ring.IntegerRing_class._randomize_mpz (build/cythonized/sage/rings/integer_ring.c:6759)()
    747                     mpz_set_si(value, rstate.c_random()%5 - 2)
    748                 else:
--> 749                     n_max = x if isinstance(x, integer.Integer) else self(x)
    750                     mpz_urandomm(value, rstate.gmp_state, n_max.value)
    751             else:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9336)()
    898         if mor is not None:
    899             if no_extra_args:
--> 900                 return mor._call_(x)
    901             else:
    902                 return mor._call_with_args(x, args, kwds)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/rational.pyx in sage.rings.rational.Q_to_Z._call_ (build/cythonized/sage/rings/rational.c:32481)()
   4157         """
   4158         if not mpz_cmp_si(mpq_denref((<Rational>x).value), 1) == 0:
-> 4159             raise TypeError("no conversion of this rational to integer")
   4160         cdef Integer n = Integer.__new__(Integer)
   4161         n.set_from_mpz(mpq_numref((<Rational>x).value))

TypeError: no conversion of this rational to integer
G = Qp(11, 1, type='lattice-cap')
random_matrix(G, 3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-72da8fe3ab6b> in <module>
----> 1 random_matrix(G, Integer(3))

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/matrix/special.py in random_matrix(ring, nrows, ncols, algorithm, implementation, *args, **kwds)
    602         A = copy(parent.zero_matrix())
    603         if density is None:
--> 604             A.randomize(density=float(1), nonzero=False, *args, **kwds)
    605         else:
    606             A.randomize(density=density, nonzero=True, *args, **kwds)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/matrix/matrix2.pyx in sage.matrix.matrix2.Matrix.randomize (build/cythonized/sage/matrix/matrix2.c:65755)()
   8969                 for i from 0 <= i < self._nrows:
   8970                     for j from 0 <= j < self._ncols:
-> 8971                         self.set_unsafe(i, j, R.random_element(*args, **kwds))
   8972             else:
   8973                 num = int(self._nrows * self._ncols * density)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/padic_base_leaves.py in random_element(self, prec, integral)
   1092             prec = self._prec_cap_absolute - val
   1093         p = self.prime()
-> 1094         x = ZZ.random_element(p**prec)
   1095         relcap = x.valuation(p) + self._prec_cap_relative
   1096         if relcap < prec:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/integer_ring.pyx in sage.rings.integer_ring.IntegerRing_class.random_element (build/cythonized/sage/rings/integer_ring.c:6528)()
    716         if x is not None and y is not None and x >= y:
    717             raise TypeError("x must be < y")
--> 718         self._randomize_mpz(z.value, x, y, distribution)
    719         return z
    720 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/integer_ring.pyx in sage.rings.integer_ring.IntegerRing_class._randomize_mpz (build/cythonized/sage/rings/integer_ring.c:6759)()
    747                     mpz_set_si(value, rstate.c_random()%5 - 2)
    748                 else:
--> 749                     n_max = x if isinstance(x, integer.Integer) else self(x)
    750                     mpz_urandomm(value, rstate.gmp_state, n_max.value)
    751             else:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9336)()
    898         if mor is not None:
    899             if no_extra_args:
--> 900                 return mor._call_(x)
    901             else:
    902                 return mor._call_with_args(x, args, kwds)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/rational.pyx in sage.rings.rational.Q_to_Z._call_ (build/cythonized/sage/rings/rational.c:32481)()
   4157         """
   4158         if not mpz_cmp_si(mpq_denref((<Rational>x).value), 1) == 0:
-> 4159             raise TypeError("no conversion of this rational to integer")
   4160         cdef Integer n = Integer.__new__(Integer)
   4161         n.set_from_mpz(mpq_numref((<Rational>x).value))

TypeError: no conversion of this rational to integer

IndexError example (only happened once for this field):

F = Qp(79, 28, type='lattice-cap')
random_matrix(F, 3)
~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/matrix/special.py in random_matrix(ring, nrows, ncols, algorithm, implementation, *args, **kwds)
    600         density = kwds.pop('density', None)
    601         # zero matrix is immutable, copy is mutable
--> 602         A = copy(parent.zero_matrix())
    603         if density is None:
    604             A.randomize(density=float(1), nonzero=False, *args, **kwds)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedMethodCallerNoArgs.__call__ (build/cythonized/sage/misc/cachefunc.c:12907)()
   2308         if self.cache is None:
   2309             f = self.f
-> 2310             self.cache = f(self._instance)
   2311         return self.cache
   2312 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/matrix/matrix_space.py in zero_matrix(self)
   1766             False
   1767         """
-> 1768         zero = self.base_ring().zero()
   1769         res = self.element_class(self, zero, False, False)
   1770         res.set_immutable()

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/ring.pyx in sage.rings.ring.Ring.zero (build/cythonized/sage/rings/ring.c:7431)()
    728         """
    729         if self._zero_element is None:
--> 730             x = self(0)
    731             self._zero_element = x
    732             return x

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9336)()
    898         if mor is not None:
    899             if no_extra_args:
--> 900                 return mor._call_(x)
    901             else:
    902                 return mor._call_with_args(x, args, kwds)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/categories/map.pyx in sage.categories.map.FormalCompositeMap._call_ (build/cythonized/sage/categories/map.c:11819)()
   1692         """
   1693         for f in self.__list:
-> 1694             x = f._call_(x)
   1695         return x
   1696 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ 
(build/cythonized/sage/structure/coerce_maps.c:4695)()
    151     argument is assumed to be bound to the codomain).
    152     """
--> 153     cpdef Element _call_(self, x):
    154         cdef Parent C = self._codomain
    155         try:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ 
(build/cythonized/sage/structure/coerce_maps.c:4623)()
    159                 print(type(C), C)
    160                 print(type(C._element_constructor), C._element_constructor)
--> 161             raise
    162 
    163     cpdef Element _call_with_args(self, x, args=(), kwds={}):

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ 
(build/cythonized/sage/structure/coerce_maps.c:4515)()
    154         cdef Parent C = self._codomain
    155         try:
--> 156             return C._element_constructor(x)
    157         except Exception:
    158             if print_warnings:

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/generic_nodes.py in _element_constructor_(self, x, prec)
    584         except (TypeError, ValueError, AttributeError):
    585             pass
--> 586         return self._element_class(self, x, prec)
    587 
    588     def convert_multiple(self, *elts):

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/padic_lattice_element.py in __init__(self, parent, x, prec, dx, dx_mode, valuation, check, reduce)
    133         else:
    134             self._value = pRational(p, QQ(x))
--> 135         trunc = self._declare_new_element(dx, prec, dx_mode)
    136         if reduce:
    137             self._value = self._value.reduce(trunc)

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/padic_lattice_element.py in _declare_new_element(self, dx, prec, dx_mode)
   1272         else:
   1273             capped = False
-> 1274         self._precision._new_element(self, dx, bigoh=prec, dx_mode=dx_mode, capped=capped)
   1275         return prec
   1276 

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/lattice_precision.py in _new_element(self, x, dx, bigoh, dx_mode, capped)
   1748         """
   1749         # First we delete some elements marked for deletion
-> 1750         self.del_elements(threshold=self._threshold_deletion)
   1751 
   1752         # Then we add the new element

~/workspace/padic_project/sage/local/lib/python3.7/site-packages/sage/rings/padics/lattice_precision.py in del_elements(self, threshold)
   1844 
   1845             tme = walltime()
-> 1846             ref = self._elements[index]
   1847             del self._elements[index]
   1848             del self._matrix[ref]

IndexError: list index out of range 

CC: @xcaruso

Component: padics

Keywords: random, lattice

Issue created by migration from https://trac.sagemath.org/ticket/30694

mkoeppe commented 3 years ago
comment:2

Moving to 9.4, as 9.3 has been released.

saraedum commented 2 years ago
comment:5

This sounds like it should be easy to fix actually.