/* * CG 3x3 case with positive definite assumption based on Sylvester's criteria. * From wiki: * 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 3 $input double diag1,diag2,diag3,off1,off2,off3; $input double b[n]; double x[n]; double xcg[n]; void cg(double A[n][n], double b[n], double x[n], int steps) { double r[n]; double p[n]; double temp[n]; double tempp[n]; double rsold; double rsnew; double rsfrac; double alpha; // x = 0 for(int i=0; i rsold = 0.0; for(int i=0; i rsnew = 0.0; for(int i=0; i 0); //assumption of Sylvester's criterion $assume((diag1*diag2 - off1*off1) > 0);//all principal minors are positive. $assume((diag1*diag2*diag3 + 2*off1*off2*off3 - diag1*off3*off3 - diag2*off2*off2 - diag3*off1*off1) > 0); cg(A,b,xcg,n); printf("\n================Solution x:================\n"); for(int i=0; i