polynomials_on_simplices.geometry.primitives.simplex module

Compute different simplex properties.

An n-simplex is the convex hull of n + 1 vertices (points in \(\mathbb{R}^d, d \geq n\)).

In this module a simplex is usually defined by specifying its vertices as rows in a matrix ((n + 1) x n for an n-simplex in \(\mathbb{R}^n\) and (n + 1) x d, d > n for an n-simplex embedded in a higher dimensional space).

affine_map_from_unit(vertices)[source]

Generate the affine map \(\Phi : \mathbb{R}^n \to \mathbb{R}^m\) which maps the n-dimensional unit simplex \(\Delta_c^n\) to the given n-dimensional simplex embedded in \(\mathbb{R}^m, n \leq m\).

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Function which takes an n-dimensional vector as input and returns an m-dimensional vector.
Return type:Callable \(\Phi(x)\).
affine_map_to_unit(vertices)[source]

Generate the affine map \(\Phi : \mathbb{R}^m \to \mathbb{R}^m\) which maps the given n-dimensional simplex embedded in \(\mathbb{R}^m, m \geq n,\) to the n-dimensional unit simplex \(\Delta_c^n\).

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Function which takes an n-dimensional vector as input and returns an n-dimensional vector.
Return type:Callable \(\Phi(x)\).
affine_transformation(vertices1, vertices2)[source]

Generate the affine transformation Ax + b which transforms from one simplex to another.

Parameters:
  • vertices1 – Vertices of the first simplex (the domain).
  • vertices2 – Vertices of the second simplex (the codomain).
Returns:

Tuple of A and b.

affine_transformation_from_unit(vertices)[source]

Generate the affine transformation Ax + b which transforms the unit simplex \(\Delta_c^n\) to the given n-dimensional simplex embedded in \(\mathbb{R}^m, n \leq m\).

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Tuple of A and b.
affine_transformation_to_unit(vertices)[source]

Generate the affine transformation Ax + b which transforms the given n-dimensional simplex embedded in \(\mathbb{R}^m, m \geq n\), to the n-dimensional unit simplex \(\Delta_c^n\).

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Tuple of A and b.
altitude(vertices, i)[source]

Compute an altitude in a simplex (i.e. the shortest distance from a vertex to the plane spanned by the opposite face).

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • i – Index of vertex/face whose altitude should be computed.
Returns:

The i:th altitude of the simplex.

barycentric_to_cartesian(barycentric, vertices)[source]

Compute the Cartesian coordinates for a point given by barycentric coordinates in a simplex.

Parameters:
  • barycentric – Barycentric coordinates of the point.
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

Cartesian coordinates of the point.

barycentric_to_cartesian_unit(barycentric)[source]

Compute the Cartesian coordinates for a point given by barycentric coordinates in the unit simplex \(\Delta_c^n\).

Parameters:barycentric – Barycentric coordinates of the point.
Returns:Cartesian coordinates of the point.
barycentric_to_trilinear(barycentric, vertices)[source]

Compute the trilinear coordinates (triangle), quadriplanar coordinates (tetrahedra) or the higher dimensional analog thereof (ratios between the distances from a point to the simplex (hyper-)faces) of a point with given barycentric coordinates. .. note:

The computed coordinates are normalized (or exact), so that the i:th entry is the actual distance to the
i:th face.
Parameters:
  • barycentric – Barycentric coordinates of the point.
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

Trilinear coordinates of the point.

basis(vertices, base_vertex=0)[source]

Compute the simplex basis consisting of the simplex edges emanating from a given vertex.

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • base_vertex – Base vertex for the basis.
Returns:

Basis vectors (matrix with the basis vectors as columns).

cartesian_to_barycentric(point, vertices)[source]

Compute the barycentric coordinates of a point in a simplex.

Parameters:
  • point – Cartesian point for which the barycentric coordinates should be computed.
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

barycentric coordinates of the point (\(b_i\) such that \(p = \sum b_i v_i\) and \(\sum b_i = 1\)).

cartesian_to_barycentric_unit(point)[source]

Compute the barycentric coordinates of a point in the unit simplex \(\Delta_c^n\).

