File: chebyshevAccelerator.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (212 lines) | stat: -rw-r--r-- 6,944 bytes parent folder | download | duplicates (10)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// $Id: chebyshevAccelerator.cpp 962 2006-11-07 15:13:34Z privmane $

#include "chebyshevAccelerator.h"
#include <cmath>
#include <cassert>

chebyshevAccelerator::chebyshevAccelerator(const chebyshevAccelerator& other):
	_alphabetSize(other._alphabetSize),
	_totalNumOfCoef(other._totalNumOfCoef),
	_usingNumberOfCoef(other._usingNumberOfCoef),
	_pb(NULL),
	_rightRange(other._rightRange),
	_leftRange(other._leftRange){
		if (other._pb != NULL) _pb = other._pb->clone();
		chebi_coff=other.chebi_coff;
		chebi_dervation_coff=other.chebi_dervation_coff;
		chebi_sec_dervation_coff=other.chebi_sec_dervation_coff;
}

chebyshevAccelerator::chebyshevAccelerator(
	 replacementModel* pb,
	const int alphanetSize,
	const int totalNumOfCoef,
	const int usingNumberOfCoef,
	const MDOUBLE rightRange,
	const MDOUBLE leftRange
	): _alphabetSize(alphanetSize),
	_totalNumOfCoef(totalNumOfCoef), _usingNumberOfCoef(usingNumberOfCoef),_pb(pb->clone()), _rightRange(rightRange), _leftRange(leftRange)
//----------------------------------------------------------------------------------
//input:	non
//output:	non
//doing:	filling the member chebi_coff[][][]; chebi_coff[1][2][4] is the forth 
//			chebichev coefficient in the chebichev polynom of the function 
//			slow_pij(1,2,t);			
//----------------------------------------------------------------------------------
{
	int tmp, tmp1;
	for (tmp  = 0; tmp < _alphabetSize ; tmp ++) {
		
		chebi_coff.resize(_alphabetSize);
		chebi_dervation_coff.resize(_alphabetSize);
		chebi_sec_dervation_coff.resize(_alphabetSize);

		for (tmp1  = 0; tmp1 < _alphabetSize ; tmp1 ++) {
			chebi_coff[tmp].resize(_alphabetSize);
			chebi_dervation_coff[tmp].resize(_alphabetSize);
			chebi_sec_dervation_coff[tmp].resize(_alphabetSize); 
			for (tmp1  = 0; tmp1 < _alphabetSize ; tmp1 ++) {
				chebi_coff[tmp][tmp1].resize(_totalNumOfCoef);
				chebi_dervation_coff[tmp][tmp1].resize(_totalNumOfCoef);
				chebi_sec_dervation_coff[tmp][tmp1].resize(_totalNumOfCoef); 
			}
		}
	}


	Vdouble coffij(_totalNumOfCoef);
	Vdouble coffij_of_derviation(_totalNumOfCoef);
	Vdouble coffij_of_second_derivation(_totalNumOfCoef);

	
	for (int from_aa =0; from_aa<_alphabetSize ; ++ from_aa)
	{
		for (int to_aa =0; to_aa<_alphabetSize ; ++ to_aa)
		{
			chebft(coffij,_totalNumOfCoef,from_aa,to_aa);
			chder(coffij,coffij_of_derviation,_totalNumOfCoef);
			chder(coffij_of_derviation,coffij_of_second_derivation,_totalNumOfCoef);

			for (int tmp=0; tmp<_totalNumOfCoef;++tmp)
			{
				chebi_coff[from_aa][to_aa][tmp] = coffij[tmp];
				chebi_dervation_coff[from_aa][to_aa][tmp] = coffij_of_derviation[tmp];
				chebi_sec_dervation_coff[from_aa][to_aa][tmp] = coffij_of_second_derivation[tmp];
			}

		}
	}
}


void chebyshevAccelerator::chebft(Vdouble& c, int n, int from_aa, int to_aa) {
//----------------------------------------------------------------------------------
//input:	c[] is the vector where the cofficient will be
//			from aa and to_aa are for chosing the right function to be developed
//output:	non
//doing:	calculating the  chebichev coefficient in the chebichev polynom of the function 
//			slow_pij(from_aa,to_aa,t), and put them in the c[] vector			
//----------------------------------------------------------------------------------
	int k,j;
	MDOUBLE fac,bpa,bma;

	Vdouble f;
	f.resize(n);
	bma=0.5*(_rightRange-_leftRange);
	bpa=0.5*(_rightRange+_leftRange);
	for (k=0;k<n;k++) {
		MDOUBLE y=cos(3.141592653589793*(k+0.5)/n);
		f[k]= _pb->Pij_t(from_aa,to_aa,y*bma+bpa); //(*func)(y*bma+bpa);
	}
	fac=2.0/n;
	for (j=0;j<n;j++) {
		MDOUBLE sum=0.0;
		for (k=0;k<n;k++)
			sum += f[k]*cos(3.141592653589793*j*(k+0.5)/n);
		c[j]=fac*sum;
	}
	
}


