File: statements.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (106 lines) | stat: -rw-r--r-- 3,870 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Module providing the `Statement` class.
"""


class Statement:
    """
    A single line mathematical statement.

    The structure is ``var op expr``.

    Parameters
    ----------
    var : str
        The left hand side of the statement, the value being written to.
    op : str
        The operation, can be any of the standard Python operators (including
        ``+=`` etc.) or a special operator ``:=`` which means you are defining
        a new symbol (whereas ``=`` means you are setting the value of an
        existing symbol).
    expr : str, `Expression`
        The right hand side of the statement.
    dtype : `dtype`
        The numpy dtype of the value or array `var`.
    constant : bool, optional
        Set this flag to ``True`` if the value will not change (only applies for
        ``op==':='``.
    subexpression : bool, optional
        Set this flag to ``True`` if the variable is a subexpression. In some
        languages (e.g. Python) you can use this to save a memory copy, because
        you don't need to do ``lhs[:] = rhs`` but a redefinition ``lhs = rhs``.
    scalar : bool, optional
        Set this flag to ``True`` if `var` and `expr` are scalar.

    Notes
    -----
    Will compute the following attribute:

    ``inplace``
        True or False depending if the operation is in-place or not.

    Boolean simplification notes:

    Will initially set the attribute ``used_boolean_variables`` to ``None``.
    This is set by `~brian2.codegen.optimisation.optimise_statements` when it
    is called on a sequence of statements to the list of boolean variables
    that are used in this expression. In addition, the attribute
    ``boolean_simplified_expressions`` is set to a dictionary with keys
    consisting of a tuple of pairs ``(var, value)`` where ``var`` is the
    name of the boolean variable (will be in ``used_boolean_variables``)
    and ``var`` is ``True`` or ``False``. The values of the dictionary are
    strings representing the simplified version of the expression if each
    ``var=value`` substitution is made for that key. The keys will range
    over all possible values of the set of boolean variables. The complexity
    of the original statement is set as the attribute ``complexity_std``,
    and the complexity of the simplified versions are in the dictionary
    ``complexities`` (with the same keys).

    This information can be used to generate code that replaces a complex
    expression that varies depending on the value of one or more boolean
    variables with an ``if/then`` sequence where each subexpression is
    simplified. It is optional to use this (e.g. the numpy codegen does
    not, but the cython one does).
    """

    def __init__(
        self,
        var,
        op,
        expr,
        comment,
        dtype,
        constant=False,
        subexpression=False,
        scalar=False,
    ):
        self.var = var.strip()
        self.op = op.strip()
        self.expr = expr
        self.comment = comment
        self.dtype = dtype
        self.constant = constant
        self.subexpression = subexpression
        self.scalar = scalar
        if constant and self.op != ":=":
            raise ValueError(f"Should not set constant flag for operation {self.op}")
        if op.endswith("=") and op != "=" and op != ":=":
            self.inplace = True
        else:
            self.inplace = False
        self.used_boolean_variables = None
        self.boolean_simplified_expressions = None

    def __str__(self):
        s = f"{self.var} {self.op} {str(self.expr)}"
        if self.constant:
            s += " (constant)"
        if self.subexpression:
            s += " (subexpression)"
        if self.inplace:
            s += " (in-place)"
        if len(self.comment):
            s += f" # {self.comment}"
        return s

    __repr__ = __str__