implementation function

(Shortest import: from brian2 import implementation)

brian2.core.functions.implementation(target, code=None, namespace=None, dependencies=None, discard_units=None)[source]

A simple decorator to extend user-written Python functions to work with code generation in other languages.


target : str

Name of the code generation target (e.g. 'weave') for which to add an implementation.

code : str or dict-like, optional

What kind of code the target language expects is language-specific, e.g. C++ code allows for a dictionary of code blocks instead of a single string.

namespaces : dict-like, optional

A namespace dictionary (i.e. a mapping of names to values) that should be added to a CodeObject namespace when using this function.

dependencies : dict-like, optional

A mapping of names to Function objects, for additional functions needed by this function.

discard_units: bool, optional :

Numpy functions can internally make use of the unit system. However, during a simulation run, state variables are passed around as unitless values for efficiency. If discard_units is set to False, input arguments will have units added to them so that the function can still use units internally (the units will be stripped away from the return value as well). Alternatively, if discard_units is set to True, the function will receive unitless values as its input. The namespace of the function will be altered to make references to units (e.g. ms) refer to the corresponding floating point values so that no unit mismatch errors are raised. Note that this system cannot work in all cases, e.g. it does not work with functions that internally imports values (e.g. does from brian2 import ms) or access values with units indirectly (e.g. uses instead of ms). If no value is given, defaults to the preference setting codegen.runtime.numpy.discard_units.


While it is in principle possible to provide a numpy implementation as an argument for this decorator, this is normally not necessary – the numpy implementation should be provided in the decorated function.

If this decorator is used with other directors such as check_units() or declare_types(), it should be the uppermost decorator (that is, the last one to be applied).


Sample usage:

            inline double usersin(double x)
                return sin(x);
def usersin(x):
    return sin(x)