Welcome to Polynomials on Simplices’s documentation!

latest

polynomials_on_simplices package

Subpackages

polynomials_on_simplices.algebra package
Submodules
polynomials_on_simplices.algebra.algebraic_operations module

General algebraic operations.

composition(f, g)[source]

Create the composition \(f \circ g\) of two functions \(f, g\).

\[ \begin{align}\begin{aligned}g : X \to Y,\\f : Y \to Z,\\f \circ g : X \to Z,\\(f \circ g)(x) = f(g(x)).\end{aligned}\end{align} \]
Parameters:
  • f (Callable) – Function applied last.
  • g (Callable) – Function applied first.
Returns:

The composition \(f \circ g\).

Return type:

Callable

polynomials_on_simplices.algebra.modular_arithmetic module

Functionality for modular arithmetic.

class IntegerModuloN(value, n)[source]

Bases: object

Integer modulo n (element of \(\mathbb{Z}/n\mathbb{Z})\).

We have

\[\mathbb{Z}/n\mathbb{Z} = \mathbb{Z}/\sim,\]

where

\[a \sim b \text{ if } a \bmod n = b \bmod n \iff \exists c \in n\mathbb{Z} \text{ such that } a + c = b.\]

This class defines the ring structure of integers modulo n.

Addition:

\[+ : \mathbb{Z}/n\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z} \to \mathbb{Z}/n\mathbb{Z},\]
\[[a] + [b] = [a + b].\]

Multiplication:

\[\cdot : \mathbb{Z}/n\mathbb{Z} \times \mathbb{Z}/n\mathbb{Z} \to \mathbb{Z}/n\mathbb{Z},\]
\[[a] \cdot [b] = [a \cdot b].\]
Parameters:
  • value – Value of the integer.
  • n – Modulus of the integer.
polynomials_on_simplices.algebra.multiindex module

Operations on multi-indices (elements of \(\mathbb{N}_0^n\)).

class MultiIndex(*components)[source]

Bases: object

A multi-index (element of \(\mathbb{N}_0^n\)).

This class defines the basic algebraic operations on multi-indices:

Addition:

\[+ : \mathbb{N}_0^n \times \mathbb{N}_0^n \to \mathbb{N}_0^n,\]
\[\alpha + \beta = (\alpha_1 + \beta_1, \alpha_2 + \beta_2, \ldots, \alpha_n + \beta_n).\]

Power:

\[\operatorname{pow} : R^n \times \mathbb{N}_0^n \to R,\]
\[\operatorname{pow}(x, \alpha) \equiv x^{\alpha} = x_1^{\alpha_1} x_2^{\alpha_2} \ldots x_n^{\alpha_n},\]

where \(R\) is any ring.

Parameters:components (int or Iterable[int]) – Component(s) (indices) for the multi-index.
components()[source]

Multi-index components/indices.

Returns:The multi-index components.
Return type:tuple[int]
to_tuple()[source]

Multi-index converted to a tuple.

Returns:Tuple containing the multi-index components (indices).
Return type:tuple[int]
class MultiIndexIterator(n, r)[source]

Bases: object

Iterate over all n-dimensional multi-indices with norm <= r.

Parameters:
  • n (int) – Dimension of the multi-indices we iterate over.
  • r (int) – Maximum norm of the multi-indices we iterate over.
next()[source]

Proceed to next multi-index.

class MultiIndexIteratorMultiCap(n, r)[source]

Bases: object

Iterate over all n-dimensional multi-indices with satisfying a_i <= r_i.

Parameters:
  • n (int) – Dimension of the multi-indices we iterate over.
  • r (Iterable[int]) – Maximum value for each component of the multi-indices we iterate over.
next()[source]

Proceed to next multi-index.

binom(a, b)[source]

Binomial coefficient of two multi-indices, a over b,

\[\binom{a}{b} = \frac{a!}{b!(a - b)!}.\]

See factorial().

Parameters:
  • a – Multi-index.
  • b – Multi-index.
Returns:

a choose b.

Return type:

int

exact_norm_to_general(a)[source]

Conversion of a multi-index from exact to general form.

Convert an n-dimensional exact multi-index to a general n-1-dimensional multi-index by removing the first number in the multi-index (exact meaning that the multi-index has norm r).

Parameters:a – Multi-index.
Returns:Multi-index.
Return type:MultiIndex
factorial(a)[source]

Factorial of a multi-index, \(a! = a_1! a_2! \ldots a_n!\).

Parameters:a – Multi-index.
Returns:Factorial of the multi-index.
general_to_exact_norm(a, r)[source]

Conversion of a multi-index from general to exact form.

Convert a general n-dimensional multi-index to an exact n+1-dimensional multi-index (exact meaning that the multi-index has norm r). Let \(a \in \mathbb{N}_0^n\). Then this function returns \(b \in \mathbb{N}_0^{n + 1}\) with \(b_1 = r - |a|\) and \(b_i = a_{i - 1}, i = 2, 3, \ldots, n + 1\).

Parameters:
  • a – Multi-index.
  • r (int) – Desired norm of exact multi-index.
Returns:

Multi-index with norm r.

Return type:

MultiIndex

Examples

>>> general_to_exact_norm((1, 2), 4).to_tuple()
(1, 1, 2)
>>> general_to_exact_norm((0, 0), 2).to_tuple()
(2, 0, 0)
generate(n, r, i)[source]

Generate the i:th multi-index in the sequence of all n-dimensional multi-indices with norm <= r.

There is a natural ordering of the multi-indices in the sense that a multi-index \(a\) of norm <= r can be identified with a natural number \(n(a)\) by \(n(a) = \sum_{k = 0}^{\dim \nu} a_k r^k\) (interpreting the indices of a as digits of a number in base r), and this number is strictly increasing with i.

Parameters:
  • n (int) – Dimension of multi-indices.
  • r (int) – Maximum norm of multi-indices.
  • i (int) – Which multi-index to generate. Need to be in the range [0, num_multiindices(n, r) - 1].
Returns:

The i:th multi-index.

Return type:

MultiIndex

generate_all(n, r)[source]

Generate the sequence of all n-dimensional multi-indices with norm <= r.

For ordering of the multi-indices see generate().

Parameters:
  • n (int) – Dimension of multi-indices.
  • r (int) – Maximum norm of multi-indices.
Returns:

List of all multi-indices.

Return type:

List[MultiIndex].

generate_all_exact_norm(n, r)[source]

Generate all n-dimensional multi-indices with norm r.

Parameters:
  • n (int) – Dimension of multi-indices.
  • r – Norm of each multi-index.
Returns:

List of all multi-indices with norm r.

Return type:

List[MultiIndex].

generate_all_increasing(n, r)[source]

Generate all increasing (see is_increasing()) n-dimensional multi-indices such that each component is less than or equal to r.

Parameters:
  • n (int) – Dimension of multi-indices.
  • r – Max value for each component of the multi-indices.
Returns:

List of increasing multi-indices.

Return type:

List[MultiIndex].

generate_all_multi_cap(r)[source]

Generate all n-dimensional multi-indices \(a\) such that \(a_i \leq r_i, i = 1, 2, \ldots, n\), where n is the length of r.

Parameters:r (Iterable[int]) – Maximum value for each entry of the multi-indices.
Returns:List of all multi-indices.
Return type:List[MultiIndex].
generate_all_non_decreasing(n, r)[source]

Generate all non-decreasing (see is_non_decreasing()) n-dimensional multi-indices such that each component is less than or equal to r.

Parameters:
  • n (int) – Dimension of multi-indices.
  • r – Max value for each component of the multi-indices.
Returns:

List of non-creasing multi-indices.

Return type:

List[MultiIndex].

generate_multi_cap(r, i)[source]

Generate the i:th multi-index among all n-dimensional multi-indices \(a\) such that \(a_i \leq r_i, i = 1, 2, \ldots, n\), where n is the length of r.

The ordering of the multi-indices is natural in the sense that each generated multi-index can be identified with a natural number expressed in the base \(\max_i r_i\), and this number is strictly increasing with i.

Parameters:
  • r (Iterable[int]) – Maximum value for each entry of the multi-indices.
  • i (int) – Which multi-index to generate. Need to be in the range [0, \((\Pi_i (r_i + 1)) - 1\)].
Returns:

The i:th multi-index.

Return type:

MultiIndex

get_index(mi, r)[source]

Get the index of a multi-index in the sequence of all multi-indices of the same dimension and with norm <= r (as given by generate_all()).

Parameters:
  • mi – Multi-index.
  • r (int) – Maximum norm of multi-indices.
Returns:

Index of multi-index.

Return type:

int

Raise:

ValueError if the given multi-index doesn’t belong to the sequence of multi-indices with the specified dimension and with norm <= r.

is_increasing(a)[source]

Check if the indices of a multi-index form an increasing sequence, i.e. \(a_i < a_j\) if \(i < j\).

Parameters:a – Multi-index.
Returns:Whether or not the indices of the multi-index are increasing.

Examples

>>> is_increasing((1, 2, 3))
True
>>> is_increasing((1, 1))
False
is_non_decreasing(a)[source]

Check if the indices of a multi-index form a non-decreasing sequence, i.e. \(a_i \leq a_j\) if \(i < j\).

Parameters:a – Multi-index.
Returns:Whether or not the indices of the multi-index are non-decreasing.

Examples

>>> is_non_decreasing((1, 2, 3))
True
>>> is_non_decreasing((1, 1))
True
>>> is_non_decreasing((1, 3, 2))
False
multinom(a)[source]

Multinomial coefficient of a multi-index.

Number of ways to put \(|a|\) elements in n boxes with \(a_i\) elements in box i (where n is the number of elements in \(a\)),

\[\binom{|a|}{a} = \frac{|a|!}{a!}.\]
Parameters:a – Multi-index.
Returns:Multinomial coefficient, \(\frac{|a|!}{a!}\).
multinom_general(r, a)[source]

Multinomial coefficient of a multi-index.

Number of ways to put \(r\) elements in n boxes with \(a_i\) elements in box i, \(i = 1, 2, \ldots, n - 1\) and \(r - |a|\) elements in box n (where n - 1 is the number of elements in \(a\)),

\[\binom{r}{a} = \frac{r!}{a!(r - |a|)!}.\]

This is equal to the multinomial coefficient of the multi-index a converted to exact form with norm r.

Parameters:
  • a – Multi-index.
  • r (int) – Total number of elements (or norm of the exact multi-index).
Returns:

Multinomial coefficient, \(\frac{r!}{a!(r - |a|)!}\).

norm(a)[source]

Absolute value of a multi-index, \(|a| = a_1 + a_2 + \ldots + a_n\).

Parameters:a – Multi-index.
Returns:Absolute value of the multi-index.
num_multiindices(n, r)[source]

Compute the number of n-dimensional multi-indices with norm <= r.

Parameters:
  • n (int) – Dimension of multi-indices.
  • r (int) – Maximum norm of multi-indices.
Returns:

Number of unique multi-indices.

Return type:

int

power(x, a)[source]

Raise a vector to the power of a multi-index, \(x^a = x_1^{a_1} x_2^{a_2} \ldots x_n^{a_n}\).

Parameters:
  • x – Iterable of same length as the multi-index a.
  • a – Multi-index.
Returns:

x raised to the power a.

random_multiindex(n, r)[source]

Generate a random multi-index from the set of all n-dimensional multi-indices with norm <= r, with uniform sampling.

Parameters:
  • n (int) – Dimension of multi-index.
  • r (int) – Maximum norm of multi-index.
Returns:

Random n-dimensional multi-index with norm <= r.

Return type:

MultiIndex

unit_multiindex(n, i)[source]

Generate the n-dimensional multi-index (element of \(\mathbb{N}_0^n\)) with all entries equal to zero except the i:th entry which is equal to 1.

Parameters:
  • n (int) – Dimension of the multi-index.
  • i (int) – Entry of the multi-index which should be equal to 1.
Returns:

The i:th n-dimensional unit multi-index.

Return type:

MultiIndex

Examples

>>> print(unit_multiindex(3, 0))
(1, 0, 0)
>>> print(unit_multiindex(2, 1))
(0, 1)
zero_multiindex(n)[source]

Generate the n-dimensional zero multi-index (element of \(\mathbb{N}_0^n\) with all entries equal to zero).

Parameters:n (int) – Dimension of the multi-index.
Returns:The n-dimensional zero multi-index.
Return type:MultiIndex

Examples

>>> print(zero_multiindex(2))
(0, 0)
polynomials_on_simplices.algebra.multiindex_order_cache module

Cached evaluation of index for multi-indices in the sequence of all multi-indices of given dimension and max norm.

polynomials_on_simplices.algebra.permutations module

Permutations module.

Operations on permutations (elements of \(S_n\)) and functions for permuting a sequence of objects. The set of permutations \(S_n\) is defined as the set of bijections from the set \(\{0, 1, \ldots, n-1\}\) (or \(\{1, 2, \ldots, n\}\)) onto itself, see https://en.wikipedia.org/wiki/Permutation and https://en.wikipedia.org/wiki/Permutation_group for an introduction.

There are two natural ways for defining how a permutation \(\sigma\) acts on a general sequence of elements \(x = (x_1, x_2, \ldots, x_n)\).

  1. The value \(x_i\) is mapped to the value \(x_{\sigma(i)}\),

    \[(x_1, \ldots, x_n) \mapsto \sigma(x) = (x_{\sigma(1)}, \ldots, x_{\sigma(n)}).\]
  2. The element at position i is mapped to the position \(\sigma(i)\), i.e. \(\sigma(x)_{\sigma(i)} = x_i \iff \sigma(x)_i = x_{\sigma^{-1}(i)} \iff x_i \mapsto x_{\sigma^{-1}(i)}\).

    \[(x_1, \ldots, x_n) \mapsto \sigma(x) = (x_{\sigma^{-1}(1)}, \ldots, x_{\sigma^{-1}(n)}).\]

As an example consider permutation of [‘a’, ‘b’, ‘c’, ‘d’] with the permutation

\[\begin{split}\begin{pmatrix} 1 & 2 & 3 & 4 \\ 3 & 2 & 4 & 1 \end{pmatrix}.\end{split}\]
  1. will then give [‘c’, ‘b’, ‘d’, ‘a’] (x[1] (‘a’) is mapped to (replaced by) x[s(1)] = x[3] (‘c’) and so on)
  2. will then give [‘d’, ‘b’, ‘a’, ‘c’] (x[1] (‘a’) is mapped to position s(1) = 3 and so on)

Obviously these two interpretations are the inverses of each other. In the code we refer to alternative 1 as permutation by value and alternative 2 as permutation by position.

Note

These two conventions for permuting a generic sequence behaves differently under composition. For alternative 1 we have

\[(\sigma \circ \pi)(x) = \pi(\sigma(x)),\]

whereas for alternative 2 we have

\[(\sigma \circ \pi)(x) = \sigma(\pi(x)).\]
class Permutation(*values)[source]

Bases: object

A permutation (element of \(S_n\)).

This class implements the group structure of the set of n-dimensional permutations.

Composition:

\[\circ : S_n \times S_n \to S_n, (\sigma \circ \pi)(x) = \sigma(\pi(x)).\]

Inverse:

\[(\cdot)^{-1} : S_n \to S_n, \sigma^{-1}(y) = x,\]

where \(x\) is the unique element that satisfies \(\sigma(x) = y\).

Examples

