collect function

(Shortest import: from brian2.codegen.optimisation import collect)


Attempts to collect commutative operations into one and simplifies them.

For example, if x and y are scalars, and z is a vector, then (x*z)*y should be rewritten as (x*y)*z to minimise the number of vector operations. Similarly, ((x*2)*3)*4 should be rewritten as x*24.

Works for either multiplication/division or addition/subtraction nodes.

The final output is a subexpression of the following maximal form:

(((numerical_value*(product of scalars))/(product of scalars))*(product of vectors))/(product of vectors)

Any possible cancellations will have been done.