Skip to content

Commit 050624a

Browse files
committed
Reorganizing the array.py file
- seq and index moved to index.py
1 parent 4441eb5 commit 050624a

File tree

4 files changed

+148
-117
lines changed

4 files changed

+148
-117
lines changed

arrayfire/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .vision import *
2424
from .graphics import *
2525
from .broadcast import *
26+
from .index import *
2627

2728
# do not export default modules as part of arrayfire
2829
del ct
@@ -31,6 +32,7 @@
3132
del os
3233

3334
#do not export internal classes
35+
del base_array
3436
del uidx
3537
del seq
3638
del index

arrayfire/array.py

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from .library import *
1212
from .util import *
1313
from .broadcast import *
14+
from .base import *
15+
from .index import *
1416

1517
def create_array(buf, numdims, idims, dtype):
1618
out_arr = ct.c_longlong(0)
@@ -92,86 +94,6 @@ def transpose(a, conj=False):
9294
def transpose_inplace(a, conj=False):
9395
safe_call(clib.af_transpose_inplace(a.arr, conj))
9496

95-
class seq(ct.Structure):
96-
_fields_ = [("begin", ct.c_double),
97-
("end" , ct.c_double),
98-
("step" , ct.c_double)]
99-
100-
def __init__ (self, S):
101-
num = __import__("numbers")
102-
103-
self.begin = ct.c_double( 0)
104-
self.end = ct.c_double(-1)
105-
self.step = ct.c_double( 1)
106-
107-
if is_number(S):
108-
self.begin = ct.c_double(S)
109-
self.end = ct.c_double(S)
110-
elif isinstance(S, slice):
111-
if (S.start is not None):
112-
self.begin = ct.c_double(S.start)
113-
if (S.stop is not None):
114-
self.end = ct.c_double(S.stop - 1)
115-
if (S.step is not None):
116-
self.step = ct.c_double(S.step)
117-
else:
118-
raise IndexError("Invalid type while indexing arrayfire.array")
119-
120-
class uidx(ct.Union):
121-
_fields_ = [("arr", ct.c_longlong),
122-
("seq", seq)]
123-
124-
class index(ct.Structure):
125-
_fields_ = [("idx", uidx),
126-
("isSeq", ct.c_bool),
127-
("isBatch", ct.c_bool)]
128-
129-
def __init__ (self, idx):
130-
131-
self.idx = uidx()
132-
self.isBatch = False
133-
self.isSeq = True
134-
135-
if isinstance(idx, array):
136-
self.idx.arr = idx.arr
137-
self.isSeq = False
138-
else:
139-
self.idx.seq = seq(idx)
140-
141-
def get_indices(key, n_dims):
142-
index_vec = index * n_dims
143-
inds = index_vec()
144-
145-
for n in range(n_dims):
146-
inds[n] = index(slice(None))
147-
148-
if isinstance(key, tuple):
149-
n_idx = len(key)
150-
for n in range(n_idx):
151-
inds[n] = index(key[n])
152-
else:
153-
inds[0] = index(key)
154-
155-
return inds
156-
157-
def slice_to_length(key, dim):
158-
tkey = [key.start, key.stop, key.step]
159-
160-
if tkey[0] is None:
161-
tkey[0] = 0
162-
elif tkey[0] < 0:
163-
tkey[0] = dim - tkey[0]
164-
165-
if tkey[1] is None:
166-
tkey[1] = dim
167-
elif tkey[1] < 0:
168-
tkey[1] = dim - tkey[1]
169-
170-
if tkey[2] is None:
171-
tkey[2] = 1
172-
173-
return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
174-
17597
def ctype_to_lists(ctype_arr, dim, shape, offset=0):
17698
if (dim == 0):
17799
return list(ctype_arr[offset : offset + shape[0]])
@@ -183,46 +105,11 @@ def ctype_to_lists(ctype_arr, dim, shape, offset=0):
183105
offset += shape[0]
184106
return res
185107

186-
def get_assign_dims(key, idims):
187-
dims = [1]*4
188-
189-
for n in range(len(idims)):
190-
dims[n] = idims[n]
191-
192-
if is_number(key):
193-
dims[0] = 1
194-
return dims
195-
elif isinstance(key, slice):
196-
dims[0] = slice_to_length(key, idims[0])
197-
return dims
198-
elif isinstance(key, array):
199-
dims[0] = key.elements()
200-
return dims
201-
elif isinstance(key, tuple):
202-
n_inds = len(key)
203-
204-
if (n_inds > len(idims)):
205-
raise IndexError("Number of indices greater than array dimensions")
206-
207-
for n in range(n_inds):
208-
if (is_number(key[n])):
209-
dims[n] = 1
210-
elif (isinstance(key[n], array)):
211-
dims[n] = key[n].elements()
212-
elif (isinstance(key[n], slice)):
213-
dims[n] = slice_to_length(key[n], idims[n])
214-
else:
215-
raise IndexError("Invalid type while assigning to arrayfire.array")
216-
217-
return dims
218-
else:
219-
raise IndexError("Invalid type while assigning to arrayfire.array")
220-
221-
class array(object):
108+
class array(base_array):
222109

