Menu

[r136]: / trunk / contrib / BiCG / main.cpp  Maximize  Restore  History

Download this file

100 lines (86 with data), 2.7 kB

 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*****************************************************************************/
/* main.cpp */
/*****************************************************************************/
//=============================================================================
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "cpplapack.h"
//=============================================================================
bool bicg
(
const CPPL::dgematrix& A,
//const CPPL::dgsmatrix& A,
CPPL::dcovector& x,
const double& eps
)
{
double alpha, beta(0.0);
CPPL::dcovector p_0(x.l), p_1, P_0(x.l), P_1, q, Q;
CPPL::dcovector r_1(x), r_2, R_1(r_1), R_2;
double rho_0(r_1%R_1), rho_1;
x.zero();
p_0.zero();
P_0.zero();
int itc(0);
const int itmax(2*x.l);
while(fabs(damax(r_1))>eps && ++itc<itmax){
std::cout << itc << " " << fabs(damax(r_1)) << std::endl;
rho_1 =r_1%R_1;
beta =rho_1/rho_0;
p_1 =r_1 +beta*p_0;
P_1 =R_1 +beta*P_0;
q =A*p_1;
Q =t(t(P_1)*A);
alpha =rho_1/(P_1%q);
x +=alpha*p_1;
r_2 =r_1 -alpha*q;
R_2 =R_1 -alpha*Q;
rho_0 =rho_1;
swap(p_0, p_1);
swap(P_0, P_1);
swap(r_1, r_2);
swap(R_1, R_2);
}
std::cerr << "itc=" << itc << " fabs(damax(r_1))=" << fabs(damax(r_1)) << std::endl;
if(itc<itmax){ return 0; }
else{ return 1; }
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
int main(int argc, char** argv)
{
srand(time(NULL));
const int size(100);
CPPL::dgematrix A(size,size);
//CPPL::dgsmatrix A(size,size);
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(rand()%2){ A(i,j) =(double(rand())/double(RAND_MAX))*2.0 -1.0; }
}
A(i,i)+=10.;
}
A.write("A.dgematrix");
CPPL::dcovector x(size);
for(int i=0; i<size; i++){
x(i) =(double(rand())/double(RAND_MAX))*1. -0.5;
}
x.write("answer.dcovector");//solution
std::cerr << "answer=\n" << t(x) << std::endl;
CPPL::dcovector y(A*x);
y.write("y.dcovector");
//std::cerr << "y=\n" << t(y) << std::endl;
double eps(fabs(damax(y))*1e-6);
//std::cerr << "eps=" << eps << std::endl;
if( bicg(A, y, eps) ){
std::cerr << "failed." << std::endl;
exit(1);
}
y.write("solution.dcovector");
std::cout << "solution=\n" << t(y) << std::endl;
//std::cerr << "A*x=\n" << t(A*y) << std::endl;
return 0;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.