util

The util module provides a collection of general purpose methods.

nutils.util.sum()

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

nutils.util.product()

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

class nutils.util.NanVec(length)

Bases: numpy.ndarray

nan-initialized vector

nutils.util.tri_merge(tri, x, mergetol=0)

Create connected triangulation by connecting (near) identical points.

Based on a set of coordinates x, create a modified copy of tri with any occurrence of j replaced by i if x[i] equals x[j] within specified tolerance. The result is a triangulation that remains valid for any associated data vector that follows the same equality relations.

Example:

>>> x = [0,0], [1,0], [0,1], [1,0], [1,1] # note: x[1] == x[3])
>>> tri = [0,1,2], [2,3,4]
>>> tri_merge(tri, x)
array([[0, 1, 2],
       [2, 1, 4]])

Requires scipy.

Parameters
  • x (float array) – Vertex coordinates.

  • tri (int array) – Triangulation.

  • mergetol (float (optional, default 0)) – Distance within which two points are considered equal. If mergetol == 0 then points are considered equal if and only if their coordinates are identical. If mergetol > 0 (required scipy) then points are considered equal if they are within euclidian distance < mergetol. If mergetol < 0 then tri is returned unchanged.

Returns

merged_tri

Return type

int array

class nutils.util.tri_interpolator(tri, x, mergetol=0)

Bases: object

Interpolate function values defined in triangulation vertices.

Convenience object that implements 2D interpolation on top of matplotlib’s triangulation routines. Unlike matplotlib’s own LinearTriInterpolator, the tri_interpolator allows for interpolation of multi-dimensional arrays, as well as repeated interpolations of different vertex values.

The arguments are identical to tri_merge().

After instantiation of the interpolator object, interpolation coordinates are specified via the object’s getitem operator. The resulting callable performs the interpolation:

>>> trix = [0,0], [1,0], [0,1], [1,1] # vertex coordinates
>>> triu = 0, 0, 10, 0 # vertex values
>>> interpolate = tri_interpolator([[0,1,2],[1,3,2]], trix)
>>> x = [.1,.1], [.1,.9], [.9,.9] # interpolation coordinates
>>> u = interpolate[x](triu) # interpolated values

Requires matplotlib.

__weakref__

list of weak references to the object (if defined)

nutils.util.obj2str(obj)

compact, lossy string representation of arbitrary object

nutils.util.single_or_multiple(f)

Method wrapper, converts first positional argument to tuple: tuples/lists are passed on as tuples, other objects are turned into tuple singleton. Return values should match the length of the argument list, and are unpacked if the original argument was not a tuple/list.

>>> class Test:
...   @single_or_multiple
...   def square(self, args):
...     return [v**2 for v in args]
...
>>> T = Test()
>>> T.square(2)
4
>>> T.square([2,3])
(4, 9)
Parameters

f (callable) – Method that expects a tuple as first positional argument, and that returns a list/tuple of the same length.

Returns

Wrapped method.

nutils.util.positional_only(*names, keep_varpositional=False)

Add var-positional arguments to function signature.

Python has no explicit syntax for defining positional-only parameters, but the effect can be achieved by using a var-positional argument and unpacking it inside the function body. The positional_only() decorator adds a check for the number of positional arguments provided, and updates the function signature to reflect this design. It requires that the first argument is var-positional, precluding positional-or-keyword arguments.

Example:

>>> @positional_only('x')
... def f(*args, **kwargs):
...   x, = args
>>> inspect.signature(f)
<Signature (x, /, **kwargs)>
>>> @positional_only('x', keep_varpositional=True)
... def f(*args, **kwargs):
...   x, *args = args
>>> inspect.signature(f)
<Signature (x, /, *args, **kwargs)>
Parameters
  • names (variable argument list of str) – Names of the positional-only arguments, in order.

  • keep_varpositional (bool (default: False)) – If True, retain the var_positional argument.

nutils.util.loadlib(**libname)

Find and load a dynamic library using ctypes.CDLL. For each (supported) platform the name of the library should be specified as a keyword argument, including the extension, where the keywords should match the possible values of sys.platform. In addition to the default directories, this function searches site.PREFIXES and site.getuserbase().

Example

To load the Intel MKL runtime library, write:

loadlib(linux='libmkl_rt.so', darwin='libmkl_rt.dylib', win32='mkl_rt.dll')