PolyFEM
Loading...
Searching...
No Matches
MatrixUtils.cpp
Go to the documentation of this file.
1#include "MatrixUtils.hpp"
2
5
6#include <vector>
7
8void polyfem::utils::show_matrix_stats(const Eigen::MatrixXd &M)
9{
10 Eigen::FullPivLU<Eigen::MatrixXd> lu(M);
11 Eigen::JacobiSVD<Eigen::MatrixXd> svd(M);
12 double s1 = svd.singularValues()(0);
13 double s2 = svd.singularValues()(svd.singularValues().size() - 1);
14 double cond = s1 / s2;
15
16 logger().trace("----------------------------------------");
17 logger().trace("-- Determinant: {}", M.determinant());
18 logger().trace("-- Singular values: {} {}", s1, s2);
19 logger().trace("-- Cond: {}", cond);
20 logger().trace("-- Invertible: {}", lu.isInvertible());
21 logger().trace("----------------------------------------");
22 // logger().trace("{}", lu.solve(M) );
23}
24
25namespace
26{
27 inline bool nanproof_equals(const double x, const double y)
28 {
29 return x == y || (std::isnan(x) == std::isnan(y));
30 }
31} // namespace
32
33// Flatten rowwises
34Eigen::VectorXd polyfem::utils::flatten(const Eigen::MatrixXd &X)
35{
36 if (X.size() == 0)
37 return Eigen::VectorXd();
38
39 Eigen::VectorXd x(X.size());
40 for (int i = 0; i < X.rows(); ++i)
41 {
42 for (int j = 0; j < X.cols(); ++j)
43 {
44 x(i * X.cols() + j) = X(i, j);
45 }
46 }
47 assert(nanproof_equals(X(0, 0), x(0)));
48 assert(X.cols() <= 1 || nanproof_equals(X(0, 1), x(1)));
49 return x;
50}
51
52// Unflatten rowwises, so every dim elements in x become a row.
53Eigen::MatrixXd polyfem::utils::unflatten(const Eigen::VectorXd &x, int dim)
54{
55 if (x.size() == 0)
56 return Eigen::MatrixXd(0, dim);
57
58 assert(x.size() % dim == 0);
59 Eigen::MatrixXd X(x.size() / dim, dim);
60 for (int i = 0; i < x.size(); ++i)
61 {
62 X(i / dim, i % dim) = x(i);
63 }
64 assert(nanproof_equals(X(0, 0), x(0)));
65 assert(X.cols() <= 1 || nanproof_equals(X(0, 1), x(1)));
66 return X;
67}
68
69void polyfem::utils::vector2matrix(const Eigen::VectorXd &vec, Eigen::MatrixXd &mat)
70{
71 int size = 1;
72 if (vec.size() == 9)
73 size = 3;
74 else if (vec.size() == 4)
75 size = 2;
76 else
77 throw std::runtime_error("Invalid size in vector2matrix!");
78
79 assert(size * size == vec.size());
80
81 mat = unflatten(vec, size);
82}
83
84Eigen::SparseMatrix<double> polyfem::utils::lump_matrix(const Eigen::SparseMatrix<double> &M)
85{
86 std::vector<Eigen::Triplet<double>> triplets;
87
88 for (int k = 0; k < M.outerSize(); ++k)
89 {
90 for (Eigen::SparseMatrix<double>::InnerIterator it(M, k); it; ++it)
91 {
92 triplets.emplace_back(it.row(), it.row(), it.value());
93 }
94 }
95
96 Eigen::SparseMatrix<double> lumped(M.rows(), M.rows());
97 lumped.setFromTriplets(triplets.begin(), triplets.end());
98 lumped.makeCompressed();
99
100 return lumped;
101}
102
104 const int full_size,
105 const int reduced_size,
106 const std::vector<int> &removed_vars,
107 const StiffnessMatrix &full,
108 StiffnessMatrix &reduced)
109{
110 POLYFEM_SCOPED_TIMER("full to reduced matrix");
111
112 if (reduced_size == full_size || reduced_size == full.rows())
113 {
114 assert(reduced_size == full.rows() && reduced_size == full.cols());
115 reduced = full;
116 return;
117 }
118 assert(full.rows() == full_size && full.cols() == full_size);
119
120 Eigen::VectorXi indices(full_size);
121 int index = 0;
122 size_t kk = 0;
123 for (int i = 0; i < full_size; ++i)
124 {
125 if (kk < removed_vars.size() && removed_vars[kk] == i)
126 {
127 ++kk;
128 indices(i) = -1;
129 }
130 else
131 {
132 indices(i) = index++;
133 }
134 }
135 assert(index == reduced_size);
136
137 std::vector<Eigen::Triplet<double>> entries;
138 entries.reserve(full.nonZeros()); // Conservative estimate
139 for (int k = 0; k < full.outerSize(); ++k)
140 {
141 if (indices(k) < 0)
142 continue;
143
144 for (StiffnessMatrix::InnerIterator it(full, k); it; ++it)
145 {
146 assert(it.col() == k);
147 if (indices(it.row()) < 0 || indices(it.col()) < 0)
148 continue;
149
150 assert(indices(it.row()) >= 0);
151 assert(indices(it.col()) >= 0);
152
153 entries.emplace_back(indices(it.row()), indices(it.col()), it.value());
154 }
155 }
156
157 reduced.resize(reduced_size, reduced_size);
158 reduced.setFromTriplets(entries.begin(), entries.end());
159 reduced.makeCompressed();
160}
161
163 const Eigen::MatrixXd &in,
164 const Eigen::VectorXi &in_to_out,
165 int out_blocks,
166 const int block_size)
167{
168 constexpr double NaN = std::numeric_limits<double>::quiet_NaN();
169
170 assert(in.rows() % block_size == 0);
171 assert(in_to_out.size() == in.rows() / block_size);
172
173 if (out_blocks < 0)
174 out_blocks = in.rows() / block_size;
175
176 Eigen::MatrixXd out = Eigen::MatrixXd::Constant(
177 out_blocks * block_size, in.cols(), NaN);
178
179 const int in_blocks = in.rows() / block_size;
180 for (int i = 0; i < in_blocks; ++i)
181 {
182 const int j = in_to_out[i];
183 if (j < 0)
184 continue;
185
186 out.middleRows(block_size * j, block_size) =
187 in.middleRows(block_size * i, block_size);
188 }
189
190 return out;
191}
192
194 const Eigen::MatrixXd &out,
195 const Eigen::VectorXi &in_to_out,
196 int in_blocks,
197 const int block_size)
198{
199 constexpr double NaN = std::numeric_limits<double>::quiet_NaN();
200
201 assert(out.rows() % block_size == 0);
202
203 if (in_blocks < 0)
204 in_blocks = out.rows() / block_size;
205 assert(in_to_out.size() == in_blocks);
206
207 Eigen::MatrixXd in = Eigen::MatrixXd::Constant(
208 in_blocks * block_size, out.cols(), NaN);
209
210 for (int i = 0; i < in_blocks; i++)
211 {
212 const int j = in_to_out[i];
213 if (j < 0)
214 continue;
215
216 in.middleRows(block_size * i, block_size) =
217 out.middleRows(block_size * j, block_size);
218 }
219
220 return in;
221}
222
224 const Eigen::MatrixXi &in,
225 const Eigen::VectorXi &index_mapping)
226{
227 Eigen::MatrixXi out(in.rows(), in.cols());
228 for (int i = 0; i < in.rows(); i++)
229 {
230 for (int j = 0; j < in.cols(); j++)
231 {
232 out(i, j) = index_mapping[in(i, j)];
233 }
234 }
235 return out;
236}
Eigen::MatrixXd vec
Definition Assembler.cpp:72
std::vector< Eigen::Triplet< double > > entries
int y
int x
#define POLYFEM_SCOPED_TIMER(...)
Definition Timer.hpp:10
Eigen::MatrixXd mat
Eigen::SparseMatrix< double > lump_matrix(const Eigen::SparseMatrix< double > &M)
Lump each row of a matrix into the diagonal.
void show_matrix_stats(const Eigen::MatrixXd &M)
Eigen::MatrixXd reorder_matrix(const Eigen::MatrixXd &in, const Eigen::VectorXi &in_to_out, int out_blocks=-1, const int block_size=1)
Reorder row blocks in a matrix.
void vector2matrix(const Eigen::VectorXd &vec, Eigen::MatrixXd &mat)
Eigen::MatrixXd unreorder_matrix(const Eigen::MatrixXd &out, const Eigen::VectorXi &in_to_out, int in_blocks=-1, const int block_size=1)
Undo the reordering of row blocks in a matrix.
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
Eigen::VectorXd flatten(const Eigen::MatrixXd &X)
Flatten rowwises.
Eigen::MatrixXi map_index_matrix(const Eigen::MatrixXi &in, const Eigen::VectorXi &index_mapping)
Map the entrys of an index matrix to new indices.
void full_to_reduced_matrix(const int full_size, const int reduced_size, const std::vector< int > &removed_vars, const StiffnessMatrix &full, StiffnessMatrix &reduced)
Map a full size matrix to a reduced one by dropping rows and columns.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:20