Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
b18d94e3
Commit
b18d94e3
authored
Dec 10, 2010
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change numeric demo to numpy demo using new features, update setup.py.
parent
31fe44bb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
54 deletions
+21
-54
Demos/numeric_demo.pyx
Demos/numeric_demo.pyx
+0
-39
Demos/numpy_demo.pyx
Demos/numpy_demo.pyx
+9
-0
Demos/setup.py
Demos/setup.py
+12
-15
No files found.
Demos/numeric_demo.pyx
deleted
100644 → 0
View file @
31fe44bb
#
# This example demonstrates how to access the internals
# of a Numeric array object.
#
cdef
extern
from
"Numeric/arrayobject.h"
:
struct
PyArray_Descr
:
int
type_num
,
elsize
char
type
ctypedef
class
Numeric
.
ArrayType
[
object
PyArrayObject
]:
cdef
char
*
data
cdef
int
nd
cdef
int
*
dimensions
,
*
strides
cdef
object
base
cdef
PyArray_Descr
*
descr
cdef
int
flags
def
print_2d_array
(
ArrayType
a
):
print
"Type:"
,
chr
(
a
.
descr
.
type
)
if
chr
(
a
.
descr
.
type
)
<>
"f"
:
raise
TypeError
(
"Float array required"
)
if
a
.
nd
<>
2
:
raise
ValueError
(
"2 dimensional array required"
)
cdef
int
nrows
,
ncols
cdef
float
*
elems
,
x
nrows
=
a
.
dimensions
[
0
]
ncols
=
a
.
dimensions
[
1
]
elems
=
<
float
*>
a
.
data
hyphen
=
"-"
divider
=
(
"+"
+
10
*
hyphen
)
*
ncols
+
"+"
print
divider
for
row
in
range
(
nrows
):
for
col
in
range
(
ncols
):
x
=
elems
[
row
*
ncols
+
col
]
print
"| %8f"
%
x
,
print
"|"
print
divider
Demos/numpy_demo.pyx
0 → 100644
View file @
b18d94e3
cimport
numpy
import
numpy
def
sum_of_squares
(
numpy
.
ndarray
[
double
,
ndim
=
1
]
arr
):
cdef
long
N
=
arr
.
shape
[
0
]
cdef
double
ss
=
0
for
i
in
range
(
N
):
ss
+=
arr
[
i
]
**
2
return
ss
Demos/setup.py
View file @
b18d94e3
import
glob
# Run as:
# python setup.py build_ext --inplace
from
distutils.core
import
setup
from
distutils.extension
import
Extension
from
Cython.
Distutils
import
build_ext
from
Cython.
Build
import
cythonize
ext_modules
=
cythonize
(
"*.pyx"
,
exclude
=
"numpy_*.pyx"
)
# Only compile the following if numpy is installed.
try
:
from
numpy.distutils.misc_util
import
get_numpy_include_dirs
numpy_include_dirs
=
get_numpy_include_dirs
()
except
:
numpy_include_dirs
=
[]
ext_modules
=
[
Extension
(
"primes"
,
[
"primes.pyx"
]),
Extension
(
"spam"
,
[
"spam.pyx"
]),
]
for
file
in
glob
.
glob
(
"*.pyx"
):
if
file
!=
"numeric_demo.pyx"
:
ext_modules
.
append
(
Extension
(
file
[:
-
4
],
[
file
],
include_dirs
=
numpy_include_dirs
))
numpy_demo
=
Extension
(
"*"
,
[
"numpy_*.pyx"
],
include_dirs
=
get_numpy_include_dirs
())
ext_modules
.
extend
(
cythonize
(
numpy_demo
))
except
ImportError
:
pass
setup
(
name
=
'Demos'
,
cmdclass
=
{
'build_ext'
:
build_ext
},
ext_modules
=
ext_modules
,
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment