Refactor eigenMatrix_to_vector function to handle both row-major and column-major matrices

This commit is contained in:
Max Lübke 2024-03-13 16:08:48 +01:00
parent 2f8ec213c8
commit 5be331b863

View File

@ -20,6 +20,21 @@ using TugType = double;
using RowMajorMat =
Eigen::Matrix<TugType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
static inline std::vector<TugType>
eigenMatrix_to_vector(const Eigen::MatrixX<TugType> &mat) {
if (mat.IsRowMajor) {
return std::vector<TugType>(mat.data(), mat.data() + mat.size());
} else {
std::vector<TugType> out_vec(mat.size());
for (int i = 0; i < mat.rows(); i++) {
for (int j = 0; j < mat.cols(); j++) {
out_vec[i * mat.cols() + j] = mat(i, j);
}
}
return out_vec;
}
}
double run_bench(const bench_input &input, const std::string &output_file) {
std::vector<std::vector<double>> raw_data =
read_conc_csv<double>(input.csv_file, input.ncols, input.nrows);
@ -77,8 +92,7 @@ double run_bench(const bench_input &input, const std::string &output_file) {
const auto &result = grid.getConcentrations();
raw_data[i] =
std::vector<TugType>(result.data(), result.data() + result.size());
raw_data[i] = eigenMatrix_to_vector(result);
}
const auto end_t = std::chrono::high_resolution_clock::now();