Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python c++;_Python_C++_Numpy - Fatal编程技术网

Python c++;

Python c++;,python,c++,numpy,Python,C++,Numpy,我正在尝试编写python的numpy.linspace函数的类似版本 double linspace(int a, int b, int c){ double line[c]; double delta =b-a/(c-1); for (int i=0; i<c; ++i){ line[i]=0 + (i*delta); } return line; 有没有人会碰巧遇到knw如何解决这个问题?你想做的事情都不管用。首先,您

我正在尝试编写python的numpy.linspace函数的类似版本

double linspace(int a, int b, int c){
    double line[c];
    double delta =b-a/(c-1);
    for (int i=0; i<c; ++i){
            line[i]=0 + (i*delta);
    }
    return line;

有没有人会碰巧遇到knw如何解决这个问题?

你想做的事情都不管用。首先,您要在
linspace
中使用

double line[c];
您可以在调用之前分配内存并将其传入,也可以动态分配内存并返回(记住以后释放内存)

要动态分配,可以执行以下操作:

double * line = new double[c];
同样,在您完成后的某个时候,需要像这样释放它,否则您将有内存泄漏

delete line[];

双线[c]
创建一个双精度数组,并
指向该数组。所以这行是一个
双*
。您将函数的返回类型指定为
double

类似的内容如何:

#include <iostream>
#include <vector>

template<typename T>
std::vector<double> linspace(T start_in, T end_in, int num_in)
{

  std::vector<double> linspaced;

  double start = static_cast<double>(start_in);
  double end = static_cast<double>(end_in);
  double num = static_cast<double>(num_in);

  if (num == 0) { return linspaced; }
  if (num == 1) 
    {
      linspaced.push_back(start);
      return linspaced;
    }

  double delta = (end - start) / (num - 1);

  for(int i=0; i < num-1; ++i)
    {
      linspaced.push_back(start + delta * i);
    }
  linspaced.push_back(end); // I want to ensure that start and end
                            // are exactly the same as the input
  return linspaced;
}

void print_vector(std::vector<double> vec)
{
  std::cout << "size: " << vec.size() << std::endl;
  for (double d : vec)
    std::cout << d << " ";
  std::cout << std::endl;
}

int main()
{
  std::vector<double> vec_1 = linspace(1, 10, 3);
  print_vector(vec_1);

  std::vector<double> vec_2 = linspace(6.0, 23.4, 5);
  print_vector(vec_2);

  std::vector<double> vec_3 = linspace(0.0, 2.0, 1);
  print_vector(vec_3);

  std::vector<double> vec_4 = linspace(0.0, 2.0, 0);
  print_vector(vec_4);


  return 0;
}
Numpy结果:

In [14]: np.linspace(1, 10, 3)
Out[14]: array([  1. ,   5.5,  10. ])

In [15]: np.linspace(6, 23.4, 5)
Out[15]: array([  6.  ,  10.35,  14.7 ,  19.05,  23.4 ])

In [16]: np.linspace(0.0, 2.0, 1)
Out[16]: array([ 0.])

In [17]: np.linspace(0.0, 2.0, 0)
Out[17]: array([], dtype=float64)

什么例子?如果您更改
双线[c]到'double*行=新的double[c];`并更改所完成函数的返回类型。无论何时处理完数据,都需要删除此函数返回的内容。所以<代码>双*lin_ret=linspace(a、b、c)完成
lin_-ret
do
delete lin_-ret[]后哈!一个询问者要求一个例子!这太丰富了。你真的应该使用向量,而不是裸的
new
b-a/(c-1)
(你是说
(b-a)/(c-1)
?)是整数除法。可能有兴趣。总结了一组模拟C++中的NoMy的库。你的模板对我来说失败了,我逐字地复制它,得到向量的超出范围错误。@谢谢你指出这一点!我想我已经修好了。我像这样编译它,它工作正常:
g++-std=c++11 my_file.cpp-o my_exec
。我必须删除“linspaced.push_back(end);”命令,因为它使“结束”出现了两次。@PedroH.N.Vieira,感谢您指出这一点。我来看看。你有样本输入,这样我就可以重现这个问题了吗?它出现在我调用“linspace(-1.0,1.0,21)”时
size: 3
1 5.5 10 
size: 5
6 10.35 14.7 19.05 23.4 
size: 1
0 
size: 0
In [14]: np.linspace(1, 10, 3)
Out[14]: array([  1. ,   5.5,  10. ])

In [15]: np.linspace(6, 23.4, 5)
Out[15]: array([  6.  ,  10.35,  14.7 ,  19.05,  23.4 ])

In [16]: np.linspace(0.0, 2.0, 1)
Out[16]: array([ 0.])

In [17]: np.linspace(0.0, 2.0, 0)
Out[17]: array([], dtype=float64)