conjugate_gradient.cpp revision d1c7cc7eb10f1bf5756d299e4a1e4f3060bfed8f
#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++)
}
}
/*
static double Linfty(valarray<double> const &vec) {
return std::max(vec.max(), -vec.min());
}
*/
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