Exemple #1
0
 def translate_expression(self, expr):
     for varname, var in self.variables.iteritems():
         if isinstance(var, Function):
             impl_name = var.implementations[self.codeobj_class].name
             if impl_name is not None:
                 expr = word_substitute(expr, {varname: impl_name})
     return NumpyNodeRenderer().render_expr(expr, self.variables).strip()
Exemple #2
0
    def ufunc_at_vectorisation(self, statement, variables, indices,
                               conditional_write_vars, created_vars,
                               used_variables):
        if not self._use_ufunc_at_vectorisation:
            raise VectorisationError()
        # Avoids circular import
        from brian2.devices.device import device

        # See https://github.com/brian-team/brian2/pull/531 for explanation
        used = set(get_identifiers(statement.expr))
        used = used.intersection(k for k in list(variables.keys())
                                 if k in indices and indices[k] != '_idx')
        used_variables.update(used)
        if statement.var in used_variables:
            raise VectorisationError()
        expr = NumpyNodeRenderer(
            auto_vectorise=self.auto_vectorise).render_expr(statement.expr)

        if statement.op == ':=' or indices[
                statement.var] == '_idx' or not statement.inplace:
            if statement.op == ':=':
                op = '='
            else:
                op = statement.op
            line = '{var} {op} {expr}'.format(var=statement.var,
                                              op=op,
                                              expr=expr)
        elif statement.inplace:
            if statement.op == '+=':
                ufunc_name = '_numpy.add'
            elif statement.op == '*=':
                ufunc_name = '_numpy.multiply'
            elif statement.op == '/=':
                ufunc_name = '_numpy.divide'
            elif statement.op == '-=':
                ufunc_name = '_numpy.subtract'
            else:
                raise VectorisationError()

            line = '{ufunc_name}.at({array_name}, {idx}, {expr})'.format(
                ufunc_name=ufunc_name,
                array_name=device.get_array_name(variables[statement.var]),
                idx=indices[statement.var],
                expr=expr)
            line = self.conditional_write(
                line,
                statement,
                variables,
                conditional_write_vars=conditional_write_vars,
                created_vars=created_vars)
        else:
            raise VectorisationError()

        if len(statement.comment):
            line += ' # ' + statement.comment

        return line
Exemple #3
0
    def ufunc_at_vectorisation(self, statement, variables, indices,
                               conditional_write_vars, created_vars, index):
        '''
        '''
        # Avoids circular import
        from brian2.devices.device import device

        # We assume that the code has passed the test for synapse order independence

        expr = NumpyNodeRenderer().render_expr(statement.expr)

        if statement.op == ':=' or indices[
                statement.var] == '_idx' or not statement.inplace:
            if statement.op == ':=':
                op = '='
            else:
                op = statement.op
            line = '{var} {op} {expr}'.format(var=statement.var,
                                              op=op,
                                              expr=expr)
        elif statement.inplace:
            if statement.op == '+=':
                ufunc_name = '_numpy.add'
            elif statement.op == '*=':
                ufunc_name = '_numpy.multiply'
            elif statement.op == '/=':
                ufunc_name = '_numpy.divide'
            elif statement.op == '-=':
                ufunc_name = '_numpy.subtract'
            else:
                raise VectorisationError()

            line = '{ufunc_name}.at({array_name}, {idx}, {expr})'.format(
                ufunc_name=ufunc_name,
                array_name=device.get_array_name(variables[statement.var]),
                idx=indices[statement.var],
                expr=expr)
            line = self.conditional_write(
                line,
                statement,
                variables,
                conditional_write_vars=conditional_write_vars,
                created_vars=created_vars)
        else:
            raise VectorisationError()

        if len(statement.comment):
            line += ' # ' + statement.comment

        return line
Exemple #4
0
 def translate_expression(self, expr):
     expr = word_substitute(expr, self.func_name_replacements)
     return NumpyNodeRenderer(
         auto_vectorise=self.auto_vectorise).render_expr(
             expr, self.variables).strip()
Exemple #5
0
def test_parse_expressions_numpy():
    parse_expressions(NumpyNodeRenderer(), numpy_evaluator)
Exemple #6
0
 def translate_expression(self, expr):
     return NumpyNodeRenderer().render_expr(expr).strip()