use crate::basis::FiniteTempBasis;
use crate::basis_trait::Basis;
use crate::dlr::{DiscreteLehmannRepresentation, IrDlrTransform};
use crate::error::Error;
use crate::kernel::{BasisKernel, KernelProperties, SVEKernel};
use crate::matrix::Mat;
use crate::traits::{Statistics, StatisticsType};
pub trait IrBasis<S: StatisticsType>: Basis<S> {
type Kernel: KernelProperties;
fn kernel(&self) -> &Self::Kernel;
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()),
});
}
let domain = (self.kernel().ymin() * wmax, self.kernel().ymax() * wmax);
if let Some(&pole) = dlr
.poles()
.iter()
.find(|p| !(domain.0..=domain.1).contains(*p))
{
return Err(Error::OutOfDomain {
name: "poles",
value: pole,
domain,
});
}
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: BasisKernel,
S: StatisticsType + 'static,
{
type Kernel = K;
fn kernel(&self) -> &K {
FiniteTempBasis::kernel(self)
}
}
pub trait DlrFromIr<S: StatisticsType>: Sized {
fn from_ir_with_poles<B: IrBasis<S>>(basis: &B, poles: Vec<f64>) -> Result<Self, Error>;
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> {
if S::STATISTICS == Statistics::Fermionic && basis.kernel().ypower() == 1 {
return Err(Error::KernelStatisticsMismatch);
}
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();
let domain = (basis.kernel().ymin() * wmax, basis.kernel().ymax() * wmax);
for (i, &pole) in poles.iter().enumerate() {
if !pole.is_finite() {
return Err(Error::NonFiniteInput {
name: "poles",
index: vec![i],
value: pole,
});
}
if !(domain.0..=domain.1).contains(&pole) {
return Err(Error::OutOfDomain {
name: "poles",
value: pole,
domain,
});
}
}
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"
),
});
}
let v_at_poles = crate::sampling::mat_from_matrix(&Basis::evaluate_omega(basis, &poles)?)?; let s = Basis::svals(basis);
let basis_size = Basis::size(basis);
let n_poles = poles.len();
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)
}
}