在Python 2.7中编写内联C ++代码

时间:2015-02-02 09:26:34

标签: python c++ python-2.7 scipy

我正在尝试重写此功能:

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?

1 个答案:

答案 0 :(得分: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;
[..]