commit 87bead4dc0dafbbd9b35abe57172f0c4999e31a5
parent f4df05e1453fc36801a2987e3f86a14de2e89392
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date: Sat, 6 Apr 2024 15:47:38 -0400
rust: savepoint
Diffstat:
8 files changed, 75 insertions(+), 102 deletions(-)
diff --git a/C/out/samples b/C/out/samples
Binary files differ.
diff --git a/README.md b/README.md
@@ -146,6 +146,10 @@ The beautiful thing about bc is that it's an arbitrary precision calculator:
I decided to go with [Gavin Howard's bc](https://git.gavinhoward.com/gavin/bc), because I've been following the guy some time, and I respect him. It also had some crucial extensions, like a random number generator and allowing specifying functions and variables with names longer than one letter.
+### Rust
+
+I'm still making up my mind about Rust. It seems foreign. Mutable pointers seem kinda weird. Their randomness libraries are very nice, and the ziggurat method they're using to sample from a normal is very nice. I haven't added parallelism yet. You can see some notes in the Rust folder.
+
### Overall thoughts
Overall I don't think that this is a fair comparison of the languages intrinsically, because I'm just differentially good at them, because I've chosen to put more effort in ones than in others. But it is still useful to me personally, and perhaps mildly informative to others.
@@ -154,13 +158,13 @@ Overall I don't think that this is a fair comparison of the languages intrinsica
- [x] bc (the standard posix calculator)
- [x] OCaml
+- [x] Go
+- [ ] Rust
- [ ] PyMC
- [ ] Zig
-- [ ] Rust
- [ ] Forth
- [ ] Julia (TuringML, MCHammer)
- [ ] Lisp
-- [ ] Go
- [ ] sh/bash, lol?
- [ ] Dagger (<https://usedagger.com/>)
- [ ] Haskell
diff --git a/go/squiggle.go b/go/squiggle.go
@@ -128,14 +128,14 @@ func main() {
avg = avg / float64(n_samples)
fmt.Printf("Average: %v\n", avg)
/*
- // Without concurrency:
- n_samples := 1_000_000
- var r = rand.New(rand.NewPCG(uint64(1), uint64(2)))
- var avg float64 = 0
- for i := 0; i < n_samples; i++ {
- avg += sample_mixture(fs[0:], ws[0:], r)
- }
- avg = avg / float64(n_samples)
- fmt.Printf("Average: %v\n", avg)
+ // Without concurrency:
+ n_samples := 1_000_000
+ var r = rand.New(rand.NewPCG(uint64(1), uint64(2)))
+ var avg float64 = 0
+ for i := 0; i < n_samples; i++ {
+ avg += sample_mixture(fs[0:], ws[0:], r)
+ }
+ avg = avg / float64(n_samples)
+ fmt.Printf("Average: %v\n", avg)
*/
}
diff --git a/rust/README.md b/rust/README.md
@@ -0,0 +1,44 @@
+## Intial impressions
+
+Rust seems like it has a) great documentation, b) a better randomness generator than the one I was previously using.
+
+Rust feels like pulling my teeth here, partly because I'm developing this on a server rather than on my own computer.
+
+## Docs
+
+The specific library I'll be using:
+
+- https://crates.io/crates/rand_distr
+- https://docs.rs/rand_distr/latest/rand_distr/index.html
+- https://github.com/rust-random/rand/
+- https://docs.rs/rand_distr/latest/rand_distr/index.html
+- https://docs.rs/rand/latest/rand/
+
+An underlying normal distribution algorithm that might be better than the Bo-Muller method.
+
+- https://docs.rs/rand_distr/latest/src/rand_distr/normal.rs.html#238-307
+- https://www.doornik.com/research/ziggurat.pdf
+- https://en.wikipedia.org/wiki/Ziggurat_algorithm
+
+A book produced as documentation (! <3%): https://rust-random.github.io/book/intro.html
+
+----
+
+Some more ressources:
+
+- https://prng.di.unimi.it/#remarks
+- https://www.pcg-random.org/pdf/hmc-cs-2014-0905.pdf
+- https://web.archive.org/web/20160801142711/http://random.mat.sbg.ac.at/results/peter/A19final.pdf
+
+---
+
+- https://doc.rust-lang.org/rust-by-example/
+- https://doc.rust-lang.org/book/
+
+## To do
+
+- [x] Figure out libraries
+- [x] Write initial botec example
+- [ ] Add concurrency <https://doc.rust-lang.org/book/ch16-03-shared-state.html>
+- [ ] Figure out how to compare with other languages
+ - This is going to be super annoying, since I no longer have the runtimes for the other languages in this new slow computer of mine.
diff --git a/rust/makefile b/rust/makefile
@@ -1,3 +1,16 @@
-run:
+SHELL=bash
+
+build:
+ cargo build --release
+ ./target/release/squiggle_rust
+
+time:
+ time ./target/release/squiggle_rust
+
+dev:
+ cargo run
+
+rustc:
rustc ./src/main.rs
./main
+
diff --git a/rust/notes.md b/rust/notes.md
@@ -1,28 +0,0 @@
-Rust seems like it has a) great documentation, b) a better randomness generator than the one I was previously using.
-
-
-The specific library I'll be using:
-
-- https://crates.io/crates/rand_distr
-- https://docs.rs/rand_distr/latest/rand_distr/index.html
-- https://github.com/rust-random/rand/
-- https://docs.rs/rand_distr/latest/rand_distr/index.html
-- https://docs.rs/rand/latest/rand/
-
-An underlying normal distribution algorithm that might be better than the Bo-Muller method.
-
-- https://docs.rs/rand_distr/latest/src/rand_distr/normal.rs.html#238-307
-- https://www.doornik.com/research/ziggurat.pdf
-- https://en.wikipedia.org/wiki/Ziggurat_algorithm
-
-A book produced as documentation (! <3%): https://rust-random.github.io/book/intro.html
-
-----
-
-Rust feels like pulling my teeth here, partly because I'm developing this on a server rather than on my own computer.
-
-Some more ressources:
-https://prng.di.unimi.it/#remarks
-https://www.pcg-random.org/pdf/hmc-cs-2014-0905.pdf
-https://web.archive.org/web/20160801142711/http://random.mat.sbg.ac.at/results/peter/A19final.pdf
-
diff --git a/rust/src/main.rs b/rust/src/main.rs
@@ -1,10 +1,7 @@
use rand_core::SeedableRng;
-use rand_distr::{Distribution, Normal, LogNormal, Uniform};
+use rand_distr::{Distribution, LogNormal, Uniform};
use rand_pcg::Pcg64Mcg;
-// use rand::thread_rng;
-// use rand::prelude::*
-
fn sample_to(low: f64, high: f64, mut rng: &mut Pcg64Mcg) -> f64 {
let normal90 = 1.6448536269514727; // change to global const later
let loglow = low.ln();
@@ -12,16 +9,10 @@ fn sample_to(low: f64, high: f64, mut rng: &mut Pcg64Mcg) -> f64 {
let normal_mean = (loghigh + loglow)/2.0;
let normal_std = (loghigh - loglow) / (2.0 * normal90);
- /*
let lognormal = LogNormal::new(normal_mean, normal_std).unwrap();
let x = lognormal.sample(&mut rng);
// https://docs.rs/rand_distr/latest/src/rand_distr/normal.rs.html#232-236
- */
- let normal = Normal::new(normal_mean, normal_std).unwrap();
- let x = normal.sample(&mut rng);
- let y = x.exp();
-
- return y;
+ return x;
}
fn model(mut rng: &mut Pcg64Mcg) -> f64 {
@@ -38,8 +29,6 @@ fn model(mut rng: &mut Pcg64Mcg) -> f64 {
} else if p < (ws[0] + ws[1]) {
return 1.0;
} else if p < (ws[0] + ws[1] + ws[2]) {
- // let normal = Normal::new(2.0, 3.0).unwrap();
- // let v = normal.sample(&mut rng);
let x = sample_to(1.0, 3.0, rng);
return x;
} else {
@@ -49,17 +38,8 @@ fn model(mut rng: &mut Pcg64Mcg) -> f64 {
}
fn main() {
- println!("Hello, world!");
-
let mut rng = Pcg64Mcg::seed_from_u64(1);
- /*
- let a = model(&mut rng);
- println!("Sample is {}", a);
- let b = model(&mut rng);
- println!("Sample is {}", b);
- */
-
let mut mean = 0.0;
let n_samples = 1_000_000;
for _ in 0..n_samples {
diff --git a/rust/tmp.txt b/rust/tmp.txt
@@ -1,40 +0,0 @@
-
-
- Compiling squiggle_rust v0.1.0 (/home/nuno/Documents/core/software/fresh/time-to-botec/rust)
-warning: variable does not need to be mutable
- --> src/main.rs:18:9
- |
-18 | let mut rng = Pcg64Mcg::seed_from_u64(1);
- | ----^^^
- | |
- | help: remove this `mut`
- |
- = note: `#[warn(unused_mut)]` on by default
-
-error[E0382]: use of moved value: `rng`
- --> src/main.rs:22:19
- |
-18 | let mut rng = Pcg64Mcg::seed_from_u64(1);
- | ------- move occurs because `rng` has type `Mcg128Xsl64`, which does not implement the `Copy` trait
-19 | // mean 2, standard deviation 3
-20 | let v = model(rng);
- | --- value moved here
-21 | println!("{} is from a N(2, 9) distribution", v);
-22 | let z = model(rng);
- | ^^^ value used here after move
- |
-note: consider changing this parameter type in function `model` to borrow instead if owning the value isn't necessary
- --> src/main.rs:8:19
- |
-8 | fn model(mut rng: Pcg64Mcg) -> f64 {
- | ----- ^^^^^^^^ this parameter takes ownership of the value
- | |
- | in this function
-help: consider cloning the value if the performance cost is acceptable
- |
-20 | let v = model(rng.clone());
- | ++++++++
-
-For more information about this error, try `rustc --explain E0382`.
-warning: `squiggle_rust` (bin "squiggle_rust") generated 1 warning
-error: could not compile `squiggle_rust` (bin "squiggle_rust") due to 1 previous error; 1 warning emitted