/* * CG 2x2 case with positive definite assumption * based on Sylvester's criteria. * From wiki: https://en.wikipedia.org/wiki/Sylvester%27s_criterion * Sylvester's criterion states that a Hermitian matrix M * is positive-definite if and only if all the following * matrices have a positive determinant: * the upper left 1-by-1 corner of M, * the upper left 2-by-2 corner of M, * the upper left 3-by-3 corner of M, * M itself. * In other words, all of the leading principal minors must be positive. */ #include #include #define n 2 $input double a1,a2,a3; $input double b[n]; double x[n]; double xcg[n]; void cg(double A[n][n], double b[n], double x[n]) { double r[n]; //residual double p[n]; //search direction double temp[n]; double temp2[n]; double rsold; // double rsnew; double alpha; double beta; for (int i=0; i rsnew = 0.0; for (int i=0; i 0); //assumption of Sylvester's criterion $assume( (a1*a3 - a2*a2) > 0);//all principal minors are positive. cg(A,b,xcg); for(int i=0; i