Compare commits
13 Commits
dab002bd15
...
41b50eb893
| Author | SHA1 | Date | |
|---|---|---|---|
| 41b50eb893 | |||
| e497987573 | |||
| 65ab0c6a1f | |||
| f6a09fe449 | |||
| b08a727fe3 | |||
| 5480522ddb | |||
| 07858b229f | |||
| 3288752dfb | |||
| 3305227ffe | |||
| df05601e26 | |||
| 48fd49e386 | |||
| c7760e2123 | |||
| 2d7c987f11 |
@ -8,81 +8,81 @@ use criterion::{criterion_group, criterion_main, Criterion};
|
|||||||
use pprof::ProfilerGuard;
|
use pprof::ProfilerGuard;
|
||||||
|
|
||||||
pub struct FlamegraphProfiler<'a> {
|
pub struct FlamegraphProfiler<'a> {
|
||||||
frequency: c_int,
|
frequency: c_int,
|
||||||
active_profiler: Option<ProfilerGuard<'a>>,
|
active_profiler: Option<ProfilerGuard<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FlamegraphProfiler<'a> {
|
impl<'a> FlamegraphProfiler<'a> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn new(frequency: c_int) -> Self {
|
pub fn new(frequency: c_int) -> Self {
|
||||||
FlamegraphProfiler {
|
FlamegraphProfiler {
|
||||||
frequency,
|
frequency,
|
||||||
active_profiler: None,
|
active_profiler: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Profiler for FlamegraphProfiler<'a> {
|
impl<'a> Profiler for FlamegraphProfiler<'a> {
|
||||||
fn start_profiling(&mut self, _benchmark_id: &str, _benchmark_dir: &Path) {
|
fn start_profiling(&mut self, _benchmark_id: &str, _benchmark_dir: &Path) {
|
||||||
self.active_profiler = Some(ProfilerGuard::new(self.frequency).unwrap());
|
self.active_profiler = Some(ProfilerGuard::new(self.frequency).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop_profiling(&mut self, _benchmark_id: &str, benchmark_dir: &Path) {
|
fn stop_profiling(&mut self, _benchmark_id: &str, benchmark_dir: &Path) {
|
||||||
std::fs::create_dir_all(benchmark_dir).unwrap();
|
std::fs::create_dir_all(benchmark_dir).unwrap();
|
||||||
let flamegraph_path = benchmark_dir.join("flamegraph.svg");
|
let flamegraph_path = benchmark_dir.join("flamegraph.svg");
|
||||||
let flamegraph_file = File::create(&flamegraph_path)
|
let flamegraph_file = File::create(&flamegraph_path)
|
||||||
.expect("File system error while creating flamegraph.svg");
|
.expect("File system error while creating flamegraph.svg");
|
||||||
if let Some(profiler) = self.active_profiler.take() {
|
if let Some(profiler) = self.active_profiler.take() {
|
||||||
profiler
|
profiler
|
||||||
.report()
|
.report()
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.flamegraph(flamegraph_file)
|
.flamegraph(flamegraph_file)
|
||||||
.expect("Error writing flamegraph");
|
.expect("Error writing flamegraph");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)] // this infact IS used by benchmarks
|
#[allow(dead_code)] // this infact IS used by benchmarks
|
||||||
fn custom_criterion() -> Criterion {
|
fn custom_criterion() -> Criterion {
|
||||||
Criterion::default()
|
Criterion::default()
|
||||||
.warm_up_time(Duration::from_millis(250))
|
.warm_up_time(Duration::from_millis(250))
|
||||||
.sample_size(1000)
|
.sample_size(1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)] // this infact IS used by benchmarks
|
#[allow(dead_code)] // this infact IS used by benchmarks
|
||||||
fn custom_criterion_flamegraph() -> Criterion {
|
fn custom_criterion_flamegraph() -> Criterion {
|
||||||
custom_criterion().with_profiler(FlamegraphProfiler::new(100))
|
custom_criterion().with_profiler(FlamegraphProfiler::new(100))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mutli_split_function(c: &mut Criterion) {
|
fn mutli_split_function(c: &mut Criterion) {
|
||||||
let data_chars = vec![
|
let data_chars = vec![
|
||||||
"sin(x)cos(x)",
|
"sin(x)cos(x)",
|
||||||
"x^2",
|
"x^2",
|
||||||
"2x",
|
"2x",
|
||||||
"log10(x)",
|
"log10(x)",
|
||||||
"E^x",
|
"E^x",
|
||||||
"xxxxx",
|
"xxxxx",
|
||||||
"xsin(x)",
|
"xsin(x)",
|
||||||
"(2x+1)(3x+1)",
|
"(2x+1)(3x+1)",
|
||||||
"x**2",
|
"x**2",
|
||||||
"pipipipipipix",
|
"pipipipipipix",
|
||||||
"pi(2x+1)",
|
"pi(2x+1)",
|
||||||
"(2x+1)pi",
|
"(2x+1)pi",
|
||||||
]
|
]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| a.chars().collect::<Vec<char>>())
|
.map(|a| a.chars().collect::<Vec<char>>())
|
||||||
.collect::<Vec<Vec<char>>>();
|
.collect::<Vec<Vec<char>>>();
|
||||||
|
|
||||||
let mut group = c.benchmark_group("split_function");
|
let mut group = c.benchmark_group("split_function");
|
||||||
for entry in data_chars {
|
for entry in data_chars {
|
||||||
group.bench_function(entry.iter().collect::<String>(), |b| {
|
group.bench_function(entry.iter().collect::<String>(), |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
split_function_chars(&entry, SplitType::Multiplication);
|
split_function_chars(&entry, SplitType::Multiplication);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
group.finish();
|
group.finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uncomment to enable flamegraph profiling
|
// Uncomment to enable flamegraph profiling
|
||||||
|
|||||||
2
build.rs
2
build.rs
@ -6,8 +6,8 @@ use allsorts::{
|
|||||||
tag,
|
tag,
|
||||||
};
|
};
|
||||||
use epaint::{
|
use epaint::{
|
||||||
text::{FontData, FontDefinitions, FontTweak},
|
|
||||||
FontFamily,
|
FontFamily,
|
||||||
|
text::{FontData, FontDefinitions, FontTweak},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
collections::BTreeMap,
|
collections::BTreeMap,
|
||||||
|
|||||||
@ -4,21 +4,14 @@ use std::collections::HashMap;
|
|||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub struct FlatExWrapper {
|
pub struct FlatExWrapper {
|
||||||
func: Option<FlatEx<f64>>,
|
func: Option<FlatEx<f64>>,
|
||||||
func_str: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FlatExWrapper {
|
impl FlatExWrapper {
|
||||||
const EMPTY: FlatExWrapper = FlatExWrapper {
|
const EMPTY: FlatExWrapper = FlatExWrapper { func: None };
|
||||||
func: None,
|
|
||||||
func_str: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
const fn new(f: FlatEx<f64>) -> Self {
|
const fn new(f: FlatEx<f64>) -> Self {
|
||||||
Self {
|
Self { func: Some(f) }
|
||||||
func: Some(f),
|
|
||||||
func_str: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -34,26 +27,6 @@ impl FlatExWrapper {
|
|||||||
.unwrap_or(f64::NAN)
|
.unwrap_or(f64::NAN)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn partial(&self, x: usize) -> Self {
|
|
||||||
self.func
|
|
||||||
.as_ref()
|
|
||||||
.map(|f| f.clone().partial(x).map(Self::new).unwrap_or(Self::EMPTY))
|
|
||||||
.unwrap_or(Self::EMPTY)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn get_string(&mut self) -> String {
|
|
||||||
match self.func_str {
|
|
||||||
Some(ref func_str) => func_str.clone(),
|
|
||||||
None => {
|
|
||||||
let calculated = self.func.as_ref().map(|f| f.unparse()).unwrap_or("");
|
|
||||||
self.func_str = Some(calculated.to_owned());
|
|
||||||
calculated.to_owned()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn partial_iter(&self, n: usize) -> Self {
|
fn partial_iter(&self, n: usize) -> Self {
|
||||||
self.func
|
self.func
|
||||||
@ -165,18 +138,6 @@ impl BackingFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prettyify_function_str(func: &str) -> String {
|
|
||||||
let new_str = func.replace("{x}", "x");
|
|
||||||
|
|
||||||
if &new_str == "0/0" {
|
|
||||||
"Undefined".to_owned()
|
|
||||||
} else {
|
|
||||||
new_str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pub const VALID_VARIABLES: [char; 3] = ['x', 'e', 'π'];
|
|
||||||
|
|
||||||
/// Case insensitive checks for if `c` is a character used to represent a variable
|
/// Case insensitive checks for if `c` is a character used to represent a variable
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn is_variable(c: &char) -> bool {
|
pub const fn is_variable(c: &char) -> bool {
|
||||||
|
|||||||
@ -62,46 +62,73 @@ impl BoolSlice {
|
|||||||
self.number && !self.masked_num
|
self.number && !self.masked_num
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if this char is a function name letter (not a standalone variable)
|
||||||
|
const fn is_function_letter(&self) -> bool {
|
||||||
|
self.letter && !self.is_unmasked_variable()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if this is a "term" - something that can be multiplied
|
||||||
|
const fn is_term(&self) -> bool {
|
||||||
|
self.is_unmasked_number() || self.is_unmasked_variable() || self.letter
|
||||||
|
}
|
||||||
|
|
||||||
const fn calculate_mask(&mut self, other: &BoolSlice) {
|
const fn calculate_mask(&mut self, other: &BoolSlice) {
|
||||||
if other.masked_num && self.number {
|
if other.masked_num && self.number {
|
||||||
// If previous char was a masked number, and current char is a number, mask current char's variable status
|
// Propagate number masking through consecutive digits
|
||||||
self.masked_num = true;
|
self.masked_num = true;
|
||||||
} else if other.masked_var && self.variable {
|
} else if other.masked_var && self.variable {
|
||||||
// If previous char was a masked variable, and current char is a variable, mask current char's variable status
|
// Propagate variable masking through consecutive variables
|
||||||
self.masked_var = true;
|
self.masked_var = true;
|
||||||
} else if other.letter && !other.is_unmasked_variable() {
|
} else if other.is_function_letter() {
|
||||||
|
// After a function letter, mask following numbers/variables as part of function name
|
||||||
self.masked_num = self.number;
|
self.masked_num = self.number;
|
||||||
self.masked_var = self.variable;
|
self.masked_var = self.variable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn splitable(&self, c: &char, other: &BoolSlice, split: &SplitType) -> bool {
|
/// Determines if we should split (insert implicit multiplication) before current char
|
||||||
if (*c == '*') | (matches!(split, &SplitType::Term) && other.open_parens) {
|
const fn splitable(&self, c: &char, prev: &BoolSlice, split: &SplitType) -> bool {
|
||||||
true
|
// Always split on explicit multiplication
|
||||||
} else if other.closing_parens {
|
if *c == '*' {
|
||||||
// Cases like `)x`, `)2`, and `)(`
|
|
||||||
return (*c == '(')
|
|
||||||
| (self.letter && !self.is_unmasked_variable())
|
|
||||||
| self.is_unmasked_variable()
|
|
||||||
| self.is_unmasked_number();
|
|
||||||
} else if *c == '(' {
|
|
||||||
// Cases like `x(` and `2(`
|
|
||||||
return (other.is_unmasked_variable() | other.is_unmasked_number()) && !other.letter;
|
|
||||||
} else if other.is_unmasked_number() {
|
|
||||||
// Cases like `2x` and `2sin(x)`
|
|
||||||
return self.is_unmasked_variable() | self.letter;
|
|
||||||
} else if self.is_unmasked_variable() | self.letter {
|
|
||||||
// Cases like `e2` and `xx`
|
|
||||||
return other.is_unmasked_number()
|
|
||||||
| (other.is_unmasked_variable() && self.is_unmasked_variable())
|
|
||||||
| other.is_unmasked_variable();
|
|
||||||
} else if (self.is_unmasked_number() | self.letter | self.is_unmasked_variable())
|
|
||||||
&& (other.is_unmasked_number() | other.letter)
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return self.is_unmasked_number() && other.is_unmasked_variable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For Term split type, also split after open parens
|
||||||
|
if matches!(split, &SplitType::Term) && prev.open_parens {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// After closing paren: split before `(`, letters, variables, or numbers
|
||||||
|
// e.g., `)x`, `)2`, `)(`, `)sin`
|
||||||
|
if prev.closing_parens {
|
||||||
|
return *c == '(' || self.is_term();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Before open paren: split if previous was a standalone number or variable
|
||||||
|
// e.g., `x(`, `2(` but not `sin(`
|
||||||
|
if *c == '(' {
|
||||||
|
return (prev.is_unmasked_variable() || prev.is_unmasked_number()) && !prev.letter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// After a number: split before variables or function letters
|
||||||
|
// e.g., `2x`, `2sin`
|
||||||
|
if prev.is_unmasked_number() {
|
||||||
|
return self.is_unmasked_variable() || self.letter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current is a variable or letter: split if previous was a number or variable
|
||||||
|
// e.g., `e2`, `xx`, `xe`
|
||||||
|
if self.is_unmasked_variable() || self.letter {
|
||||||
|
return prev.is_unmasked_number() || prev.is_unmasked_variable();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current is a number after a variable
|
||||||
|
// e.g., `x2`
|
||||||
|
if self.is_unmasked_number() && prev.is_unmasked_variable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
use crate::math_app::AppSettings;
|
use crate::math_app::AppSettings;
|
||||||
use crate::misc::{EguiHelper, newtons_method_helper, step_helper};
|
use crate::misc::{EguiHelper, newtons_method_helper, step_helper};
|
||||||
|
use crate::symbolic::try_symbolic;
|
||||||
use egui::{Checkbox, Context};
|
use egui::{Checkbox, Context};
|
||||||
use egui_plot::{Bar, BarChart, PlotPoint, PlotUi};
|
use egui_plot::{Bar, BarChart, PlotPoint, PlotUi, Points};
|
||||||
|
|
||||||
use epaint::Color32;
|
use epaint::Color32;
|
||||||
use parsing::{AutoComplete, generate_hint};
|
use parsing::{AutoComplete, generate_hint};
|
||||||
@ -43,7 +44,7 @@ pub struct FunctionEntry {
|
|||||||
/// If displaying derivatives are enabled (note, they are still calculated for other purposes)
|
/// If displaying derivatives are enabled (note, they are still calculated for other purposes)
|
||||||
pub derivative: bool,
|
pub derivative: bool,
|
||||||
|
|
||||||
pub nth_derviative: bool,
|
pub nth_derivative: bool,
|
||||||
|
|
||||||
pub back_data: Vec<PlotPoint>,
|
pub back_data: Vec<PlotPoint>,
|
||||||
pub integral_data: Option<(Vec<Bar>, f64)>,
|
pub integral_data: Option<(Vec<Bar>, f64)>,
|
||||||
@ -64,7 +65,7 @@ impl Hash for FunctionEntry {
|
|||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
self.raw_func_str.hash(state);
|
self.raw_func_str.hash(state);
|
||||||
self.integral.hash(state);
|
self.integral.hash(state);
|
||||||
self.nth_derviative.hash(state);
|
self.nth_derivative.hash(state);
|
||||||
self.curr_nth.hash(state);
|
self.curr_nth.hash(state);
|
||||||
self.settings_opened.hash(state);
|
self.settings_opened.hash(state);
|
||||||
}
|
}
|
||||||
@ -128,7 +129,7 @@ impl Default for FunctionEntry {
|
|||||||
raw_func_str: String::new(),
|
raw_func_str: String::new(),
|
||||||
integral: false,
|
integral: false,
|
||||||
derivative: false,
|
derivative: false,
|
||||||
nth_derviative: false,
|
nth_derivative: false,
|
||||||
back_data: Vec::new(),
|
back_data: Vec::new(),
|
||||||
integral_data: None,
|
integral_data: None,
|
||||||
derivative_data: Vec::new(),
|
derivative_data: Vec::new(),
|
||||||
@ -157,7 +158,7 @@ impl FunctionEntry {
|
|||||||
.collapsible(false)
|
.collapsible(false)
|
||||||
.show(ctx, |ui| {
|
.show(ctx, |ui| {
|
||||||
ui.add(Checkbox::new(
|
ui.add(Checkbox::new(
|
||||||
&mut self.nth_derviative,
|
&mut self.nth_derivative,
|
||||||
"Display Nth Derivative",
|
"Display Nth Derivative",
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -252,27 +253,33 @@ impl FunctionEntry {
|
|||||||
) -> Vec<PlotPoint> {
|
) -> Vec<PlotPoint> {
|
||||||
self.function.generate_derivative(derivative_level);
|
self.function.generate_derivative(derivative_level);
|
||||||
self.function.generate_derivative(derivative_level + 1);
|
self.function.generate_derivative(derivative_level + 1);
|
||||||
let newtons_method_output: Vec<f64> = match derivative_level {
|
|
||||||
0 => newtons_method_helper(
|
let data_source = match derivative_level {
|
||||||
threshold,
|
0 => self.back_data.as_slice(),
|
||||||
range,
|
1 => self.derivative_data.as_slice(),
|
||||||
self.back_data.as_slice(),
|
|
||||||
self.function.get_function_derivative(0),
|
|
||||||
self.function.get_function_derivative(1),
|
|
||||||
),
|
|
||||||
1 => newtons_method_helper(
|
|
||||||
threshold,
|
|
||||||
range,
|
|
||||||
self.derivative_data.as_slice(),
|
|
||||||
self.function.get_function_derivative(1),
|
|
||||||
self.function.get_function_derivative(2),
|
|
||||||
),
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
newtons_method_output
|
newtons_method_helper(
|
||||||
.into_iter()
|
threshold,
|
||||||
.map(|x| PlotPoint::new(x, self.function.get(0, x)))
|
range,
|
||||||
|
data_source,
|
||||||
|
self.function.get_function_derivative(derivative_level),
|
||||||
|
self.function.get_function_derivative(derivative_level + 1),
|
||||||
|
)
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| PlotPoint::new(x, self.function.get(0, x)))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates plot data for a given derivative level over the resolution iterator
|
||||||
|
fn generate_plot_data(&mut self, derivative: usize, resolution_iter: &[f64]) -> Vec<PlotPoint> {
|
||||||
|
if derivative > 0 {
|
||||||
|
self.function.generate_derivative(derivative);
|
||||||
|
}
|
||||||
|
resolution_iter
|
||||||
|
.iter()
|
||||||
|
.map(|&x| PlotPoint::new(x, self.function.get(derivative, x)))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,32 +311,17 @@ impl FunctionEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.back_data.is_empty() {
|
if self.back_data.is_empty() {
|
||||||
let data: Vec<PlotPoint> = resolution_iter
|
self.back_data = self.generate_plot_data(0, &resolution_iter);
|
||||||
.clone()
|
debug_assert_eq!(self.back_data.len(), settings.plot_width + 1);
|
||||||
.into_iter()
|
|
||||||
.map(|x| PlotPoint::new(x, self.function.get(0, x)))
|
|
||||||
.collect();
|
|
||||||
debug_assert_eq!(data.len(), settings.plot_width + 1);
|
|
||||||
|
|
||||||
self.back_data = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.derivative_data.is_empty() {
|
if self.derivative_data.is_empty() {
|
||||||
self.function.generate_derivative(1);
|
self.derivative_data = self.generate_plot_data(1, &resolution_iter);
|
||||||
let data: Vec<PlotPoint> = resolution_iter
|
debug_assert_eq!(self.derivative_data.len(), settings.plot_width + 1);
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.map(|x| PlotPoint::new(x, self.function.get(1, x)))
|
|
||||||
.collect();
|
|
||||||
debug_assert_eq!(data.len(), settings.plot_width + 1);
|
|
||||||
self.derivative_data = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.nth_derviative && self.nth_derivative_data.is_none() {
|
if self.nth_derivative && self.nth_derivative_data.is_none() {
|
||||||
let data: Vec<PlotPoint> = resolution_iter
|
let data = self.generate_plot_data(self.curr_nth, &resolution_iter);
|
||||||
.into_iter()
|
|
||||||
.map(|x| PlotPoint::new(x, self.function.get(self.curr_nth, x)))
|
|
||||||
.collect();
|
|
||||||
debug_assert_eq!(data.len(), settings.plot_width + 1);
|
debug_assert_eq!(data.len(), settings.plot_width + 1);
|
||||||
self.nth_derivative_data = Some(data);
|
self.nth_derivative_data = Some(data);
|
||||||
}
|
}
|
||||||
@ -352,7 +344,7 @@ impl FunctionEntry {
|
|||||||
self.clear_integral();
|
self.clear_integral();
|
||||||
}
|
}
|
||||||
|
|
||||||
let threshold: f64 = resolution / 2.0;
|
let threshold: f64 = f64::EPSILON;
|
||||||
let x_range = settings.min_x..settings.max_x;
|
let x_range = settings.min_x..settings.max_x;
|
||||||
|
|
||||||
// Calculates extrema
|
// Calculates extrema
|
||||||
@ -385,7 +377,28 @@ impl FunctionEntry {
|
|||||||
let step = (settings.max_x - settings.min_x) / (settings.plot_width as f64);
|
let step = (settings.max_x - settings.min_x) / (settings.plot_width as f64);
|
||||||
debug_assert!(step > 0.0);
|
debug_assert!(step > 0.0);
|
||||||
|
|
||||||
// Plot back data
|
// Collect special points (extrema and roots) for exclusion from line hover detection
|
||||||
|
let special_points: Vec<&PlotPoint> = {
|
||||||
|
let mut points = Vec::new();
|
||||||
|
if settings.do_extrema {
|
||||||
|
points.extend(self.extrema_data.iter());
|
||||||
|
}
|
||||||
|
if settings.do_roots {
|
||||||
|
points.extend(self.root_data.iter());
|
||||||
|
}
|
||||||
|
points
|
||||||
|
};
|
||||||
|
|
||||||
|
// Helper to check if a point is near any special point
|
||||||
|
// Uses a radius in x-axis units based on the step size
|
||||||
|
let exclusion_radius = step * 3.0; // Exclude points within 3 steps of special points
|
||||||
|
let is_near_special_point = |p: &PlotPoint| -> bool {
|
||||||
|
special_points
|
||||||
|
.iter()
|
||||||
|
.any(|sp| (p.x - sp.x).abs() < exclusion_radius)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Plot back data, filtering out points near special points for better hover detection
|
||||||
if !self.back_data.is_empty() {
|
if !self.back_data.is_empty() {
|
||||||
if self.integral && (step >= integral_step) {
|
if self.integral && (step >= integral_step) {
|
||||||
plot_ui.line(
|
plot_ui.line(
|
||||||
@ -403,45 +416,78 @@ impl FunctionEntry {
|
|||||||
.fill(0.0),
|
.fill(0.0),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter out points near special points so cursor snaps to them more easily
|
||||||
|
let filtered_back_data: Vec<PlotPoint> = self
|
||||||
|
.back_data
|
||||||
|
.iter()
|
||||||
|
.filter(|p| !is_near_special_point(p))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
plot_ui.line(
|
plot_ui.line(
|
||||||
self.back_data
|
filtered_back_data
|
||||||
.clone()
|
|
||||||
.to_line()
|
.to_line()
|
||||||
.stroke(egui::Stroke::new(4.0, main_plot_color)),
|
.stroke(egui::Stroke::new(4.0, main_plot_color)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plot derivative data
|
// Plot derivative data, also filtering near special points
|
||||||
if self.derivative && !self.derivative_data.is_empty() {
|
if self.derivative && !self.derivative_data.is_empty() {
|
||||||
plot_ui.line(self.derivative_data.clone().to_line().color(Color32::GREEN));
|
let filtered_derivative_data: Vec<PlotPoint> = self
|
||||||
|
.derivative_data
|
||||||
|
.iter()
|
||||||
|
.filter(|p| !is_near_special_point(p))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
plot_ui.line(filtered_derivative_data.to_line().color(Color32::GREEN));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plot extrema points
|
// Plot extrema points
|
||||||
if settings.do_extrema && !self.extrema_data.is_empty() {
|
if settings.do_extrema && !self.extrema_data.is_empty() {
|
||||||
plot_ui.points(
|
for point in &self.extrema_data {
|
||||||
self.extrema_data
|
let name = format!(
|
||||||
.clone()
|
"({}, {})",
|
||||||
.to_points()
|
try_symbolic(point.x)
|
||||||
.color(Color32::YELLOW)
|
.map(|s| s.to_string())
|
||||||
.radius(5.0), // Radius of points of Extrema
|
.unwrap_or_else(|| format!("{:.4}", point.x)),
|
||||||
);
|
try_symbolic(point.y)
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.unwrap_or_else(|| format!("{:.4}", point.y))
|
||||||
|
);
|
||||||
|
plot_ui.points(
|
||||||
|
Points::new(name, vec![[point.x, point.y]])
|
||||||
|
.color(Color32::YELLOW)
|
||||||
|
.radius(5.0),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plot roots points
|
// Plot roots points
|
||||||
if settings.do_roots && !self.root_data.is_empty() {
|
if settings.do_roots && !self.root_data.is_empty() {
|
||||||
plot_ui.points(
|
for point in &self.root_data {
|
||||||
self.root_data
|
let name = format!(
|
||||||
.clone()
|
"({}, {})",
|
||||||
.to_points()
|
try_symbolic(point.x)
|
||||||
.color(Color32::LIGHT_BLUE)
|
.map(|s| s.to_string())
|
||||||
.radius(5.0), // Radius of points of Roots
|
.unwrap_or_else(|| format!("{:.4}", point.x)),
|
||||||
);
|
try_symbolic(point.y)
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.unwrap_or_else(|| format!("{:.4}", point.y))
|
||||||
|
);
|
||||||
|
plot_ui.points(
|
||||||
|
Points::new(name, vec![[point.x, point.y]])
|
||||||
|
.color(Color32::LIGHT_BLUE)
|
||||||
|
.radius(5.0),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.nth_derviative
|
if self.nth_derivative
|
||||||
&& let Some(ref nth_derviative) = self.nth_derivative_data
|
&& let Some(ref nth_derivative) = self.nth_derivative_data
|
||||||
{
|
{
|
||||||
plot_ui.line(nth_derviative.clone().to_line().color(Color32::DARK_RED));
|
plot_ui.line(nth_derivative.clone().to_line().color(Color32::DARK_RED));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plot integral data
|
// Plot integral data
|
||||||
|
|||||||
@ -12,7 +12,7 @@ mod widgets;
|
|||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
function_entry::{FunctionEntry, Riemann},
|
function_entry::{FunctionEntry, Riemann},
|
||||||
math_app::AppSettings,
|
math_app::{AppSettings, MathApp},
|
||||||
misc::{EguiHelper, newtons_method, option_vec_printer, step_helper},
|
misc::{EguiHelper, newtons_method, option_vec_printer, step_helper},
|
||||||
unicode_helper::{to_chars_array, to_unicode_hash},
|
unicode_helper::{to_chars_array, to_unicode_hash},
|
||||||
};
|
};
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@ -1,14 +1,3 @@
|
|||||||
#[macro_use]
|
|
||||||
extern crate static_assertions;
|
|
||||||
|
|
||||||
mod consts;
|
|
||||||
mod function_entry;
|
|
||||||
mod function_manager;
|
|
||||||
mod math_app;
|
|
||||||
mod misc;
|
|
||||||
mod unicode_helper;
|
|
||||||
mod widgets;
|
|
||||||
|
|
||||||
// For running the program natively! (Because why not?)
|
// For running the program natively! (Because why not?)
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn main() -> eframe::Result<()> {
|
fn main() -> eframe::Result<()> {
|
||||||
@ -21,6 +10,6 @@ fn main() -> eframe::Result<()> {
|
|||||||
eframe::run_native(
|
eframe::run_native(
|
||||||
"(Yet-to-be-named) Graphing Software",
|
"(Yet-to-be-named) Graphing Software",
|
||||||
eframe::NativeOptions::default(),
|
eframe::NativeOptions::default(),
|
||||||
Box::new(|cc| Ok(Box::new(math_app::MathApp::new(cc)))),
|
Box::new(|cc| Ok(Box::new(ytbn_graphing_software::MathApp::new(cc)))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
104
src/math_app.rs
104
src/math_app.rs
@ -3,6 +3,7 @@ use crate::{
|
|||||||
function_entry::Riemann,
|
function_entry::Riemann,
|
||||||
function_manager::FunctionManager,
|
function_manager::FunctionManager,
|
||||||
misc::option_vec_printer,
|
misc::option_vec_printer,
|
||||||
|
widgets::toggle_button,
|
||||||
};
|
};
|
||||||
use eframe::App;
|
use eframe::App;
|
||||||
use egui::{
|
use egui::{
|
||||||
@ -14,7 +15,7 @@ use egui_plot::Plot;
|
|||||||
use emath::{Align, Align2};
|
use emath::{Align, Align2};
|
||||||
use epaint::Margin;
|
use epaint::Margin;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::{io::Read, ops::BitXorAssign};
|
use std::io::Read;
|
||||||
|
|
||||||
/// Stores current settings/state of [`MathApp`]
|
/// Stores current settings/state of [`MathApp`]
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -134,15 +135,6 @@ impl MathApp {
|
|||||||
|
|
||||||
tracing::info!("Web Info: {:?}", &cc.integration_info.web_info);
|
tracing::info!("Web Info: {:?}", &cc.integration_info.web_info);
|
||||||
|
|
||||||
fn get_storage_decompressed() -> Option<Vec<u8>> {
|
|
||||||
let data = get_localstorage().get_item(DATA_NAME).ok()??;
|
|
||||||
let cached_data = crate::misc::hashed_storage_read(&data)?;
|
|
||||||
|
|
||||||
tracing::info!("Reading decompression cache. Bytes: {}", cached_data.len());
|
|
||||||
return Some(cached_data);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_functions() -> Option<FunctionManager> {
|
fn load_functions() -> Option<FunctionManager> {
|
||||||
let data = get_localstorage().get_item(FUNC_NAME).ok()??;
|
let data = get_localstorage().get_item(FUNC_NAME).ok()??;
|
||||||
let func_data = crate::misc::hashed_storage_read(&data)?;
|
let func_data = crate::misc::hashed_storage_read(&data)?;
|
||||||
@ -172,17 +164,6 @@ impl MathApp {
|
|||||||
.read_to_end(&mut data)
|
.read_to_end(&mut data)
|
||||||
.expect("unable to read compressed data");
|
.expect("unable to read compressed data");
|
||||||
|
|
||||||
#[cfg(target = "wasm32")]
|
|
||||||
{
|
|
||||||
tracing::info!("Setting decompression cache");
|
|
||||||
|
|
||||||
let saved_data = hashed_storage_create(data);
|
|
||||||
tracing::info!("Bytes: {}", saved_data.len());
|
|
||||||
get_localstorage()
|
|
||||||
.set_item(DATA_NAME, saved_data)
|
|
||||||
.expect("failed to set local storage cache");
|
|
||||||
}
|
|
||||||
|
|
||||||
bincode::deserialize(data.as_slice()).expect("unable to deserialize bincode")
|
bincode::deserialize(data.as_slice()).expect("unable to deserialize bincode")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,19 +171,7 @@ impl MathApp {
|
|||||||
|
|
||||||
// Initialize fonts
|
// Initialize fonts
|
||||||
// This used to be in the `update` method, but (after a ton of digging) this actually caused OOMs. that was a pain to debug
|
// This used to be in the `update` method, but (after a ton of digging) this actually caused OOMs. that was a pain to debug
|
||||||
cc.egui_ctx.set_fonts({
|
cc.egui_ctx.set_fonts({ decompress_fonts() });
|
||||||
#[cfg(target = "wasm32")]
|
|
||||||
if let Some(Ok(data)) =
|
|
||||||
get_storage_decompressed().map(|data| bincode::deserialize(data.as_slice()))
|
|
||||||
{
|
|
||||||
data
|
|
||||||
} else {
|
|
||||||
decompress_fonts()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target = "wasm32"))]
|
|
||||||
decompress_fonts()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Set dark mode by default
|
// Set dark mode by default
|
||||||
// cc.egui_ctx.set_visuals(crate::style::style());
|
// cc.egui_ctx.set_visuals(crate::style::style());
|
||||||
@ -319,22 +288,20 @@ impl MathApp {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
self.settings.do_extrema.bitxor_assign(
|
toggle_button(
|
||||||
ui.add(Button::new("Extrema"))
|
ui,
|
||||||
.on_hover_text(match self.settings.do_extrema {
|
&mut self.settings.do_extrema,
|
||||||
true => "Disable Displaying Extrema",
|
"Extrema",
|
||||||
false => "Display Extrema",
|
"Disable Displaying Extrema",
|
||||||
})
|
"Display Extrema",
|
||||||
.clicked(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
self.settings.do_roots.bitxor_assign(
|
toggle_button(
|
||||||
ui.add(Button::new("Roots"))
|
ui,
|
||||||
.on_hover_text(match self.settings.do_roots {
|
&mut self.settings.do_roots,
|
||||||
true => "Disable Displaying Roots",
|
"Roots",
|
||||||
false => "Display Roots",
|
"Disable Displaying Roots",
|
||||||
})
|
"Display Roots",
|
||||||
.clicked(),
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -376,22 +343,21 @@ impl App for MathApp {
|
|||||||
// If keyboard input isn't being grabbed, check for key combos
|
// If keyboard input isn't being grabbed, check for key combos
|
||||||
if !ctx.wants_keyboard_input() {
|
if !ctx.wants_keyboard_input() {
|
||||||
// If `H` key is pressed, toggle Side Panel
|
// If `H` key is pressed, toggle Side Panel
|
||||||
self.opened
|
if ctx.input_mut(|x| x.consume_key(egui::Modifiers::NONE, Key::H)) {
|
||||||
.side_panel
|
self.opened.side_panel = !self.opened.side_panel;
|
||||||
.bitxor_assign(ctx.input_mut(|x| x.consume_key(egui::Modifiers::NONE, Key::H)));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates Top bar that contains some general options
|
// Creates Top bar that contains some general options
|
||||||
TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
// Button in top bar to toggle showing the side panel
|
// Button in top bar to toggle showing the side panel
|
||||||
self.opened.side_panel.bitxor_assign(
|
toggle_button(
|
||||||
ui.add(Button::new("Panel"))
|
ui,
|
||||||
.on_hover_text(match self.opened.side_panel {
|
&mut self.opened.side_panel,
|
||||||
true => "Hide Side Panel",
|
"Panel",
|
||||||
false => "Show Side Panel",
|
"Hide Side Panel",
|
||||||
})
|
"Show Side Panel",
|
||||||
.clicked(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Button to add a new function
|
// Button to add a new function
|
||||||
@ -407,13 +373,12 @@ impl App for MathApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Toggles opening the Help window
|
// Toggles opening the Help window
|
||||||
self.opened.help.bitxor_assign(
|
toggle_button(
|
||||||
ui.add(Button::new("Help"))
|
ui,
|
||||||
.on_hover_text(match self.opened.help {
|
&mut self.opened.help,
|
||||||
true => "Close Help Window",
|
"Help",
|
||||||
false => "Open Help Window",
|
"Close Help Window",
|
||||||
})
|
"Open Help Window",
|
||||||
.clicked(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Display Area and time of last frame
|
// Display Area and time of last frame
|
||||||
@ -492,13 +457,8 @@ impl App for MathApp {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|(_, func)| func.get_test_result())
|
.map(|(_, func)| func.get_test_result())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(_, error)| error.is_some())
|
.filter_map(|(i, error)| error.as_ref().map(|x| (i, x)))
|
||||||
.map(|(i, error)| {
|
.map(|(i, error)| format!("(Function #{}) {}\n", i, error))
|
||||||
// use unwrap_unchecked as None Errors are already filtered out
|
|
||||||
unsafe {
|
|
||||||
format!("(Function #{}) {}\n", i, error.as_ref().unwrap_unchecked())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
if !errors_formatted.is_empty() {
|
if !errors_formatted.is_empty() {
|
||||||
|
|||||||
@ -91,9 +91,7 @@ pub fn newtons_method_helper(
|
|||||||
.filter(|(prev, curr)| prev.y.is_finite() && curr.y.is_finite())
|
.filter(|(prev, curr)| prev.y.is_finite() && curr.y.is_finite())
|
||||||
.filter(|(prev, curr)| prev.y.signum() != curr.y.signum())
|
.filter(|(prev, curr)| prev.y.signum() != curr.y.signum())
|
||||||
.map(|(start, _)| start.x)
|
.map(|(start, _)| start.x)
|
||||||
.map(|x| newtons_method(f, f_1, x, range, threshold))
|
.filter_map(|x| newtons_method(f, f_1, x, range, threshold))
|
||||||
.filter(|x| x.is_some())
|
|
||||||
.map(|x| unsafe { x.unwrap_unchecked() })
|
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::misc::Offset;
|
use crate::misc::Offset;
|
||||||
use egui::{Id, InnerResponse};
|
use egui::{Button, Id, InnerResponse, Ui};
|
||||||
|
use std::ops::BitXorAssign;
|
||||||
|
|
||||||
/// Creates an area ontop of a widget with an y offset
|
/// Creates an area ontop of a widget with an y offset
|
||||||
pub fn widgets_ontop<R>(
|
pub fn widgets_ontop<R>(
|
||||||
@ -15,3 +16,19 @@ pub fn widgets_ontop<R>(
|
|||||||
|
|
||||||
area.show(ui.ctx(), |ui| add_contents(ui))
|
area.show(ui.ctx(), |ui| add_contents(ui))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A toggle button that XORs its state when clicked.
|
||||||
|
/// Shows different hover text based on current state.
|
||||||
|
pub fn toggle_button(
|
||||||
|
ui: &mut Ui,
|
||||||
|
state: &mut bool,
|
||||||
|
label: &str,
|
||||||
|
enabled_tip: &str,
|
||||||
|
disabled_tip: &str,
|
||||||
|
) {
|
||||||
|
state.bitxor_assign(
|
||||||
|
ui.add(Button::new(label))
|
||||||
|
.on_hover_text(if *state { enabled_tip } else { disabled_tip })
|
||||||
|
.clicked(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user