Util

Collection of utility functions

ndmap.util.curry_apply(function: Callable, table: tuple[int, ...], *pars: tuple) Callable[source]

Curry apply

Given f(x, y, …) and table = map(len, (x, y, …)) return g(*x, *y, …) = f(x, y, …)

Parameters:
  • function (Callable) – input function

  • table (tuple[int, ...]) – map(len, (x, y, …))

  • *pars (tuple) – passed to input function

Return type:

Callable

Examples

>>> def fn(x, y):
...    x1, x2 = x
...    y1, y2, y3 = y
...    return x1*x2*y1*y2*y3
>>> def gn(x1, x2, y1, y2, y3):
...    return fn((x1, x2), (y1, y2, y3))
>>> x, y = (1, 1), (1, 1, 1)
>>> gn(*x, *y) == curry_apply(fn, (2, 3))(*x, *y)
True
ndmap.util.first(xs: Iterable[Any]) Any[source]

Return first element

Parameters:

xs (Iterable[Any]) – xs

Return type:

Any

Examples

>>> first([1, 2, 3, 4])
1
ndmap.util.flatten(array: ~typing.Iterable, *, target: type = <class 'tuple'>) Iterator[source]

Flatten a nested tuple (or other selected target type container)

Parameters:
  • array (Iterable) – input nested iterable

  • target (type, default=tuple) – target iterable type to flatten

Yields:

Iterator

Examples

>>> [*flatten((1, (1, (1, (1, 1), 1)), ((1), (1))), target=tuple)]
[1, 1, 1, 1, 1, 1, 1, 1]
>>> [*flatten([1, [1, [1, [1, 1], 1]], [[1], [1]]], target=list)]
[1, 1, 1, 1, 1, 1, 1, 1]
ndmap.util.last(xs: Iterable[Any]) Any[source]

Return last element

Parameters:

xs (Iterable[Any]) – xs

Return type:

Any

Examples

>>> last([1, 2, 3, 4])
4
ndmap.util.most(xs: Iterable[Any]) Any[source]

Return all but first element

Parameters:

xs (Iterable[Any]) – xs

Return type:

Any

Examples

>>> most([1, 2, 3, 4])
[2, 3, 4]
ndmap.util.multinomial(*sequence: int) float[source]

Compute multinomial coefficient for a given sequence (n, m, …) of non-negative integers (n + m + …)! / (n! * m! * … )

Parameters:

*sequence (int, non-negative) – input sequence of integers

Return type:

float

Examples

>>> multinomial(2, 0)
1.0
>>> multinomial(1, 1)
2.0
>>> multinomial(0, 2)
1.0
ndmap.util.nest(power: int, function: Callable, *pars: tuple) Callable[source]

Generate nested function

Parameters:
  • power (int) – nest power

  • function (Callable) – function to nest

  • *pars (tuple) – fixed parameters

Return type:

Callable

Examples

>>> nest(5, lambda x: x**2)(2)
4294967296
ndmap.util.orthogonal(n: int, m: int, *, dtype: torch.dtype = torch.float64, device: torch.device = torch.device, **kwargs) torch.Tensor[source]

Generate random orthonormal (n x m) matrix

Parameters:
  • n (int) – n, m

  • m (int) – n, m

  • dtype (torch.dtype, default=torch.float64) – output type

  • device (torch.device, torch.device=torch.device('cpu')) – output device

  • **kwargs (dict) – passed to torch.linalg.svd function

Return type:

Tensor

Examples

>>> import torch
>>> generator = torch.manual_seed(1)
>>> orthogonal(4, 4)
tensor([[-0.4048, -0.7515, -0.5066, -0.1216],
        [ 0.1141, -0.5599,  0.6068,  0.5525],
        [ 0.1797,  0.1702, -0.5821,  0.7746],
        [-0.8893,  0.3046,  0.1909,  0.2827]], dtype=torch.float64)
ndmap.util.rest(xs: Iterable[Any]) Any[source]

Return all but last element

Parameters:

xs (Iterable[Any]) – xs

Return type:

Any

Examples

>>> rest([1, 2, 3, 4])
[1, 2, 3]
ndmap.util.symplectic(state: torch.Tensor) torch.Tensor[source]

Generate symplectic identity matrix for a given state

Parameters:

state (Tensor) – state

Return type:

Tensor

Examples

>>> import torch
>>> state = torch.tensor([0.0, 0.0])
>>> symplectic(state)
tensor([[ 0.,  1.],
        [-1.,  0.]])
>>> state = torch.tensor([0.0, 0.0, 0.0, 0.0])
>>> symplectic(state)
tensor([[ 0.,  1.,  0.,  0.],
        [-1.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  1.],
        [ 0.,  0., -1.,  0.]])
ndmap.util.tolist(tensor: numpy.ndarray) list

Convert input (gradtracking) tensor to list

Note, emmits storage deprication warning

Parameters:

tensor (Array) – input tensor

Return type:

list

Examples

>>> import numpy
>>> tolist(numpy.array([0.0, 0.0, 0.0, 0.0]))
[0.0, 0.0, 0.0, 0.0]