>>> Permutation(4, 3, 2, 1, 0) * Permutation(1, 3, 0, 2, 4) == Permutation(3, 1, 4, 2, 0)
True
>>> Permutation(1, 4, 3, 2, 0)**(-1) == Permutation(4, 0, 3, 2, 1)
True
Parameters:values (Iterable[int]) – Sequence of the elements \(\{0, 1, \ldots, n - 1 \}\) defining the values of the permutation (e.g. \(0 \mapsto \text{ values[0]}, 1 \mapsto \text{ values[1]}, \ldots\).
index(value)[source]

Return first index of value.

Raises ValueError if the value is not present.

to_tuple()[source]

Permutation converted to a tuple (one-line notation).

Returns:Tuple containing the permutation values.
Return type:Tuple[int]
circularly_equivalent(permutation1, permutation2)[source]

Check if two permutations are circularly equivalent.

Check whether or not two permutations belong to the same circular equivalence class (whether or not we can reach the second permutation by successively moving the last element of the first permutation to the front).

Parameters:
  • permutation1 – First permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • permutation2 – Second permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:

True/False whether or not the permutations belong to the same equivalence class.

Examples

>>> circularly_equivalent((0, 1, 2), (2, 0, 1))
True
composition(sigma, pi)[source]

Compute the composition of two permutations, \(\sigma \circ \pi, x \mapsto \sigma(\pi(x))\).

Parameters:
  • sigma – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • pi – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:

Composition of the two permutations, which again is a permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

construct_permutation(domain, codomain, n)[source]

Construct a permutation satisfying given constraints.

Construct a length n permutation which maps the given domain into the given codomain. Let \((x_0, x_1, \ldots, x_k)\) be the given domain and \((y_0, y_1, \ldots, y_k)\) be the given codomain. Then the output permutation \(\sigma\) should satisfy \(\sigma(x_i) = y_i, i = 0, 1, \ldots, k\).

Parameters:
  • domain – Domain for which the permutation is prescribed. Subset of the set {0, 1, …, n - 1}.
  • codomain – Image for each element in the prescribed domain. Subset of the set {0, 1, …, n - 1}. Need to be the same length as the domain.
  • n – Length of permutation.
Returns:

Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> construct_permutation([0, 1], [1, 0], 3)
(1, 0, 2)
construct_permutation_general(domain, codomain, by_value=True)[source]

Construct a permutation from two general sequences of (the same) elements.

Construct a length n permutation which maps the given domain into the given codomain. Let \(x = (x_0, x_1, \ldots, x_k)\) be the given domain and \(y = (y_0, y_1, \ldots, y_k)\) be the given codomain. Then the output permutation \(\sigma\) should satisfy \(\sigma(x_i) = y_i, i = 0, 1, \ldots, k\).

Parameters:
  • domain – Domain for which the permutation is prescribed.
  • codomain – Image for each element in the prescribed domain. Need to be the same length as the domain.
  • by_value – Whether to use the “by value” or “by position” interpretation of the permutation of an array of objects.
Returns:

Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> construct_permutation_general(['a', 'b', 'c', 'd'], ['d', 'b', 'a', 'c'])
(3, 1, 0, 2)
>>> construct_permutation_general(['a', 'b', 'c', 'd'], ['d', 'b', 'a', 'c'], by_value=False)
(2, 1, 3, 0)
cycle(permutation, start)[source]

Compute a cycle of a permutation.

Parameters:
  • permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • start – Permutation element to start with.
Returns:

Tuple of elements we pass until we cycle back to the start element.

Examples

>>> cycle((2, 3, 0, 1), 0)
(0, 2)
>>> cycle((2, 3, 0, 1), 1)
(1, 3)
cyclic_permutations(permutation)[source]

Get list of all cyclic permutations equivalent to a given permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:List of tuples containing all cyclic permutations of the input permutation.

Examples

>>> cyclic_permutations((0, 1, 2))
[(0, 1, 2), (2, 0, 1), (1, 2, 0)]
from_cycle_notation(permutation, n)[source]

Convert a permutation from cycle notation to one-line notation.

Parameters:
  • permutation – Permutation in cycle notation (list of tuples fo cycles in the permutation).
  • n – Length of the permutation (needed since length 1 cycles are omitted in the cycle notation).
Returns:

Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> from_cycle_notation([(0, 1, 4), (2, 3)], 6)
(1, 4, 3, 2, 0, 5)
from_one_based(permutation)[source]

Convert a permutation using one based elements to zero based elements.

In Python code it’s most natural to represent \(S_n\) by the numbers \(\{0, 1, \ldots, n-1\}\), while in mathematical literature it’s standard to use the numbers \(\{1, 2, \ldots, n\}\).

Parameters:permutation – One based permutation in one-line notation (length n tuple of the numbers 1, 2, …, n-1).
Returns:One based permutation in one line notation (length n tuple of the numbers 0, 1, …, n).

Examples

>>> from_one_based((1, 2, 3))
(0, 1, 2)
from_transpositions(transpositions, n)[source]

Convert a permutation from a composition of transpositions to one-line notation.

Parameters:
  • transpositions – Permutation as a composition of transpositions (length 2 cycles, applied from right to left).
  • n – Length of the permutation (needed since elements mapping to themselves are not represented in the composition of transpositions).
Returns:

Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> from_transpositions([(0, 2), (0, 1)], 3)
(2, 0, 1)
from_two_line_notation(permutation)[source]

Convert a permutation from two-line notation to one-line notation.

Parameters:permutation – Permutation in two line notations (two length n tuples of the numbers 0, 1, …, n-1, where the first lists the elements \(x_i\), and the second lists the image under the permutation \(\sigma(x_i)\).
Returns:Permutation expressed in one-line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> from_two_line_notation(((0, 2, 1, 3), (0, 2, 3, 1)))
(0, 3, 2, 1)
generate_increasing_subset_permutations(array, k)[source]

Generate all increasing subset permutations.

Generate all permutations of any k elements of an array, where the elements appear in increasing order, based on the element order in the input array.

Parameters:
  • array – Input list of elements.
  • k – Number of elements to pick from the array.
Returns:

Generator generating all permutations of subsets of k increasing elements from the input array.

generate_random_increasing_subset_permutation(n, k)[source]

Generate random increasing subset permutation.

Generate a random length k permutation of the elements 0, 1, …, n-1 where the elements appear in increasing order.

Parameters:
  • n – Number of elements to pick from.
  • k – Length of permutation.
Returns:

Permutation in one-line notation (length k tuple of increasing numbers from the set 0, 1, …, n-1).

generate_random_permutation(n)[source]

Generate a random length n permutation (element of \(S_n\)).

Parameters:n – Length of permutation.
Returns:Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
generate_random_subset_permutation(n, k)[source]

Generate a random length k permutation of the elements 0, 1, …, n-1.

Parameters:
  • n – Number of elements to pick from.
  • k – Length of permutation.
Returns:

Permutation in one-line notation (length k tuple of the numbers 0, 1, …, n-1).

generate_subset_permutations(array, k)[source]

Generate all permutations of any k elements of an array.

Parameters:
  • array – Input list of elements.
  • k – Number of elements to pick from the array.
Returns:

Generator generating all permutations of subsets of k elements from the input array.

identity(n)[source]

Get the identity permutation in \(S_n\).

Parameters:n – Length of the permutation.
Returns:Identity permutation.

Examples

>>> identity(3)
(0, 1, 2)
increasing_subset_permutations(n, k)[source]

Get increasing subset permutations.

Get a list of all permutations of any k elements of the elements 0, 1, …, n-1, where the elements appear in increasing order.

Parameters:
  • n – Number of total elements.
  • k – Number of elements to pick.
Returns:

List of tuples containing all permutations of subsets of k increasing elements from the elements 0, 1, …, n-1.

Examples

>>> increasing_subset_permutations(3, 2)
[(0, 1), (0, 2), (1, 2)]
inverse(permutation)[source]

Compute the inverse of a permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Inverse permutation in one line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> inverse((1, 4, 3, 2, 0))
(4, 0, 3, 2, 1)
num_fixed_points(permutation)[source]

Compute the number of fixed points (elements mapping to themselves) of a permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Number of fixed points in the permutation.

Examples

>>> num_fixed_points((0, 2, 1))
1
num_increasing_subset_permutations(n, k)[source]

Get the number of permutations of any k elements of the elements 0, 1, …, n - 1, where the elements appear in increasing order.

This is given by \(\binom{n}{k} = \frac{n!}{k! (n - k)!}\).

Parameters:
  • n – Number of total elements.
  • k – Number of elements to pick.
Returns:

Number of length k permutations of n elements.

Return type:

int

num_permutations(n)[source]

Get the number of length n permutations, n! (number of permutations of n elements = number of elements in \(S_n\)).

Parameters:n – Length of permutations.
Returns:Number of length n permutations.
Return type:int
num_subset_permutations(n, k)[source]

Get the number of permutations of any k elements of the elements 0, 1, …, n - 1.

This is given by \(\frac{n!}{(n - k)!}\)

Parameters:
  • n – Number of total elements.
  • k – Number of elements to pick.
Returns:

Number of length k permutations of n elements.

Return type:

int

permutation_matrix_positions(permutation)[source]

Compute permutation matrix for permutation by position.

Compute the permutation matrix which when multiplied with a column vector permutes the position of the elements in the vector according to the given permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:n by n permutation matrix (orthogonal matrix containing a single 1 in each row and column).
Return type:Numpy array

Examples

>>> permutation_matrix_positions((0, 3, 1, 4, 2))
array([[1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0]])
permutation_matrix_values(permutation)[source]

Compute permutation matrix for permutation by value.

Compute the permutation matrix which when multiplied with a column vector permutes the values of the vector according to the given permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:n by n permutation matrix (orthogonal matrix containing a single 1 in each row and column).
Return type:Numpy array

Examples

>>> permutation_matrix_values((0, 3, 1, 4, 2))
array([[1, 0, 0, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 1, 0, 0]])
permutations(n)[source]

Get a list of all permutations of length n (elements of \(S_n\)).

Parameters:n – Length of permutations.
Returns:List of tuples containing all permutations of the elements 0, 1, …, n-1.

Examples

>>> permutations(3)
[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
permute_positions(permutation, sequence)[source]

Permute the elements of a sequence with a specified permutation.

The permutation specifies what position each element in the input sequence is mapped to in the permuted sequence. Mathematically we have: \(\sigma((x_0, x_1, \ldots, x_n)) = (x_{\sigma^{-1}(0)}, x_{\sigma^{-1}(1)}, \ldots, x_{\sigma^{-1}(n-1)})\).

Parameters:
  • permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • sequence – Sequence of elements we wish to permute.
Returns:

The permuted sequence.

Examples

>>> permute_positions((2, 0, 1), ['a', 'b', 'c'])
['b', 'c', 'a']
permute_values(permutation, sequence)[source]

Permute the elements of a sequence with a specified permutation.

The permutation specifies how each element of the input sequence maps into another element of the input sequence. Mathematically we have: \(\sigma((x_0, x_1, \ldots, x_{n-1})) = (x_{\sigma(0)}, x_{\sigma(1)}, \ldots, x_{\sigma(n-1)})\).

Parameters:
  • permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • sequence – Sequence of elements we wish to permute.
Returns:

The permuted sequence.

Examples

>>> permute_values((2, 0, 1), ['a', 'b', 'c'])
['c', 'a', 'b']
sign(permutation)[source]

Compute the sign of a permutation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Sign of the permutation.

Examples

>>> sign((0, 1, 2))
1
>>> sign((0, 2, 1))
-1
subset_permutations(n, k)[source]

Get a list of all permutations of any k elements of the elements 0, 1, …, n-1.

Parameters:
  • n – Number of total elements.
  • k – Number of elements to pick.
Returns:

List of tuples containing all permutations of subsets of k elements from the elements 0, 1, …, n-1.

Examples

>>> subset_permutations(3, 2)
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
swap(permutation, transposition)[source]

Swap image of two elements in a permutation (apply a transposition to the permutation).

Let \(\sigma\) be the input permutation, \(i, j\) be the elements of the transposition, and \(\pi\) be the output permutation after the applied transposition. Then we have \(\pi(i) = \sigma(j), \pi(j) = \sigma(i), \pi(k) = \sigma(k), k \neq i, j\).

Parameters:
  • permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
  • transposition – Tuple of two elements that should be switched in the permutation.
Returns:

Permutation after the applied transposition in one line notation (length n tuple of the numbers 0, 1, …, n-1).

Examples

>>> swap((0, 1, 2, 3), (1, 3))
(0, 3, 2, 1)
>>> swap((0, 3, 2, 1), (2, 3))
(0, 3, 1, 2)
to_cycle_notation(permutation)[source]

Convert a permutation from one-line notation to cycle notation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Permutation in cycle notation (list of tuples of cycles in the permutation).

Examples

>>> to_cycle_notation((1, 4, 3, 2, 0))
[(0, 1, 4), (2, 3)]
to_one_based(permutation)[source]

Convert a permutation using zero based elements to one based elements.

In Python code it’s most natural to represent \(S_n\) by the numbers \(\{0, 1, \ldots, n-1\}\), while in mathematical literature it’s standard to use the numbers \(\{1, 2, \ldots, n\}\).

Parameters:permutation – Zero based permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:One based permutation in one line notation (length n tuple of the numbers 1, 2, …, n).

Examples

>>> to_one_based((0, 1, 2))
(1, 2, 3)
to_transpositions(permutation)[source]

Convert a permutation from one-line notation to a composition of transpositions.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Permutation as a composition of transpositions (length 2 cycles, applied from right to left).

Examples

>>> to_transpositions((2, 0, 1))
[(0, 2), (0, 1)]
to_two_line_notation(permutation)[source]

Convert a permutation from one-line notation to two line notation.

Parameters:permutation – Permutation in one-line notation (length n tuple of the numbers 0, 1, …, n-1).
Returns:Permutation expressed in two line notations (two length n tuples of the numbers 0, 1, …, n-1, where the first lists the elements \(x_i\), and the second lists the image under the permutation \(\sigma(x_i)\).

Examples

>>> to_two_line_notation((0, 3, 2, 1))
((0, 1, 2, 3), (0, 3, 2, 1))
Module contents
polynomials_on_simplices.calculus package
Submodules
polynomials_on_simplices.calculus.affine_map module

Functionality for dealing with affine maps \(\Phi : \mathbb{R}^m \to \mathbb{R}^n\) with

\[\Phi(x) = Ax + b,\]

where \(A \in \mathbb{R}^{n \times m}, b \in \mathbb{R}^n\).

affine_composition(phi1, phi2)[source]

Compute the matrix and vector defining the composition of two affine maps.

\(\Phi_1(x) = A_1x + b_1, \Phi_2(x) = A_2x + b_2, \Phi(x) = (\Phi_2 \circ \Phi_1)(x) = Ax + b\). \(A = A_2 A_1, b = A_2 b_1 + b_2\).

Parameters:
  • phi1 – Tuple of A matrix and b vector for the first affine transformation.
  • phi2 – Tuple of A matrix and b vector for the second affine transformation.
Returns:

Tuple of A and b.

create_affine_map(a, b, multiple_arguments=False)[source]

Generate the affine map \(\Phi : \mathbb{R}^m \to \mathbb{R}^n, \Phi(x) = Ax + b\) from a matrix A and a vector b (or scalars a and b in the case m = n = 1).

Parameters:
  • a – Matrix or scalar defining the linear part of the affine map.
  • b – Vector or scalar defining the translation part of the affine map.
  • multiple_arguments (bool) – For a multivariate affine map, this argument determines if the generated map should take m scalar arguments or 1 m-dimensional vector as argument. For example with a 2-dimensional domain the generated map could have the signature \(\Phi([x, y])\) (if multiple_arguments is False) or \(\Phi(x, y)\) (if multiple_arguments is True).
Returns:

Map \(\Phi\) which takes an m-dimensional vector as input and returns an n-dimensional vector (or scalar input and output for m = n = 1).

Return type:

Callable \(\Phi(x)\).

inverse_affine_transformation(a, b)[source]

Generate the matrix and vector defining the inverse of a given affine map \(\Phi : \mathbb{R}^n \to \mathbb{R}^n\),

\[\Phi(x) = Ax + b.\]

The inverse of the affine map \(\Phi(x)\) is given by \(\Phi^{-1}(x) = A^{-1}x - A^{-1}b\). This function returns the matrix \(A^{-1}\) and vector \(-A^{-1}b\) defining the inverse map (or scalars 1 / A and -b / A for scalar input A, b).

Parameters:
  • a – Matrix or scalar defining the linear part of the affine map we want to invert.
  • b – Vector or scalar defining the translation part of the affine map we want to invert.
Returns:

Tuple of A and b.

pseudoinverse_affine_transformation(a, b)[source]

Generate the matrix and vector defining the pseudoinverse of a given affine map.

The inverse of an affine map \(\Phi(x) = Ax + b\) is given by \(\Phi^{-1}(x) = A^{-1}x - A^{-1}b\). Assuming the matrix A is not invertible, but \(A^T A\) is, so that it has pseudoinverse \(A^+ = (A^T A)^{-1} A^T\). Then the affine map \(\Phi^+(x) = A^+x - A^+b\) satisfies \(\Phi^+(\Phi(x)) = x\). This function returns the matrix \(A^+\) and vector \(-A^+b\) defining this pseudoinverse map.

Parameters:
  • a – Matrix or scalar defining the linear part of the affine map we want to invert.
  • b – Vector or scalar defining the translation part of the affine map we want to invert.
Returns:

Tuple of A and b.

polynomials_on_simplices.calculus.angle module

Functionality related to angles and for computing angles between vectors.

class Dial[source]

Bases: object

A dial pointing at a position on a circle, while remembering the total number of laps it has turned around.

get_total_position()[source]

Get position the dial points at, including full revolutions.

Returns:Dial position in radians.
Return type:float
update_position(new_angle_pos)[source]

Set the new dial position, adjusting the number of laps if the dial crosses the discontinuity at \(\pm\pi\).

Parameters:new_angle_pos – New angle position the dial points at in the range \([-\pi, \pi)\).
compute_angle(v0, v1)[source]

Compute the angle between two vectors.

Parameters:
  • v0 – First vector.
  • v1 – Second vector.
Returns:

Angle between the two vectors (in \([0, \pi]\)).

degrees_to_radians(angle)[source]

Convert an angle from degrees to radians.

Parameters:angle – Angle in degrees.
Returns:Corresponding angle in radians.
Return type:float
direction(a0, a1)[source]

Compute the direction to go from a0 to a1.

Parameters:
  • a0 – First angle (in the range \([-\pi, \pi)\)).
  • a1 – Second angle (in the range \([-\pi, \pi)\)).
Returns:

1 if the shortest path from a0 to a1 goes anti-clockwise around the unit circle. -1 otherwise.

distance(a0, a1)[source]

Compute the distance between two angles (minimum distance along the unit circle).

Parameters:
  • a0 – First angle (in the range \([-\pi, \pi)\)).
  • a1 – Second angle (in the range \([-\pi, \pi)\)).
Returns:

Distance between the angles.

is_parallel(v0, v1)[source]

Check if two vectors are parallel.

Parameters:
  • v0 – First vector.
  • v1 – Second vector.
Returns:

True/False whether or not the two vectors are (approximately) parallel.

orthogonal_vector(v)[source]

Compute a vector which is orthogonal to the input vector \(v\).

Note

The returned orthogonal vector \(v^{\perp}\) satisfies \(\| v^{\perp} \| \geq \frac{2}{n} \| v \|\), where \(n\) is the dimension of the input vector \(v\).

Parameters:v – Non-zero n-dimensional vector.
Returns:n-dimensional vector which is orthogonal to the input vector.
Return type:Numpy array
orthonormal_frame(v, i=0)[source]

Compute an orthonormal frame R in 3d such that the i:th (i in {0, 1, 2}) column is parallel to the given vector v (\(R e_i = v, R^T R = R R^T = I\)). The two other columns could then be used as an orthonormal basis for the plane with normal v and which passes through the origin.

This is the unique rotation matrix R which rotates \(e_i\) to \(v\) without twist, i.e. vectors parallel to \(e_x \times v\) are kept fixed.

Parameters:
  • v – Non-zero 3d vector from which we compute the orthonormal frame (point where the derivative is evaluated).
  • i (int) – Column of the orthonormal frame which should be parallel to the input vector (i in {0, 1, 2}).
Returns:

Orthonormal frame (3 by 3 orthogonal matrix).

Return type:

Numpy array

radians_to_degrees(angle)[source]

Convert an angle from radians to degrees.

Parameters:angle – Angle in radians.
Returns:Corresponding angle in degrees.
Return type:float
to_centered_angle_interval(angle)[source]

Return the equivalent angle in the interval \([-\pi, \pi)\).

Parameters:angle – Angle in the range \((-\infty, \infty)\).
Returns:Equivalent angle in the range \([-\pi, \pi)\).
Return type:float
to_positive_angle_interval(angle)[source]

Return the equivalent angle in the interval \([0, 2 \pi)\).

Parameters:angle – Angle in the range \((-\infty, \infty)\).
Returns:Equivalent angle in the range \([0, 2 \pi)\).
Return type:float
polynomials_on_simplices.calculus.error_measures module

Functionality for computing the error between exact and approximate values.

absolute_error(x0, x)[source]

Compute absolute error between a value x and its expected value x0.

Parameters:
  • x0 – Expected value.
  • x – Actual value.
Returns:

Absolute error between the actual and expected value.

Return type:

float

relative_error(x0, x, zero_tol=1e-05)[source]

Compute relative error between a value x and its expected value x0.

Parameters:
  • x0 – Expected value.
  • x – Actual value.
  • zero_tol – If x0 is smaller than this value, the absolute error is returned instead.
Returns:

Relative error between the actual and expected value.

Return type:

float

Examples

>>> abs(relative_error(0.1, 0.4) - 3) < 1e-10
True
>>> abs(relative_error(0.4, 0.1) - 0.75) < 1e-10
True

For small values the absolute error is used >>> abs(relative_error(1e-6, 1e-6 + 1e-12) - 1e-12) < 1e-20 True

relative_error_symmetric(x1, x2, zero_tol=1e-05)[source]

Compute relative error between two values x1 and x2.

Note

The relative_error() function is not symmetric, i.e. in general relative_error(a, b) != relative_error(b, a), which makes sense when comparing to a reference value. However when just checking the error between two values (without one of them being more correct than the other) it makes more sense with a symmetric error, which is what this function returns.

\[\varepsilon = \frac{|x_1 - x_2|}{\max(|x_1|, |x_2|)}.\]
Parameters:
  • x1 – First value.
  • x2 – Second value.
  • zero_tol – If max(abs(x1), abs(x2)) is smaller than this value, the absolute error is returned instead.
Returns:

Relative error between the two values.

Return type:

float

Examples

>>> relative_error_symmetric(0.1, 0.2)
0.5
>>> relative_error_symmetric(0.1, 0.2) == relative_error_symmetric(0.2, 0.1)
True
polynomials_on_simplices.calculus.finite_difference module

Functions used to compute derivatives using finite difference.

central_difference(f, x, h=1e-08)[source]

Compute numerical gradient of the scalar valued function f using central finite difference.

\[f : \mathbb{R}^n \to \mathbb{R},\]
\[\nabla f(x)_i = \frac{\partial f(x)}{\partial x^i},\]
\[\nabla f_{\delta}(x)_i = \frac{f(x + \frac{h}{2}e_i) - f(x - \frac{h}{2}e_i)}{h}.\]
Parameters:
  • f (Callable f(x)) – Scalar valued function.
  • x (float or Iterable[float]) – Point where the gradient should be evaluated.
  • h (float) – Step size in the central difference method.
Returns:

Approximate gradient of f.

Return type:

float or length n Numpy array.

central_difference_jacobian(f, n, x, h=1e-08)[source]

Compute numerical jacobian of the vector valued function f using central finite difference.

\[f : \mathbb{R}^m \to \mathbb{R}^n,\]
\[J_f (x)^i_j = \frac{\partial f(x)^i}{\partial x^j},\]
\[J_{f, \delta}(x, h)^i_j = \frac{f(x + \frac{h}{2}e_j)^i - f(x - \frac{h}{2}e_j)^i}{h},\]

with \(i = 1, 2, \ldots, n, j = 1, 2, \ldots, m\).

Parameters:
  • f (Callable f(x)) – Vector valued function.
  • n (int) – Dimension of the target of f.
  • x (float or Iterable[float]) – Point where the jacobian should be evaluated.
  • h (float) – Step size in the finite difference method.
Returns:

Approximate jacobian of f.

Return type:

n by m Numpy matrix.

discrete_forward_difference(f0, x0, f1, x1)[source]

Compute numerical derivative of a scalar valued, univariate function f using two discrete point evaluations.

Parameters:
  • f0 (float) – Function value at x0.
  • x0 (float) – First point where the function has been evaluated.
  • f1 (float) – Function value at x1.
  • x1 (float) – Second point where the function has been evaluated.
Returns:

Numerical approximation of the derivative.

Return type:

float

forward_difference(f, x, h=1e-08)[source]

Compute numerical gradient of the scalar valued function f using forward finite difference.

\[f : \mathbb{R}^n \to \mathbb{R},\]
\[\nabla f(x)_i = \frac{\partial f(x)}{\partial x^i},\]
\[\nabla f_{\Delta}(x)_i = \frac{f(x + he_i) - f(x)}{h}.\]
Parameters:
  • f (Callable f(x)) – Scalar valued function.
  • x (float or Iterable[float]) – Point where the gradient should be evaluated.
  • h (float) – Step size in the finite difference method.
Returns:

Approximate gradient of f.

Return type:

float or length n Numpy array.

forward_difference_jacobian(f, n, x, h=1e-08)[source]

Compute numerical jacobian of the vector valued function f using forward finite difference.

\[f : \mathbb{R}^m \to \mathbb{R}^n,\]
\[J_f (x)^i_j = \frac{\partial f(x)^i}{\partial x^j},\]
\[J_{f, \Delta}(x, h)^i_j = \frac{f(x + he_j)^i - f(x)^i}{h},\]

with \(i = 1, 2, \ldots, n, j = 1, 2, \ldots, m\).

Parameters:
  • f (Callable f(x)) – Vector valued function.
  • n (int) – Dimension of the target of f.
  • x (float or Iterable[float]) – Point where the jacobian should be evaluated.
  • h (float) – Step size in the finite difference method.
Returns:

Approximate jacobian of f.

Return type:

n by m Numpy matrix.

second_central_difference(f, x, h=2e-05)[source]

Compute the numerical Hessian of the scalar valued function f using second order central finite difference.

\[f : \mathbb{R}^n \to \mathbb{R},\]
\[H_f(x)_{ij} = \frac{\partial^2 f(x)}{\partial x^i \partial x^j},\]
\[ \begin{align}\begin{aligned}H_{f, \delta}(x)_{ij} = \bigg[ &f(x + \frac{h}{2} (e_i + e_j)) - f(x + \frac{h}{2} (e_i - e_j))\\&- f(x + \frac{h}{2} (-e_i + e_j)) + f(x + \frac{h}{2} (-e_i - e_j)) \bigg] / h^2.\end{aligned}\end{align} \]
Parameters:
  • f (Callable f(x)) – Scalar valued function.
  • x (float or Iterable[float]) – Point where the Hessian should be evaluated.
  • h (float) – Step size in the finite difference method.
Returns:

Hessian (full matrix, i.e., not utilizing the symmetry or any sparsity structure of the Hessian).

Return type:

float or n by n Numpy matrix.

second_forward_difference(f, x, h=1e-05)[source]

Compute the numerical Hessian of the scalar valued function f using second order forward finite difference.

\[f : \mathbb{R}^n \to \mathbb{R},\]
\[H_f(x)_{ij} = \frac{\partial^2 f(x)}{\partial x^i \partial x^j},\]
\[H_{f, \Delta}(x)_{ij} = \frac{f(x + h (e_i + e_j)) - f(x + h e_i) - f(x + h e_j) + f(x)}{h^2}.\]
Parameters:
  • f (Callable f(x)) – Scalar valued function.
  • x (float or Iterable[float]) – Point where the Hessian should be evaluated.
  • h (float) – Step size in the finite difference method.
Returns:

Hessian (full matrix, i.e., not utilizing the symmetry or any sparsity structure of the Hessian).

Return type:

float or n by n Numpy matrix.

polynomials_on_simplices.calculus.plot_function module

Plotting of analytic expressions (in the form of callables).

evaluate_curve(gamma, t)[source]

Evaluate a parametric curve in 2d (\(\gamma(t) = (\gamma_1(t), \gamma_2(t))\)) or 3d (\(\gamma(t) = (\gamma_1(t), \gamma_2(t), \gamma_3(t))\)) at a number of locations.

Parameters:
  • gamma (Callable \(\gamma(t)\)) – Parametric curve (vector valued (2d or 3d) univariate function).
  • t – Positions where the curve should be evaluated (array of floats).
Returns:

Array of curve values, one for each location in the input t array.

plot_bivariate_function(f, tri_vertices, *args, **kwargs)[source]

Plot a bivariate function on a triangular domain.

Parameters:
  • f (Callable f(x, y)) – Bivariate function to plot (bivariate real-valued function).
  • tri_vertices – Vertices of the triangular domain (3 by 2 matrix where each row is a vertex in the triangle).
  • args – Additional arguments passed to the plot_triangle_mesh() command.
  • kwargs – ‘edge_resolution’: Number of discrete points in the plot along each edge in the triangle mesh. Further keyword arguments are passed to the plot_triangle_mesh() command.
plot_curve(gamma, t_start=0.0, t_stop=1.0, *args, **kwargs)[source]

Plot a parametric curve in 2d (\(\gamma(t) = (\gamma_1(t), \gamma_2(t))\)) or 3d (\(\gamma(t) = (\gamma_1(t), \gamma_2(t), \gamma_3(t))\)).

Parameters:
  • gamma (Callable \(\gamma(t)\)) – Parametric curve (vector valued (2d or 3d) univariate function).
  • t_start (float) – Beginning of range to plot for the curve parameter t.
  • t_stop (float) – End of range to plot for the curve parameter t.
  • args – Additional arguments passed to the plot_curve() command.
  • kwargs – Keyword arguments. ‘num’: Number of discrete points in the plot. Further keyword arguments are passed to the plot_curve() command.
plot_function(f, x_start=0.0, x_stop=1.0, *args, **kwargs)[source]

Plot a univariate function.

Parameters:
  • f (Callable f(x)) – Function expression to plot (univariate real-valued function).
  • x_start (float) – Beginning of x-range to plot.
  • x_stop (float) – End of x-range to plot.
  • args – Additional arguments passed to the plot_curve() command.
  • kwargs – Keyword arguments passed to the plot_curve() command.
polynomials_on_simplices.calculus.quadrature module

Numerical evaluation of integrals over triangles (or simplices).

For triangle quadrature rules, see D.A. Dunavant, High degree efficient symmetrical Gaussian quadrature rules for the triangle. For tetrahedron quadrature rules, see Yu Jinyun Symmetric Gaussian Quadrature Formulae for Tetrahedronal Regions, and the PHG FEM software: http://lsec.cc.ac.cn/phg/index_en.htm.

gauss_legendre_unit_interval(n, n_digits=10)[source]

Compute quadrature points and weights for Gauss-Legendre quadrature modified from the interval [-1, 1] to the unit interval.

\[\int_0^1 f(x)\,dx \approx \sum_{i=0}^{n-1} w_i f(x_i).\]
Parameters:
  • n – The order of quadrature (number of points and weights).
  • n_digits – Number of significant digits of the points and weights to return.
Returns:

Tuple (x, w) of lists containing the points x and the weights w.

quadrature_interval(f, a, b)[source]

Numerically compute the integral \(\int_a^b f(x) \, dx\).

Parameters:
  • f (Callable f(x)) – Function to integrate.
  • a – Start point of the interval over which we should integrate f.
  • b – End point of the interval over which we should integrate f.
Returns:

Approximate value of the integral.

quadrature_interval_fixed(f, a, b, n)[source]

Numerically compute the integral \(\int_a^b f(x) \, dx\).

Parameters:
  • f (Callable f(x)) – Function to integrate.
  • a – Start point of the interval over which we should integrate f.
  • b – End point of the interval over which we should integrate f.
  • n – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= n.
Returns:

Approximate value of the integral.

quadrature_tetrahedron(f, vertices)[source]

Numerically compute the integral \(\int_{T} f(x) \, dx\), for a given tetrahedron T.

Parameters:
  • f (Callable f(x, y, z)) – Function to integrate.
  • vertices – Vertices of the tetrahedron (4 by 3 array).
Returns:

Approximate value of the integral.

quadrature_tetrahedron_fixed(f, vertices, n)[source]

Numerically compute the integral \(\int_{T} f(x) \, dx\), for a given tetrahedron T.

Parameters:
  • f (Callable f(x, y, z)) – Function to integrate.
  • vertices – Vertices of the tetrahedron (4 by 3 array).
  • n – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= n.
Returns:

Approximate value of the integral.

quadrature_tetrahedron_midpoint_rule(f, vertices, n)[source]

Numerically compute the integral \(\int_T f \, dx\) using the midpoint rule.

Note

Only useful for testing, since quadrature_tetrahedron() and quadrature_tetrahedron_fixed() are much more efficient.

Parameters:
  • f (Callable f(x, y, z)) – Function to integrate.
  • vertices – Vertices of the tetrahedron to integrate over (4 by 3 array).
  • n – Number of smaller tetrahedrons along each edge of the full tetrahedron, over which the function is approximated with a constant value.
Returns:

Approximate value of the integral.

quadrature_triangle(f, vertices)[source]

Numerically compute the integral \(\int_{T} f(x) \, dx\), for a given triangle T.

Parameters:
  • f (Callable f(x, y)) – Function to integrate.
  • vertices – Vertices of the triangle (3 by 2 array).
Returns:

Approximate value of the integral.

quadrature_triangle_fixed(f, vertices, n)[source]

Numerically compute the integral \(\int_{T} f(x) \, dx\), for a given triangle T.

Parameters:
  • f (Callable f(x, y)) – Function to integrate.
  • vertices – Vertices of the triangle (3 by 2 array).
  • n – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= n.
Returns:

Approximate value of the integral.

quadrature_triangle_midpoint_rule(f, vertices, n)[source]

Numerically compute the integral \(\int_T f \, dx\) using the midpoint rule.

Note

Only useful for testing, since quadrature_triangle() and quadrature_triangle_fixed() are much more efficient.

Parameters:
  • f (Callable f(x, y)) – Function to integrate.
  • vertices – Vertices of the triangle to integrate over (3 by 2 array).
  • n – Number of smaller triangles along each edge of the full triangle, over which the function is approximated with a constant value.
Returns:

Approximate value of the integral.

quadrature_unit_interval(f)[source]

Numerically compute the integral \(\int_0^1 f(x) \, dx\).

Parameters:f (Callable f(x)) – Function to integrate.
Returns:Approximate value of the integral.
quadrature_unit_interval_fixed(f, r)[source]

Numerically compute the integral \(\int_0^1 f(x) \, dx\).

Parameters:
  • f (Callable f(x)) – Function to integrate.
  • r – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= r.
Returns:

Approximate value of the integral.

quadrature_unit_tetrahedron(f)[source]

Numerically compute the integral \(\int_{\Delta^3_c} f(x) \, dx\).

Parameters:f (Callable f(x, y, z)) – Function to integrate.
Returns:Approximate value of the integral.
quadrature_unit_tetrahedron_fixed(f, n)[source]

Numerically compute the integral \(\int_{\Delta^3_c} f(x) \, dx\).

Parameters:
  • f (Callable f(x, y, z)) – Function to integrate.
  • n – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= n.
Returns:

Approximate value of the integral.

quadrature_unit_triangle(f)[source]

Numerically compute the integral \(\int_{\Delta^2_c} f(x) \, dx\).

Parameters:f (Callable f(x, y)) – Function to integrate.
Returns:Approximate value of the integral.
quadrature_unit_triangle_fixed(f, n)[source]

Numerically compute the integral \(\int_{\Delta^2_c} f(x) \, dx\).

Parameters:
  • f (Callable f(x, y)) – Function to integrate.
  • n – Quadrature degree. The result is guaranteed to be exact for all polynomials of degree <= n.
Returns:

Approximate value of the integral.

polynomials_on_simplices.calculus.real_interval module

Functionality for working with intervals on the real line.

constrain_to_range(s, min_val, max_val)[source]

Make sure that a value lies in the given (closed) range.

Parameters:
  • s – Value to check.
  • min_val – Lower boundary of the interval.
  • max_val – Upper boundary of the interval.
Returns:

Point closest to the input value which lies in the given range.

Return type:

float

equivalent_periodic_element(a, t)[source]

Get the equivalent scalar in [0, T) for an input scalar a, where two scalars are equivalent if they differ by an integer translation of T.

I.e. consider the equivalence relation \(a ~ b \leftrightarrow b = a + n \cdot T, n \in \mathbb{Z}\), then given a this function returns the unique element b in [0, T) such that a ~ b.

Parameters:
  • a (float) – Input scalar in \((-\infty, \infty)\).
  • t (float) – Period T.
Returns:

Equivalent element b in [0, T).

Return type:

float

in_closed_range(s, min_val, max_val)[source]

Check if a value lies in the closed interval [min_val, max_val].

Parameters:
  • s – Value to check.
  • min_val – Lower boundary of the interval.
  • max_val – Upper boundary of the interval.
Returns:

True/False whether or not the value lies in the closed interval.

Return type:

bool

in_left_closed_range(s, min_val, max_val)[source]

Check if a value lies in the left-closed interval [min_val, max_val).

Parameters:
  • s – Value to check.
  • min_val – Lower boundary of the interval.
  • max_val – Upper boundary of the interval.
Returns:

True/False whether or not the value lies in the left-closed interval.

Return type:

bool

in_open_range(s, min_val, max_val)[source]

Check if a value lies in the open interval (min_val, max_val).

Parameters:
  • s – Value to check.
  • min_val – Lower boundary of the interval.
  • max_val – Upper boundary of the interval.
Returns:

True/False whether or not the value lies in the open interval.

Return type:

bool

in_right_closed_range(s, min_val, max_val)[source]

Check if a value lies in the right-closed interval (min_val, max_val].

Parameters:
  • s – Value to check.
  • min_val – Lower boundary of the interval.
  • max_val – Upper boundary of the interval.
Returns:

True/False whether or not the value lies in the right-closed interval.

Return type:

bool

Module contents
polynomials_on_simplices.generic_tools package
Submodules
polynomials_on_simplices.generic_tools.code_generation_utils module

Utilities for generating code.

class CodeWriter[source]

Bases: object

Class used to generate formatted text, i.e. text with control of indentation and scope. Useful for generating code.

al(string)[source]

Append code to the current line.

bl(string)[source]

Begin a line of code.

count_num_lines()[source]

Count number of lines in the code.

Returns:Number of lines in the code.
dec_indent()[source]

Decrease indent depth.

el(string)[source]

Append code to the current line and end line.

empty()[source]

Check if anything has been written.

get_scope()[source]

Get current scope.

inc_indent()[source]

Increase indent depth.

pop_first_line()[source]

Pop and return first line from code.

pop_scope()[source]

Pop scope.

print_trace(message='')[source]

Insert debug information into code. Useful for tracing the origin of the code.

push_scope(scope_name)[source]

Push scope.

reset()[source]

Reset code writer.

verbatim(string)[source]

Write code verbatim.

wc(string)[source]

Write several lines of code.

wl(string)[source]

Write a line of code.

ws()[source]

Output current scope.

polynomials_on_simplices.generic_tools.package_utils module

Extensions to the standard library pkgutil module.

source_file_paths(package_path, absolute_path=True, exclude_test_files=False, include_hidden_files=False, include_private_files=False)[source]

Yield the path of each Python source file (files ending with ‘.py’) in a package.

Parameters:
  • package_path (str) – Path to the package we want to investigate.
  • package_path – Path to the package we want to investigate.
  • absolute_path (bool) – Whether or not yielded paths should be absolute or relative to the package path.
  • exclude_test_files (bool) – Whether or not to exclude unit test files. A file is considered a test file if it ends with ‘_test.py’ or ‘_test_interactive.py’ and resides in a folder called ‘test’.
  • include_hidden_files (bool) – Whether or not to include hidden source files in the package (files starting with ‘.’ or residing in a directory starting with ‘.’).
  • include_private_files (bool) – Whether or not to include private source files in the package (files starting with ‘_’).
Returns:

Path to each source file in the package.

Return type:

Iterator[str]

submodule_paths(package_path, absolute_path=True)[source]

Yield the path of each submodule in a package.

Parameters:
  • package_path (str) – Path to the package we want to investigate.
  • absolute_path (bool) – Whether or not yielded paths should be absolute or relative to the package path.
Returns:

Path to each submodule in the package.

Return type:

Iterator[str]

submodules(package_path)[source]

Yield each submodule in a package.

Parameters:package_path (str) – Path to the package we want to investigate.
Returns:Relative import path to each submodule in the package.
Return type:Iterator[str]
subpackage_paths(package_path, absolute_path=True)[source]

Yield the path of each subpackage in a package.

Parameters:
  • package_path (str) – Path to the package we want to investigate.
  • absolute_path (bool) – Whether or not yielded paths should be absolute or relative to the package path.
Returns:

Path to each subpackage in the package.

Return type:

Iterator[str]

polynomials_on_simplices.generic_tools.str_utils module

Utility functions for generating math-related strings.

convert_float_to_fraction(s, latex_fraction=False)[source]

Convert a float in string format to a fraction.

Parameters:
  • s (str) – Float value as a string.
  • latex_fraction (bool) – Whether or not to encode fractions using the Latex “frac” syntax. Default is to just use ‘/’ for the fraction.
Returns:

Fraction string.

Examples

>>> convert_float_to_fraction('0.5')
'1 / 2'
>>> convert_float_to_fraction('0')
'0'
>>> convert_float_to_fraction('0.5', latex_fraction=True)
'\\frac{1}{2}'
>>> convert_float_to_fraction('0', latex_fraction=True)
'0'
>>> convert_float_to_fraction('-0.5', latex_fraction=True)
'-\\frac{1}{2}'
>>> convert_float_to_fraction('2', latex_fraction=True)
'2'
is_nice_fraction(f, n=10)[source]

Check if a scalar can be expressed as a ‘nice’ rational number. Nice in this sense means that it can be written as a fraction with a denominator less than a given number n.

Parameters:
  • f (float) – Scalar value we want to check.
  • n (int) – How large the denominator can be in the fraction for the fraction to be considered nice.
Returns:

Whether or not the scalar is a ‘nice’ fraction.

Return type:

bool

Examples

>>> is_nice_fraction(0.1)
True
>>> is_nice_fraction(0.01)
False
>>> is_nice_fraction(0.01, 100)
True
split_long_str(string, sep=' ', long_str_threshold=-1, sep_replacement_before='', sep_replacement_after='')[source]

Split a long string into an array of parts.

The string will be split at occurrences of the given separator in a way that makes sure that each part is shorter than the given threshold length.

Parameters:
  • string (str) – String we want to split.
  • sep (str) – The delimiter according which to split the string.
  • long_str_threshold (int) – Don’t split at a delimiter if the resulting parts will be shorter than this threshold. The default value -1 indicates that we should always split at a delimiter.
  • sep_replacement_before (str) – When splitting at a separator, the separator will be replaced with this string in the part before the split.
  • sep_replacement_after (str) – When splitting at a separator, the separator will be replaced with this string in the part after the split.
Returns:

Parts of the original string after the split.

Return type:

List[str]

>>> split_long_str("Hello world", long_str_threshold=1)
['Hello', 'world']
>>> split_long_str("Hello world", long_str_threshold=15)
['Hello world']
>>> split_long_str("My quite long sentence example", long_str_threshold=15)
['My quite long', 'sentence', 'example']
>>> split_long_str("Hello world", sep_replacement_before="_")
['Hello_', 'world']
>>> split_long_str("Hello world", sep_replacement_after="_")
['Hello', '_world']
str_dot_product(a, b, multiplication_character='')[source]

Generate the string for a dot product of two arrays, with array elements given by strings.

Parameters:
  • a (List[str]) – First array of elements in the dot product.
  • b (List[str]) – Second array of elements in the dot product.
  • multiplication_character (str) – Character(s) used to represent the product operator.
Returns:

String for the dot product of the two arrays.

Return type:

str

Examples

>>> str_dot_product(["a1", "a2"], ["b1", "b2"])
'a1 b1 + a2 b2'
>>> str_dot_product(["a1", "a2"], ["b1", "b2"], multiplication_character="\\cdot")
'a1 \\cdot b1 + a2 \\cdot b2'
str_exponent(base, exp, str_type='python')[source]

Generate the string for the power of one value raised to an exponent, given by strings.

Parameters:
  • base (str) – Base value.
  • exp (str) – Exponent value.
  • str_type (str) – Type of string, ‘python’ or ‘latex’. Determines the exponentiation character used, ‘**’ or ‘^’. Also for the ‘latex’ string type the exponent is wrapped in braces ({}) if it’s longer than one character.
Returns:

String for the base raised to the power of the exponent.

Return type:

str

>>> str_exponent("e", "x")
'e**x'
>>> str_exponent("e", "x", str_type="latex")
'e^x'
>>> str_exponent("e", "x_2", str_type="latex")
'e^{x_2}'
>>> str_exponent("e", "0")
'1'
>>> str_exponent("e", "1")
'e'
>>> str_exponent("0", "0")
'1'
str_multi_product(a, multiplication_character='')[source]

Generate the string for the product of an array of values, given by strings.

Parameters:
  • a (List[str]) – Values in the product.
  • multiplication_character (str) – Character(s) used to represent the product operator.
Returns:

String for the product of the array of values.

Return type:

str

Examples

>>> str_multi_product(["a1", "a2", "a3"])
'a1 a2 a3'
>>> str_multi_product(["a1", "-a2"], multiplication_character="\\times")
'-a1 \\times a2'
>>> str_multi_product(["a1", "0", "a3"])
'0'
>>> str_multi_product(["a1", "1", "a3"])
'a1 a3'
str_multi_sum(a)[source]

Generate the string for the sum of an array of values, given by strings.

Parameters:a (List[str]) – Values in the sum.
Returns:String for the sum of the values in the array.
Return type:str

Examples

>>> str_multi_sum(["a1", "a2"])
'a1 + a2'
>>> str_multi_sum(["a1", "-a2"])
'a1 - a2'
>>> str_multi_sum(["a1", "0", "a3"])
'a1 + a3'
>>> str_multi_sum(["0", "-a2", "a3"])
'-a2 + a3'
str_number(a, latex_fraction=False, prettify_fractions=True, n=10)[source]

Convert a number to string.

Parameters:
  • a – Number we want to convert to string.
  • latex_fraction (bool) – If the number is a fraction, whether or not to encode the fractions using the Latex “frac” syntax. Default is to just use ‘/’ for the fraction.
  • prettify_fractions (bool) – Whether or not to convert a number which is a ‘nice’ fraction (see is_nice_fraction()) to its fraction representation.
  • n (int) – How large the denominator can be in a fraction for the fraction to be considered nice. Only used when prettify_fractions is True.
Returns:

String representation of the number.

Return type:

str

Examples

>>> str_number(1.0)
'1'
>>> str_number(0.0)
'0'
>>> str_number(0.5)
'1 / 2'
>>> str_number(0.5, prettify_fractions=False)
'0.5'
str_number_array(a, latex=False, latex_column_vector=True, latex_array_style='pmatrix')[source]

Convert an array of numbers to string.

Parameters:
  • a – Array of numbers we want to convert to string.
  • latex (bool) – Whether or not to encode the array using Latex syntax.
  • latex_column_vector (bool) – Whether the array should be encoded as a row or column vector. Only applicable if latex is true.
  • latex_array_style – Latex array style to use. Only applicable if latex is true.
Returns:

String representation of the array of numbers.

Return type:

str

Examples

>>> str_number_array([1.0, 2.0])
'[1, 2]'
>>> str_number_array([0.0, 0.5], latex=True)
'\\begin{pmatrix}0 \\\\ \\frac{1}{2}\\end{pmatrix}'
>>> str_number_array([3, 4], latex=True, latex_column_vector=False, latex_array_style="bmatrix")
'\\begin{bmatrix}3 & 4\\end{bmatrix}'
str_product(a, b, multiplication_character='')[source]

Generate the string for the product of two values, given by strings.

Parameters:
  • a (str) – First value in the product.
  • b (str) – Second value in the product.
  • multiplication_character (str) – Character(s) used to represent the product operator.
Returns:

String for the product of the two values.

Return type:

str

Examples

>>> str_product("a1", "a2")
'a1 a2'
>>> str_product("a1", "-a2", multiplication_character="\\times")
'-a1 \\times a2'
>>> str_product("a1", "0")
'0'
>>> str_product("a1", "1")
'a1'
str_sequence(symbol, n, indexing='fortran', index_style='inline')[source]

Generate a sequence of enumerated strings, e.g. (a1, a2, a3).

Parameters:
  • symbol (str) – Base symbol(s) to use for each string.
  • n (int) – Number of strings to generate.
  • indexing – Which indexing to use. Can be “c” for 0-based indexing (0, 1, 2, …), or “fortran” for 1-based indexing (1, 2, 3, …).
  • index_style – How indices should be added to the base symbol(s). Can be any of “inline”, “subscript”, “superscript”, “latex_subscript”, “latex_superscript” or “list”. See examples below. The “latex_subscript” differ from the “subscript” variant in that the index is wrapped in braces ({}) if it’s longer than one character, and similarly for the “latex_superscript” variant.
Returns:

List of strings.

Examples

>>> str_sequence('a', 3)
['a1', 'a2', 'a3']
>>> str_sequence('a', 3, indexing="c")
['a0', 'a1', 'a2']
>>> str_sequence('a', 3, index_style="subscript")
['a_1', 'a_2', 'a_3']
>>> str_sequence('a', 3, index_style="superscript")
['a^1', 'a^2', 'a^3']
>>> str_sequence('a', 10, index_style="latex_subscript")
['a_1', 'a_2', 'a_3', 'a_4', 'a_5', 'a_6', 'a_7', 'a_8', 'a_9', 'a_{10}']
>>> str_sequence('a', 3, index_style="list")
['a[1]', 'a[2]', 'a[3]']
str_sum(a, b)[source]

Generate the string for the sum of two values, given by strings.

Parameters:
  • a (str) – First value in the sum.
  • b (str) – Second value in the sum.
Returns:

String for the sum of the two values.

Return type:

str

Examples

>>> str_sum("a1", "a2")
'a1 + a2'
>>> str_sum("a1", "-a2")
'a1 - a2'
>>> str_sum("a1", "0")
'a1'
>>> str_sum("0", "-a2")
'-a2'
>>> str_sum("-a1", "1")
'-a1 + 1'
Module contents
polynomials_on_simplices.geometry package
Subpackages
polynomials_on_simplices.geometry.mesh package
Subpackages
polynomials_on_simplices.geometry.mesh.basic_meshes package
Submodules
polynomials_on_simplices.geometry.mesh.basic_meshes.tet_meshes module

Geometries made up of tetrahedrons (polyhedron with four vertices).

general_tetrahedron_vertices(tet_vertices, edge_resolution)[source]

Create the vertices of the a tetrahedron defined by its four vertices, subdivided into a number of smaller tetrahedrons.

Parameters:
  • tet_vertices – The four vertices of the tetrahedron.
  • edge_resolution – Number of vertices along each edge of the tetrahedron.
Returns:

n by 3 list of vertices.

rectangular_box_triangulation(width_resolution, height_resolution, depth_resolution)[source]

Create a triangulation of a rectangular box (subdivision of the geometry into a set of tetrahedrons).

Parameters:
  • width_resolution – Number of vertices along the width of the rectangular box.
  • height_resolution – Number of vertices along the height of the rectangular box.
  • depth_resolution – Number of vertices along the depth of the rectangular box.
Returns:

n by 4 list of tetrahedrons.

tetrahedron_triangulation(edge_resolution)[source]

Create a triangulation of a tetrahedron (subdivision of the geometry into a set of tetrahedrons).

Parameters:edge_resolution – Number of vertices along each edge of the tetrahedron.
Returns:n by 4 list of tetrahedrons.
tetrahedron_vertices(edge_resolution)[source]

Create the vertices of the unit tetrahedron subdivided into a number of smaller tetrahedrons.

Parameters:edge_resolution – Number of vertices along each edge of the tetrahedron.
Returns:n by 3 list of vertices.
unit_cube_vertices(width_resolution, height_resolution, depth_resolution)[source]

Create the vertices of the unit cube.

Parameters:
  • width_resolution – Number of vertices along the width of the unit cube.
  • height_resolution – Number of vertices along the height of the unit cube.
  • depth_resolution – Number of vertices along the depth of the unit cube.
Returns:

n by 3 list of vertices.

polynomials_on_simplices.geometry.mesh.basic_meshes.triangle_meshes module

Triangulated geometries.

annulus_triangulation(angular_resolution, radial_resolution)[source]

Create a triangulation of an annulus.

Parameters:
  • angular_resolution – Number of vertices in the triangulation, going around the annulus.
  • radial_resolution – Number of vertices in the triangulation, from the inner perimeter to the outer perimeter.
Returns:

n by 3 list of triangles.

disc_triangulation(angular_resolution=20, radial_resolution=2)[source]

Create a triangulation of a disc.

Parameters:
  • angular_resolution – Number of vertices in the triangulation, going around the disc.
  • radial_resolution – Number of vertices in the triangulation, from the center of the disc to the perimeter.
Returns:

n by 3 list of triangles.

equilateral_triangle_vertices(edge_length=1.0, edge_resolution=3)[source]

Create the vertices of an equilateral triangle in the xy-plane with two corners along the x-axis and the third corner in the first quadrant.

Parameters:
  • edge_length – Length of each edge in the triangle.
  • edge_resolution – Number of vertices along each edge of the triangle.
Returns:

n by 3 list of vertices.

general_triangle_vertices(tri_vertices, edge_resolution=3)[source]

Create the vertices of a triangle defined by its three vertices.

Parameters:
  • tri_vertices – The three vertices of the triangle.
  • edge_resolution – Number of vertices along each edge of the triangle.
Returns:

n by 3 or n by 2 list of vertices (depending on the shape of the input triangle vertices).

offset_triangulation_vertices(triangles, offset)[source]

Offset the index of each vertex in a triangulation. The triangles list is modified in-place.

Parameters:
  • triangles – n by 3 list of indexed triangles which will be edited.
  • offset – Offset for each vertex index.
rectangle_triangulation(width_resolution=2, height_resolution=2)[source]

Create a triangulation of a rectangle.

Parameters:
  • width_resolution – Number of vertices along the width of the rectangle.
  • height_resolution – Number of vertices along the height of the rectangle.
Returns:

n by 3 list of triangles.

rectangle_vertices(width, height, width_resolution=2, height_resolution=2)[source]

Create the vertices of a rectangle in the xy-plane centered in the origin, and with edges aligned with the x- and y-axis.

Parameters:
  • width – Width of the rectangle.
  • height – Height of the rectangle.
  • width_resolution – Number of vertices along the width of the rectangle.
  • height_resolution – Number of vertices along the height of the rectangle.
Returns:

n by 3 list of vertices.

triangle_mesh_triangulation(original_triangles, edge_resolution)[source]

Create a triangulation of a triangle mesh, where each original triangle is subdivided with the given number of vertices along each edge.

Parameters:
  • original_triangles – Triangle mesh to be subdivided.
  • edge_resolution – Number of vertices along each edge in the original triangle mesh.
Returns:

n by 3 list of triangles.

triangle_mesh_vertices(original_triangles, original_vertices, edge_resolution)[source]
Create the vertices of a triangle mesh, where each original triangle is subdivided with the given number
of vertices along each edge.
Parameters:
  • original_triangles – Triangle mesh to be subdivided.
  • original_vertices – Vertices in the original triangle mesh.
  • edge_resolution – Number of vertices along each edge in the original triangle mesh.
Returns:

n by 3 list of vertices.

triangle_triangulation(edge_resolution)[source]

Create a triangulation of a triangle.

Parameters:edge_resolution – Number of vertices along each edge of the triangle.
Returns:n by 3 list of triangles.
triangle_vertices(width=1.0, height=1.0, edge_resolution=3)[source]

Create the vertices of a triangle in the xy-plane with corners in (0, 0), (width, 0) and (0, height).

Parameters:
  • width – ‘Width’ of the triangle.
  • height – ‘Height’ of the triangle.
  • edge_resolution – Number of vertices along each edge of the triangle.
Returns:

n by 3 list of vertices.

unit_square_vertices(width_resolution=2, height_resolution=2)[source]

Create the vertices of the unit square in the xy-plane.

Parameters:
  • width_resolution – Number of vertices along the width of the unit square.
  • height_resolution – Number of vertices along the height of the unit square.
Returns:

n by 3 list of vertices.

Module contents
Submodules
polynomials_on_simplices.geometry.mesh.point_clouds module

Point cloud operations.

embed_point_cloud_in_rn(points, n)[source]

Embed an m-dimensional point cloud in \(\mathbb{R}^n, m < n\) by adding zeros for the missing coordinates.

Parameters:
  • points (Numpy array) – Points in the point cloud (num points by m array).
  • n (int) – Dimension of the space we want to embed in.
Returns:

The same point cloud embedded in \(\mathbb{R}^n\).

Return type:

Numpy array

Examples

>>> embed_point_cloud_in_rn(np.array([[0, 0], [1, 1]]), 3)
array([[0., 0., 0.],
       [1., 1., 0.]])
embed_point_in_rn(point, n)[source]

Embed an m-dimensional point in \(\mathbb{R}^n, m < n\) by adding zeros for the missing coordinates.

Parameters:
  • point (Numpy array) – Point (m:d vector).
  • n (int) – Dimension of the space we want to embed in.
Returns:

The same point embedded in \(\mathbb{R}^n\).

Return type:

Numpy array

Examples

>>> embed_point_in_rn(np.array([1, 1]), 3)
array([1., 1., 0.])
mean(points)[source]

Compute the mean, or centroid, of a point cloud in \(\mathbb{R}^m\).

\[\frac{1}{N} \sum_{i = 1}^N x_i,\]

where N is the number of points in the point cloud.

Parameters:points (Numpy array) – Points in the point cloud (N by m array).
Returns:Mean of the point cloud along each axis (length m array).
Return type:Numpy array

Examples

>>> mean(np.array([1.0, 3.0, 5.0]))
3.0
>>> mean(np.array([[1.0, 2.0], [3.0, 4.0]]))
array([2., 3.])
median(points)[source]

Compute the median of a point cloud in \(\mathbb{R}^m\).

The i:th entry of the median is the median of the i:th component of the points in the point cloud.

Parameters:points (Numpy array) – Points in the point cloud (num points by m array).
Returns:Median of the point cloud along each axis (length m array).
Return type:Numpy array

Examples

>>> median(np.array([1.0, 3.25, 5.0]))
3.25
>>> median(np.array([[1.0, 2.0], [3.0, 4.0]]))
array([2., 3.])
principal_component_axis(points)[source]

Get the principal component axis of a point cloud in \(\mathbb{R}^m\).

The principal component axis is the direction in which the point cloud is most spread out, i.e. the unit vector w which maximizes

\[\sum_{i = 1}^N \langle x_i - \bar{x}, w \rangle,\]

where N is the number of points in the point cloud and \(\bar{x}\) is the mean of the point cloud.

Parameters:points (Numpy array) – Points in the point cloud (N by m array).
Returns:Principal component axis of the point cloud (length m array).
Return type:Numpy array
polynomials_on_simplices.geometry.mesh.simplicial_complex module

Calculations on a simplicial complex.

boundary(simplices)[source]

Compute the boundary simplices of a simplicial complex.

Note

Assumes that the simplicial complex is a manifold (with boundary), i.e., each boundary simplex is shared by at most two simplices in the complex.

Parameters:simplices – Simplicial complex (list of list of vertex indices for each simplex in the complex).
Returns:List of list of boundary simplices.

Examples

>>> boundary([[0, 1, 2], [1, 3, 2]])
array([[2, 0],
       [0, 1],
       [3, 2],
       [1, 3]])
>>> boundary([[0, 1, 2, 3], [1, 4, 2, 3]])
array([[0, 3, 2],
       [0, 1, 3],
       [0, 2, 1],
       [4, 2, 3],
       [1, 4, 3],
       [1, 2, 4]])
boundary_map_matrix(simplices, k)[source]

Compute the matrix representation of the boundary map \(\mathcal{C}^k \to \mathcal{C}^{k-1}\) for the given simplicial complex.

Parameters:
  • simplices – Simplicial complex. Multi-array where the k:th entry contains an inner array with all k-dimensional simplices in the simplicial complex.
  • k – Determines which boundary map we want to compute the matrix representation of.
Returns:

Matrix representation of the boundary map (scipy sparse matrix in coordinate format (coo_matrix).

num_simplex_boundary_simplices(n)[source]

Get the number of boundary simplices of an n-dimensional simplex.

Parameters:n – Dimension of the simplex.
Returns:Number of boundary simplices.

Examples

>>> num_simplex_boundary_simplices(2)
3
>>> num_simplex_boundary_simplices(3)
4
num_simplex_vertices(n)[source]

Get the number of vertices in an n-dimensional simplex.

Parameters:n – Dimension of the simplex.
Returns:Number of vertices of an n-dimensional simplex.
opposite_sub_simplex(simplex, sub_simplex)[source]

Get the opposite sub simplex of a given sub simplex in a simplex.

The opposite sub simplex of a sub simplex f in a simplex T is the simplex consisting of all the vertices of T not in f.

Parameters:
  • simplex (List[int]) – Simplex defined by a list of vertex indices.
  • sub_simplex (List[int]) – Sub simplex defined by a list of vertex indices.
Returns:

Opposite sub simplex defined by a list of vertex indices.

Return type:

List[int]

Examples

>>> opposite_sub_simplex([0, 1, 2], [1])
[0, 2]
>>> opposite_sub_simplex([0, 1, 2], [0, 1])
[2]
>>> opposite_sub_simplex([0, 1, 2], [0, 1, 2])
[]
simplex_boundary(simplex)[source]

Compute the boundary simplices of a simplex.

Parameters:simplex (List[int]) – Simplex defined by a list of vertex indices.
Returns:List of boundary simplices.
Return type:Numpy array of ints.

Examples

>>> simplex_boundary([0, 1, 2])
array([[1, 2],
       [2, 0],
       [0, 1]])
>>> simplex_boundary([0, 1, 2, 3])
array([[1, 2, 3],
       [0, 3, 2],
       [0, 1, 3],
       [0, 2, 1]])
>>> simplex_boundary([0, 1, 12, 4])
array([[ 1, 12,  4],
       [ 0,  4, 12],
       [ 0,  1,  4],
       [ 0, 12,  1]])
simplex_boundary_map(simplex)[source]

Compute the (k-1)-chain which is the boundary of the given k-simplex.

Parameters:simplex (List[int]) – Simplex defined by a list of vertex indices.
Returns:Boundary chain (pair of two arrays, the first containing the boundary simplices and the second containing the chain coefficients).
simplex_boundary_orientation(simplex, boundary_simplex)[source]

Compute the orientation of a boundary simplex of the given simplex.

Parameters:
  • simplex (List[int]) – Simplex defined by a list of vertex indices.
  • boundary_simplex – Boundary sub simplex (list of vertex indices).
Returns:

Orientation of the boundary simplex (-1/1).

Examples

>>> simplex_boundary_orientation([0, 1, 2], [0, 1])
1
>>> simplex_boundary_orientation([0, 1, 2], [1, 0])
-1
simplex_dimension(simplex)[source]

Get the dimension of a simplex.

Parameters:simplex (List[int]) – Simplex defined by a list of vertex indices.
Returns:Dimension of the simplex.
simplex_sub_simplices(simplex, include_self=True)[source]

Get the set of all sub simplices of a simplex.

Parameters:
  • simplex (List[int]) – Simplex defined by a list of vertex indices.
  • include_self (bool) – Whether or not to include the simplex itself in the set of sub simplices.
Returns:

Set of sub simplices, where each sub simplex is defined by a tuple of sorted vertex indices.

Return type:

Set[Tuple[int]]

Examples

>>> simplex_sub_simplices([0, 1, 2])
{(1, 2), (0, 1), (0,), (1,), (0, 1, 2), (2,), (0, 2)}
>>> simplex_sub_simplices([0, 1, 2], include_self=False)
{(1, 2), (0, 1), (0,), (1,), (2,), (0, 2)}
simplex_sub_simplices_fixed_dimension(simplex, k)[source]

Get the set of all k-dimensional sub simplices of a simplex T.

Parameters:
  • simplex (List[int]) – Simplex T defined by a list of vertex indices.
  • k (int) – Dimension of the sub simplices (in 0, 1, …, dim(T)).
Returns:

Set of sub simplices, where each sub simplex is defined by a tuple of sorted vertex indices.

Return type:

Set[Tuple[int]]

Examples

>>> simplex_sub_simplices_fixed_dimension([0, 1, 2], 0)
{(2,), (0,), (1,)}
>>> simplex_sub_simplices_fixed_dimension([0, 1, 2], 1)
{(1, 2), (0, 1), (0, 2)}
simplex_vertices(simplex, vertex_list)[source]

Get the vertices of a simplex.

Parameters:
  • simplex (List[int]) – Simplex defined by a list of vertex indices.
  • vertex_list – List of vertices in the simplicial complex.
Returns:

List of vertices in the simplex.

swap_orientation(simplices)[source]

Swap orientation for all simplices in a simplicial complex.

Parameters:simplices – Simplicial complex (list of list of vertex indices for each simplex in the complex). Modified in place.
swap_simplex_orientation(simplex)[source]

Swap orientation of a simplex.

Parameters:simplex (List[int]) – Simplex defined by a list of vertex indices.
Returns:Same simplex but with opposite orientation.
polynomials_on_simplices.geometry.mesh.triangle_mesh module

Calculations on a triangle mesh.

centroid(t, v)[source]

Compute the centroid of a triangle mesh (the centroid is the center of gravity if all triangles have uniform and identical density).

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • v – List of vertices in the mesh. Size (m, 3) where m is the number of vertices.
Returns:

The centroid of the triangle mesh.

centroids(t, v)[source]

Compute triangle centroids for each triangle in the mesh.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • v – List of vertices in the mesh. Size (m, 3) where m is the number of vertices.
Returns:

List of triangle centroids.

edge_index(indexed_triangle, edge)[source]

Find the index of an edge in a triangle.

Note

The i:th edge of a triangle is the edge opposite to the i:th vertex, i in {0, 1, 2}.

Parameters:
  • indexed_triangle (Length 3 array of ints) – Indices for the three vertices in the triangle.
  • edge (Pair of ints) – Indices for the two vertices of the edge.
Returns:

Index (in {0, 1, 2}) of the edge in the triangle.

Return type:

int

Examples

>>> edge_index([0, 1, 2], (0, 1))
2
>>> edge_index([3, 8, 11], (3, 11))
1
get_num_vertices(triangles)[source]

Get the number of vertices in a triangulation.

Parameters:triangles – List of triangles in the triangulation.
Returns:Number of vertices in the triangulation.
has_consistent_orientation(triangles)[source]

Check if all triangles in a triangulation have the same orientation.

Parameters:triangles – List of triangles in the triangulation.
Returns:True if all the triangles have the same orientation. False otherwise.
has_same_edge_orientation(triangles, tri_idx, edge)[source]

Check if a triangle and its neighbour across a given edge have the same orientation.

Parameters:
  • triangles – List of triangles in the triangulation.
  • tri_idx – Triangle we originate from.
  • edge – Edge index in {0, 1, 2}. We compare the orientation with the triangle across this edge.
Returns:

True if the orientation is the same, or if the triangle don’t have any neighbour across the given edge. False otherwise.

has_same_orientation(triangle1, triangle2)[source]

Check if two neighbouring indexed triangles have the same orientation.

Parameters:
  • triangle1 – First indexed triangle (list of three vertex indices).
  • triangle2 – Second indexed triangle (list of three vertex indices).
Returns:

True if the orientation is the same, False otherwise.

has_vertex(t, tri, vertex)[source]

Whether or not a triangle contains a specific vertex.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • tri – Triangle index.
  • vertex – Vertex index.
Returns:

True if the triangle contains the vertex, otherwise False.

neighbour(t, i, j)[source]

Get the triangle edge neighbour of a triangle.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • i – Triangle index.
  • j – Edge index.
Returns:

Index of triangle neighbouring triangle i along edge j, or None if no neighbour exists.

normals(t, v)[source]

Compute triangle normals for each triangle in the mesh.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • v – List of vertices in the mesh. Size (m, 3) where m is the number of vertices.
Returns:

List of normalized triangle normals.

opposite_vertex(t, tri, edge)[source]

Get the opposite vertex of a triangle across one of its edges.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • tri – Triangle index.
  • edge – Edge index.
Returns:

Index of vertex opposite edge j of triangle i, or None if no neighbour exists.

swap_orientation(triangles, tri_idx)[source]

Change the orientation of a triangle.

Parameters:
  • triangles – List of triangles in the triangulation.
  • tri_idx – Triangle which should have its orientation swapped.
Returns:

Nothing.

vertex_normals(t, v, weight='area')[source]

Compute vertex normals for each vertex in the mesh by computing a weighted sum of the triangle normals surrounding each vertex.

The area weighted vertex normal arises when you identify the vertex normal with the volume gradient with respect to the vertex coordinates.

Parameters:
  • t – List of triangle indices in the mesh. Size (n, 3) where n is the number of triangles.
  • v – List of vertices in the mesh. Size (m, 3) where m is the number of vertices.
  • weight (str) –

    Weight factor used when computing the weighted sum of triangle normals. Possible values:

    • None: 1 is used as weight for each triangle normal.
    • ”area”: Each triangle normal is weighted with the area of the triangle.
    • ”angle”: Each triangle normal is weighted with the triangle angle at the vertex.
Returns:

List of normalized vertex normals.

vertex_normals_cotangent(t, v)[source]

Compute a normal at each vertex in the mesh using the cotan formula. This expression arises when you identify the vertex normal with the surface area gradient with respect to the vertex coordinates.

vertex_position(indexed_triangle, vertex_index)[source]

Position of a specific vertex in an indexed triangle.

Parameters:
  • indexed_triangle – List of three vertex indices.
  • vertex_index – Vertex index to look for in the triangle.
Returns:

0, 1 or 2 depending on whether the vertex is the first, second or third vertex in the triangle. Returns None if the vertex does not exist in the triangle.

vertices(tri, vertex_list)[source]

Get the vertices of a triangle in the mesh.

Parameters:
  • tri – Triangle (list of 3 vertex indices).
  • vertex_list – List of vertices in the mesh. Size (m, dim) where m is the number of vertices.
Returns:

3 by dim matrix containing the triangle vertices as row vectors.

Module contents
polynomials_on_simplices.geometry.primitives package
Submodules
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)
polynomials_on_simplices.geometry.primitives.triangle module

Compute different triangle properties. A triangle is defined by its 3 vertices, given as rows in a matrix.

altitude_feet(vertices)[source]

Compute the foot of each altitude (the point where the altitude vector intersects the base vector).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:3 by n matrix with the altitude feet as rows.
Return type:Numpy array
altitude_vectors(vertices)[source]

Compute the altitude vectors of a triangle.

An altitude vector is a line segment from a vertex to the opposite edge, such that it is perpendicular to the edge. An altitude vector is parallel with the corresponding dual edge vector. But whereas the dual edge vector has the same length as the edge, the length of the altitude vector is the altitude or height of the triangle.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle altitude vectors.
Return type:Numpy array
altitudes(vertices)[source]

Compute the altitudes in a triangle (i.e. the shortest distance from each vertex to the line spanned by the opposite edge).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:Length 3 array of triangle altitudes.
Return type:List[float]
angle(vertices, i)[source]

Compute the angle of triangle at a vertex of the triangle.

Parameters:
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
  • i – Vertex at which we compute the triangle angle.
Returns:

The triangle angle at the supplied vertex.

Return type:

float

area(vertices)[source]

Compute the area of a triangle.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle area.
Return type:float
area_weighted_normal(vertices)[source]

Compute the area weighted normal of a triangle (\(2 A N = (p_1 - p_0) \times (p_2 - p_1)\)), where \(p_0, p_1, p_2\) are the vertices of the triangle.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The area weighted triangle normal.
Return type:Numpy array
barycentric_to_cartesian(bary, vertices)[source]

Compute the Cartesian coordinates of a point with given barycentric coordinates.

Parameters:
  • bary – The barycentric coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

The Cartesian coordinates vector.

Return type:

n-dimensional vector

barycentric_to_trilinear(bary, vertices)[source]

Compute the trilinear coordinates (ratios between the distances to the triangle edges) of a point with given barycentric coordinates.

Parameters:
  • bary – The barycentric coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

The trilinear coordinates (length 3 array).

Return type:

Numpy array

basis(vertices)[source]

Compute an orthonormal basis \({b_0, b_1}\) in the triangle plane, such that \(b_0 \times b_1 = n\) and \(b_0 || v_0\).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The orthonormal basis.
Return type:Numpy array
cartesian_to_barycentric(cartesian, vertices)[source]

Compute the barycentric coordinates of a point with given Cartesian coordinates.

Parameters:
  • cartesian – The Cartesian coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

The barycentric coordinates (length 3 array).

Return type:

Numpy array

centroid(vertices)[source]

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

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle centroid.
Return type:n-dimensional vector
circumcenter(vertices)[source]

Compute the circumcenter of a triangle (the center of the circle which passes through all the vertices of the triangle).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle circumcenter.
Return type:n-dimensional vector
circumradius(vertices)[source]

Compute the circumradius of a triangle (the radius of the circle which passes through all the vertices of the triangle). See http://mathworld.wolfram.com/Circumradius.html.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle circumradius.
Return type:float
diameter(vertices)[source]

Return largest distance between two points in a triangle, i.e. the length of the longest triangle edge.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:Triangle diameter.
Return type:float
dual_edges(vertices)[source]

Compute the dual edge vectors of a triangle. Dual edge vectors are normal to the edge vectors and points out of the triangle, \(d_i = v_i \times n\), where \(v_i\) are the triangle edges and \(n\) is the triangle normal.

Parameters:vertices – The triangle vertices (3 by 2 or 3 by 3 matrix with the vertices as rows).
Returns:The triangle dual edge vectors.
Return type:Numpy array
dual_edges_2(vertices)[source]

Compute the dual edge vectors of a triangle, expressed in the triangle plane orthonormal basis.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle dual edge vectors (3 by 2 matrix with the coordinates for edge i in row i).
Return type:Numpy array
edge_lengths(vertices)[source]

Compute the length of each edge in a triangle (i.e. the length of each vector returned by the edges() function.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:Length 3 array of edge lengths.
Return type:List[float]
edges(vertices)[source]

Compute the edge vectors of a triangle. The i:th edge is the edge opposite the i:th vertex.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle edge vectors (3 by n matrix with the edges as rows (where n is the dimension of the space)).
Return type:Numpy array
edges_2(vertices)[source]

Compute the edge vectors of a triangle, expressed in the triangle plane orthonormal basis.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle edge vectors (3 by 2 matrix with the coordinates for edge i in row i).
Return type:Numpy array
in_triangleplane_coords(vertices, v)[source]

Compute the representation of the vector v in the triangle plane orthonormal basis.

Parameters:
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
  • v (n-dimensional vector) – Vector whose coordinates we are interested in.
Returns:

Vector expressed in the triangle plane orthonormal basis (2d array with vector components).

Return type:

Numpy array

incenter(vertices)[source]

Compute the incenter of a triangle (the center of the largest inscribed circle or the intersection of all the angle bisectors).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle incenter.
Return type:n-dimensional vector
inradius(vertices)[source]

Compute the inradius of a triangle (the radius of the of the largest inscribed circle). See http://mathworld.wolfram.com/Inradius.html.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle inradius.
Return type:float
inside_triangle(point, vertices, include_boundary=True)[source]

Check whether or not a point lies inside a triangle.

Parameters:
  • point – Point which we want to check.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
  • include_boundary – Whether or not to consider points on the boundary as inside the triangle.
Returns:

Whether or not the points lies inside the triangle.

Return type:

bool

is_degenerate(vertices, eps=0.0002)[source]

Check if a triangle is degenerate.

A triangle is considered degenerate if any of its angles is smaller than \(\varepsilon\) radians,

\[\min_{i = 1, 2, 3} \alpha_i < \varepsilon.\]
Parameters:
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
  • 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

medians(vertices)[source]

Compute the median vectors of a triangle, pointing from each vertex into the triangle to the midpoint of the opposite edge.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle median vectors (3 by n matrix with the median vectors as rows).
Return type:Numpy array
medians_2(vertices)[source]

Compute the median vectors of a triangle, pointing from each vertex into the triangle. Expressed in the triangle plane orthonormal basis.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle median vectors (3 by 2 matrix with the median vectors as rows).
Return type:Numpy array
normal(vertices)[source]

Compute the normal of a triangle.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The normalized triangle normal.
Return type:Numpy array
normalize_trilinear_coordinates(trilinear, vertices)[source]

Compute normalized (or exact) trilinear coordinates (ratios between the distances to the triangle edges).

Two trilinear coordinates x:y:z and kx:ky:kz describe the same point. Here normalized means that the three entries in the trilinear coordinates is exactly the distance from the point to the three edges. See http://mathworld.wolfram.com/ExactTrilinearCoordinates.html.

Parameters:
  • trilinear (3d vector) – Trilinear coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

Equivalent normalized trilinear coordinates.

Return type:

3d vector

orthocenter(vertices)[source]

Compute the orthocenter of a triangle (the point where all the triangle heights intersect).

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle orthocenter.
Return type:n-dimensional vector
perimeter(vertices)[source]

Compute the perimeter of a triangle.

Parameters:vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:The triangle perimeter.
Return type:float
side_distances_to_trilinear(side_distances)[source]

Compute trilinear coordinates (ratios between the distances to the triangle edges) for a point with given distances to each edge of the triangle.

Parameters:side_distances (3d vector) – Distance from the point to each edge of the triangle.
Returns:Trilinear coordinates.
Return type:3d vector
trilinear_to_barycentric(trilinear, vertices)[source]

Compute the barycentric coordinates of a point with given trilinear coordinates (ratios between the distances to the triangle edges).

Parameters:
  • trilinear (3d vector) – The trilinear coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

The barycentric coordinates (length 3 array).

Return type:

Numpy array

trilinear_to_side_distances(trilinear, vertices)[source]

Compute actual distances to the triangle edges for a point with given trilinear coordinates (ratios between the distances to the triangle edges).

Parameters:
  • trilinear (3d vector) – The trilinear coordinates.
  • vertices – The triangle vertices (3 by n matrix with the vertices as rows (where n is the dimension of the space)).
Returns:

Distance from the point to each edge of the triangle (length 3 array).

Return type:

3d vector

Module contents
polynomials_on_simplices.geometry.proximity package
Submodules
polynomials_on_simplices.geometry.proximity.aabb module

Routines for dealing with axis aligned bounding boxes (AABB).

An AABB is represented with two n:d vectors giving the min and max point of the AABB, i.e., aabb = (min, max) = ((x_min, y_min, z_min), (x_max, y_max, z_max)) (for an AABB in 3D).

corner(aabb, i)[source]

Get the i:th corner of the AABB. Corners are ordered based on their x_n coordinate, with the x_{n-1} coordinate as first tiebreaker, x_{n-2} coordinate as second tiebreaker, and so on. For example the corners of a 3D AABB are ordered on their z-value, with y-value as first tiebreaker, and x-value as second tiebreaker.

Parameters:
  • aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
  • i – Index of the corner (in the range 0, 1, …, 2^n-1 where n is the dimension of the AABB).
Returns:

AABB corner (nD vector).

create(points)[source]

Create the minimum AABB containing a set of points.

Parameters:points – Array of points (num_of_points by n).
Returns:Minimum AABB including all input points.
diameter(aabb)[source]

Compute the length of the diameter of an AABB.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:Length of the diameter of the AABB.
dimension(aabb)[source]

Get the dimension of an AABB.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:Dimension of the AABB.
empty(n=3)[source]

Create an empty AABB.

Parameters:n – Dimension of the AABB.
Returns:Empty AABB.
full(n=3)[source]

Create an AABB covering all of \(\mathbb{R}^n\).

Parameters:n – Dimension of the AABB.
Returns:The AABB containing all of \(\mathbb{R}^n\).

Examples

>>> full(1)
(array([-inf]), array([inf]))
>>> full(2)
(array([-inf, -inf]), array([inf, inf]))
half_diagonal(aabb)[source]

Compute the half-diagonal of an AABB.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:Half-diagonal of the AABB (vector from the AABB midpoint to the max point).
intersection(aabb1, aabb2)[source]

Compute the intersection of two AABBs.

Parameters:
  • aabb1 – First AABB.
  • aabb2 – Second AABB.
Returns:

Minimum AABB including all points in both aabb1 and aabb2.

is_empty(aabb)[source]

Check if an AABB is empty.

Note

An AABB is empty if max[i] < min[i] for some i in [0, 1, 2]. An AABB with min = max is not considered empty since it contains a single point.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:True/False depending on whether or not the AABB is empty.
is_equal(aabb1, aabb2, rel_tol=1e-09, abs_tol=1e-07)[source]

Check if two AABBs aabb1 and aabb2 are approximately equal.

The two AABBs are considered equal if the min and max points of the AABBs are componentwise approximately equal. For the componentwise equality check, the standard function math.isclose is used, with the given relative and absolute tolerances.

is_valid(aabb)[source]

Check if an AABB is valid (has valid min and max points, and min[i] <= max[i] for all i in [0, 1, 2]).

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:True/False depending on whether or not the AABB is valid.
midpoint(aabb)[source]

Compute the midpoint of an AABB.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:Midpoint of the AABB (nd vector).
point_inside(aabb, p)[source]

Check if a point is inside an AABB.

Parameters:
  • aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
  • p – Point (n dimensional vector).
Returns:

True/False whether or not the point is inside the AABB.

union(aabb1, aabb2)[source]

Compute the union of two AABBs.

Parameters:
  • aabb1 – First AABB.
  • aabb2 – Second AABB.
Returns:

Minimum AABB including all points in either aabb1 or aabb2.

unit(n=3)[source]

Create the unit AABB (side lengths 1 and with the min point in the origin).

Parameters:n – Dimension of the AABB.
Returns:The unit AABB.
volume(aabb)[source]

Compute the volume of an AABB.

Parameters:aabb (Pair of n-dimensional vectors) – AABB defined by its min and max point.
Returns:Volume of the AABB.
Module contents
polynomials_on_simplices.geometry.space_partitioning package
Subpackages
polynomials_on_simplices.geometry.space_partitioning.kd_mesh_tree package
Submodules
polynomials_on_simplices.geometry.space_partitioning.kd_mesh_tree.simplicial_mesh_kd_tree module

Kd-tree built around a simplicial mesh (a mesh consisting of n-dimensional simplices, e.g. a triangle mesh or a tetrahedral mesh).

class KdTreeSMNode(parent, side, simplices, vertices, simplices_of_interest=None)[source]

Bases: polynomials_on_simplices.geometry.space_partitioning.kd_tree.KdTreeNode

A node in a k-d tree, which is augmented with information about the simplices in a simplicial mesh which potentially intersects with the space associated with the node.

The calculation of potentially intersecting simplices is conservative, in that the list of potentially intersecting simplices can contain simplices that doesn’t intersect with the node. However it will not miss any simplices, i.e. there are no simplices in the mesh that intersect a node which are not part of that nodes list of potentially intersecting simplices.

Parameters:
  • parent (Optional[KdTreeSMNode]) – Parent node. None for the root node.
  • side (int) – Indicate if this node is associated with the negative or positive side of the parent node hyperplane, indicated by a 0 or 1.
  • simplices (num simplices by (n + 1) array of integers) – The simplices in the mesh the k-d tree is built around.
  • vertices (num vertices by n array of floats) – The vertices in the mesh the k-d tree is built around.
  • simplices_of_interest (Optional[List[None]]) – Optional subset of simplices in the mesh that we want to build the k-d tree around. If not specified all simplices will be used.
subdivide(plane_point, plane_normal_idx=0)[source]

Subdivide this node into 2 child nodes by splitting this node into two halves using the given (hyper-)plane.

Parameters:
  • plane_point – A point in the (hyper-)plane used to split this node.
  • plane_normal_idx (int) – Index of the coordinate axis that should be the normal of the hyperplane that splits this node (in [0, 1, …, k - 1]).
create_kd_tree(simplices, vertices, fixed_depth=False, max_depth=5, max_simplices_in_leaf=None, simplices_of_interest=None)[source]

Create a k-d tree which covers a simplicial mesh.

Parameters:
  • simplices (num simplices by (n + 1) array of integers) – The simplices in the mesh the k-d tree is built around.
  • vertices (num vertices by n array of floats) – The vertices in the mesh the k-d tree is built around.
  • fixed_depth (bool) – Specify how to subdivide the k-d tree when covering the simplicial mesh. If fixed depth is True, k-d tree nodes intersecting simplices will be subdivided to the max depth specified by the max_depth parameter. If fixed_depth is False, the tree will instead be adaptively subdivided until the max depth is reached or a leaf node doesn’t intersect with more than max_simplices_in_leaf simplices (if specified).
  • max_depth (int) – Maximum depth of the created k-d tree.
  • max_simplices_in_leaf (Optional[int]) – If fixed_depth is False, the tree will be subdivided until no leaf node intersects with more than this number of simplices, or the maximum depth is reached.
  • simplices_of_interest (Optional[List[int]]) – Optional subset of simplices in the mesh that we want to build the k-d tree around. If not specified all simplices will be used.
Returns:

K-d tree build around the line strip.

Return type:

KdTreeSMNode

Module contents
Submodules
polynomials_on_simplices.geometry.space_partitioning.kd_tree module

A K-d tree is a special case of a binary space partitioning tree, where each space partitioning (hyper-)plane is aligned with the coordinate axes (plane normal is some coordinate axis).

class KdTreeNode(parent, k=3, side=0)[source]

Bases: object

A node in a K-d tree.

The node is either a leaf node, or it contains a hyperplane that splits space into two parts, each part being associated with one of its two child nodes. Hence a node in a K-d tree has a subspace of \(\mathbb{R}^k\) associated with it. In a k-d tree the space partitioning hyperplane is always aligned with the coordinate axes. This means that the space associated with a node is an axis aligned box (potentially stretching to infinity), see in_node_space() and node_aabb().

Parameters:
  • parent (Optional[KdTreeNode]) – Parent node. None for the root node.
  • k (int) – Dimension of the space the k-d tree exists in.
  • side (int) – Indicate if this node is associated with the negative or positive side of the parent node hyperplane, indicated by a 0 or 1.
address()[source]

Address of node in the tree.

An address in the tree is a list of sibling indices. For example:

[0] gives the root node. [0, 1] gives the second (zero-based indexing) child node of the root node. [0, 1, 0] gives the first child node of the second child node of the root node.

Returns:List of nodes to follow to reach to this node.
Return type:List[{0, 1}]
cut_subtree()[source]

Remove the subtree beneath this node (remove child nodes and plane splitting this node).

depth()[source]

Get depth of node in the tree (number of parents to traverse to reach the root node).

Returns:Depth of the node in the tree.
Return type:int
is_leaf()[source]
Returns:Whether or not this node is a leaf node in the tree.
Return type:bool
is_root()[source]
Returns:Whether or not this node is the root node of the tree.
Return type:bool
recurse(fn)[source]

Recursively call a function on this node and all child nodes.

Parameters:fn (Callable f(node)) – Function to call.
sibling()[source]

Get the sibling node for this node.

Returns:Sibling node.
Return type:KdTreeNode
sibling_index()[source]

Get index of node in the pair of siblings.

Returns:0 or 1 depending on whether this is the first or second child node of the parent.
subdivide(plane_point, plane_normal_idx=0)[source]

Subdivide this node into 2 child nodes by splitting this node into two halves using the given (hyper-)plane.

Parameters:
  • plane_point – A point in the (hyper-)plane used to split this node.
  • plane_normal_idx (int) – Index of the coordinate axis that should be the normal of the hyperplane that splits this node (in [0, 1, …, k - 1]).
create_kd_tree(k)[source]

Create a k-d tree containing a single root node.

Parameters:k (int) – Dimension of the space the k-d tree exists in.
Returns:KdTreeNode tree containing a single root node.
Return type:KdTreeNode
find_leaf_containing_point(tree_root, point)[source]

Find a leaf node in a k-d tree which contains the given point.

Parameters:
  • tree_root (KdTreeNode) – Root node in the k-d tree.
  • point (n-dimensional vector) – Point in \(\mathbb{R}^k\).
Returns:

Leaf node containing the point.

Return type:

KdTreeNode

in_node_space(tree_node, point)[source]

Check if the given point lies in the part of space associated with the given node in a k-d tree.

The root node in a k-d tree is associated with all of space. For a non root node the node is associated with the part of the parent node space lying on either the negative or positive side of the parent node splitting plane (depending on whether the node is the first or second child node of the parent).

Parameters:
  • tree_node (KdTreeNode) – Tree node in whose space we want to check if the point lies.
  • point (n-dimensional vector) – Point in \(\mathbb{R}^k\).
Returns:

Whether or not the point lies in the part of space associated with the given node.

Return type:

bool

node_aabb(tree_node)[source]

Get the space associated with a node in a k-d tree. Since the space partitioning hyperplanes in a k-d tree is aligned with the coordinate axis, this space is an axis aligned box.

Parameters:tree_node (KdTreeNode) – Tree node whose associated space (axis aligned bounding box) we want to get.
Returns:Axis aligned box for the given tree node, represented with two n:d vectors giving the min and max point of the AABB.
Return type:Pair of n-dimensional vectors
polynomials_on_simplices.geometry.space_partitioning.tree_traversal module

Functionality for navigating through a tree.

get_descendant_by_address(tree_node, relative_address)[source]

Get the descendant node with given address relative to the given tree node.

A relative address with respect to a tree node is a list of successive children to traverse to reach the desired descendant. For example:

[0] is the address of the first child of the given tree node. [1, 2] is the address of the third child of the second child of the given tree node.

Parameters:
  • tree_node – Tree node whose descendant we want to get.
  • relative_address (List[int]) – Relative address to the descendant we want to get.
get_node_by_address(root_node, address)[source]

Get the node in the given tree with the given address.

An address in the tree is a list of sibling indices. For example:

[0] gives the root node. [0, 1] gives the second (zero-based indexing) child node of the root node. [0, 1, 0] gives the first child node of the second child node of the root node.

Parameters:
  • root_node – Root node of the tree we want to traverse.
  • address (List[int]) – Address to the tree node we want to get.
Module contents
Module contents
polynomials_on_simplices.linalg package
Submodules
polynomials_on_simplices.linalg.basis module

Functionality for dealing with bases.

basis_matrix(basis_vectors_array)[source]

Construct the matrix representation of a basis from a list of basis vectors.

Parameters:basis_vectors_array – List of basis vectors.
Returns:Matrix representation of basis (square matrix with basis vectors as columns).
basis_vectors(basis)[source]

Get a list of basis vectors from the matrix representation of a basis.

Parameters:basis – Matrix representation of basis (square matrix with basis vectors as columns).
Returns:List of basis vectors.
cartesian_basis(dimension=3)[source]

Get the standard Cartesian basis in the specified dimension.

Parameters:dimension (int) – Dimension of Euclidean space.
Returns:The identity matrix in \(\mathbb{R}^n\), where n is the desired dimension.
Return type:Numpy array

Examples

>>> cartesian_basis(2)
array([[1., 0.],
       [0., 1.]])
cartesian_basis_vector(i, dimension=3)[source]

Get the i:th standard Cartesian basis vector in the specified dimension (3 by default).

Parameters:
  • i (int) – Index of the basis vector (in [0, 1, …, dimension - 1]).
  • dimension (int) – Dimension of Euclidean space.
Returns:

The i:th Cartesian basis vector.

Return type:

Numpy array

Examples

>>> cartesian_basis_vector(1)
array([0., 1., 0.])
dual_basis(basis)[source]

Compute the dual basis of a given basis, i.e., the basis \(e^i\) such that \(\langle e_i, e^j \rangle = \delta_i^j\).

Parameters:basis – Basis (square matrix with basis vectors as columns).
Returns:Dual basis (square matrix with dual basis vectors as columns).
gram_schmidt_orthonormalization(basis, inner_product=<function dot>)[source]

Create an orthonormal basis for an inner product space V from a general basis using the Gram-Schmidt process.

Parameters:
  • basis – Input array of basis vectors.
  • inner_product – Inner product for the inner product space (a map \(V \times V \to \mathbb{R}\) satisfying the inner product properties).
Returns:

Orthonormal basis (matrix with the basis vectors as columns), where the basis vectors is obtained by Gram-Schmidt orthonormalization of the input basis vectors.

gram_schmidt_orthonormalization_rn(basis)[source]

Create an orthonormal basis from a general basis of \(\mathbb{R}^n\) or an n dimensional subspace of \(\mathbb{R}^m\) using the Gram-Schmidt process.

Parameters:basis (Numpy array) – Input basis (m by n matrix with the basis vectors as columns).
Returns:Orthonormal basis (m by n matrix with the basis vectors as columns), where the basis vectors is obtained by Gram-Schmidt orthonormalization of the input basis vectors.
Return type:Numpy array
inverse_metric_tensor(basis)[source]

Compute the inverse metric tensor for a basis.

Parameters:basis – Basis (square matrix with basis vectors as columns).
Returns:Inverse metric tensor.
metric_tensor(basis)[source]

Compute the metric tensor for a basis.

Parameters:basis – Basis (square matrix with basis vectors as columns).
Returns:Metric tensor.
transform(v, basis0, basis1)[source]

Transform a vector or set of vectors from one basis to another.

Parameters:
  • v – Representation of the vector(s) in the initial basis (vector or matrix with vectors as columns).
  • basis0 – Initial basis to transform from (matrix with basis vectors as columns).
  • basis1 – Final basis to transform to (matrix with basis vectors as columns).
Returns:

Representation of the vector(s) in the final basis.

transform_bilinear_form(b, basis0, basis1)[source]

Transform a bilinear form (matrix) from one basis to another.

Parameters:
  • b – Representation of the bilinear form in the initial basis (n by n matrix).
  • basis0 – Initial basis to transform from (matrix with basis vectors as columns).
  • basis1 – Final basis to transform to (matrix with basis vectors as columns).
Returns:

Representation of the bilinear form in the final basis (n by n matrix).

transform_from_basis(v, basis)[source]

Transform a vector or set of vectors in a given coordinate system to Cartesian coordinates.

Parameters:
  • v – Vector(s) expressed in the supplied basis (vector or matrix with vectors as columns).
  • basis – Basis the vector is expressed in (matrix with basis vectors as columns).
Returns:

Vector(s) in Cartesian coordinates.

transform_to_basis(v, basis)[source]

Transform a vector or set of vectors from Cartesian coordinates to the supplied coordinate system.

Parameters:
  • v – Vector(s) expressed in Cartesian coordinates (vector or matrix with vectors as columns).
  • basis – Basis to express the vector in (matrix with basis vectors as columns).
Returns:

Vector representation(s) in the supplied basis.

polynomials_on_simplices.linalg.rigid_motion module

Rigid motions (elements of \(SE(3)\)).

move(rotation, translation, v)[source]

Move a vector or set of vectors using a rigid motion. The rigid body motion is composed of a rotation followed by a translation.

Parameters:
  • rotation – Rotation part of the rigid motion (rotation matrix).
  • translation – Translation part of the rigid motion (3d vector).
  • v – Vector(s) to move (specified as columns in a matrix).
Returns:

Moved vector(s).

random_rigid_motion(radius=1.0, n=3)[source]

Generate a random rigid motion with finite translation.

Parameters:
  • radius – Translation vector will be uniformly sampled from the ball with this radius.
  • n – Dimension of the space in which the generated rigid motion acts.
Returns:

Tuple of a rotation and a translation (orthogonal 3x3 matrix and 3d vector in 3d, orthogonal 2x2 matrix and 2d vector in 2d).

translate(translation, v)[source]

Translate a vector or set of vectors.

Parameters:
  • translation – Translation (3d vector).
  • v – Vector(s) to translate (specified as columns in a matrix).
Returns:

Translated vector(s).

polynomials_on_simplices.linalg.rotation module

Basic functionality for rotations.

axis_angle_to_rotation_matrix(axis, angle)[source]

Convert an axis-angle representation of a rotation (exponential coordinates) to the corresponding rotation matrix.

Parameters:
  • axis – Rotation axis (unit vector).
  • angle – Rotation angle.
Returns:

Rotation matrix (orthogonal matrix).

compute_rotation(v0, v1)[source]

Compute the rotation in axis-angle representation which would transform one vector into another, while keeping orthogonal vectors fixed.

Parameters:
  • v0 – Initial vector (unit vector).
  • v1 – Final vector (unit vector).
Returns:

Tuple of an axis (3d vector) and an angle (scalar).

hat(w)[source]

Get the skew-symmetric matrix \(\hat{\omega}\) corresponding to a rotation vector \(\omega\), i.e., the matrix \(\hat{\omega}\) such that

\[\hat{\omega} v = \omega \times v, \, \forall v \in \mathbb{R}^3.\]

Commonly referred to as the “hat” map.

Parameters:w – Euler vector (rotation vector).
Returns:Equivalent skew-symmetric matrix (\(\hat{\omega}\)).
random_rotation_matrix_2()[source]

Generate a random 2d rotation matrix.

Returns:Rotation matrix (orthogonal matrix).
random_rotation_matrix_3()[source]

Generate a random 3d rotation matrix.

Returns:Rotation matrix (orthogonal matrix).
rodrigues_formula(v, k, cost, sint)[source]

Rotate a vector in \(\mathbb{R}^3\) using Rodrigues’ formula.

Parameters:
  • v – Vector to be rotated.
  • k – Unit vector describing the axis of rotation.
  • cost – Cosine of the rotation angle.
  • sint – Sine of the rotation angle.
Returns:

Rotated vector.

rodrigues_formula_matrix(k, cost, sint)[source]

Compute a rotation matrix from a rotation axis and cosine and sine values of the rotation angle using the matrix form of Rodrigues’ formula.

Parameters:
  • k – Unit vector describing the axis of rotation.
  • cost – Cosine of the rotation angle.
  • sint – Sine of the rotation angle.
Returns:

Rotation matrix (3 by 3 Numpy array).

rotate(axis, angle, v)[source]

Rotate a vector or a set of vectors a specified angle around an axis.

Parameters:
  • axis – Axis to rotate around (unit vector).
  • angle – Angle to rotate (in radians).
  • v – Vector(s) to rotate (specified as columns in a matrix).
Returns:

Rotated vector(s).

rotation_matrix_to_axis_angle(R)[source]

Convert a rotation matrix to the axis-angle representation (exponential coordinates).

Parameters:R – Rotation matrix (orthogonal matrix).
Returns:Tuple of an axis (3d vector) and an angle (scalar in the range \([0, \pi]\)).
polynomials_on_simplices.linalg.vector_space_projection module

Projection routines.

interval_projection(p, interval)[source]

Project a point in \(\mathbb{R}\) to an interval [a, b].

Parameters:
  • p – Point to project (scalar).
  • interval – Interval to project to (pair of two scalars).
Returns:

Point in [a, b] closest to p (scalar).

subspace_projection_map(basis, origin=None)[source]

Generate the affine map \(P_V : \mathbb{R}^n \to \mathbb{R}^n, P(x) = Ax + b\) which projects a point \(x \in \mathbb{R}^n\) onto an m-dimensional vector subspace V of \(\mathbb{R}^n, m \leq n\).

We have

\[A = BB^T \in \mathbb{R}^{n \times n},\]
\[b = (I - BB^T) o_V \in \mathbb{R}^n,\]

where \(B\) is the Gram-Schmidt orthonormalization of the basis spanning V and \(o_V\) is the origin of V.

Parameters:
  • basis (Element in \(\mathbb{R}^{n \times m}\).) – Basis for the subspace V (matrix with the basis vectors as columns).
  • origin (Element in \(\mathbb{R}^n\).) – Origin of the subspace V. Optional, the n-dimensional zero-vector is used if not specified.
Returns:

Function which takes an n-dimensional vector as input and returns an n-dimensional vector in V.

Return type:

Callable \(P_V(x)\).

subspace_projection_transformation(basis, origin=None)[source]

Generate the affine transformation \(P_V : \mathbb{R}^n \to \mathbb{R}^n, P(x) = Ax + b\) which projects a point \(x \in \mathbb{R}^n\) onto an m-dimensional vector subspace V of \(\mathbb{R}^n, m \leq n\).

We have

\[A = BB^T \in \mathbb{R}^{n \times n},\]
\[b = (I - BB^T) o_V \in \mathbb{R}^n,\]

where \(B\) is the Gram-Schmidt orthonormalization of the basis spanning V and \(o_V\) is the origin of V.

Parameters:
  • basis (Element in \(\mathbb{R}^{n \times m}\).) – Basis for the subspace V (matrix with the basis vectors as columns).
  • origin (Element in \(\mathbb{R}^n\).) – Origin of the subspace V. Optional, the n-dimensional zero-vector is used if not specified.
Returns:

Tuple of A and b.

vector_oblique_projection_2(a, b, c)[source]

Projection of vector a on b, along vector c (oblique projection). Only applicable to two dimensional vectors.

Parameters:
  • a – Vector to project.
  • b – Vector to project onto.
  • c – Vector defining the direction to project along.
Returns:

Oblique projection of a onto b along c.

vector_plane_projection(v, n)[source]

Projection of a vector onto a plane with normal n.

Parameters:
  • v – Vector to project onto the plane.
  • n – Normal of the plane.
Returns:

Vector projected onto the plane.

vector_projection(a, b)[source]

Orthogonal projection of vector a on b.

Parameters:
  • a – Vector to project.
  • b – Vector to project onto.
Returns:

Projection of vector a on b.

vector_rejection(a, b)[source]

Rejection of vector a from b (the component of a orthogonal to b).

Parameters:
  • a – Vector to reject.
  • b – Vector to reject from.
Returns:

Rejection of vector a from b.

Module contents
polynomials_on_simplices.piecewise_polynomial package
Submodules
polynomials_on_simplices.piecewise_polynomial.continuous_piecewise_polynomial module

Functionality for Lagrange (continuous Galerkin) finite elements (continuous piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\).

The space of continuous piecewise polynomials of degree r on \(\mathcal{T}, C\mathcal{P}_r (\mathcal{T}) = C\mathcal{P}_r (\mathcal{T}, \mathbb{R}^n) \subset H^1(\mathcal{T}, \mathbb{R}^n)\) is defined as

\[C\mathcal{P}_r (\mathcal{T}) = \{ v \in C(\mathcal{T}, \mathbb{R}^n) \big| v|_T \in \mathcal{P}_r(T, \mathbb{R}^n) \, \forall T \in \mathcal{T} \}.\]

Correspondingly the space of continuous piecewise polynomials of degree r on \(\mathcal{T}\) which are zero on specified set of simplices or subsimplices of \(\mathcal{T}, C\mathcal{P}_{r, 0} (\mathcal{T}))\) is defined as

\[C\mathcal{P}_{r, 0} (\mathcal{T}) = \{ v \in C_0(\mathcal{T}, \mathbb{R}^n) \big| v|_T \in \mathcal{P}_r(T, \mathbb{R}^n) \, \forall T \in \mathcal{T} \}.\]
class ContinuousPiecewisePolynomialBase(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None)[source]

Bases: polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial.PiecewisePolynomialBase, abc.ABC

Abstract base class for a continuous piecewise polynomial function of degree r on a triangulation \(\mathcal{T}\), i.e. an element of \(C\mathcal{P}_r (\mathcal{T})\) or \(C\mathcal{P}_{r, 0} (\mathcal{T})\). The space of continuous piecewise polynomials is a subspace of the space of piecewise polynomials, see polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial and in particular PiecewisePolynomialBase.

Differentiable structure

Besides the basic algebraic structures of the space of piecewise polynomials this class also defines the (weakly) differentiable structure of the space of continuous piecewise polynomials.

i:th partial weak derivative: \(\partial_i : C\mathcal{P}_r (\mathcal{T}) \to D\mathcal{P}_{r - 1} (\mathcal{T})\).

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the basis for \(\mathcal{P}_r (\Delta_c^m)\) used (see polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial.PiecewisePolynomialBase.basis()) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Callable \(\tau(j, \nu)\)) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function, in a way that makes sure that the piecewise polynomial is continuous. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial function should vanish (for an element of \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the continuous piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
static generate_local_to_global_map(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map \(\tau\) for the space of continuous piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, C \mathcal{P}_r (\mathcal{T})\) or \(C \mathcal{P}_{r, 0} (\mathcal{T})\). See generate_local_to_global_map().

weak_partial_derivative(i=0)[source]

Compute the i:th weak partial derivative of the continuous piecewise polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th weak partial derivative of this continuous piecewise polynomial.
generate_local_to_global_map(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map \(\tau\) for the space of continuous piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, C \mathcal{P}_r (\mathcal{T})\) or \(C \mathcal{P}_{r, 0} (\mathcal{T})\).

We have \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \to \mathbb{N}_0 \cup \{ -1 \}\).

A local-to-global map maps a local polynomial basis function on a simplex in the triangulation to a global basis index for a basis for the space of continuous piecewise polynomials. The value -1 is used to indicate that a local basis function has no corresponding global basis function (see later).

Let \(d = \dim \mathcal{P}_r (\Delta_c^m)\), and let the triangles in \(\mathcal{T}\) be enumerated so that we have \(\mathcal{T} = \{ T_0, T_1, \ldots, T_{| \mathcal{T} | - 1} \}\). Given a basis \(\{ \bar{\varphi}_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) for \(\mathcal{P}_r (\Delta_c^m)\), the functions \(\{ \varphi_{\nu, j} = \bar{\varphi}_{\nu} \circ \Phi_{T_j}^{-1} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) is a basis for \(\mathcal{P}_r(T_j)\), where \(\Phi_{T_j}, j = 0, 1, \ldots, | \mathcal{T} | - 1\) is the unique affine map which maps the unit simplex \(\Delta_c^m\) to the simplex \(T_j\). From these local bases and the local-to-global map \(\tau\) a basis \(\{ \phi_i \}_{i = 1}^N\) for \(C \mathcal{P}_r (\mathcal{T})\) is constructed, with

\[\phi_i(x) = \sum_{(j, \nu) \in \operatorname{preim}_{\tau}(i)} \chi_{T_j}(x) \cdot \varphi_{\nu, j}(x).\]

For each triangle \(T_j\) a basis function \(\phi_i\) either agrees with a basis function \(\varphi_{\nu, j}\) or it’s zero. I.e. the set \(v_j = \{ (k, \nu) \in \operatorname{preim}_{\tau}(i) \big| k = j \}\) either contains zero or one element.

\[\begin{split}\phi_i(x) = \begin{cases} \bar{\varphi}_{\nu} \circ \Phi_{T_j}^{-1}(x), & (\nu, j) \text{ unique element in } v_j \\ 0, & v_j \text{ is empty} \end{cases}.\end{split}\]

Optionally a set of boundary simplices can be prescribed where the piecewise polynomial function should vanish. This is achieved by associating an invalid global index (-1) for all local basis functions supported on any of the boundary simplices). Alternatively local basis functions supported on boundary simplices can be associated with global indices placed last in the enumeration of global basis functions (by setting the keep_boundary_dofs_last to true).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the continuous piecewise polynomial functions.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for an element of \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last makes \(C\mathcal{P}_{r, 0} (\mathcal{T})\) a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

Tuple containing the local to global map \(\tau\) and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[Callable \(\tau(j, \nu)\), int, Optional[int]].

generate_local_to_global_map_as_dictionaries(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map for the space of continuous piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, C \mathcal{P}_r (\mathcal{T})\) or \(C \mathcal{P}_{r, 0} (\mathcal{T})\). For details see generate_local_to_global_map(). This function differs from that function in that a list of dictionaries (one for each triangle in the mesh) with multi-indices (second function argument) as keys and global DOF indices (function value) as values is returned instead of a callable, which might be preferable in some cases.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the piecewise polynomial functions.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

Tuple containing the local to global map dictionaries, one for each triangle, and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[List[dict[Tuple[int], int]], int, Optional[int]]

generate_local_to_global_preimage_map(tau, num_triangles, num_dofs, r, m)[source]

Generate the preimage map of a local-to-global map.

Let \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \to \mathbb{N}_0 \cup \{ -1 \}\) be a local-to-global map (see generate_local_to_global_map()). Then the preimage map of \(\tau\) is the map

\[\operatorname{preim}_{\tau} : \mathcal{P} \mathbb{N}_0 \to \mathcal{P} \left( \{ 0, 1, \ldots, |\mathcal{T}| - 1 \} \times \mathbb{N}_0^N \right)\]

such that

\[\tau(j, \nu) = i \quad \forall \, (j, \nu) \in \operatorname{preim}_{\tau}(\{i\}).\]
Parameters:
  • tau (Callable \(\tau(j, \nu)\)) – Local-to-global map that we want to create the preimage for.
  • num_triangles (int) – Number of triangles in the triangulation \(\mathcal{T}\) on which the continuous piecewise polynomials associated with the local-to-global map are defined.
  • num_dofs (int) – Number of degrees of freedom (dimension) for the space of continuous piecewise polynomials.
  • r (int) – Polynomial degree for the continuous piecewise polynomials.
  • m (int) – Dimension of the domain of the continuous piecewise polynomials.
Returns:

Preimage of the local-to-global map.

Return type:

Callable \(\operatorname{preim}_{\tau}(A)\).

generate_vector_valued_local_to_global_map(triangles, r, n, boundary_simplices=None, keep_boundary_dofs_last=False, ordering='interleaved')[source]

Generate a local-to-global map \(\tau\) for the space of vector valued continuous piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, C \mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\) or \(C \mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\).

We have \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \times \{ 0, 1, \ldots, n - 1\} \to \mathbb{N}_0 \cup \{ -1 \}\).

A local-to-global map maps a local polynomial basis function on a simplex in the triangulation to a global basis index for a basis for the space of vector valued continuous piecewise polynomials. The value -1 is used to indicate that a local basis function has no corresponding global basis function (see later).

Let \(d = \dim \mathcal{P}_r (\Delta_c^m, \mathbb{R}^n)\), and let the triangles in \(\mathcal{T}\) be enumerated so that we have \(\mathcal{T} = \{ T_0, T_1, \ldots, T_{| \mathcal{T} | - 1} \}\). Given a basis \(\{ \bar{\varphi}_{\nu, i} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r \\ i \in \{ 0, 1, \ldots, n - 1 \}}}\) for \(\mathcal{P}_r (\Delta_c^m, \mathbb{R}^n)\), the functions \(\{ \varphi_{\nu, j, i} = \bar{\varphi}_{\nu, i} \circ \Phi_{T_j}^{-1} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r \\ i \in \{ 0, 1, \ldots, n - 1 \}}}\) is a basis for \(\mathcal{P}_r(T_j, \mathbb{R}^n)\), where \(\Phi_{T_j}, j = 0, 1, \ldots, | \mathcal{T} | - 1\) is the unique affine map which maps the unit simplex \(\Delta_c^m\) to the simplex \(T_j\). From these local bases and the local-to-global map \(\tau\) a basis \(\{ \phi_i \}_{i = 1}^N\) for \(C \mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\) is constructed, with

