[python-arrayfire] 04/250: Ported all functions from algorithm.h
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:24 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 95e628409769c73f087da910f7e874495ec0461f
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date: Fri Jun 19 12:22:59 2015 -0400
Ported all functions from algorithm.h
- Changed behavior of reductions to be more pythonish
---
arrayfire/algorithm.py | 140 +++++++++++++++++++++++++++++++++++++++----------
examples/helloworld.py | 14 +++--
2 files changed, 119 insertions(+), 35 deletions(-)
diff --git a/arrayfire/algorithm.py b/arrayfire/algorithm.py
index b2ea16f..20548fe 100644
--- a/arrayfire/algorithm.py
+++ b/arrayfire/algorithm.py
@@ -1,52 +1,138 @@
from .library import *
from .array import *
-def sum(A, dim=0):
- out = array()
- clib.af_sum(pointer(out.arr), A.arr, c_int(dim))
- return out
-def min(A, dim=0):
+def parallel_dim(a, dim, c_func):
out = array()
- clib.af_min(pointer(out.arr), A.arr, c_int(dim))
+ c_func(pointer(out.arr), a.arr, c_int(dim))
return out
-def max(A, dim=0):
- out = array()
- clib.af_max(pointer(out.arr), A.arr, c_int(dim))
- return out
+def reduce_all(a, c_func):
+ real = c_double(0)
+ imag = c_double(0)
+ c_func(pointer(real), pointer(imag), a.arr)
+ real = real.value
+ imag = imag.value
+ return real if imag == 0 else real + imag * 1j
-def any_true(A, dim=0):
- out = array()
- clib.af_any_true(pointer(out.arr), A.arr, c_int(dim))
- return out
+def sum(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_sum)
+ else:
+ return reduce_all(a, clib.af_sum_all)
+
+def product(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_product)
+ else:
+ return reduce_all(a, clib.af_product_all)
+
+def min(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_min)
+ else:
+ return reduce_all(a, clib.af_min_all)
+
+def max(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_max)
+ else:
+ return reduce_all(a, clib.af_max_all)
+
+def all_true(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_all_true)
+ else:
+ return reduce_all(a, clib.af_all_true_all)
-def all_true(A, dim=0):
+def any_true(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_any_true)
+ else:
+ return reduce_all(a, clib.af_any_true_all)
+
+def count(a, dim=None):
+ if dim is not None:
+ return parallel_dim(a, dim, clib.af_count)
+ else:
+ return reduce_all(a, clib.af_count_all)
+
+def imin(a, dim=None):
+ if dim is not None:
+ out = array()
+ idx = array()
+ clib.af_imin(pointer(out.arr), pointer(idx.arr), a.arr, c_int(dim))
+ return out,idx
+ else:
+ real = c_double(0)
+ imag = c_double(0)
+ idx = c_uint(0)
+ clib.af_imin_all(pointer(real), pointer(imag), pointer(idx), a.arr)
+ real = real.value
+ imag = imag.value
+ val = real if imag == 0 else real + imag * 1j
+ return val,idx.value
+
+def imax(a, dim=None):
+ if dim is not None:
+ out = array()
+ idx = array()
+ clib.af_imax(pointer(out.arr), pointer(idx.arr), a.arr, c_int(dim))
+ return out,idx
+ else:
+ real = c_double(0)
+ imag = c_double(0)
+ idx = c_uint(0)
+ clib.af_imax_all(pointer(real), pointer(imag), pointer(idx), a.arr)
+ real = real.value
+ imag = imag.value
+ val = real if imag == 0 else real + imag * 1j
+ return val,idx.value
+
+
+def accum(a, dim=0):
+ return parallel_dim(a, dim, clib.af_accum)
+
+def where(a):
out = array()
- clib.af_all_true(pointer(out.arr), A.arr, c_int(dim))
+ clib.af_where(pointer(out.arr), a.arr)
return out
-def accum(A, dim=0):
+def diff1(a, dim=0):
+ return parallel_dim(a, dim, clib.af_diff1)
+
+def diff2(a, dim=0):
+ return parallel_dim(a, dim, clib.af_diff2)
+
+def sort(a, dim=0, is_ascending=True):
out = array()
- clib.af_accum(pointer(out.arr), A.arr, c_int(dim))
+ clib.af_sort(pointer(out.arr), a.arr, c_uint(dim), c_bool(is_ascending))
return out
-def sort(A, dim=0):
+def sort_index(a, dim=0, is_is_ascending=True):
out = array()
- clib.af_sort(pointer(out.arr), A.arr, c_int(dim))
- return out
+ idx = array()
+ clib.af_sort_index(pointer(out.arr), pointer(idx.arr), a.arr, c_uint(dim), c_bool(is_ascending))
+ return out,idx
+
+def sort_by_key(iv, ik, dim=0, is_is_ascending=True):
+ ov = array()
+ ok = array()
+ clib.af_sort_by_key(pointer(ov.arr), pointer(ok.arr), \
+ iv.arr, ik.arr, c_uint(dim), c_bool(is_ascending))
+ return ov,ok
-def diff1(A, dim=0):
+def set_unique(a, is_sorted=False):
out = array()
- clib.af_diff1(pointer(out.arr), A.arr, c_int(dim))
+ clib.af_set_unique(pointer(out.arr), a.arr, c_bool(is_sorted))
return out
-def diff2(A, dim=0):
+def set_union(a, b, is_unique=False):
out = array()
- clib.af_diff2(pointer(out.arr), A.arr, c_int(dim))
+ clib.af_set_union(pointer(out.arr), a.arr, b.arr, c_bool(is_sorted))
return out
-def where(A):
+def set_intersect(a, b, is_unique=False):
out = array()
- clib.af_where(pointer(out.arr), A.arr)
+ clib.af_set_intersect(pointer(out.arr), a.arr, b.arr, c_bool(is_sorted))
return out
diff --git a/examples/helloworld.py b/examples/helloworld.py
index 50f129f..7da886b 100644
--- a/examples/helloworld.py
+++ b/examples/helloworld.py
@@ -1,16 +1,14 @@
#!/usr/bin/python
import arrayfire as af
+# Display backend information
af.info()
-print('\nGenerate a random matrix a:')
+# Generate a uniform random array with a size of 5 elements
a = af.randu(5, 1)
-af.print_array(a)
-print('\nMin value of a')
-a_min = af.min(a)
-af.print_array(a_min)
+# Print a and its minimum value
+af.print_array(a)
-print('\nMax value of a')
-a_max = af.max(a)
-af.print_array(a_max)
+# Print min and max values of a
+print("Minimum, Maximum: ", af.min(a), af.max(a))
--
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