/// <summary>Initializes a new instance of the <see cref="HaltonSequence" /> class.</summary> /// <param name="dimensionCount">The number of dimensions.</param> /// <param name="startIndex">The start index.</param> /// <exception cref="ArgumentException">Thrown when the specified index is negative. /// or /// The specified number of dimensions is less than 1.</exception> public HaltonSequence(int dimensionCount, int startIndex) { if (dimensionCount < 1) { throw new ArgumentException("The number of dimensions must be greater than zero.", nameof(dimensionCount)); } if (startIndex < 0) { throw new ArgumentException("The index must not be negative.", nameof(startIndex)); } this.dimensionCount = dimensionCount; vanDerCorputSequences = new VanDerCorputSequence[dimensionCount]; for (int j = 0; j < dimensionCount; j++) { vanDerCorputSequences[j] = new VanDerCorputSequence(startIndex, PrimeNumberMath.Primes[j + 1]); } }
/// <summary>Gets the original index of an element of the Halton sequence.</summary> /// <param name="element">The element of the Halton sequence.</param> /// <returns>The corresponding index.</returns> /// <exception cref="OverflowException">Thrown when the resulting integer value is too large.</exception> /// <exception cref="ArgumentNullException">Thrown when the specified element is <c>null</c>.</exception> /// <exception cref="ArgumentException">Thrown when the specified element does not contain any values. /// or /// A component is less than 0 or greater than or equal to 1.</exception> /// <remarks>Only the first component, assumed to be computed with base 2, is used for computing the index.</remarks> public static int Invert(IReadOnlyList <double> element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (element.Count == 0) { throw new ArgumentException("The element is empty.", nameof(element)); } if (element.Any(c => c < 0.0 || c >= 1.0)) { throw new ArgumentException("Each component must be >= 0 and < 1.", nameof(element)); } // Invert using the first component only, because working with base 2 is accurate. return(VanDerCorputSequence.Invert(element[0])); }