1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#ifndef RELAXATION_H
#define RELAXATION_H
#include <assert.h>
#include <iostream>
template<class I, class T>
void gauss_seidel(const I n_row,
const I Ap[],
const I Aj[],
const T Ax[],
T x[],
const T b[],
const I row_start,
const I row_stop,
const I row_step)
{
for(I i = row_start; i != row_stop; i += row_step) {
I start = Ap[i];
I end = Ap[i+1];
T rsum = 0;
T diag = 0;
for(I jj = start; jj < end; jj++){
I j = Aj[jj];
if (i == j)
diag = Ax[jj];
else
rsum += Ax[jj]*x[j];
}
assert(diag != 0);
x[i] = (b[i] - rsum)/diag;
}
}
template<class I, class T>
void jacobi(const I n_row,
const I Ap[],
const I Aj[],
const T Ax[],
T x[],
const T b[],
T temp[],
const I row_start,
const I row_stop,
const I row_step,
const T omega)
{
std::copy(x,x+n_row,temp);
for(I i = row_start; i != row_stop; i += row_step) {
I start = Ap[i];
I end = Ap[i+1];
T rsum = 0;
T diag = 0;
for(I jj = start; jj < end; jj++){
I j = Aj[jj];
if (i == j)
diag = Ax[jj];
else
rsum += Ax[jj]*temp[j];
}
assert(diag != 0);
x[i] = (1 - omega) * temp[i] + omega * ((b[i] - rsum)/diag);
}
}
#endif
|