Commit 73abd9a7 authored by Markus Holzer's avatar Markus Holzer
Browse files

Merge branch 'fix_slicing' into 'master'

Fix: shift_slice to accept single slices as argument, and return tuples

See merge request pycodegen/pystencils!177
parents f2e811e8 a817ea2c
......@@ -89,9 +89,12 @@ def shift_slice(slices, offset):
raise ValueError()
if hasattr(offset, '__len__'):
return [shift_slice_component(k, off) for k, off in zip(slices, offset)]
return tuple(shift_slice_component(k, off) for k, off in zip(slices, offset))
else:
return [shift_slice_component(k, offset) for k in slices]
if isinstance(slices, slice) or isinstance(slices, int) or isinstance(slices, float):
return shift_slice_component(slices, offset)
else:
return tuple(shift_slice_component(k, offset) for k in slices)
def slice_from_direction(direction_name, dim, normal_offset=0, tangential_offset=0):
......
import numpy as np
from numpy.testing import assert_array_equal
from pystencils import create_data_handling
from pystencils.slicing import SlicedGetter, make_slice, SlicedGetterDataHandling, shift_slice, slice_intersection
......@@ -59,6 +60,28 @@ def test_shift_slice():
assert sh[1] == 1.5
def test_shifted_array_access():
arr = np.array(range(10))
sh = make_slice[2:5]
assert_array_equal(arr[sh], [2,3,4])
sh = shift_slice(sh, 3)
assert_array_equal(arr[sh], [5,6,7])
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
sh = make_slice[0:2, 0:2]
assert_array_equal(arr[sh], [[1, 2], [4, 5]])
sh = shift_slice(sh, (1,1))
assert_array_equal(arr[sh], [[5, 6], [8, 9]])
def test_slice_intersection():
sl1 = make_slice[1:10, 1:10]
sl2 = make_slice[5:15, 5:15]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment