# Green’s functions¶

The gfs class of TRIQS contains objects representing Green functions over real or imaginary times, real or imaginary frequencies… that can be easily manipulated at the C++ level. Here are a couple of simple examples showing the basic use of this class. Learn more in the full reference.

## Matsubara Green’s functions¶

### Creation of a simple Green’s function $$G(i\omega)$$¶

In this example, we show how to initialize the following Green’s functions:

$G(i\omega) = \frac{1}{i\omega -3}$
#include <triqs/gfs.hpp>
using namespace triqs::gfs;
using triqs::clef::placeholder;

int main() {

double beta = 1;                                       //inverse temperature
int nw      = 100;                                     //number of Matsubara frequencies
auto g      = gf<imfreq>{{beta, Fermion, nw}, {1, 1}}; //{1,1} : 1x1 Green's function

//the shortest way to fill a gf
placeholder<0> w_;
g(w_) << 1 / (w_ - 3);
std::cout << g(0) << std::endl;

g() = 0.0;
//an equivalent way
for (auto const &w : g.mesh()) g[w] = 1 / (w - 3);
std::cout << g(0) << std::endl;

//an incorrect way : throws exception as expected
//g(w) returns a const_view: () are to be used for interpolation, see bottom of the page
//for(auto const & w : g.mesh())  g(w) = 1/(w-3);
}


### Two-frequency Green’s function $$G(i\omega,i\nu)$$¶

In this example, we show how to initialize the following Green’s functions:

$G(i\omega, i\nu) = \frac{1}{i\omega + i\nu -4}$
#include <triqs/gfs.hpp>
using namespace triqs::gfs;
using triqs::clef::placeholder;

int main() {

double beta = 1;
int nw      = 100;
auto g2     = gf<cartesian_product<imfreq, imfreq>>{{{beta, Fermion, nw}, {beta, Fermion, nw}}, {1, 1}};

//the shortest way to fill a gf
placeholder<0> w_;
placeholder<1> nu_;
g2(w_, nu_) << 1 / (w_ + nu_ - 4);
std::cout << g2(0, 0) << std::endl;

g2() = 0.0;
//equivalent way
for (auto const &w : std::get<0>(g2.mesh().components()))
for (auto const &nu : std::get<1>(g2.mesh().components())) g2[{w, nu}] = 1 / (w + nu - 4);
std::cout << g2(0, 0) << std::endl;
}


## Imaginary-time Green’s functions $$G(\tau)$$¶

#include <triqs/gfs.hpp>
using namespace triqs::gfs;

int main() {
double beta = 10;  // the time interval
int n_times = 100; // we will have 100
auto g      = gf<imtime, scalar_valued>{{beta, Fermion, n_times}};
}


## Real-time Green’s functions $$G(t)$$¶

Here we create a GF defined on the time interval from tmin to tmax. If we want the value of the GF at any time to be a scalar, we use:

#include <triqs/gfs.hpp>
using namespace triqs::gfs;

int main() {
double tmin = 0, tmax = 10; // the time interval
int n_times = 100;          // we will have 100 points
auto g      = gf<retime, scalar_valued>{{tmin, tmax, n_times}};
}


If we need a matrix of size n by m, we use:

#include <triqs/gfs.hpp>
using namespace triqs::gfs;

int main() {
double tmin = 0, tmax = 10;            // the time interval
const int n = 2, m = 2, n_times = 100; // we will have 100 points
auto g = gf<retime, matrix_valued>{{tmin, tmax, n_times}, {n, m}};
}


Or a tensor!

#include <triqs/gfs.hpp>
using namespace triqs::gfs;

int main() {
double tmin = 0, tmax = 10; // the time interval
double beta = 1;
int n_times = 100; // we will have 100 pointspoints
auto g      = gf<cartesian_product<retime, imtime>, tensor_valued<3>>{{{tmin, tmax, n_times}, {beta, Fermion, n_times}}, {2, 2, 2}};
}


### Creation of a two real time GF $$G(t,t')$$¶

#include <triqs/gfs.hpp>
using namespace triqs::gfs;
int main() {
double tmin = 0, tmax = 1.0;
int nt = 100;
auto g = gf<cartesian_product<retime, retime>, scalar_valued>{{{tmin, tmax, nt}, {tmin, tmax, nt}}};
}


### How to fill a GF with placeholders¶

#include <triqs/gfs.hpp>
using namespace triqs::gfs;
int main() {
double tmin = 0, tmax = 1.0;
int nt = 100;
auto g = gf<cartesian_product<retime, retime>, scalar_valued>{{{tmin, tmax, nt}, {tmin, tmax, nt}}};
triqs::clef::placeholder<0> t1_;
triqs::clef::placeholder<1> t2_;
g(t1_, t2_) << 2 * t1_;
}


### How to interpolate the GF value at a point of the domain¶

You simply have to call the GF with the coordinates of the point:

#include <triqs/gfs.hpp>
using namespace triqs::gfs;
int main() {
double tmin = 0, tmax = 1.0;
int nt = 100;
auto g = gf<cartesian_product<retime, retime>, scalar_valued>{{{tmin, tmax, nt}, {tmin, tmax, nt}}};
triqs::clef::placeholder<0> t1_;
triqs::clef::placeholder<1> t2_;
g(t1_, t2_) << 2. * t1_;
std::cout << g(0.24, 0.36) << std::endl;
}