\[\phi_i(x) = \sum_{(j, \nu, k) \in \operatorname{preim}_{\tau}(i)} \chi_{T_j}(x) \cdot \varphi_{\nu, j, k}(x).\]

For each triangle \(T_j\) a basis function \(\phi_i\) either agrees with a basis function \(\varphi_{\nu, j, k}\) or it’s zero. I.e. the set \(v_j = \{ (l, \nu, k) \in \operatorname{preim}_{\tau}(i) \big| l = j \}\) either contains zero or one element.

\[\begin{split}\phi_i(x) = \begin{cases} \bar{\varphi}_{\nu, k} \circ \Phi_{T_j}^{-1}(x), & (j, \nu, k) \text{ unique element in } v_j \\ 0, & v_j \text{ is empty} \end{cases}.\end{split}\]

Optionally a set of boundary simplices can be prescribed where the piecewise polynomial function should vanish. This is achieved by associating an invalid global index (-1) for all local basis functions supported on any of the boundary simplices). Alternatively local basis functions supported on boundary simplices can be associated with global indices placed last in the enumeration of global basis functions (by setting the keep_boundary_dofs_last to true).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the continuous piecewise polynomial functions.
  • n (int) – Dimension of the target.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for an element of \(C\mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last makes \(C\mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\) a subset of \(C\mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\) in a practical way.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

Tuple containing the local to global map \(\tau\) and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[Callable \(\tau(j, \nu, k)\), int, Optional[int]].

generate_vector_valued_local_to_global_preimage_map(tau, num_triangles, num_dofs, r, m, n)[source]

Generate the preimage map of a local-to-global map.

Let \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \times \{ 0, 1, \ldots, n - 1\} \to \mathbb{N}_0 \cup \{ -1 \}\) be a local-to-global map (see generate_vector_valued_local_to_global_map()). Then the preimage map of \(\tau\) is the map

