13#include <Eigen/Sparse>
58 LinearSolver(
int num_rows_,
int num_variables_,
int num_rhs_,
bool lsq_)
60 assert(num_variables_ > 0);
88 std::vector<EigenVectorX>
b;
89 std::vector<EigenVectorX>
x;
104 return new LinearSolver(num_rows, num_columns, num_right_hand_sides,
false);
109 int num_right_hand_sides)
111 return new LinearSolver(num_rows, num_columns, num_right_hand_sides,
true);
133 if (!solver->
variable[index].locked) {
135 solver->
variable[index].locked =
true;
141 if (solver->
variable[index].locked) {
143 solver->
variable[index].locked =
false;
154 for (
int j = 0; j < num_rhs; j++) {
155 solver->
x[j][
v->index] =
v->value[j];
168 for (
int j = 0; j < num_rhs; j++) {
169 v->value[j] = solver->
x[j][
v->index];
201 solver->
Mtriplets.reserve(std::max(m, n) * 3);
207 solver->
b[
i].setZero(m);
208 solver->
x[
i].setZero(n);
257 solver->
b[
rhs][index] += value;
259 else if (!solver->
variable[index].locked) {
260 index = solver->
variable[index].index;
261 solver->
b[
rhs][index] += value;
270 if (solver->
m == 0 || solver->
n == 0) {
280 solver->
M.resize(solver->
m, solver->
n);
286 solver->
MtM = solver->
M.transpose() * solver->
M;
297 sparseLU->compute(
M);
298 result = (sparseLU->info() == Eigen::Success);
313 std::vector<LinearSolver::Coeff> &a = variable->
a;
315 for (
int j = 0; j < a.size(); j++) {
316 b[a[j].index] -= a[j].value * variable->
value[
rhs];
331 if (solver->
sparseLU->info() != Eigen::Success) {
343 solver->
b[
rhs].setZero(solver->
m);
353 std::cout <<
"A:" << solver->
M << std::endl;
356 std::cout <<
"b " <<
rhs <<
":" << solver->
b[
rhs] << std::endl;
359 if (solver->
MtM.rows() && solver->
MtM.cols()) {
360 std::cout <<
"AtA:" << solver->
MtM << std::endl;
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define assert(assertion)
Eigen::SparseMatrix< double, Eigen::ColMajor > EigenSparseMatrix
void EIG_linear_solver_print_matrix(LinearSolver *solver)
LinearSolver * EIG_linear_solver_new(int num_rows, int num_columns, int num_right_hand_sides)
void EIG_linear_solver_variable_set(LinearSolver *solver, int rhs, int index, double value)
Eigen::SparseLU< EigenSparseMatrix > EigenSparseLU
void EIG_linear_solver_right_hand_side_add(LinearSolver *solver, int rhs, int index, double value)
void EIG_linear_solver_variable_unlock(LinearSolver *solver, int index)
LinearSolver * EIG_linear_least_squares_solver_new(int num_rows, int num_columns, int num_right_hand_sides)
void EIG_linear_solver_delete(LinearSolver *solver)
Eigen::Triplet< double > EigenTriplet
Eigen::VectorXd EigenVectorX
static void linear_solver_variables_to_vector(LinearSolver *solver)
double EIG_linear_solver_variable_get(LinearSolver *solver, int rhs, int index)
static void linear_solver_vector_to_variables(LinearSolver *solver)
void EIG_linear_solver_matrix_add(LinearSolver *solver, int row, int col, double value)
bool EIG_linear_solver_solve(LinearSolver *solver)
static void linear_solver_ensure_matrix_construct(LinearSolver *solver)
void EIG_linear_solver_variable_lock(LinearSolver *solver, int index)
@ STATE_VARIABLES_CONSTRUCT
std::vector< Variable > variable
LinearSolver(int num_rows_, int num_variables_, int num_rhs_, bool lsq_)
std::vector< EigenVectorX > b
std::vector< EigenVectorX > x
std::vector< EigenTriplet > Mtriplets