Skip to content

Commit f0d50c3

Browse files
committed
Improve handling of negative steps in sequences
1 parent 131d0bc commit f0d50c3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

arrayfire/index.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .util import *
1111
from .base import *
1212
from .broadcast import *
13+
import math
1314

1415
class Seq(ct.Structure):
1516
_fields_ = [("begin", ct.c_double),
@@ -18,7 +19,6 @@ class Seq(ct.Structure):
1819

1920
def __init__ (self, S):
2021
num = __import__("numbers")
21-
2222
self.begin = ct.c_double( 0)
2323
self.end = ct.c_double(-1)
2424
self.step = ct.c_double( 1)
@@ -27,12 +27,14 @@ def __init__ (self, S):
2727
self.begin = ct.c_double(S)
2828
self.end = ct.c_double(S)
2929
elif isinstance(S, slice):
30+
if (S.step is not None):
31+
self.step = ct.c_double(S.step)
32+
if(S.step < 0):
33+
self.begin, self.end = self.end, self.begin
3034
if (S.start is not None):
3135
self.begin = ct.c_double(S.start)
3236
if (S.stop is not None):
33-
self.end = ct.c_double(S.stop - 1)
34-
if (S.step is not None):
35-
self.step = ct.c_double(S.step)
37+
self.end = ct.c_double(S.stop - math.copysign(1, self.step))
3638
else:
3739
raise IndexError("Invalid type while indexing arrayfire.array")
3840

0 commit comments

Comments
 (0)