Expression class

(Shortest import: from brian2 import Expression)

class brian2.equations.codestrings.Expression(code=None, sympy_expression=None, **substitutions)[source]

Bases: CodeString

Class for representing an expression with support for substitution.

Parameters:

code : str, optional

The expression. Note that the expression has to be written in a form that is parseable by sympy. Alternatively, a sympy expression can be provided (in the sympy_expression argument).

sympy_expression : sympy expression, optional

A sympy expression. Alternatively, a plain string expression can be provided (in the code argument).

**substitutions :

Substitutions to apply to the expression. Can be either strings (to replace a name with another name) or values (to replace a name with a value).

Examples

>>> Expression('g + k*w')
Expression('g + k*w')
>>> Expression('g + k*w', k=0.3)
Expression('g + (0.3)*w')
>>> Expression('g + k*w', g='g_ampa')
Expression('g_ampa + k*w')
>>> Expression('g + k*w', g='g_ampa', k=0.3)
Expression('g_ampa + (0.3)*w')

Attributes

stochastic_variables

Stochastic variables in this expression

Methods

split_stochastic()

Split the expression into a stochastic and non-stochastic part.

Details

stochastic_variables

Stochastic variables in this expression

split_stochastic()[source]

Split the expression into a stochastic and non-stochastic part.

Splits the expression into a tuple of one Expression objects f (the non-stochastic part) and a dictionary mapping stochastic variables to Expression objects. For example, an expression of the form f + g * xi_1 + h * xi_2 would be returned as: (f, {'xi_1': g, 'xi_2': h}) Note that the Expression objects for the stochastic parts do not include the stochastic variable itself.

Returns:

(f, d) : (Expression, dict)

A tuple of an Expression object and a dictionary, the first expression being the non-stochastic part of the equation and the dictionary mapping stochastic variables (xi or starting with xi_) to Expression objects. If no stochastic variable is present in the code string, a tuple (self, None) will be returned with the unchanged Expression object.