0

Disclaimer: this stuff is not my specialty.

I am trying to feed 2 different 3 column 1 row arrays into a linspace function using the NumCPP package, but i'm getting errors such as:

"no instance of function template "nc::linspace" matches the argument list -- argument types are: (float, float, int)" <-- from VSCode intelisense and "error: cannot convert ‘float’ to ‘float**’" when ran in terminal.

the code relating to this error goes as follows:

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

Towards the end of my code in the main function I define these parameters as:

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

I did the same thing with python using numpy's linspace function and has no issues. C++ is tough, so any help is appreciated.

1 Answers1

0

Here is an example of how to do it (without "C" style arrays)

#include <cassert>
#include <iostream>
#include <vector>
#include <NumCpp/Functions/linspace.hpp>

// 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 const& so they won't be copied
// make number_of_samples a size_t type since it should never be < 0 (which is allowed by int)
auto linspace(const std::vector<double>& arr1, const std::vector<double>& arr2, const std::size_t number_of_samples)
{
    std::vector<std::vector<double>> retval;
    assert(arr1.size() == arr2.size());

    for (std::size_t n = 0; n < 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::NdArray<double> sub_result = nc::linspace(arr1[n], arr2[n], static_cast<nc::uint32>(number_of_samples));

        // construct a std::vector<double> 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::vector<double> array_A{ 0.0, 1.0, 2.0 };
    std::vector<double> 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 << "result of linspace : \n";

    for (const auto& arr : result)
    {
        bool comma = false;
        // range based for loop over the result array to display output
        for (const auto& value : arr)
        {
            if (comma) std::cout << ", ";
            std::cout << value;
            comma = true;
        }
        std::cout << "\n";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19