Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ edition = "2021"

[dependencies]
num-traits = "0.2.19"
rand_distr = "0.4"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool, but not a core dependency of an n dimensional array, more an example

rand="0.8"

[lib]

Expand Down
41 changes: 41 additions & 0 deletions src/brownian.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use num_traits::Num;
use rand_distr::{Normal, Distribution};
use crate::{storage::DimensionalStorage, Dimensional};
use std::ops::AddAssign;

// TODO: make it work for all types (now works only for floating types)
// TODO: have the option to save the path of the walk (although may be costly)


fn brownian_motion<T, S, const N: usize>(dimensional: &mut Dimensional<T, S, N>, std_dev: f64, n_steps: usize)
where
T: Num + Copy + AddAssign ,
f64: Into<T>,
S: DimensionalStorage<T, N>,
{
let normal = Normal::new(0.0, std_dev).unwrap();
let length_dimensional = dimensional.len();
let mut iter = dimensional.iter_mut();
for _ in 1..=length_dimensional {
if let Some(elem) = iter.next() {
for _ in 0..n_steps{
*elem += normal.sample(&mut rand::thread_rng()).into();
}
}
}

}

mod tests {
use crate::{brownian::brownian_motion, Dimensional, LinearArrayStorage};


#[test]
fn test_len() {
let mut zeros: Dimensional<f64, LinearArrayStorage<f64, 2>, 2> = Dimensional::zeros([2, 3]);
brownian_motion(&mut zeros,0.1, 1000);
println!("{}", format!("{}", zeros));

}

}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod display;
mod iterators;
mod operators;
mod storage;
mod brownian;

// Public API
pub use crate::core::Dimensional;
Expand Down