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);
236 SpinorMatrix<T> out(a.m_i0, a.m_stride, a.m_size, a.m_incl_g, a.m_rgrid);
242 out.
ff() = a.
ff() * b.
ff();
243 if (a.m_incl_g && b.m_incl_g) {
244 out.
ff() += a.fg() * b.gf();
245 out.fg() = a.
ff() * b.fg() + a.fg() * b.gg();
246 out.gf() = a.gf() * b.
ff() + a.gg() * b.gf();
247 out.gg() = a.gf() * b.fg() + a.gg() * b.gg();
256 if (this->m_incl_g) {
276 if (this->m_incl_g) {
303 out.ff().conj_in_place();
304 out.fg().conj_in_place();
305 out.gf().conj_in_place();
306 out.gg().conj_in_place();
314 out.fg() = m_fg.
real();
315 out.gf() = m_gf.
real();
316 out.gg() = m_gg.
real();
323 out.fg() = m_fg.
imag();
324 out.gf() = m_gf.
imag();
325 out.gg() = m_gg.
imag();
345 const auto &ai = m_ff;
346 const auto &b = m_fg;
347 const auto &c = m_gf;
348 const auto &d = m_gg;
349 const auto cai = c * ai;
351 const auto aib_dmcaib = ai * b * dmcaib;
352 m_ff += aib_dmcaib * cai;
353 m_fg = -1.0 * aib_dmcaib;
354 m_gf = -1.0 * dmcaib * cai;
362 return out.invert_in_place();
368 const auto dus = m_rgrid->du() * double(m_stride);
369 for (
auto i = 0ul; i < m_size; ++i) {
370 for (
auto j = 0ul; j < m_size; ++j) {
372 const auto dr = m_rgrid->drdu(sj) * dus;
377 for (
auto i = 0ul; i < m_size; ++i) {
378 for (
auto j = 0ul; j < m_size; ++j) {
380 const auto dr = m_rgrid->drdu(sj) * dus;
391 const auto dus = m_rgrid->du() * double(m_stride);
392 for (
auto i = 0ul; i < m_size; ++i) {
394 const auto dr = m_rgrid->drdu(si) * dus;
395 for (
auto j = 0ul; j < m_size; ++j) {
400 for (
auto i = 0ul; i < m_size; ++i) {
402 const auto dr = m_rgrid->drdu(si) * dus;
403 for (
auto j = 0ul; j < m_size; ++j) {
415 return out.drj_in_place();
420 return out.dri_in_place();
424 double dr(std::size_t sub_index)
const {
426 return m_rgrid->drdu(full_index) * m_rgrid->du() * double(m_stride);
432 return m_i0 + i * m_stride;
442 for (
auto i = 0ul; i < m_size; ++i) {
444 for (
auto j = 0ul; j < m_size; ++j) {
446 m_ff[i][j] += k * ket.f(si) * bra.f(sj);
451 for (
auto i = 0ul; i < m_size; ++i) {
453 for (
auto j = 0ul; j < m_size; ++j) {
456 m_fg[i][j] += k * ket.f(si) * bra.g(sj);
457 m_gf[i][j] += k * ket.g(si) * bra.f(sj);
458 m_gg[i][j] += k * ket.g(si) * bra.g(sj);
469 const auto &r = Fn.
grid().
r();
474 std::vector<double> f(m_size), g;
475 for (
auto i = 0ul; i < m_size; ++i) {
476 for (
auto j = 0ul; j < m_size; ++j) {
478 f[i] += m_ff(i, j) * Fn.
f(j_f);
483 for (
auto i = 0ul; i < m_size; ++i) {
484 for (
auto j = 0ul; j < m_size; ++j) {
486 f[i] += m_fg(i, j) * Fn.
g(j_f);
487 g[i] += (m_gf(i, j) * Fn.
f(j_f) + m_gg(i, j) * Fn.
g(j_f));
504 friend std::ostream &operator<<(std::ostream &os,
const SpinorMatrix<T> &a) {
523 equal(lhs.gf(), rhs.gf()) &&
equal(lhs.gg(), rhs.gg());
529 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
530 for (
auto i = 0ul; i < a.size(); ++i) {
531 for (
auto j = 0ul; j < a.size(); ++j) {
532 if (std::abs(a.
ff(i, j)) > xff)
533 xff = std::abs(a.
ff(i, j));
534 if (a.g_size() != 0) {
535 if (std::abs(a.fg(i, j)) > xfg)
536 xfg = std::abs(a.fg(i, j));
537 if (std::abs(a.gf(i, j)) > xgf)
538 xgf = std::abs(a.gf(i, j));
539 if (std::abs(a.gg(i, j)) > xgg)
540 xgg = std::abs(a.gg(i, j));
544 return std::max({xff, xfg, xgf, xgg});
550 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
551 for (
auto i = 0ul; i < a.size(); ++i) {
552 for (
auto j = 0ul; j < a.size(); ++j) {
553 if (std::abs(a.
ff(i, j) - b.
ff(i, j)) > xff)
554 xff = std::abs(a.
ff(i, j) - b.
ff(i, j));
555 if (a.g_size() != 0) {
556 if (std::abs(a.fg(i, j) - b.fg(i, j)) > xfg)
557 xfg = std::abs(a.fg(i, j) - b.fg(i, j));
558 if (std::abs(a.gf(i, j) - b.gf(i, j)) > xgf)
559 xgf = std::abs(a.gf(i, j) - b.gf(i, j));
560 if (std::abs(a.gg(i, j) - b.gg(i, j)) > xgg)
561 xgg = std::abs(a.gg(i, j) - b.gg(i, j));
565 return std::max({xff, xfg, xgf, xgg});
572 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
573 for (
auto i = 0ul; i < a.size(); ++i) {
574 for (
auto j = 0ul; j < a.size(); ++j) {
576 std::abs((a.
ff(i, j) - b.
ff(i, j)) / (a.
ff(i, j) + b.
ff(i, j)));
579 if (a.g_size() != 0) {
581 std::abs((a.fg(i, j) - b.fg(i, j)) / (a.fg(i, j) + b.fg(i, j)));
583 std::abs((a.gf(i, j) - b.gf(i, j)) / (a.gf(i, j) + b.gf(i, j)));
585 std::abs((a.gg(i, j) - b.gg(i, j)) / (a.gg(i, j) + b.gg(i, j)));
595 return std::max({xff, xfg, xgf, xgg});
599using GMatrix = SpinorMatrix<double>;
600using ComplexGMatrix = SpinorMatrix<std::complex<double>>;
601using ComplexDouble = std::complex<double>;
Stores radial Dirac spinor: F_nk = (f, g)
Definition DiracSpinor.hpp:41
const std::vector< double > & f() const
Upper (large) radial component function, f.
Definition DiracSpinor.hpp:125
const Grid & grid() const
Resturns a const reference to the radial grid.
Definition DiracSpinor.hpp:115
const std::vector< double > & g() const
Lower (small) radial component function, g.
Definition DiracSpinor.hpp:132
const std::vector< double > & r() const
Grid points, r.
Definition Grid.hpp:75
Matrix class; row-major.
Definition Matrix.hpp:35
Matrix< T > & invert_in_place()
Inverts the matrix, in place. Uses GSL; via LU decomposition. Only works for double/complex<double>.
Definition Matrix.ipp:77
auto complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition Matrix.ipp:205
auto imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:194
Matrix< T > & zero()
Sets all elements to zero, in place.
Definition Matrix.ipp:154
Matrix< T > & mult_elements_by(const Matrix< T > &a)
Muplitplies all the elements by those of matrix a, in place: M_ij *= a_ij.
Definition Matrix.ipp:270
auto real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition Matrix.ipp:183
void resize(std::size_t rows, std::size_t cols)
Resizes matrix to new dimension; all values reset to default.
Definition Matrix.hpp:92
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:437
SpinorMatrix< T > & operator-=(T aI)
Adition of identity: Matrix<T> -= T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:214
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:267
SpinorMatrix< std::complex< double > > complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition SpinorMatrix.hpp:330
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:221
SpinorMatrix< T > drj() const
Multiplies by drj: Q_ij -> Q_ij*dr_j. Returns new matrix (orig unchanged)
Definition SpinorMatrix.hpp:413
SpinorMatrix< T > & drj_in_place()
Multiplies by drj: Q_ij -> Q_ij*dr_j, in place.
Definition SpinorMatrix.hpp:367
SpinorMatrix< T > & operator*=(const T x)
Scalar multiplication.
Definition SpinorMatrix.hpp:183
SpinorMatrix< T > & dri_in_place()
Multiplies by dri: Q_ij -> Q_ij*dr_i, in place.
Definition SpinorMatrix.hpp:390
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const RadialMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:286
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:202
SpinorMatrix< T > & operator-=(const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:175
SpinorMatrix< T > & operator+=(T aI)
Adition of identity: Matrix<T> += T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:208
SpinorMatrix< double > imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:320
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:418
SpinorMatrix< T > & mult_elements_by(const SpinorMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:254
DiracSpinor operator*(const DiracSpinor &Fn) const
Action of SpinorMatrix operator on DiracSpinor. Inludes Integration: G*F = Int[G(r,...
Definition SpinorMatrix.hpp:467
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 +,-.
Definition SpinorMatrix.hpp:167
SpinorMatrix< T > & invert_in_place()
Inversion (in place)
Definition SpinorMatrix.hpp:342
friend SpinorMatrix< T > mult_elements(const RadialMatrix< T > &rhs, SpinorMatrix< T > lhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:292
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:431
friend SpinorMatrix< T > operator-(SpinorMatrix< T > M, T aI)
Adition of identity: Matrix<T> - T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:225
double dr(std::size_t sub_index) const
returns dr at position along sub grid
Definition SpinorMatrix.hpp:424
friend SpinorMatrix< T > operator*(const SpinorMatrix< T > &a, const SpinorMatrix< T > &b)
Matrix multplication: Note: integration measure not automatically included: call ....
Definition SpinorMatrix.hpp:233
SpinorMatrix< double > real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:311
friend SpinorMatrix< T > operator+(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:192
SpinorMatrix< T > inverse() const
Returns inverse of matrix; original matrix unchanged.
Definition SpinorMatrix.hpp:360
SpinorMatrix< T > & mult_elements_by(const RadialMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:274
SpinorMatrix< T > conj() const
Returns conjugate of matrix.
Definition SpinorMatrix.hpp:301
friend SpinorMatrix< T > operator-(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:197
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)
Performs interpolation using GSL (GNU Scientific Library)
Definition Interpolator.hpp:162
Many-body perturbation theory.
Definition CI_Integrals.hpp:9
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