[python-arrayfire] 49/250: Reorganizing the array.py file
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:29 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.
commit 050624a108b7cb203bc57b026abe443f680d5bc8
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date: Tue Jul 28 18:41:44 2015 -0400
Reorganizing the array.py file
- seq and index moved to index.py
---
arrayfire/__init__.py | 2 +
arrayfire/array.py | 121 ++---------------------------------------------
arrayfire/base.py | 14 ++++++
arrayfire/index.py | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 148 insertions(+), 117 deletions(-)
diff --git a/arrayfire/__init__.py b/arrayfire/__init__.py
index e1bf098..905fda6 100644
--- a/arrayfire/__init__.py
+++ b/arrayfire/__init__.py
@@ -23,6 +23,7 @@ from .features import *
from .vision import *
from .graphics import *
from .broadcast import *
+from .index import *
# do not export default modules as part of arrayfire
del ct
@@ -31,6 +32,7 @@ del numbers
del os
#do not export internal classes
+del base_array
del uidx
del seq
del index
diff --git a/arrayfire/array.py b/arrayfire/array.py
index 2253f1e..cd1135f 100644
--- a/arrayfire/array.py
+++ b/arrayfire/array.py
@@ -11,6 +11,8 @@ import inspect
from .library import *
from .util import *
from .broadcast import *
+from .base import *
+from .index import *
def create_array(buf, numdims, idims, dtype):
out_arr = ct.c_longlong(0)
@@ -92,86 +94,6 @@ def transpose(a, conj=False):
def transpose_inplace(a, conj=False):
safe_call(clib.af_transpose_inplace(a.arr, conj))
-class seq(ct.Structure):
- _fields_ = [("begin", ct.c_double),
- ("end" , ct.c_double),
- ("step" , ct.c_double)]
-
- def __init__ (self, S):
- num = __import__("numbers")
-
- self.begin = ct.c_double( 0)
- self.end = ct.c_double(-1)
- self.step = ct.c_double( 1)
-
- if is_number(S):
- self.begin = ct.c_double(S)
- self.end = ct.c_double(S)
- elif isinstance(S, slice):
- if (S.start is not None):
- self.begin = ct.c_double(S.start)
- if (S.stop is not None):
- self.end = ct.c_double(S.stop - 1)
- if (S.step is not None):
- self.step = ct.c_double(S.step)
- else:
- raise IndexError("Invalid type while indexing arrayfire.array")
-
-class uidx(ct.Union):
- _fields_ = [("arr", ct.c_longlong),
- ("seq", seq)]
-
-class index(ct.Structure):
- _fields_ = [("idx", uidx),
- ("isSeq", ct.c_bool),
- ("isBatch", ct.c_bool)]
-
- def __init__ (self, idx):
-
- self.idx = uidx()
- self.isBatch = False
- self.isSeq = True
-
- if isinstance(idx, array):
- self.idx.arr = idx.arr
- self.isSeq = False
- else:
- self.idx.seq = seq(idx)
-
-def get_indices(key, n_dims):
- index_vec = index * n_dims
- inds = index_vec()
-
- for n in range(n_dims):
- inds[n] = index(slice(None))
-
- if isinstance(key, tuple):
- n_idx = len(key)
- for n in range(n_idx):
- inds[n] = index(key[n])
- else:
- inds[0] = index(key)
-
- return inds
-
-def slice_to_length(key, dim):
- tkey = [key.start, key.stop, key.step]
-
- if tkey[0] is None:
- tkey[0] = 0
- elif tkey[0] < 0:
- tkey[0] = dim - tkey[0]
-
- if tkey[1] is None:
- tkey[1] = dim
- elif tkey[1] < 0:
- tkey[1] = dim - tkey[1]
-
- if tkey[2] is None:
- tkey[2] = 1
-
- return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
-
def ctype_to_lists(ctype_arr, dim, shape, offset=0):
if (dim == 0):
return list(ctype_arr[offset : offset + shape[0]])
@@ -183,46 +105,11 @@ def ctype_to_lists(ctype_arr, dim, shape, offset=0):
offset += shape[0]
return res
-def get_assign_dims(key, idims):
- dims = [1]*4
-
- for n in range(len(idims)):
- dims[n] = idims[n]
-
- if is_number(key):
- dims[0] = 1
- return dims
- elif isinstance(key, slice):
- dims[0] = slice_to_length(key, idims[0])
- return dims
- elif isinstance(key, array):
- dims[0] = key.elements()
- return dims
- elif isinstance(key, tuple):
- n_inds = len(key)
-
- if (n_inds > len(idims)):
- raise IndexError("Number of indices greater than array dimensions")
-
- for n in range(n_inds):
- if (is_number(key[n])):
- dims[n] = 1
- elif (isinstance(key[n], array)):
- dims[n] = key[n].elements()
- elif (isinstance(key[n], slice)):
- dims[n] = slice_to_length(key[n], idims[n])
- else:
- raise IndexError("Invalid type while assigning to arrayfire.array")
-
- return dims
- else:
- raise IndexError("Invalid type while assigning to arrayfire.array")
-
-class array(object):
+class array(base_array):
def __init__(self, src=None, dims=(0,)):
- self.arr = ct.c_longlong(0)
+ super(array, self).__init__()
buf=None
buf_len=0
diff --git a/arrayfire/base.py b/arrayfire/base.py
new file mode 100644
index 0000000..ce8e23a
--- /dev/null
+++ b/arrayfire/base.py
@@ -0,0 +1,14 @@
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+from .library import *
+from .util import *
+
+class base_array(object):
+ def __init__(self):
+ self.arr = ct.c_longlong(0)
diff --git a/arrayfire/index.py b/arrayfire/index.py
new file mode 100644
index 0000000..7b264a9
--- /dev/null
+++ b/arrayfire/index.py
@@ -0,0 +1,128 @@
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+from .library import *
+from .util import *
+from .base import *
+
+class seq(ct.Structure):
+ _fields_ = [("begin", ct.c_double),
+ ("end" , ct.c_double),
+ ("step" , ct.c_double)]
+
+ def __init__ (self, S):
+ num = __import__("numbers")
+
+ self.begin = ct.c_double( 0)
+ self.end = ct.c_double(-1)
+ self.step = ct.c_double( 1)
+
+ if is_number(S):
+ self.begin = ct.c_double(S)
+ self.end = ct.c_double(S)
+ elif isinstance(S, slice):
+ if (S.start is not None):
+ self.begin = ct.c_double(S.start)
+ if (S.stop is not None):
+ self.end = ct.c_double(S.stop - 1)
+ if (S.step is not None):
+ self.step = ct.c_double(S.step)
+ else:
+ raise IndexError("Invalid type while indexing arrayfire.array")
+
+def slice_to_length(key, dim):
+ tkey = [key.start, key.stop, key.step]
+
+ if tkey[0] is None:
+ tkey[0] = 0
+ elif tkey[0] < 0:
+ tkey[0] = dim - tkey[0]
+
+ if tkey[1] is None:
+ tkey[1] = dim
+ elif tkey[1] < 0:
+ tkey[1] = dim - tkey[1]
+
+ if tkey[2] is None:
+ tkey[2] = 1
+
+ return int(((tkey[1] - tkey[0] - 1) / tkey[2]) + 1)
+
+class uidx(ct.Union):
+ _fields_ = [("arr", ct.c_longlong),
+ ("seq", seq)]
+
+class index(ct.Structure):
+ _fields_ = [("idx", uidx),
+ ("isSeq", ct.c_bool),
+ ("isBatch", ct.c_bool)]
+
+ def __init__ (self, idx):
+
+ self.idx = uidx()
+ self.isBatch = False
+ self.isSeq = True
+
+ if isinstance(idx, base_array):
+ self.idx.arr = idx.arr
+ self.isSeq = False
+ else:
+ self.idx.seq = seq(idx)
+
+def get_indices(key, n_dims):
+
+ index_vec = index * n_dims
+ inds = index_vec()
+
+ for n in range(n_dims):
+ inds[n] = index(slice(None))
+
+ if isinstance(key, tuple):
+ n_idx = len(key)
+ for n in range(n_idx):
+ inds[n] = index(key[n])
+ else:
+ inds[0] = index(key)
+
+ return inds
+
+def get_assign_dims(key, idims):
+
+ dims = [1]*4
+
+ for n in range(len(idims)):
+ dims[n] = idims[n]
+
+ if is_number(key):
+ dims[0] = 1
+ return dims
+ elif isinstance(key, slice):
+ dims[0] = slice_to_length(key, idims[0])
+ return dims
+ elif isinstance(key, base_array):
+ dims[0] = key.elements()
+ return dims
+ elif isinstance(key, tuple):
+ n_inds = len(key)
+
+ if (n_inds > len(idims)):
+ raise IndexError("Number of indices greater than array dimensions")
+
+ for n in range(n_inds):
+ if (is_number(key[n])):
+ dims[n] = 1
+ elif (isinstance(key[n], base_array)):
+ dims[n] = key[n].elements()
+ elif (isinstance(key[n], slice)):
+ dims[n] = slice_to_length(key[n], idims[n])
+ else:
+ raise IndexError("Invalid type while assigning to arrayfire.array")
+
+ return dims
+ else:
+ raise IndexError("Invalid type while assigning to arrayfire.array")
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git
More information about the debian-science-commits
mailing list