Skip to content

Commit 94a8441

Browse files
committed
[itops] split convmat in allocation and a convmat_inplace builder function.
1 parent e57828e commit 94a8441

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

c++/cppdlr/dlr_imtime.hpp

+72-9
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,70 @@ namespace cppdlr {
451451
template <nda::MemoryArray T, nda::Scalar S = nda::get_value_t<T>>
452452
nda::matrix<S> convmat(double beta, statistic_t statistic, T const &fc, bool time_order = false) const {
453453

454+
int n, m;
455+
456+
if constexpr (T::rank == 1) { // Scalar-valued Green's function
457+
n = r;
458+
m = r;
459+
} else if (T::rank == 3) { // Matrix-valued Green's function
460+
n = r * fc.shape(1);
461+
m = r * fc.shape(2);
462+
} else {
463+
throw std::runtime_error("Input arrays must be rank 1 (scalar-valued Green's function) or 3 (matrix-valued Green's function).");
464+
}
465+
466+
auto fconv = nda::matrix<S, nda::C_layout>(n, m); // Matrix of convolution by f
467+
convmat_inplace(nda::matrix_view<S, nda::C_layout>(fconv), beta, statistic, fc, time_order);
468+
469+
return fconv;
470+
}
471+
472+
/**
473+
* @brief Compute matrix of convolution by an imaginary time Green's function
474+
*
475+
* The convolution of f and g is defined as h(t) = (f * g)(t) = int_0^beta
476+
* f(t-t') g(t') dt', where fermionic/bosonic antiperiodicity/periodicity are
477+
* used to define the Green's functions on (-beta, 0). This method takes the
478+
* DLR coefficients of f as input and returns the matrix of convolution by f.
479+
* This matrix can be applied to the values of g on the DLR imaginary time
480+
* grid, to produce the values of h on the DLR imaginary time grid.
481+
*
482+
* By specifying the @p time_order flag, this method can be used to compute
483+
* the time-ordered convolution of f and g, defined as h(t) = (f * g)(t) =
484+
* int_0^tau f(t-t') g(t') dt'.
485+
*
486+
* The convolution matrix is constructed using the method described in
487+
* Appendix A of
488+
*
489+
* J. Kaye, H. U. R. Strand, D. Golez, "Decomposing imaginary time Feynman
490+
* diagrams using separable basis functions: Anderson impurity model strong
491+
* coupling expansion," arXiv:2307.08566 (2023).
492+
*
493+
* @param[out] fconv Convolution matrix from DLR coefficients to DLR grid
494+
* @param[in] beta Inverse temperature
495+
* @param[in] statistic Fermionic ("Fermion" or 0) or bosonic ("Boson" or 1)
496+
* @param[in] fc DLR coefficients of f
497+
* @param[in] time_order Flag for ordinary (false or ORDINARY, default) or
498+
* time-ordered (true or TIME_ORDERED) convolution
499+
*
500+
* \note Whereas the method imtime_ops::convolve takes the DLR coefficients
501+
* of f and g as input and computes their convolution h directly, this method
502+
* returns a matrix which should be applied to the DLR imaginary time grid values
503+
* of g, rather than its DLR coefficients, in to order to obtain the
504+
* convolution h. The purpose of this is to make the input and output
505+
* representations of the convolution matrix equal, which is often convenient
506+
* in practice.
507+
*
508+
* \note In the case of matrix-valued Green's functions, we think of the
509+
* matrix of convolution by f as an r*norb x r*norb matrix, or a block r x r
510+
* matrix of norb x norb blocks. Here r is the DLR rank and norb is the
511+
* number of orbital indices. This matrix would then be applied to a Green's
512+
* function g, represented as an r*norb x norb matrix, or a block r x 1
513+
* matrix of norb x norb blocks.
514+
* */
515+
template <nda::MemoryArray T, nda::Scalar S = nda::get_value_t<T>>
516+
void convmat_inplace(nda::matrix_view<S, nda::C_layout> fconv, double beta, statistic_t statistic, T const &fc, bool time_order = false) const {
517+
454518
if (r != fc.shape(0)) throw std::runtime_error("First dim of input array must be equal to DLR rank r.");
455519

456520
// TODO: implement bosonic case and remove
@@ -466,10 +530,9 @@ namespace cppdlr {
466530

467531
if constexpr (T::rank == 1) { // Scalar-valued Green's function
468532

469-
// First construct convolution matrix from DLR coefficients to DLR grid
470-
// values
471-
auto fconv = nda::matrix<S>(r, r); // Matrix of convolution by f
472-
533+
if (fconv.shape(0) != r || fconv.shape(1) != r)
534+
throw std::runtime_error("Matrix shape must be equal to DLR rank (r,r).");
535+
473536
// Diagonal contribution (given by diag(tau_k) * K(tau_k, om_l) * diag(fc_l))
474537
for (int k = 0; k < r; ++k) {
475538
for (int l = 0; l < r; ++l) { fconv(k, l) = tcf2it_v(k, l) * fc(l); }
@@ -495,16 +558,16 @@ namespace cppdlr {
495558
nda::lapack::getrs(transpose(it2cf.lu), fconv, it2cf.piv);
496559
}
497560

498-
return beta * fconv;
561+
fconv *= beta;
499562

500563
} else if (T::rank == 3) { // Matrix-valued Green's function
501564

502565
int norb1 = fc.shape(1);
503566
int norb2 = fc.shape(2);
504567

505-
// First construct convolution matrix from DLR coefficients to DLR grid
506-
// values
507-
auto fconv = nda::matrix<S>(r * norb1, r * norb2); // Matrix of convolution by f
568+
if (fconv.shape(0) != r*norb1 || fconv.shape(1) != r*norb2)
569+
throw std::runtime_error("Matrix shape must be equal to DLR rank times norbs (r*norb1,r*norb2).");
570+
508571
auto fconv_rs = nda::reshape(fconv, r, norb1, r, norb2); // Array view to index into fconv for conevenience
509572

510573
// Diagonal contribution (given by diag(tau_k) * K(tau_k, om_l) * diag(fc_l))
@@ -547,7 +610,7 @@ namespace cppdlr {
547610
for (int k = 0; k < r; ++k) { fconv_rs(_, _, k, i) = fconvtmp_rs(_, _, i, k); }
548611
}
549612

550-
return beta * fconv;
613+
fconv *= beta;
551614

552615
} else {
553616
throw std::runtime_error("Input arrays must be rank 1 (scalar-valued Green's function) or 3 (matrix-valued Green's function).");

0 commit comments

Comments
 (0)