C++ 有R代码段,想在C++;

C++ 有R代码段,想在C++;,c++,r,quantitative-finance,C++,R,Quantitative Finance,下面是一篇技术论文中的R代码片段,我想在正在编写的C++程序中执行它 for(i in 1:m) w[i] <- 1/sum(exp(L-L[i])) 等等 我不确定如何在C++中执行此操作。您可以执行以下操作: #include <vector> #include <cmath> std::vector<double> func(const std::vector<double> &L) { std::vector<

下面是一篇技术论文中的R代码片段,我想在正在编写的C++程序中执行它

for(i in 1:m)
w[i] <- 1/sum(exp(L-L[i]))
等等


我不确定如何在C++中执行此操作。

您可以执行以下操作:

#include <vector>
#include <cmath>

std::vector<double> func(const std::vector<double> &L)
{
    std::vector<double> w;
    double sum;
    for (int i = 0, endi = L.size(); i < endi; ++i)
    {
        sum = 0.0;
        for (int j = 0, endj = i; j < endj; ++j)
        {
            sum += exp(L[j]-L[i]);
        }
        w.push_back(1.0/sum);
    }
    return w;
}
#包括
#包括
std::vector func(const std::vector&L)
{
std::向量w;
双和;
对于(int i=0,endi=L.size();i
总和可由外部for循环内的循环处理。至少试着编写一些psuedo代码。
#include <vector>
#include <cmath>

std::vector<double> func(const std::vector<double> &L)
{
    std::vector<double> w;
    double sum;
    for (int i = 0, endi = L.size(); i < endi; ++i)
    {
        sum = 0.0;
        for (int j = 0, endj = i; j < endj; ++j)
        {
            sum += exp(L[j]-L[i]);
        }
        w.push_back(1.0/sum);
    }
    return w;
}