Menu

[r6218]: / trunk / py4science / examples / weave_exercises.py  Maximize  Restore  History

Download this file

39 lines (31 with data), 1.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
#!/usr/bin/env python
"""A simple weave exercise.
Problem: write a routine, called asum, which will correctly return the sum
of all the elements of a 1-d array. Test that it does the right thing by
comparing to nx.sum() for the array
"""
from weave import inline,converters
import Numeric as nx
#-----------------------------------------------------------------------------
# Problem 1
# Let's fill up some arrays with numbers
array_bytes = nx.arange(256).astype(nx.UInt8)
array_short = nx.arange(256**2).astype(nx.UInt16)
# nx.sum has a problem. What is it?
print 'WRONG'
print nx.sum(array_bytes)
print nx.sum(array_short)
# Now, a brute-force Python sum works OK
def pysum(arr):
"""Return the sum of all the elements in the array"""
ss = 0
for i in xrange(len(arr)):
ss += arr[i]
return ss
print 'RIGHT'
print pysum(array_bytes)
print pysum(array_short)
# but it's very slow. Now write your own correct routine using weave.inline:
def asum(arr):
raise NotImplementedError
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.