17 #ifndef EXP_FIT_CLASS_H 18 #define EXP_FIT_CLASS_H 38 inline void Fit(std::vector<double>& r, std::vector<double>& u);
39 inline void FitCusp(std::vector<double>& r, std::vector<double>& u,
double cusp);
40 inline void eval(
double r,
double& u);
41 inline void eval(
double r,
double& u,
double& du,
double& d2u);
49 sign = u[0] < 0.0 ? -1.0 : 1.0;
50 if (r.size() != u.size())
51 app_error() <<
"Different number of rows of basis functions than" 52 <<
" of data points in LinFit. Exitting.\n";
54 std::vector<double> log_u(
N);
55 for (
int i = 0; i <
N; i++)
58 double r2jp1 = r[i] * r[i];
60 for (
int j = 1; j < M - 1; j++)
69 for (
int j = 0; j < M - 1; j++)
70 for (
int k = 0; k < M - 1; k++)
73 for (
int i = 0; i <
N; i++)
74 alpha(k, j) += F[i][j] * F[i][k];
79 for (
int k = 0; k < M - 1; k++)
80 for (
int i = 0; i <
N; i++)
81 beta[k] += log_u[i] * F[i][k];
83 for (
int i = 0; i < M - 1; i++)
84 for (
int j = 0; j < M - 1; j++)
85 alphaInv(i, j) = alpha(i, j);
88 for (
int i = 0; i < M - 1; i++)
91 for (
int j = 0; j < M - 1; j++)
92 c[i] += alphaInv(i, j) * beta[j];
96 for (
int i = 2; i < M; i++)
100 for (
int i = 0; i < M - 1; i++)
101 dCoefs[i] = (
double)(i + 1) * Coefs[i + 1];
102 for (
int i = 0; i < M - 2; i++)
103 d2Coefs[i] = (
double)(i + 1) * dCoefs[i + 1];
110 sign = u[0] < 0.0 ? -1.0 : 1.0;
111 if (r.size() != u.size())
112 app_error() <<
"Different number of rows of basis functions than" 113 <<
" of data points in LinFit. Exitting.\n";
114 std::vector<TinyVector<double, M>> F(
N);
115 std::vector<double> log_u(
N);
116 for (
int i = 0; i <
N; i++)
120 for (
int j = 0; j < M; j++)
129 for (
int j = 0; j < M; j++)
130 for (
int k = 0; k < M; k++)
133 for (
int i = 0; i <
N; i++)
134 alpha(k, j) += F[i][j] * F[i][k];
139 for (
int k = 0; k < M; k++)
140 for (
int i = 0; i <
N; i++)
141 beta[k] += log_u[i] * F[i][k];
143 for (
int i = 0; i < M; i++)
144 for (
int j = 0; j < M; j++)
145 alphaInv(i, j) = alpha(i, j);
147 for (
int i = 0; i < M; i++)
150 for (
int j = 0; j < M; j++)
151 Coefs[i] += alphaInv(i, j) * beta[j];
155 for (
int i = 0; i < M - 1; i++)
156 dCoefs[i] = (
double)(i + 1) * Coefs[i + 1];
157 for (
int i = 0; i < M - 2; i++)
158 d2Coefs[i] = (
double)(i + 1) * dCoefs[i + 1];
167 for (
int j = 0; j < M; j++)
169 poly += Coefs[j] * r2j;
179 double P = 0.0, dP = 0.0, d2P = 0.0;
180 for (
int j = 0; j < M; j++)
183 dP += dCoefs[j] * r2j;
184 d2P += d2Coefs[j] * r2j;
189 d2u = (d2P + dP * dP) * u;
200 inline void Fit(std::vector<double>& r, std::vector<std::complex<double>>& u);
201 inline void FitCusp(std::vector<double>& r, std::vector<std::complex<double>>& u,
double cusp);
202 inline void eval(
double r, std::complex<double>& u);
203 inline void eval(
double r, std::complex<double>& u, std::complex<double>& du, std::complex<double>& d2u);
212 std::vector<double> realVals(
N), imagVals(
N);
213 for (
int i = 0; i <
N; i++)
215 realVals[i] =
real(u[i]);
216 imagVals[i] =
imag(u[i]);
218 realFit.
FitCusp(r, realVals, cusp);
219 imagFit.
FitCusp(r, imagVals, cusp);
220 realSign = realFit.
sign;
221 imagSign = imagFit.
sign;
222 for (
int i = 0; i < M; i++)
224 Coefs[i] = std::complex<double>(realFit.
Coefs[i], imagFit.
Coefs[i]);
225 dCoefs[i] = std::complex<double>(realFit.
dCoefs[i], imagFit.
dCoefs[i]);
226 d2Coefs[i] = std::complex<double>(realFit.
d2Coefs[i], imagFit.
d2Coefs[i]);
235 std::vector<double> realVals(
N), imagVals(
N);
236 for (
int i = 0; i <
N; i++)
238 realVals[i] =
real(u[i]);
239 imagVals[i] =
imag(u[i]);
241 realFit.
Fit(r, realVals);
242 imagFit.
Fit(r, imagVals);
243 realSign = realFit.
sign;
244 imagSign = imagFit.
sign;
245 for (
int i = 0; i < M; i++)
247 Coefs[i] = std::complex<double>(realFit.
Coefs[i], imagFit.
Coefs[i]);
248 dCoefs[i] = std::complex<double>(realFit.
dCoefs[i], imagFit.
dCoefs[i]);
249 d2Coefs[i] = std::complex<double>(realFit.
d2Coefs[i], imagFit.
d2Coefs[i]);
258 std::complex<double> P = std::complex<double>();
259 for (
int j = 0; j < M; j++)
264 u = std::complex<double>(realSign *
std::exp(P.real()), imagSign *
std::exp(P.imag()));
271 std::complex<double> P, dP, d2P;
272 P = dP = d2P = std::complex<double>();
273 for (
int j = 0; j < M; j++)
276 dP += dCoefs[j] * r2j;
277 d2P += d2Coefs[j] * r2j;
280 u = std::complex<double>(realSign *
std::exp(P.real()), imagSign *
std::exp(P.imag()));
281 du = std::complex<double>(dP.real() * u.real(), dP.imag() * u.imag());
282 d2u = std::complex<double>((d2P.real() + dP.real() * dP.real()) * u.real(),
283 (d2P.imag() + dP.imag() * dP.imag()) * u.imag());
void eval(double r, std::complex< double > &u)
MatrixA::value_type invert_matrix(MatrixA &M, bool getdet=true)
invert a matrix
helper functions for EinsplineSetBuilder
Tensor< T, D >::Type_t det(const Tensor< T, D > &a)
TinyVector< double, M > dCoefs
std::ostream & app_error()
TinyVector< double, M > Coefs
float real(const float &c)
real part of a scalar. Cannot be replaced by std::real due to AFQMC specific needs.
TinyVector< std::complex< double >, M > d2Coefs
TinyVector< std::complex< double >, M > Coefs
float imag(const float &c)
imaginary part of a scalar. Cannot be replaced by std::imag due to AFQMC specific needs...
void FitCusp(std::vector< double > &r, std::vector< double > &u, double cusp)
TinyVector< double, M > d2Coefs
void eval(double r, double &u)
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
Define determinant operators.
void FitCusp(std::vector< double > &r, std::vector< std::complex< double >> &u, double cusp)
TinyVector< std::complex< double >, M > dCoefs
void Fit(std::vector< double > &r, std::vector< std::complex< double >> &u)
void Fit(std::vector< double > &r, std::vector< double > &u)