# Buggy code
def get_conditionally_linear_system(eqs, variables=None):
    '''
    Convert equations into a linear system using sympy.

    Parameters
    ----------
    eqs : `Equations`
        The model equations.

    Returns
    -------
    coefficients : dict of (sympy expression, sympy expression) tuples
        For every variable x, a tuple (M, B) containing the coefficients M and
        B (as sympy expressions) for M * x + B

    Raises
    ------
    ValueError
        If one of the equations cannot be converted into a M * x + B form.

    Examples
    --------
    >>> from brian2 import Equations
    >>> eqs = Equations("""
    ... dv/dt = (-v + w**2) / tau : 1
    ... dw/dt = -w / tau : 1
    ... """)
    >>> system = get_conditionally_linear_system(eqs)
    >>> print(system['v'])
    (-1/tau, w**2.0/tau)
    >>> print(system['w'])
    (-1/tau, 0)

    '''
    diff_eqs = eqs.get_substituted_expressions(variables)

    coefficients = {}

    for name, expr in diff_eqs:
        var = sp.Symbol(name, real=True)

        s_expr = str_to_sympy(expr.code, variables).expand()
        if s_expr.has(var):
            # Factor out the variable
            s_expr = sp.collect(s_expr, var, evaluate=False)

            if len(s_expr) > 2 or var not in s_expr:
                raise ValueError(
                    (
                        'The expression "%s", defining the variable %s, '
                        "could not be separated into linear components"
                    )
                    % (expr, name)
                )
            coefficients[name] = (s_expr[var], s_expr.get(1, 0))
        else:
            coefficients[name] = (0, s_expr)

    return coefficients


# Traceback (IndexError)
BrianObjectException: Original error and traceback:
Traceback (most recent call last):
File "miniconda2/envs/brian/lib/python2.7/site-packages/brian2/codegen/runtime/numpy_rt/numpy_rt.py", line 246, in run
exec(self.compiled_code, self.namespace)
File "(string)", line 19, in <module>
File "miniconda2/envs/brian/lib/python2.7/site-packages/brian2/input/timedarray.py", line 230, in unitless_timed_array_func
return values[timestep, i]
IndexError: arrays used as indices must be of integer (or boolean) type

Error encountered with object named "neurongroup".
Object was created here (most recent call only, full details in debug log):
File "<ipython-input-6-321ca0774a4a>", line 18, in <module>
method='euler')

An exception occured during the execution of code object neurongroup_stateupdater_codeobject.
The error was raised in the following line:
_v = (_lio_1 * I(t, 2.0 + i)) + v
IndexError: arrays used as indices must be of integer (or boolean) type
(See above for original error message and traceback.)

# Fixed code
def get_conditionally_linear_system(eqs, variables=None):
    '''
    Convert equations into a linear system using sympy.

    Parameters
    ----------
    eqs : `Equations`
        The model equations.

    Returns
    -------
    coefficients : dict of (sympy expression, sympy expression) tuples
        For every variable x, a tuple (M, B) containing the coefficients M and
        B (as sympy expressions) for M * x + B

    Raises
    ------
    ValueError
        If one of the equations cannot be converted into a M * x + B form.

    Examples
    --------
    >>> from brian2 import Equations
    >>> eqs = Equations("""
    ... dv/dt = (-v + w**2.0) / tau : 1
    ... dw/dt = -w / tau : 1
    ... """)
    >>> system = get_conditionally_linear_system(eqs)
    >>> print(system['v'])
    (-1/tau, w**2.0/tau)
    >>> print(system['w'])
    (-1/tau, 0)

    '''
    diff_eqs = eqs.get_substituted_expressions(variables)

    coefficients = {}

    for name, expr in diff_eqs:
        var = sp.Symbol(name, real=True)

        s_expr = str_to_sympy(expr.code, variables).expand()
        if s_expr.has(var):
            # Factor out the variable
            s_expr = sp.collect(s_expr, var, evaluate=False)

            if len(s_expr) > 2 or var not in s_expr:
                raise ValueError(
                    (
                        'The expression "%s", defining the variable %s, '
                        "could not be separated into linear components"
                    )
                    % (expr, name)
                )
            coefficients[name] = (s_expr[var], s_expr.get(1, 0))
        else:
            coefficients[name] = (0, s_expr)

    return coefficients