bool EigenLisLinearSolver::solve(EigenMatrix &A_, EigenVector& b_, EigenVector &x_) { static_assert(EigenMatrix::RawMatrixType::IsRowMajor, "Sparse matrix is required to be in row major storage."); auto &A = A_.getRawMatrix(); auto &b = b_.getRawVector(); auto &x = x_.getRawVector(); if (!A.isCompressed()) A.makeCompressed(); int nnz = A.nonZeros(); int* ptr = A.outerIndexPtr(); int* col = A.innerIndexPtr(); double* data = A.valuePtr(); LisMatrix lisA(A_.getNumberOfRows(), nnz, ptr, col, data); LisVector lisb(b.rows(), b.data()); LisVector lisx(x.rows(), x.data()); LisLinearSolver lissol; // TODO not always creat Lis solver here lissol.setOption(_lis_option); lissol.solve(lisA, lisb, lisx); for (std::size_t i=0; i<lisx.size(); i++) x[i] = lisx[i]; return true; // TODO implement checks }
void addToMatrix(EigenMatrix& m, std::initializer_list<double> values) { auto const rows = m.getNumberOfRows(); auto const cols = m.getNumberOfColumns(); assert(static_cast<EigenMatrix::IndexType>(values.size()) == rows*cols); Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp(rows, cols); auto it = values.begin(); for (GlobalIndexType r=0; r<rows; ++r) { for (GlobalIndexType c=0; c<cols; ++c) { tmp(r, c) = *(it++); } } m.getRawMatrix() += tmp.sparseView(); }