Integrators

class diffinytrace.integrators.Integrator[source]

Bases: object

sample(num_points: int | list[int], method: IntegrationMethod) tuple[Tensor, Tensor][source]

Sample points and weights using the specified method. :param num_points: Number of points in each dimension. :type num_points: int or list :param method: The integration method to use. Options are ‘simpson’, ‘midpoint’, ‘monte_carlo’, ‘sobol’, and ‘sobol_pow2’. :type method: str

Returns:

A tuple containing the sampled points and their corresponding weights.

Return type:

tuple

in_bounds(x: Tensor) Tensor[source]
get_volume() float[source]
class diffinytrace.integrators.Cube(bounds)[source]

Bases: Integrator

Integrator for a multi-dimensional cube (hyperrectangle).

Parameters:

bounds (array-like) – The bounds for each dimension of the cube. Should be a list or array of shape (n_dim, 2), where each row specifies [lower_bound, upper_bound] for a dimension.

Example

>>> cube = dit.integrators.Cube([[0, 1], [0, 1]])
>>> points, weights = cube.sample([10, 10], method=IntegrationMethod.MIDPOINT)
>>> volume = cube.get_volume()
>>> all_in_bounds = cube.in_bounds(points)
>>> print("Sampled points:", points)
>>> print("Integration weights:", weights)
>>> print("Cube volume:", volume)
>>> print("All points in bounds:", all_in_bounds)
sample(num_points: int | list[int], method: IntegrationMethod = IntegrationMethod.MIDPOINT) tuple[Tensor, Tensor][source]

Sample points and weights using the specified method.

Parameters:
  • num_points (int or list) – Number of points in each dimension.

  • method (str) – The integration method to use. Options are ‘simpson’, ‘midpoint’, ‘monte_carlo’, ‘sobol’, and ‘sobol_pow2’.

Returns:

A tuple containing the sampled points and their corresponding weights.

Return type:

tuple

in_bounds(x: Tensor) Tensor[source]
get_volume() float[source]
Returns:

Volume of the Cube.

Return type:

float

class diffinytrace.integrators.Disc(radius)[source]

Bases: Integrator

Integrator for a 2D disc (circle).

Parameters:

radius (float) – The radius of the disc.

Example

>>> disc = dit.integrators.Disc(1.0)
>>> points, weights = disc.sample(2**4, method="sobol_pow2")
>>> volume = disc.get_volume()
>>> all_in_bounds = disc.in_bounds(points)
>>> print("Sampled points:", points)
>>> print("Integration weights:", weights)
>>> print("Disc area:", volume)
>>> print("All points in bounds:", all_in_bounds)
sample(num_points: int | list[int], method: IntegrationMethod = IntegrationMethod.SOBOL) tuple[Tensor, Tensor][source]

Sample points and weights using the specified method.

Parameters:
  • num_points (int or list) – Number of points in each dimension.

  • method (str) – The integration method to use. Options are ‘simpson’, ‘midpoint’, ‘monte_carlo’, ‘sobol’, and ‘sobol_pow2’.

Returns:

A tuple containing the sampled points and their corresponding weights.

Return type:

tuple

in_bounds(x)[source]

Check if points are within the disc.

Parameters:

x (torch.Tensor) – Points to check.

Returns:

Boolean tensor indicating if points are within the disc.

Return type:

torch.Tensor

get_volume()[source]

Calculate the volume of the disc.

Returns:

Volume of the disc.

Return type:

float

class diffinytrace.integrators.IntegrationMethod(*values)[source]

Bases: Enum

SIMPSON = 'simpson'
MIDPOINT = 'midpoint'
MONTE_CARLO = 'monte_carlo'
SOBOL = 'sobol'
SOBOL_POW2 = 'sobol_pow2'