Skip to content

Commit d8fdf50

Browse files
author
David Butterworth
committed
Add VanDerCorputSequence() which returns a generator.
1 parent 479efff commit d8fdf50

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/prpy/util.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,52 @@ def ConvertIntToBinaryString(x, reverse=False):
976976
return ''.join(bin(x)[2:])
977977

978978

979+
def VanDerCorputSequence(lower=0.0, upper=1.0, include_endpoints=True):
980+
"""
981+
Generate the binary Van der Corput sequence, where each value
982+
is a dyadic fraction re-scaled to the desired range.
983+
984+
For example, on the interval [0,1], the first 5 values of
985+
the Van der Corput sequence are:
986+
[0.0, 1.0, 0.5, 0.5, 0.75]
987+
988+
@param float lower: The first value of the range of the sequence.
989+
@param float upper: The last value of the range of the sequence.
990+
991+
@param bool include_endpoints: If True, the output sequence will
992+
include the value 'lower' and the
993+
value 'upper'.
994+
If False, these endpoint values
995+
will not be returned.
996+
997+
@returns generator: A sequence of float values.
998+
"""
999+
from itertools import count, chain
1000+
1001+
if include_endpoints == True:
1002+
endpoints = (0.0, 1.0)
1003+
else:
1004+
endpoints = None
1005+
1006+
# Get a sequence of reversed binary numbers:
1007+
# '1', '01', '11', '001', '101', '011', '111', '0001', ....
1008+
#
1009+
# Note: count(1) is a generator, starting at 1, making steps of 1.
1010+
reverse_binary_seq = (ConvertIntToBinaryString(x, True) for x in count(1))
1011+
1012+
# From the reversed binary sequence, generate the Van der Corput
1013+
# sequence, for which: 0.0 < x < 1.0 (the end-points are excluded)
1014+
# 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625, ....
1015+
#
1016+
# Note: int(x,2) converts the binary string (base 2) to an integer.
1017+
raw_seq = (float(int(x,2)) / (2**len(x)) for x in reverse_binary_seq)
1018+
1019+
# Scale the Van der Corput sequence across the desired range
1020+
# and optionally add the end-points.
1021+
scale = float(upper - lower)
1022+
return (scale * val + lower for val in chain(endpoints or [], raw_seq))
1023+
1024+
9791025
def GetCollisionCheckPts(robot, traj, include_start=True, start_time=0.,
9801026
first_step=None, epsilon=1e-6):
9811027
"""

0 commit comments

Comments
 (0)