full name: tenpy.tools.optimization.use_cython
- tenpy.tools.optimization.use_cython(func=None, replacement=None, check_doc=True)[source]¶
Decorator to replace a function with a Cython-equivalent from _npc_helper.pyx.
This is a decorator, which is supposed to be used in front of function definitions with an
@sign, for example:
@use_cython def my_slow_function(a): "some example function with slow python loops" result = 0. for i in range(a.shape): for j in range(a.shape): #... heavy calculations ... result += np.cos(a[i, j]**2) * (i + j) return result
This decorator indicates that there is a Cython implementation in the file
tenpy/linalg/_npc_helper.pyx, which should have the same signature (i.e. same arguments and return values) as the decorated function, and can be used as a replacement for the decorated function. However, if the cython code could not be compiled on your system (or if the environment variable
TENPY_OPTIMIZEis set to negative values, or the environment variable
TENPY_NO_CYTHONis “true”), we just pass the previous function.
In case that the decorator is used for a class method, the corresponding Cython version needs to have an
func (function) – The defined function
replacement (string | None) – The name of the function defined in
tenpy/linalg/_npc_helper.pyxwhich should replace the decorated function.
Nonedefaults to the name of the decorated function, e.g., in the above example my_slow_function.
check_doc (bool) – If True, we check that the cython version of the function has the exact same doc string (up to a possible first line containing the function signature) to exclude typos and inconsistent versions.
replacement_func – The function replacing the decorated function func. If the cython code can not be loaded, this is just func, otherwise it’s the cython version specified by replacement.
- Return type