use super::sparse::SpectralCsrMatrix;
use crate::rng::LcgRng;
#[must_use]
pub fn anderson_hamiltonian(n: usize, disorder: f64, seed: u64) -> (Vec<f64>, Vec<f64>) {
let mut rng = LcgRng::new(seed);
let diagonal: Vec<f64> = (0..n).map(|_| disorder * (rng.uniform() - 0.5)).collect();
let off_diag = vec![-1.0; n - 1];
(diagonal, off_diag)
}
#[must_use]
pub fn anderson_potential(n: usize, disorder: f64, seed: u64) -> Vec<f64> {
let mut rng = LcgRng::new(seed);
(0..n).map(|_| disorder * (rng.uniform() - 0.5)).collect()
}
#[must_use]
pub fn lyapunov_exponent(potential: &[f64], energy: f64) -> f64 {
let n = potential.len();
if n == 0 {
return 0.0;
}
let mut v_prev = 0.0f64;
let mut v_curr = 1.0f64;
let mut log_growth = 0.0f64;
for &v_i in potential {
let v_next = (energy - v_i).mul_add(v_curr, -v_prev);
v_prev = v_curr;
v_curr = v_next;
let norm = v_curr.hypot(v_prev);
if norm > 0.0 {
log_growth += norm.ln();
v_curr /= norm;
v_prev /= norm;
}
}
log_growth / n as f64
}
#[must_use]
pub fn lyapunov_averaged(
n_sites: usize,
disorder: f64,
energy: f64,
n_realizations: usize,
base_seed: u64,
) -> f64 {
let mut sum = 0.0;
for r in 0..n_realizations {
let pot = anderson_potential(n_sites, disorder, base_seed + r as u64 * 1000);
sum += lyapunov_exponent(&pot, energy);
}
sum / n_realizations as f64
}
#[must_use]
pub fn anderson_2d(lx: usize, ly: usize, disorder: f64, seed: u64) -> SpectralCsrMatrix {
let n = lx * ly;
let mut rng = LcgRng::new(seed);
let mut row_ptr = Vec::with_capacity(n + 1);
let mut col_idx = Vec::new();
let mut values = Vec::new();
let idx = |ix: usize, iy: usize| -> usize { ix * ly + iy };
row_ptr.push(0);
for ix in 0..lx {
for iy in 0..ly {
let i = idx(ix, iy);
let v_i = disorder * (rng.uniform() - 0.5);
let mut entries: Vec<(usize, f64)> = Vec::new();
if ix > 0 {
entries.push((idx(ix - 1, iy), -1.0));
}
if iy > 0 {
entries.push((idx(ix, iy - 1), -1.0));
}
entries.push((i, v_i));
if iy + 1 < ly {
entries.push((idx(ix, iy + 1), -1.0));
}
if ix + 1 < lx {
entries.push((idx(ix + 1, iy), -1.0));
}
entries.sort_by_key(|&(c, _)| c);
for (c, v) in entries {
col_idx.push(c);
values.push(v);
}
row_ptr.push(col_idx.len());
}
}
SpectralCsrMatrix {
n,
row_ptr,
col_idx,
values,
}
}
#[must_use]
pub fn clean_2d_lattice(lx: usize, ly: usize) -> SpectralCsrMatrix {
anderson_2d(lx, ly, 0.0, 0)
}
#[must_use]
pub fn anderson_3d(lx: usize, ly: usize, lz: usize, disorder: f64, seed: u64) -> SpectralCsrMatrix {
let n = lx * ly * lz;
let mut rng = LcgRng::new(seed);
let mut row_ptr = Vec::with_capacity(n + 1);
let mut col_idx = Vec::new();
let mut values = Vec::new();
let idx = |ix: usize, iy: usize, iz: usize| -> usize { (ix * ly + iy) * lz + iz };
row_ptr.push(0);
for ix in 0..lx {
for iy in 0..ly {
for iz in 0..lz {
let v_i = disorder * (rng.uniform() - 0.5);
let mut entries: Vec<(usize, f64)> = Vec::new();
if ix > 0 {
entries.push((idx(ix - 1, iy, iz), -1.0));
}
if iy > 0 {
entries.push((idx(ix, iy - 1, iz), -1.0));
}
if iz > 0 {
entries.push((idx(ix, iy, iz - 1), -1.0));
}
entries.push((idx(ix, iy, iz), v_i));
if iz + 1 < lz {
entries.push((idx(ix, iy, iz + 1), -1.0));
}
if iy + 1 < ly {
entries.push((idx(ix, iy + 1, iz), -1.0));
}
if ix + 1 < lx {
entries.push((idx(ix + 1, iy, iz), -1.0));
}
entries.sort_by_key(|&(c, _)| c);
for (c, v) in entries {
col_idx.push(c);
values.push(v);
}
row_ptr.push(col_idx.len());
}
}
}
SpectralCsrMatrix {
n,
row_ptr,
col_idx,
values,
}
}
#[must_use]
pub fn clean_3d_lattice(l: usize) -> SpectralCsrMatrix {
anderson_3d(l, l, l, 0.0, 0)
}
#[must_use]
pub fn anderson_3d_correlated(
l: usize,
disorder: f64,
xi_corr: f64,
seed: u64,
) -> SpectralCsrMatrix {
let n = l * l * l;
let mut rng = LcgRng::new(seed);
let raw: Vec<f64> = (0..n).map(|_| disorder * (rng.uniform() - 0.5)).collect();
let potential = if xi_corr < 0.01 {
raw
} else {
let idx_to_xyz = |i: usize| -> (usize, usize, usize) {
let iz = i % l;
let iy = (i / l) % l;
let ix = i / (l * l);
(ix, iy, iz)
};
let reach = (3.0 * xi_corr).ceil() as i64;
let mut smoothed = vec![0.0; n];
for (i, s_val) in smoothed.iter_mut().enumerate() {
let (ix, iy, iz) = idx_to_xyz(i);
let mut sum = 0.0;
let mut norm = 0.0;
for dx in -reach..=reach {
for dy in -reach..=reach {
for dz in -reach..=reach {
let jx = ix as i64 + dx;
let jy = iy as i64 + dy;
let jz = iz as i64 + dz;
if (0..l as i64).contains(&jx)
&& (0..l as i64).contains(&jy)
&& (0..l as i64).contains(&jz)
{
let j = (jx as usize * l + jy as usize) * l + jz as usize;
let r = ((dx * dx + dy * dy + dz * dz) as f64).sqrt();
let kernel = (-r / xi_corr).exp();
sum = kernel.mul_add(raw[j], sum);
norm += kernel;
}
}
}
}
*s_val = sum / norm;
}
let var: f64 = {
let mean = smoothed.iter().sum::<f64>() / n as f64;
smoothed.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / n as f64
};
let target_var = disorder * disorder / 12.0;
let scale = if var > 1e-30 {
(target_var / var).sqrt()
} else {
1.0
};
smoothed.iter().map(|v| v * scale).collect()
};
let idx = |ix: usize, iy: usize, iz: usize| -> usize { (ix * l + iy) * l + iz };
let mut row_ptr = Vec::with_capacity(n + 1);
let mut col_idx = Vec::new();
let mut values = Vec::new();
row_ptr.push(0);
for ix in 0..l {
for iy in 0..l {
for iz in 0..l {
let site = idx(ix, iy, iz);
let mut entries: Vec<(usize, f64)> = Vec::new();
if ix > 0 {
entries.push((idx(ix - 1, iy, iz), -1.0));
}
if iy > 0 {
entries.push((idx(ix, iy - 1, iz), -1.0));
}
if iz > 0 {
entries.push((idx(ix, iy, iz - 1), -1.0));
}
entries.push((site, potential[site]));
if iz + 1 < l {
entries.push((idx(ix, iy, iz + 1), -1.0));
}
if iy + 1 < l {
entries.push((idx(ix, iy + 1, iz), -1.0));
}
if ix + 1 < l {
entries.push((idx(ix + 1, iy, iz), -1.0));
}
entries.sort_by_key(|&(c, _)| c);
for (c, v) in entries {
col_idx.push(c);
values.push(v);
}
row_ptr.push(col_idx.len());
}
}
}
SpectralCsrMatrix {
n,
row_ptr,
col_idx,
values,
}
}
#[derive(Debug, Clone, Copy)]
pub struct AndersonSweepPoint {
pub w: f64,
pub r_mean: f64,
pub r_stderr: f64,
}
#[must_use]
pub fn anderson_sweep_averaged(
l: usize,
w_min: f64,
w_max: f64,
n_w: usize,
n_realizations: usize,
base_seed: u64,
) -> Vec<AndersonSweepPoint> {
use super::lanczos::{lanczos, lanczos_eigenvalues};
use super::stats::level_spacing_ratio;
let n = l * l * l;
let mut results = Vec::with_capacity(n_w);
for wi in 0..n_w {
let w = if n_w <= 1 {
w_min
} else {
w_min + (w_max - w_min) * wi as f64 / (n_w - 1) as f64
};
let mut r_values = Vec::with_capacity(n_realizations);
for r in 0..n_realizations {
let seed = base_seed + (wi * 1000 + r * 100 + l * 10) as u64;
let mat = anderson_3d(l, l, l, w, seed);
let tri = lanczos(&mat, n, seed);
let eigs = lanczos_eigenvalues(&tri);
r_values.push(level_spacing_ratio(&eigs));
}
let mean = r_values.iter().sum::<f64>() / n_realizations as f64;
let variance = if n_realizations > 1 {
r_values.iter().map(|r| (r - mean).powi(2)).sum::<f64>() / (n_realizations - 1) as f64
} else {
0.0
};
let stderr = (variance / n_realizations as f64).sqrt();
results.push(AndersonSweepPoint {
w,
r_mean: mean,
r_stderr: stderr,
});
}
results
}
#[must_use]
pub fn find_w_c(sweep: &[AndersonSweepPoint], midpoint: f64) -> Option<f64> {
let mut last = None;
for i in 1..sweep.len() {
let (w0, r0) = (sweep[i - 1].w, sweep[i - 1].r_mean);
let (w1, r1) = (sweep[i].w, sweep[i].r_mean);
if r0 > midpoint && r1 <= midpoint {
let t = (midpoint - r0) / (r1 - r0);
last = Some(t.mul_add(w1 - w0, w0));
}
}
last
}
#[must_use]
pub fn anderson_eigenvalues(n: usize, disorder: f64, seed: u64) -> Vec<f64> {
let (diag, off) = anderson_hamiltonian(n, disorder, seed);
super::tridiag::find_all_eigenvalues(&diag, &off)
}
#[must_use]
pub fn anderson_4d(l: usize, disorder: f64, seed: u64) -> SpectralCsrMatrix {
let n = l * l * l * l;
let mut rng = LcgRng::new(seed);
let mut row_ptr = Vec::with_capacity(n + 1);
let mut col_idx = Vec::new();
let mut values = Vec::new();
let idx =
|ix: usize, iy: usize, iz: usize, iw: usize| -> usize { ((ix * l + iy) * l + iz) * l + iw };
row_ptr.push(0);
for ix in 0..l {
for iy in 0..l {
for iz in 0..l {
for iw in 0..l {
let v_i = disorder * (rng.uniform() - 0.5);
let mut entries: Vec<(usize, f64)> = Vec::new();
if ix > 0 {
entries.push((idx(ix - 1, iy, iz, iw), -1.0));
}
if iy > 0 {
entries.push((idx(ix, iy - 1, iz, iw), -1.0));
}
if iz > 0 {
entries.push((idx(ix, iy, iz - 1, iw), -1.0));
}
if iw > 0 {
entries.push((idx(ix, iy, iz, iw - 1), -1.0));
}
entries.push((idx(ix, iy, iz, iw), v_i));
if iw + 1 < l {
entries.push((idx(ix, iy, iz, iw + 1), -1.0));
}
if iz + 1 < l {
entries.push((idx(ix, iy, iz + 1, iw), -1.0));
}
if iy + 1 < l {
entries.push((idx(ix, iy + 1, iz, iw), -1.0));
}
if ix + 1 < l {
entries.push((idx(ix + 1, iy, iz, iw), -1.0));
}
entries.sort_by_key(|&(c, _)| c);
for (c, v) in entries {
col_idx.push(c);
values.push(v);
}
row_ptr.push(col_idx.len());
}
}
}
}
SpectralCsrMatrix {
n,
row_ptr,
col_idx,
values,
}
}
#[must_use]
pub fn clean_4d_lattice(l: usize) -> SpectralCsrMatrix {
anderson_4d(l, 0.0, 0)
}
#[must_use]
pub fn wegner_block_4d(original: &SpectralCsrMatrix, l: usize) -> SpectralCsrMatrix {
assert!(
l >= 2 && l.is_multiple_of(2),
"L must be even and >= 2 for Wegner blocking"
);
let l2 = l / 2;
let n_coarse = l2 * l2 * l2 * l2;
let fine_idx =
|ix: usize, iy: usize, iz: usize, iw: usize| -> usize { ((ix * l + iy) * l + iz) * l + iw };
let coarse_idx = |ix: usize, iy: usize, iz: usize, iw: usize| -> usize {
((ix * l2 + iy) * l2 + iz) * l2 + iw
};
let mut block_diag = vec![0.0; n_coarse];
for bx in 0..l2 {
for by in 0..l2 {
for bz in 0..l2 {
for bw in 0..l2 {
let mut sum = 0.0;
for dx in 0..2 {
for dy in 0..2 {
for dz in 0..2 {
for dw in 0..2 {
let site = fine_idx(
bx * 2 + dx,
by * 2 + dy,
bz * 2 + dz,
bw * 2 + dw,
);
for k in original.row_ptr[site]..original.row_ptr[site + 1] {
if original.col_idx[k] == site {
sum += original.values[k];
}
}
}
}
}
}
block_diag[coarse_idx(bx, by, bz, bw)] = sum / 16.0;
}
}
}
}
let mut row_ptr = Vec::with_capacity(n_coarse + 1);
let mut col_idx_out = Vec::new();
let mut values_out = Vec::new();
row_ptr.push(0);
for bx in 0..l2 {
for by in 0..l2 {
for bz in 0..l2 {
for bw in 0..l2 {
let ci = coarse_idx(bx, by, bz, bw);
let mut entries: Vec<(usize, f64)> = Vec::new();
if bx > 0 {
entries.push((coarse_idx(bx - 1, by, bz, bw), -1.0));
}
if by > 0 {
entries.push((coarse_idx(bx, by - 1, bz, bw), -1.0));
}
if bz > 0 {
entries.push((coarse_idx(bx, by, bz - 1, bw), -1.0));
}
if bw > 0 {
entries.push((coarse_idx(bx, by, bz, bw - 1), -1.0));
}
entries.push((ci, block_diag[ci]));
if bw + 1 < l2 {
entries.push((coarse_idx(bx, by, bz, bw + 1), -1.0));
}
if bz + 1 < l2 {
entries.push((coarse_idx(bx, by, bz + 1, bw), -1.0));
}
if by + 1 < l2 {
entries.push((coarse_idx(bx, by + 1, bz, bw), -1.0));
}
if bx + 1 < l2 {
entries.push((coarse_idx(bx + 1, by, bz, bw), -1.0));
}
entries.sort_by_key(|&(c, _)| c);
for (c, v) in entries {
col_idx_out.push(c);
values_out.push(v);
}
row_ptr.push(col_idx_out.len());
}
}
}
}
SpectralCsrMatrix {
n: n_coarse,
row_ptr,
col_idx: col_idx_out,
values: values_out,
}
}
#[cfg(test)]
#[path = "anderson_tests.rs"]
mod tests;