conjugate_gradient.cpp revision c787248292420aeb9b05e2e4852df8291904cdee
#include <math.h>
#include <stdlib.h>
#include <valarray>
#include <cassert>
#include "conjugate_gradient.h"
/*
* Authors:
* Nathan Hurst <njh@njhurst.com>
* Tim Dwyer <tgdwyer@gmail.com>
*
* Copyright (C) 2006 Authors
*
* Released under GNU LGPL.
*/
/* lifted wholely from wikipedia. Well, apart from the bug in the wikipedia version. */
static void
{
for (unsigned i = 0; i < m; i++) {
double res = 0;
for (unsigned j = 0; j < n; j++)
}
}
}
double
double total = 0;
for(unsigned i = 0; i < x.size(); i++)
total += x[i]*y[i];
return total;// (x*y).sum(); <- this is more concise, but ineff
}
void
conjugate_gradient(double **A,
double *x,
double *b,
unsigned n,
double tol,
unsigned max_iterations) {
for(unsigned i=0;i<n;i++) {
vx[i]=x[i];
vb[i]=b[i];
for(unsigned j=0;j<n;j++) {
vA[i*n+j]=A[i][j];
}
}
for(unsigned i=0;i<n;i++) {
x[i]=vx[i];
}
}
void
unsigned n, double tol,
unsigned max_iterations) {
matrix_times_vector(A,x,Ap);
r=b-Ap;
unsigned k = 0;
k++;
if(k == 1)
p = r;
else {
}
matrix_times_vector(A, p, Ap);
x += alpha_k*p;
}
//printf("njh: %d iters, Linfty = %g L2 = %g\n", k,
//std::max(-r.min(), r.max()), sqrt(r_r));
// x is solution
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4