\[\operatorname{preim}_{\tau} : \mathcal{P} \mathbb{N}_0 \to \mathcal{P} \left( \{ 0, 1, \ldots, |\mathcal{T}| - 1 \} \times \mathbb{N}_0^N \times \{ 0, 1, \ldots, n - 1 \} \right)\]

such that

\[\tau(j, \nu, k) = i \quad \forall \, (j, \nu, k) \in \operatorname{preim}_{\tau}(\{i\}).\]
Parameters:
  • tau (Callable \(\tau(j, \nu, k)\)) – Local-to-global map that we want to create the preimage for.
  • num_triangles (int) – Number of triangles in the triangulation \(\mathcal{T}\) on which the continuous piecewise polynomials associated with the local-to-global map are defined.
  • num_dofs (int) – Number of degrees of freedom (dimension) for the space of continuous piecewise polynomials.
  • r (int) – Polynomial degree for the continuous piecewise polynomials.
  • m (int) – Dimension of the domain of the continuous piecewise polynomials.
  • n (int) – Dimension of the domain of the continuous piecewise polynomials.
Returns:

Preimage of the local-to-global map.

Return type:

Callable \(\operatorname{preim}_{\tau}(A)\).

polynomials_on_simplices.piecewise_polynomial.continuous_piecewise_polynomial_bernstein_basis module

Lagrange finite elements (continuous piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\), i.e. elements of \(C\mathcal{P}_{r, 0} (\mathcal{T})\) or \(C\mathcal{P}_r (\mathcal{T})\), expressed using the Bernstein polynomial basis.

class ContinuousPiecewisePolynomialBernstein(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None, basis_polynomials=None)[source]

