Using pybind11, how to create a NumPy array with zero value and pass its pointer to another function?

77 Views Asked by At

I'm new to C++ and Pybind11. In order to initialize a NumPy array with zero values, pass its reference to another function, and then return the results, is there a way to accomplish this?

Here's a simplified version of my code:

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include "foo2.h"

namespace py = pybind11;

py::array_t<double> foo1(py::array_t<double, py::array::c_style | py::array::forcecast> signal)

{
    // Accessing the data pointer of the input numpy array
    auto signal_data = signal.mutable_data(); // Mutable (non-const) pointer to the data of the NumPy array

    // Initialize output array and data pointers
    auto output = py::array_t<double>(signal.size());
    output[py::make_tuple(py::ellipsis())] = 0.0; // Set all values of the array to zero

    auto output_data = output.mutable_data();

    // Running another function
    foo2(signal_data, output_data);

    return output;
}

Does this approach make sense?

Thank you...

0

There are 0 best solutions below