From 3fc5f4ac839250eac9917eb465d5e2cc85aa6e70 Mon Sep 17 00:00:00 2001 From: Gurtej Kanwar Date: Sat, 11 Sep 2021 15:13:52 +0200 Subject: [PATCH] Implements some manual checks for distributions --- .cargo/config.toml | 2 ++ Cargo.toml | 2 +- src/bin/test_rand.rs | 22 ++++++++++++++++++++++ src/lib/color_matrix.rs | 16 +++++++++++++++- src/lib/lib.rs | 4 ++++ src/lib/rand_dist.rs | 9 ++++----- 6 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 src/bin/test_rand.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..488dad5 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.'cfg(all(target_os = "macos"))'] +rustflags = ["-C", "link-args=-L/usr/local/opt/openblas/lib -lopenblas"] diff --git a/Cargo.toml b/Cargo.toml index a6a3809..1c20d5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rust_qcd" +name = "rs-lattice" version = "0.1.0" edition = "2018" diff --git a/src/bin/test_rand.rs b/src/bin/test_rand.rs new file mode 100644 index 0000000..0b83222 --- /dev/null +++ b/src/bin/test_rand.rs @@ -0,0 +1,22 @@ +use lqft::{sample_su2_theta, ColorMat, SampleHaar, IntoBytes}; +use std::fs::File; +use std::io::Write; + +fn main() -> Result<(), std::io::Error> { + { + let beta = 1.0; + let mut file = File::create("su2_theta.dat")?; + for _ in 0..10000 { + let th: f64 = sample_su2_theta(beta); + file.write_all(&th.to_le_bytes())?; + } + } + { + let mut file = File::create("u2_haar.dat")?; + for _ in 0..10000 { + let u = ColorMat::<2>::sample_haar(); + file.write_all(&u.into_bytes())?; + } + } + Ok(()) +} diff --git a/src/lib/color_matrix.rs b/src/lib/color_matrix.rs index 29ebe4e..8e1251d 100644 --- a/src/lib/color_matrix.rs +++ b/src/lib/color_matrix.rs @@ -1,12 +1,13 @@ use crate::math::{c64, CMat, CVec}; use crate::rand_dist::{SampleHaar}; +use crate::{IntoBytes}; use std::ops::{Add, Index, IndexMut}; // Color matrices #[derive(Clone)] pub struct ColorMat { - data: CMat + pub data: CMat } impl ColorMat { @@ -23,6 +24,19 @@ impl ColorMat { } } +impl IntoBytes for ColorMat { + fn into_bytes(&self) -> Vec { + let mut bs: Vec = vec!(); + for i in 0..N { + for j in 0..N { + bs.extend_from_slice(&self.data[i][j].re.to_le_bytes()); + bs.extend_from_slice(&self.data[i][j].im.to_le_bytes()); + } + } + bs + } +} + impl Index<(usize, usize)> for ColorMat { type Output = c64; diff --git a/src/lib/lib.rs b/src/lib/lib.rs index cdb93ba..a8daf8f 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -14,6 +14,10 @@ use ndarray::Array4; use std::convert::TryInto; use std::ops::Index; +pub trait IntoBytes { + fn into_bytes(&self) -> Vec; +} + pub trait LatticeObject { fn latt_shape(&self) -> [usize; ND]; fn nd(&self) -> usize { diff --git a/src/lib/rand_dist.rs b/src/lib/rand_dist.rs index 8d758c6..dd68c6a 100644 --- a/src/lib/rand_dist.rs +++ b/src/lib/rand_dist.rs @@ -9,14 +9,13 @@ use ndarray_linalg::qr::QR; // on a wrapped Cauchy base distribution. pub fn sample_von_mises(beta: f64) -> f64 { let mut rng = thread_rng(); - let u1: f64 = rng.gen(); - let u2: f64 = rng.gen(); - let u3: f64 = rng.gen(); - let tau = 1.0 + (1.0 + 4.0 * beta * beta).sqrt(); let rho = (tau - (2.0 * tau).sqrt()) / (2.0 * beta); loop { + let u1: f64 = rng.gen(); + let u2: f64 = rng.gen(); + let u3: f64 = rng.gen(); let r = (1.0 + rho * rho) / (2.0 * rho); let z = (PI * u1).cos(); let f = (1.0 + r * z) / (r + z); @@ -78,4 +77,4 @@ macro_rules! impl_sample_haar { }; } impl_sample_haar!(2); -impl_sample_haar!(3); +impl_sample_haar!(3); \ No newline at end of file