redone function

This commit is contained in:
2024-06-02 14:37:15 +02:00
parent 6b48a51be4
commit f9e9fb7e10
4 changed files with 35 additions and 23 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
/target /target
/Cargo.lock /Cargo.lock
*/output.json src/output*.json

View File

@@ -3,6 +3,14 @@ name = "montecarlo-rust"
version = "0.1.0" version = "0.1.0"
edition = "2021" 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.rand] [dependencies.rand]

View File

@@ -5,33 +5,27 @@ use crate::linear_bounds::LinearBounds;
use crate::point::Point; use crate::point::Point;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Function{} pub struct Function{
f: fn(&f64)->f64,
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;
} }
impl Function { impl Function {
pub fn new() -> Function { pub fn new(f: fn(&f64) -> f64) -> Function {
return Function {}; return Function {f};
} }
pub fn integrate(&self, bounds: &LinearBounds) -> f64 { pub fn integrate(&self, _bounds: &LinearBounds) -> f64 {
return big_f(&bounds.get_higher()) - big_f(&bounds.get_lower()); 0.0
} }
fn includes(&self, point: &Point) -> bool { 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; 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 { pub fn approximate(&self, bounds: &Bounds, samples: usize, thread_cnt: usize) -> f64 {
let mut hits: u64 = 0; let mut hits: i64 = 0;
let mut threads: Vec<JoinHandle<u64>> = vec![]; let mut threads: Vec<JoinHandle<i64>> = vec![];
let function: Arc<Function> = Arc::new(*self); let function: Arc<Function> = Arc::new(*self);
let bounds: Arc<Bounds> = Arc::new(*bounds); let bounds: Arc<Bounds> = Arc::new(*bounds);
@@ -41,11 +35,12 @@ impl Function {
let bounds = bounds.clone(); let bounds = bounds.clone();
threads.push(std::thread::spawn(move || { threads.push(std::thread::spawn(move || {
let mut hits: u64 = 0; let mut hits: i64 = 0;
for _ in 0..samples/thread_cnt { for _ in 0..samples/thread_cnt {
let point: Point = bounds.get_random_point(); let point: Point = bounds.get_random_point();
if function.includes(&point) { if function.includes(&point) {
hits += 1; if point.get_y() >= 0.0 { hits += 1;}
else { hits -= 1; }
} }
} }
return hits; return hits;

View File

@@ -1,6 +1,8 @@
use std::f64::consts::E;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::time::Instant;
use crate::bounds::Bounds; use crate::bounds::Bounds;
use crate::function::Function; use crate::function::Function;
use crate::linear_bounds::LinearBounds; use crate::linear_bounds::LinearBounds;
@@ -13,15 +15,22 @@ mod function;
mod plot; mod plot;
const THREAD_CNT: usize = 8; 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 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() { fn main() {
let bounds: Bounds = Bounds::new(LinearBounds::new(0_f64, 20_f64), LinearBounds::new(-100_f64, 150000_f64)); let bounds: Bounds = Bounds::new(LinearBounds::new(3.1_f64, 3.2_f64), LinearBounds::new(-15_f64, 35_f64));
let func: Function = Function::new(); 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) { fn simulate(bounds: &Bounds, func: &Function) {