25static double *
diag_precon(
const double *diag,
double *x,
double *y) {
29 for (i = 0; i < m; i++) y[i] = x[i]*diag[i];
34 int j, *ia =
A->ia, *ja =
A->ja;
35 const size_t m =
A->m;
42 double *data =
gv_calloc(
A->m + 1,
sizeof(
double));
47 for (
size_t i = 0; i < m; i++){
49 for (j = ia[i]; j < ia[i+1]; j++){
50 if ((
int)i == ja[j] && fabs(a[j]) > 0) diag[i] = 1./a[j];
58 double *x,
double *rhs,
double tol,
61 double rho, rho_old = 1, res0, beta;
76 "on entry, cg iter = %d of %.0f, residual = %g, tol = %g\n",
81 while ((iter++) <
maxit && res >
tol*res0){
89 memcpy(p,
z,
sizeof(
double)*n);
105 _statistics[0] += iter - 1;
110 fprintf(stderr,
" cg iter = %d, residual = %g, relative res = %g\n", iter, res, res/res0);
117 double *x0,
double *rhs,
double tol,
double maxit) {
120 double *x =
gv_calloc(n,
sizeof(
double));
121 double *b =
gv_calloc(n,
sizeof(
double));
122 for (k = 0; k <
dim; k++){
123 for (
size_t i = 0; i < n; i++) {
124 x[i] = x0[(int)i *
dim + k];
125 b[i] = rhs[(int)i *
dim + k];
129 for (
size_t i = 0; i < n; i++) {
130 rhs[(int)i *
dim + k] = x[i];
140 const size_t n =
A->m;
void SparseMatrix_multiply_vector(SparseMatrix A, double *v, double **res)
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
double * vector_saxpy(size_t n, double *x, double *y, double beta)
y = x+beta*y
double vector_product(int n, double *x, double *y)
double * vector_subtract_to(size_t n, double *x, double *y)
y = x-y
double * vector_saxpy2(size_t n, double *x, double *y, double beta)
x = x+beta*y
static double conjugate_gradient(SparseMatrix A, const double *precon, size_t n, double *x, double *rhs, double tol, double maxit)
static double cg(SparseMatrix A, const double *precond, size_t n, int dim, double *x0, double *rhs, double tol, double maxit)
static double * diag_precon_new(SparseMatrix A)
double SparseMatrix_solve(SparseMatrix A, int dim, double *x0, double *rhs, double tol, double maxit)
static double * diag_precon(const double *diag, double *x, double *y)
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t