[PYTHON] Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-

Purpose

By using boost.python or boost.numpy, you can easily create a python module that can handle numpy.ndarray in c ++. Although boost.numpy is a well-made library, it has some inconveniences in accessing data, such as not being able to use iterators. Therefore, make the data easily accessible.

background

What is numpy.ndarray

It is a multidimensional array provided by numpy, one of the python packages. Without this, numerical calculations in python are so widely used that it is impossible.

What is boost.numpy

A library that allows you to operate numpy from c ++. Access the data as follows.

namespace bp = boost::python;
namespace np = boost::numpy;

void func(np::ndarray &data2d) {
  //data2d is i as a two-dimensional array,Assign 0 to j element
  for (int i = 0; i < data2d.shape(0); ++i) {
    for (int j = 0; j < data2d.shape(1); ++j) {
      data2d[bp::make_tuple(i, j)] = 0.0;
    }
  }
}

Conditions for tricks

In order to use this trick, the following conditions must be met.

I think the first condition will be met in many cases. Also, the second one should be satisfied unless it is a variable that was originally cut out from a large array.

Make iterators available

Use boost.multi_array_ref. Since numpy.ndarray is a python multidimensional array, c ++ also uses the multidimensional array library. boost.multi_array_ref is a self-allocating version of boost.multi_array. Click here for a detailed explanation of boost.multi_array. (boost :: multi_array --Kmonos.net) By the way, there is a version that can only read boost.const_multi_array_ref and cannot write.

Concrete example

void func(np:ndarray &data2d) {
  const std::array<int, 2> shape = {data2d.shape(0), data2d.shape(1)};
  boost::multi_array_ref<double, 2> wrapper(reinterpret_cast<double*>(data2d.get_data()), shape);
  // i,wrapper for j element[i][j]Can be accessed with, but uses an iterator
  for (boost::multi_array_ref_double, 2>::subarray<1>::type &&sub : wrapper) {
    boost::fill(sub, 0.0);
  }
}

Then you can access it like this.

Of course, there is some overhead associated with using boost.multi_array_ref. There is also a way to manipulate pointers directly (Boost.NumPy Tutorial for Extending Python in C ++ (Practice)), but with some overhead. I would like to take safety measures without worrying about it.

By the way, in the above example, boost :: multi_array_ref <double, 2> and a wrapper for a two-dimensional array are prepared because it is easy to understand and has a meaning of introduction.

const std::array<int, 1> shape = {data2d.shape(0) * data2d.shape(1)};
  boost::multi_array_ref<double, 1> wrapper(reinterpret_cast<double*>(data2d.get_data()), shape); 
boost::fill(wrapper, 0.0);

By doing so, the process is completed in one shot.

Finally

After all, the boost library can be relied on here as well. If you know of any other good ways (libraries other than boost.multi_array_ref, something to replace boost.numpy, etc.), please let us know.

Recommended Posts

Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
I want to use jar from python
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
I want to use ceres solver from python
I want to make C ++ code from Python code!
Tips for calling Python from C
I want to use an external library with IBM Cloud Functions
I want to use both key and value of Python iterator
I want to use Linux on mac
I want to use IPython Qt Console
I want to make an automation program!
An introduction to Python for C programmers
[Python] I was hooked for an hour trying to use list comprehensions
I want to connect to PostgreSQL from various languages
I want to use MATLAB feval with python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to be an OREMO with setParam!
Wrap C with Cython for use from Python
I want to make fits from my head
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
[Python] I want to manage 7DaysToDie from Discord! 2/3
Wrap C ++ with Cython for use from Python
I want to use the activation function Mish
[Python] I want to use only index when looping a list with a for statement
I want to convert an image to WebP with lollipop
I want to see the file name from DataLoader
I want to detect images of cats from Instagram
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to develop an Android application on Android (debugging)
I wanted to use the Python library from MATLAB
[Python3] I want to generate harassment names from Japanese!
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to use the R dataset in python
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
[For those who want to use TPU] I tried using the Tensorflow Object Detection API 2
I want to copy an English paper from pdf and put it in Google Translate
I want to use the Ubuntu desktop environment on Android for the time being (Termux version)
I want to use Ubuntu's desktop environment on Android for the time being (UserLAnd version)
I don't want to search for high para because it is IQ1 (how to use lightgbm_tuner)
[TensorFlow] I want to master the indexing for Ragged Tensor
I want to use the latest gcc without sudo privileges! !!
I want to use R functions easily with ipython notebook
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
I want to exchange gifts even for myself! [Christmas hackathon]
I want to move selenium for the time being [for mac]
I want to start a lot of processes from python
[NetworkX] I want to search for nodes with specific attributes
I want to use only the normalization process of SudachiPy
I want to calculate the allowable downtime from the operating rate
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to convert an ISO-8601 character string to Japan time
I didn't know how to use the [python] for statement
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot
Tips for Python beginners to use the Scikit-image example for themselves
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-