Menu

[r4663]: / trunk / py4science / examples / pyrex / sums / sums.pyx  Maximize  Restore  History

Download this file

40 lines (28 with data), 793 Bytes

 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
39
# -*- Mode: Python -*- Not really, but close enough
cimport c_python
cimport c_numpy
import numpy
# Numpy must be initialized
c_numpy.import_array()
def sum_elements(c_numpy.ndarray arr):
cdef int i
cdef double x, val
x = 0.
val = 0.
for i from 0<=i<arr.dimensions[0]:
val = (<double*>(arr.data + i*arr.strides[0]))[0]
x = x + val
return x
def sum_elements2(c_numpy.ndarray arr):
cdef int i
cdef double x, val
arr = numpy.asarray(arr, numpy.float_)
if arr.nd!=1:
raise RuntimeError('only 1D arrays supported; found shape=%s'%str(arr.shape))
assert(arr.nd==1)
x = 0.
val = 0.
for i from 0<=i<arr.dimensions[0]:
val = (<double*>(arr.data + i*arr.strides[0]))[0]
x = x + val
return x
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.