/* * Simple implementation of Conjugate Gradient algorithm for 3x3 * symmetric matrix. Instead of assuming positive-definite-ness, * we assume that in every division, the denominator is non-0. * Based on https://en.wikipedia.org/wiki/Conjugate_gradient_method */ #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