Tag: visual language

  • Arithmetic Operations in Bonsai

    Arithmetic Operations in Bonsai

    Bonsai is widely used for designing experiments in neuroscience and other fields.
    With Bonsai you can build sophisticated programs without writing code, and run them with precise synchronization.

    Because it is so simple, however, operations that would be trivial in ordinary programming—such as basic arithmetic—have to be done in a somewhat unintuitive way.
    Here I explain how to perform arithmetic in Bonsai, using MouseMove as an example.

    Below is what these arithmetic operations actually look like: from top to bottom, addition, subtraction, multiplication, and division.

    For clarity, I used the X and Y values of MouseMove as the example here.
    In practice, though, you can perform calculations on values obtained from other sensors as well.

    The key point in Bonsai is that, before performing an operation, you need to combine the two values using “Zip”.

    The contents of “Zip” take the form (value 1, value 2); its role is to bundle the two values at that moment into one.
    Each operation then computes on value 1 and value 2 accordingly.

    This is hard to explain in words, so try to get a rough intuition for it.
    Without “Zip”, the program would not know which value to multiply with which, so the calculation could not be performed. That is why the X and Y values must first be combined so that they correspond to each other.

    The names of the operators for each arithmetic operation are listed below.

    • Addition => Add
    • Subtraction => Subtract
    • Multiplication => Multiply
    • Division => Divide

    I have posted a demonstration below, so please take a look.

    <Example in action>