| 1 |
""" |
| 2 |
Module providing the `Statement` class. |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
class Statement: |
| 7 |
""" |
| 8 |
A single line mathematical statement. |
| 9 |
|
| 10 |
The structure is ``var op expr``. |
| 11 |
|
| 12 |
Parameters |
| 13 |
---------- |
| 14 |
var : str |
| 15 |
The left hand side of the statement, the value being written to. |
| 16 |
op : str |
| 17 |
The operation, can be any of the standard Python operators (including |
| 18 |
``+=`` etc.) or a special operator ``:=`` which means you are defining |
| 19 |
a new symbol (whereas ``=`` means you are setting the value of an |
| 20 |
existing symbol). |
| 21 |
expr : str, `Expression` |
| 22 |
The right hand side of the statement. |
| 23 |
dtype : `dtype` |
| 24 |
The numpy dtype of the value or array `var`. |
| 25 |
constant : bool, optional |
| 26 |
Set this flag to ``True`` if the value will not change (only applies for |
| 27 |
``op==':='``. |
| 28 |
subexpression : bool, optional |
| 29 |
Set this flag to ``True`` if the variable is a subexpression. In some |
| 30 |
languages (e.g. Python) you can use this to save a memory copy, because |
| 31 |
you don't need to do ``lhs[:] = rhs`` but a redefinition ``lhs = rhs``. |
| 32 |
scalar : bool, optional |
| 33 |
Set this flag to ``True`` if `var` and `expr` are scalar. |
| 34 |
|
| 35 |
Notes |
| 36 |
----- |
| 37 |
Will compute the following attribute: |
| 38 |
|
| 39 |
``inplace`` |
| 40 |
True or False depending if the operation is in-place or not. |
| 41 |
|
| 42 |
Boolean simplification notes: |
| 43 |
|
| 44 |
Will initially set the attribute ``used_boolean_variables`` to ``None``. |
| 45 |
This is set by `~brian2.codegen.optimisation.optimise_statements` when it |
| 46 |
is called on a sequence of statements to the list of boolean variables |
| 47 |
that are used in this expression. In addition, the attribute |
| 48 |
``boolean_simplified_expressions`` is set to a dictionary with keys |
| 49 |
consisting of a tuple of pairs ``(var, value)`` where ``var`` is the |
| 50 |
name of the boolean variable (will be in ``used_boolean_variables``) |
| 51 |
and ``var`` is ``True`` or ``False``. The values of the dictionary are |
| 52 |
strings representing the simplified version of the expression if each |
| 53 |
``var=value`` substitution is made for that key. The keys will range |
| 54 |
over all possible values of the set of boolean variables. The complexity |
| 55 |
of the original statement is set as the attribute ``complexity_std``, |
| 56 |
and the complexity of the simplified versions are in the dictionary |
| 57 |
``complexities`` (with the same keys). |
| 58 |
|
| 59 |
This information can be used to generate code that replaces a complex |
| 60 |
expression that varies depending on the value of one or more boolean |
| 61 |
variables with an ``if/then`` sequence where each subexpression is |
| 62 |
simplified. It is optional to use this (e.g. the numpy codegen does |
| 63 |
not, but the cython one does). |
| 64 |
""" |
| 65 |
|
| 66 |
def __init__( |
| 67 |
self, |
| 68 |
var, |
| 69 |
op, |
| 70 |
expr, |
| 71 |
comment, |
| 72 |
dtype, |
| 73 |
constant=False, |
| 74 |
subexpression=False, |
| 75 |
scalar=False, |
| 76 |
): |
| 77 |
self.var = var.strip() |
| 78 |
self.op = op.strip() |
| 79 |
self.expr = expr |
| 80 |
self.comment = comment |
| 81 |
self.dtype = dtype |
| 82 |
self.constant = constant |
| 83 |
self.subexpression = subexpression |
| 84 |
self.scalar = scalar |
| 85 |
if constant and self.op != ":=": |
| 86 |
raise ValueError(f"Should not set constant flag for operation {self.op}") |
| 87 |
if op.endswith("=") and op != "=" and op != ":=": |
| 88 |
self.inplace = True |
| 89 |
else: |
| 90 |
self.inplace = False |
| 91 |
self.used_boolean_variables = None |
| 92 |
self.boolean_simplified_expressions = None |
| 93 |
|
| 94 |
def __str__(self): |
| 95 |
s = f"{self.var} {self.op} {str(self.expr)}" |
| 96 |
if self.constant: |
| 97 |
s += " (constant)" |
| 98 |
if self.subexpression: |
| 99 |
s += " (subexpression)" |
| 100 |
if self.inplace: |
| 101 |
s += " (in-place)" |
| 102 |
if len(self.comment): |
| 103 |
s += f" # {self.comment}" |
| 104 |
return s |
| 105 |
|
| 106 |
__repr__ = __str__ |