Skip to content

Commit

Permalink
API: add parameter nsamples to function polytope.polytope.volume
Browse files Browse the repository at this point in the history
to enable changing the number of samples used in the
randomized algorithm that estimates polytope volume.
  • Loading branch information
johnyf committed May 28, 2021
1 parent 7f59645 commit 2bd0a5b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion polytope/polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,12 +1463,15 @@ def intersect(poly1, poly2, abs_tol=ABS_TOL):
return poly1.intersect(poly2, abs_tol)


def volume(polyreg):
def volume(polyreg, nsamples=None):
"""Approximately compute the volume of a Polytope or Region.
A randomized algorithm is used.
@type polyreg: L{Polytope} or L{Region}
@param nsamples: number of samples to generate to
use for estimating volume
@type nsamples: positive integer
@return: Volume of input
"""
Expand Down Expand Up @@ -1496,6 +1499,18 @@ def volume(polyreg):
N = 3000
else:
N = 10000
if nsamples is not None and nsamples < 1:
raise ValueError(
'`nsamples` must be >= 1, given: {v}'.format(
v=nsamples))
if nsamples is not None:
N = nsamples
if N != int(N):
raise ValueError((
'it appears that a noninteger number of samples '
'has been given, namely: {v}'
).format(
v=nsamples))
l_b, u_b = polyreg.bounding_box
x = (np.tile(l_b, (1, N))
+ np.random.rand(n, N)
Expand Down

0 comments on commit 2bd0a5b

Please sign in to comment.