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
506cbb03
Commit
506cbb03
authored
Jun 13, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved two code examples from cdef_classes.rst to the examples directory to enable testing on them.
parent
5fadf79e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
17 deletions
+32
-17
docs/examples/tutorial/cdef_classes/integrate.pyx
docs/examples/tutorial/cdef_classes/integrate.pyx
+14
-0
docs/examples/tutorial/cdef_classes/sin_of_square.pxd
docs/examples/tutorial/cdef_classes/sin_of_square.pxd
+5
-0
docs/examples/tutorial/cdef_classes/sin_of_square.pyx
docs/examples/tutorial/cdef_classes/sin_of_square.pyx
+9
-0
docs/src/tutorial/cdef_classes.rst
docs/src/tutorial/cdef_classes.rst
+4
-17
No files found.
docs/examples/tutorial/cdef_classes/integrate.pyx
0 → 100644
View file @
506cbb03
from
sin_of_square
cimport
Function
,
SinOfSquareFunction
def
integrate
(
Function
f
,
double
a
,
double
b
,
int
N
):
cdef
int
i
cdef
double
s
,
dx
if
f
is
None
:
raise
ValueError
(
"f cannot be None"
)
s
=
0
dx
=
(
b
-
a
)
/
N
for
i
in
range
(
N
):
s
+=
f
.
evaluate
(
a
+
i
*
dx
)
return
s
*
dx
print
(
integrate
(
SinOfSquareFunction
(),
0
,
1
,
10000
))
docs/examples/tutorial/cdef_classes/sin_of_square.pxd
0 → 100644
View file @
506cbb03
cdef
class
Function
:
cpdef
double
evaluate
(
self
,
double
x
)
except
*
cdef
class
SinOfSquareFunction
(
Function
):
cpdef
double
evaluate
(
self
,
double
x
)
except
*
docs/examples/tutorial/cdef_classes/sin_of_square.pyx
0 → 100644
View file @
506cbb03
from
libc.math
cimport
sin
cdef
class
Function
:
cpdef
double
evaluate
(
self
,
double
x
)
except
*
:
return
0
cdef
class
SinOfSquareFunction
(
Function
):
cpdef
double
evaluate
(
self
,
double
x
)
except
*
:
return
sin
(
x
**
2
)
docs/src/tutorial/cdef_classes.rst
View file @
506cbb03
...
...
@@ -40,31 +40,18 @@ function on floating point numbers::
return 0
The directive cpdef makes two versions of the method available; one
fast for use from Cython and one slower for use from Python. Then:
:
fast for use from Cython and one slower for use from Python. Then:
cdef class SinOfSquareFunction(Function):
cpdef double evaluate(self, double x) except *:
return sin(x**2)
.. literalinclude:: ../../examples/tutorial/cdef_classes/sin_of_square.pyx
This does slightly more than providing a python wrapper for a cdef
method: unlike a cdef method, a cpdef method is fully overridable by
methods and instance attributes in Python subclasses. It adds a
little calling overhead compared to a cdef method.
Using this, we can now change our integration example::
def integrate(Function f, double a, double b, int N):
cdef int i
cdef double s, dx
if f is None:
raise ValueError("f cannot be None")
s = 0
dx = (b-a)/N
for i in range(N):
s += f.evaluate(a+i*dx)
return s * dx
Using this, we can now change our integration example:
print(integrate(SinOfSquareFunction(), 0, 1, 10000))
.. literalinclude:: ../../examples/tutorial/cdef_classes/integrate.pyx
This is almost as fast as the previous code, however it is much more flexible
as the function to integrate can be changed. We can even pass in a new
...
...
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