Skip to content

Commit 02b3772

Browse files
committed
Begin example usage doc
1 parent 28e26cc commit 02b3772

File tree

13 files changed

+78
-40
lines changed

13 files changed

+78
-40
lines changed

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Documenter
44
DocMeta.setdocmeta!(SparseIR, :DocTestSetup, :(using SparseIR); recursive=true)
55

66
makedocs(;
7-
modules=[SparseIR],
7+
# modules=[SparseIR],
88
authors="Samuel Badr <samuel.badr@gmail.com> and contributors",
99
repo="https://github.com/SpM-lab/SparseIR.jl/blob/{commit}{path}#{line}",
1010
sitename="SparseIR.jl",

docs/src/guide.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\newcommand{\var}{\mathrm{Var}}
2+
3+
# Example usage and detailed explanation
4+
5+
We will explain the inner workings of `SparseIR.jl` by means of an example use case, adapted from the `sparse-ir` paper.
6+
<!-- TODO: link to paper once it's released -->
7+
8+
## Problem statement
9+
10+
> Let us perform self-consistent second-order perturbation theory for the single impurity Anderson model at finite temperature.
11+
> Its Hamiltonian is given by
12+
> $$
13+
> H = U c^\dagger_\uparrow c^\dagger_\downarrow c_\downarrow c_\uparrow + \sum_{p\sigma} \big(V_{p\sigma} f_{p\sigma}^\dagger c_\sigma + V_{p\sigma}^* c_\sigma^\dagger c_\sigma^\dagger\big) + \sum_{p\sigma} \epsilon_{p} f_{p\sigma}^\dagger f_{p\sigma}
14+
> $$
15+
> where $U$ is the electron interaction strength, $c_\sigma$ annihilates an electron on the impurity, $f_{p\sigma}$ annihilates an electron in the bath, $\dagger$ denotes the Hermitian conjugate, $p\in\mathbb R$ is bath momentum, and $\sigma\in\{\uparrow, \downarrow\}$ is spin.
16+
> The hybridization strength $V_{p\sigma}$ and bath energies $\epsilon_p$ are chosen such that the non-interacting density of states is semi-elliptic with a half-bandwidth of one, $\rho_0(\omega) = \frac2\pi\sqrt{1-\omega^2}$, $U=1.2$, $\beta=10$, and the system is assumed to be half-filled.
17+
18+
## Treatment
19+
20+
We first import `SparseIR` and construct an appropriate basis ($\omega_\mathrm{max} = 8$ should be more than enough for this example):
21+
```julia-repl
22+
julia> using SparseIR
23+
24+
julia> basis = FiniteTempBasis(fermion, 10, 8)
25+
FiniteTempBasis{LogisticKernel, Float64}(fermion, 10.0, 8.0)
26+
```
27+
There's quite a lot happening behind the scenes in this first innocuous-looking statement, so let's break it down:
28+
Because we did not specify otherwise, the constructor chose the analytic continuation kernel for fermions, `LogisticKernel(Λ=80.0)`, defined by
29+
$$
30+
K(x, y) = \frac{e^{-Λ y (x + 1) / 2}}{1 + e^{-Λ y}},
31+
$$
32+
for us.
33+
34+
Central is the _singular value expansion_'s (SVE) computation, which is handled by the function `compute_sve`:
35+
36+
It first constructs

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ Documentation for [SparseIR](https://github.com/SpM-lab/SparseIR.jl).
1111

1212
```@autodocs
1313
Modules = [SparseIR]
14+
Private = false
1415
```

src/_linalg.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Truncated rank-revealing QR decomposition with full column pivoting.
1111
1212
Decomposes a `(m, n)` matrix `A` into the product:
1313
14-
A[:,piv] == Q @ R
14+
A[:,piv] == Q * R
1515
1616
where `Q` is an `(m, k)` isometric matrix, `R` is a `(k, n)` upper
1717
triangular matrix, `piv` is a permutation vector, and `k` is chosen such
@@ -92,9 +92,9 @@ end
9292
"""
9393
Truncated singular value decomposition.
9494
95-
Decomposes a `(m, n)` matrix `A` into the product:
95+
Decomposes an `(m, n)` matrix `A` into the product:
9696
97-
A == U @ (s[:,None] * VT)
97+
A == U * (s .* VT)
9898
9999
where `U` is a `(m, k)` matrix with orthogonal columns, `VT` is a `(k, n)`
100100
matrix with orthogonal rows and `s` are the singular values, a set of `k`

src/augment.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ they used:
77
88
where P_l[x] is the $l$-th Legendre polynomial.
99
10-
In this class, the basis functions are defined by
10+
In this type, the basis functions are defined by
1111
1212
U_l(\tau) \equiv c_l (\sqrt{2l+1}/beta) * P_l[x(\tau)],
1313
14-
where c_l are additional l-depenent constant factors.
14+
where c_l are additional l-dependent constant factors.
1515
By default, we take c_l = 1, which reduces to the original definition.
1616
"""
1717
struct LegendreBasis{T<:AbstractFloat} <: AbstractBasis

src/basis.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ getstatistics(basis::AbstractBasis) = basis.statistics
1010
Intermediate representation (IR) basis in reduced variables.
1111
1212
For a continuation kernel `K` from real frequencies, `ω ∈ [-ωmax, ωmax]`, to
13-
imaginary time, `τ ∈ [0, β]`, this class stores the truncated singular
13+
imaginary time, `τ ∈ [0, β]`, this type stores the truncated singular
1414
value expansion or IR basis:
1515
1616
K(x, y) ≈ sum(u[l](x) * s[l] * v[l](y) for l in range(L))
@@ -66,7 +66,6 @@ struct DimensionlessBasis{K<:AbstractKernel,T<:AbstractFloat} <: AbstractBasis
6666
uhat::PiecewiseLegendreFTVector{T}
6767
s::Vector{T}
6868
v::PiecewiseLegendrePolyVector{T}
69-
sampling_points_v::Vector{T}
7069
statistics::Statistics
7170
end
7271

@@ -91,9 +90,7 @@ function DimensionlessBasis(
9190
# since it has lower relative error.
9291
even_odd = Dict(fermion => :odd, boson => :even)[statistics]
9392
= hat.(u, even_odd; n_asymp=conv_radius(kernel))
94-
rts = roots(last(v))
95-
sampling_points_v = [v.xmin; (rts[begin:(end - 1)] .+ rts[(begin + 1):end]) / 2; v.xmax]
96-
return DimensionlessBasis(kernel, u, û, s, v, sampling_points_v, statistics)
93+
return DimensionlessBasis(kernel, u, û, s, v, statistics)
9794
end
9895

9996
"""
@@ -114,7 +111,7 @@ end
114111
Intermediate representation (IR) basis for given temperature.
115112
116113
For a continuation kernel `K` from real frequencies, `ω ∈ [-ωmax, ωmax]`, to
117-
imaginary time, `τ ∈ [0, beta]`, this class stores the truncated singular
114+
imaginary time, `τ ∈ [0, beta]`, this type stores the truncated singular
118115
value expansion or IR basis:
119116
120117
K(τ, ω) ≈ sum(u[l](τ) * s[l] * v[l](ω) for l in 1:L)

src/basis_set.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
FiniteTempBasisSet
33
4-
Class for holding IR bases and sparse-sampling objects.
4+
Type for holding IR bases and sparse-sampling objects.
55
6-
An object of this class holds IR bases for fermions and bosons
6+
An object of this type holds IR bases for fermions and bosons
77
and associated sparse-sampling objects.
88
99
# Fields

src/kernel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ on ``[0, 1] × [0, 1]`` that is given as either:
111111
```math
112112
K_\mathrm{red}(x, y) = K(x, y) \pm K(x, -y)
113113
```
114-
This kernel is what this class represents. The full singular functions can
114+
This kernel is what this type represents. The full singular functions can
115115
be reconstructed by (anti-)symmetrically continuing them to the negative
116116
axis.
117117
"""

src/poly.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ intervals ``S[i] = [a[i], a[i+1]]``, where on each interval the function
88
is expanded in scaled Legendre polynomials.
99
"""
1010
struct PiecewiseLegendrePoly{T} <: Function
11-
nsegments::Int
1211
polyorder::Int
1312
xmin::T
1413
xmax::T
@@ -24,20 +23,21 @@ struct PiecewiseLegendrePoly{T} <: Function
2423
norm::Vector{T}
2524

2625
function PiecewiseLegendrePoly(
27-
nsegments, polyorder, xmin, xmax, knots, Δx, data, symm, l, xm, inv_xs, norm
26+
polyorder::Integer, xmin::AbstractFloat, xmax::AbstractFloat, knots::AbstractVector,
27+
Δx::AbstractVector, data::AbstractMatrix, symm::Integer, l::Integer,
28+
xm::AbstractVector, inv_xs::AbstractVector, norm::AbstractVector,
2829
)
2930
!any(isnan, data) || error("data contains NaN")
30-
size(knots) == (nsegments + 1,) || error("Invalid knots array")
3131
issorted(knots) || error("knots must be monotonically increasing")
3232
Δx diff(knots) || error("Δx must work with knots")
3333
return new{eltype(knots)}(
34-
nsegments, polyorder, xmin, xmax, knots, Δx, data, symm, l, xm, inv_xs, norm
34+
polyorder, xmin, xmax, knots, Δx, data, symm, l, xm, inv_xs, norm
3535
)
3636
end
3737
end
3838

3939
function PiecewiseLegendrePoly(data, p::PiecewiseLegendrePoly; symm=0)
40-
return PiecewiseLegendrePoly(p.nsegments, p.polyorder, p.xmin, p.xmax, p.knots, p.Δx,
40+
return PiecewiseLegendrePoly(p.polyorder, p.xmin, p.xmax, p.knots, p.Δx,
4141
data, symm, p.l, p.xm, p.inv_xs, p.norm)
4242
end
4343

@@ -50,7 +50,7 @@ function PiecewiseLegendrePoly(
5050
inv_xs = 2 ./ Δx
5151
norm = sqrt.(inv_xs)
5252

53-
return PiecewiseLegendrePoly(nsegments, polyorder, first(knots), last(knots), knots,
53+
return PiecewiseLegendrePoly(polyorder, first(knots), last(knots), knots,
5454
Δx, data, symm, l, xm, inv_xs, norm)
5555
end
5656

@@ -236,7 +236,7 @@ struct PowerModel{T<:AbstractFloat}
236236
moments::Vector{T}
237237
end
238238

239-
const _DEFAULT_GRID = [
239+
const DEFAULT_GRID = [
240240
range(0; length=2^6)
241241
trunc.(Int, exp2.(range(6, 25; length=16 * (25 - 6) + 1)))
242242
]
@@ -332,11 +332,11 @@ function hat(poly::PiecewiseLegendrePoly, freq; n_asymp=Inf)
332332
end
333333

334334
"""
335-
findextrema(polyFT::PiecewiseLegendreFT, part=nothing, grid=_DEFAULT_GRID)
335+
findextrema(polyFT::PiecewiseLegendreFT, part=nothing, grid=DEFAULT_GRID)
336336
337337
Obtain extrema of fourier-transformed polynomial.
338338
"""
339-
function findextrema(polyFT::PiecewiseLegendreFT, part=nothing, grid=_DEFAULT_GRID)
339+
function findextrema(polyFT::PiecewiseLegendreFT, part=nothing, grid=DEFAULT_GRID)
340340
f = _func_for_part(polyFT, part)
341341
x₀ = _discrete_extrema(f, grid)
342342
x₀ .= 2x₀ .+ polyFT.ζ
@@ -359,7 +359,7 @@ function _func_for_part(polyFT::PiecewiseLegendreFT, part=nothing)
359359
end
360360

361361
function _discrete_extrema(f::Function, xgrid)
362-
fx = f.(xgrid)
362+
fx = Float64.(f.(xgrid))
363363
absfx = abs.(fx)
364364

365365
# Forward differences: derivativesignchange[i] now means that the secant changes sign

src/sampling.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
AbstractSampling
33
4-
Abstract class for sparse sampling.
4+
Abstract type for sparse sampling.
55
66
Encodes the "basis transformation" of a propagator from the truncated IR
77
basis coefficients `G_ir[l]` to time/frequency sampled on sparse points

0 commit comments

Comments
 (0)