To run this notebook locally, copy and paste the following into your Julia REPL:
using Pkg; Pkg.activate(temp=true); Pkg.add("Pluto")
BASE_URL = "https://raw.githubusercontent.com/tensor4all/T4APlutoExamples/refs/heads/main/pluto_notebooks/"
notebook = "quantics1d.jl"
url = joinpath(BASE_URL, notebook)
using Pluto; Pluto.run(notebook=download(url))
Quantics TCI of univariate function
Example 1
The first example is taken from Fig. 1 in Ritter2024.
We are going to compute the integral \ (generic function with 175 methods)mathrm{I}[f] = \int_0^{\ln 20} \mathrm{d}x f(x) $ of the function
$$f(x) = \cos\left(\frac{x}{B}\right) \cos\left(\frac{x}{4\sqrt{5}B}\right) e^{-x^2} + 2e^{-x},$$
where $B = 2^{-30}$. The integral evaluates to $\mathrm{I}[f] = 19/10 + O(e^{-1/(4B^2)})$.
We first construct a QTT representation of the function $f(x)$ as follows:
Let's examine the behaviour of $f(x)$. This function involves structure on widely different scales: rapid, incommensurate oscillations and a slowly decaying envelope. We'll use PythonPlot.jl visualisation library which uses Python library matplotlib behind the scenes.
For small $x$ we have:
For $x \in (0, 3]$ we will get:
QTT representation
We construct a QTT representation of this function on the domain $[0, \ln 20]$, discretized on a quantics grid of size $2^\mathcal{R}$ with $\mathcal{R} = 40$ bits:
TensorCrossInterpolation.TensorCI2{Float64} with rank 15
40
0.0
2.99573
2
:fused
true
TensorCrossInterpolation.CachedFunction{Float64, UInt128} with 45775 entries
15
15
16
3.12368e-8
3.02237e-8
4.02562e-8
40
Here, we've created the object ci
of type QuanticsTensorCI2{Float64}
. This can be evaluated at an linear index $i$ ($1 \le i \le 2^\mathcal{R}$) as follows:
We see that ci(i)
approximates the original f
at x = QG.grididx_to_origcoord(qgrid, i)
. Let's plot them together.
Above, one can see that the original function is interpolated very accurately.
Let's plot of $x$ vs interpolation error $|f(x) - \mathrm{ci}(x)|$ for small $x$
... and for all $x$: