use_cython
full name: tenpy.tools.optimization.use_cython
parent module:
tenpy.tools.optimization
type: function
- 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[0]): for j in range(a.shape[1]): #... 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 variableTENPY_OPTIMIZE
is set to negative values, or the environment variableTENPY_NO_CYTHON
is “true”), we just pass the previous function.Note
In case that the decorator is used for a class method, the corresponding Cython version needs to have an
@cython.binding(True)
.- Parameters:
func (function) – The defined function
replacement (string | None) – The name of the function defined in
tenpy/linalg/_npc_helper.pyx
which should replace the decorated function.None
defaults 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.
- Returns:
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:
function