pyscience11 icon indicating copy to clipboard operation
pyscience11 copied to clipboard

i dont find api numpy.c_

Open ronyuzhang opened this issue 6 years ago • 3 comments

i use c++ and call numpy , but i dont find c++ version of numpy.c_.

ronyuzhang avatar Sep 14 '18 16:09 ronyuzhang

Hi @ronyuzhang, sorry for inconvenience. Currently pyscience11 does not support numpy.c_ because it is an object rather than being a function. It is difficult to map them into C++ because C++ does not support properties.

Practically it is still possible to use numpy.c_, since pybind11 allows us to access properties via attr function.

#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pyscience11/numpy.h>

namespace py = pybind11;
namespace n11 = numpy11;

int main(void)
{
    py::scoped_interpreter interpreter;
    py::list li;
    li.append(1);
    li.append(2);
    py::print(py::repr(li));

    auto numpy = n11::import_numpy();
    py::print(py::repr(numpy.attr("c_")[li]));

    return 0;
}

This yields the following result.

$ ./sample
[1, 2]
array([[1],
       [2]])

Does it satisfy your purpose?

yokaze avatar Sep 16 '18 10:09 yokaze

so great, thanks. It would be better if we could improve the documentation. something, i dont how to use api to achieve my work.

ronyuzhang avatar Sep 22 '18 13:09 ronyuzhang

Thanks for your comment. I will consider to prepare some documentation.

yokaze avatar Oct 01 '18 13:10 yokaze