Menu

[r49]: / trunk / contrib / QMR / main.cpp  Maximize  Restore  History

Download this file

146 lines (125 with data), 3.8 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*****************************************************************************/
/* main.cpp */
/*****************************************************************************/
//=============================================================================
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "cpplapack.h"
//=============================================================================
bool qmr
(
const CPPL::dgematrix& A,
CPPL::dcovector& x,
const double& eps
)
{
CPPL::dcovector r(x), d(x.l), s(x.l);
CPPL::dcovector v, v2(r), w, w2(r), p(x.l), p2, q(x.l);
double xi(nrm2(w2)), eta(-1.0), epsilon(1.0), delta(0.0),
rho, rho_old(nrm2(v2)), gamma, gamma_old(1.0), theta, theta_old(0.0), beta;
x.zero();
p.zero();
q.zero();
d.zero();
s.zero();
int itc(0);
const int itmax(2*x.l);
do{
std::cout << "itc=" << itc << ", fabs(damax(r))=" << fabs(damax(r)) << std::endl;
v =v2/rho_old;
w =w2/xi;
delta =v%w;
p =v -(xi*delta/epsilon)*p;
q =w -(rho_old*delta/epsilon)*q;
p2 =A*p;
epsilon =q%p2;
beta =epsilon/delta;
v2 =p2-beta*v;
rho =nrm2(v2);
w2 =CPPL::t(t(q)*A)-beta*w;
xi =nrm2(w2);
theta =rho/(gamma_old*beta);
gamma =1./sqrt(1.+theta*theta);
eta = -eta*rho_old*gamma*gamma/(beta*gamma_old*gamma_old);
d =eta*p +pow(theta_old*gamma, 2)*d;
s =eta*p2 +pow(theta_old*gamma, 2)*s;
x+=d;
r-=s;
gamma_old =gamma;
rho_old =rho;
theta_old =theta;
itc++;
}while(fabs(damax(r))>eps && itc<itmax);
if(fabs(damax(r))<eps){ return 0; }
else{ return 1; }
}
//=============================================================================
bool bicg
(
const CPPL::dgematrix& A,
CPPL::dcovector x,
const double& eps
)
{
double alpha, beta(0.0);
const CPPL::dcovector y(x);
CPPL::dcovector p_0(x.l), p_1, q_0(x.l), q_1, Ap1, Aq1;
CPPL::dcovector r_1(y-A*x), r_2, s_1(r_1), s_2;
//p_0.zero(); q_0.zero();
int itc(0);
const int itmax(2*x.l);
while(fabs(damax(r_1))>eps && ++itc<itmax){
std::cout << itc << " " << fabs(damax(y-A*x)) << std::endl;
p_1 =r_1 +beta*p_0;
q_1 =s_1 +beta*q_0;
Ap1 =A*p_1;
Aq1 =t(t(q_1)*A);
alpha =(s_1%r_1)/(q_1%Ap1);
x +=alpha*p_1;
r_2 =r_1 -alpha*Ap1;
s_2 =s_1 -alpha*Aq1;
beta =(s_2%r_2)/(s_1%r_1);
swap(p_0, p_1); swap(q_0, q_1);
swap(r_1, r_2); swap(s_1, s_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);
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
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(size);
y=A*x;
y.write("y.dcovector");
double eps(fabs(damax(y))*1e-6);
//std::cerr << "eps=" << eps << std::endl;
if( qmr(A, y, eps) ){
std::cerr << "failed." << std::endl;
exit(1);
}
y.write("solution.dcovector");
std::cout << "solution=\n" << t(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.