| 1 |
{# USES_VARIABLES { N } #} |
| 2 |
{# ALLOWS_SCALAR_WRITE #} |
| 3 |
{% extends 'common_group.py_' %} |
| 4 |
|
| 5 |
{% block maincode %} |
| 6 |
numpy_False = _numpy.bool_(False) |
| 7 |
numpy_True = _numpy.bool_(True) |
| 8 |
|
| 9 |
# Phase 1: we compute the indices where the conditional setting is to |
| 10 |
# be applied, and to do this we want to vectorise over all the values, |
| 11 |
# but we don't want to do the iterate all protocol, so we explicitly |
| 12 |
# set the idx to be slice(None) |
| 13 |
# scalar code |
| 14 |
_vectorisation_idx = 1 |
| 15 |
{{scalar_code['condition']|autoindent}} |
| 16 |
|
| 17 |
# vector code |
| 18 |
_idx = slice(None) |
| 19 |
_vectorisation_idx = LazyArange({{constant_or_scalar('N', variables['N'])}}) |
| 20 |
{{vector_code['condition']|autoindent}} |
| 21 |
|
| 22 |
# Phase 2: having computed _cond, the boolean array of points where |
| 23 |
# the setting is to be applied, we want to vectorise over idx being |
| 24 |
# only these values. |
| 25 |
|
| 26 |
{# Note that we don't write to scalar variables conditionally. The scalar code |
| 27 |
should therefore only include the calculation of scalar expressions |
| 28 |
that are used below for writing to a vector variable. The only exception is |
| 29 |
the "conditional writing" with the condition True. #} |
| 30 |
# scalar code |
| 31 |
_vectorisation_idx = 1 |
| 32 |
{{scalar_code['statement']|autoindent}} |
| 33 |
|
| 34 |
# vector code |
| 35 |
if _cond is True or _cond is numpy_True: |
| 36 |
_idx = slice(None) |
| 37 |
_vectorisation_idx = LazyArange(N) |
| 38 |
elif _cond is False or _cond is numpy_False: |
| 39 |
_idx = [] |
| 40 |
_vectorisation_idx = _numpy.array([], dtype=_numpy.int32) |
| 41 |
else: |
| 42 |
_vectorisation_idx = _idx = _numpy.nonzero(_cond)[0] |
| 43 |
|
| 44 |
{{vector_code['statement']|autoindent}} |
| 45 |
{% endblock %} |