Using Eigen :: Map <Eigen :: MatrixXd> as an argument to a function of type Eigen :: MatrixXd

In short, the question is how to convey

Eigen::Map<Eigen::MatrixXd>

object to function that expects

Eigen::MatrixXd

an object.


Longer:

I have this C ++ function declaration

void npMatrix(const Eigen::MatrixXd &data, Eigen::MatrixXd &result);

along with this implementation

void npMatrix(const Eigen::MatrixXd &data, Eigen::MatrixXd &result)
{
//Just do s.th. with arguments
std::cout << data << std::endl;

result(1,1) = -5;
std::cout << result << std::endl;
}

I want to call this function from python using numpy.array as arguments. To this end, I use a wrapper function written in C ++

void pyMatrix(const double* p_data, const int dimData[],
                              double* p_result, const int dimResult[]);

which takes a pointer to the data, the size of the data array, a pointer to the result, and the size of the results array. The data pointer indicates a permanent memory patch, since the data should not be changed, while the memory patch reserved for the result is written. Function implementation

void pyMatrix(const double *p_data, const int dimData[], double *p_result, const int dimResult[])
{
Eigen::Map<const Eigen::MatrixXd> dataMap(p_data, dimData[0], dimData[1]);
Eigen::Map<Eigen::MatrixXd> resultMap(p_result, dimResult[0], dimResult[1]);

resultMap(0,0) = 100;

npMatrix(dataMap, resultMap);
}

Eigen:: Map . A Eigen:: Map Eigen:: Matrix.

<const Eigen::MatrixXd>

; resultMap

<Eigen::MatrixXd>

.

resultMap(0,0) = 100;

, resultMap . dataMap npMatrix(), const Eigen:: MatrixXd, resultMap . , , npMatrix const, - . , ,

Eigen::MatrixXd resultMatrix = resultMap;

resutlMatrix npMatrix(). , , , , Eigen:: Map. .

Eigen: Map , Eigen:: MatrixXd?

: npMatrix, Eigen:: Map, , .

, python pyMatrix()

import ctypes as ct
import numpy as np
import matplotlib.pyplot as plt

# Load libfit and define input types
ct.cdll.LoadLibrary("/home/wmader/Methods/fdmb-refactor/build/pyinterface/libpyfit.so")
libfit = ct.CDLL("libpyfit.so")

libfit.pyMatrix.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, ndim=2),
                                                     np.ctypeslib.ndpointer(dtype=np.int32, ndim=1),
                                                     np.ctypeslib.ndpointer(dtype=np.float64, ndim=2, flags='WRITEABLE'),
                                                     np.ctypeslib.ndpointer(dtype=np.int32, ndim=1)
                                                     ]

data = np.array(np.random.randn(10, 2), dtype=np.float64, order='F')
result = np.zeros_like(data, dtype=np.float64, order='F')

libfit.pyMatrix(data, np.array(data.shape, dtype=np.int32),
                              result, np.array(result.shape, dtype=np.int32))
+4
1

, Eigen:: Map . , template <typename Derived> .., http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html , , , API, . , , - , ( - ) .

, , :

Eigen::MatrixXd a; //lets assume a data pointer like double* DATA that we want to map //Now we do new (&a) Eigen::Map<Eigen::Matrix<Double,Eigen::Dynamic,Eigen::Dynamic>> (DATA,DATA rows,DATA cols);

, , . , , a Xd, . . a . , resize, !

! , ! , :: Map, , , .

+1

All Articles