1

I have a 4d matrix of complex doubles being generated by numpy. I need to transfer these numbers to some C++ code, where the data can be stored in whatever format is most convenient for this purpose. It seems to me that this is a highly complicated problem, which would involve writing the 4d matrix to a text file in some regular way, and having the C++ code read it back line by line in the same way, while parsing the complex numbers correctly. In the interest of not reinventing the wheel, I was wondering if there was any standard package or way to transfer these sorts of data structures from Python code to some C++ equivalent.

For a concrete example:

A = np.zeros((10, 20, 50, 1000), dtype='complex128')
# Then fill up all the entries of A with values
Henry Shackleton
  • 351
  • 2
  • 11
  • 1
    Please provide some example data. Is each dimensional array the same size? Is the data always the same time? (not e.g. mixed `string` and `int`?) – Iyashi Oct 30 '20 at 18:45
  • If data type is always a primitive (fixed size like `int` => `4 bytes`), you can simply export it as binary data and read it in C++ and cast it to the specifc data type. – Iyashi Oct 30 '20 at 18:48
  • 1
    If you do this often, you could consider looking at Google Protocol Buffers. Or stuff it in Redis... https://stackoverflow.com/a/55313342/2836621 – Mark Setchell Oct 30 '20 at 19:13
  • @Iyashi unfortunately in C++ `int` isn't always 4 bytes. The Standard specifies a minimum of two and after that any size up to and including the size of `long` is legal. Currently 8 byte `int`s are uncommon but not unheard of. – user4581301 Oct 30 '20 at 19:30
  • Could you pass it through as a 1D array of the correct expanded size? – tadman Oct 30 '20 at 21:44
  • What is your intent? Do you want to process the data with some C++ library (or your own C++ code) and maybe get the result back into python? In that case you might try [pibind11](https://github.com/pybind/pybind11) instead. It's a great library to create python extensions with C++ code. But if you don't need this integration and you only want to serialize from python and read it back in C++ then simply using a text format like CSV is enough for low amount of data. If the data is large, then some binary format like [HDF5](https://www.hdfgroup.org/solutions/hdf5/) is better. – darcamo Oct 31 '20 at 01:12

0 Answers0