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:
@@ -6,7 +6,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
from ctypes import util, CDLL
|
||||
|
||||
```
|
||||
@@ -17,7 +17,7 @@ from ctypes import util, CDLL
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
libc_name = util.find_library('c')
|
||||
|
||||
# on WINDOWS
|
||||
@@ -25,7 +25,7 @@ print libc_name
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
msvcr90.dll
|
||||
|
||||
```
|
||||
@@ -34,7 +34,7 @@ msvcr90.dll
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
libc = CDLL(libc_name)
|
||||
|
||||
```
|
||||
@@ -43,14 +43,14 @@ libc 包含 `C` 标准库中的函数:
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
libc.printf
|
||||
|
||||
```
|
||||
|
||||
Out[4]:
|
||||
|
||||
```
|
||||
```py
|
||||
<_FuncPtr object at 0x0000000003CEE048>
|
||||
```
|
||||
|
||||
@@ -58,14 +58,14 @@ Out[4]:
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
libc.printf("%s, %d\n", "hello", 5)
|
||||
|
||||
```
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
9
|
||||
```
|
||||
|
||||
@@ -77,14 +77,14 @@ Out[5]:
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
libm_name = util.find_library('m')
|
||||
|
||||
print libm_name
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
msvcr90.dll
|
||||
|
||||
```
|
||||
@@ -93,14 +93,14 @@ msvcr90.dll
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
libm = CDLL(libm_name)
|
||||
|
||||
libm.atan2(1.0, 2.0)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
---------------------------------------------------------------------------
|
||||
ArgumentError Traceback (most recent call last)
|
||||
<ipython-input-7-e784e41ee9f3> in <module>()
|
||||
@@ -115,7 +115,7 @@ ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to conv
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
from ctypes import c_double
|
||||
|
||||
libm.atan2.argtypes = [c_double, c_double]
|
||||
@@ -125,14 +125,14 @@ libm.atan2.restype = c_double
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
libm.atan2(1.0, 2.0)
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
0.4636476090008061
|
||||
```
|
||||
|
||||
@@ -140,21 +140,21 @@ Out[9]:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
from math import atan2
|
||||
|
||||
```
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
atan2(1.0, 2.0)
|
||||
|
||||
```
|
||||
|
||||
Out[11]:
|
||||
|
||||
```
|
||||
```py
|
||||
0.4636476090008061
|
||||
```
|
||||
|
||||
@@ -162,7 +162,7 @@ Out[11]:
|
||||
|
||||
假设我们有这样的一个函数:
|
||||
|
||||
```
|
||||
```py
|
||||
float _sum(float *vec, int len) {
|
||||
float sum = 0.0;
|
||||
int i;
|
||||
@@ -176,7 +176,7 @@ float _sum(float *vec, int len) {
|
||||
|
||||
并且已经编译成动态链接库,那么我们可以这样调用:
|
||||
|
||||
```
|
||||
```py
|
||||
from ctypes import c_float, CDLL, c_int
|
||||
from numpy import array, float32
|
||||
from numpy.ctypeslib import ndpointer
|
||||
|
||||
Reference in New Issue
Block a user