- all tests passing

This commit is contained in:
w.pomp committed 2026-07-28 11:51:04 +02:00
1 parent 9f8764ee00
commit 0f315e66ca
5 files changed
+20 -92

No files matched your search

+11 -78
View File
@@ -167,15 +167,13 @@ impl Sampling {
fn index(&self) -> Vec<Vec<f64>> {
match self {
Self::Fixed(index) => index.clone(),
Self::Random((n_samples, shape, _cached)) => {
rand::rng()
.random_iter::<f64>()
.take(n_samples * shape.len())
.chunks(shape.len())
.into_iter()
.map(|c| c.zip_eq(shape.iter()).map(|(i, s)| i * s - 0.5).collect())
.collect()
}
Self::Random((n_samples, shape, _cached)) => rand::rng()
.random_iter::<f64>()
.take(n_samples * shape.len())
.chunks(shape.len())
.into_iter()
.map(|c| c.zip_eq(shape.iter()).map(|(i, s)| i * s - 0.5).collect())
.collect(),
}
}
}
@@ -208,7 +206,7 @@ where
shape: Vec<f64>,
center: Vec<f64>,
fixed: BSpline<0, D>,
moving: BSpline<3, D>,
moving: BSpline<1, D>,
fixed_mu: FixedMu,
minmax: [f64; 2],
sampling: Sampling,
@@ -226,7 +224,7 @@ where
{
pub fn new(
fixed: BSpline<0, D>,
moving: BSpline<3, D>,
moving: BSpline<1, D>,
sampling: SamplingArg,
n_bins: usize,
edge: f64,
@@ -523,8 +521,6 @@ mod tests {
use algos::optimization::{bfgs_minimize, gradient_descent_minimize};
use ndarray::{MeshIndex, array, meshgrid, stack};
use ndarray_npy::NpzWriter;
use num::integer::Roots;
use num::traits::FloatConst;
use std::fs::File;
#[test]
@@ -546,69 +542,6 @@ mod tests {
Ok(())
}
#[test]
fn grad() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::rng();
let u1 = (&mut rng)
.random_iter::<f64>()
.take(1000)
.collect::<Vec<_>>();
let u2 = (&mut rng)
.random_iter::<f64>()
.take(1000)
.collect::<Vec<_>>();
let f = u1
.iter()
.zip_eq(u2.iter())
.map(|(i, j)| (-2.0 * i.ln()).sqrt() * (2.0 * f64::PI() * j).cos() - 1.0)
.collect::<Vec<_>>();
let m = u1
.iter()
.zip_eq(u2.iter())
.map(|(i, j)| (-2.0 * i.ln()).sqrt() * (2.0 * f64::PI() * j).sin() + 1.0)
.collect::<Vec<_>>();
let s = f.len().sqrt();
let n_bins = s.max(11);
let e = 2.0 / (n_bins - 3) as f64;
let first_bin = -5.0 - e;
let last_bin = 5.0 + e;
// println!("f: {:?}, m: {:?}", f, m);
// println!("first_bin: {}, last_bin: {}, n_bins: {}, e: {}", first_bin, last_bin, n_bins, e);
let w = vec![1.0; f.len()];
let dw = vec![vec![0.0]; f.len()];
let (jpdf, d_jpdf_m) = parzen(
1.0,
&[1.0],
&f,
&m,
&[],
&w,
&dw,
first_bin,
last_bin,
n_bins,
);
let pdf_f = jpdf.rows().into_iter().map(|i| i.sum()).collect::<Vec<_>>();
let pdf_m = jpdf
.columns()
.into_iter()
.map(|i| i.sum())
.collect::<Vec<_>>();
let d_pdf_m = d_jpdf_m
.columns()
.into_iter()
.map(|i| i.sum())
.collect::<Vec<_>>();
println!(
"pdf_f = {:?}\npdf_m = {:?}\nd_pdf_m = {:?}",
pdf_f.to_vec(),
pdf_m.to_vec(),
d_pdf_m.to_vec()
);
Ok(())
}
#[test]
fn metric() -> Result<(), Box<dyn std::error::Error>> {
let im_a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
@@ -837,7 +770,7 @@ mod tests {
let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let fixed = BSpline::<0, _>::new(a.view());
let moving = BSpline::<3, _>::new(b.view());
let moving = BSpline::<1, _>::new(b.view());
let mus = vec![0.0, 0.001];
let mut npz = NpzWriter::new(File::create(
@@ -918,7 +851,7 @@ mod tests {
let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let fixed = BSpline::<0, _>::new(a.view());
let moving = BSpline::<3, _>::new(b.view());
let moving = BSpline::<1, _>::new(b.view());
let mus = Array1::linspace(-10.0, 10.0, 500);
let s = a.len() as f64;