UFL utils#
Mixin class for types with a global unique counter attached to each object.
- class ufl.utils.counted.Counted(count=None, counted_class=None)[source]#
Bases:
objectMixin class for globally counted objects.
Initialize the Counted instance.
- Parameters:
count – The object count, if
Nonedefaults to the next value according to the global counter (per type).counted_class – Class to attach the global counter too. If
Nonethentype(self)will be used.
Various string formatting utilities.
- ufl.utils.formatting.camel2underscore(name)[source]#
Convert a CamelCaps string to underscore_syntax.
- ufl.utils.formatting.lstr(a)[source]#
Pretty-print list or tuple, invoking str() on items instead of repr() like str() does.
Collection of utilities for mapping between multiindices and a flattened index space.
- ufl.utils.indexflattening.flatten_multiindex(ii, strides)[source]#
Return the flat index corresponding to the given multiindex.
- ufl.utils.indexflattening.shape_to_strides(sh)[source]#
Return a tuple of strides given a shape tuple.
- ufl.utils.indexflattening.unflatten_index(i, strides)[source]#
Return the multiindex corresponding to the given flat index.
Various sequence manipulation utilities.
- ufl.utils.sequences.max_degree(degrees)[source]#
Maximum degree for mixture of scalar and tuple degrees.
- ufl.utils.sequences.reduce(function, iterable[, initial]) value#
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
This effectively reduces the iterable to a single value. If initial is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1 + 2) + 3) + 4) + 5).
Utilites for sorting.
- ufl.utils.sorting.canonicalize_metadata(metadata: None | dict | str | list | tuple | ndarray) tuple[source]#
Assuming metadata to be a dict with string keys and builtin python types as values.
Transform dict to a tuple of (key, value) item tuples ordered by key, with dict, list and tuple values converted the same way recursively. Lists and tuples are converted to tuples. Other values are converted using str(). This is such that the end result can be hashed and sorted using regular <, because python 3 doesn’t allow e.g. (3 < “auto”) which occurs regularly in metadata.
- ufl.utils.sorting.sorted_by_key(mapping)[source]#
Sort dict items by key, allowing different key types.
- ufl.utils.sorting.topological_sorting(nodes, edges)[source]#
Return a topologically sorted list of the nodes.
Implemented algorithm from Wikipedia (http://en.wikipedia.org/wiki/Topological_sorting).
No error for cyclic edges…
Various utility data structures.