diff --git a/lang-trials/rust-benchmark/src/engine.rs b/lang-trials/rust-benchmark/src/engine.rs index 3b2b290..a2661aa 100644 --- a/lang-trials/rust-benchmark/src/engine.rs +++ b/lang-trials/rust-benchmark/src/engine.rs @@ -1,10 +1,87 @@ use nalgebra::{*, allocator::Allocator}; use std::f64::consts::{PI, E}; -use web_sys::console; /*use std::ops::Sub;*/ /*use typenum::{B1, UInt, UTerm};*/ -/*pub fn eigvals_rotated(A: SMatrix, time: f64): complex_eigenvalues(&self) -> OVector, D>*/ +/* dynamic matrices */ +pub fn rand_eigval_series(time_res: usize) -> Vec, Dyn>> + where + N: ToTypenum + DimName + DimSub, + DefaultAllocator: + Allocator + + Allocator + + Allocator<>::Output> + + Allocator>::Output> +{ + // initialize the random matrix + let dim = N::try_to_usize().unwrap(); + let mut rand_mat = DMatrix::::from_fn(dim, dim, |j, k| { + let n = j*dim + k; + E*((n*n) as f64) % 2.0 - 1.0 + }) * (3.0 / (dim as f64)).sqrt(); + + // initialize the rotation step + let mut rot_step = DMatrix::::identity(dim, dim); + let max_freq = 4; + for n in (0..dim).step_by(2) { + let ang = PI * ((n % max_freq) as f64) / (time_res as f64); + let ang_cos = ang.cos(); + let ang_sin = ang.sin(); + rot_step[(n, n)] = ang_cos; + rot_step[(n+1, n)] = ang_sin; + rot_step[(n, n+1)] = -ang_sin; + rot_step[(n+1, n+1)] = ang_cos; + } + + // find the eigenvalues + let mut eigval_series = Vec::, Dyn>>::with_capacity(time_res); + eigval_series.push(rand_mat.complex_eigenvalues()); + for _ in 1..time_res { + rand_mat = &rot_step * rand_mat; + eigval_series.push(rand_mat.complex_eigenvalues()); + } + eigval_series +} + +/* dynamic single float matrices */ +/*pub fn rand_eigval_series(time_res: usize) -> Vec, Dyn>> + where + N: ToTypenum + DimName + DimSub, + DefaultAllocator: + Allocator + + Allocator + + Allocator<>::Output> + + Allocator>::Output> +{ + // initialize the random matrix + let dim = N::try_to_usize().unwrap(); + let mut rand_mat = DMatrix::::from_fn(dim, dim, |j, k| { + let n = j*dim + k; + (E as f32)*((n*n) as f32) % 2.0_f32 - 1.0_f32 + }) * (3.0_f32 / (dim as f32)).sqrt(); + + // initialize the rotation step + let mut rot_step = DMatrix::::identity(dim, dim); + let max_freq = 4; + for n in (0..dim).step_by(2) { + let ang = (PI as f32) * ((n % max_freq) as f32) / (time_res as f32); + let ang_cos = ang.cos(); + let ang_sin = ang.sin(); + rot_step[(n, n)] = ang_cos; + rot_step[(n+1, n)] = ang_sin; + rot_step[(n, n+1)] = -ang_sin; + rot_step[(n+1, n+1)] = ang_cos; + } + + // find the eigenvalues + let mut eigval_series = Vec::, Dyn>>::with_capacity(time_res); + eigval_series.push(rand_mat.complex_eigenvalues()); + for _ in 1..time_res { + rand_mat = &rot_step * rand_mat; + eigval_series.push(rand_mat.complex_eigenvalues()); + } + eigval_series +}*/ /* static matrices. should only be used when the dimension is really small */ /*pub fn rand_eigval_series(time_res: usize) -> Vec, N>> @@ -18,7 +95,6 @@ use web_sys::console; { // initialize the random matrix let dim = N::try_to_usize().unwrap(); - console::log_1(&format!("dimension {dim}").into()); let mut rand_mat = OMatrix::::from_fn(|j, k| { let n = j*dim + k; E*((n*n) as f64) % 2.0 - 1.0 @@ -40,9 +116,7 @@ use web_sys::console; // find the eigenvalues let mut eigval_series = Vec::, N>>::with_capacity(time_res); - console::log_1(&"before engine eigenvalues".into()); eigval_series.push(rand_mat.complex_eigenvalues()); - console::log_1(&"after engine eigenvalues".into()); for _ in 1..time_res { rand_mat = &rot_step * rand_mat; eigval_series.push(rand_mat.complex_eigenvalues()); @@ -87,90 +161,4 @@ use web_sys::console; eigvals[t] = rand_mat.complex_eigenvalues(); } eigvals -}*/ - -/* dynamic matrices */ -pub fn rand_eigval_series(time_res: usize) -> Vec, Dyn>> - where - N: ToTypenum + DimName + DimSub, - DefaultAllocator: - Allocator + - Allocator + - Allocator<>::Output> + - Allocator>::Output> -{ - // initialize the random matrix - let dim = N::try_to_usize().unwrap(); - console::log_1(&format!("dimension {dim}").into()); - let mut rand_mat = DMatrix::::from_fn(dim, dim, |j, k| { - let n = j*dim + k; - E*((n*n) as f64) % 2.0 - 1.0 - }) * (3.0 / (dim as f64)).sqrt(); - - // initialize the rotation step - let mut rot_step = DMatrix::::identity(dim, dim); - let max_freq = 4; - for n in (0..dim).step_by(2) { - let ang = PI * ((n % max_freq) as f64) / (time_res as f64); - let ang_cos = ang.cos(); - let ang_sin = ang.sin(); - rot_step[(n, n)] = ang_cos; - rot_step[(n+1, n)] = ang_sin; - rot_step[(n, n+1)] = -ang_sin; - rot_step[(n+1, n+1)] = ang_cos; - } - - // find the eigenvalues - let mut eigval_series = Vec::, Dyn>>::with_capacity(time_res); - console::log_1(&"before engine eigenvalues".into()); - eigval_series.push(rand_mat.complex_eigenvalues()); - console::log_1(&"after engine eigenvalues".into()); - for _ in 1..time_res { - rand_mat = &rot_step * rand_mat; - eigval_series.push(rand_mat.complex_eigenvalues()); - } - eigval_series -} - -/* dynamic single float matrices */ -/*pub fn rand_eigval_series(time_res: usize) -> Vec, Dyn>> - where - N: ToTypenum + DimName + DimSub, - DefaultAllocator: - Allocator + - Allocator + - Allocator<>::Output> + - Allocator>::Output> -{ - // initialize the random matrix - let dim = N::try_to_usize().unwrap(); - console::log_1(&format!("dimension {dim}").into()); - let mut rand_mat = DMatrix::::from_fn(dim, dim, |j, k| { - let n = j*dim + k; - (E as f32)*((n*n) as f32) % 2.0_f32 - 1.0_f32 - }) * (3.0_f32 / (dim as f32)).sqrt(); - - // initialize the rotation step - let mut rot_step = DMatrix::::identity(dim, dim); - let max_freq = 4; - for n in (0..dim).step_by(2) { - let ang = (PI as f32) * ((n % max_freq) as f32) / (time_res as f32); - let ang_cos = ang.cos(); - let ang_sin = ang.sin(); - rot_step[(n, n)] = ang_cos; - rot_step[(n+1, n)] = ang_sin; - rot_step[(n, n+1)] = -ang_sin; - rot_step[(n+1, n+1)] = ang_cos; - } - - // find the eigenvalues - let mut eigval_series = Vec::, Dyn>>::with_capacity(time_res); - console::log_1(&"before engine eigenvalues".into()); - eigval_series.push(rand_mat.complex_eigenvalues()); - console::log_1(&"after engine eigenvalues".into()); - for _ in 1..time_res { - rand_mat = &rot_step * rand_mat; - eigval_series.push(rand_mat.complex_eigenvalues()); - } - eigval_series }*/ \ No newline at end of file diff --git a/lang-trials/rust-benchmark/src/main.rs b/lang-trials/rust-benchmark/src/main.rs index 7143e20..9662dd7 100644 --- a/lang-trials/rust-benchmark/src/main.rs +++ b/lang-trials/rust-benchmark/src/main.rs @@ -1,7 +1,7 @@ use nalgebra::*; use std::f64::consts::PI as PI; use sycamore::{prelude::*, rt::{JsCast, JsValue}}; -use web_sys::{console, window}; +use web_sys::window; mod engine; @@ -10,14 +10,6 @@ fn main() { #[cfg(feature = "console_error_panic_hook")] console_error_panic_hook::set_once(); - /*console::log_1(&"before test schur 60".into());*/ - /*let test_rand_mat = OMatrix::::identity();*/ - /*let test_rot_step = OMatrix::::identity();*/ - /*let test_schur = test_rand_mat.schur(); - console::log_1(&format!("after test schur").into()); - let test_eigvals = test_schur.complex_eigenvalues(); - console::log_1(&format!("after test eigenvalues").into());*/ - sycamore::render(|| { let time_res: usize = 100; let time_step = create_signal(0.0);