SparseVector<double> SparseSolverEigenCustom::cgSolveSparse(const SparseMatrix<double> & A,const SparseVector<double> & b,int iter, double residual)
{
	SparseVector<double> r(b.rows());
	SparseVector<double> p(b.rows());
	SparseVector<double> Ap(b.rows());
	SparseVector<double> x(b.rows());

	r = b - A *x;
	p = r;

	double rTr,pTAp,alpha,beta,rTrnew,rnorm;
	SparseVector<double> vtemp;
	bool isConverged = false;
	for(int k=0;k<iter;k++)
	{
		Ap = A*p;
		vtemp = r.transpose()*r;
		rTr = vtemp.coeff(0);

		vtemp = p.transpose()*Ap;
		pTAp = vtemp.coeff(0);
		alpha = rTr/pTAp;

		x = x + (alpha * p);
		r = r - (alpha * Ap);
		rnorm = r.norm();
		if(rnorm<residual)
		{
			isConverged = true;
			break;
		}

		vtemp = r.transpose()*r;
		rTrnew = vtemp.coeff(0);

		beta = rTrnew / rTr;
		p = r + (beta * p);
	}

	return x;
}