mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-31 17:09:13 +01:00
Higher order derivatation
This commit is contained in:
parent
ece50bf92c
commit
1971793e08
@ -26,7 +26,7 @@ pub enum Expr {
|
|||||||
pub struct Identifier {
|
pub struct Identifier {
|
||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
pub pure_name: String,
|
pub pure_name: String,
|
||||||
pub prime_count: i32,
|
pub prime_count: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Identifier {
|
impl Identifier {
|
||||||
@ -39,9 +39,17 @@ impl Identifier {
|
|||||||
prime_count,
|
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 prim_count = 0;
|
||||||
let mut pure_identifier = identifier.to_string();
|
let mut pure_identifier = identifier.to_string();
|
||||||
|
|
||||||
|
@ -13,21 +13,14 @@ pub fn derive_func(
|
|||||||
) -> Result<KalkNum, CalcError> {
|
) -> Result<KalkNum, CalcError> {
|
||||||
const H: f64 = 0.000001;
|
const H: f64 = 0.000001;
|
||||||
let unit = &argument.unit.to_string();
|
let unit = &argument.unit.to_string();
|
||||||
|
|
||||||
let argument_with_h = Expr::Literal(argument.clone().add(context, H.into()).to_f64());
|
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 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(
|
let f_x_h = interpreter::eval_fn_call_expr(context, &new_identifier, &[argument_with_h], unit)?;
|
||||||
context,
|
let f_x =
|
||||||
&Identifier::from_full_name(&name.pure_name),
|
interpreter::eval_fn_call_expr(context, &new_identifier, &[argument_without_h], unit)?;
|
||||||
&[argument_with_h],
|
|
||||||
unit,
|
|
||||||
)?;
|
|
||||||
let f_x = interpreter::eval_fn_call_expr(
|
|
||||||
context,
|
|
||||||
&Identifier::from_full_name(&name.pure_name),
|
|
||||||
&[argument_without_h],
|
|
||||||
unit,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(f_x_h.sub(context, f_x).div(context, (2f64 * H).into()))
|
Ok(f_x_h.sub(context, f_x).div(context, (2f64 * H).into()))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user