Skip to content
Snippets Groups Projects
Commit 441a9ff3 authored by Thomas Kennedy's avatar Thomas Kennedy
Browse files

Add Rust version of Monte Carlo integration example

parent d9ca07e2
No related branches found
No related tags found
No related merge requests found
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
[[package]]
name = "monte_carlo_integration"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "ppv-lite86"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[package]
name = "monte_carlo_integration"
version = "0.1.0"
authors = ["Thomas J. Kennedy <tkennedy@cs.odu.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.7"
use rand::prelude::*;
use std::env;
struct Point(f64, f64);
/// Generate a sequence of random x values and plug them into f(x).
///
/// Args:
/// f: mathematical function
/// lower_limit: 'a' the lower bound
/// upper_bound: 'b' the upper bound
/// n: number of points to generate
///
/// Yields:
/// A sequence of points in the form (x, f(x))
fn generate_random_points(f: fn(f64) -> f64,
lower_limit: f64,
upper_limit: f64,
n: u64) -> Vec<Point> {
let d = rand::distributions::Uniform::new_inclusive(lower_limit, upper_limit);
let mut rng = thread_rng();
(0..n).map(|_| {
let x = rng.sample(d);
Point(x, f(x))
}).collect()
}
/// This main demonstrates the impact of the number of points on Monte Carlo
/// integration
fn main() {
let num_points = env::args().nth(1).unwrap().parse::<u64>(); // Unused in this version of main
let limit_a = env::args().nth(2).unwrap().parse::<f64>().unwrap();
let limit_b = env::args().nth(3).unwrap().parse::<f64>().unwrap();
let max_magnitude = env::args().nth(4).unwrap().parse::<u32>().unwrap();
let math_f = |x: f64| x.powf(2_f64);
println!("| {:^16} | {:^20} |", "# Points", "Est. f(x)");
let max_num_points: u64 = 2_u64.pow(max_magnitude);
let point_sequence = (generate_random_points(math_f, limit_a, limit_b, max_num_points));
for magnitude in 0..=max_magnitude {
let num_points = 2_u64.pow(magnitude);
let sum_of_f_of_x_values: f64 = point_sequence.iter()
.map(|point| point.1)
.take((num_points as usize))
.sum();
let integral_result = (limit_b - limit_a) /
(num_points as f64) *
sum_of_f_of_x_values;
println!("| {:>16} | {:^20.8} |", num_points, integral_result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment