sagemath/sage

Defect `UniformRandomUniform` method in hypergraph_generators.py

Closed this issue · 11 comments

On going through the example in the documentation of UniformRandomUniform method following error occurs.

sage: H = hypergraphs.UniformRandomUniform(52, 3, 17)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-56df6490a66e> in <module>()
----> 1 H = hypergraphs.UniformRandomUniform(Integer(52), Integer(3), Integer(17))

/home/vipul/sage/local/lib/python3.7/site-packages/sage/graphs/hypergraph_generators.py in UniformRandomUniform(self, n, k, m)
    263         all_edges = Subsets(vertices, uniformity)
    264         try:
--> 265             edges = sample(all_edges, m)
    266         except OverflowError:
    267             raise OverflowError("binomial({}, {}) too large to be treated".format(n, k))

/home/vipul/sage/local/lib/python3.7/site-packages/sage/misc/prandom.py in sample(population, k)
    178         [357009070, 558990255, 196187132, 752551188, 85926697, 954621491, 624802848]
    179     """
--> 180     return _pyrand().sample(population, k)
    181
    182 def random():

/home/vipul/sage/local/lib/python3.7/random.py in sample(self, population, k)
    315             population = tuple(population)
    316         if not isinstance(population, _Sequence):
--> 317             raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
    318         randbelow = self._randbelow
    319         n = len(population)

TypeError: Population must be a sequence or set.  For dicts, use list(d).

Above defect should be fixed as early as possible. As It is already in latest sage release.

P.S : It also passes doctest. So something is wrong there too.

here is a link of discussion regarding this https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/sage-devel/6vv6k1vhihQ/55-lqLAXCAAJ

CC: @dcoudert @dimpase

Component: graph theory

Keywords: Hypergraph

Author: David Coudert

Branch: cc3dee4

Reviewer: Dima Pasechnik

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

Commit: cc3dee4

Author: David Coudert

comment:1

Works on my sage session.


New commits:

cc3dee4trac #29550: fix

Reviewer: Dima Pasechnik

comment:2

looks good to me, thanks!

comment:3

Note that the testing framework did not catch this because Sage is using a verbatim copy of the Python 2 version of the Random module during doctesting. This is to ensure identical results between Python 2 and Python 3 in the doctests.

comment:4

Oh, this is new to me. Does this mean we need to test on Python3 with a proper Python 3 Random module, just to make sure no more errors like this are left?

comment:5

Replying to @dimpase:

Oh, this is new to me. Does this mean we need to test on Python3 with a proper Python 3 Random module, just to make sure no more errors like this are left?

I have just done that, and fortunately there do not seem to be any other errors of this kind. Only two other files have doctests that lead to an error when using the Python 3 Random module, but in both cases this is because the tests implicitly depend on a specific "random" result from a previous doctest.

File "src/sage/modular/modform/numerical.py", line 216, in sage.modular.modform.numerical.NumericalEigenforms._eigenvectors
Failed example:
    diff = n._hecke_matrix*evectors - evectors*evalues
Exception raised:
    Traceback (most recent call last):
      File "/amd/compute/mwagerin/git/sage_compute/python3/local/lib/python3.7/site-packages/sage/doctest/forker.py", line 681, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/amd/compute/mwagerin/git/sage_compute/python3/local/lib/python3.7/site-packages/sage/doctest/forker.py", line 1123, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.modular.modform.numerical.NumericalEigenforms._eigenvectors[5]>", line 1, in <module>
        diff = n._hecke_matrix*evectors - evectors*evalues
      File "sage/structure/element.pyx", line 3719, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:22885)
        return coercion_model.bin_op(left, right, mul)
      File "sage/structure/coerce.pyx", line 1255, in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:11180)
        raise bin_op_exception(op, x, y)
    TypeError: unsupported operand parent(s) for *: 'Full MatrixSpace of 5 by 5 dense matrices over Real Double Field' and 'Full MatrixSpace of 3 by 3 sparse matrices over Complex Double Field'
File "src/sage/matrix/matrix_modn_sparse.pyx", line 213, in sage.matrix.matrix_modn_sparse.Matrix_modn_sparse._dict
Failed example:
    parent(m._dict()[26,9])
Exception raised:
    Traceback (most recent call last):
      File "/amd/compute/mwagerin/git/sage_compute/python3/local/lib/python3.7/site-packages/sage/doctest/forker.py", line 681, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/amd/compute/mwagerin/git/sage_compute/python3/local/lib/python3.7/site-packages/sage/doctest/forker.py", line 1123, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.matrix.matrix_modn_sparse.Matrix_modn_sparse._dict[3]>", line 1, in <module>
        parent(m._dict()[Integer(26),Integer(9)])
    KeyError: (26, 9)

Changed commit from cc3dee4 to none