Discrete
continuiti.discrete
Functionalities handling discretization of continuous functionals.
UniformBoxSampler(x_min, x_max)
¶
Bases: BoxSampler
Sample uniformly from an n-dimensional box.
Example
Output:Note
Using torch.rand
the UniformBoxSampler samples from a right-open
interval in every dimension.
Source code in src/continuiti/discrete/box_sampler.py
__call__(n)
¶
Generates n
uniformly distributed samples within the n-dimensional box.
PARAMETER | DESCRIPTION |
---|---|
n |
Number of samples to draw.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Samples as tensor of shape (dim, n). |
Source code in src/continuiti/discrete/uniform.py
RegularGridSampler(x_min, x_max, prefer_more_samples=True)
¶
Bases: BoxSampler
Regular Grid sampler class.
A class for generating regularly spaced samples within an n-dimensional box defined by its minimum and maximum corner points. This sampler creates a regular grid evenly spaced in each dimension, trying to create sub-boxes that are as close to cubical as possible.
If cubical sub-boxes are not possible (e.g., drawing 8 samples from a unit
square as 8 is not a power of 2), the prefer_more_samples
flag determines
of we either over or undersample the domain: If the product of the number of
samples in each dimension does not equal the requested number of samples,
the most under/over-sampled dimension will gain/lose one sample.
PARAMETER | DESCRIPTION |
---|---|
x_min |
The minimum corner point of the n-dimensional box, specifying the start of each dimension. |
x_max |
The maximum corner point of the n-dimensional box, specifying the end of each dimension. |
prefer_more_samples |
Flag indicating whether to prefer a sample count slightly above (True) or below (False) the desired total if an exact match isn't possible due to the properties of the regular grid. Defaults to True.
TYPE:
|
Example
Output:Source code in src/continuiti/discrete/regular_grid.py
__call__(n_samples)
¶
Generate a uniformly spaced grid of samples within an n-dimensional box.
PARAMETER | DESCRIPTION |
---|---|
n_samples |
The number of samples to generate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor containing the samples of shape (ndim, ~n_samples, ...) as a grid. |
Source code in src/continuiti/discrete/regular_grid.py
__calculate_samples_per_dim(n_samples)
¶
Calculate the (floating point) number of samples in each dimension to obtain an evenly spaced grid. This method also ensures that there is at least one sample in each dimension. The implemented method is best understood by the following example.
Example
For x_min = [0, 0, 1]
, xmax = [1, 2, 1]
and n_samples = 200
, this method computes:
PARAMETER | DESCRIPTION |
---|---|
n_samples |
Desired total number of samples.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Approximate number of samples for each dimension as a float vector. |
Source code in src/continuiti/discrete/regular_grid.py
__adjust_samples_to_fit(n_samples, samples_per_dim)
¶
Round and adjust the samples_per_dim
to fit the n_samples
requirement.
The result of __calculate_samples_per_dim
is a floating point
representation, which is rounded by this method to the next integer value.
If the product of the rounded samples equals the required number of samples,
we return this number. Otherwise, the most under-sampled dimension or
the most over-sampled dimension will gain or lose one sample, according
to the prefer_more_samples
flag.
PARAMETER | DESCRIPTION |
---|---|
n_samples |
Desired total number of samples.
TYPE:
|
samples_per_dim |
Initial distribution of samples across dimensions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Adjusted number of samples for each dimension as an integer vector. |
Source code in src/continuiti/discrete/regular_grid.py
Created: 2024-08-20