Как передать массивы в функцию linspace в C ?

#c #arrays #pointers #floating-point #linspace

Вопрос:

Отказ от ответственности: это не моя специальность.

Я пытаюсь ввести 2 разных массива строк по 3 столбца 1 в функцию linspace с помощью пакета NumCPP, но получаю такие ошибки, как:

«ни один экземпляр шаблона функции» nc::linspace » не соответствует списку аргументов-типы аргументов: (float, float, int)»

код, относящийся к этой ошибке, выглядит следующим образом:

 float** XYZ[3];  float** function(float array_A, float array_B, int C) {  XYZ** = nc::linspacelt;float**gt;(array_A, array_B, C);  return XYZ; };  

Ближе к концу моего кода в основной функции я определяю эти параметры как:

 float array_A [3]= {0,0,0};  float array_B [3]= {0,PI/4,0};  int C = 10000;  

Я сделал то же самое с python, используя функцию linspace numpy, и у меня нет проблем. C — это сложно, поэтому мы ценим любую помощь.

Ответ №1:

Вот пример того, как это сделать (без массивов в стиле «C»)

 #include lt;cassertgt; #include lt;iostreamgt; #include lt;vectorgt; #include lt;NumCpp/Functions/linspace.hppgt;  // in C   std::vector (or std::array) are preferred over manually allocated "C" style arrays. // this will avoid a lot of issues related to (pointer) type decay in which actual size information  // of arrays is lost, and it will avoid manual memory managment with new/delete.  // pass vector's by constamp; so they won't be copied // make number_of_samples a size_t type since it should never be lt; 0 (which is allowed by int) auto linspace(const std::vectorlt;doublegt;amp; arr1, const std::vectorlt;doublegt;amp; arr2, const std::size_t number_of_samples) {  std::vectorlt;std::vectorlt;doublegt;gt; retval;  assert(arr1.size() == arr2.size());   for (std::size_t n = 0; n lt; arr1.size();   n)  {  // linspace documentationhttps://dpilger26.github.io/NumCpp/doxygen/html/namespacenc.html#a672fbcbd2271d5fc58bd1b94750bbdcc  // in C   linspace only accepts values, not array like stuff.  nc::NdArraylt;doublegt; sub_result = nc::linspace(arr1[n], arr2[n], static_castlt;nc::uint32gt;(number_of_samples));   // construct a std::vectorlt;doublegt; from nc::NdArray and put it at back of return value  retval.emplace_back(sub_result.begin(), sub_result.end());  }   return retval; }  int main() {  // Create two dynamically allocated arrays of doubles  std::vectorlt;doublegt; array_A{ 0.0, 1.0, 2.0 };  std::vectorlt;doublegt; array_B{ 4.0, 5.0, 6.0 };   // do a linspace on them (linespace is what you called function)  const std::size_t number_of_samples = 4;  auto result = linspace(array_A, array_B, number_of_samples);   // and show the output  std::cout lt;lt; "result of linspace : n";   for (const autoamp; arr : result)  {  bool comma = false;  // range based for loop over the result array to display output  for (const autoamp; value : arr)  {  if (comma) std::cout lt;lt; ", ";  std::cout lt;lt; value;  comma = true;  }  std::cout lt;lt; "n";  }   return 0; }