223110
def __init__(self, src=None, dims=(0,)):
224111

225-
self.arr = ct.c_longlong(0)
112+
super(array, self).__init__()
226113

227114
buf=None
228115
buf_len=0

arrayfire/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#######################################################
2+
# Copyright (c) 2015, ArrayFire
3+
# All rights reserved.
4+
#
5+
# This file is distributed under 3-clause BSD license.
6+
# The complete license agreement can be obtained at:
7+
# http://arrayfire.com/licenses/BSD-3-Clause
8+
########################################################
9+
from .library import *
10+
from .util import *
11+
12+
class base_array(object):
13+
def __init__(self):
14+
self.arr = ct.c_longlong(0)

arrayfire/index.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#######################################################
2+
# Copyright (c) 2015, ArrayFire
3+
# All rights reserved.
4+
#
5+
# This file is distributed under 3-clause BSD license.
6+
# The complete license agreement can be obtained at:
7+
# http://arrayfire.com/licenses/BSD-3-Clause
8+
########################################################
9+
from .library import *
10+
from .util import *
11+
from .base import *
12+
13+
class seq(ct.Structure):
14+
_fields_ = [("begin", ct.c_double),
15+
("end" , ct.c_double),
16+
("step" , ct.c_double)]
17+
18+
def __init__ (self, S):
19+
num = __import__("numbers")
20+
21+
self.begin = ct.c_double( 0)
22+
self.end = ct.c_double(-1)
23+
self.step = ct.c_double( 1)
24+
25+
if is_number(S):
26+
self.begin = ct.c_double(S)
27+
self.end = ct.c_double(S)
28+
elif isinstance(S, slice):
29+
if (S.start is not None):
30+
self.begin = ct.c_double(S.start)
31+
if (S.stop is not None):
32+
self.end = ct.c_double(S.stop - 1)
33+
if (S.step is not None):
34+
self.step = ct.c_double(S.step)
35+
else:
36+
raise IndexError("Invalid type while indexing arrayfire.array")
37+
38+
def slice_to_length(key, dim):
39+
tkey = [key.start, key.stop, key.step]
40+
41+
if tkey[0] is None:
42+
tkey[0] = 0
43+
elif tkey[0] < 0:
44+
tkey[0] = dim - tkey[0]
45+
46+
if tkey[1] is None:
47+
tkey[1] = dim
48+
elif tkey[1] < 0:
49+
tkey[1] = dim - tkey[1]
50+
51+
if tkey[2] is None:
52+
tkey[2] = 1
53+
54+
return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
55+
56+
class uidx(ct.Union):
57+
_fields_ = [("arr", ct.c_longlong),
58+
("seq", seq)]
59+
60+
class index(ct.Structure):
61+
_fields_ = [("idx", uidx),
62+
("isSeq", ct.c_bool),
63+
("isBatch", ct.c_bool)]
64+
65+
def __init__ (self, idx):
66+
67+
self.idx = uidx()
68+
self.isBatch = False
69+
self.isSeq = True
70+
71+
if isinstance(idx, base_array):
72+
self.idx.arr = idx.arr
73+
self.isSeq = False
74+
else:
75+
self.idx.seq = seq(idx)
76+
77+
def get_indices(key, n_dims):
78+
79+
index_vec = index * n_dims
80+
inds = index_vec()
81+
82+
for n in range(n_dims):
83+
inds[n] = index(slice(None))
84+
85+
if isinstance(key, tuple):
86+
n_idx = len(key)
87+
for n in range(n_idx):
88+
inds[n] = index(key[n])
89+
else:
90+
inds[0] = index(key)
91+
92+
return inds
93+
94+
def get_assign_dims(key, idims):
95+
96+
dims = [1]*4
97+
98+
for n in range(len(idims)):
99+
dims[n] = idims[n]
100+
101+
if is_number(key):
102+
dims[0] = 1
103+
return dims
104+
elif isinstance(key, slice):
105+
dims[0] = slice_to_length(key, idims[0])
106+
return dims
107+
elif isinstance(key, base_array):
108+
dims[0] = key.elements()
109+
return dims
110+
elif isinstance(key, tuple):
111+
n_inds = len(key)
112+
113+
if (n_inds > len(idims)):
114+
raise IndexError("Number of indices greater than array dimensions")
115+
116+
for n in range(n_inds):
117+
if (is_number(key[n])):
118+
dims[n] = 1
119+
elif (isinstance(key[n], base_array)):
120+
dims[n] = key[n].elements()
121+
elif (isinstance(key[n], slice)):
122+
dims[n] = slice_to_length(key[n], idims[n])
123+
else:
124+
raise IndexError("Invalid type while assigning to arrayfire.array")
125+
126+
return dims
127+
else:
128+
raise IndexError("Invalid type while assigning to arrayfire.array")

0 commit comments

Comments
 (0)