diff --git a/.gitignore b/.gitignore index 87b1bec..ff21b7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target /Cargo.lock -*/output.json \ No newline at end of file +src/output*.json \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index c1d0ace..8593ee6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,14 @@ name = "montecarlo-rust" version = "0.1.0" edition = "2021" +[[bin]] +name = "monte-carlo" +path = "src/main.rs" + +[profile.release] +strip = true # Automatically strip symbols from the binary. +opt-level = 3 + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies.rand] diff --git a/src/function.rs b/src/function.rs index e9e3bac..d056e17 100644 --- a/src/function.rs +++ b/src/function.rs @@ -5,33 +5,27 @@ use crate::linear_bounds::LinearBounds; use crate::point::Point; #[derive(Clone, Copy)] -pub struct Function{} - -fn f(x: &f64) -> f64 { - return x.powi(4) - 4_f64 * x.powi(3) + 18_f64 * x.powi(2) - 12_f64 * x - 69_f64 -} - -fn big_f(x: &f64) -> f64 { - return 0.2 * x.powi(5) - x.powi(4) + 6_f64 * x.powi(3) - 6_f64 * x.powi(2) - 69_f64 * x; +pub struct Function{ + f: fn(&f64)->f64, } impl Function { - pub fn new() -> Function { - return Function {}; + pub fn new(f: fn(&f64) -> f64) -> Function { + return Function {f}; } - pub fn integrate(&self, bounds: &LinearBounds) -> f64 { - return big_f(&bounds.get_higher()) - big_f(&bounds.get_lower()); + pub fn integrate(&self, _bounds: &LinearBounds) -> f64 { + 0.0 } fn includes(&self, point: &Point) -> bool { - let y_0: f64 = f(&point.get_x()); + let y_0: f64 = (self.f)(&point.get_x()); return y_0.abs() > point.get_y().abs() && y_0 * point.get_y() >= 0_f64; } pub fn approximate(&self, bounds: &Bounds, samples: usize, thread_cnt: usize) -> f64 { - let mut hits: u64 = 0; - let mut threads: Vec> = vec![]; + let mut hits: i64 = 0; + let mut threads: Vec> = vec![]; let function: Arc = Arc::new(*self); let bounds: Arc = Arc::new(*bounds); @@ -41,11 +35,12 @@ impl Function { let bounds = bounds.clone(); threads.push(std::thread::spawn(move || { - let mut hits: u64 = 0; + let mut hits: i64 = 0; for _ in 0..samples/thread_cnt { let point: Point = bounds.get_random_point(); if function.includes(&point) { - hits += 1; + if point.get_y() >= 0.0 { hits += 1;} + else { hits -= 1; } } } return hits; diff --git a/src/main.rs b/src/main.rs index 7f10f9d..707fcc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ +use std::f64::consts::E; use std::fs::File; use std::io; use std::io::Write; +use std::time::Instant; use crate::bounds::Bounds; use crate::function::Function; use crate::linear_bounds::LinearBounds; @@ -13,15 +15,22 @@ mod function; mod plot; const THREAD_CNT: usize = 8; -const SAMPLE_LIMIT: usize = 1_000_000_000; +const SAMPLE_LIMIT: usize = 100_000_000; const SUB_SAMPLES: [usize; 3] = [1, 2, 5]; -const SAMPLES_PER_ITERATION: usize = 1000; +const SAMPLES_PER_ITERATION: usize = 250; + +fn f(x: &f64) -> f64 { + E.powf(*x) * (1.0 / (*x).sin()).cos() + (*x).powf(2.0) +} fn main() { - let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64)); - let func: Function = Function::new(); + let bounds: Bounds = Bounds::new(LinearBounds::new(3.1_f64, 3.2_f64), LinearBounds::new(-15_f64, 35_f64)); + let func: Function = Function::new(f); - simulate(&bounds, &func) + let start: Instant = Instant::now(); + println!("{}\n{}ms", func.approximate(&bounds, 10_000_000, THREAD_CNT), start.elapsed().as_millis()) + + //simulate(&bounds, &func) } fn simulate(bounds: &Bounds, func: &Function) {