adaptive.learner.triangulation_backend module#
Select the triangulation backend used by the learners.
If the optional Rust-accelerated adaptive-triangulation package is
installed (pip install "adaptive[rust]"), it is used automatically as a
drop-in replacement for the pure-Python implementation in
adaptive.learner.triangulation, which makes LearnerND
significantly faster.
The selection can be overridden with the ADAPTIVE_TRIANGULATION_BACKEND
environment variable:
auto(default): use the Rust backend if available, else pure Pythonpython: always use the pure-Python implementationrust: require the Rust backend, raising ImportError if it is missing
The active backend is exposed as the string TRIANGULATION_BACKEND
("python" or "rust").
Note that the pure-Python implementation in adaptive.learner.triangulation
is always importable under its own name, regardless of the selected backend,
so pickles that reference it keep working.
- class adaptive.learner.triangulation_backend.Triangulation(coords)[source]#
Bases:
objectA triangulation object.
- Parameters:
coords (2d array-like of floats) β Coordinates of vertices.
- vertex_to_simplices#
Set of simplices connected to a vertex, the index of the vertex is the index of the list.
- Type:
list of sets
- Raises:
ValueError β if the list of coordinates is incorrect or the points do not form one or more simplices in the
- add_point(point, simplex=None, transform=None)[source]#
Add a new vertex and create simplices as appropriate.
- Parameters:
point (float vector) β Coordinates of the point to be added.
transform (N*N matrix of floats) β Multiplication matrix to apply to the point (and neighbouring simplices) when running the Bowyer Watson method.
simplex (tuple of ints, optional) β Simplex containing the point. Empty tuple indicates points outside the hull. If not provided, the algorithm costs O(N), so this should be used whenever possible.
- bowyer_watson(pt_index, containing_simplex=None, transform=None)[source]#
Modified Bowyer-Watson point adding algorithm.
Create a hole in the triangulation around the new point, then retriangulate this hole.
- Parameters:
pt_index (number) β the index of the point to inspect
- Returns:
deleted_simplices (set of tuples) β Simplices that have been deleted
new_simplices (set of tuples) β Simplices that have been added
- circumscribed_circle(simplex, transform)[source]#
Compute the center and radius of the circumscribed circle of a simplex.
- property default_transform#
- property dim#
- faces(dim=None, simplices=None, vertices=None)[source]#
Iterator over faces of a simplex or vertex sequence.
- get_face_sharing_neighbors(neighbors, simplex)[source]#
Keep only the simplices sharing a whole face with simplex.
- get_reduced_simplex(point, simplex, eps=1e-08) list[source]#
Check whether vertex lies within a simplex.
- Returns:
vertices β Indices of vertices of the simplex to which the vertex belongs. An empty list indicates that the vertex is outside the simplex.
- Return type:
list of ints
- property hull#
Compute hull from triangulation.
- adaptive.learner.triangulation_backend.circumsphere(pts)[source]#
Compute the center and radius of a N dimension sphere which touches each point in pts.
- Parameters:
pts (array-like, of shape (N-dim + 1, N-dim)) β The points for which we would like to compute a circumsphere.
- Returns:
center (tuple of floats of size N-dim)
radius (a positive float)
A valid center and radius, if a circumsphere is possible, and no points are repeated.
If points are repeated, or a circumsphere is not possible, will return nans, and a
ZeroDivisionError may occur.
Will fail for matrices which are not (N-dim + 1, N-dim) in size due to non-square determinants
will raise numpy.linalg.LinAlgError.
May fail for points that are integers (due to 32bit integer overflow).
- adaptive.learner.triangulation_backend.fast_2d_circumcircle(points)[source]#
Compute the center and radius of the circumscribed circle of a triangle
- Parameters:
points (2D array-like) β the points of the triangle to investigate
- Returns:
(center point : tuple(float), radius: float)
- Return type:
- adaptive.learner.triangulation_backend.fast_3d_circumcircle(points)[source]#
Compute the center and radius of the circumscribed sphere of a simplex.
- Parameters:
points (2D array-like) β the points of the triangle to investigate
- Returns:
(center point : tuple(float), radius: float)
- Return type:
- adaptive.learner.triangulation_backend.fast_norm(v)[source]#
Take the vector norm for len 2, 3 vectors. Defaults to a square root of the dot product for larger vectors.
Note that for large vectors, it is possible for integer overflow to occur. For instance: vec = [49024, 59454, 12599, -63721, 18517, 27961] dot(vec, vec) = -1602973744
- adaptive.learner.triangulation_backend.orientation(face, origin)[source]#
Compute the orientation of the face with respect to a point, origin.
- Parameters:
face (array-like, of shape (N-dim, N-dim)) β The hyperplane we want to know the orientation of Do notice that the order in which you provide the points is critical
origin (array-like, point of shape (N-dim)) β The point to compute the orientation from
- Returns:
0 if the origin lies in the same hyperplane as face,
-1 or 1 to indicate left or right orientation
If two points lie on the same side of the face, the orientation will
be equal, if they lie on the other side of the face, it will be negated.
- adaptive.learner.triangulation_backend.resolve_triangulation_class(backend='auto')[source]#
Return the
Triangulationclass to use for backend.- Parameters:
backend (str or type) β
"auto"(the module-level default backend, which prefers the Rust implementation when available),"python","rust", or aTriangulation-compatible class.
- adaptive.learner.triangulation_backend.simplex_volume_in_embedding(vertices) float[source]#
Calculate the volume of a simplex in a higher dimensional embedding. That is: dim > len(vertices) - 1. For example if you would like to know the surface area of a triangle in a 3d space.
This algorithm has not been tested for numerical stability.
- Parameters:
vertices (2D arraylike of floats) β
- Returns:
volume β the volume of the simplex with given vertices.
- Return type:
- Raises:
ValueError β if the vertices do not form a simplex (for example, because they are coplanar, colinear or coincident).
Warning β this algorithm has not been tested for numerical stability.: