51 if (g.
mesh().positive_only())
52 TRIQS_RUNTIME_ERROR <<
"density is only implemented for g(i omega_n) with full mesh (positive and negative frequencies)";
54 nda::array_const_view<dcomplex, 3> mom_123;
59 double _abs_tail0 = max_element(abs(known_moments(0, range::all, range::all)));
61 "ERROR: Density implementation requires vanishing 0th moment\n error is :" +
std::to_string(_abs_tail0) +
"\n");
63 if (known_moments.shape()[0] < 4) {
64 auto [tail, error] =
fit_tail(g, known_moments);
66 "ERROR: High frequency moments have an error greater than 1e-2.\n Error = " +
std::to_string(error)
67 +
"\n Please make sure you treat the constant offset analytically!\n");
69 std::cerr <<
"WARNING: High frequency moments have an error greater than 1e-4.\n Error = " << error
70 <<
"\n Please make sure you treat the constant offset analytically!\n";
71 TRIQS_ASSERT2((first_dim(tail) > 3),
"ERROR: Density implementation requires at least a proper 3rd high-frequency moment\n");
74 mom_123.rebind(known_moments(range(1, 4), range::all, range::all));
77 long N1 = sh[0], N2 = sh[1];
78 nda::matrix<dcomplex> res(sh);
79 auto beta = g.
mesh().beta();
81 auto S = g.
mesh().statistic();
82 double b1 = 0, b2 = 0, b3 = 0;
90 }
else if (S == Boson) {
100 auto F = [&beta, &xi](dcomplex a,
double b) {
return xi * a / (-xi + exp(-beta * b)); };
102 for (
int n1 = 0; n1 < N1; n1++)
103 for (
int n2 = n1; n2 < N2; n2++) {
104 dcomplex m1 = mom_123(0, n1, n2), m2 = mom_123(1, n1, n2), m3 = mom_123(2, n1, n2);
121 }
else if (S == Boson) {
122 a1 = m1 / 6. - m2 / 2. + m3 / 3.;
123 a2 = -m1 / 2. + m2 / 2. + m3;
124 a3 = 4. * m1 / 3. - 4. * m3 / 3.;
129 for (
auto w : g.
mesh()) r += g[w](n1, n2) - (a1 / (w - b1) + a2 / (w - b2) + a3 / (w - b3));
130 res(n1, n2) = r / beta + m1 + F(a1, b1) + F(a2, b2) + F(a3, b3);
133 if (n2 > n1) res(n2, n1) =
conj(res(n1, n2));
141 auto km = array<dcomplex, 3>(make_shape(known_moments.shape()[0], 1, 1));
142 if (!known_moments.is_empty()) km(range::all, 0, 0) = known_moments();
154 double wmin = g.
mesh().w_min();
155 double dw = g.
mesh().delta();
159 auto N0 =
static_cast<int>(std::floor(-wmin / dw)) + 1;
160 double dw0 = -wmin - (N0 - 1) * dw;
166 for (
auto widx : range(1, N0)) res += g[widx];
167 if (abs(dw0) > 1e-9) {
169 res += 0.5 * ((a * a - 1.) * g[N0 - 1] + a * (2. - a) * g[N0]);
171 res -= 0.5 * g[N0 - 1];
176 res *=
dcomplex(0., 1.) * dw / M_PI;
180 return 0.5 * (res + dagger(res));
187 EXPECTS(beta > 0 and N == M);
189 auto res = nda::matrix<dcomplex>::zeros(N, N);
190 for (
auto w : g.
mesh()) res += g[w] / (1. + exp(beta * w));
199 return 0.5 * (res + dagger(res));
218 for (
auto l : gl.
mesh()) res -= std::sqrt(2 * l.index() + 1) * gl[l];
219 res /= gl.
mesh().beta();
A read-only, non-owning view of a Green's function.
mesh_t const & mesh() const
Get the mesh of the Green's function.
std::array< long, Target::rank > target_shape() const
Get the shape of the target.
Provides common type aliases, forward declarations and internal helpers for the Green's function cont...
Provides functions to compute the density (occupation) from Green's functions.
TRIQS exception hierarchy and related macros.
Provides tail fitting, slicing, inversion, reality and matrix-multiplication functions for Green's fu...
Provides the triqs::gfs::gf_const_view container, a read-only non-owning view of a Green's function.
nda::matrix< dcomplex > density(gf_const_view< mesh::imfreq > g, array_const_view< dcomplex, 3 > known_moments)
Compute the density from a Green's function.
auto reinterpret_scalar_valued_gf_as_matrix_valued(block_gf< M, T, L, A > &g)
Apply reinterpret_scalar_valued_gf_as_matrix_valued block-wise to each block of the block Green's fun...
G::regular_type::real_t imag(G const &g)
Take the imaginary part of a Green's function (no check), returning a new Green's function with a rea...
G::regular_type conj(G const &g)
Complex-conjugate a Green's function, returning a new Green's function.
gf< M, matrix_valued > transpose(gf_view< M, matrix_valued > g)
Transpose the target matrix of a matrix-valued Green's function, returning a new Green's function.
auto make_zero_tail(G const &g, int n_moments=10)
Create a zero-initialized tail object for a given Green function object.
std::pair< typename A::regular_type, double > fit_tail(G const &g, A const &known_moments={})
Fit the high-frequency tail of a Green's function using a least-squares procedure.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
#define TRIQS_ASSERT2(X,...)
Like TRIQS_ASSERT but lets the caller add a custom message.
std::complex< double > dcomplex
Convenience alias for std::complex<double>.
string to_string(string const &str)
Identity overload for std::string.
Common macros used in TRIQS.
Provides a mesh type on the imaginary frequency axis.
Provides a mesh type for Legendre polynomials as basis functions.
Provides a mesh type on the real frequency axis.
Provides the target types that fix the value stored at each mesh point of a Green's function.