void MeshPara::LSCM()
{
	is_Parameterized = true;
	int nb_vertices = mesh_.n_vertices();

	nlNewContext();
	nlSolverParameteri(NL_SOLVER, NL_CG);
	nlSolverParameteri(NL_PRECONDITIONER, NL_PRECOND_JACOBI);
	nlSolverParameteri(NL_NB_VARIABLES, 2 * nb_vertices);
	nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE);
	nlSolverParameteri(NL_MAX_ITERATIONS, 5 * nb_vertices);
	nlSolverParameterd(NL_THRESHOLD, 1e-10);
	
	nlBegin(NL_SYSTEM);
	init_slover();
	nlBegin(NL_MATRIX);
	setup_LSCM();
	nlEnd(NL_MATRIX);
	nlEnd(NL_SYSTEM);
	std::cout << "Solving ..." << std::endl;
	nlSolve();

	// Get results
	get_result();

	// Display time and iter_num
	double time;
	NLint iterations;
	nlGetDoublev(NL_ELAPSED_TIME, &time);
	nlGetIntergerv(NL_USED_ITERATIONS, &iterations);
	std::cout << "Solver time: " << time << std::endl;
	std::cout << "Used iterations: " << iterations << std::endl;

	nlDeleteContext(nlGetCurrent());	
}
void leastSquaresSystem::solve(double *vs)
{
 int i;
 Node *n;
 coeffIndexPair *f;

 nlNewContext();
 nlSolverParameteri(NL_SOLVER, NL_PERM_SUPERLU_EXT);
 nlSolverParameteri(NL_NB_VARIABLES, num_variables);
 nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE) ;
 nlSolverParameteri(NL_MAX_ITERATIONS, 1000) ;
 nlSolverParameterd(NL_THRESHOLD, 1e-10) ;

 nlBegin(NL_SYSTEM);

 for (i=0; i<num_variables; i++)
 {
  nlSetVariable(i, vs[i]);
  if (locks[i]) nlLockVariable(i);
 }

 nlBegin(NL_MATRIX);

 for (i=0; i<num_equations; i++)
 {
  nlRowParameterd(NL_RIGHT_HAND_SIDE, known_term[0][i]);
  nlBegin(NL_ROW);
  for (n=rows[i].cips.head(); n!=NULL; n=n->next())
  {
   f = (coeffIndexPair *)n->data;
   nlCoefficient(f->index, f->coeff);
  }  
  nlEnd(NL_ROW);
 }

 nlEnd(NL_MATRIX);
 nlEnd(NL_SYSTEM);
 nlSolve();

 for (i=0; i<num_variables; i++) vs[i] = nlGetVariable(i);

 nlDeleteContext(nlGetCurrent());
}
void sparse3System::solve(double *vs)
{
 int i, j;
 Node *n;
 coeffIndexPair *f;

 for (j=0; j<3; j++)
 {
  nlNewContext();
  nlSolverParameteri(NL_SOLVER, NL_PERM_SUPERLU_EXT);
  nlSolverParameteri(NL_NB_VARIABLES, num_variables);
  nlBegin(NL_SYSTEM);

  for (i=0; i<num_variables; i++)
  {
   nlSetVariable(i, vs[i*3 + j]);
   if (locks[i]) nlLockVariable(i);
  }

  nlBegin(NL_MATRIX);

  for (i=0; i<num_equations; i++)
  {
   nlRowParameterd(NL_RIGHT_HAND_SIDE, known_term[j][i]);
   nlBegin(NL_ROW);
   for (n=rows[i].cips.head(); n!=NULL; n=n->next())
   {
    f = (coeffIndexPair *)n->data;
    nlCoefficient(f->index, f->coeff);
   }  
   nlEnd(NL_ROW);
  }

  nlEnd(NL_MATRIX);
  nlEnd(NL_SYSTEM);
  nlSolve();

  for (i=0; i<num_variables; i++) vs[i*3 + j] = nlGetVariable(i);

  nlDeleteContext(nlGetCurrent());
 }
}
// Solves the system for j'th component of B
bool sparseSystem::solve(double *x, int j)
{
 int i;
 Node *n;
 coeffIndexPair *f;

 nlNewContext();
 nlSolverParameteri(NL_SOLVER, NL_PERM_SUPERLU_EXT);
 nlSolverParameteri(NL_NB_VARIABLES, num_variables);
 nlBegin(NL_SYSTEM);

 for (i=0; i<num_variables; i++) nlSetVariable(i, x[i]);

 nlBegin(NL_MATRIX);

 for (i=0; i<num_equations; i++)
 {
  nlRowParameterd(NL_RIGHT_HAND_SIDE, known_term[j][i]);
  nlBegin(NL_ROW);
  for (n=rows[i].cips.head(); n!=NULL; n=n->next())
  {
   f = (coeffIndexPair *)n->data;
   nlCoefficient(f->index, f->coeff);
  }  
  nlEnd(NL_ROW);
 }

 nlEnd(NL_MATRIX);
 nlEnd(NL_SYSTEM);
 bool success = (bool)nlSolve();

 if (success) for (i=0; i<num_variables; i++) x[i] = nlGetVariable(i);

 nlDeleteContext(nlGetCurrent());

 return success;
}