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
e858f582
Commit
e858f582
authored
Jun 15, 2018
by
scoder
Committed by
GitHub
Jun 15, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2331 from gabrieldemarmiesse/test_cdef_classes_3
Added tests for cdef_classes.rst. Part 3.
parents
d88c784a
3b9873ff
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
16 deletions
+36
-16
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
+8
-16
No files found.
docs/examples/tutorial/cdef_classes/integrate.pyx
0 → 100644
View file @
e858f582
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 @
e858f582
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 @
e858f582
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 @
e858f582
...
...
@@ -32,31 +32,23 @@ function on floating point numbers:
.. literalinclude:: ../../examples/tutorial/cdef_classes/math_function_2.pyx
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::
To make the classes reusable across modules, we define them
in a :file:`sin_of_square.pxd` file:
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
.. literalinclude:: ../../examples/tutorial/cdef_classes/sin_of_square.pxd
print(integrate(SinOfSquareFunction(), 0, 1, 10000))
Using this, we can now change our integration example:
.. 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