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); }
103 std::size_t size()
const {
return m_size; }
104 std::size_t g_size()
const {
return m_g_size; }
105 bool includes_g()
const {
return m_g_size == m_size; };
106 std::size_t i0()
const {
return m_i0; }
107 std::size_t stride()
const {
return m_stride; }
133 m_fg.
resize(m_size, m_size);
134 m_gf.
resize(m_size, m_size);
135 m_gg.
resize(m_size, m_size);
210 SpinorMatrix<T> out(a.m_i0, a.m_stride, a.m_size, a.m_incl_g, a.m_rgrid);
216 out.
ff() = a.
ff() * b.
ff();
217 if (a.m_incl_g && b.m_incl_g) {
218 out.
ff() += a.fg() * b.gf();
219 out.fg() = a.
ff() * b.fg() + a.fg() * b.gg();
220 out.gf() = a.gf() * b.
ff() + a.gg() * b.gf();
221 out.gg() = a.gf() * b.fg() + a.gg() * b.gg();
230 if (this->m_incl_g) {
250 if (this->m_incl_g) {
277 out.ff().conj_in_place();
278 out.fg().conj_in_place();
279 out.gf().conj_in_place();
280 out.gg().conj_in_place();
288 out.fg() = m_fg.
real();
289 out.gf() = m_gf.
real();
290 out.gg() = m_gg.
real();
297 out.fg() = m_fg.
imag();
298 out.gf() = m_gf.
imag();
299 out.gg() = m_gg.
imag();
319 const auto &ai = m_ff;
320 const auto &b = m_fg;
321 const auto &c = m_gf;
322 const auto &d = m_gg;
323 const auto cai = c * ai;
325 const auto aib_dmcaib = ai * b * dmcaib;
326 m_ff += aib_dmcaib * cai;
327 m_fg = -1.0 * aib_dmcaib;
328 m_gf = -1.0 * dmcaib * cai;
336 return out.invert_in_place();
342 const auto dus = m_rgrid->du() * double(m_stride);
343 for (
auto i = 0ul; i < m_size; ++i) {
344 for (
auto j = 0ul; j < m_size; ++j) {
346 const auto dr = m_rgrid->drdu(sj) * dus;
351 for (
auto i = 0ul; i < m_size; ++i) {
352 for (
auto j = 0ul; j < m_size; ++j) {
354 const auto dr = m_rgrid->drdu(sj) * dus;
365 const auto dus = m_rgrid->du() * double(m_stride);
366 for (
auto i = 0ul; i < m_size; ++i) {
368 const auto dr = m_rgrid->drdu(si) * dus;
369 for (
auto j = 0ul; j < m_size; ++j) {
374 for (
auto i = 0ul; i < m_size; ++i) {
376 const auto dr = m_rgrid->drdu(si) * dus;
377 for (
auto j = 0ul; j < m_size; ++j) {
389 return out.drj_in_place();
394 return out.dri_in_place();
398 double dr(std::size_t sub_index)
const {
400 return m_rgrid->drdu(full_index) * m_rgrid->du() * double(m_stride);
406 return m_i0 + i * m_stride;
416 for (
auto i = 0ul; i < m_size; ++i) {
418 for (
auto j = 0ul; j < m_size; ++j) {
420 m_ff[i][j] += k * ket.f(si) * bra.f(sj);
425 for (
auto i = 0ul; i < m_size; ++i) {
427 for (
auto j = 0ul; j < m_size; ++j) {
430 m_fg[i][j] += k * ket.f(si) * bra.g(sj);
431 m_gf[i][j] += k * ket.g(si) * bra.f(sj);
432 m_gg[i][j] += k * ket.g(si) * bra.g(sj);
443 const auto &r = Fn.
grid().
r();
448 std::vector<double> f(m_size), g;
449 for (
auto i = 0ul; i < m_size; ++i) {
450 for (
auto j = 0ul; j < m_size; ++j) {
452 f[i] += m_ff(i, j) * Fn.
f(j_f);
457 for (
auto i = 0ul; i < m_size; ++i) {
458 for (
auto j = 0ul; j < m_size; ++j) {
460 f[i] += m_fg(i, j) * Fn.
g(j_f);
461 g[i] += (m_gf(i, j) * Fn.
f(j_f) + m_gg(i, j) * Fn.
g(j_f));
478 friend std::ostream &operator<<(std::ostream &os,
const SpinorMatrix<T> &a) {
497 equal(lhs.gf(), rhs.gf()) &&
equal(lhs.gg(), rhs.gg());
503 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
504 for (
auto i = 0ul; i < a.size(); ++i) {
505 for (
auto j = 0ul; j < a.size(); ++j) {
506 if (std::abs(a.
ff(i, j)) > xff)
507 xff = std::abs(a.
ff(i, j));
508 if (a.g_size() != 0) {
509 if (std::abs(a.fg(i, j)) > xfg)
510 xfg = std::abs(a.fg(i, j));
511 if (std::abs(a.gf(i, j)) > xgf)
512 xgf = std::abs(a.gf(i, j));
513 if (std::abs(a.gg(i, j)) > xgg)
514 xgg = std::abs(a.gg(i, j));
518 return std::max({xff, xfg, xgf, xgg});
524 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
525 for (
auto i = 0ul; i < a.size(); ++i) {
526 for (
auto j = 0ul; j < a.size(); ++j) {
527 if (std::abs(a.
ff(i, j) - b.
ff(i, j)) > xff)
528 xff = std::abs(a.
ff(i, j) - b.
ff(i, j));
529 if (a.g_size() != 0) {
530 if (std::abs(a.fg(i, j) - b.fg(i, j)) > xfg)
531 xfg = std::abs(a.fg(i, j) - b.fg(i, j));
532 if (std::abs(a.gf(i, j) - b.gf(i, j)) > xgf)
533 xgf = std::abs(a.gf(i, j) - b.gf(i, j));
534 if (std::abs(a.gg(i, j) - b.gg(i, j)) > xgg)
535 xgg = std::abs(a.gg(i, j) - b.gg(i, j));
539 return std::max({xff, xfg, xgf, xgg});
546 double xff = 0.0, xfg = 0.0, xgf = 0.0, xgg = 0.0;
547 for (
auto i = 0ul; i < a.size(); ++i) {
548 for (
auto j = 0ul; j < a.size(); ++j) {
550 std::abs((a.
ff(i, j) - b.
ff(i, j)) / (a.
ff(i, j) + b.
ff(i, j)));
553 if (a.g_size() != 0) {
555 std::abs((a.fg(i, j) - b.fg(i, j)) / (a.fg(i, j) + b.fg(i, j)));
557 std::abs((a.gf(i, j) - b.gf(i, j)) / (a.gf(i, j) + b.gf(i, j)));
559 std::abs((a.gg(i, j) - b.gg(i, j)) / (a.gg(i, j) + b.gg(i, j)));
569 return std::max({xff, xfg, xgf, xgg});
573using GMatrix = SpinorMatrix<double>;
574using ComplexGMatrix = SpinorMatrix<std::complex<double>>;
575using 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:411
SpinorMatrix< T > & operator-=(T aI)
Adition of identity: Matrix<T> -= T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:188
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:241
SpinorMatrix< std::complex< double > > complex() const
Converts a real to complex matrix (changes type; returns a complex matrix)
Definition SpinorMatrix.hpp:304
SpinorMatrix< T > & create_g()
Creates g parts of spinor matrix - will have value 0.
Definition SpinorMatrix.hpp:130
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:195
SpinorMatrix< T > drj() const
Multiplies by drj: Q_ij -> Q_ij*dr_j. Returns new matrix (orig unchanged)
Definition SpinorMatrix.hpp:387
SpinorMatrix< T > & drj_in_place()
Multiplies by drj: Q_ij -> Q_ij*dr_j, in place.
Definition SpinorMatrix.hpp:341
SpinorMatrix< T > & operator*=(const T x)
Scalar multiplication.
Definition SpinorMatrix.hpp:157
SpinorMatrix< T > & dri_in_place()
Multiplies by dri: Q_ij -> Q_ij*dr_i, in place.
Definition SpinorMatrix.hpp:364
friend SpinorMatrix< T > mult_elements(SpinorMatrix< T > lhs, const RadialMatrix< T > &rhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:260
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:176
SpinorMatrix< T > & operator-=(const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:149
SpinorMatrix< T > & operator+=(T aI)
Adition of identity: Matrix<T> += T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:182
SpinorMatrix< double > imag() const
Returns imag part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:294
void zero()
Sets all matrix elements to zero.
Definition SpinorMatrix.hpp:111
SpinorMatrix< T > dri() const
Multiplies by dri: Q_ij -> Q_ij*dr_i. Returns new matrix (orig unchanged)
Definition SpinorMatrix.hpp:392
SpinorMatrix< T > & mult_elements_by(const SpinorMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:228
DiracSpinor operator*(const DiracSpinor &Fn) const
Action of SpinorMatrix operator on DiracSpinor. Inludes Integration: G*F = Int[G(r,...
Definition SpinorMatrix.hpp:441
SpinorMatrix< T > & drop_g()
Kills g parts of spinor matrix, in place!
Definition SpinorMatrix.hpp:120
SpinorMatrix< T > & operator+=(const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:141
SpinorMatrix< T > & invert_in_place()
Inversion (in place)
Definition SpinorMatrix.hpp:316
friend SpinorMatrix< T > mult_elements(const RadialMatrix< T > &rhs, SpinorMatrix< T > lhs)
Multiply elements (new matrix): Gij = Aij*Bij.
Definition SpinorMatrix.hpp:266
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:405
friend SpinorMatrix< T > operator-(SpinorMatrix< T > M, T aI)
Adition of identity: Matrix<T> - T : T assumed to be *Identity!
Definition SpinorMatrix.hpp:199
double dr(std::size_t sub_index) const
returns dr at position along sub grid
Definition SpinorMatrix.hpp:398
friend SpinorMatrix< T > operator*(const SpinorMatrix< T > &a, const SpinorMatrix< T > &b)
Matrix multplication: C=A*B := Cij = \sum_k Aik*Bkj. Note: integration measure not included: call ....
Definition SpinorMatrix.hpp:207
SpinorMatrix< double > real() const
Returns real part of complex matrix (changes type; returns a real matrix)
Definition SpinorMatrix.hpp:285
friend SpinorMatrix< T > operator+(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:166
SpinorMatrix< T > inverse() const
Returns inverse of matrix; original matrix unchanged.
Definition SpinorMatrix.hpp:334
SpinorMatrix< T > & mult_elements_by(const RadialMatrix< T > &rhs)
Multiply elements (in place): Gij -> Gij*Bij.
Definition SpinorMatrix.hpp:248
SpinorMatrix< T > conj() const
Returns conjugate of matrix.
Definition SpinorMatrix.hpp:275
friend SpinorMatrix< T > operator-(SpinorMatrix< T > lhs, const SpinorMatrix< T > &rhs)
Matrix adition +,-.
Definition SpinorMatrix.hpp:171
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