Parameters:point – Cartesian point for which the barycentric coordinates should be computed.
Returns:barycentric coordinates of the point (\(b_i\) such that \(p = \sum b_i v_i\) and \(\sum b_i = 1\)).
centroid(vertices)[source]

Compute the centroid of a simplex (center of gravity).

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:The centroid of the simplex.
circumcenter(vertices)[source]

Compute the circumcenter of a simplex (the center of the n-sphere which passes through all the n + 1 vertices of the simplex).

Parameters:vertices – Vertices of the simplex (n + 1 x n matrix where row i contains the i:th vertex of the simplex).
Returns:The circumcenter of the simplex.
circumradius(vertices)[source]

Compute the circumradius of a simplex (the radius of the n-sphere which passes through all the n + 1 vertices of the simplex).

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:The circumradius of the simplex.
diameter(vertices)[source]

Compute largest distance between any two points in a simplex, i.e. the length of the longest simplex edge.

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:Simplex diameter.
dimension(vertices)[source]

Get the dimension n of a simplex.

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Dimension n of the simplex.
Return type:int
edges(vertices)[source]

Get all edges of a simplex.

This returns edges pointing from a vertex with lower index to a vertex with higher index, e.g. the edge from vertex 0 to vertex 1, from vertex 2 to vertex 4, etc. The order of the returned edges is as follows: First all edges containing vertex 0, with the second vertex in increasing order. Then all edges containing vertex 1, excluding the edge (v0, v1), with the second vertex in increasing order etc.

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:The edges of the simplex as rows in a matrix.
embedding_dimension(vertices)[source]

Get the dimension of the space a simplex is embedded in.

