Function class
(Shortest import: from brian2 import Function)
- class brian2.core.functions.Function(pyfunc, sympy_func=None, arg_units=None, arg_names=None, return_unit=None, arg_types=None, return_type=None, stateless=True, auto_vectorise=False)[source]
Bases:
object
An abstract specification of a function that can be used as part of model equations, etc.
- Parameters:
pyfunc : function
A Python function that is represented by this
Function
object.sympy_func :
sympy.Function
, optionalA corresponding sympy function (if any). Allows functions to be interpreted by sympy and potentially make simplifications. For example,
sqrt(x**2)
could be replaced byabs(x)
.arg_units : list of
Unit
, optionalIf
pyfunc
does not provide unit information (which typically means that it was not annotated with acheck_units()
decorator), the units of the arguments have to specified explicitly using this parameter.return_unit :
Unit
or callable, optionalSame as for
arg_units
: ifpyfunc
does not provide unit information, this information has to be provided explictly here.return_unit
can either be a specificUnit
, if the function always returns the same unit, or a function of the input units, e.g. a “square” function would return the square of its input units, i.e.return_unit
could be specified aslambda u: u**2
.arg_types : list of str, optional
Similar to
arg_units
, but gives the type of the argument rather than its unit. In the current version of Brian arguments are specified by one of the following strings: ‘boolean’, ‘integer’, ‘float’, ‘any’. Ifarg_types
is not specified, ‘any’ will be assumed. In future versions, a more refined specification may be possible. Note that any argument with a type other than float should have no units. Ifreturn_type : str, optional
Similar to
return_unit
andarg_types
. In addition to ‘boolean’, ‘integer’ and ‘float’ you can also use ‘highest’ which will return the highest type of its arguments. You can also give a function, as forreturn_unit
. If the return type is not specified, it is assumed to be ‘float’.stateless : bool, optional
Whether this function does not have an internal state, i.e. if it always returns the same output when called with the same arguments. This is true for mathematical functions but not true for
rand()
, for example. Defaults toTrue
.auto_vectorise : bool, optional
Whether the implementations of this function should get an additional argument (not specified in abstract code) that can be used to determine the number of values that should be returned (for the numpy target), or an index potentially useful for generating deterministic values independent of the order of vectorisation (for all other targets). The main use case are random number functions, e.g. equations refer to
rand()
, but the generate code will actually callrand(_vectorisation_idx)
. Defaults toFalse
.
Notes
If a function should be usable for code generation targets other than Python/numpy, implementations for these target languages have to be added using the
implementation
decorator or using theadd_implementations
function.Attributes
Stores implementations for this function in a
FunctionImplementationContainer
Methods
__call__
(*args)Call self as a function.
Return whether this function (if interpreted as a function of time) should be considered constant over a timestep.
Details
- implementations
Stores implementations for this function in a
FunctionImplementationContainer
- is_locally_constant(dt)[source]
Return whether this function (if interpreted as a function of time) should be considered constant over a timestep. This is most importantly used by
TimedArray
so that linear integration can be used. In its standard implementation, always returnsFalse
.- Parameters:
dt : float
The length of a timestep (without units).
- Returns:
constant : bool
Whether the results of this function can be considered constant over one timestep of length
dt
.