0

Is there an easy way to wrap a double** in a c++ Eigen typ so that it can be used in Eigen expressions? As far as I know, the Eigen::Map class only supports double*. The storage where double** points to is not guaranteed to be continuous.

Jodebo
  • 125
  • 6
  • 1
    Possible duplicate of [Map a Eigen Matrix to an C array](http://stackoverflow.com/questions/13535399/map-a-eigen-matrix-to-an-c-array) – phuclv Dec 02 '16 at 06:15
  • [Convert Eigen Matrix to C array](http://stackoverflow.com/q/8443102/995714) – phuclv Dec 02 '16 at 06:15
  • 1
    As stated in my question I want to wrap a C matrix not a C array. The two links only show the usage of `Eigen::Map` with a C array. The storage where the `double**` points to is not guaranteed to be continuous. – Jodebo Dec 02 '16 at 07:37
  • 1
    there's no matrix type in C. If you create it your own so you have to work with them manually – phuclv Dec 02 '16 at 07:57
  • 1
    @Jodebo `double**` is not a matrix, assuming array decay, it's an array of arrays at best. It doesn't guarantee rectangular size, which is pretty important for a matrix, that's a bigger problem IMO. – luk32 Dec 02 '16 at 09:53
  • @luk32 That's true but I know that the shape is rectangular. I want to use it with `ceres::DynamicAutoDiffCostFunction` [Link](http://ceres-solver.org/nnls_modeling.html#dynamicautodiffcostfunction) and the interface of the cost functor is `T const* const*`. – Jodebo Dec 08 '16 at 07:37

1 Answers1

0

As you pointed out, you can use an Eigen::Map to wrap contiguous data:

double *data = new...
Map<MatrixXd> mappedData(data, m, n);
mappedData.transposeInPlace(); // etc.

Non-contiguous data has to be made contiguous, either as a copy in an Eigen object, or as a contiguous double* and using a Map as above.

Edit

You can't use (map/wrap) a non-contiguous section of memory as an Eigen::Matrix. If you have control of the memory allocation, you can do something like:

// make contiguous data array; better yet, make an
// Eigen::VectorXd of the same size to ensure alignment
double *actualData = new double[m*n]; 
double **columnPointers = new double[n];
for(int i = 0; i < n; ++i) columnPointers[i] = actualData + i * m;
... // do whatever you need to do with the double**
Map<MatrixXd> mappedData(actualData, m, n);
... // send to ceres, or whatever
Community
  • 1
  • 1
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
  • I don't want to copy the data, I would like to wrap it. I want to use it with `ceres::DynamicAutoDiffCostFunction` [Link](http://ceres-solver.org/nnls_modeling.html#dynamicautodiffcostfunction) where copying is probably too expensive. – Jodebo Dec 08 '16 at 07:29