在 Python 2.7 中编织内联 C++ 代码
Posted
技术标签:
【中文标题】在 Python 2.7 中编织内联 C++ 代码【英文标题】:Weave Inline C++ Code in Python 2.7 【发布时间】:2015-02-02 09:26:34 【问题描述】:我正在尝试重写这个函数:
def smoothen_fast(heightProfile, travelTime):
smoothingInterval = 30 * travelTime
heightProfile.extend([heightProfile[-1]]*smoothingInterval)
# Get the mean of first `smoothingInterval` items
first_mean = sum(heightProfile[:smoothingInterval]) / smoothingInterval
newHeightProfile = [first_mean]
for i in xrange(len(heightProfile)-smoothingInterval-1):
prev = heightProfile[i] # the item to be subtracted from the sum
new = heightProfile[i+smoothingInterval] # item to be added
# Calculate the sum of previous items by multiplying
# last mean with smoothingInterval
prev_sum = newHeightProfile[-1] * smoothingInterval
new_sum = prev_sum - prev + new
mean = new_sum / smoothingInterval
newHeightProfile.append(mean)
return newHeightProfile
作为嵌入式 C++ 代码:
import scipy.weave as weave
heightProfile = [0.14,0.148,1.423,4.5]
heightProfileSize = len(heightProfile)
travelTime = 3
code = r"""
#include <string.h>
int smoothingInterval = 30 * travelTime;
double *heightProfileR = new double[heightProfileSize+smoothingInterval];
for (int i = 0; i < heightProfileSize; i++)
heightProfileR[i] = heightProfile[i];
for (int i = 0; i < smoothingInterval; i++)
heightProfileR[heightProfileSize+i] = -1;
double mean = 0;
for (int i = 0; i < smoothingInterval; i++)
mean += heightProfileR[i];
mean = mean/smoothingInterval;
double *heightProfileNew = new double[heightProfileSize-smoothingInterval];
for (int i = 0; i < heightProfileSize-smoothingInterval-1; i++)
double prev = heightProfileR[i];
double newp = heightProfile[i+smoothingInterval];
double prev_sum = heightProfileNew[i] * smoothingInterval;
double new_sum = prev_sum - prev + newp;
double meanp = new_sum / smoothingInterval;
heightProfileNew[i+1] = meanp;
return_val = Py::new_reference_to(Py::Double(heightProfileNew));
"""
d = weave.inline(code,['heightProfile','heightProfileSize','travelTime'])
作为返回类型,我需要heightProfileNew
。稍后我需要像 Python 中的列表一样访问它。
我看看这些例子:
http://docs.scipy.org/doc/scipy/reference/tutorial/weave.html
他一直告诉我他不知道Py::
,但在示例中没有 Py-Includes?
【问题讨论】:
您的标题是 C,您的问题是 C++/C,并且被标记为 C++,而不是 C,并且您的代码是 C++ 和 C 的混合体。选择这些语言中的 一种 ! 【参考方案1】:我知道,这个问题很老了,但我认为它仍然很有趣。
假设您使用 weave 来提高计算速度并且您事先知道输出的长度,我建议您在调用 inline
之前创建结果。这样您就可以在 python 中创建结果变量(非常简单)。我还建议使用nd.ndarray
作为结果,因为它可以让您使用正确的数据类型。您可以像迭代列表一样在 python 中迭代ndarrays
。
import numpy as np
heightProfileArray = np.array(heightprofile)
# heightProfileArray = np.array(heightprofile, dtype = np.float32) if you want to make shure you have the right datatype. Another choice would be np.float64
resultArray = np.zeros_like(heightProfileArray) # same array size and data type but filled with zeros
[..]
weave.inline(code,['heightProfile','heightProfileSize','travelTime','resultArray'])
for element in resultArray:
print element
在您的 C++ 代码中,您可以将值分配给该数组的元素:
[..]
resultArray[i+1] = 5.5;
[..]
【讨论】:
以上是关于在 Python 2.7 中编织内联 C++ 代码的主要内容,如果未能解决你的问题,请参考以下文章