pathsim.utils.utils module

pathsim.utils.utils.dict_to_array(a)[source]

convert a dict with integer keys to a numpy array

Parameters:

a (dict[int: int, float, complex]) – dict to convert to numpy array

Returns:

out – converted array

Return type:

array[int, float, complex]

pathsim.utils.utils.array_to_dict(a)[source]

convert a numpy array to a dict with integer keys

Parameters:

a (array[int, float, complex]) – numpy array to convert

Returns:

out – converted dict

Return type:

dict[int: int, float, complex]

pathsim.utils.utils.rel_error(a, b)[source]

Computes the relative error between two scalars. It is robust to one of them being zero and falls back to the absolute error in this case.

Notes

this is actually faster then inlining the branching into the return statement

Parameters:
Returns:

err – retative error

Return type:

float

pathsim.utils.utils.abs_error(a, b)[source]

Computes the absolute error between two scalars.

Parameters:
Returns:

err – absolute error

Return type:

float

pathsim.utils.utils.max_error(a, b)[source]

Computes the maximum absolute error / deviation between two iterables such as lists with numerical values. Returns a scalar value representing the maximum deviation.

Notes

this is actually faster then ‘max’ over a list comprehension

Parameters:
  • a (iterable[float, int, complex]) – first iterable with numerical values

  • b (iterable[float, int, complex]) – second iterable with numerical values

Returns:

err – maximum absolute error

Return type:

float

pathsim.utils.utils.max_rel_error(a, b)[source]

Computes the maximum relative error between two iterables such as lists with numerical values.

It is robust to one of them being zero and falls back to the absolute error in this case.

It returns a scalar value representing the maximum relative error.

Notes

this is actually faster then ‘max’ over a list comprehension

Parameters:
  • a (iterable[float, int, complex]) – first iterable with numerical values

  • b (iterable[float, int, complex]) – second iterable with numerical values

Returns:

err – maximum retative error

Return type:

float

pathsim.utils.utils.max_error_dicts(a, b)[source]

Computes the maximum absolute error between two dictionaries with numerical values.

It returns a scalar value representing the maximum absolute error.

Parameters:
  • a (dict[int: float, int, complex]) – first dict with numerical values

  • b (dict[int: float, int, complex]) – second iterable with numerical values

Returns:

err – maximum absolute error

Return type:

float

pathsim.utils.utils.max_rel_error_dicts(a, b)[source]

Computes the maximum relative error between two dictionaries with numerical values.

It is robust to one of them being zero and falls back to the absolute error in this case.

It returns a scalar value representing the maximum relative error.

Parameters:
  • a (dict[int: float, int, complex]) – first dict with numerical values

  • b (dict[int: float, int, complex]) – second iterable with numerical values

Returns:

err – maximum relative error

Return type:

float

pathsim.utils.utils.numerical_jacobian(func, x, h=1e-08)[source]

Numerically computes the jacobian of the function ‘func’ by central differences.

With the stepsize ‘h’ which is set to a default value of ‘h=1e-8’ which is the point where the truncation error of the central differences balances with the machine accuracy of 64bit floating point numbers.

Parameters:
  • func (callable) – function to compute jacobian for

  • x (float, array[float]) – value for function at which the jacobian is evaluated

  • h (float) – step size for central differences

Returns:

jac – 2d jacobian array

Return type:

array[array[float]]

pathsim.utils.utils.auto_jacobian(func)[source]

Wraps a function object such that it computes the jacobian of the function with respect to the first argument.

This is intended to compute the jacobian ‘jac(x, u, t)’ of the right hand side function ‘func(x, u, t)’ of numerical integrators with respect to ‘x’.

pathsim.utils.utils.path_length_dfs(connections, starting_block, visited=None)[source]

Recursively compute the longest path (depth first search) in a directed graph from a starting node / block.

Parameters:
  • connections (list[Connection]) – connections of the graph

  • starting_block (Block) – block to start dfs

  • visited (None, set) – set of already visited graph nodes (blocks)

Returns:

length – length of path starting from ´starting_block´

Return type:

int