Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Typo, parameter checks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelegm committed Jun 29, 2018
1 parent 2b6b048 commit de49d8c
Showing 1 changed file with 84 additions and 7 deletions.
91 changes: 84 additions & 7 deletions src/sage/graphs/hypergraph_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def CompleteUniform(self, n, k):

def UniformRandomUniform(self, n, k, m):
r"""
Retrun a uniformly sampled `k`-uniform hypergraph on `n` points with
Return a uniformly sampled `k`-uniform hypergraph on `n` points with
`m` hyperedges.
- ``n`` -- number of nodes of the graph
Expand All @@ -188,24 +188,59 @@ def UniformRandomUniform(self, n, k, m):
- ``m`` -- number of edges
TESTS::
EXAMPLES::
sage: H = hypergraphs.UniformRandomUniform(52, 3, 17)
sage: H
Incidence structure with 52 points and 17 blocks
sage: H.is_connected()
False
TESTS::
sage: hypergraphs.UniformRandomUniform(-52, 3, 17)
Traceback (most recent call last):
...
ValueError: number of vertices should be non-negative
sage: hypergraphs.UniformRandomUniform(52.9, 3, 17)
Traceback (most recent call last):
...
ValueError: number of vertices should be an integer
sage: hypergraphs.UniformRandomUniform(52, -3, 17)
Traceback (most recent call last):
...
ValueError: the uniformity should be non-negative
sage: hypergraphs.UniformRandomUniform(52, I, 17)
Traceback (most recent call last):
...
ValueError: the uniformity should be an integer
"""
from sage.rings.integer import Integer
from sage.combinat.subset import Subsets
from sage.misc.prandom import sample
vertices = list(range(n))
all_edges = Subsets(vertices, k)

# Construct the vertex set
if n < 0:
raise ValueError("number of vertices should be non-negative")
try:
nverts = Integer(n)
except TypeError:
raise ValueError("number of vertices should be an integer")
vertices = range(nverts)

# Construct the edge set
if k < 0:
raise ValueError("the uniformity should be non-negative")
try:
uniformity = Integer(k)
except TypeError:
raise ValueError("the uniformity should be an integer")
all_edges = Subsets(vertices, uniformity)
try:
edges = sample(all_edges, m)
except OverflowError:
raise OverflowError("binomial({},{}) too large to be treated".format(n,k))
except ValueError:
raise ValueError("number of edges m must between 0 and binomial({},{})".format(n,k))
raise ValueError("number of edges m must be between 0 and binomial({},{})".format(n,k))

from sage.combinat.designs.incidence_structures import IncidenceStructure
return IncidenceStructure(vertices, edges)
Expand All @@ -221,16 +256,58 @@ def BinomialRandomUniform(self, n, k, p):
- ``p`` -- probability of an edge
TESTS::
EXAMPLES::
sage: hypergraphs.BinomialRandomUniform(50, 3, 1).num_blocks()
19600
sage: hypergraphs.BinomialRandomUniform(50, 3, 0).num_blocks()
0
TESTS::
sage: hypergraphs.BinomialRandomUniform(50, 3, -0.1)
Traceback (most recent call last):
...
ValueError: edge probability should be in [0,1]
sage: hypergraphs.BinomialRandomUniform(50, 3, 1.1)
Traceback (most recent call last):
...
ValueError: edge probability should be in [0,1]
sage: hypergraphs.BinomialRandomUniform(-50, 3, 0.17)
Traceback (most recent call last):
...
ValueError: number of vertices should be non-negative
sage: hypergraphs.BinomialRandomUniform(50.9, 3, 0.17)
Traceback (most recent call last):
...
ValueError: number of vertices should be an integer
sage: hypergraphs.BinomialRandomUniform(50, -3, 0.17)
Traceback (most recent call last):
...
ValueError: the uniformity should be non-negative
sage: hypergraphs.BinomialRandomUniform(50, I, 0.17)
Traceback (most recent call last):
...
ValueError: the uniformity should be an integer
"""
from sage.rings.integer import Integer
if n < 0:
raise ValueError("number of vertices should be non-negative")
try:
nverts = Integer(n)
except TypeError:
raise ValueError("number of vertices should be an integer")
if k < 0:
raise ValueError("the uniformity should be non-negative")
try:
uniformity = Integer(k)
except TypeError:
raise ValueError("the uniformity should be an integer")
if not 0 <= p <= 1:
raise ValueError("edge probability should be in [0,1]")

import numpy.random as nrn
from sage.functions.other import binomial
m = nrn.binomial(binomial(n, k), p)
m = nrn.binomial(binomial(nverts, uniformity), p)
return hypergraphs.UniformRandomUniform(n, k, m)


Expand Down

0 comments on commit de49d8c

Please sign in to comment.