Bases: polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial_bernstein_basis.PiecewisePolynomialBernstein, polynomials_on_simplices.piecewise_polynomial.continuous_piecewise_polynomial.ContinuousPiecewisePolynomialBase

Implementation of the abstract continuous piecewise polynomial base class using the Bernstein polynomial basis on the unit simplex.

\[p(x) = \sum_{i = 1}^N a_i \phi_i(x),\]

where the basis \(\{ \phi_i \}_{i = 1}^N\) for the space of continuous piecewise polynomials is constructed from the Bernstein polynomial basis and the local-to-global map. See PiecewisePolynomialBase and generate_local_to_global_map() for details.

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the continuous piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the Bernstein basis for \(\mathcal{P}_r (\Delta_c^m)\) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function, in a way that makes sure that the piecewise polynomial is continuous. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial function should vanish (for an element of \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the continuous piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialBernsteinSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
static get_unit_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically one. See unit_continuous_piecewise_polynomial_bernstein().

weak_partial_derivative(i=0)[source]

Compute the i:th weak partial derivative of the continuous piecewise polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th weak partial derivative of this continuous piecewise polynomial.
Return type:PiecewisePolynomialBernstein.
continuous_piecewise_polynomial_bernstein_basis(triangles, vertices, r, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Generate all basis functions for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Return type:

List[ContinuousPiecewisePolynomialBernstein].

continuous_piecewise_polynomial_bernstein_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None, basis_polynomials=None)[source]

Generate a basis function for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • i (int) – Index of the basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialBernsteinSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
Returns:

Basis function.

Return type:

ContinuousPiecewisePolynomialBernstein.

dual_continuous_piecewise_polynomial_bernstein_basis(triangles, vertices, r, tau=None, num_dofs=None, tau_preim=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate all dual basis functions to the basis of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, C \mathcal{P}_r(\mathcal{T})\) or \(C \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Bernstein basis.

See dual_continuous_piecewise_polynomial_bernstein_basis_fn().

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_preim (Optional[Callable \(\operatorname{preim}_{\tau}(i)\)]) – Preimage of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

List of dual base functions.

Return type:

List[Callable \(\phi_i^*(p)\)].

dual_continuous_piecewise_polynomial_bernstein_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, tau_preim=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a dual basis function to the basis for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, C \mathcal{P}_r(\mathcal{T})\) or \(C \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Bernstein basis. I.e. the linear map \(\phi_i^* : C \mathcal{P}_r(\mathcal{T}) \to \mathbb{R}\) that satisfies

\[\phi_i^* (\phi_j) = \delta_{ij},\]

where \(\phi_j\) is the j:th Bernstein basis function for the space of continuous piecewise polynomials of degree r (see continuous_piecewise_polynomial_bernstein_basis_fn()).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • i (int) – Index of the dual basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_preim (Optional[Callable \(\operatorname{preim}_{\tau}(i)\)]) – Preimage of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

The i:th dual Bernstein basis function as specified by mu and r.

Return type:

Callable \(\phi_i^*(p)\).

unit_continuous_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically one.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The unit continuous piecewise polynomial.

Return type:

ContinuousPiecewisePolynomialBernstein.

zero_continuous_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically zero.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The zero piecewise polynomial.

Return type:

ContinuousPiecewisePolynomialBernstein.

polynomials_on_simplices.piecewise_polynomial.continuous_piecewise_polynomial_lagrange_basis module

Lagrange finite elements (continuous piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\), i.e. elements of \(C\mathcal{P}_{r, 0} (\mathcal{T})\) or \(C\mathcal{P}_r (\mathcal{T})\), expressed using the Lagrange polynomial basis.

class ContinuousPiecewisePolynomialLagrange(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None, basis_polynomials=None)[source]

Bases: polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial_lagrange_basis.PiecewisePolynomialLagrange, polynomials_on_simplices.piecewise_polynomial.continuous_piecewise_polynomial.ContinuousPiecewisePolynomialBase

Implementation of the abstract continuous piecewise polynomial base class using the Bernstein polynomial basis on the unit simplex.

\[p(x) = \sum_{i = 1}^N a_i \phi_i(x),\]

where the basis \(\{ \phi_i \}_{i = 1}^N\) for the space of continuous piecewise polynomials is constructed from the Lagrange polynomial basis and the local-to-global map. See PiecewisePolynomialBase and generate_local_to_global_map() for details.

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the continuous piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the Lagrange basis for \(\mathcal{P}_r (\Delta_c^m)\) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function, in a way that makes sure that the piecewise polynomial is continuous. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial function should vanish (for an element of \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the continuous piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialLagrangeSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
static get_unit_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically one. See unit_continuous_piecewise_polynomial_lagrange().

weak_partial_derivative(i=0)[source]

Compute the i:th weak partial derivative of the continuous piecewise polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th weak partial derivative of this continuous piecewise polynomial.
Return type:PiecewisePolynomialLagrange.
continuous_piecewise_polynomial_lagrange_basis(triangles, vertices, r, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Generate all basis functions for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Return type:

List[ContinuousPiecewisePolynomialLagrange].

continuous_piecewise_polynomial_lagrange_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None, basis_polynomials=None)[source]

Generate a basis function for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • i (int) – Index of the basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialLagrangeSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
Returns:

Basis function.

Return type:

ContinuousPiecewisePolynomialLagrange.

dual_continuous_piecewise_polynomial_lagrange_basis(triangles, vertices, r, tau=None, num_dofs=None, tau_preim=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate all dual basis functions to the basis of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, C \mathcal{P}_r(\mathcal{T})\) or \(C \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Lagrange basis.

See dual_continuous_piecewise_polynomial_lagrange_basis_fn().

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_preim (Optional[Callable \(\operatorname{preim}_{\tau}(i)\)]) – Preimage of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

List of dual base functions.

Return type:

List[Callable \(\phi_i^*(p)\)].

dual_continuous_piecewise_polynomial_lagrange_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, tau_preim=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a dual basis function to the basis for the space of continuous piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, C \mathcal{P}_r(\mathcal{T})\) or \(C \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Lagrange basis. I.e. the linear map \(\phi_i^* : C \mathcal{P}_r(\mathcal{T}) \to \mathbb{R}\) that satisfies

\[\phi_i^* (\phi_j) = \delta_{ij},\]

where \(\phi_j\) is the j:th Lagrange basis function for the space of continuous piecewise polynomials of degree r (see continuous_piecewise_polynomial_lagrange_basis_fn()).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the continuous piecewise polynomial.
  • i (int) – Index of the dual basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of continuous piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_preim (Optional[Callable \(\operatorname{preim}_{\tau}(i)\)]) – Preimage of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

The i:th dual Lagrange basis function as specified by mu and r.

Return type:

Callable \(\phi_i^*(p)\).

unit_continuous_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically one.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The unit continuous piecewise polynomial.

Return type:

ContinuousPiecewisePolynomialLagrange.

zero_continuous_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the continuous piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically zero.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the continuous piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the continuous piecewise polynomial functions should vanish (for \(C\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(C\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(C\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The zero piecewise polynomial.

Return type:

ContinuousPiecewisePolynomialLagrange.

polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial module

Functionality for discontinuous Galerkin finite elements (piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\).

The space of piecewise polynomials of degree r on \(\mathcal{T}, D\mathcal{P}_r (\mathcal{T}) = D\mathcal{P}_r (\mathcal{T}, \mathbb{R}^n) \subset L^2(\mathcal{T}, \mathbb{R}^n)\) is defined as

\[D\mathcal{P}_r (\mathcal{T}) = \{ v \in L^2(\mathcal{T}, \mathbb{R}^n) \big| v|_T \in \mathcal{P}_r(T, \mathbb{R}^n) \, \forall T \in \mathcal{T} \}.\]

Correspondingly the space of piecewise polynomials of degree r on \(\mathcal{T}\) which are zero on specified set of simplices or subsimplices of \(\mathcal{T}, D\mathcal{P}_{r, 0} (\mathcal{T}\) is defined as

\[D\mathcal{P}_{r, 0} (\mathcal{T}) = \{ v \in L^2_0(\mathcal{T}, \mathbb{R}^n) \big| v|_T \in \mathcal{P}_r(T, \mathbb{R}^n) \, \forall T \in \mathcal{T} \}.\]
class PiecewisePolynomialBase(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None)[source]

Bases: abc.ABC

Abstract base class for a piecewise polynomial function of degree r on a triangulation \(\mathcal{T}\), i.e. an element of \(D\mathcal{P}_r (\mathcal{T})\) or \(D\mathcal{P}_{r, 0} (\mathcal{T})\).

The domain dimension m = \(\dim \mathcal{T}\) and the target dimension n of the piecewise polynomial is given by the domain_dimension() and target_dimension() functions respectively.

The degree r of the piecewise polynomial is given by the degree() method.

Let \(N = \dim D\mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\). This value is given by the dimension() method.

Restriction to a simplex

On each simplex \(T \in \mathcal{T}\) the piecewise polynomial is given by a polynomial of degree r. This polynomial can be acquired using the restrict_to_simplex() method.

Basis

This class assumes that a basis \(\{ \bar{\varphi}_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) for \(\mathcal{P}_r (\Delta_c^m)\) has been chosen. From this bases \(\{ \varphi_{\nu, j} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) for \(\mathcal{P}_r (T_j, \mathbb{R}^n)\) are constructed by

\[\varphi_{\nu, j}(x) = (\bar{\varphi}_{\nu} \circ \Phi_{T_j}^{-1})(x),\]

where \(\Phi_{T_j}\) is the unique affine map which maps the unit simplex \(\Delta_c^m\) to the simplex \(T_j\) (the i:th vertex of the unit simplex is mapped to the i:th vertex of \(T_j\)).

This class also assumes that a local-to-global map \(\tau, \tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \to \mathbb{N}_0 \cup \{ -1 \}\) is available which maps local (simplex) basis functions to the index of the corresponding global basis function (or to -1 if the local basis function doesn’t correspond to a global basis function (which is the case if the piecewise polynomial has a fixed (prescribed) value on a (sub)simplex)). See generate_local_to_global_map(). Then a basis \(\{ \phi_i \}_{i = 1}^N\) is given by

\[\phi_i(x) = \sum_{(j, \nu) \in \operatorname{preim}_{\tau}(i)} \chi_{T_j}(x) \cdot \varphi_{\nu, j}(x),\]

And an arbitrary piecewise polynomial \(p(x)\) is given by specifying coefficients \(a_i \in \mathbb{R}^n, i = 1, 2, \ldots, N\) in front of each basis function.

\[p(x) = \sum_{i = 1}^N a_i \phi_i(x).\]

Hence the value of the piecewise polynomial function on a simplex \(T_j\) is given by

\[\begin{split}p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r \\ \tau(j, \nu) \neq -1}} a_{\tau(j, \nu)} \cdot \varphi_{\nu, j}(x), x \in T_j.\end{split}\]

The basis chosen for \(\mathcal{P}_r (\Delta_c^m)\) is given by the basis() method.

Algebraic structure

This class also defines the basic algebraic structures of the space of piecewise polynomials.

Ring structure:

Addition: \(+ : D\mathcal{P}_r (\mathcal{T}) \times D\mathcal{P}_r (\mathcal{T}) \to D\mathcal{P}_r (\mathcal{T}), (p_1 + p_2)(x) = p_1(x) + p_2(x)\).

Multiplication: \(\cdot : D\mathcal{P} (\mathcal{T}) \times D\mathcal{P} (\mathcal{T}) \to D\mathcal{P} (\mathcal{T}), (p_1 \cdot p_2)(x) = p_1(x) \cdot p_2(x)\) (only for n = 1).

Vector space structure:

Scalar multiplication: \(\cdot : \mathbb{R} \times D\mathcal{P}_r (\mathcal{T}) \to D\mathcal{P}_r (\mathcal{T}), (s \cdot p)(x) = s \cdot p(x)\).

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the basis for \(\mathcal{P}_r (\Delta_c^m)\) used (see basis()) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Callable \(\tau(j, \nu)\)) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial function should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\Delta_c^m)\) used to express the piecewise polynomial.

Returns:Unique identifier for the basis used.
Return type:str
degree()[source]

Get degree of each polynomial in the piecewise polynomial.

Returns:Polynomial degree.
Return type:int
degree_elevate(s)[source]

Express the piecewise polynomial using a higher degree polynomial basis.

Let \(p(x)\) be this piecewise polynomial. Let \(\{ \bar{\varphi}_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) be the polynomial basis for \(\mathcal{P}_r(\Delta_c^m)\) used for this piecewise polynomial, and let \(\{ \bar{\varphi}_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the corresponding higher degree polynomial basis for \(\mathcal{P}_s(\Delta_c^m)\). Then this function returns a piecewise polynomial \(q\) using the basis \(\{ \bar{\varphi}_{\nu, s} \}\) such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).

Parameters:s (int) – New degree for the basic polynomial basis the piecewise polynomial should use.
Returns:Elevation of this piecewise polynomial to the higher degree basis.
dimension()[source]

Get dimension of the space of piecewise polynomial the function belongs to.

Returns:Dimension of the function space.
Return type:int
domain_dimension()[source]

Get dimension of the domain of the piecewise polynomial.

Returns:Dimension of the domain of the piecewise polynomial.
Return type:int
evaluate_on_simplex(j, x)[source]

Evaluate the piecewise polynomial at a point \(x \in \mathbb{R}^m\) in the j:th simplex of the triangulation the piecewise polynomial is defined on.

In other words the polynomial at the j:th simplex is evaluated at the point x.

Parameters:
  • j (int) – Index of simplex where the piecewise polynomial should be evaluated.
  • x (float or length m Numpy array) – Point where the piecewise polynomial should be evaluated.
Returns:

Value of the piecewise polynomial.

Return type:

float or length n Numpy array.

static generate_local_to_global_map(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map \(\tau\) for the space of piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, D \mathcal{P}_r (\mathcal{T})\) or \(D \mathcal{P}_{r, 0} (\mathcal{T})\). See generate_local_to_global_map().

multiply_with_constant(c)[source]

Multiplication of this piecewise polynomial with a constant scalar or a vector (only if n = 1), self * c.

Parameters:c (Union[float, Numpy array]) – Scalar or vector we should multiply this polynomial with.
Returns:Product of this piecewise polynomial with the constant.
Return type:Instance of self.__class__
restrict_to_simplex(i)[source]

Restriction of the piecewise polynomial to a specified simplex \(T_i \in \mathcal{T}\).

Parameters:i (int) – Index of the simplex we want to restrict the piecewise polynomial to (in \(0, 1, \ldots, | \mathcal{T} | - 1\)).
Returns:Polynomial which agrees with the piecewise polynomial on the simplex \(T_i\).
support()[source]

Get the indices of the simplices in \(\mathcal{T}\) where this piecewise polynomial is non-zero.

Returns:Indices of simplices whose intersection with the support of this piecewise polynomial is non-empty.
Return type:Set[int]
target_dimension()[source]

Get dimension of the target of the piecewise polynomial.

Returns:Dimension of the target of the piecewise polynomial.
Return type:int
generate_inverse_local_to_global_map(tau, num_triangles, num_dofs, r, m)[source]

Generate the inverse of a local-to-global map (i.e. a global-to-local map).

Let \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \to \mathbb{N}_0 \cup \{ -1 \}\) be a local-to-global map for the space of piecewise polynomials of degree r on a triangulation (see generate_local_to_global_map()). Then this function generates the inverse of \(\tau\), i.e. the map \(\tau^{-1} : \mathbb{N}_0 \to \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m\) (\(\tau\) is not invertible where it has the value -1) such that

\[\tau(\tau^{-1} (i)) = i, \, i = 0, 1, \ldots, N - 1\]

where \(N\) is the number of degrees of freedom for the piecewise polynomial.

Parameters:
  • tau (Callable \(\tau(j, \nu)\)) – Local-to-global map that we want to invert.
  • num_triangles (int) – Number of triangles in the triangulation \(\mathcal{T}\) on which the piecewise polynomials associated with the local-to-global map are defined.
  • num_dofs (int) – Number of degrees of freedom (dimension) for the space of piecewise polynomials.
  • r (int) – Polynomial degree for the piecewise polynomials.
  • m (int) – Dimension of the domain of the piecewise polynomials.
Returns:

Inverse local-to-global map.

Return type:

Callable \(\tau^{-1}(i)\).

generate_inverse_vector_valued_local_to_global_map(tau, num_triangles, num_dofs, r, m, n)[source]

Generate the inverse of a local-to-global map (i.e. a global-to-local map).

Let \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \times \{ 0, 1, \ldots, n - 1 \} \to \mathbb{N}_0 \cup \{ -1 \}\) be a local-to-global map for the space of vector valued piecewise polynomials of degree r on a triangulation (see generate_vector_valued_local_to_global_map()). Then this function generates the inverse of \(\tau\), i.e. the map \(\tau^{-1} : \mathbb{N}_0 \to \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \times \{ 0, 1, \ldots, n - 1 \}\) (\(\tau\) is not invertible where it has the value -1) such that

\[\tau(\tau^{-1} (i)) = i, \, i = 0, 1, \ldots, N - 1\]

where \(N\) is the number of degrees of freedom for the vector valued piecewise polynomial.

Parameters:
  • tau (Callable \(\tau(j, \nu, k)\)) – Local-to-global map that we want to invert.
  • num_triangles (int) – Number of triangles in the triangulation \(\mathcal{T}\) on which the piecewise polynomials associated with the local-to-global map are defined.
  • num_dofs (int) – Number of degrees of freedom (dimension) for the space of piecewise polynomials.
  • r (int) – Polynomial degree for the piecewise polynomials.
  • m (int) – Dimension of the domain of the piecewise polynomials.
  • n (int) – Dimension of the target of the piecewise polynomials.
Returns:

Inverse local-to-global map.

Return type:

Callable \(\tau^{-1}(i)\).

generate_local_to_global_map(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map \(\tau\) for the space of piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, D \mathcal{P}_r (\mathcal{T})\) or \(D \mathcal{P}_{r, 0} (\mathcal{T})\).

We have \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \to \mathbb{N}_0 \cup \{ -1 \}\).

A local-to-global map maps a local polynomial basis function on a simplex in the triangulation to a global basis index for a basis for the space of piecewise polynomials. The value -1 is used to indicate that a local basis function has no corresponding global basis function (see later).

Let \(d = \dim \mathcal{P}_r (\Delta_c^m)\), and let the triangles in \(\mathcal{T}\) be enumerated so that we have \(\mathcal{T} = \{ T_0, T_1, \ldots, T_{| \mathcal{T} | - 1} \}\). Given a basis \(\{ \bar{\varphi}_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) for \(\mathcal{P}_r (\Delta_c^m)\), the functions \(\{ \bar{\varphi}_{\nu} \circ \Phi_{T_j}^{-1} \}_{ \substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r}}\) is a basis for \(\mathcal{P}_r(T_j)\), where \(\Phi_{T_j}, j = 0, 1, \ldots, | \mathcal{T} | - 1\) is the unique affine map which maps the unit simplex \(\Delta_c^m\) to the simplex \(T_j\). From these local bases and the local-to-global map \(\tau\) a basis \(\{ \phi_i \}_{i = 1}^N\) for \(D \mathcal{P}_r (\mathcal{T})\) is constructed, with

\[\begin{split}\phi_{\tau(j, \nu)}(x) = \begin{cases} \bar{\varphi}_{\nu} \circ \Phi_{T_j}^{-1}(x), & x \in T_j \\ 0, & \text{else} \end{cases}.\end{split}\]

Optionally a set of boundary simplices can be prescribed where the piecewise polynomial function should vanish. This is achieved by associating an invalid global index (-1) for all local basis functions supported on any of the boundary simplices). Alternatively local basis functions supported on boundary simplices can be associated with global indices placed last in the enumeration of global basis functions (by setting the keep_boundary_dofs_last to true).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the piecewise polynomial functions.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

Tuple containing the local to global map \(\tau\) and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[Callable \(\tau(j, \nu)\), int, Optional[int]].

generate_local_to_global_map_as_dictionaries(triangles, r, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a local-to-global map for the space of piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}, D \mathcal{P}_r (\mathcal{T})\) or \(D \mathcal{P}_{r, 0} (\mathcal{T})\). For details see generate_local_to_global_map(). This function differs from that function in that a list of dictionaries (one for each triangle in the mesh) with multi-indices (second function argument) as keys and global DOF indices (function value) as values is returned instead of a callable, which might be preferable in some cases.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the piecewise polynomial functions.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

Tuple containing the local to global map dictionaries, one for each triangle, and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[List[Dict[Tuple[int], int]], int, Optional[int]]

generate_vector_valued_local_to_global_map(triangles, r, n, boundary_simplices=None, keep_boundary_dofs_last=False, ordering='interleaved')[source]

Generate a local-to-global map \(\tau\) for the space of vector valued piecewise polynomial functions of degree r on a triangulation \(\mathcal{T}\) with values in \(\mathbb{R}^n\), D mathcal{P}_r (mathcal{T}, mathbb{R}^n)` or \(D \mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\).

We have \(\tau : \{ 0, 1, \ldots, | \mathcal{T} | - 1 \} \times \mathbb{N}_0^m \times \{ 0, 1, \ldots, n - 1 \} \to \mathbb{N}_0 \cup \{ -1 \}\).

A local-to-global map maps a local polynomial basis function on a simplex in the triangulation to a global basis index for a basis for the space of vector valued piecewise polynomials. The value -1 is used to indicate that a local basis function has no corresponding global basis function (see later).

Let \(d = \dim \mathcal{P}_r (\Delta_c^m, \mathbb{R}^n)\), and let the triangles in \(\mathcal{T}\) be enumerated so that we have \(\mathcal{T} = \{ T_0, T_1, \ldots, T_{| \mathcal{T} | - 1} \}\). Given a basis \(\{ \bar{\varphi}_{\nu, i} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r \\ i \in \{ 0, 1, \ldots, n - 1 \}}}\) for \(\mathcal{P}_r (\Delta_c^m, \mathbb{R}^n)\), the functions \(\{ \bar{\varphi}_{\nu, i} \circ \Phi_{T_j}^{-1} \}_{\substack{\nu \in \mathbb{N}_0^m \\ | \nu | \leq r \\ i \in \{ 0, 1, \ldots, n - 1 \}}}\) is a basis for \(\mathcal{P}_r(T_j, \mathbb{R}^n)\), where \(\Phi_{T_j}, j = 0, 1, \ldots, | \mathcal{T} | - 1\) is the unique affine map which maps the unit simplex \(\Delta_c^m\) to the simplex \(T_j\). From these local bases and the local-to-global map \(\tau\) a basis \(\{ \phi_i \}_{i = 1}^N\) for \(D \mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\) is constructed, with

\[\begin{split}\phi_{\tau(j, \nu, k)}(x) = \begin{cases} \bar{\varphi}_{\nu, k} \circ \Phi_{T_j}^{-1}(x), & x \in T_j \\ 0, & \text{else} \end{cases}.\end{split}\]

Optionally a set of boundary simplices can be prescribed where the piecewise polynomial function should vanish. This is achieved by associating an invalid global index (-1) for all local basis functions supported on any of the boundary simplices). Alternatively local basis functions supported on boundary simplices can be associated with global indices placed last in the enumeration of global basis functions (by setting the keep_boundary_dofs_last to true).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh (num_triangles by n + 1 list of indices, where n is the dimension of each simplex).
  • r (int) – Polynomial degree for the piecewise polynomial functions.
  • n (int) – Dimension of the target.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T}, \mathbb{R}^n)\) as a subset of \(D\mathcal{P}_r (\mathcal{T}, \mathbb{R}^n)\) in a practical way.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

Tuple containing the local to global map \(\tau\) and the number of global basis functions. If keep_boundary_dofs_last is true then the number of global basis functions not supported on the boundary is also returned.

Return type:

Tuple[Callable \(\tau(j, \nu, k)\), int, Optional[int]].

piecewise_polynomials_equal(p1, p2, rel_tol=1e-09, abs_tol=1e-07)[source]

Check if two piecewise polynomials p1 and p2 are approximately equal by comparing their values on each simplex in the domain of the first piecewise polynomial.

For equality on a simplex the polynomials_equal_on_simplex() function is used.

Parameters:
  • p1 (Implementation of the PiecewisePolynomialBase interface) – First piecewise polynomial.
  • p2 (Callable p2(x)) – Second piecewise polynomial.
  • rel_tol (float) – Tolerance for the relative error. See math.isclose for details.
  • abs_tol (float) – Tolerance for the absolute error. See math.isclose for details.
Returns:

Whether or not the two piecewise polynomials are approximately equal.

Return type:

bool

polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial_bernstein_basis module

Discontinuous Galerkin finite elements (piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\), i.e. elements of \(D\mathcal{P}_{r, 0} (\mathcal{T})\) or \(D\mathcal{P}_r (\mathcal{T})\), expressed using the Bernstein polynomial basis.

class PiecewisePolynomialBernstein(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None, basis_polynomials=None)[source]

Bases: polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial.PiecewisePolynomialBase

Implementation of the abstract piecewise polynomial base class using the Bernstein polynomial basis on the unit simplex.

\[p(x) = \sum_{i = 1}^N a_i \phi_i(x),\]

where the basis \(\{ \phi_i \}_{i = 1}^N\) for the space of piecewise polynomials is constructed from the Bernstein polynomial basis and the local-to-global map. See PiecewisePolynomialBase and generate_local_to_global_map() for details.

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the Bernstein basis for \(\mathcal{P}_r (\Delta_c^m)\) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial function should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialBernsteinSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\Delta_c^m)\) used to express the piecewise polynomial.

Returns:Unique identifier for the basis used.
Return type:str
degree_elevate(s)[source]

Express the piecewise polynomial using a higher degree polynomial basis.

Let \(p(x)\) be this piecewise polynomial. Let \(\{ \bar{\varphi}_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) be the polynomial basis for \(\mathcal{P}_r(\Delta_c^m)\) used for this piecewise polynomial, and let \(\{ \bar{\varphi}_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the corresponding higher degree polynomial basis for \(\mathcal{P}_s(\Delta_c^m)\). Then this function returns a piecewise polynomial \(q\) using the basis \(\{ \bar{\varphi}_{\nu, s} \}\) such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).

Parameters:s (int) – New degree for the basic polynomial basis the piecewise polynomial should use.
Returns:Elevation of this piecewise polynomial to the higher degree basis.
Return type:Instance of self.__class__
static get_unit_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically one. See unit_piecewise_polynomial_bernstein().

restrict_to_simplex(i)[source]

Restriction of the piecewise polynomial to a specified simplex \(T_i \in \mathcal{T}\).

Parameters:i (int) – Index of the simplex we want to restrict the piecewise polynomial to (in \(0, 1, \ldots, | \mathcal{T} | - 1\)).
Returns:Polynomial which agrees with the piecewise polynomial on the simplex \(T_i\).
Return type:PolynomialBernsteinSimplex.
dual_piecewise_polynomial_bernstein_basis(triangles, vertices, r, tau=None, num_dofs=None, tau_inv=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate all dual basis functions to the basis of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, D \mathcal{P}_r(\mathcal{T})\) or \(D \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Bernstein basis.

See dual_piecewise_polynomial_bernstein_basis_fn().

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_inv (Optional[Callable \(\tau^{-1}(i)\)]) – Inverse of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

List of dual base functions.

Return type:

List[Callable \(\phi_i^*(p)\)].

dual_piecewise_polynomial_bernstein_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, tau_inv=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a dual basis function to the basis for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, D \mathcal{P}_r(\mathcal{T})\) or \(D \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Bernstein basis. I.e. the linear map \(\phi_i^* : D \mathcal{P}_r(\mathcal{T}) \to \mathbb{R}\) that satisfies

\[\phi_i^* (\phi_j) = \delta_{ij},\]

where \(\phi_j\) is the j:th Bernstein basis function for the space of piecewise polynomials of degree r (see piecewise_polynomial_bernstein_basis_fn()).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • i (int) – Index of the dual basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_inv (Optional[Callable \(\tau^{-1}(i)\)]) – Inverse of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

The i:th dual Bernstein basis function as specified by mu and r.

Return type:

Callable \(\phi_i^*(p)\).

piecewise_polynomial_bernstein_basis(triangles, vertices, r, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Generate all basis functions for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Return type:

List[PiecewisePolynomialBernstein].

piecewise_polynomial_bernstein_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None, basis_polynomials=None)[source]

Generate a basis function for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • i (int) – Index of the basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialBernsteinSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
Returns:

Basis function.

Return type:

PiecewisePolynomialBernstein.

unit_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically one.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The unit piecewise polynomial.

Return type:

PiecewisePolynomialBernstein.

zero_piecewise_polynomial_bernstein(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Bernstein basis, which is identically zero.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The zero piecewise polynomial.

Return type:

PiecewisePolynomialBernstein.

polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial_lagrange_basis module

Discontinuous Galerkin finite elements (piecewise polynomials) on a simplicial domain (triangulation) \(\mathcal{T}\), i.e. elements of \(D\mathcal{P}_{r, 0} (\mathcal{T})\) or \(D\mathcal{P}_r (\mathcal{T})\), expressed using the Lagrange polynomial basis.

class PiecewisePolynomialLagrange(coeff, triangles, vertices, r, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, support=None, bsp_tree=None, basis_polynomials=None)[source]

Bases: polynomials_on_simplices.piecewise_polynomial.piecewise_polynomial.PiecewisePolynomialBase

Implementation of the abstract piecewise polynomial base class using the Lagrange polynomial basis on the unit simplex.

\[p(x) = \sum_{i = 1}^N a_i \phi_i(x),\]

where the basis \(\{ \phi_i \}_{i = 1}^N\) for the space of piecewise polynomials is constructed from the Lagrange polynomial basis and the local-to-global map. See PiecewisePolynomialBase and generate_local_to_global_map() for details.

Parameters:
  • coeff (List[Union[Scalar, Vector]]) – Coefficients for the piecewise polynomial in the \(\{ \phi_i \}_{i = 1}^N\) basis derived from the Lagrange basis for \(\mathcal{P}_r (\Delta_c^m)\) and the local-to-global map \(\tau\).
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial function should vanish (for an element of \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • support (Optional[Set[int]]) – Indices of the triangles in the triangulation where the piecewise polynomial is supported. Will be generated if not supplied.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialLagrangeSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\Delta_c^m)\) used to express the piecewise polynomial.

Returns:Unique identifier for the basis used.
Return type:str
degree_elevate(s)[source]

Express the piecewise polynomial using a higher degree polynomial basis.

Let \(p(x)\) be this piecewise polynomial. Let \(\{ \bar{\varphi}_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) be the polynomial basis for \(\mathcal{P}_r(\Delta_c^m)\) used for this piecewise polynomial, and let \(\{ \bar{\varphi}_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the corresponding higher degree polynomial basis for \(\mathcal{P}_s(\Delta_c^m)\). Then this function returns a piecewise polynomial \(q\) using the basis \(\{ \bar{\varphi}_{\nu, s} \}\) such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).

Parameters:s (int) – New degree for the basic polynomial basis the piecewise polynomial should use.
Returns:Elevation of this piecewise polynomial to the higher degree basis.
Return type:Instance of self.__class__
static get_unit_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically one. See unit_piecewise_polynomial_lagrange().

restrict_to_simplex(i)[source]

Restriction of the piecewise polynomial to a specified simplex \(T_i \in \mathcal{T}\).

Parameters:i (int) – Index of the simplex we want to restrict the piecewise polynomial to (in \(0, 1, \ldots, | \mathcal{T} | - 1\)).
Returns:Polynomial which agrees with the piecewise polynomial on the simplex \(T_i\).
Return type:PolynomialLagrangeSimplex.
dual_piecewise_polynomial_lagrange_basis(triangles, vertices, r, tau=None, num_dofs=None, tau_inv=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate all dual basis functions to the basis of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, D \mathcal{P}_r(\mathcal{T})\) or \(D \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Lagrange basis.

See dual_piecewise_polynomial_lagrange_basis_fn().

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_inv (Optional[Callable \(\tau^{-1}(i)\)]) – Inverse of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

List of dual base functions.

Return type:

List[Callable \(\phi_i^*(p)\)].

dual_piecewise_polynomial_lagrange_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, tau_inv=None, boundary_simplices=None, keep_boundary_dofs_last=False)[source]

Generate a dual basis function to the basis for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}, D \mathcal{P}_r(\mathcal{T})\) or \(D \mathcal{P}_{r, 0}(\mathcal{T})\), where the polynomials on each simplex is expressed in the Lagrange basis. I.e. the linear map \(\phi_i^* : D \mathcal{P}_r(\mathcal{T}) \to \mathbb{R}\) that satisfies

\[\phi_i^* (\phi_j) = \delta_{ij},\]

where \(\phi_j\) is the j:th Lagrange basis function for the space of piecewise polynomials of degree r (see piecewise_polynomial_lagrange_basis_fn()).

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • i (int) – Index of the dual basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • tau_inv (Optional[Callable \(\tau^{-1}(i)\)]) – Inverse of the local-to-global map. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
Returns:

The i:th dual Lagrange basis function as specified by mu and r.

Return type:

Callable \(\phi_i^*(p)\).

piecewise_polynomial_lagrange_basis(triangles, vertices, r, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Generate all basis functions for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Return type:

List[PiecewisePolynomialLagrange].

piecewise_polynomial_lagrange_basis_fn(triangles, vertices, r, i, tau=None, num_dofs=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None, basis_polynomials=None)[source]

Generate a basis function for the space of piecewise polynomials of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • r (int) – Degree of each polynomial in the piecewise polynomial.
  • i (int) – Index of the basis function that should be generated.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • num_dofs (Optional[int]) – Dimension of the space of piecewise polynomials on the given triangulation. Will be computed if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
  • basis_polynomials (Optional[Dict[int, List[ PolynomialLagrangeSimplex]]]) – Precomputed basis polynomials for each triangle in the given triangulation. Will be generated where necessary if not supplied.
Returns:

Basis function.

Return type:

PiecewisePolynomialLagrange.

unit_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically one.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The unit piecewise polynomial.

Return type:

PiecewisePolynomialLagrange.

zero_piecewise_polynomial_lagrange(triangles, vertices, n=1, tau=None, boundary_simplices=None, keep_boundary_dofs_last=False, bsp_tree=None)[source]

Get the piecewise polynomial of degree r on the given triangulation \(\mathcal{T}\), where the polynomials on each simplex is expressed in the Lagrange basis, which is identically zero.

Parameters:
  • triangles – Triangles (or in general simplices) in the mesh \(\mathcal{T}\) (num_triangles by m + 1 array of indices).
  • vertices – Vertices in the mesh \(\mathcal{T}\) (num_vertices by m array of scalars).
  • n (int) – Dimension of the target of the piecewise polynomial.
  • tau (Optional[Callable \(\tau(j, \nu)\)]) – Local-to-global map for mapping local basis functions to the index of the corresponding global basis function. Will be generated if not supplied.
  • boundary_simplices (List[List[int]]) – List of simplices or subsimplices on which the piecewise polynomial functions should vanish (for \(D\mathcal{P}_{r, 0} (\mathcal{T})\)) or which should be treated separately (if keep_boundary_dofs_last is set to True). Each simplex or subsimplex is specified as a list of vertex indices of the vertices that form the simplex.
  • keep_boundary_dofs_last (bool) – Whether or not to collect all global basis functions associated with any boundary simplex last in the enumeration of all basis functions. Enumerating basis functions associated with boundary simplices last is useful for handling \(D\mathcal{P}_{r, 0} (\mathcal{T})\) as a subset of \(D\mathcal{P}_r (\mathcal{T})\) in a practical way.
  • bsp_tree – Optional implementation detail. A binary space partitioning tree built around the triangulation \(\mathcal{T}\) for quicker lookup of triangle a point lies in. Will be generated if not supplied.
Returns:

The zero piecewise polynomial.

Return type:

PiecewisePolynomialLagrange.

polynomials_on_simplices.piecewise_polynomial.plot_continuous_piecewise_polynomial module

Functionality for plotting Lagrange finite elements (continuous piecewise polynomials) on a simplicial domain (triangulation).

plot_bivariate_continuous_piecewise_polynomial(p, *args, **kwargs)[source]

Plot a bivariate scalar valued continuous piecewise polynomial.

Parameters:
  • p (Implementation of ContinuousPiecewisePolynomialBase) – Continuous piecewise polynomial that we want to plot.
  • args – Additional arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘edge_resolution’: Number of discrete points in the plot along each edge in the mesh the piecewise polynomial is defined on. Further keyword arguments are passed to the plot_curve command.
polynomials_on_simplices.piecewise_polynomial.plot_piecewise_polynomial module

Functionality for plotting discontinuous Galerkin finite elements (piecewise polynomials) on a simplicial domain (triangulation).

plot_bivariate_piecewise_polynomial(p, *args, **kwargs)[source]

Plot a bivariate scalar valued piecewise polynomial.

Parameters:
  • p (Implementation of PiecewisePolynomialBase) – Piecewise polynomial that we want to plot.
  • args – Additional arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘edge_resolution’: Number of discrete points in the plot along each edge in the mesh the piecewise polynomial is defined on. Further keyword arguments are passed to the plot_curve command.
plot_univariate_piecewise_polynomial(p, *args, **kwargs)[source]

Plot a univariate scalar valued piecewise polynomial.

Parameters:
  • p (Implementation of PiecewisePolynomialBase) – Piecewise polynomial that we want to plot.
  • args – Additional arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘edge_resolution’: Number of discrete points in the plot on each line in the mesh the piecewise polynomial is defined on. Further keyword arguments are passed to the plot_curve command.
Module contents
polynomials_on_simplices.polynomial package
Subpackages
polynomials_on_simplices.polynomial.code_generation package
Submodules
polynomials_on_simplices.polynomial.code_generation.generate_barycentric_polynomial_functions_simplex module

Functionality for generating Python code used to evaluate barycentric polynomials.

generate_barycentric_basis(m, r, str_type='python')[source]

Generate string representation for all barycentric base polynomials for the space \(\mathcal{P}_r(\Delta_c^m)\).

Parameters:
  • m (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
  • str_type – What kind of strings should be generated. Can be either “python” or “latex”.
Returns:

List of codes for evaluating each of the base polynomials.

Return type:

List[str]

generate_barycentric_basis_fn(nu, r, str_type='python')[source]

Generate string representation for a barycentric basis polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), \(p_{\nu, r} (x) = x^{\nu} (1 - |x|)^{r - |\nu|}\), where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which barycentric basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • str_type – What kind of string should be generated. Can be either “python” or “latex”.
Returns:

Python code for evaluating the barycentric base polynomial as specified by nu and r.

Return type:

str

Examples

>>> generate_barycentric_basis_fn(1, 2)
'x * (1 - x)'
>>> generate_barycentric_basis_fn((1, 1), 3)
'x[0] * x[1] * (1 - x[0] - x[1])'
>>> generate_barycentric_basis_fn((1, 1), 3, str_type="latex")
'x_1 x_2 (1 - x_1 - x_2)'
generate_function_eval_specific_scalar_valued(m, r, a, prettify_coefficients=False)[source]

Generate code for evaluating a specific scalar valued degree r polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), expressed in the barycentric basis.

\[ \begin{align}\begin{aligned}p_{\nu, r} : \Delta_c^m \to \mathbb{R},\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\Delta_c^m)) - 1} a_{\nu_i} x^{\nu_i} (1 - |x|)^{r - |\nu_i|},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[float]) – Coefficients for the polynomial in the barycentric basis for \(\mathcal{P}_r (\Delta_c^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
  • prettify_coefficients (bool) – Whether or not coefficients in the a array should be prettified in the generated code (e.g. converting 0.25 -> 1 / 4).
Returns:

Python code for evaluating the barycentric base polynomial as specified by m, r and a.

Return type:

str

generate_function_eval_specific_vector_valued(m, r, a)[source]

Generate code for evaluating a specific vector valued degree r polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), expressed in the barycentric basis.

\[ \begin{align}\begin{aligned}p_{\nu, r} : \Delta_c^m \to \mathbb{R}^n, n > 1,\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\Delta_c^m)) - 1} a_{\nu_i} x^{\nu_i} (1 - |x|)^{r - |\nu_i|},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[n-dimensional vector]) – Coefficients for the polynomial in the barycentric basis for \(\mathcal{P}_r (\Delta_c^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the barycentric base polynomial as specified by m, r and a.

Return type:

str

generate_function_general(m, r)[source]

Generate code for evaluating a general degree r barycentric polynomial on the m-dimensional unit simplex.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\Delta_c^m)) - 1} a_{\nu_i} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
Returns:

Python code for evaluating the polynomial

Return type:

str

generate_function_specific(m, r, a)[source]

Generate code for evaluating the degree r barycentric polynomial on the m-dimensional unit simplex with given basis coefficients a.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\Delta_c^m)) - 1} a_{\nu_i} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
  • a (Union[Iterable[float], Iterable[n-dimensional vector]]) – Coefficients for the polynomial in the barycentric basis for \(\mathcal{P}_r (\Delta_c^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the polynomial

Return type:

str

polynomials_on_simplices.polynomial.code_generation.generate_lagrange_coefficients_unit_simplex_cache module

Functions used to generate the cache of coefficients for Lagrange basis polynomials found in polynomials_unit_simplex_lagrange_basis_cache.py.

polynomials_on_simplices.polynomial.code_generation.generate_lagrange_polynomial_functions_simplex module

Functionality for generating Python code used to evaluate Lagrange polynomials.

generate_function_eval_general_scalar_valued(m, r)[source]

Generate code for evaluating a general scalar valued degree r polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), expressed in the Lagrange basis.

\[p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i},\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
Returns:

Python code for evaluating a general degree r Lagrange polynomial on an m-dimensional domain.

Return type:

str

generate_function_eval_specific_scalar_valued(m, r, a, prettify_coefficients=False)[source]

Generate code for evaluating a specific scalar valued degree r polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), expressed in the Lagrange basis.

\[ \begin{align}\begin{aligned}p : \Delta_c^m \to \mathbb{R},\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[float]) – Coefficients for the polynomial in the Lagrange basis for \(\mathcal{P}_r (\mathbb{R}^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
  • prettify_coefficients (bool) – Whether or not coefficients in the a array should be prettified in the generated code (e.g. converting 0.25 -> 1 / 4).
Returns:

Python code for evaluating the Lagrange polynomial as specified by m, r and a.

Return type:

str

generate_function_eval_specific_vector_valued(m, r, a)[source]

Generate code for evaluating a specific vector valued degree r polynomial on the m-dimensional unit simplex (\(\Delta_c^m\)), expressed in the Lagrange basis.

\[ \begin{align}\begin{aligned}p : \Delta_c^m \to \mathbb{R}^n, n > 1,\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[n-dimensional vector]) – Coefficients for the polynomial in the Lagrange basis for \(\mathcal{P}_r (\mathbb{R}^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the Lagrange base polynomial as specified by m, r and a.

Return type:

str

generate_function_general(m, r)[source]

Generate code for evaluating a general degree r Lagrange polynomial on the m-dimensional unit simplex.

\[l(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} l_{\nu, r}(x),\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
Returns:

Python code for evaluating the polynomial

Return type:

str

generate_function_specific(m, r, a)[source]

Generate code for evaluating the degree r Lagrange polynomial on an m-dimensional domain with given basis coefficients a.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} l_{\nu, r}(x),\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
  • a (Union[Iterable[float], Iterable[n-dimensional vector]]) – Coefficients for the polynomial in the Lagrange basis for \(\mathcal{P}_r (\Delta_c^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the polynomial

Return type:

str

generate_lagrange_basis(m, r)[source]

Generate code for evaluating all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^m)\).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
Returns:

List of codes for evaluating each of the base polynomials.

Return type:

List[str]

generate_lagrange_basis_fn(nu, r)[source]

Generate code for evaluating a Lagrange basis polynomial on the m-dimensional unit simplex, where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial code should be generated for.
  • r (int) – Degree of polynomial.
Returns:

Python code for evaluating the Lagrange base polynomial as specified by nu.

Return type:

str

Examples

>>> generate_lagrange_basis_fn(1, 1)
'x'
>>> generate_lagrange_basis_fn((1, 1), 2)
'4 * x[0] * x[1]'
polynomials_on_simplices.polynomial.code_generation.generate_monomial_polynomial_functions module

Functionality for generating Python code used to evaluate monomial polynomials.

generate_function_eval_specific_scalar_valued(m, r, a, prettify_coefficients=False)[source]

Generate code for evaluating a specific scalar valued degree r polynomial on an m-dimensional domain, expressed in the monomial basis.

\[ \begin{align}\begin{aligned}p : \mathbb{R}^m \to \mathbb{R},\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[float]) – Coefficients for the polynomial in the monomial basis for \(\mathcal{P}_r (\mathbb{R}^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
  • prettify_coefficients (bool) – Whether or not coefficients in the a array should be prettified in the generated code (e.g. converting 0.25 -> 1 / 4).
Returns:

Python code for evaluating the monomial base polynomial as specified by m, r and a.

Return type:

str

generate_function_eval_specific_vector_valued(m, r, a)[source]

Generate code for evaluating a specific vector valued degree r polynomial on an m-dimensional domain, expressed in the monomial basis.

\[ \begin{align}\begin{aligned}p : \mathbb{R}^m \to \mathbb{R}^n, n > 1,\\p_{\nu, r}(x)=\sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i},\end{aligned}\end{align} \]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
  • a (Iterable[n-dimensional vector]) – Coefficients for the polynomial in the monomial basis for \(\mathcal{P}_r (\mathbb{R}^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the monomial base polynomial as specified by m, r and a.

Return type:

str

generate_function_general(m, r)[source]

Generate code for evaluating a general degree r monomial polynomial on an m-dimensional domain.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu},\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
Returns:

Python code for evaluating the polynomial

Return type:

str

generate_function_specific(m, r, a)[source]

Generate code for evaluating the degree r monomial polynomial on an m-dimensional domain with given basis coefficients a.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu},\]

where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
  • a (Union[Iterable[float], Iterable[n-dimensional vector]]) – Coefficients for the polynomial in the monomial basis for \(\mathcal{P}_r (\mathbb{R}^m)\). \(\text{a}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function).
Returns:

Python code for evaluating the polynomial

Return type:

str

generate_monomial_basis(m, r)[source]

Generate code for evaluating all monomial base polynomials for the space \(\mathcal{P}_r(\mathbb{R}^m)\).

Parameters:
  • m (int) – Dimension of the domain.
  • r (int) – Degree of the polynomial space.
Returns:

List of codes for evaluating each of the base polynomials.

Return type:

List[str]

generate_monomial_basis_fn(nu)[source]

Generate code for evaluating a monomial basis polynomial on an m-dimensional domain, \(p_{\nu} (x) = x^{\nu}\), where m is equal to the length of nu.

Parameters:nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which monomial basis polynomial code should be generated for.
Returns:Python code for evaluating the monomial base polynomial as specified by nu.
Return type:str

Examples

>>> generate_monomial_basis_fn(0)
'1'
>>> generate_monomial_basis_fn(1)
'x'
>>> generate_monomial_basis_fn((1, 1))
'x[0] * x[1]'
polynomials_on_simplices.polynomial.code_generation.generate_polynomial_functions module

Generic functionality for generating Python code used to evaluate polynomials.

generate_docstring(m, r, basis, latex_str)[source]

Generate a docstring for a general function evaluating a polynomial given polynomial coefficients and a point of evaluation.

Parameters:
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space.
  • basis (str) – Name for the polynomial basis used.
  • latex_str (str) – Latex str for the calculation done by the function.
Returns:

Docstring for the function.

Return type:

str

generate_function_specific_name(a)[source]

Generate name for a general function evaluating a polynomial.

Parameters:a (Iterable[float]) – Coefficients for the polynomial used to generate a unique name.
Returns:Name for the function.
Return type:str
Module contents
Submodules
polynomials_on_simplices.polynomial.polynomials_base module

Abstract base class and basic functionality for polynomials.

class PolynomialBase(coeff, r=None, m=1)[source]

Bases: abc.ABC

Abstract base class for a polynomial. The space of polynomials \(\mathcal{P} = \mathcal{P}(\mathbb{R}^m, \mathbb{R}^n)\) is defined as

\[\mathcal{P} = \{ p : \mathbb{R}^m \to \mathbb{R}^n | p(x) = \sum_{\nu} a_{\nu} x^{\nu}, \nu \in \mathbb{N}_0^m, a_{\nu} \in \mathbb{R}^n \}.\]

The domain dimension m and the target dimension n of the polynomial is given by the domain_dimension() and target_dimension() functions respectively.

For a computable polynomial we must have \(a_{\nu} = 0\) for all but finitely many \(\nu\), and then the degree of \(p\) is defined as \(r = \deg{p} = \max_{\nu : a_{\nu} \neq 0} |\nu|\). The degree is given by the degree() method.

There are many common bases for the space of polynomials. The basis used for a specific polynomial is given by the basis() method.

This class also defines the basic algebraic and differentiable structures of the space of polynomials.

Ring structure:

Addition: \(+ : \mathcal{P} \times \mathcal{P} \to \mathcal{P}, (p_1 + p_2)(x) = p_1(x) + p_2(x)\).

Multiplication: \(\cdot : \mathcal{P} \times \mathcal{P} \to \mathcal{P}, (p_1 \cdot p_2)(x) = p_1(x) \cdot p_2(x)\).

Vector space structure:

Scalar multiplication: \(\cdot : \mathbb{R} \times \mathcal{P} \to \mathcal{P}, (s \cdot p)(x) = s \cdot p(x)\).

Differentiable structure:

i:th partial derivative: \(\partial_i : \mathcal{P} \to \mathcal{P}, (\partial_i p)(x) = \frac{\partial p(x)}{\partial x^i}\).

Parameters:
  • coeff – Coefficients for the polynomial in the chosen basis for \(\mathcal{P}_r (\mathbb{R}^m)\) (see basis()). If p is expressed in the chosen basis \(\{ b_{\nu, r} \}\) as \(p(x) = \sum_{\nu} a_{\nu} b_{\nu, r}(x)\) then \(\text{coeff}[i] = a_{\nu(i)}\), where \(\nu(i)\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
  • m (int) – Dimension of the domain of the polynomial.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree()[source]

Get degree of the polynomial.

Returns:Polynomial degree.
Return type:int
degree_elevate(s)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x)\) be this polynomial, where \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is the chosen basis for \(\mathcal{P}_r (\mathbb{R}^m)\). Let \(\{ b_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the corresponding basis for \(\mathcal{P}_s (\mathbb{R}^m)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} b_{\nu, s}(x),\end{split}\]

such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
domain_dimension()[source]

Get dimension of the polynomial domain.

Returns:Dimension of the domain of the polynomial.
Return type:int
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
multiply_with_constant(c)[source]

Multiplication of this polynomial with a constant scalar or a vector (only for a scalar valued polynomial), self * c.

Parameters:c (Union[float, Numpy array]) – Scalar or vector we should multiply this polynomial with.
Returns:Product of this polynomial with the constant.
Return type:Instance of self.__class__
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
target_dimension()[source]

Get dimension of the polynomial target.

Returns:Dimension of the target of the polynomial.
Return type:int
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
class PolynomialComponentsIterator(p)[source]

Bases: object

Iterator for iterating over the components of a vector valued polynomial.

Parameters:p (Instance of PolynomialBase) – Vector valued polynomial.
get_degree_from_dimension(dim, n)[source]

Get the maximum polynomial degree r for a polynomial in the space of all polynomials on an n-dimensional domain with given dimension. In a sense this is the inverse of the get_dimension() function.

Parameters:
  • dim (int) – Dimension of the polynomial space.
  • n (int) – Dimension of the domain.
Returns:

Maximum polynomial degree.

Return type:

int

get_dimension(r, n)[source]

Get the dimension of the space of polynomials of degree <= r on an n-dimensional domain.

Parameters:
  • r (int) – Maximum polynomial degree.
  • n (int) – Dimension of the domain.
Returns:

Dimension of the space of polynomials (number of basis functions needed to span the space).

Return type:

int

polynomials_equal(p1, p2, r, m, rel_tol=1e-09, abs_tol=1e-07)[source]

Check if two polynomials p1 and p2 are approximately equal.

For scalar valued polynomials, the two polynomials are considered equal if

math.isclose(p1(xi), p2(xi), rel_tol=rel_tol, abs_tol=abs_tol)

is true for a set of random points \(\{ x_i \}_{i = 0}^{d - 1}\) from the m-dimensional unit cube, where \(d\) is the dimension of the polynomial space p1 and p2 belongs to (as given by the get_dimension() function).

For vector valued polynomials the same check is done component wise.

Parameters:
  • p1 (Callable p1(x)) – First polynomial.
  • p2 (Callable p2(x)) – Second polynomial.
  • r (int) – Degree of the polynomials.
  • m (int) – Dimension of the domain of the polynomials.
  • rel_tol (float) – Tolerance for the relative error. See math.isclose for details.
  • abs_tol (float) – Tolerance for the absolute error. See math.isclose for details.
Returns:

Whether or not the two polynomials are approximately equal.

Return type:

bool

polynomials_on_simplices.polynomial.polynomials_monomial_basis module

Polynomials on \(\mathbb{R}^m\) with values in \(\mathbb{R}^n\), expressed using the monomial basis.

An monomial in n variables of degree r is a product

\[m = x_1^{d_1} x_2^{d_2} \ldots x_n^{d_n},\]

where \(d_i \mathbb{N}_0, i = 1, 2, \ldots, n\) and \(\sum_{i = 1}^n d_i = r\). I.e. \(d = (d_1, d_2, \ldots, d_n) \in \mathbb{N}_0^n\) and in multi-index notation we simply have

\[m = x^d, x = (x_1, x_2, \ldots, x_n).\]

The set \(\{ x^{\nu} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is a basis for the space of all polynomials of degree less than or equal to r on \(\mathbb{R}^m, \mathcal{P}_r (\mathbb{R}^m)\).

\[\begin{split}p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} x^{\nu},\end{split}\]

where \(a_{\nu} \in \mathbb{R}^n\).

class Polynomial(coeff, r=None, m=1)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_base.PolynomialBase

Implementation of the abstract polynomial base class for a polynomial using the monomial basis.

\[p(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} x^{\nu_i}.\]
Parameters:
  • coeff – Coefficients for the polynomial in the monomial basis for \(\mathcal{P}_r (\mathbb{R}^m, \mathbb{R}^n). \text{coeff}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
  • m (int) – Dimension of the domain of the polynomial.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree_elevate(s)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} x^{\nu}\) be this polynomial expressed in the monomial basis for \(\mathcal{P}_r (\mathbb{R}^m)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} x^{\nu},\end{split}\]

such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
Return type:Polynomial.
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
Return type:Polynomial.
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
Return type:Polynomial.
dual_monomial_basis(r, n)[source]

Generate all dual monomial base functions for the space \(\mathcal{P}_r(\mathbb{R}^n)\) (i.e. the monomial basis for \(\mathcal{P}_r(\mathbb{R}^n)^*\)).

See dual_monomial_basis_fn().

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the domain.
Returns:

List of dual base functions.

Return type:

List[callable q(p)].

dual_monomial_basis_fn(mu)[source]

Generate a dual basis function to the monomial polynomial basis, i.e. the linear map \(q_{\mu} : \mathcal{P}_r(\mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu}(p_{\nu}) = \delta_{\mu, \nu},\]

where \(p_{\nu}\) is the monomial basis polynomial indexed by the multi-index \(\nu\), \(n = |\nu|\) (see monomial_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:mu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which dual monomial basis function should be generated.
Returns:The dual monomial basis function as specified by mu.
Return type:Callable \(q_{\mu}(p)\).
dual_vector_valued_monomial_basis(r, m, n, ordering='interleaved')[source]

Generate all dual monomial base functions for the space \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\) (i.e. the monomial basis for \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)^*\)).

See dual_vector_valued_monomial_basis_fn().

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the domain.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(p)].

dual_vector_valued_monomial_basis_fn(mu, i, n)[source]

Generate a dual basis function to the vector valued monomial polynomial basis, i.e. the linear map \(q_{\mu, i} : \mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, i}(p_{\nu, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(p_{\nu, j}\) is the degree \(|\nu|\) vector valued monomial basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component (see vector_valued_monomial_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual monomial basis function should be generated.
  • i (int) – Integer indicating which dual monomial basis function should be generated.
  • n (int) – Dimension of the target.
Returns:

The dual monomial basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, i}(p)\).

monomial_basis(r, n)[source]

Generate all monomial base polynomials for the space \(\mathcal{P}_r(\mathbb{R}^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the domain.
Returns:

List of base polynomials.

Return type:

List[Polynomial].

monomial_basis_fn(nu)[source]

Generate a monomial basis polynomial \(p_{\nu}\) in the space \(\mathcal{P}_r(\mathbb{R}^n)\), where \(r = |\nu|\) and n is equal to the length of nu.

\[p_{\nu}(x) = x^{\nu}.\]
Parameters:nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which monomial basis polynomial should be generated. Gives the exponent for each x_i term.
Returns:The monomial base polynomial as specified by nu.
Return type:Polynomial.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> monomial_basis_fn(1)(x1)
x1
>>> monomial_basis_fn(2)(x1)
x1**2
>>> monomial_basis_fn((1, 1))((x1, x2))
x1*x2
monomial_basis_fn_latex(nu)[source]

Generate Latex string for a monomial basis polynomial in the space \(\mathcal{P}_r(\mathbb{R}^n)\), where n is equal to the length of nu.

Parameters:nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which monomial basis polynomial we should generate Latex string for. Gives the exponent for each x_i term.
Returns:Latex string for the monomial base polynomial as specified by nu.
Return type:str

Examples

>>> monomial_basis_fn_latex(3)
'x^3'
>>> monomial_basis_fn_latex((1, 1, 0))
'x_1 x_2'
monomial_basis_fn_latex_compact(nu)[source]

Generate compact Latex string for a monomial basis polynomial in the space \(\mathcal{P}_r(\mathbb{R}^n)\), where n is equal to the length of nu.

Parameters:nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which monomial basis polynomial we should generate Latex string for. Gives the exponent for each x_i term.
Returns:Latex string for the monomial base polynomial as specified by nu.
Return type:str

Examples

>>> monomial_basis_fn_latex_compact(3)
'x^3'
>>> monomial_basis_fn_latex_compact((1, 1, 0))
'x^{(1, 1, 0)}'
monomial_basis_latex(r, n)[source]

Generate Latex strings for all monomial base polynomials for the space \(\mathcal{P}_r(\mathbb{R}^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the domain.
Returns:

List of Latex strings for each monomial base polynomial.

Return type:

List[str]

Examples

>>> monomial_basis_latex(2, 1)
['1', 'x', 'x^2']
>>> monomial_basis_latex(2, 2)
['1', 'x_1', 'x_1^2', 'x_2', 'x_1 x_2', 'x_2^2']
monomial_basis_latex_compact(r, n)[source]

Generate compact Latex strings for all monomial base polynomials for the space \(\mathcal{P}_r(\mathbb{R}^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the space.
Returns:

List of Latex strings for each monomial base polynomial.

Return type:

List[str]

Examples

>>> monomial_basis_latex_compact(2, 1)
['1', 'x', 'x^2']
>>> monomial_basis_latex_compact(2, 2)
['x^{(0, 0)}', 'x^{(1, 0)}', 'x^{(2, 0)}', 'x^{(0, 1)}', 'x^{(1, 1)}', 'x^{(0, 2)}']
unique_identifier_monomial_basis()[source]

Get unique identifier for the monomial polynomial basis.

Returns:Unique identifier.
Return type:str
unit_polynomial(r=0, m=1, n=1)[source]

Get the monomial polynomial \(p \in \mathcal{P}(\mathbb{R}^m, \mathbb{R}^n)\) which is identically one.

Parameters:
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
  • r (int) – The unit polynomial will be expressed in the monomial basis for \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\).
Returns:

The unit polynomial.

Return type:

Polynomial.

vector_valued_monomial_basis(r, m, n, ordering='interleaved')[source]

Generate all monomial base polynomials for the space \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the domain.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials.

Return type:

List[Polynomial].

vector_valued_monomial_basis_fn(nu, i, n)[source]

Generate a vector valued monomial basis polynomial \(p_{\nu, i}\) in the space \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\), where \(r = |\nu|\) and m is equal to the length of nu.

The vector valued basis polynomial is generated by specifying a scalar valued basis polynomial and the component of the vector valued basis polynomial that should be equal to the scalar valued basis polynomial. All other components of the vector valued basis polynomial will be zero, i.e.

\[\begin{split}p_{\nu, i}^j (x) = \begin{cases} p_{\nu} (x), & i = j \\ 0, & \text{else} \end{cases}.\end{split}\]
Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which scalar valued monomial basis polynomial should be generated for the non-zero component.
  • i (int) – Index of the vector component that is non-zero.
  • n (int) – Dimension of the target.
Returns:

The monomial base polynomial as specified by nu, r, i and n.

Return type:

Polynomial.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> vector_valued_monomial_basis_fn(0, 0, 2)(x1)
array([1, 0])
>>> vector_valued_monomial_basis_fn(1, 1, 2)(x1)
array([0, x1], dtype=object)
>>> vector_valued_monomial_basis_fn((1, 0), 0, 2)((x1, x2))
array([x1, 0], dtype=object)
>>> vector_valued_monomial_basis_fn((1, 1), 1, 3)((x1, x2))
array([0, x1*x2, 0], dtype=object)
zero_polynomial(r=0, m=1, n=1)[source]

Get the monomial polynomial \(p \in \mathcal{P}(\mathbb{R}^m, \mathbb{R}^n)\) which is identically zero.

Parameters:
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
  • r (int) – The zero polynomial will be expressed in the monomial basis for \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\).
Returns:

The zero polynomial.

Return type:

Polynomial.

polynomials_on_simplices.polynomial.polynomials_simplex_base module

Abstract base class and basic functionality for polynomials defined on a simplex.

class PolynomialSimplexBase(coeff, vertices, r=None)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_base.PolynomialBase, abc.ABC

Abstract base class for a polynomial defined on an m-dimensional simplex T.

Parameters:
  • coeff – Coefficients for the polynomial in the chosen basis for \(\mathcal{P}_r (T)\) (see basis()). If p is expressed in the chosen basis \(\{ b_{\nu, r} \}\) as \(p(x) = \sum_{\nu} a_{\nu} b_{\nu, r}(x)\) then \(\text{coeff}[i] = a_{\nu(i)}\), where \(\nu(i)\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
multiply_with_constant(c)[source]

Multiplication of this polynomial with a constant scalar or a vector (only for a scalar valued polynomial), self * c.

Parameters:c (Union[float, Numpy array]) – Scalar or vector we should multiply this polynomial with.
Returns:Product of this polynomial with the constant.
Return type:Instance of self.__class__
simplex_vertices()[source]

Get the vertices of the simplex T on which this polynomial is defined.

Returns:Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
polynomials_equal_on_simplex(p1, p2, r, vertices, rel_tol=1e-09, abs_tol=1e-07)[source]

Check if two polynomials p1 and p2 are approximately equal by comparing their values on a given n-dimensional simplex T.

For scalar valued polynomials, the two polynomials are considered equal if

math.isclose(p1(xi), p2(xi), rel_tol=rel_tol, abs_tol=abs_tol)

is true for a set of random points \(\{ x_i \}_{i = 0}^{d - 1}\) in the given n-dimensional simplex T, where \(d\) is the dimension of the polynomial space p1 and p2 belongs to (as given by the get_dimension() function).

For vector valued polynomials the same check is done component wise.

Parameters:
  • p1 (Callable p1(x)) – First polynomial.
  • p2 (Callable p2(x)) – Second polynomial.
  • r (int) – Degree of the polynomials.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • rel_tol (float) – Tolerance for the relative error. See math.isclose for details.
  • abs_tol (float) – Tolerance for the absolute error. See math.isclose for details.
Returns:

Whether or not the two polynomials are approximately equal.

Return type:

bool

polynomials_on_simplices.polynomial.polynomials_simplex_bernstein_basis module

Polynomials on an m-dimensional simplex T with values in \(\mathbb{R}^n\), expressed using the Bernstein basis.

\[\begin{split}b(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x) = \sum_{\nu} a_{\nu} (\bar{b}_{\nu, r} \circ \Phi^{-1})(x),\end{split}\]

where \(a_{\nu} \in \mathbb{R}^n, \bar{l}_{\nu, r}\) is the Bernstein basis on the unit simplex and \(\Phi\) is the unique affine map which maps the unit simplex onto the simplex T (the i:th vertex of the unit simplex is mapped to the i:th vertex of the simplex T).

The set \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is a basis for the space of all polynomials of degree less than or equal to r on the simplex T, \(\mathcal{P}_r (T)\).

class PolynomialBernsteinSimplex(coeff, vertices, r=None)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_simplex_base.PolynomialSimplexBase

Implementation of the abstract polynomial base class for a polynomial on an m-dimensional simplex T, expressed in the Bernstein basis.

\[b(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} b_{\nu_i, r}(x).\]
Parameters:
  • coeff – Coefficients for the polynomial in the Bernstein basis for \(\mathcal{P}_r (T, \mathbb{R}^n). \text{coeff}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
basis()[source]

Get basis for the space \(\mathcal{P}_r (T)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree_elevate(s)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^k \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x)\) be this polynomial, where \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^k \\ |\nu| \leq r}}\) is the Bernstein basis for \(\mathcal{P}_r (T)\). Let \(\{ b_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^k \\ |\nu| \leq s}}, s \geq r\) be the Bernstein basis for \(\mathcal{P}_s (T)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^k \\ |\nu| \leq s}} \tilde{a}_{\nu} b_{\nu, s}(x),\end{split}\]

such that \(p(x) = q(x) \, \forall x \in T\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
Return type:PolynomialBernsteinSimplex.
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
latex_str_expanded()[source]

Generate a Latex string for this polynomial, where each basis function has been expanded in the monomial basis.

Returns:Latex string for this polynomial.
Return type:str
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
Return type:PolynomialBernsteinSimplex.
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
Return type:Polynomial.
bernstein_basis_fn_simplex(nu, r, vertices)[source]

Generate a Bernstein basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu.

\[b_{\nu, r}(x) = (\bar{b}_{\nu, r} \circ \Phi^{-1})(x),\]

where \(\bar{b}_{\nu, r}\) is the corresponding Bernstein basis polynomial on the (n-dimensional) unit simplex, and \(\Phi\) is the unique affine map which maps the unit simplex to the simplex T.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial should be generated. The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The Bernstein base polynomial on the simplex T, as specified by nu and r.

Return type:

PolynomialBernsteinSimplex.

bernstein_basis_fn_simplex_latex(nu, r, vertices)[source]

Generate Latex string for a Bernstein basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

Latex string for the Bernstein base polynomial on T, as specified by nu and r.

Return type:

str

bernstein_basis_fn_simplex_monomial(nu, r, vertices)[source]

Generate a Bernstein basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu, expanded in the monomial basis.

This is the same polynomial as given by the bernstein_basis_fn_simplex() function, but expressed in the monomial basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial should be generated The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The Bernstein base polynomial on the simplex T, as specified by nu and r.

Return type:

Polynomial.

bernstein_basis_simplex(r, vertices)[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of base polynomials.

Return type:

List[PolynomialBernsteinSimplex].

bernstein_basis_simplex_latex(r, vertices)[source]

Generate Latex strings for all Bernstein base polynomials for the space \(\mathcal{P}_r(T)\), where T is an m-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:

List of Latex strings for each Bernstein base polynomial.

Return type:

List[str]

bernstein_basis_simplex_monomial(r, vertices)[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex, expanded in the monomial basis.

This is the same set of polynomials as given by the bernstein_basis_simplex() function, but expressed in the monomial basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of base polynomials.

Return type:

List[Polynomial].

dual_bernstein_basis_fn_simplex(mu, r, vertices)[source]

Generate a dual basis function to the Bernstein polynomial basis, i.e. the linear map \(q_{\mu, r} : \mathcal{P}_r(T) \to \mathbb{R}\) that satisfies

\[q_{\mu, r}(b_{\nu, r}) = \delta_{\mu, \nu},\]

where \(b_{\nu, r}\) is the degree r Bernstein basis polynomial on T indexed by the multi-index \(\nu\) (see bernstein_basis_fn_simplex()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Bernstein basis function should be generated.
  • r (int) – Degree of polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The dual Bernstein basis function as specified by mu and r.

Return type:

Callable \(q_{\mu, r}(b)\).

dual_bernstein_basis_polynomial_simplex(mu, r, vertices)[source]

Generate a dual Bernstein basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu.

The dual Bernstein basis \(d_{\mu, r} (x)\) is the unique polynomial that satisfies

\[\int_{T} d_{\mu, r}(x) b_{\nu, r} (x) \, dx = \delta_{\mu, \nu}.\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which dual Bernstein basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The dual Bernstein base polynomial as specified by mu and r.

Return type:

PolynomialBernsteinSimplex.

dual_bernstein_basis_simplex(r, vertices)[source]

Generate all dual Bernstein base functions for the space \(\mathcal{P}_r(T)\), where T is an n-dimensional simplex (i.e. a basis for \(\mathcal{P}_r(T)^*\)).

See dual_bernstein_basis_fn_simplex().

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of dual base functions.

Return type:

List[callable q(b)].

dual_bernstein_polynomial_basis_simplex(r, vertices)[source]

Generate all dual Bernstein base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex.

See dual_bernstein_basis_polynomial_simplex().

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of dual base polynomials.

Return type:

List[PolynomialBernsteinSimplex].

dual_vector_valued_bernstein_basis_fn_simplex(mu, r, i, vertices, n)[source]

Generate a dual basis function to the vector valued Bernstein polynomial basis, i.e. the linear map \(q_{\mu, r, i} : \mathcal{P}_r(T, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r, i}(b_{\nu, r, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(b_{\nu, r, j}\) is the degree r vector valued Bernstein basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component (see vector_valued_bernstein_basis_fn_simplex()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Bernstein basis function should be generated.
  • r (int) – Degree of polynomial space.
  • i (int) – Integer indicating which dual Bernstein basis function should be generated.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
Returns:

The dual Bernstein basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, r, i}(b)\).

dual_vector_valued_bernstein_basis_simplex(r, vertices, n, ordering='interleaved')[source]

Generate all dual Bernstein base functions for the space \(\mathcal{P}_r(T, \mathbb{R}^n)\), where T is an m-dimensional simplex (i.e. a basis for \(\mathcal{P}_r(T, \mathbb{R}^n)^*\)).

See dual_vector_valued_bernstein_basis_fn_simplex().

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(b)].

unique_identifier_bernstein_basis_simplex(vertices)[source]

Get unique identifier for the Bernstein polynomial basis on a simplex T.

Parameters:vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Unique identifier.
Return type:str
unit_polynomial_simplex(vertices, r=0, n=1)[source]

Get the Bernstein polynomial \(b \in \mathcal{P}(T, \mathbb{R}^n)\) which is identically one, where T is an m-dimensional simplex.

Parameters:
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – The unit polynomial will be expressed in the Bernstein basis for \(\mathcal{P}_r(T, \mathbb{R}^n)\).
  • n (int) – Dimension of the polynomial target.
Returns:

The unit polynomial.

Return type:

PolynomialBernsteinSimplex.

vector_valued_bernstein_basis_fn_simplex(nu, r, i, vertices, n)[source]

Generate a vector valued Bernstein basis polynomial on an m-dimensional simplex T, \(b_{\nu, r, i} : T \to \mathbb{R}^n\).

The vector valued basis polynomial is generated by specifying a scalar valued basis polynomial and the component of the vector valued basis polynomial that should be equal to the scalar valued basis polynomial. All other components of the vector valued basis polynomial will be zero, i.e.

\[\begin{split}b_{\nu, r, i}^j (x) = \begin{cases} b_{\nu, r} (x), & i = j \\ 0, & \text{else} \end{cases},\end{split}\]

where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which scalar valued Bernstein basis polynomial should be generated for the non-zero component.
  • r (int) – Degree of polynomial.
  • i (int) – Index of the vector component that is non-zero.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
Returns:

The Bernstein base polynomial on the simplex T as specified by nu, r, i and n.

Return type:

PolynomialBernsteinSimplex.

vector_valued_bernstein_basis_simplex(r, vertices, n, ordering='interleaved')[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(T, \mathbb{R}^n)\), where T is an m-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials.

Return type:

List[PolynomialBernsteinSimplex].

zero_polynomial_simplex(vertices, r=0, n=1)[source]

Get the Bernstein polynomial \(b \in \mathcal{P}(T, \mathbb{R}^n)\) which is identically zero, where T is an m-dimensional simplex.

Parameters:
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – The zero polynomial will be expressed in the Bernstein basis for \(\mathcal{P}_r(T, \mathbb{R}^n)\).
  • n (int) – Dimension of the polynomial target.
Returns:

The zero polynomial.

Return type:

PolynomialBernsteinSimplex.

polynomials_on_simplices.polynomial.polynomials_simplex_lagrange_basis module

Polynomials on an m-dimensional simplex T with values in \(\mathbb{R}^n\), expressed using the Lagrange basis.

\[\begin{split}l(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} l_{\nu, r}(x) = \sum_{\nu} a_{\nu} (\bar{l}_{\nu, r} \circ \Phi^{-1})(x),\end{split}\]

where \(a_{\nu} \in \mathbb{R}^n, \bar{l}_{\nu, r}\) is the Lagrange basis on the unit simplex and \(\Phi\) is the unique affine map which maps the unit simplex onto the simplex T (the i:th vertex of the unit simplex is mapped to the i:th vertex of the simplex T).

The basis polynomials \(l_{\nu, r} = \bar{l}_{\nu, r} \circ \Phi^{-1}\) satisfies

\[l_{\nu, r}(\Phi(x_{\mu}) = \delta_{\mu, \nu},\]

where \(x_{\mu}\) are the Lagrange points on the unit simplex.

The set \(\{ l_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is a basis for the space of all polynomials of degree less than or equal to r on the simplex T, \(\mathcal{P}_r (T)\).

class PolynomialLagrangeSimplex(coeff, vertices, r=None)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_simplex_base.PolynomialSimplexBase

Implementation of the abstract polynomial base class for a polynomial on an m-dimensional simplex T, expressed in the Lagrange basis.

\[l(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} l_{\nu_i, r}(x).\]
Parameters:
  • coeff – Coefficients for the polynomial in the Lagrange basis for \(\mathcal{P}_r (T, \mathbb{R}^n). \text{coeff}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree_elevate(s)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} l_{\nu, r}(x)\) be this polynomial, where \(\{ l_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is the Lagrange basis for \(\mathcal{P}_r (T)\). Let \(\{ l_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the Lagrange basis for \(\mathcal{P}_s (T)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} l_{\nu, s}(x),\end{split}\]

such that \(p(x) = q(x) \, \forall x \in T\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
Return type:PolynomialLagrangeSimplex.
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
latex_str_expanded()[source]

Generate a Latex string for this polynomial, where each basis function has been expanded in the monomial basis.

Returns:Latex string for this polynomial.
Return type:str
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
Return type:PolynomialLagrangeSimplex.
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
Return type:Polynomial.
dual_lagrange_basis_fn_simplex(mu, r, vertices)[source]

Generate a dual basis function to the Lagrange polynomial basis, i.e. the linear map \(q_{\mu, r} : \mathcal{P}_r(T) \to \mathbb{R}\) that satisfies

\[q_{\mu, r}(l_{\nu, r}) = \delta_{\mu, \nu},\]

where \(l_{\nu, r}\) is the degree r Lagrange basis polynomial on T indexed by the multi-index \(\nu\) (see lagrange_basis_fn_simplex()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Lagrange basis function should be generated.
  • r (int) – Degree of polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The dual Lagrange basis function as specified by mu and r.

Return type:

Callable \(q_{\mu, r}(l)\).

dual_lagrange_basis_simplex(r, vertices)[source]

Generate all dual Lagrange base functions for the space \(\mathcal{P}_r(T)\), where T is an n-dimensional simplex (i.e. the Lagrange basis for \(\mathcal{P}_r(T)^*\)).

See dual_lagrange_basis_fn_simplex().

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of dual base functions.

Return type:

List[callable q(l)].

dual_vector_valued_lagrange_basis_fn_simplex(mu, r, i, vertices, n)[source]

Generate a dual basis function to the vector valued Lagrange polynomial basis, i.e. the linear map \(q_{\mu, r, i} : \mathcal{P}_r(T, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r, i}(l_{\nu, r, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(l_{\nu, r, j}\) is the degree r vector valued Lagrange basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component (see vector_valued_lagrange_basis_fn_simplex()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Lagrange basis function should be generated.
  • r (int) – Degree of polynomial space.
  • i (int) – Integer indicating which dual Lagrange basis function should be generated.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
Returns:

The dual Lagrange basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, r, i}(l)\).

dual_vector_valued_lagrange_basis_simplex(r, vertices, n, ordering='interleaved')[source]

Generate all dual Lagrange base functions for the space \(\mathcal{P}_r(T, \mathbb{R}^n)\), where T is an m-dimensional simplex (i.e. the Lagrange basis for \(\mathcal{P}_r(T, \mathbb{R}^n)^*\)).

See dual_vector_valued_lagrange_basis_fn_simplex().

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(l)].

generate_lagrange_point_simplex(vertices, r, nu)[source]

Generate a Lagrange point indexed by a multi-index on an n-dimensional simplex T from the set \(\{ \bar{x}_nu \}\) of evenly spaced Lagrange points on the m-dimensional unit simplex (\(\Delta_c^n\)) (Lagrange basis points are constructed so that each basis function has the value 1 at one of the points, and 0 at all the other points).

\[\bar{x}_{\nu} = \frac{\nu}{r},\]
\[x_{\nu} = \Phi(\bar{x}_{\nu},\]

where \(\Phi\) is the unique affine map which maps the unit simplex to the simplex T.

Parameters:
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • r (int) – Degree of the polynomial.
  • nu – Multi-index \(\nu\) indexing the Lagrange point, where \(\frac{\nu_i}{r}\) gives the i:th coordinate of the corresponding Lagrange point in the unit simplex.
Returns:

Point in the n-dimensional simplex T.

Return type:

Numpy array

generate_lagrange_points_simplex(vertices, r)[source]

Generate evenly spaced Lagrange points on an n-dimensional simplex T (Lagrange basis points are constructed so that each basis function has the value 1 at one of the points, and 0 at all the other points).

\[\begin{split}\{ x_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^n \\ |\nu| \leq r}}, x_{\nu} = x_{\nu} = \Phi(\bar{x}_{\nu},\end{split}\]

where \(\{ \bar{x}_{\nu} \}\) is the set of evenly spaced Lagrange points on the unit simplex, and \(\Phi\) is the unique affine map which maps the unit simplex to the simplex T.

Parameters:
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
  • r (int) – Degree of the polynomial.
Returns:

List of points in the n-dimensional simplex T.

Return type:

Numpy array

lagrange_basis_fn_simplex(nu, r, vertices)[source]

Generate a Lagrange basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu.

\[l_{\nu, r}(x) = (\bar{l}_{\nu, r} \circ \Phi^{-1})(x),\]

where \(\bar{l}_{\nu, r}\) is the corresponding Lagrange basis polynomial on the (n-dimensional) unit simplex, and \(\Phi\) is the unique affine map which maps the unit simplex to the simplex T.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial should be generated. The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The Lagrange base polynomial on the simplex T, as specified by nu and r.

Return type:

PolynomialLagrangeSimplex.

lagrange_basis_fn_simplex_latex(nu, r, vertices)[source]

Generate Latex string for a Lagrange basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

Latex string for the Lagrange base polynomial on T, as specified by nu and r.

Return type:

str

lagrange_basis_fn_simplex_monomial(nu, r, vertices)[source]

Generate a Lagrange basis polynomial on an n-dimensional simplex T, where n is equal to the length of nu, expanded in the monomial basis.

This is the same polynomial as given by the lagrange_basis_fn_simplex() function, but expressed in the monomial basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial should be generated The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

The Lagrange base polynomial on the simplex T, as specified by nu and r.

Return type:

Polynomial.

lagrange_basis_simplex(r, vertices)[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of base polynomials.

Return type:

List[PolynomialLagrangeSimplex].

lagrange_basis_simplex_latex(r, vertices)[source]

Generate Latex strings for all Lagrange base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of Latex strings for each Lagrange base polynomial.

Return type:

List[str]

lagrange_basis_simplex_monomial(r, vertices)[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(T)\) where T is an n-dimensional simplex, expanded in the monomial basis.

This is the same set of polynomials as given by the lagrange_basis_simplex() function, but expressed in the monomial basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((n + 1) x n matrix where row i contains the i:th vertex of the simplex).
Returns:

List of base polynomials.

Return type:

List[Polynomial].

unique_identifier_lagrange_basis_simplex(vertices)[source]

Get unique identifier for the Lagrange polynomial basis on a simplex T.

Parameters:vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
Returns:Unique identifier.
Return type:str
unit_polynomial_simplex(vertices, r=0, n=1)[source]

Get the Lagrange polynomial \(l \in \mathcal{P}(T, \mathbb{R}^n)\) which is identically one, where T is an m-dimensional simplex.

Parameters:
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – The unit polynomial will be expressed in the Lagrange basis for \(\mathcal{P}_r(T, \mathbb{R}^n)\).
  • n (int) – Dimension of the polynomial target.
Returns:

The unit polynomial.

Return type:

PolynomialLagrangeSimplex.

vector_valued_lagrange_basis_fn_simplex(nu, r, i, vertices, n)[source]

Generate a vector valued Lagrange basis polynomial on an m-dimensional simplex T, \(l_{\nu, r, i} : T \to \mathbb{R}^n\).

The vector valued basis polynomial is generated by specifying a scalar valued basis polynomial and the component of the vector valued basis polynomial that should be equal to the scalar valued basis polynomial. All other components of the vector valued basis polynomial will be zero, i.e.

\[\begin{split}l_{\nu, r, i}^j (x) = \begin{cases} l_{\nu, r} (x), & i = j \\ 0, & \text{else} \end{cases},\end{split}\]

where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which scalar valued Lagrange basis polynomial should be generated for the non-zero component.
  • r (int) – Degree of polynomial.
  • i (int) – Index of the vector component that is non-zero.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
Returns:

The Lagrange base polynomial on the simplex T as specified by nu, r, i and n.

Return type:

PolynomialLagrangeSimplex.

vector_valued_lagrange_basis_simplex(r, vertices, n, ordering='interleaved')[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(T, \mathbb{R}^n)\), where T is an m-dimensional simplex.

Parameters:
  • r (int) – Degree of the polynomial space.
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials.

Return type:

List[PolynomialLagrangeSimplex].

zero_polynomial_simplex(vertices, r=0, n=1)[source]

Get the Lagrange polynomial \(l \in \mathcal{P}(T, \mathbb{R}^n)\) which is identically zero, where T is an m-dimensional simplex.

Parameters:
  • vertices – Vertices of the simplex T ((m + 1) x m matrix where row i contains the i:th vertex of the simplex).
  • r (int) – The zero polynomial will be expressed in the Lagrange basis for \(\mathcal{P}_r(T, \mathbb{R}^n)\).
  • n (int) – Dimension of the polynomial target.
Returns:

The zero polynomial.

Return type:

PolynomialLagrangeSimplex.

polynomials_on_simplices.polynomial.polynomials_unit_simplex_bases module

Functionality for working with different bases for polynomials on the unit simplex, and for converting between these bases.

convert_polynomial_to_basis(p, target_basis)[source]

Convert a polynomial in \(\mathcal{P}_r (\Delta_c^m)\) to the given basis.

Parameters:
  • p – Polynomial expanded in some basis.
  • target_basis (str) – Unique identifier for the basis we want to expand the polynomial in.
Returns:

Polynomial expanded in the given basis.

dual_polynomial_basis(r, n, basis)[source]

Generate all dual base functions for the space \(\mathcal{P}_r(\Delta_c^n)\) in the given basis (i.e. a basis for \(\mathcal{P}_r(\Delta_c^n)^*\)).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the domain.
  • basis (str) – Unique identifier for the basis we should generate dual base functions for.
Returns:

List of dual base functions.

Return type:

List[callable q(p)].

dual_polynomial_basis_fn(mu, r, basis)[source]

Generate a dual basis function to a polynomial basis, i.e. the linear map \(q_{\mu, r} : \mathcal{P}_r(\Delta_c^n) \to \mathbb{R}\) such that

\[q_{\mu, r}(p_{\nu, r}) = \delta_{\mu, \nu},\]

where \(p_{\nu, r}\) is the degree r basis polynomial indexed by the multi-index \(\nu\) in the given basis and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which dual basis function should be generated.
  • r (int) – Degree of polynomial space.
  • basis (str) – Unique identifier for the basis we should generate a dual base function for.
Returns:

The dual basis function as specified by mu, r and basis.

Return type:

Callable \(q_{\mu, r}(p)\).

dual_vector_valued_polynomial_basis(r, m, n, basis, ordering='interleaved')[source]

Generate all dual base functions for the space \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\) in the given basis (i.e. the basis for \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)^*\)).

See dual_vector_valued_polynomial_basis_fn().

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the domain.
  • n (int) – Dimension of the target.
  • basis (str) – Unique identifier for the basis we should generate dual base functions for.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(p)].

dual_vector_valued_polynomial_basis_fn(mu, r, i, n, basis)[source]

Generate a dual basis function to a vector valued polynomial basis, i.e. the linear map \(q_{\mu, i} : \mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, i}(p_{\nu, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(p_{\nu, j}\) is the degree \(|\nu|\) vector valued basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component in the given basis (see vector_valued_polynomial_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual basis function should be generated.
  • r (int) – Degree of polynomial space.
  • i (int) – Integer indicating which dual basis function should be generated.
  • n (int) – Dimension of the target.
  • basis (str) – Unique identifier for the basis we should generate a dual base function for.
Returns:

The dual basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, i}(p)\).

polynomial_basis(r, n, basis)[source]

Generate all base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\) in the given basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
  • basis (str) – Unique identifier for the basis we should generate base polynomials for.
Returns:

List of base polynomials in the specified basis.

polynomial_basis_fn(nu, r, basis)[source]

Generate a basis polynomial in the space \(\mathcal{P}_r(\Delta_c^n)\) (where n is equal to the length of nu) in the given basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • basis (str) – Unique identifier for the basis we should generate a base polynomial for.
Returns:

The base polynomial as specified by nu, r and basis.

Return type:

Implementation of PolynomialBase.

polynomial_basis_fn_latex(nu, r, basis)[source]

Generate Latex string for a basis polynomial for the space \(\mathcal{P}_r(\mathbb{R}^n)\) (where n is equal to the length of nu) in the given basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • basis (str) – Unique identifier for the basis we should generate a basis polynomial Latex string for.
Returns:

Latex string for the base polynomial as specified by nu, r and basis.

Return type:

str

Examples

>>> polynomial_basis_fn_latex(3, 3, unique_identifier_monomial_basis())
'x^3'
>>> polynomial_basis_fn_latex((1, 1, 1), 3, unique_identifier_bernstein_basis())
'6 x_1 x_2 x_3'
polynomial_basis_fn_latex_compact(nu, r, basis)[source]

Generate compact Latex string for a basis polynomial for the space \(\mathcal{P}_r(\mathbb{R}^n)\) (where n is equal to the length of nu) in the given basis, using the common shorthand notation for the given basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • basis (str) – Unique identifier for the basis we should generate a basis polynomial Latex string for.
Returns:

Latex string for the base polynomial as specified by nu, r and basis.

Return type:

str

Examples

>>> polynomial_basis_fn_latex_compact(3, 3, unique_identifier_monomial_basis())
'x^3'
>>> polynomial_basis_fn_latex_compact((1, 1), 3, unique_identifier_monomial_basis())
'x^{(1, 1)}'
>>> polynomial_basis_fn_latex_compact((1, 1, 1), 3, unique_identifier_bernstein_basis())
'b_{(1, 1, 1), 3}(x)'
polynomial_basis_latex(r, n, basis)[source]

Generate Latex strings for all base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\) in the given basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
  • basis (str) – Unique identifier for the basis we should generate base polynomial Latex strings for.
Returns:

List of Latex strings for each base polynomials in the specified basis.

Return type:

List[str]

Examples

>>> polynomial_basis_latex(2,1,unique_identifier_monomial_basis())
['1', 'x', 'x^2']
>>> polynomial_basis_latex(2,2,unique_identifier_bernstein_basis())
['(1 - x_1 - x_2)^2', '2 x_1 (1 - x_1 - x_2)', 'x_1^2', '2 x_2 (1 - x_1 - x_2)', '2 x_1 x_2', 'x_2^2']
polynomial_basis_latex_compact(r, n, basis)[source]

Generate compact Latex strings for all base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\) in the given basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
  • basis (str) – Unique identifier for the basis we should generate base polynomial Latex strings for.
Returns:

List of Latex strings for each base polynomials in the specified basis.

Return type:

List[str]

Examples

>>> polynomial_basis_latex_compact(2,1,unique_identifier_monomial_basis())
['1', 'x', 'x^2']
>>> polynomial_basis_latex_compact(1,2,unique_identifier_bernstein_basis())
['b_{(0, 0), 1}(x)', 'b_{(1, 0), 1}(x)', 'b_{(0, 1), 1}(x)']
vector_valued_polynomial_basis(r, m, n, basis, ordering='interleaved')[source]

Generate all base polynomials for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\) in the given basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the domain.
  • n (int) – Dimension of the target.
  • basis (str) – Unique identifier for the basis we should generate base polynomials for.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials in the specified basis.

vector_valued_polynomial_basis_fn(nu, r, i, n, basis)[source]

Generate a basis polynomial for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\) (where m is equal to the length of nu) in the given basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • i (int) – Index of the vector component that is non-zero.
  • n (int) – Dimension of the target.
  • basis (str) – Unique identifier for the basis we should generate a base polynomial for.
Returns:

The base polynomial as specified by nu, r and basis.

Return type:

Implementation of PolynomialBase.

polynomials_on_simplices.polynomial.polynomials_unit_simplex_bernstein_basis module

Polynomials on the m-dimensional unit simplex with values in \(\mathbb{R}^n\), expressed using the Bernstein basis.

\[\begin{split}b(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x),\end{split}\]

where \(a_{\nu} \in \mathbb{R}^n\) and

\[b_{\nu, r}(x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|}.\]

The set \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is a basis for the space of all polynomials of degree less than or equal to r on the unit simplex, \(\mathcal{P}_r (\Delta_c^m)\).

The operations on Bernstein polynomials on a simplex used here is a generalization of the corresponding operations on Bernstein polynomials on the unit interval, as given in [Farouki_and_Rajan_1988].

References

[Farouki_and_Rajan_1988]R.T. Farouki and V.T. Rajan. Algorithms for polynomials in bernstein form, Computer Aided Geometric Design, 5 (1):1 – 26, 1988. ISSN 0167-8396. doi:10.1016/0167-8396(88)90016-7. URL http://www.sciencedirect.com/science/article/pii/0167839688900167.
class PolynomialBernstein(coeff, r=None, m=1)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_base.PolynomialBase

Implementation of the abstract polynomial base class for a polynomial on the m-dimensional unit simplex, expressed in the Bernstein basis.

\[b(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} b_{\nu_i, r}(x).\]
Parameters:
  • coeff – Coefficients for the polynomial in the Bernstein basis for \(\mathcal{P}_r (\mathbb{R}^m, \mathbb{R}^n). \text{coeff}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree_elevate(s=1)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x)\) be this polynomial, where \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is the Bernstein basis for \(\mathcal{P}_r (\mathbb{R}^m)\). Let \(\{ b_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the Bernstein basis for \(\mathcal{P}_s (\mathbb{R}^m)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} b_{\nu, s}(x),\end{split}\]

such that \(p(x) = q(x) \, \forall x \in \Delta_c^n\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
Return type:PolynomialBernstein.
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
latex_str_expanded()[source]

Generate a Latex string for this polynomial, where each basis function has been expanded in the monomial basis.

Returns:Latex string for this polynomial.
Return type:str
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
Return type:PolynomialBernstein.
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
Return type:Polynomial.
barycentric_polynomial_general(r, a, x)[source]

Evaluate a degree r barycentric polynomial on the m-dimensional unit simplex.

\[\begin{split}b(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} x^{\nu} (1 - |x|)^{r - |\nu|}.\end{split}\]
Parameters:
  • r (int) – Degree of the polynomial.
  • a – Coefficient in front of each barycentric base polynomial.
  • x – Point where the polynomial should be evaluated.
Returns:

Value of the polynomial.

bernstein_basis(r, n)[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of base polynomials.

Return type:

List[PolynomialBernstein].

bernstein_basis_fn(nu, r)[source]

Generate a Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\))

\[b_{\nu, r} (x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial should be generated.
  • r (int) – Degree of polynomial.
Returns:

The Bernstein base polynomial as specified by nu and r.

Return type:

PolynomialBernstein.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> bernstein_basis_fn(0, 1)(x1) - (-x1 + 1)
0
>>> bernstein_basis_fn(1, 1)(x1) - x1
0
>>> sp.simplify(bernstein_basis_fn((1, 0), 2)((x1, x2)) - 2*x1*(-x1 - x2 + 1))
0
>>> sp.simplify(bernstein_basis_fn((1, 1), 3)((x1, x2)) - 6*x1*x2*(-x1 - x2 + 1))
0
bernstein_basis_fn_latex(nu, r)[source]

Generate Latex string for a Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\))

\[b_{\nu, r} (x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
Returns:

Latex string for the Bernstein base polynomial as specified by nu and r.

Return type:

str

Examples

>>> bernstein_basis_fn_latex(2, 3)
'3 x^2 (1 - x)'
>>> bernstein_basis_fn_latex((1, 1), 3)
'6 x_1 x_2 (1 - x_1 - x_2)'
bernstein_basis_fn_latex_compact(nu, r)[source]

Generate compact Latex string for a Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\))

\[b_{\nu, r} (x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
Returns:

Latex string for the Bernstein base polynomial as specified by nu and r.

Return type:

str

Examples

>>> bernstein_basis_fn_latex_compact(2, 3)
'b_{2, 3}(x)'
>>> bernstein_basis_fn_latex_compact((1, 1), 3)
'b_{(1, 1), 3}(x)'
bernstein_basis_fn_monomial(nu, r)[source]

Generate a Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\))

\[b_{\nu, r} (x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|},\]

where n is equal to the length of nu, expanded in the monomial basis.

This is the same polynomial as given by the bernstein_basis_fn() function, but expressed in the monomial basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial should be generated.
  • r (int) – Degree of polynomial.
Returns:

The Bernstein base polynomial as specified by nu and r.

Return type:

Polynomial.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> bernstein_basis_fn_monomial(0, 1)(x1)
-x1 + 1
>>> bernstein_basis_fn_monomial(1, 1)(x1)
x1
>>> bernstein_basis_fn_monomial((1, 0), 2)((x1, x2))
-2*x1**2 - 2*x1*x2 + 2*x1
>>> bernstein_basis_fn_monomial((1, 1), 3)((x1, x2))
-6*x1**2*x2 - 6*x1*x2**2 + 6*x1*x2
bernstein_basis_latex(r, n)[source]

Generate Latex strings for all Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of Latex strings for each Bernstein base polynomial.

Return type:

List[str]

Examples

>>> bernstein_basis_latex(2, 1)
['(1 - x)^2', '2 x (1 - x)', 'x^2']
>>> bernstein_basis_latex(2, 2)
['(1 - x_1 - x_2)^2', '2 x_1 (1 - x_1 - x_2)', 'x_1^2', '2 x_2 (1 - x_1 - x_2)', '2 x_1 x_2', 'x_2^2']
bernstein_basis_latex_compact(r, n)[source]

Generate compact Latex strings for all Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of Latex strings for each Bernstein base polynomial.

Return type:

List[str]

Examples

>>> bernstein_basis_latex_compact(2, 1)
['b_{0, 2}(x)', 'b_{1, 2}(x)', 'b_{2, 2}(x)']
>>> bernstein_basis_latex_compact(1, 2)
['b_{(0, 0), 1}(x)', 'b_{(1, 0), 1}(x)', 'b_{(0, 1), 1}(x)']
bernstein_basis_monomial(r, n)[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\), expanded in the monomial basis.

This is the same set of polynomials as given by the bernstein_basis() function, but expressed in the monomial basis.

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of base polynomials.

Return type:

List[Polynomial].

degree_elevated_bernstein_basis_fn(nu, r, s)[source]

Generate a Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\)) \(b_{\nu, r} (x) = \binom{r}{\nu} x^{\nu} (1 - |x|)^{r - |\nu|}\), where n is equal to the length of nu, and degree elevate it to a degree s Bernstein polynomial.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Bernstein basis polynomial should be generated.
  • r (int) – Degree of polynomial.
  • s (int) – Degree for the polynomial basis the polynomial should be expressed in.
Returns:

The Bernstein base polynomial as specified by nu and r, expressed as a degree s Bernstein polynomial.

Return type:

PolynomialBernstein.

dual_bernstein_basis(r, n)[source]

Generate all dual Bernstein base functions for the space \(\mathcal{P}_r(\Delta_c^n)\) (i.e. the Bernstein basis for \(\mathcal{P}_r(\Delta_c^n)^*\)).

See dual_bernstein_basis_fn().

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of dual base functions.

Return type:

List[callable q(b)].

dual_bernstein_basis_fn(mu, r)[source]

Generate a dual basis function to the Bernstein polynomial basis, i.e. the linear map \(q_{\mu, r} : \mathcal{P}_r(\Delta_c^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r}(b_{\nu, r}) = \delta_{\mu, \nu},\]

where \(b_{\nu, r}\) is the degree r Bernstein basis polynomial indexed by the multi-index \(\nu\) (see bernstein_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Bernstein basis function should be generated.
  • r (int) – Degree of polynomial space.
Returns:

The dual Bernstein basis function as specified by mu and r.

Return type:

Callable \(q_{\mu, r}(b)\).

dual_bernstein_basis_polynomial(mu, r)[source]

Generate a dual Bernstein basis polynomial on the n-dimensional unit simplex (\(\Delta_c^n\)) \(d_{\mu, r} (x)\), where n is equal to the length of mu.

The dual Bernstein basis \(d_{\mu, r} (x)\) is the unique polynomial that satisfies

\[\int_{\Delta_c^n} d_{\mu, r}(x) b_{\nu, r} (x) \, dx = \delta_{\mu, \nu}.\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which dual Bernstein basis polynomial should be generated.
  • r (int) – Degree of polynomial.
Returns:

The dual Bernstein base polynomial as specified by mu and r.

Return type:

PolynomialBernstein.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> d = dual_bernstein_basis_polynomial((0, 0), 2)((x1, x2))
>>> b = bernstein_basis_fn((0, 0), 2)((x1, x2))
>>> abs(sp.integrate(d * b, (x2, 0, -x1 + 1), (x1, 0, 1)) - 1.0) < 1e-10
True
>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> d = dual_bernstein_basis_polynomial((0, 0), 2)((x1, x2))
>>> b = bernstein_basis_fn((1, 0), 2)((x1, x2))
>>> abs(sp.integrate(d * b, (x2, 0, -x1 + 1), (x1, 0, 1))) < 1e-10
True
dual_bernstein_polynomial_basis(r, n)[source]

Generate all dual Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

See dual_bernstein_basis_polynomial().

Parameters:
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the unit simplex.
Returns:

List of dual base polynomials.

Return type:

List[PolynomialBernstein].

dual_vector_valued_bernstein_basis(r, m, n, ordering='interleaved')[source]

Generate all dual Bernstein base functions for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\) (i.e. the Bernstein basis for \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)^*\)).

See dual_vector_valued_bernstein_basis_fn().

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the unit simplex.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(b)].

dual_vector_valued_bernstein_basis_fn(mu, r, i, n)[source]

Generate a dual basis function to the vector valued Bernstein polynomial basis, i.e. the linear map \(q_{\mu, r, i} : \mathcal{P}_r(\Delta_c^m, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r, i}(b_{\nu, r, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(b_{\nu, r, j}\) is the degree r vector valued Bernstein basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component (see vector_valued_bernstein_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Bernstein basis function should be generated.
  • r (int) – Degree of polynomial space.
  • i (int) – Integer indicating which dual Bernstein basis function should be generated.
  • n (int) – Dimension of the target.
Returns:

The dual Bernstein basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, r, i}(b)\).

get_associated_sub_simplex(nu, r, simplex=None)[source]

Get the sub simplex associated with a Bernstein basis polynomial.

For a Bernstein basis polynomial p on a simplex T there exist a unique sub simplex f of T such that p vanishes to degree r on on f*, where f* is the sub simplex opposite to the simplex f (see polynomials_on_simplices.geometry.mesh.simplicial_complex.opposite_sub_simplex()). A polynomial vanishes to degree r on a simplex f* if

\[(\partial_{\alpha} p) = 0, \, \forall x \in f^*, \, \forall \alpha \in \mathbb{N}_0^n, |\alpha| \leq r - 1.\]
Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating for which Bernstein basis polynomial we should get the associated sub simplex.
  • r (int) – Degree of polynomial.
  • simplex (Optional[List[int]]) – Vertex indices for the vertices of the simplex. [0, 1, …, n] assumed if not specified.
Returns:

Tuple containing the associated sub simplex, and the sub multi-index associated with the sub simplex (where all non-zero entries has been removed).

Examples

>>> get_associated_sub_simplex((0,), 1)
([0], (1,))
>>> get_associated_sub_simplex((1,), 1)
([1], (1,))
>>> get_associated_sub_simplex((1, 0, 1), 2, [1, 2, 3, 4])
([2, 4], (1, 1))
unique_identifier_bernstein_basis()[source]

Get unique identifier for the Bernstein polynomial basis on the unit simplex.

Returns:Unique identifier.
Return type:str
unit_polynomial(r=0, m=1, n=1)[source]

Get the Bernstein polynomial \(b \in \mathcal{P}(\Delta_c^m, \mathbb{R}^n)\) which is identically one.

Parameters:
  • r (int) – The unit polynomial will be expressed in the Bernstein basis for \(\mathcal{P}_r(\mathbb{R}^m, \mathbb{R}^n)\).
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
Returns:

The unit polynomial.

Return type:

PolynomialBernstein.

vector_valued_bernstein_basis(r, m, n, ordering='interleaved')[source]

Generate all Bernstein base polynomials for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\).

Parameters:
  • r (int) – Degree of the polynomial space.
  • m (int) – Dimension of the unit simplex.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials.

Return type:

List[PolynomialBernstein].

vector_valued_bernstein_basis_fn(nu, r, i, n)[source]

Generate a vector valued Bernstein basis polynomial on the m-dimensional unit simplex, \(b_{\nu, r, i} : \Delta_c^m \to \mathbb{R}^n\).

The vector valued basis polynomial is generated by specifying a scalar valued basis polynomial and the component of the vector valued basis polynomial that should be equal to the scalar valued basis polynomial. All other components of the vector valued basis polynomial will be zero, i.e.

\[\begin{split}b_{\nu, r, i}^j (x) = \begin{cases} b_{\nu, r} (x), & i = j \\ 0, & \text{else} \end{cases},\end{split}\]

where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which scalar valued Bernstein basis polynomial should be generated for the non-zero component.
  • r (int) – Degree of polynomial.
  • i (int) – Index of the vector component that is non-zero.
  • n (int) – Dimension of the target.
Returns:

The Bernstein base polynomial as specified by nu, r, i and n.

Return type:

PolynomialBernstein.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> vector_valued_bernstein_basis_fn(0, 1, 0, 2)(x1)
array([-x1 + 1, 0], dtype=object)
>>> vector_valued_bernstein_basis_fn(1, 1, 1, 2)(x1)
array([0, x1], dtype=object)
>>> vector_valued_bernstein_basis_fn((1, 0), 2, 0, 2)((x1, x2))
array([2*x1*(-x1 - x2 + 1), 0], dtype=object)
>>> vector_valued_bernstein_basis_fn((1, 1), 3, 1, 3)((x1, x2))
array([0, 6*x1*x2*(-x1 - x2 + 1), 0], dtype=object)
zero_polynomial(r=0, m=1, n=1)[source]

Get the Bernstein polynomial \(b \in \mathcal{P}(\Delta_c^m, \mathbb{R}^n)\) which is identically zero.

Parameters:
  • r (int) – The zero polynomial will be expressed in the Bernstein basis for \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\).
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
Returns:

The zero polynomial.

Return type:

PolynomialBernstein.

polynomials_on_simplices.polynomial.polynomials_unit_simplex_lagrange_basis module

Polynomials on the m-dimensional unit simplex with values in \(\mathbb{R}^n\), expressed using the Lagrange basis.

\[\begin{split}l(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} l_{\nu, r}(x),\end{split}\]

where \(a_{\nu} \in \mathbb{R}^n\).

Basis polynomials in the Lagrange basis are uniquely determined by selecting a sequence of \(\dim \mathcal{P}_r (\Delta_c^m)\) unique points (Lagrange points) in the unit simplex and demanding that the i:th basis function has the value one at the i:th of these points and zero at all the other points.

Here we have used evenly spaced Lagrange points, so that for \(\dim \mathcal{P}_r (\Delta_c^m)\) we have the Lagrange points

\[\begin{split}\{ x_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}, x_{\nu} = \frac{\nu}{r}.\end{split}\]

The basis polynomials \(l_{\nu, r}(x), \nu \in \mathbb{N}_0^n, |\nu| \leq r\) are thus uniquely determined by the conditions

\[\begin{split}l_{\nu, r}(x_{\mu}) = \begin{cases} 1 & \nu = \mu \\ 0 & \text{else} \end{cases}.\end{split}\]

The set \(\{ l_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is a basis for the space of all polynomials of degree less than or equal to r on the unit simplex, \(\mathcal{P}_r (\Delta_c^m)\).

class PolynomialLagrange(coeff, r=None, m=1)[source]

Bases: polynomials_on_simplices.polynomial.polynomials_base.PolynomialBase

Implementation of the abstract polynomial base class for a polynomial on the m-dimensional unit simplex, expressed in the Lagrange basis.

\[l(x) = \sum_{i = 0}^{\dim(\mathcal{P}_r(\mathbb{R}^m)) - 1} a_{\nu_i} l_{\nu_i, r}(x).\]
Parameters:
  • coeff – Coefficients for the polynomial in the Lagrange basis for \(\mathcal{P}_r (\mathbb{R}^m, \mathbb{R}^n). \text{coeff}[i] = a_{\nu_i}\), where \(\nu_i\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (see polynomials_on_simplices.algebra.multiindex.generate() function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)).
  • m (int) – Dimension of the domain of the polynomial.
  • r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
basis()[source]

Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.

Returns:Unique identifier for the basis used.
Return type:str
code_str(fn_name)[source]

Generate a function code string for evaluating this polynomial.

Parameters:fn_name (str) – Name for the function in the generated code.
Returns:Code string for evaluating this polynomial.
Return type:str
degree_elevate(s)[source]

Express the polynomial using a higher degree basis.

Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} l_{\nu, r}(x)\) be this polynomial, where \(\{ l_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is the Lagrange basis for \(\mathcal{P}_r (\mathbb{R}^m)\). Let \(\{ l_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the Lagrange basis for \(\mathcal{P}_s (\mathbb{R}^m)\). Then this function returns a polynomial \(q(x)\)

\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} l_{\nu, s}(x),\end{split}\]

such that \(p(x) = q(x) \, \forall x \in \Delta_c^m\).

Parameters:s (int) – New degree for the polynomial basis the polynomial should be expressed in.
Returns:Elevation of this polynomial to the higher degree basis.
Return type:PolynomialLagrange.
latex_str()[source]

Generate a Latex string for this polynomial.

Returns:Latex string for this polynomial.
Return type:str
latex_str_expanded()[source]

Generate a Latex string for this polynomial, where each basis function has been expanded in the monomial basis.

Returns:Latex string for this polynomial.
Return type:str
partial_derivative(i=0)[source]

Compute the i:th partial derivative of the polynomial.

Parameters:i (int) – Index of partial derivative.
Returns:i:th partial derivative of this polynomial.
Return type:PolynomialLagrange.
to_monomial_basis()[source]

Compute the monomial representation of this polynomial.

Returns:This polynomial expressed in the monomial basis.
Return type:Polynomial.
dual_lagrange_basis(r, n)[source]

Generate all dual Lagrange base functions for the space \(\mathcal{P}_r(\Delta_c^n)\) (i.e. the Lagrange basis for \(\mathcal{P}_r(\Delta_c^n)^*\)).

See dual_lagrange_basis_fn().

Parameters:
  • n (int) – Dimension of the space.
  • r (int) – Degree of the polynomial space.
Returns:

List of dual base functions.

Return type:

List[callable q(l)].

dual_lagrange_basis_fn(mu, r)[source]

Generate a dual basis function to the Lagrange polynomial basis, i.e. the linear map \(q_{\mu, r} : \mathcal{P}_r(\Delta_c^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r}(l_{\nu, r}) = \delta_{\mu, \nu},\]

where \(l_{\nu, r}\) is the degree r Lagrange basis polynomial indexed by the multi-index \(\nu\) (see lagrange_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Lagrange basis function should be generated.
  • r (int) – Degree of polynomial space.
Returns:

The dual Lagrange basis function as specified by mu and r.

Return type:

Callable \(q_{\mu, r}(l)\).

dual_vector_valued_lagrange_basis(r, m, n, ordering='interleaved')[source]

Generate all dual Lagrange base functions for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\) (i.e. the Lagrange basis for \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)^*\)).

See dual_vector_valued_lagrange_basis_fn().

Parameters:
  • m (int) – Dimension of the space.
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of dual base functions.

Return type:

List[callable q(l)].

dual_vector_valued_lagrange_basis_fn(mu, r, i, n)[source]

Generate a dual basis function to the vector valued Lagrange polynomial basis, i.e. the linear map \(q_{\mu, r, i} : \mathcal{P}_r(\Delta_c^m, \mathbb{R}^n) \to \mathbb{R}\) that satisfies

\[q_{\mu, r, i}(l_{\nu, r, j}) = \delta_{\mu, \nu} \delta_{i, j},\]

where \(l_{\nu, r, j}\) is the degree r vector valued Lagrange basis polynomial indexed by the multi-index \(\nu\) with a non-zero i:th component (see vector_valued_lagrange_basis_fn()) and

\[\begin{split}\delta_{\mu, \nu} = \begin{cases} 1 & \mu = \nu \\ 0 & \text{else} \end{cases}.\end{split}\]
Parameters:
  • mu (int or MultiIndex or Tuple[int, …].) – Multi-index indicating which dual Lagrange basis function should be generated.
  • r (int) – Degree of polynomial space.
  • i (int) – Integer indicating which dual Lagrange basis function should be generated.
  • n (int) – Dimension of the target.
Returns:

The dual Lagrange basis function as specified by mu, r and i.

Return type:

Callable \(q_{\mu, r, i}(l)\).

generate_lagrange_base_coefficients(r, n)[source]

Generate monomial coefficients for all the Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\), with evenly spaced Lagrange points (see generate_lagrange_points()).

Parameters:
  • n (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
Returns:

Matrix containing the coefficients for each base polynomial as column vectors.

Return type:

Numpy array

generate_lagrange_basis_fn_coefficients(nu, r)[source]

Generate monomial coefficients for a Lagrange basis polynomial in the basis for the space \(\mathcal{P}_r(\Delta_c^n)\), with evenly spaced Lagrange points (see generate_lagrange_points()).

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial we should generate monomial coefficients for.
  • r (int) – Degree of polynomial.
Returns:

Array containing the coefficients for the basis polynomial.

Return type:

Numpy array

generate_lagrange_point(n, r, nu)[source]

Generate a Lagrange point indexed by a multi-index from the set of evenly spaced Lagrange points on the n-dimensional unit simplex (\(\Delta_c^n\)) (Lagrange basis points are constructed so that each basis function has the value 1 at one of the points, and 0 at all the other points).

\[x_{\nu} = \frac{\nu}{r}.\]
Parameters:
  • n (int) – Dimension of the simplex.
  • r (int) – Degree of the polynomial.
  • nu – Multi-index \(\nu\) indexing the Lagrange point, where \(\frac{\nu_i}{r}\) gives the i:th coordinate of the Lagrange point.
Returns:

Point in the n-dimensional unit simplex.

Return type:

Numpy array

Examples

>>> generate_lagrange_point(1, 2, (1,))
array([0.5])
>>> generate_lagrange_point(2, 2, (1, 0))
array([0.5, 0. ])
>>> generate_lagrange_point(2, 2, (0, 1))
array([0. , 0.5])
generate_lagrange_points(n, r)[source]

Generate evenly spaced Lagrange points on the n-dimensional unit simplex (\(\Delta_c^n\)) (Lagrange basis points are constructed so that each basis function has the value 1 at one of the points, and 0 at all the other points).

\[\begin{split}\{ x_{\nu} \}_{\substack{\nu \in \mathbb{N}_0^n \\ |\nu| \leq r}}, x_{\nu} = \frac{\nu}{r}.\end{split}\]
Parameters:
  • n (int) – Dimension of the simplex.
  • r (int) – Degree of the polynomial.
Returns:

List of points in the n-dimensional unit simplex.

Return type:

Numpy array

Examples

>>> generate_lagrange_points(1, 2)
array([0. , 0.5, 1. ])
>>> generate_lagrange_points(2, 2)
array([[0. , 0. ],
       [0.5, 0. ],
       [1. , 0. ],
       [0. , 0.5],
       [0.5, 0.5],
       [0. , 1. ]])
get_associated_sub_simplex(nu, r, simplex=None)[source]

Get the sub simplex associated with a Lagrange basis polynomial.

For a Lagrange basis polynomial p on a simplex T there exist a unique sub simplex f of T such that

  • \(p|_f \neq 0\),
  • \(p|_g = 0\) for all \(g \in \Delta_k(T), g \neq f\),
  • \(\dim f < \dim h\), where \(h\) is any other sub simplex of T for which the above two conditions hold,

where \(\Delta_k(T)\) is the set of all k-dimensional sub simplices of T and \(k = \dim f\).

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating for which Lagrange basis polynomial we should get the associated sub simplex.
  • r (int) – Degree of polynomial.
  • simplex (Optional[List[int]]) – Vertex indices for the vertices of the simplex. [0, 1, …, n] assumed if not specified.
Returns:

Tuple containing the associated sub simplex, and the sub multi-index associated with the sub simplex (where all non-zero entries has been removed).

Examples

>>> get_associated_sub_simplex((0,), 1)
([0], (1,))
>>> get_associated_sub_simplex((1,), 1)
([1], (1,))
>>> get_associated_sub_simplex((1, 0, 1), 2, [1, 2, 3, 4])
([2, 4], (1, 1))
get_lagrange_base_coefficients(r, n)[source]

Get monomial coefficients for all the Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\), with evenly spaced Lagrange points (see generate_lagrange_points()).

Parameters:
  • n (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
Returns:

Matrix containing the coefficients for each base polynomial as column vectors.

Return type:

Numpy array

get_lagrange_basis_fn_coefficients(nu, r)[source]

Get monomial coefficients for a Lagrange basis polynomial in the basis for the space \(\mathcal{P}_r(\Delta_c^n)\), with evenly spaced Lagrange points (see generate_lagrange_points()).

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial we should get monomial coefficients for.
  • r (int) – Degree of polynomial.
Returns:

Array containing the coefficients for the basis polynomial.

Return type:

Numpy array

lagrange_basis(r, n)[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • n (int) – Dimension of the space.
  • r (int) – Degree of the polynomial space.
Returns:

List of base polynomials.

Return type:

List[PolynomialLagrange].

lagrange_basis_fn(nu, r)[source]

Generate a Lagrange basis polynomial on the unit simplex (\(\Delta_c^n\)), where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial should be generated. The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
Returns:

The Lagrange base polynomial as specified by nu and r.

Return type:

PolynomialLagrange.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> lagrange_basis_fn(1, 1)(x1) - x1
0
>>> sp.simplify(lagrange_basis_fn(2, 2)(x1) - (2*x1**2 - x1))
0
>>> sp.simplify(lagrange_basis_fn((1, 1), 2)((x1, x2)) - 4*x1*x2)
0
lagrange_basis_fn_latex(nu, r)[source]

Generate Latex string for a Lagrange basis polynomial on the unit simplex (\(\Delta_c^n\)), where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
Returns:

Latex string for the Lagrange base polynomial as specified by nu and r.

Return type:

str

Examples

>>> lagrange_basis_fn_latex(2, 3)
'-9 / 2 x + 18 x^2 - 27 / 2 x^3'
>>> lagrange_basis_fn_latex((1, 1), 3)
'27 x_1 x_2 - 27 x_1^2 x_2 - 27 x_1 x_2^2'
lagrange_basis_fn_latex_compact(nu, r)[source]

Generate compact Latex string for a Lagrange basis polynomial on the unit simplex (\(\Delta_c^n\)), where n is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial we should generate Latex string for.
  • r (int) – Degree of polynomial.
Returns:

Latex string for the Lagrange base polynomial as specified by nu and r.

Return type:

str

Examples

>>> lagrange_basis_fn_latex_compact(2, 3)
'l_{2, 3}(x)'
>>> lagrange_basis_fn_latex_compact((1, 1), 3)
'l_{(1, 1), 3}(x)'
lagrange_basis_fn_monomial(nu, r)[source]

Generate a Lagrange basis polynomial on the unit simplex (\(\Delta_c^n\)), where n is equal to the length of nu, expanded in the monomial basis.

This is the same polynomial as given by the lagrange_basis_fn() function, but expressed in the monomial basis.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which Lagrange basis polynomial should be generated The polynomial will have the value 1 at the point associated with the multi-index, and value 0 at all other points.
  • r (int) – Degree of polynomial.
Returns:

The Lagrange base polynomial as specified by nu and r.

Return type:

Polynomial.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> lagrange_basis_fn(1, 1)(x1) - x1
0
>>> sp.simplify(lagrange_basis_fn(2, 2)(x1) - (2*x1**2 - x1))
0
>>> sp.simplify(lagrange_basis_fn((1, 1), 2)((x1, x2)) - 4*x1*x2)
0
lagrange_basis_latex(r, n)[source]

Generate Latex strings for all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • n (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
Returns:

List of Latex strings for each Lagrange base polynomial.

Return type:

List[str]

Examples

>>> lagrange_basis_latex(2, 1)
['1 - 3 x + 2 x^2', '4 x - 4 x^2', '-x + 2 x^2']
>>> basis_strings = lagrange_basis_latex(2, 2)
>>> expected_basis_strings = list()
>>> expected_basis_strings.append('1 - 3 x_1 + 2 x_1^2 - 3 x_2 + 4 x_1 x_2 + 2 x_2^2')
>>> expected_basis_strings.append('4 x_1 - 4 x_1^2 - 4 x_1 x_2')
>>> expected_basis_strings.append('-x_1 + 2 x_1^2')
>>> expected_basis_strings.append('4 x_2 - 4 x_1 x_2 - 4 x_2^2')
>>> expected_basis_strings.append('4 x_1 x_2')
>>> expected_basis_strings.append('-x_2 + 2 x_2^2')
>>> basis_strings == expected_basis_strings
True
lagrange_basis_latex_compact(r, n)[source]

Generate compact Latex strings for all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\).

Parameters:
  • n (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
Returns:

List of Latex strings for each Lagrange base polynomial.

Return type:

List[str]

Examples

>>> lagrange_basis_latex_compact(2, 1)
['l_{0, 2}(x)', 'l_{1, 2}(x)', 'l_{2, 2}(x)']
>>> lagrange_basis_latex_compact(1, 2)
['l_{(0, 0), 1}(x)', 'l_{(1, 0), 1}(x)', 'l_{(0, 1), 1}(x)']
lagrange_basis_monomial(r, n)[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^n)\), expanded in the monomial basis.

This is the same set of polynomials as given by the lagrange_basis() function, but expressed in the monomial basis.

Parameters:
  • n (int) – Dimension of the space.
  • r (int) – Degree of the polynomial space.
Returns:

List of base polynomials.

Return type:

List[Polynomial].

unique_identifier_lagrange_basis()[source]

Get unique identifier for the Lagrange polynomial basis on the unit simplex.

Returns:Unique identifier.
Return type:str
unit_polynomial(r=0, m=1, n=1)[source]

Get the Lagrange polynomial \(l \in \mathcal{P}(\Delta_c^m, \mathbb{R}^n)\) which is identically one.

Parameters:
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
  • r (int) – The unit polynomial will be expressed in the Lagrange basis for \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\).
Returns:

The unit polynomial.

Return type:

PolynomialLagrange.

vector_valued_lagrange_basis(r, m, n, ordering='interleaved')[source]

Generate all Lagrange base polynomials for the space \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\).

Parameters:
  • m (int) – Dimension of the unit simplex.
  • r (int) – Degree of the polynomial space.
  • n (int) – Dimension of the target.
  • ordering (str) – How the vector valued basis functions are ordered. Can be “sequential” or “interleaved”. For sequential, sorting is first done on the index of the component that is non-zero, and then the non-zero component is sorted in the same way as the scalar valued basis functions. For “interleaved” basis functions are first sorted on their non-zero component in the same way as scalar valued basis functions, and then they are sorted on the index of the component that is non-zero.
Returns:

List of base polynomials.

Return type:

List[PolynomialLagrange].

vector_valued_lagrange_basis_fn(nu, r, i, n)[source]

Generate a vector valued Lagrange basis polynomial on the m-dimensional unit simplex, \(l_{\nu, r, i} : \Delta_c^m \to \mathbb{R}^n\).

The vector valued basis polynomial is generated by specifying a scalar valued basis polynomial and the component of the vector valued basis polynomial that should be equal to the scalar valued basis polynomial. All other components of the vector valued basis polynomial will be zero, i.e.

\[\begin{split}l_{\nu, r, i}^j (x) = \begin{cases} l_{\nu, r} (x), & i = j \\ 0, & \text{else} \end{cases},\end{split}\]

where m is equal to the length of nu.

Parameters:
  • nu (int or MultiIndex or Tuple[int, …]) – Multi-index indicating which scalar valued Lagrange basis polynomial should be generated for the non-zero component.
  • r (int) – Degree of polynomial.
  • i (int) – Index of the vector component that is non-zero.
  • n (int) – Dimension of the target.
Returns:

The Lagrange base polynomial as specified by nu, r, i and n.

Return type:

PolynomialLagrange.

Examples

>>> import sympy as sp
>>> x1, x2 = sp.symbols('x1 x2')
>>> vector_valued_lagrange_basis_fn(0, 1, 0, 2)(x1)
array([-x1 + 1, 0], dtype=object)
>>> vector_valued_lagrange_basis_fn(1, 1, 1, 2)(x1)
array([0, x1], dtype=object)
>>> vector_valued_lagrange_basis_fn((1, 0), 2, 0, 2)((x1, x2))
array([-4*x1**2 - 4*x1*x2 + 4*x1, 0], dtype=object)
>>> vector_valued_lagrange_basis_fn((1, 1), 3, 1, 3)((x1, x2))
array([0, -27*x1**2*x2 - 27*x1*x2**2 + 27*x1*x2, 0], dtype=object)
zero_polynomial(r=0, m=1, n=1)[source]

Get the Lagrange polynomial \(l \in \mathcal{P}(\Delta_c^m, \mathbb{R}^n)\) which is identically zero.

Parameters:
  • m (int) – Dimension of the polynomial domain.
  • n (int) – Dimension of the polynomial target.
  • r (int) – The zero polynomial will be expressed in the Lagrange basis for \(\mathcal{P}_r(\Delta_c^m, \mathbb{R}^n)\).
Returns:

The zero polynomial.

Return type:

PolynomialLagrange.

polynomials_on_simplices.polynomial.polynomials_unit_simplex_lagrange_basis_cache module

Cached evaluation of coefficients for Lagrange basis polynomials on the unit simplex.

Module contents
polynomials_on_simplices.probability_theory package
Submodules
polynomials_on_simplices.probability_theory.uniform_sampling module

Uniform sampling of random points in different geometries.

closed_unit_interval_sample()[source]

Generate a random number sampled from the uniform distribution over the closed unit interval [0, 1].

Returns:Random number.
closed_unit_interval_sampling(num_points)[source]

Uniform random sampling of points in the closed unit interval [0, 1].

Parameters:num_points – Number of points to sample.
Returns:List of random points.
left_closed_interval_sample()[source]

Generate a random number sampled from the uniform distribution over the left closed unit interval [0, 1).

Returns:Random number.
left_closed_unit_interval_sampling(num_points)[source]

Uniform random sampling of points in the unit interval [0, 1).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
ncube_sampling(n, num_points)[source]

Uniform random sampling of points in the n-dimensional unit cube \([0, 1)^n\).

Parameters:
  • n – Dimension of cube.
  • num_points – Number of points to sample.
Returns:

List of random points.

nsimplex_sampling(n, num_points)[source]

Uniform random sampling of points inside the n-dimensional unit simplex.

See polynomials_on_simplices.geometry.primitives.simplex.unit().

Parameters:
  • n – Dimension of the simplex.
  • num_points – Number of points to sample.
Returns:

List of random points.

nsphere_sampling(n, num_points)[source]

Uniform random sampling of points in the n-dimensional unit sphere \(\{x \in \mathbb{R}^n : \|x\| \leq 1\}\).

Parameters:
  • n – Dimension of sphere.
  • num_points – Number of points to sample.
Returns:

List of random points.

nsphere_surface_sampling(n, num_points)[source]

Uniform random sampling of points on the surface of the n-dimensional unit sphere (\(\partial B^n = S^{n-1}\)).

Parameters:
  • n – Dimension of sphere.
  • num_points – Number of points to sample.
Returns:

List of random points.

open_unit_interval_sample()[source]

Generate a random number sampled from the uniform distribution over the open unit interval (0, 1).

Returns:Random number.
open_unit_interval_sampling(num_points)[source]

Uniform random sampling of points in the unit interval (0, 1).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
right_closed_interval_sample()[source]

Generate a random number sampled from the uniform distribution over the right closed unit interval (0, 1].

Returns:Random number.
right_closed_unit_interval_sampling(num_points)[source]

Uniform random sampling of points in the unit interval (0, 1].

Parameters:num_points – Number of points to sample.
Returns:List of random points.
unit_circle_sampling(num_points)[source]

Uniform random sampling of points on the unit circle (\(S^1\)).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
unit_disc_sampling(num_points)[source]

Uniform random sampling of points in the unit disc \(\{x \in \mathbb{R}^2 : \|x\| \leq 1\}\).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
unit_interval_sampling(num_points)[source]

Uniform random sampling of points in the unit interval [0, 1).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
unit_square_sampling(num_points)[source]

Uniform random sampling of points in the unit square [0, 1) x [0, 1).

Parameters:num_points – Number of points to sample.
Returns:List of random points.
Module contents
polynomials_on_simplices.visualization package
Submodules
polynomials_on_simplices.visualization.plot_lines module

Functionality for plotting lines.

plot_curve(vertices, *args, **kwargs)[source]

Plot a discrete curve consisting of a set of connected vertices.

Parameters:
  • vertices – The curve vertices (num_vertices by [23] array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
plot_plane_curve(vertices, *args, **kwargs)[source]

Plot a discrete plane curve consisting of a set of connected vertices.

Parameters:
  • vertices – The curve vertices (num_vertices by 2 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
plot_space_curve(vertices, *args, **kwargs)[source]

Plot a discrete space curve consisting of a set of connected vertices.

Parameters:
  • vertices – The curve vertices (num_vertices by 3 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
polynomials_on_simplices.visualization.plot_triangles module

Functionality for plotting triangles.

plot_triangle_mesh(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by [23] array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
plot_triangle_mesh_2d(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by 2 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
plot_triangle_mesh_3d(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by 3 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. Further keyword arguments are passed to the plot command.
plot_triangle_mesh_wireframe(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh as a wireframe.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by [23] array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments passed to the plot command.
plot_triangle_mesh_wireframe_2d(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh as a wireframe.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by 2 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. ‘color’: Color of the wireframe. Further keyword arguments are passed to the plot command.
plot_triangle_mesh_wireframe_3d(triangles, vertices, *args, **kwargs)[source]

Plot a triangle mesh as a wireframe.

Parameters:
  • triangles – The triangles in the mesh (num_triangles by 3 array of integers).
  • vertices – The vertices in the mesh (num_vertices by 3 array of floats).
  • args – Further arguments passed to the plot command.
  • kwargs – Keyword arguments. ‘fig’: Figure to populate. A new figure will be generated if this argument is not given. ‘color’: Color of the wireframe. Further keyword arguments are passed to the plot command.
Module contents

Submodules

polynomials_on_simplices.set_theory module

Basic set theoretic calculations.

cartesian_product(s, t)[source]

Compute the Cartesian product of two finite sets, \(S \times T\).

Parameters:
  • s (set) – First set in the Cartesian product.
  • t (set) – Second set in the Cartesian product.
Returns:

The Cartesian product of the two sets.

Return type:

set

>>> prod = cartesian_product({1, 2}, {1, 2})
>>> prod == {(1, 1), (1, 2), (2, 1), (2, 2)}
True
nfold_cartesian_product(s, n)[source]

Compute the n-fold Cartesian product of a finite set, \(\underbrace{S \times S \times \ldots \times S}_{n \text{ times}}\).

Parameters:
  • s – Set we take the n-fold Cartesian product of.
  • n (int) – Number of times we take the Cartesian product.
Returns:

n-fold Cartesian product of the given set.

Return type:

set

>>> prod = nfold_cartesian_product({1, 2}, 3)
>>> prod == {(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)}
True
polynomials_on_simplices.sort_imports module

Sort imports in all library modules using isort.

sort_imports()[source]

Sort imports in all library modules using isort. Sorting is controlled by the settings in the .isort.cfg file.

Module contents

Python library containing generic math functionality.

Indices and tables