MCPcopy Create free account
hub / github.com/BIMCoderLiang/LNLib / SolveLinearSystemBanded

Method SolveLinearSystemBanded

src/LNLib/Mathematics/Utils/MathUtils.cpp:290–343  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

288 return result;
289}
290bool LNLib::MathUtils::SolveLinearSystemBanded(int matrixDimension, const std::vector<std::vector<double>>& matrix, int bandwidth, const std::vector<std::vector<double>>& right, std::vector<std::vector<double>>& result)
291{
292 int sbw = bandwidth / 2;
293 int n = matrixDimension;
294
295 Eigen::SparseMatrix<double> A(n, n);
296 std::vector<Eigen::Triplet<double>> triplets;
297 triplets.reserve(n * bandwidth);
298
299 for (int i = 0; i < n; i++)
300 {
301 for (int k = 0; k < bandwidth; k++)
302 {
303 int j = i - sbw + k;
304 if (j >= 0 && j < n)
305 {
306 double value = matrix[i][k];
307 if (value != 0.0)
308 {
309 triplets.push_back(Eigen::Triplet<double>(i, j, value));
310 }
311 }
312 }
313 }
314 A.setFromTriplets(triplets.begin(), triplets.end());
315
316 Eigen::SparseLU<Eigen::SparseMatrix<double>> solver;
317 solver.analyzePattern(A);
318 solver.factorize(A);
319
320 if (solver.info() != Eigen::Success) {
321 return false;
322 }
323
324 for (int k = 0; k < right[0].size(); k++)
325 {
326 Eigen::VectorXd rhs(n), solution(n);
327 for (int i = 0; i < n; i++)
328 {
329 rhs(i) = right[i][k];
330 }
331 solution = solver.solve(rhs);
332 if (solver.info() != Eigen::Success)
333 {
334 return false;
335 break;
336 }
337 for (int i = 0; i < n; i++)
338 {
339 result[i][k] = solution(i);
340 }
341 }
342 return true;
343}
344
345

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected