mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-01 05:51:01 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
|
||||
`Cython` 属性可以使用关键词 `property` 来定义,然后定义 `__get__` 和 `__set__` 方法来进行获取和设置:
|
||||
|
||||
```
|
||||
```py
|
||||
property name:
|
||||
def __get__(self):
|
||||
return something
|
||||
@@ -29,7 +29,7 @@ property name:
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file particle_extern.h
|
||||
#ifndef _PARTICLE_EXTERN_H_
|
||||
#define _PARTICLE_EXTERN_H_
|
||||
@@ -65,14 +65,14 @@ class Particle {
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting particle_extern.h
|
||||
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file particle_extern.cpp
|
||||
#include "particle_extern.h"
|
||||
|
||||
@@ -96,7 +96,7 @@ void Particle::applyImpulse(float *f, float t)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting particle_extern.cpp
|
||||
|
||||
```
|
||||
@@ -105,7 +105,7 @@ Overwriting particle_extern.cpp
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file particle.pyx
|
||||
import numpy as np
|
||||
|
||||
@@ -170,14 +170,14 @@ cdef class Particle:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting particle.pyx
|
||||
|
||||
```
|
||||
|
||||
首先从头文件声明这个类:
|
||||
|
||||
```
|
||||
```py
|
||||
cdef extern from "particle_extern.h":
|
||||
|
||||
cppclass _Particle "Particle":
|
||||
@@ -193,7 +193,7 @@ cdef extern from "particle_extern.h":
|
||||
|
||||
这里要使用 `cppclass` 关键词,并且为了方便,我们将 `Particle` 类的名字在 `Cython` 中重命名为 `_Particle`。
|
||||
|
||||
```
|
||||
```py
|
||||
cdef class Particle:
|
||||
cdef _Particle *thisptr
|
||||
def __cinit__(self, m, c, float[::1] p, float[::1] v):
|
||||
@@ -207,7 +207,7 @@ cdef class Particle:
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file setup.py
|
||||
from distutils.core import setup
|
||||
from distutils.extension import Extension
|
||||
@@ -222,7 +222,7 @@ setup(
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting setup.py
|
||||
|
||||
```
|
||||
@@ -231,12 +231,12 @@ Overwriting setup.py
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
!python setup.py build_ext -i
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
running build_ext
|
||||
cythoning particle.pyx to particle.cpp
|
||||
building 'particle' extension
|
||||
@@ -247,7 +247,7 @@ C:\Anaconda\Scripts\g++.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Relea
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
particle.cpp: In function 'void __Pyx_RaiseArgtupleInvalid(const char*, int, Py_ssize_t, Py_ssize_t, Py_ssize_t)':
|
||||
particle.cpp:14931:59: warning: unknown conversion type character 'z' in format [-Wformat]
|
||||
particle.cpp:14931:59: warning: format '%s' expects argument of type 'char*', but argument 5 has type 'Py_ssize_t {aka long long int}' [-Wformat]
|
||||
@@ -286,7 +286,7 @@ particle.cpp:16941:50: warning: too many arguments for format [-Wformat-extra-ar
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
import particle
|
||||
|
||||
```
|
||||
@@ -295,7 +295,7 @@ import particle
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
import numpy as np
|
||||
|
||||
pos = vel = np.arange(3., dtype='float32')
|
||||
@@ -309,7 +309,7 @@ p
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
particle.Particle(mass=1.0, charge=2.0, pos=[ 0\. 1\. 2.], vel=[ 0\. 1\. 2.])
|
||||
```
|
||||
|
||||
@@ -317,7 +317,7 @@ particle.Particle(mass=1.0, charge=2.0, pos=[ 0\. 1\. 2.], vel=[ 0\. 1\. 2.]
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
p.apply_impulse(np.arange(3., dtype='float32'), 1.0)
|
||||
|
||||
p
|
||||
@@ -326,7 +326,7 @@ p
|
||||
|
||||
Out[8]:
|
||||
|
||||
```
|
||||
```py
|
||||
particle.Particle(mass=1.0, charge=2.0, pos=[ 0\. 1.5 3\. ], vel=[ 0\. 2\. 4.])
|
||||
```
|
||||
|
||||
@@ -334,14 +334,14 @@ particle.Particle(mass=1.0, charge=2.0, pos=[ 0\. 1.5 3\. ], vel=[ 0\. 2\.
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
p.mass
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
1.0
|
||||
```
|
||||
|
||||
@@ -349,7 +349,7 @@ Out[9]:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
p.mass = 3.0
|
||||
|
||||
```
|
||||
@@ -358,14 +358,14 @@ p.mass = 3.0
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
p.charge
|
||||
|
||||
```
|
||||
|
||||
Out[11]:
|
||||
|
||||
```
|
||||
```py
|
||||
2.0
|
||||
```
|
||||
|
||||
@@ -373,7 +373,7 @@ Out[11]:
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
import zipfile
|
||||
|
||||
f = zipfile.ZipFile('07-05-particle.zip','w',zipfile.ZIP_DEFLATED)
|
||||
|
||||
Reference in New Issue
Block a user