logic improvements
This commit is contained in:
parent
dae135cd38
commit
c99e367512
@ -218,14 +218,15 @@ impl FunctionEntry {
|
||||
|
||||
let sum_func = self.get_sum_func(*sum);
|
||||
|
||||
let data2: Vec<(f64, f64)> = dyn_iter(&step_helper(*integral_num, integral_min_x, &step))
|
||||
let data2: Vec<(f64, f64)> = step_helper(*integral_num, integral_min_x, &step)
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
let step_offset = step * x.signum(); // store the offset here so it doesn't have to be calculated multiple times
|
||||
let x2: f64 = x + step_offset;
|
||||
|
||||
let (left_x, right_x) = match x.is_sign_positive() {
|
||||
true => (*x, x2),
|
||||
false => (x2, *x),
|
||||
true => (x, x2),
|
||||
false => (x2, x),
|
||||
};
|
||||
|
||||
let y = sum_func.get(left_x, right_x);
|
||||
@ -263,8 +264,8 @@ impl FunctionEntry {
|
||||
};
|
||||
|
||||
newtons_method_output
|
||||
.iter()
|
||||
.map(|x| Value::new(*x, self.function.get(*x)))
|
||||
.into_iter()
|
||||
.map(|x| Value::new(x, self.function.get(x)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@ -338,9 +339,14 @@ impl FunctionEntry {
|
||||
self.back_data = back_data;
|
||||
|
||||
if derivative_required {
|
||||
debug_assert!(derivative_data_1[0].is_some());
|
||||
/*
|
||||
debug_assert!(derivative_data_1.iter().any(|x| x.is_none()));
|
||||
self.derivative_data = unsafe {
|
||||
std::mem::transmute::<Vec<Option<Value>>, Vec<Value>>(derivative_data_1)
|
||||
};
|
||||
*/
|
||||
self.derivative_data = derivative_data_1
|
||||
.iter()
|
||||
.into_iter()
|
||||
.map(|ele| unsafe { ele.unwrap_unchecked() })
|
||||
.collect::<Vec<Value>>();
|
||||
} else {
|
||||
@ -348,10 +354,16 @@ impl FunctionEntry {
|
||||
}
|
||||
|
||||
if do_nth_derivative {
|
||||
/*
|
||||
debug_assert!(new_nth_derivative_data.iter().any(|x| x.is_none()));
|
||||
self.nth_derivative_data = Some(unsafe {
|
||||
std::mem::transmute::<Vec<Option<Value>>, Vec<Value>>(new_nth_derivative_data)
|
||||
});
|
||||
*/
|
||||
self.nth_derivative_data = Some(
|
||||
new_nth_derivative_data
|
||||
.iter()
|
||||
.map(|c| unsafe { c.unwrap_unchecked() })
|
||||
.into_iter()
|
||||
.map(|ele| unsafe { ele.unwrap_unchecked() })
|
||||
.collect(),
|
||||
);
|
||||
} else {
|
||||
@ -397,8 +409,11 @@ impl FunctionEntry {
|
||||
&settings.riemann_sum,
|
||||
&settings.integral_num,
|
||||
);
|
||||
self.integral_data =
|
||||
Some((data.iter().map(|(x, y)| Bar::new(*x, *y)).collect(), area));
|
||||
|
||||
self.integral_data = Some((
|
||||
data.into_iter().map(|(x, y)| Bar::new(x, y)).collect(),
|
||||
area,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
self.invalidate_integral();
|
||||
|
||||
@ -145,7 +145,7 @@ impl MathApp {
|
||||
|
||||
fn get_storage_decompressed() -> Option<Vec<u8>> {
|
||||
let data = get_localstorage().get_item(DATA_NAME).ok()??;
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?;
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(data)?;
|
||||
|
||||
debug_assert!(!commit.is_empty());
|
||||
debug_assert!(!cached_data.is_empty());
|
||||
@ -183,7 +183,7 @@ impl MathApp {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (commit, func_data) = crate::misc::hashed_storage_read(&data)?;
|
||||
let (commit, func_data) = crate::misc::hashed_storage_read(data)?;
|
||||
|
||||
debug_assert!(!commit.is_empty());
|
||||
debug_assert!(!func_data.is_empty());
|
||||
|
||||
@ -233,7 +233,7 @@ pub fn newtons_method_helper(
|
||||
threshold: &f64, range: &std::ops::Range<f64>, data: &[Value], f: &dyn Fn(f64) -> f64,
|
||||
f_1: &dyn Fn(f64) -> f64,
|
||||
) -> Vec<f64> {
|
||||
data.iter()
|
||||
data.into_iter()
|
||||
.tuple_windows()
|
||||
.filter(|(prev, curr)| prev.y.is_finite() && curr.y.is_finite())
|
||||
.filter(|(prev, curr)| prev.y.signum() != curr.y.signum())
|
||||
@ -337,7 +337,7 @@ pub fn hashed_storage_create(hash: HashBytes, data: &[u8]) -> String {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
|
||||
pub fn hashed_storage_read(data: String) -> Option<(HashBytes, Vec<u8>)> {
|
||||
if HASH_LENGTH >= data.len() {
|
||||
return None;
|
||||
}
|
||||
@ -347,7 +347,7 @@ pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
|
||||
assume(data.len() > HASH_LENGTH);
|
||||
}
|
||||
|
||||
let decoded_1: Vec<u8> = unsafe { std::mem::transmute::<&str, &[u8]>(data) }.to_vec();
|
||||
let decoded_1: Vec<u8> = unsafe { std::mem::transmute::<String, Vec<u8>>(data) };
|
||||
|
||||
let hash: HashBytes = unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) };
|
||||
let cached_data = decoded_1[HASH_LENGTH..].to_vec();
|
||||
|
||||
@ -105,14 +105,14 @@ fn hashed_storage() {
|
||||
data.as_slice(),
|
||||
);
|
||||
|
||||
let read = hashed_storage_read(&storage);
|
||||
let read = hashed_storage_read(storage);
|
||||
assert_eq!(read.map(|(a, b)| (a.to_vec(), b)), Some((commit, data)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_hashed_storage() {
|
||||
use ytbn_graphing_software::hashed_storage_read;
|
||||
assert_eq!(hashed_storage_read("aaaa"), None);
|
||||
assert_eq!(hashed_storage_read("aaaa".to_owned()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user