mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-11-08 08:54:57 +01:00
Higher order derivatation
This commit is contained in:
parent
ff6ad6a10d
commit
7f32829921
@ -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();
|
||||
|
||||
|
@ -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()))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user