Parameters:vertices – Vertices of the simplex ((n + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Dimension m of the space the simplex is embedded in.
Return type:int
equilateral(n, d=1, ne=None)[source]

Create the vertices of a n-dimensional equilateral simplex (simplex where all the edges have the same length), with edge length d.

The n-dimensional equilateral simplex returned here is created from the n-1 dimensional equilateral simplex by adding a new vertex above the centroid (with positive n:th coordinate), with an equal distance to all the vertices of the n-1 dimensional simplex.

Parameters:
  • n (int) – Dimension of the simplex.
  • d (float) – Length of each edge in the simplex.
  • ne (int) – Dimension of the space the simplex is embedded in. Equal to n if None.
Returns:

Array of vertices in the simplex.

Return type:

Numpy array

Examples

>>> equilateral(1)
array([[0.],
       [1.]])
>>> equilateral(2, ne=3)
array([[0.       , 0.       , 0.       ],
       [1.       , 0.       , 0.       ],
       [0.5      , 0.8660254, 0.       ]])
face(vertices, i)[source]

Get the vertices of the i:th face of a simplex. The i:th face is the sub simplex containing all but the i:th vertex of the simplex.

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • i – Index of face.
Returns:

Vertices of the face.

face_normal(vertices, i)[source]

Compute normal to the i:th face of a simplex. The i:th face is the sub simplex containing all but the i:th vertex of the simplex. The normal is oriented so that it points out of the simplex (away from omitted vertex).

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • i – Index of face whose normal should be computed.
Returns:

Face normal vector (unit length vector).

in_subspace(point, vertices, tol=0.0)[source]

Check whether or not a point in \(\mathbb{R}^n\) lies in the subspace spanned by the edges of an m-dimensional simplex (\(m \leq n\)).

A point p is considered to lie in the simplex subspace if

\[\| p - p_{\text{proj}} \| leq \text{tol},\]

where \(p_{\text{proj}}\) is the projection of p onto the subspace spanned by the simplex.

Parameters:
  • point (Element in \(\mathbb{R}^n\)) – Point which we want to check.
  • vertices – Vertices of the simplex ((m + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • tol (float) – Tolerance for the distance check.
Returns:

Whether or not the point lies inside the subspace spanned by the edges of the simplex.

Return type:

bool

incenter(vertices)[source]

Compute the incenter of a simplex (the center of the largest inscribed n-sphere).

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:The incenter of the simplex.
inradius(vertices)[source]

Compute the inradius of a simplex (the radius of the largest inscribed n-sphere).

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:The inradius of the simplex.
inside_simplex(point, vertices, include_boundary=True, tol=0.0)[source]

Check whether or not a point lies inside a simplex.

A point lies inside a simplex if all its barycentric coordinates are in the range [0, 1] (or (0, 1) if the boundary is not included. However these kind of sharp checks often doesn’t make sense for floating point values. So for this a tolerance can be specified, in which case the point is considered to be inside the simplex if all its barycentric coordinates lies in the range (0 - tol, 1 + tol).

Parameters:
  • point – Point which we want to check.
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • include_boundary – Whether or not to consider points on the boundary as inside the simplex.
  • tol (float) – Tolerance used for fuzzy inside simplex checks.
Returns:

Whether or not the point lies inside the simplex.

Return type:

bool

is_degenerate(vertices, eps=0.0001)[source]

Check if a simplex is degenerate.

A simplex is considered degenerate if the ratio of any height of the simplex to the simplex diameter is smaller than the specified threshold \(\varepsilon\),

\[\min_{i = 1, 2, \ldots, n + 1} \frac{h_i}{d} < \varepsilon,\]

where \(h_i\) is the i:th height of the simplex (see altitude()) and d is the diameter of the simplex (see diameter()).

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • eps (float) – Triangle angle tolerance in the degeneracy check.
Returns:

Whether or not the triangle is degenerate (has an angle that is too small).

Return type:

bool

local_coordinates(vertices)[source]

Express the vertices of a simplex in the local orthonormal simplex basis (as given by the orthonormal_basis() function).

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:Coordinates of the simplex in the local orthonormal basis.
orientation(vertices)[source]

Compute the orientation of a simplex.

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:Orientation of the simplex (-1/1).
orthonormal_basis(vertices, base_vertex=0)[source]

Compute the orthonormalization of the simplex basis returned from the basis() function.

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • base_vertex – Base vertex for the basis.
Returns:

Orthonormal basis vectors (matrix with the basis vectors as columns).

signed_volume(vertices)[source]

Compute the signed volume of a simplex, defined by a list of vertices.

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:Signed volume of the simplex.
sub_simplex(vertices, f)[source]

Get the vertices of a sub simplex of a simplex.

Parameters:
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • f (List[int]) – Sub simplex f of the input simplex, defined as a list of indices (in [0, 1, …, n]) to vertices that are contained in f.
Returns:

Array of vertices in the sub simplex f.

Return type:

Numpy array

Examples

Sub simplices of the unit triangle:

>>> sub_simplex(unit(2), [0, 1])
array([[0., 0.],
       [1., 0.]])
>>> sub_simplex(unit(2), [1, 2])
array([[1., 0.],
       [0., 1.]])
>>> sub_simplex(unit(2), [2, 0])
array([[0., 1.],
       [0., 0.]])
>>> sub_simplex(unit(2), [1])
array([[1., 0.]])

Line sub simplex of the unit tetrahedra:

>>> sub_simplex(unit(3), [1, 3])
array([[1., 0., 0.],
       [0., 0., 1.]])
trilinear_to_barycentric(trilinear, vertices)[source]

Compute the barycentric coordinates of a point with given trilinear coordinates (ratios between the distances to the simplex (hyper-)faces).

Parameters:
  • trilinear – Trilinear coordinates of the point.
  • vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

Barycentric coordinates of the point.

unit(n, ne=None)[source]

Create the vertices of the n-dimensional unit simplex \(\Delta_c^n\).

Parameters:
  • n (int) – Dimension of the simplex.
  • ne (int) – Dimension of the space the simplex is embedded in. Equal to n if None.
Returns:

Array of vertices in the simplex.

Return type:

Numpy array

Examples

>>> unit(2)
array([[0., 0.],
       [1., 0.],
       [0., 1.]])
>>> unit(2, 3)
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])
volume(vertices)[source]

Compute the volume of a simplex, defined by a list of vertices.

Parameters:vertices – Vertices of the simplex ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:Volume of the simplex.
volume_unit(n)[source]

Compute the volume of the n-dimensional unit simplex \(\Delta_c^n\).

Parameters:n (int) – Dimension of the simplex.
Returns:Volume of the unit simplex.
Return type:float

Examples

>>> volume_unit(1)
1.0
>>> volume_unit(2)
0.5
>>> from fractions import Fraction
>>> Fraction(volume_unit(3)).limit_denominator()
Fraction(1, 6)