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
Kirill Smelkov
cython
Commits
2e13048e
Commit
2e13048e
authored
Mar 27, 2012
by
Liguo Kong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update docs/src/tutorial/external.rst
parent
571bff88
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 addition
and
35 deletions
+1
-35
docs/src/tutorial/external.rst
docs/src/tutorial/external.rst
+1
-35
No files found.
docs/src/tutorial/external.rst
View file @
2e13048e
...
@@ -76,38 +76,4 @@ declaration in ``math.h`` at compile time, but Cython does not parse
...
@@ -76,38 +76,4 @@ declaration in ``math.h`` at compile time, but Cython does not parse
Just like the ``sin()`` function from the math library, it is possible
Just like the ``sin()`` function from the math library, it is possible
to declare and call into any C library as long as the module that
to declare and call into any C library as long as the module that
Cython generates is properly linked against the shared or static
Cython generates is properly linked against the shared or static
library.
library.
Since use of pointers in C is ubiquitous, here we give a quick example of how
\ No newline at end of file
to call C functions whose arguments contain pointers. Suppose you want to
manage an array (allocate and deallocate) with Numpy, but its data are
computed by an external C function declared in :file:`C_func_file.h`::
void C_func(double * CPointer, unsigned int N);
where CPointer points to the array and N is its size.
You can call the function in a Cython file in the following way::
cdef extern from "C_func_file.h":
void C_func(double *, unsigned int)
import cython
import numpy as np
cimport numpy as np
def f(arr): # 'arr' is a one-dimensional array of size N
# Before calling the external function, we need to check whether the
# memory for 'arr' is contiguous or not; if not, we store the computed
# data in an contiguous array and then copy the data from that array.
np.ndarray[np.double_t, ndim=1, mode="c"] contig_arr
if arr.flags.c_contiguous:
contig_arr = arr
else:
contig_arr = arr.copy('C')
C_func(<cython.double *> contig_arr.data, contig_arr.size)
if contig_arr is not arr:
arr[...] = contig_arr
return
This way, you can have access the function more or less as a regular
Python function while its data and associated memory gracefully managed
by Numpy.
\ No newline at end of file
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