const MDOUBLE chebyshevAccelerator::Pij_t(const int from_aa, const int to_aa, const MDOUBLE x) const
//----------------------------------------------------------------------------------
//input:	like pij_t
//output:	the probabilty
//doing:	calculating with the polinom of chebi and via eigenvalue decomposition			
//----------------------------------------------------------------------------------
{
  
	MDOUBLE d=0.0,dd=0.0,sv,y,y2,check;
	int j;

	if ((x-_leftRange)*(x-_rightRange) > 0.0) {
	return _pb->Pij_t(from_aa,to_aa,x);
//		errorMsg::reportError("x not in range in routine fast_Pij_t");// also quit the program
	}

	y2=2.0*(y=(2.0*x-_leftRange-_rightRange)/(_rightRange-_leftRange));
	for (j=_usingNumberOfCoef;j>0;j--) {
		sv=d;
		d=y2*d-dd+chebi_coff[from_aa][to_aa][j];
		dd=sv;
	}
	check =  y*d-dd+0.5*chebi_coff[from_aa][to_aa][0];
	if ((check>1) || (check<=0)) check = _pb->Pij_t(from_aa,to_aa,x);
	assert(check<=1);
	assert(check>=0);
	return check;
}


const MDOUBLE chebyshevAccelerator::dPij_dt(const int from_aa, const int to_aa, const MDOUBLE x) const
//----------------------------------------------------------------------------------
//input:	like pij_t
//output:	the derivation of probabilty
//doing:	calculating with the polinom of chebi and via eigenvalue decomposition			
//----------------------------------------------------------------------------------
{
  
	MDOUBLE d=0.0,dd=0.0,sv,y,y2;
	int j;

	if ((x-_leftRange)*(x-_rightRange) > 0.0) {
		return _pb->dPij_dt(from_aa,to_aa,x);
	}
	y2=2.0*(y=(2.0*x-_leftRange-_rightRange)/(_rightRange-_leftRange));
	for (j=_usingNumberOfCoef;j>0;j--) {
		sv=d;
		d=y2*d-dd+chebi_dervation_coff[from_aa][to_aa][j];
		dd=sv;
	}
	return y*d-dd+0.5*chebi_dervation_coff[from_aa][to_aa][0];
}


const MDOUBLE chebyshevAccelerator::d2Pij_dt2(const int from_aa, const int to_aa, const MDOUBLE x) const {
//----------------------------------------------------------------------------------
//input:	like pij_t
//output:	the second derivation of the probabilty
//doing:	calculating with the polynom of chebi and via eigenvalue decomposition			
//----------------------------------------------------------------------------------
	MDOUBLE d=0.0,dd=0.0,sv,y,y2;
	int j;

	if ((x-_leftRange)*(x-_rightRange) > 0.0) {
			return _pb->d2Pij_dt2(from_aa,to_aa,x);
	}
	y2=2.0*(y=(2.0*x-_leftRange-_rightRange)/(_rightRange-_leftRange));
	for (j=_usingNumberOfCoef;j>0;j--) {
		sv=d;
		d=y2*d-dd+chebi_sec_dervation_coff[from_aa][to_aa][j];
		dd=sv;
	}
	return y*d-dd+0.5*chebi_sec_dervation_coff[from_aa][to_aa][0];
}




void chebyshevAccelerator::chder(Vdouble &c, Vdouble &cder, int n) {
//----------------------------------------------------------------------------------
//input:	chebicev coff of f(x) i.e. in c[]. n is the vector size
//output:	chebicev coff of df(x)/dx i.e. in cder[]
//doing:	calculating the coff of the dervation from the coff of f.
//reference:numercal recepies in c, pg 195.			
//----------------------------------------------------------------------------------
	int j;
	MDOUBLE con;

	cder[n-1]=0.0;
	cder[n-2]=2*(n-1)*c[n-1];
	for (j=n-3;j>=0;j--)
		cder[j]=cder[j+2]+2*(j+1)*c[j+1];
	con=2.0f/(_rightRange-_leftRange);
	for (j=0;j<n;j++)
		cder[j] *= con;
}