Higher order derivatation

This commit is contained in:
bakk 2021-05-17 23:09:59 +02:00
parent ff6ad6a10d
commit 7f32829921
2 changed files with 15 additions and 14 deletions

View File

@ -26,7 +26,7 @@ pub enum Expr {
pub struct Identifier {
pub full_name: String,
pub pure_name: String,
pub prime_count: i32,
pub prime_count: u32,
}
impl Identifier {
@ -39,9 +39,17 @@ impl Identifier {
prime_count,
}
}
pub fn from_name_and_primes(pure_name: &str, prime_count: u32) -> Self {
Identifier {
full_name: format!("{}{}", pure_name, "'".repeat(prime_count as usize)),
pure_name: pure_name.into(),
prime_count,
}
}
}
fn separate_identifier_and_prime(identifier: &str) -> (String, i32) {
fn separate_identifier_and_prime(identifier: &str) -> (String, u32) {
let mut prim_count = 0;
let mut pure_identifier = identifier.to_string();

View File

@ -13,21 +13,14 @@ pub fn derive_func(
) -> Result<KalkNum, CalcError> {
const H: f64 = 0.000001;
let unit = &argument.unit.to_string();
let argument_with_h = Expr::Literal(argument.clone().add(context, H.into()).to_f64());
let argument_without_h = Expr::Literal(argument.sub(context, H.into()).to_f64());
let new_identifier = Identifier::from_name_and_primes(&name.pure_name, name.prime_count - 1);
let f_x_h = interpreter::eval_fn_call_expr(
context,
&Identifier::from_full_name(&name.pure_name),
&[argument_with_h],
unit,
)?;
let f_x = interpreter::eval_fn_call_expr(
context,
&Identifier::from_full_name(&name.pure_name),
&[argument_without_h],
unit,
)?;
let f_x_h = interpreter::eval_fn_call_expr(context, &new_identifier, &[argument_with_h], unit)?;
let f_x =
interpreter::eval_fn_call_expr(context, &new_identifier, &[argument_without_h], unit)?;
Ok(f_x_h.sub(context, f_x).div(context, (2f64 * H).into()))
}