<jupyter_start><jupyter_text>Sparse samplingIn this page, we describe how to infer IR expansion coefficients using the sparse-sampling techiniques.<jupyter_code>import sparse_ir
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 15<jupyter_output><empty_output><jupyter_text>SetupWe consider a semicircular spectral modeli (full bandwidth of 2):$$\rho(\omega) = \frac{2}{\pi}\sqrt{1-\omega^2}.$$First, we compute the numerically exact expansion coefficients $g_l$.Below, we plot the data for even $l$.<jupyter_code>def rho(omega):
    if np.abs(omega) < 1:
        return (2/np.pi) * np.sqrt(1-omega**2)
    else:
        return 0.0

beta = 10000
wmax = 1
basis = sparse_ir.FiniteTempBasis("F", beta, wmax, eps=1e-15)

rhol = basis.v.overlap(rho)
gl = - basis.s * rhol

ls = np.arange(basis.size)
plt.semilogy(ls[::2], np.abs(gl[::2]), marker="x", label=r"$|g_l|$")
plt.xlabel(r"$l$")
plt.ylabel(r"$|g_l|$")
plt.ylim([1e-15, 10])
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>From sampling timesWe first create a `TauSampling` object for the default sampling times.<jupyter_code>smpl_tau = sparse_ir.TauSampling(basis)
print("sampling times: ", smpl_tau.sampling_points)
print("Condition number: ", smpl_tau.cond)<jupyter_output><empty_output><jupyter_text>The condition number is around 50, indicating that 1--2 significant digits may be lost in a fit from the sampling times. Let us fit from the sampling times!<jupyter_code># Evaluate G(τ) on the sampling times
gtau_smpl = smpl_tau.evaluate(gl)
plt.plot(smpl_tau.sampling_points, gtau_smpl, marker="x", ls="")
plt.xlabel(r"$\tau$")
plt.ylabel(r"$G(\tau)$")
plt.show()

# Fit G(τ) on the sampling times
gl_reconst_from_tau = smpl_tau.fit(gtau_smpl)<jupyter_output><empty_output><jupyter_text>From sampling frequenciesWe create a `MatsubaraSampling` object for the default sampling frequencies.<jupyter_code>smpl_matsu = sparse_ir.MatsubaraSampling(basis)
print("sampling frequencies: ", smpl_matsu.sampling_points)
print("Condition number: ", smpl_matsu.cond)<jupyter_output><empty_output><jupyter_text>The condition number is slightly larger than that for the sampling times.<jupyter_code># Evaluate G(iv) on the sampling frequencies
giv_smpl = smpl_matsu.evaluate(gl)
plt.plot((np.pi/beta)*smpl_matsu.wn, giv_smpl.imag, marker="x", ls="")
plt.xlabel(r"$\nu$")
plt.ylabel(r"Im $G(\mathrm{i}\nu)$")
plt.show()

# Fit G(τ) on the sampling times
gl_reconst_from_matsu = smpl_matsu.fit(giv_smpl)<jupyter_output><empty_output><jupyter_text>Comparison with exact resultsWe now compare the reconstructed expansion coefficients with the exact one. For clarity, we plot only the data for even $l$.<jupyter_code>plt.semilogy(ls[::2], np.abs(gl[::2]), marker="", ls="-", label="Exact")
plt.semilogy(ls[::2], np.abs(gl_reconst_from_tau[::2]), marker="x", label="from sampling times")
plt.semilogy(ls[::2], np.abs(gl_reconst_from_matsu[::2]), marker="+", label="from sampling frequencies")
plt.xlabel(r"$l$")
plt.ylabel(r"$|g_l|$")
plt.ylim([1e-17, 10])
plt.legend(frameon=False)
plt.show()<jupyter_output><empty_output><jupyter_text>We saw a perfect match! Let us plot the differences from the exact one.<jupyter_code>plt.semilogy(ls[::2], np.abs((gl_reconst_from_tau-gl)[::2]), marker="x", label="from sampling times")
plt.semilogy(ls[::2], np.abs((gl_reconst_from_matsu-gl)[::2]), marker="+", label="from sampling frequencies")
plt.xlabel(r"$l$")
plt.ylabel(r"Error in $g_l$")
plt.ylim([1e-18, 10])
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15712682379349047, 'fraction_numerical': 0.014590347923681257, 'mean_word_length': 7.083900226757369, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '612133', 'license': 'MIT', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1320, 'n_words': 289, 'path': 'src/sparse_sampling_demo_py.ipynb', 'repo_name': 'SpM-lab/sparse-ir-tutorial', 'size': '3564'}