NumCpp icon indicating copy to clipboard operation
NumCpp copied to clipboard

nc.reshape not support 3-dimension array, can you add an overloading function?

Open Robinsci opened this issue 2 years ago • 1 comments

nc.reshape not support 3-dimension array, can you add an overloading function?

Robinsci avatar Aug 29 '23 08:08 Robinsci

to me, it appears this library is not really setup for 3d, and that you might be expected to slice your 3d data into 2d slices to

the following appears to work for future reference


#include "NumCpp.hpp"

#include <array>
#include <vector>
#include <cstdlib>
#include <iostream>

struct Slicer{

  template<typename Op>
  void slicer_2d(std::vector<double> &ds, Op& fn, std::size_t from){
    nc::NdArray<double> ar{ds.data() + from, dims[0], dims[1], nc::PointerPolicy::SHELL};
    fn(ar);
  }

  template<typename Op>
  void operator()(std::vector<double> & ds, Op fn){
    auto slice = dims[0] * dims[1];
    for(std::size_t k = 0; k < dims[2]; ++k){
      slicer_2d(ds, fn, k * slice);
    }
  }

  std::array<std::size_t, 3> dims;
};


int main(){
  std::vector<double> ds(27);
  for(std::size_t i = 0; i < ds.size(); ++i) ds[i] = i;

  Slicer{3,3,3}(ds, [](auto& ar){
      std::cout << ar << std::endl;
  });

  return 0;
}

PhilipDeegan avatar Mar 05 '24 10:03 PhilipDeegan

This has been asked and answered many times... No it doesn't, please see the documentation.

dpilger26 avatar Mar 27 '24 13:03 dpilger26