1

In Eigen C/C++ Library, how to converter the operation result (example below) from Eigen Matrix to C/C++ Array?

Example:

const Eigen::MatrixXf mat = Eigen::Map<Eigen::MatrixXf>( array_C_input , 3, 3);

const Eigen::MatrixSquareRootReturnValue<Eigen::MatrixXf> result = m.sqrt();

float* array_C_output = (float*) result;   // Error: convert sqrt output to C array
Bruno Gallego
  • 27
  • 1
  • 9
  • Do you want to get a `float*` pointer which you can pass to another C-style function, or do you have an existing C-style buffer where you want to store the result? – chtz Jan 09 '21 at 22:58
  • 1
    Does this answer your question? [Convert Eigen Matrix to C array](https://stackoverflow.com/questions/8443102/convert-eigen-matrix-to-c-array) – juicedatom Jan 09 '21 at 23:03
  • @juicedatom the member .data() is not available in the return of the function. It returns "MatrixSquareRootReturnType" an this type does not have "data()" member to access the result. – Bruno Gallego Jan 10 '21 at 01:00
  • @chtz I want the result of "sqrt" function as a C-style float array. I did not see any method/member in the result that provides it. – Bruno Gallego Jan 10 '21 at 01:01

1 Answers1

1

If you want to compute the matrix root of a matrix passed as C-style array and handle the result like a C-style array, you can either store the result into a MatrixXf and use the data() member of that matrix:

Eigen::MatrixXf matrix_root = Eigen::MatrixXf::Map( array_C_input , 3, 3).sqrt();
float* array_C_output = matrix_root.data();

Alternatively, if you already have memory allocated for the result, you can map the output to that:

void foo(float* output_array, float const* input_array) {
  Eigen::MatrixXf::Map( output_array , 3, 3) = 
       Eigen::MatrixXf::Map( input_array , 3, 3).sqrt();
}

Note that Matrix::sqrt computes the matrix-root, i.e., if S = A.sqrt(), then S*S == M. If you want an element-wise root, you need to use

Eigen::ArrayXXf::Map( input_array , 3, 3).sqrt()
chtz
  • 17,329
  • 4
  • 26
  • 56