Documentation for v0.1 version (#199)

* Adding Equations, solving typos
* improve _code.rst
* the team rst and restuctore index.rst
* fixing errors

---------

Co-authored-by: Dario Coscia <dariocoscia@dhcp-015.eduroam.sissa.it>
This commit is contained in:
Dario Coscia
2023-11-08 14:39:00 +01:00
committed by Nicola Demo
parent 3f9305d475
commit 8b7b61b3bd
144 changed files with 2741 additions and 1766 deletions

View File

@@ -13,17 +13,21 @@ class SimplexDomain(Location):
:param simplex_matrix: A matrix of LabelTensor objects representing
a vertex of the simplex (a tensor), and the coordinates of the
point (a list of labels).
:type simplex_matrix: list[LabelTensor]
:param sample_surface: A variable for choosing sample strategies. If
`sample_surface=True` only samples on the Simplex surface
frontier are taken. If `sample_surface=False`, no such criteria
``sample_surface=True`` only samples on the Simplex surface
frontier are taken. If ``sample_surface=False``, no such criteria
is followed.
:type sample_surface: bool
.. warning::
Sampling for dimensions greater or equal to 10 could result
in a shrinking of the simplex, which degrades the quality
of the samples. For dimensions higher than 10, other algorithms
for sampling should be used.
:Example:
>>> spatial_domain = SimplexDomain(
[
@@ -48,12 +52,14 @@ class SimplexDomain(Location):
matrix_labels = simplex_matrix[0].labels
if not all(vertex.labels == matrix_labels for vertex in simplex_matrix):
raise ValueError(f"Labels don't match.")
# check consistency dimensions
dim_simplex = len(matrix_labels)
if len(simplex_matrix) != dim_simplex + 1:
raise ValueError("An n-dimensional simplex is composed by n + 1 tensors of dimension n.")
raise ValueError(
"An n-dimensional simplex is composed by n + 1 tensors of dimension n."
)
# creating vertices matrix
self._vertices_matrix = LabelTensor.vstack(simplex_matrix)
@@ -86,8 +92,10 @@ class SimplexDomain(Location):
for i, coord in enumerate(self.variables):
sorted_vertices = sorted(vertices, key=lambda vertex: vertex[i])
# respective coord bounded by the lowest and highest values
span_dict[coord] = [float(sorted_vertices[0][i]),
float(sorted_vertices[-1][i])]
span_dict[coord] = [
float(sorted_vertices[0][i]),
float(sorted_vertices[-1][i])
]
return CartesianDomain(span_dict)
@@ -96,31 +104,32 @@ class SimplexDomain(Location):
Check if a point is inside the simplex.
Uses the algorithm described involving barycentric coordinates:
https://en.wikipedia.org/wiki/Barycentric_coordinate_system.
.. note::
When ```'sample_surface'``` in the ```'__init()__'```
is set to ```'True'```, then the method only checks
points on the surface, and not inside the domain.
:param point: Point to be checked.
:type point: LabelTensor
:param check_border: Check if the point is also on the frontier
of the simplex, default False.
of the simplex, default ``False``.
:type check_border: bool
:return: Returning True if the point is inside, False otherwise.
:return: Returning ``True`` if the point is inside, ``False`` otherwise.
:rtype: bool
.. note::
When ``sample_surface`` in the ``__init()__``
is set to ``True``, then the method only checks
points on the surface, and not inside the domain.
"""
if not all(label in self.variables for label in point.labels):
raise ValueError(
"Point labels different from constructor"
f" dictionary labels. Got {point.labels},"
f" expected {self.variables}."
)
raise ValueError("Point labels different from constructor"
f" dictionary labels. Got {point.labels},"
f" expected {self.variables}.")
point_shift = point - self._vertices_matrix[-1]
point_shift = point_shift.tensor.reshape(-1, 1)
# compute barycentric coordinates
lambda_ = torch.linalg.solve(self._vectors_shifted * 1.0, point_shift * 1.0)
lambda_ = torch.linalg.solve(self._vectors_shifted * 1.0,
point_shift * 1.0)
lambda_1 = 1.0 - torch.sum(lambda_)
lambdas = torch.vstack([lambda_, lambda_1])
@@ -128,16 +137,15 @@ class SimplexDomain(Location):
if not check_border:
return all(torch.gt(lambdas, 0.0)) and all(torch.lt(lambdas, 1.0))
return all(torch.ge(lambdas, 0)) and (
any(torch.eq(lambdas, 0)) or any(torch.eq(lambdas, 1))
)
return all(torch.ge(lambdas, 0)) and (any(torch.eq(lambdas, 0))
or any(torch.eq(lambdas, 1)))
def _sample_interior_randomly(self, n, variables):
"""
Randomly sample points inside a simplex of arbitrary
dimension, without the boundary.
:param int n: Number of points to sample in the shape.
:param variables: pinn variable to be sampled, defaults to 'all'.
:param variables: pinn variable to be sampled, defaults to ``all``.
:type variables: str or list[str], optional
:return: Returns tensor of n sampled points.
:rtype: torch.Tensor
@@ -155,9 +163,9 @@ class SimplexDomain(Location):
sampled_points = []
while len(sampled_points) < n:
sampled_point = self._cartesian_bound.sample(
n=1, mode="random", variables=variables
)
sampled_point = self._cartesian_bound.sample(n=1,
mode="random",
variables=variables)
if self.is_inside(sampled_point, self._sample_surface):
sampled_points.append(sampled_point)
@@ -188,7 +196,9 @@ class SimplexDomain(Location):
# extract number of vertices
number_of_vertices = self._vertices_matrix.shape[0]
# extract idx lambda to set to zero randomly
idx_lambda = torch.randint(low=0, high=number_of_vertices, size=(1,))
idx_lambda = torch.randint(low=0,
high=number_of_vertices,
size=(1, ))
# build lambda vector
# 1. sampling [1, 2)
lambdas = torch.rand((number_of_vertices, 1))
@@ -203,13 +213,14 @@ class SimplexDomain(Location):
def sample(self, n, mode="random", variables="all"):
"""
Sample n points from Simplex domain.
:param int n: Number of points to sample in the shape.
:param str mode: Mode for sampling, defaults to 'random'.
Available modes include: 'random'.
:param variables: pinn variable to be sampled, defaults to 'all'.
:type variables: str or list[str], optional
:return: Returns LabelTensor of n sampled points
:rtype: LabelTensor(tensor)
:param str mode: Mode for sampling, defaults to ``random``. Available modes include: ``random``.
:param variables: Variables to be sampled, defaults to ``all``.
:type variables: str | list[str]
:return: Returns ``LabelTensor`` of n sampled points.
:rtype: LabelTensor
.. warning::
When ``sample_surface = True`` in the initialization, all
the variables are sampled, despite passing different once
@@ -225,4 +236,4 @@ class SimplexDomain(Location):
else:
raise NotImplementedError(f"mode={mode} is not implemented.")
return LabelTensor(sample_pts, labels=self.variables)
return LabelTensor(sample_pts, labels=self.variables)