numpycpp
 All Classes Namespaces Files Functions Typedefs Pages
demo.py
Go to the documentation of this file.
1 # Copyright (c) 2016 Michael Welter
2 #
3 # This file is part of numpycpp.
4 #
5 # numpycpp is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # numpycpp is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 #You should have received a copy of the GNU General Public License
16 #along with numpycpp. If not, see <http://www.gnu.org/licenses/>.
17 
18 import numpy as np
19 import sys
20 import os
21 
22 sys.path.append(os.getcwd())
23 import libdemo
24 
25 print ('VARIOUS TYPES MAPPING:')
26 datatypes = [np.object, np.int8, np.bool, np.byte, np.short, np.int16, np.int, np.long, np.int32, np.int64, np.longlong, np.float, np.float32, np.double, np.float64]
27 for dt in datatypes:
28  a = np.zeros(5, dtype = dt)
29  print ('-----' + str(dt) + '-------')
30  libdemo.printArray(a, False)
31 
32 print ('')
33 print ('UNSIGNED TYPES:')
34 unsignedtypes = [np.uint8, np.ubyte, np.ushort, np.uint16, np.uint, np.uint32, np.uint64, np.ulonglong]
35 for dt in unsignedtypes:
36  a = np.zeros(5, dtype = dt)
37  print ('-----' + str(dt) + '-------')
38  libdemo.printArray(a, False)
39 
40 print ('PRINTING CONTENTS:')
41 a = np.arange(2*3*4, dtype = np.int32).reshape(2,3,4)
42 print ('PRINT IN PYTHON:')
43 print (a)
44 print ('PRINT c++ DEMO:')
45 libdemo.printArray(a, True)
46 
47 a = np.asarray(['hello', 'python', 'wrapper!'], dtype = np.object)
48 print ('PRINT IN PYTHON:')
49 print (a)
50 print ('PRINT c++ DEMO:')
51 libdemo.printArray(a, True)
52 
53 
54 print ('PRINTING CONVERTED ARRAY:')
55 a = np.arange(2*3*4, dtype = np.int32).reshape(2,3,4)
56 libdemo.printConvertedArray_int(a)
57 
58 
59 def wrap_to_str(f):
60  def wrapper(x):
61  try:
62  return f(x)
63  except:
64  return 'ERR'
65  return wrapper
66 
67 for fun in [libdemo.int_to_str, libdemo.char_to_str, libdemo.float_to_str, libdemo.double_to_str]:
68  locals()[fun.__name__] = wrap_to_str(fun)
69 
70 print ('TESTING SCALAR CONVERSION:')
71 types = [np.int8, np.byte, np.short, np.int16, np.int, np.long, np.int32, np.int64, np.longlong, np.uint8, np.ubyte, np.ushort, np.uint16, np.uint, np.uint32, np.uint64, np.ulonglong, np.float, np.float32, np.double, np.float64]
72 for dt in types:
73  a = np.asarray((42,), dtype = dt)[0]
74  print ('%s: as int: %s, as char: %s, as float: %s, as double: %s' % (str(dt), int_to_str(a), char_to_str(a), float_to_str(a), double_to_str(a)))
75 
76 print ('RETURNING ARRAY FROM CPP:')
77 a = libdemo.ReturnedFromCPP()
78 print a
79 
80 
81 if 0:
82  # direct memory access with arrayt<float> is approximately 38x faster than using numeric::array
83  print ('PERFORMANCE TEST:')
84  import time
85  import matplotlib.pyplot as pyplot
86 
87  def PerfTest(fun):
88  print ('TEST: '+str(fun.__name__))
89  sizes = [1000, 10000, 100000, 1000000]
90  times = []
91  for s in sizes:
92  a1 = np.arange(s, dtype = np.float32)
93  a2 = np.ones(s, dtype = np.float32)
94  tavg = []
95  for n in xrange(10):
96  t0 = time.clock()
97  result = fun(a1, a2)
98  t1 = time.clock()
99  tavg.append(t1 - t0)
100  times.append(np.average(tavg))
101  return sizes, np.asarray(times)
102 
103  sz, perfArrayT = PerfTest(libdemo.SumArrayT)
104  print perfArrayT
105  sz, perfBoostArray = PerfTest(libdemo.SumNumericArray)
106  print perfBoostArray
107 
108  pyplot.plot(sz, perfBoostArray / perfArrayT)
109  pyplot.gca().set(xlabel = 'Array Size', ylabel = 'Relative Compute Time')
110  pyplot.show()
object zeros(int rank, const Py_ssize_t *dims, int type)
Creates a new ndarray filled with zeros.
Definition: numpy.cpp:224