10 #define MATRIX_UTILS_H
30 inline Eigen::MatrixXd reduced_standard_mul(Eigen::MatrixXd &red, Eigen::MatrixXd &std)
32 int rows_red = red.rows();
33 int non_null_per_row = rows_red*(red.cols());
34 int rows_std = std.rows();
35 int cols_std = std.cols();
36 Eigen::MatrixXd result(rows_red,cols_std);
37 #pragma omp parallel for collapse(2) shared(red,std,result,rows_red,non_null_per_row,rows_std,cols_std)
38 for (
int i = 0; i < rows_red; i++)
40 for (
int j = 0; j < cols_std; j++)
43 for (
int k = 0; k < rows_std; k++)
45 result(i,j) = red(i,k)*std(k*non_null_per_row,j);
53 inline Eigen::MatrixXd standard_reduced_mul(Eigen::MatrixXd &std, Eigen::MatrixXd &red)
55 int rows_std = std.rows();
56 int cols_std = std.cols();
57 int rows_red = red.rows();
58 int cols_red = red.cols();
59 int non_null_per_row = red.cols();
60 Eigen::MatrixXd result(rows_std,cols_red*rows_red);
61 result = move(Eigen::MatrixXd::Zero(rows_std,cols_red*rows_red));
62 for (
int i = 0; i < rows_std; i++)
64 for (
int j = 0; j < cols_std; j++)
67 for (
int k = 0; k < non_null_per_row; k++)
69 result(i,j*non_null_per_row+k) = std(i,j)*red(j,k);
77 inline Eigen::MatrixXd reduced_to_standard(Eigen::MatrixXd &a)
80 int rows_a = a.rows();
81 int cols_a = a.cols();
82 int a_non_null_per_row = cols_a;
83 int cols_res = rows_a*a_non_null_per_row;
84 Eigen::MatrixXd result;
85 result = move(Eigen::MatrixXd::Zero(rows_a,cols_res));
86 #pragma omp parallel for shared(a,result,rows_a,cols_a,a_non_null_per_row)
87 for (
int i = 0; i < rows_a; i++)
90 for (
int j = 0; j < cols_a; j++)
92 result(i,i*a_non_null_per_row+j) = a(i,j);
101 inline Eigen::MatrixXd standard_to_reduced(Eigen::MatrixXd &a)
103 int rows_a = a.rows();
104 int cols_a = a.cols();
105 int a_non_null_per_row = cols_a/rows_a;
106 int cols_res = a_non_null_per_row;
107 Eigen::MatrixXd result(rows_a,cols_res);
108 #pragma omp parallel for shared(a,result,rows_a,cols_a,a_non_null_per_row,cols_res)
109 for (
int i = 0; i < rows_a; i++)
112 for (
int j = 0; j < cols_res; j++)
114 result(i,j) = a(i,i*a_non_null_per_row+j);
127 inline double Frobenius_norm(Eigen::MatrixXd &mat)
129 const int rows = mat.rows();
130 const int cols = mat.cols();
132 #pragma omp parallel for collapse(2) shared(mat) reduction(+: result)
133 for (
int i = 0; i < rows; i++)
135 for (
int j = 0; j < cols; j++)
138 result += mat(i,j)*mat(i,j);
141 result = sqrt(result);
151 inline void write_eigen_matrixxf_to_file(
string filename,
char separating_char, Eigen::MatrixXd &mat)
155 const int nrows = mat.rows();
156 const int ncols = mat.cols();
157 for (
int i = 0; i < nrows; i++)
159 for (
int j = 0; j < ncols; j++)
162 file << separating_char;