2#include "LinAlg/Matrix.hpp"
3#include "Maths/Grid.hpp"
4#include "Maths/Interpolator.hpp"
5#include "RadialMatrix.hpp"
6#include "Wavefunction/DiracSpinor.hpp"
45 std::size_t m_i0, m_stride;
50 std::shared_ptr<const Grid> m_rgrid;
51 std::vector<double> sub_r{};
56 SpinorMatrix(std::size_t i0, std::size_t stride, std::size_t size,
57 bool incl_g, std::shared_ptr<const Grid> rgrid)
61 m_g_size(incl_g ? size : 0),
70 const auto &r = m_rgrid->r();
71 sub_r.reserve(m_size);
72 assert(m_i0 + m_stride * m_size <= r.size());
73 for (std::size_t i = 0; i < m_size; ++i) {
76 assert(m_i0 + m_stride * m_size <= r.size());
84 T &
ff(std::size_t i, std::size_t j) {
return m_ff(i, j); }
85 T &fg(std::size_t i, std::size_t j) {
return m_fg(i, j); }
86 T &gf(std::size_t i, std::size_t j) {
return m_gf(i, j); }
87 T &gg(std::size_t i, std::size_t j) {
return m_gg(i, j); }
88 const T
ff(std::size_t i, std::size_t j)
const {
return m_ff(i, j); }
89 const T fg(std::size_t i, std::size_t j)
const {
return m_fg(i, j); }
90 const T gf(std::size_t i, std::size_t j)
const {
return m_gf(i, j); }
91 const T gg(std::size_t i, std::size_t j)
const {
return m_gg(i, j); }
104 assert(mu < 2 && nu < 2);
105 if (mu == 0 && nu == 0)
107 if (mu == 0 && nu == 1)
109 if (mu == 1 && nu == 0)
111 if (mu == 1 && nu == 1)
117 assert(mu < 2 && nu < 2);
118 if (mu == 0 && nu == 0)
120 if (mu == 0 && nu == 1)
122 if (mu == 1 && nu == 0)
124 if (mu == 1 && nu == 1)
129 std::size_t size()
const {
return m_size; }
130 std::size_t g_size()
const {
return m_g_size; }
131 bool includes_g()
const {
return m_g_size == m_size; };
132 std::size_t i0()
const {
return m_i0; }
133 std::size_t stride()
const {
return m_stride; }
159 m_fg.
resize(m_size, m_size);
160 m_gf.
resize(m_size, m_size);
161 m_gg.
resize(m_size, m_size);
248 SpinorMatrix<T> out(a.m_i0, a.m_stride, a.m_size, a.m_incl_g, a.m_rgrid);
254 out.
ff() = a.
ff() * b.
ff();
255 if (a.m_incl_g && b.m_incl_g) {
256 out.
ff() += a.fg() * b.gf();
257 out.fg() = a.
ff() * b.fg() + a.fg() * b.gg();
258 out.gf() = a.gf() * b.
ff() + a.gg() * b.gf();
259 out.gg() = a.gf() * b.fg() + a.gg() * b.gg();
268 if (this->m_incl_g) {
288 if (this->m_incl_g) {
315 out.ff().conj_in_place();
316 out.fg().conj_in_place();
317 out.gf().conj_in_place();
318 out.gg().conj_in_place();
326 out.fg() = m_fg.
real();
327 out.gf() = m_gf.
real();
328 out.gg() = m_gg.
real();
335 out.fg() = m_fg.
imag();
336 out.gf() = m_gf.
imag();
337 out.gg() = m_gg.
imag();
357 const auto &ai = m_ff;
358 const auto &b = m_fg;
359 const auto &c = m_gf;
360 const auto &d = m_gg;
361 const auto cai = c * ai;
363 const auto aib_dmcaib = ai * b * dmcaib;
364 m_ff += aib_dmcaib * cai;
365 m_fg = -1.0 * aib_dmcaib;
366 m_gf = -1.0 * dmcaib * cai;
374 return out.invert_in_place();
380 const auto dus = m_rgrid->du() * double(m_stride);
381 for (
auto i = 0ul; i < m_size; ++i) {
382 for (
auto j = 0ul; j < m_size; ++j) {
384 const auto dr = m_rgrid->drdu(sj) * dus;
389 for (
auto i = 0ul; i < m_size; ++i) {
390 for (
auto j = 0ul; j < m_size; ++j) {
392 const auto dr = m_rgrid->drdu(sj) * dus;
403 const auto dus = m_rgrid->du() * double(m_stride);
404 for (
auto i = 0ul; i < m_size; ++i) {
406 const auto dr = m_rgrid->drdu(si) * dus;
407 for (
auto j = 0ul; j < m_size; ++j) {
412 for (
auto i = 0ul; i < m_size; ++i) {
414 const auto dr = m_rgrid->drdu(si) * dus;
415 for (
auto j = 0ul; j < m_size; ++j) {
427 return out.drj_in_place();
432 return out.dri_in_place();
436 double dr(std::size_t sub_index)
const {
438 return m_rgrid->drdu(full_index) * m_rgrid->du() * double(m_stride);
444 return m_i0 + i * m_stride;
454 for (
auto i = 0ul; i < m_size; ++i) {
456 for (
auto j = 0ul; j < m_size; ++j) {
458 m_ff[i][j] += k * ket.f(si) * bra.f(sj);
463 for (
auto i = 0ul; i < m_size; ++i) {
465 for (
auto j = 0ul; j < m_size; ++j) {
468 m_fg[i][j] += k * ket.f(si) * bra.g(sj);
469 m_gf[i][j] += k * ket.g(si) * bra.f(sj);
470 m_gg[i][j] += k * ket.g(si) * bra.g(sj);
480 const auto &r = Fn.
grid().
r();
482 std::vector<double> f(m_size), g;
483 for (
auto i = 0ul; i < m_size; ++i) {
484 for (
auto j = 0ul; j < m_size; ++j) {
486 f[i] += m_ff(i, j) * Fn.
f(j_f);
491 for (
auto i = 0ul; i < m_size; ++i) {
492 for (
auto j = 0ul; j < m_size; ++j) {
494 f[i] += m_fg(i, j) * Fn.
g(j_f);
495 g[i] += (m_gf(i, j) * Fn.
f(j_f) + m_gg(i, j) * Fn.
g(j_f));
514 constexpr bool extrapolate_tail =
true;
515 if (extrapolate_tail) {
516 constexpr std::size_t n_fit = 10;
517 double alpha_eff = 0.0;
518 std::size_t n_used = 0;
519 for (
auto i = m_size - std::min(n_fit, m_size); i < m_size; ++i) {
521 if (Fn.
f(i_f) == 0.0)
523 alpha_eff += f[i] / Fn.
f(i_f) * std::pow(r[i_f], 4);
527 alpha_eff /= double(n_used);
529 for (
auto i = i_rmax + 1; i < Fn.
max_pt(); ++i) {
530 const auto V = alpha_eff / std::pow(r[i], 4);
531 out.
f(i) = V * Fn.
f(i);
533 out.
g(i) = V * Fn.
g(i);
543 friend std::ostream &operator<<(std::ostream &os,
const SpinorMatrix<T> &a) {
562 equal(lhs.gf(), rhs.gf()) &&
equal(lhs.gg(), rhs.gg());
568 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
569 for (
auto i = 0ul; i < a.size(); ++i) {
570 for (
auto j = 0ul; j < a.size(); ++j) {
571 if (std::abs(a.
ff(i, j)) > xff)
572 xff = std::abs(a.
ff(i, j));
573 if (a.g_size() != 0) {
574 if (std::abs(a.fg(i, j)) > xfg)
575 xfg = std::abs(a.fg(i, j));
576 if (std::abs(a.gf(i, j)) > xgf)
577 xgf = std::abs(a.gf(i, j));
578 if (std::abs(a.gg(i, j)) > xgg)
579 xgg = std::abs(a.gg(i, j));
583 return std::max({xff, xfg, xgf, xgg});
589 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
590 for (
auto i = 0ul; i < a.size(); ++i) {
591 for (
auto j = 0ul; j < a.size(); ++j) {
592 if (std::abs(a.
ff(i, j) - b.
ff(i, j)) > xff)
593 xff = std::abs(a.
ff(i, j) - b.
ff(i, j));
594 if (a.g_size() != 0) {
595 if (std::abs(a.fg(i, j) - b.fg(i, j)) > xfg)
596 xfg = std::abs(a.fg(i, j) - b.fg(i, j));
597 if (std::abs(a.gf(i, j) - b.gf(i, j)) > xgf)
598 xgf = std::abs(a.gf(i, j) - b.gf(i, j));
599 if (std::abs(a.gg(i, j) - b.gg(i, j)) > xgg)
600 xgg = std::abs(a.gg(i, j) - b.gg(i, j));
604 return std::max({xff, xfg, xgf, xgg});
611 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
612 for (
auto i = 0ul; i < a.size(); ++i) {
613 for (
auto j = 0ul; j < a.size(); ++j) {
615 std::abs((a.
ff(i, j) - b.
ff(i, j)) / (a.
ff(i, j) + b.
ff(i, j)));
618 if (a.g_size() != 0) {
620 std::abs((a.fg(i, j) - b.fg(i, j)) / (a.fg(i, j) + b.fg(i, j)));
622 std::abs((a.gf(i, j) - b.gf(i, j)) / (a.gf(i, j) + b.gf(i, j)));
624 std::abs((a.gg(i, j) - b.gg(i, j)) / (a.gg(i, j) + b.gg(i, j)));
634 return std::max({xff, xfg, xgf, xgg});
638using GMatrix = SpinorMatrix<double>;
639using ComplexGMatrix = SpinorMatrix<std::complex<double>>;
640using ComplexDouble = std::complex<double>;
Stores radial Dirac spinor: F_nk = (f, g)
Definition DiracSpinor.hpp:44
auto max_pt() const
Effective size(); index after last non-zero point (index for f[i])
Definition DiracSpinor.hpp:157
const std::vector< double > & f() const
Upper (large) radial component function, f.
Definition DiracSpinor.hpp:136
const Grid & grid() const
Resturns a const reference to the radial grid.
Definition DiracSpinor.hpp:126
const std::vector< double > & g() const
Lower (small) radial component function, g.
Definition DiracSpinor.hpp:143
const std::vector< double > & r() const
Full grid vector r.
Definition Grid.hpp:131
Row-major dense matrix with arithmetic and linear algebra support.
Definition Matrix.hpp:208
Matrix< T > & invert_in_place()
Inverts the matrix in place.
Definition Matrix.ipp:54
auto complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition Matrix.ipp:188
auto imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:177
Matrix< T > & zero()
Sets all elements to zero, in place.
Definition Matrix.ipp:137
Matrix< T > & mult_elements_by(const Matrix< T > &a)
Elementwise multiply in place: M_ij *= a_ij.
Definition Matrix.ipp:268
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:166
void resize(std::size_t rows, std::size_t cols)
Resizes matrix to new dimension; all values reset to default.
Definition Matrix.hpp:265
Definition RadialMatrix.hpp:27
const LinAlg::Matrix< T > & Rmatrix() const
direct access to radial matrix
Definition RadialMatrix.hpp:69
Definition SpinorMatrix.hpp:43
void add(const DiracSpinor &ket, const DiracSpinor &bra, T k=T(1.0))
Adds k*|ket><bra| to matrix (used for building Green's functions)
Definition SpinorMatrix.hpp:449
SpinorMatrix< T > & operator-=(T aI)
Adition of identity: Matrix<T> -= T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:226
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:279
SpinorMatrix< std::complex< double > > complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition SpinorMatrix.hpp:342
SpinorMatrix< T > & create_g()
Creates g parts of spinor matrix - will have value 0.
Definition SpinorMatrix.hpp:156
T & ff(std::size_t i, std::size_t j)
direct access to matrix elements
Definition SpinorMatrix.hpp:84
friend SpinorMatrix< T > operator+(SpinorMatrix< T > M, T aI)
Adition of identity: Matrix<T> + T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:233
SpinorMatrix< T > drj() const
Multiplies by drj: Q_ij -> Q_ij*dr_j. Returns new matrix (orig unchanged)
Definition SpinorMatrix.hpp:425
SpinorMatrix< T > & drj_in_place()
Multiplies by drj: Q_ij -> Q_ij*dr_j, in place.
Definition SpinorMatrix.hpp:379
SpinorMatrix< T > & operator*=(const T x)
Scalar multiplication.
Definition SpinorMatrix.hpp:195
SpinorMatrix< T > & dri_in_place()
Multiplies by dri: Q_ij -> Q_ij*dr_i, in place.
Definition SpinorMatrix.hpp:402
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const RadialMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:298
const LinAlg::Matrix< T > & ff() const
direct access to matrix's
Definition SpinorMatrix.hpp:94
friend SpinorMatrix< T > operator*(const T x, SpinorMatrix< T > rhs)
Scalar multiplication.
Definition SpinorMatrix.hpp:214
SpinorMatrix< T > & operator-=(const SpinorMatrix< T > &rhs)
Matrix adition +,- (see operator+= for matrices without g parts)
Definition SpinorMatrix.hpp:182
SpinorMatrix< T > & operator+=(T aI)
Adition of identity: Matrix<T> += T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:220
SpinorMatrix< double > imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:332
void zero()
Sets all matrix elements to zero.
Definition SpinorMatrix.hpp:137
SpinorMatrix< T > dri() const
Multiplies by dri: Q_ij -> Q_ij*dr_i. Returns new matrix (orig unchanged)
Definition SpinorMatrix.hpp:430
SpinorMatrix< T > & mult_elements_by(const SpinorMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:266
DiracSpinor operator*(const DiracSpinor &Fn) const
Action of SpinorMatrix operator on DiracSpinor. Assumes matrix already includes integration measure.
Definition SpinorMatrix.hpp:478
SpinorMatrix< T > & drop_g()
Kills g parts of spinor matrix, in place!
Definition SpinorMatrix.hpp:146
SpinorMatrix< T > & operator+=(const SpinorMatrix< T > &rhs)
Matrix adition +,-. A matrix without g parts is treated as having zero g parts: adding one to a matri...
Definition SpinorMatrix.hpp:169
SpinorMatrix< T > & invert_in_place()
Inversion (in place)
Definition SpinorMatrix.hpp:354
friend SpinorMatrix< T > mult_elements(const RadialMatrix< T > &rhs, SpinorMatrix< T > lhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:304
std::size_t index_to_fullgrid(std::size_t i) const
Converts an index on the sub-grid to the full grid.
Definition SpinorMatrix.hpp:443
friend SpinorMatrix< T > operator-(SpinorMatrix< T > M, T aI)
Adition of identity: Matrix<T> - T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:237
double dr(std::size_t sub_index) const
returns dr at position along sub grid
Definition SpinorMatrix.hpp:436
friend SpinorMatrix< T > operator*(const SpinorMatrix< T > &a, const SpinorMatrix< T > &b)
Matrix multplication: Note: integration measure not automatically included: call ....
Definition SpinorMatrix.hpp:245
SpinorMatrix< double > real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:323
friend SpinorMatrix< T > operator+(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:204
SpinorMatrix< T > inverse() const
Returns inverse of matrix; original matrix unchanged.
Definition SpinorMatrix.hpp:372
SpinorMatrix< T > & mult_elements_by(const RadialMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:286
SpinorMatrix< T > conj() const
Returns conjugate of matrix.
Definition SpinorMatrix.hpp:313
friend SpinorMatrix< T > operator-(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:209
std::vector< double > interpolate(const std::vector< double > &x_in, const std::vector< double > &y_in, const std::vector< double > &x_out, Method method=Method::cspline)
Convenience wrapper: interpolates y_in(x_in) and evaluates at x_out.
Definition Interpolator.hpp:154
Many-body perturbation theory.
Definition MatrixElements.hpp:12
double max_element(const RadialMatrix< T > &a)
returns maximum element (by abs)
Definition RadialMatrix.hpp:287
bool equal(const RadialMatrix< T > &lhs, const RadialMatrix< T > &rhs)
Checks if two matrix's are equal (to within parts in 10^12)
Definition RadialMatrix.hpp:281
double max_delta(const RadialMatrix< T > &a, const RadialMatrix< T > &b)
returns maximum difference (abs) between two matrixs
Definition RadialMatrix.hpp:301
double max_epsilon(const RadialMatrix< T > &a, const RadialMatrix< T > &b)
returns maximum relative diference [aij-bij/(aij+bij)] (abs) between two matrices
Definition RadialMatrix.hpp:316