27 #include <Eigen/Sparse>
72 LinearSolver(
int num_rows_,
int num_variables_,
int num_rhs_,
bool lsq_)
74 assert(num_variables_ > 0);
75 assert(num_rhs_ <= 4);
102 std::vector<EigenVectorX>
b;
103 std::vector<EigenVectorX>
x;
118 return new LinearSolver(num_rows, num_columns, num_rhs,
false);
123 return new LinearSolver(num_rows, num_columns, num_rhs,
true);
135 solver->
variable[index].value[rhs] = value;
140 return solver->
variable[index].value[rhs];
145 if (!solver->
variable[index].locked) {
147 solver->
variable[index].locked =
true;
153 if (solver->
variable[index].locked) {
155 solver->
variable[index].locked =
false;
166 for (
int j = 0; j < num_rhs; j++)
167 solver->
x[j][
v->index] =
v->value[j];
179 for (
int j = 0; j < num_rhs; j++)
180 v->value[j] = solver->
x[j][
v->index];
214 for (
int i = 0; i < solver->
num_rhs; i++) {
215 solver->
b[i].setZero(m);
216 solver->
x[i].setZero(n);
261 solver->
b[rhs][index] += value;
263 else if (!solver->
variable[index].locked) {
264 index = solver->
variable[index].index;
265 solver->
b[rhs][index] += value;
274 if (solver->
m == 0 || solver->
n == 0)
283 solver->
M.resize(solver->
m, solver->
n);
289 solver->
MtM = solver->
M.transpose() * solver->
M;
299 sparseLU->compute(
M);
300 result = (sparseLU->info() == Eigen::Success);
307 for (
int rhs = 0; rhs < solver->
num_rhs; rhs++) {
315 std::vector<LinearSolver::Coeff> &
a = variable->
a;
317 for (
int j = 0; j <
a.size(); j++)
318 b[
a[j].index] -=
a[j].value * variable->
value[rhs];
325 solver->
x[rhs] = solver->
sparseLU->solve(Mtb);
329 solver->
x[rhs] = solver->
sparseLU->solve(b);
332 if (solver->
sparseLU->info() != Eigen::Success)
341 for (
int rhs = 0; rhs < solver->
num_rhs; rhs++)
342 solver->
b[rhs].setZero(solver->
m);
351 std::cout <<
"A:" << solver->
M << std::endl;
353 for (
int rhs = 0; rhs < solver->
num_rhs; rhs++)
354 std::cout <<
"b " << rhs <<
":" << solver->
b[rhs] << std::endl;
356 if (solver->
MtM.rows() && solver->
MtM.cols())
357 std::cout <<
"AtA:" << solver->
MtM << std::endl;
ATTR_WARN_UNUSED_RESULT const BMVert * v
void EIG_linear_solver_print_matrix(LinearSolver *solver)
Eigen::VectorXd EigenVectorX
void EIG_linear_solver_variable_set(LinearSolver *solver, int rhs, int index, double value)
LinearSolver * EIG_linear_solver_new(int num_rows, int num_columns, int num_rhs)
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)
Eigen::SparseMatrix< double, Eigen::ColMajor > EigenSparseMatrix
Eigen::SparseLU< EigenSparseMatrix > EigenSparseLU
LinearSolver * EIG_linear_least_squares_solver_new(int num_rows, int num_columns, int num_rhs)
void EIG_linear_solver_delete(LinearSolver *solver)
Eigen::Triplet< double > EigenTriplet
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)
struct LinearSolver LinearSolver
@ 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