mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 10:34:08 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
可以定义局部变量:
|
||||
|
||||
```
|
||||
```py
|
||||
def fib(int n):
|
||||
cdef int a,b,i
|
||||
...
|
||||
@@ -17,7 +17,7 @@ def fib(int n):
|
||||
|
||||
定义函数返回值:
|
||||
|
||||
```
|
||||
```py
|
||||
cdef float distance(float *x, float *y, int n):
|
||||
cdef:
|
||||
int i
|
||||
@@ -30,7 +30,7 @@ cdef float distance(float *x, float *y, int n):
|
||||
|
||||
定义函数:
|
||||
|
||||
```
|
||||
```py
|
||||
cdef class Particle(object):
|
||||
cdef float psn[3], vel[3]
|
||||
cdef int id
|
||||
@@ -51,7 +51,7 @@ cdef class Particle(object):
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
from math import sin as pysin
|
||||
from numpy import sin as npsin
|
||||
|
||||
@@ -59,7 +59,7 @@ from numpy import sin as npsin
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%load_ext Cython
|
||||
|
||||
```
|
||||
@@ -68,7 +68,7 @@ In [2]:
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%cython
|
||||
from libc.math cimport sin
|
||||
from libc.stdlib cimport malloc, free
|
||||
@@ -81,14 +81,14 @@ from libc.stdlib cimport malloc, free
|
||||
|
||||
`fib.pxd, fib.pyx` 文件存在,那么可以这样调用:
|
||||
|
||||
```
|
||||
```py
|
||||
from fib cimport fib
|
||||
|
||||
```
|
||||
|
||||
还可以调用 `C++` 标准库和 `Numpy C Api` 中的文件:
|
||||
|
||||
```
|
||||
```py
|
||||
from libcpp.vector cimport vector
|
||||
cimport numpy as cnp
|
||||
|
||||
@@ -100,7 +100,7 @@ cimport numpy as cnp
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file len_extern.pyx
|
||||
cdef extern from "string.h":
|
||||
int strlen(char *c)
|
||||
@@ -110,7 +110,7 @@ def get_len(char *message):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing len_extern.pyx
|
||||
|
||||
```
|
||||
@@ -119,7 +119,7 @@ Writing len_extern.pyx
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file setup_len_extern.py
|
||||
from distutils.core import setup
|
||||
from distutils.extension import Extension
|
||||
@@ -132,7 +132,7 @@ setup(
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing setup_len_extern.py
|
||||
|
||||
```
|
||||
@@ -141,12 +141,12 @@ Writing setup_len_extern.py
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
!python setup_len_extern.py build_ext --inplace
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
running build_ext
|
||||
cythoning len_extern.pyx to len_extern.c
|
||||
building 'len_extern' extension
|
||||
@@ -163,7 +163,7 @@ C:\Miniconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Rele
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
import len_extern
|
||||
|
||||
```
|
||||
@@ -172,14 +172,14 @@ import len_extern
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
dir(len_extern)
|
||||
|
||||
```
|
||||
|
||||
Out[8]:
|
||||
|
||||
```
|
||||
```py
|
||||
['__builtins__',
|
||||
'__doc__',
|
||||
'__file__',
|
||||
@@ -193,14 +193,14 @@ Out[8]:
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
len_extern.get_len('hello')
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
```
|
||||
|
||||
@@ -208,14 +208,14 @@ Out[9]:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
len_extern.get_len('hello\0world!')
|
||||
|
||||
```
|
||||
|
||||
Out[10]:
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
```
|
||||
|
||||
@@ -223,7 +223,7 @@ Out[10]:
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file time_extern.pyx
|
||||
cdef extern from "time.h":
|
||||
|
||||
@@ -246,7 +246,7 @@ def get_date():
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing time_extern.pyx
|
||||
|
||||
```
|
||||
@@ -255,7 +255,7 @@ Writing time_extern.pyx
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file setup_time_extern.py
|
||||
from distutils.core import setup
|
||||
from distutils.extension import Extension
|
||||
@@ -268,7 +268,7 @@ setup(
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing setup_time_extern.py
|
||||
|
||||
```
|
||||
@@ -277,12 +277,12 @@ Writing setup_time_extern.py
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
!python setup_time_extern.py build_ext --inplace
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
running build_ext
|
||||
cythoning time_extern.pyx to time_extern.c
|
||||
building 'time_extern' extension
|
||||
@@ -296,7 +296,7 @@ C:\Miniconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Rele
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
import time_extern
|
||||
|
||||
time_extern.get_date()
|
||||
@@ -305,7 +305,7 @@ time_extern.get_date()
|
||||
|
||||
Out[14]:
|
||||
|
||||
```
|
||||
```py
|
||||
(19, 9, 2015)
|
||||
```
|
||||
|
||||
@@ -313,7 +313,7 @@ Out[14]:
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
import zipfile
|
||||
|
||||
f = zipfile.ZipFile('07-04-extern.zip','w',zipfile.ZIP_DEFLATED)
|
||||
|
||||
Reference in New Issue
Block a user