Skip to content

Commit 8e764b6

Browse files
committed
Merge pull request #25 from FilipeMaia/indexing
Improve handling of negative steps
2 parents 131d0bc + d355098 commit 8e764b6

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

arrayfire/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def __setitem__(self, key, val):
441441

442442
if (is_number(val)):
443443
tdims = get_assign_dims(key, self.dims())
444-
other_arr = constant_array(val, tdims[0], tdims[1], tdims[2], tdims[3])
444+
other_arr = constant_array(val, tdims[0], tdims[1], tdims[2], tdims[3], self.type())
445445
else:
446446
other_arr = val.arr
447447

arrayfire/index.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
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),
1617
("end" , ct.c_double),
1718
("step" , ct.c_double)]
1819

1920
def __init__ (self, S):
20-
num = __import__("numbers")
21-
2221
self.begin = ct.c_double( 0)
2322
self.end = ct.c_double(-1)
2423
self.step = ct.c_double( 1)
@@ -27,12 +26,14 @@ def __init__ (self, S):
2726
self.begin = ct.c_double(S)
2827
self.end = ct.c_double(S)
2928
elif isinstance(S, slice):
29+
if (S.step is not None):
30+
self.step = ct.c_double(S.step)
31+
if(S.step < 0):
32+
self.begin, self.end = self.end, self.begin
3033
if (S.start is not None):
3134
self.begin = ct.c_double(S.start)
3235
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)
36+
self.end = ct.c_double(S.stop - math.copysign(1, self.step))
3637
else:
3738
raise IndexError("Invalid type while indexing arrayfire.array")
3839

0 commit comments

Comments
 (0)