13#include <Eigen/Sparse>
45 memset(value, 0,
sizeof(value));
58 LinearSolver(
int num_rows_,
int num_variables_,
int num_rhs_,
bool lsq_)
60 assert(num_variables_ > 0);
61 assert(num_rhs_ <= 4);
88 std::vector<EigenVectorX>
b;
89 std::vector<EigenVectorX>
x;
104 return new LinearSolver(num_rows, num_columns, num_rhs,
false);
109 return new LinearSolver(num_rows, num_columns, num_rhs,
true);
131 if (!solver->
variable[index].locked) {
133 solver->
variable[index].locked =
true;
139 if (solver->
variable[index].locked) {
141 solver->
variable[index].locked =
false;
152 for (
int j = 0; j < num_rhs; j++) {
153 solver->
x[j][
v->index] =
v->value[j];
166 for (
int j = 0; j < num_rhs; j++) {
167 v->value[j] = solver->
x[j][
v->index];
199 solver->
Mtriplets.reserve(std::max(m, n) * 3);
204 for (
int i = 0; i < solver->
num_rhs; i++) {
205 solver->
b[i].setZero(m);
206 solver->
x[i].setZero(n);
255 solver->
b[
rhs][index] += value;
257 else if (!solver->
variable[index].locked) {
258 index = solver->
variable[index].index;
259 solver->
b[
rhs][index] += value;
268 if (solver->
m == 0 || solver->
n == 0) {
278 solver->
M.resize(solver->
m, solver->
n);
284 solver->
MtM = solver->
M.transpose() * solver->
M;
295 sparseLU->compute(
M);
296 result = (sparseLU->info() == Eigen::Success);
310 if (variable->locked) {
311 std::vector<LinearSolver::Coeff> &a = variable->a;
313 for (
int j = 0; j < a.size(); j++) {
314 b[a[j].index] -= a[j].value * variable->value[
rhs];
329 if (solver->
sparseLU->info() != Eigen::Success) {
341 solver->
b[
rhs].setZero(solver->
m);
351 std::cout <<
"A:" << solver->
M << std::endl;
354 std::cout <<
"b " <<
rhs <<
":" << solver->
b[
rhs] << std::endl;
357 if (solver->
MtM.rows() && solver->
MtM.cols()) {
358 std::cout <<
"AtA:" << solver->
MtM << std::endl;
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
local_group_size(16, 16) .push_constant(Type rhs
LinearSolver * EIG_linear_solver_new(int num_rows, int num_columns, int num_rhs)
void EIG_linear_solver_print_matrix(LinearSolver *solver)
LinearSolver * EIG_linear_least_squares_solver_new(int num_rows, int num_columns, int num_rhs)
Eigen::VectorXd EigenVectorX
void EIG_linear_solver_variable_set(LinearSolver *solver, int rhs, int index, double value)
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
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