str_to_sympy function

(Shortest import: from brian2.parsing.sympytools import str_to_sympy)

brian2.parsing.sympytools.str_to_sympy(expr)[source]

Parses a string into a sympy expression. There are two reasons for not using sympify directly: 1) sympify does a from sympy import *, adding all functions to its namespace. This leads to issues when trying to use sympy function names as variable names. For example, both beta and factor – quite reasonable names for variables – are sympy functions, using them as variables would lead to a parsing error. 2) We want to use a common syntax across expressions and statements, e.g. we want to allow to use and (instead of &) and function names like ceil (instead of ceiling).

Parameters:

expr : str

The string expression to parse..

Returns:

s_expr :

A sympy expression

Raises

SyntaxError
In case of any problems during parsing.

Notes

Parsing is done in two steps: First, the expression is parsed and rendered as a new string by SympyNodeRenderer, translating function names (e.g. ceil to ceiling) and operator names (e.g. and to &), all unknown names are wrapped in Symbol(...) or Function(...). The resulting string is then evaluated in the from sympy import * namespace.