前期准备:
numpy.i文件,有些conda下载的numpy库里面自带,而有些没有(比如我的,,),可以去下载numpy.i源码。
我们实现一个cos_doubles模块的cos_double函数来对numpy数组求cos, 并返回他的cos值到一个新的numpy数组中:
测试样例test.py如下:
import numpy as np
import cos_doubles
x = np.arange(0, 2 * np.pi, 0.1)
y = np.empty_like(x)
cos_doubles.cos_doubles_func(x, y)
print(x)
print(y)
首先我们先实现cos_doubles 的C版本: cos_doubles.h
void cos_doubles(double * in_array, double * out_array, int size);
cos_doubles.c
#include <math.h>
#include "cos_doubles.h"
/* Compute the cosine of each element in in_array, storing the result in
* out_array. */
void cos_doubles(double * in_array, double * out_array, int size){
int i;
for(i=0;