1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! Discrete Lehmann representations built from an IR basis
//!
//! The DLR itself needs no IR basis (see
//! [`DiscreteLehmannRepresentation::new`]). This module connects the two:
//! [`IrBasis`] is a [`Basis`] with a kernel (the IR basis
//! [`FiniteTempBasis`]), [`DlrFromIr`] builds a DLR whose poles come from such
//! a basis, and [`IrBasis::dlr_transform`] the linear map between the
//! coefficients of the basis and of any DLR on the same domain.
use crate::basis::FiniteTempBasis;
use crate::basis_trait::Basis;
use crate::dlr::{DiscreteLehmannRepresentation, IrDlrTransform};
use crate::error::Error;
use crate::kernel::{CentrosymmKernel, KernelProperties};
use crate::matrix::Mat;
use crate::traits::{Statistics, StatisticsType};
/// A basis with a kernel: the IR basis [`FiniteTempBasis`]
///
/// The kernel supplies the pole weights `w(β, ω)` of a DLR built from the
/// basis. A DLR is not an `IrBasis`, so no DLR can be built from a DLR.
pub trait IrBasis<S: StatisticsType>: Basis<S> {
/// Kernel type
type Kernel: KernelProperties;
/// The kernel the basis was built from
fn kernel(&self) -> &Self::Kernel;
/// The linear map between the coefficients of this basis and of `dlr`.
///
/// # Errors
/// * [`Error::KernelStatisticsMismatch`] if the kernel of this basis does not
/// support the statistics
/// * [`Error::InvalidParameter`] named `basis` if the β of this basis differs from that
/// of `dlr`, or its kernel weight vanishes at a pole where the DLR
/// weight does not
/// * [`Error::OutOfDomain`] named `poles` for a pole of `dlr` outside
/// [-ωmax, ωmax] of this basis
fn dlr_transform(&self, dlr: &DiscreteLehmannRepresentation<S>) -> Result<IrDlrTransform, Error>
where
S: 'static,
{
if S::STATISTICS == Statistics::Fermionic && self.kernel().ypower() == 1 {
return Err(Error::KernelStatisticsMismatch);
}
let (beta, wmax) = (Basis::beta(self), Basis::wmax(self));
let rel = |a: f64, b: f64| (a - b).abs() <= 1e-12 * a.abs().max(b.abs());
if !rel(beta, dlr.beta()) {
return Err(Error::InvalidParameter {
name: "basis",
value: format!("beta = {beta:?}"),
reason: format!("must equal beta = {:?} of the DLR", dlr.beta()),
});
}
if let Some(&pole) = dlr.poles().iter().find(|p| !(-wmax..=wmax).contains(*p)) {
return Err(Error::OutOfDomain {
name: "poles",
value: pole,
domain: (-wmax, wmax),
});
}
let mut ratio = Vec::with_capacity(dlr.poles().len());
for (&pole, &w) in dlr.poles().iter().zip(dlr.pole_weights()) {
let wir = self.kernel().regularizer::<S>(beta, pole);
if w == wir {
ratio.push(1.0);
} else if wir == 0.0 {
return Err(Error::InvalidParameter {
name: "basis",
value: format!("kernel weight 0 at pole {pole:?}"),
reason: "must not vanish where the DLR weight does not".to_string(),
});
} else {
ratio.push(w / wir);
}
}
let v_at_poles =
crate::sampling::mat_from_matrix(&Basis::evaluate_omega(self, dlr.poles())?)?;
let s = Basis::svals(self);
let fitmat = Mat::<f64>::from_fn([Basis::size(self), dlr.poles().len()], |idx| {
let (l, i) = (idx[0], idx[1]);
-s[l] * v_at_poles[[i, l]] * ratio[i]
});
Ok(IrDlrTransform::from_matrix(fitmat))
}
}
impl<K, S> IrBasis<S> for FiniteTempBasis<K, S>
where
K: KernelProperties + CentrosymmKernel + Clone + 'static,
S: StatisticsType + 'static,
{
type Kernel = K;
fn kernel(&self) -> &K {
FiniteTempBasis::kernel(self)
}
}
/// Constructors of a [`DiscreteLehmannRepresentation`] from an IR basis
///
/// The returned DLR carries the [`IrDlrTransform`] of its source basis, so
/// [`DiscreteLehmannRepresentation::from_ir_nd`] and
/// [`DiscreteLehmannRepresentation::to_ir_nd`] work on it.
pub trait DlrFromIr<S: StatisticsType>: Sized {
/// Create a DLR from an IR basis with custom poles
///
/// The tau-domain pole basis is built from the logistic representation, while
/// kernel-specific regularizers are preserved for compatible kernels.
///
/// # Arguments
/// * `basis` - The IR basis to construct DLR from
/// * `poles` - Pole positions on the real-frequency axis, in
/// [-ωmax, ωmax] of `basis`
///
/// # Errors
/// * [`Error::KernelStatisticsMismatch`] if the kernel does not support
/// the requested statistics (e.g. `RegularizedBoseKernel` with fermionic
/// statistics)
/// * [`Error::EmptyInput`] if `poles` is empty
/// * [`Error::NonFiniteInput`] if a pole is NaN or infinite, and
/// [`Error::OutOfDomain`] if a pole is outside [-ωmax, ωmax] of `basis`,
/// both named `poles`, for the first such pole
/// * [`Error::NotSupported`] for a bosonic pole at 0 if the kernel has a
/// `ypower` other than 0 or 1 (the DLR knows the limit at 0 for those
/// only)
///
/// Duplicate poles are accepted. They make [`DiscreteLehmannRepresentation::from_ir_nd`]
/// ill-conditioned: the coefficients of equal poles are not unique,
/// although the round trip through [`DiscreteLehmannRepresentation::to_ir_nd`] still recovers the
/// IR coefficients.
fn from_ir_with_poles<B: IrBasis<S>>(basis: &B, poles: Vec<f64>) -> Result<Self, Error>;
/// Create a DLR from an IR basis with its default pole locations
///
/// Uses the default omega sampling points from the basis.
///
/// # Arguments
/// * `basis` - The IR basis to construct DLR from
///
/// # Errors
/// * [`Error::InsufficientDefaultPoles`] if the basis has fewer default
/// poles than functions. This can happen with certain kernel types
/// (e.g. `RegularizedBoseKernel`) due to numerical precision limitations
/// in root finding.
/// * [`Error::KernelStatisticsMismatch`] as in [`Self::from_ir_with_poles`]
/// * [`Error::NotSupported`] as in [`Self::from_ir_with_poles`]: for a default
/// pole at 0 of a bosonic basis whose kernel has a `ypower` other than
/// 0 or 1
/// * The errors of
/// [`Basis::default_omega_sampling_points`]
/// (NotSupported for an SVE with too few singular functions)
fn from_ir<B: IrBasis<S>>(basis: &B) -> Result<Self, Error>;
}
impl<S: StatisticsType + 'static> DlrFromIr<S> for DiscreteLehmannRepresentation<S> {
fn from_ir_with_poles<B: IrBasis<S>>(basis: &B, poles: Vec<f64>) -> Result<Self, Error> {
// RegularizedBoseKernel (ypower == 1) is meaningful only for bosonic
// statistics; its regularizer panics for fermionic input. Reject the
// combination before computing anything.
if S::STATISTICS == Statistics::Fermionic && basis.kernel().ypower() == 1 {
return Err(Error::KernelStatisticsMismatch);
}
// Without poles the fitting matrix has no columns and the DLR has no
// functions.
if poles.is_empty() {
return Err(Error::EmptyInput { name: "poles" });
}
let beta = Basis::beta(basis);
let wmax = Basis::wmax(basis);
let accuracy = Basis::accuracy(basis);
let kernel_ypower = basis.kernel().ypower();
// Each pole must be finite and in [-ωmax, ωmax], the domain of V_l.
// evaluate_omega below checks the domain of the basis again.
for (i, &pole) in poles.iter().enumerate() {
if !pole.is_finite() {
return Err(Error::NonFiniteInput {
name: "poles",
index: vec![i],
value: pole,
});
}
if !(-wmax..=wmax).contains(&pole) {
return Err(Error::OutOfDomain {
name: "poles",
value: pole,
domain: (-wmax, wmax),
});
}
}
// A bosonic pole at 0 is evaluated through its finite limit, which is
// known for ypower 0 (regularizer tanh(βω/2)) and 1 (regularizer ω)
// only; see zero_pole_tau_limit.
if S::STATISTICS == Statistics::Bosonic
&& !(0..=1).contains(&kernel_ypower)
&& poles.contains(&0.0)
{
return Err(Error::NotSupported {
what: format!(
"a bosonic DLR pole at 0 for a kernel with ypower = {kernel_ypower}: \
its limit is known for ypower 0 and 1 only"
),
});
}
// Compute fitting matrix: fitmat = -s · V(poles)
// This transforms DLR coefficients to IR coefficients
let v_at_poles = crate::sampling::mat_from_matrix(&Basis::evaluate_omega(basis, &poles)?)?; // shape: [n_poles, basis_size]
let s = Basis::svals(basis); // Non-normalized singular values (same as C++)
let basis_size = Basis::size(basis);
let n_poles = poles.len();
// fitmat[l, i] = -s[l] * V_l(pole[i])
// C++: fitmat = (-A_array * s_array.replicate(1, A.cols())).matrix()
let fitmat = Mat::<f64>::from_fn([basis_size, n_poles], |idx| {
let l = idx[0];
let i = idx[1];
-s[l] * v_at_poles[[i, l]]
});
let ir = IrDlrTransform::from_matrix(fitmat);
let regularizers: Vec<f64> = poles
.iter()
.map(|&pole| basis.kernel().regularizer::<S>(beta, pole))
.collect();
Ok(DiscreteLehmannRepresentation::from_parts(
beta,
wmax,
accuracy,
poles,
regularizers,
kernel_ypower,
Some(ir),
))
}
fn from_ir<B: IrBasis<S>>(basis: &B) -> Result<Self, Error> {
let poles = Basis::default_omega_sampling_points(basis)?;
let basis_size = Basis::size(basis);
if basis_size > poles.len() {
return Err(Error::InsufficientDefaultPoles {
basis_size,
n_poles: poles.len(),
});
}
Self::from_ir_with_poles(basis, poles)
}
}