From 5309eafc9a90c5147d93e4065d6f548d44a1fc42 Mon Sep 17 00:00:00 2001 From: bakk Date: Mon, 24 May 2021 21:51:25 +0200 Subject: [PATCH] Keep previous variable value after integration using the same identifier --- kalk/src/calculus.rs | 11 +++++++++++ kalk/src/symbol_table.rs | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/kalk/src/calculus.rs b/kalk/src/calculus.rs index 1e3b0b7..c8d05cc 100644 --- a/kalk/src/calculus.rs +++ b/kalk/src/calculus.rs @@ -79,6 +79,9 @@ fn simpsons_rule( integration_variable: &str, ) -> Result { let mut result = KalkNum::default(); + let original_variable_value = context + .symbol_table + .get_and_remove_var(integration_variable); const N: i32 = 900; let a = interpreter::eval_expr(context, a_expr, "")?; @@ -105,6 +108,14 @@ fn simpsons_rule( result.imaginary_value += mul.imaginary_value; } + if let Some(value) = original_variable_value { + context.symbol_table.insert(value); + } else { + context + .symbol_table + .get_and_remove_var(integration_variable); + } + Ok(result.mul_without_unit(KalkNum::new_with_imaginary( 3f64 / 8f64 * h.value, &h.unit, diff --git a/kalk/src/symbol_table.rs b/kalk/src/symbol_table.rs index 784093a..fc78764 100644 --- a/kalk/src/symbol_table.rs +++ b/kalk/src/symbol_table.rs @@ -81,6 +81,10 @@ impl SymbolTable { } } + pub fn get_and_remove_var(&mut self, identifier: &str) -> Option { + self.hashmap.remove(&format!("var.{}", identifier)) + } + pub fn contains_var(&self, identifier: &str) -> bool { prelude::CONSTANTS.contains_key(identifier) || identifier == "i"