Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff #51

Merged
merged 4 commits into from
Jun 5, 2024
Merged

Diff #51

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/functions/minus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use gnuplot::palettes::PLASMA;

use crate::exact_math::rationals::Rationals;
use crate::exact_math::symbolic::size;
use crate::functions::function::apply_operator;
Expand Down
155 changes: 96 additions & 59 deletions src/interpreting/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::f64::consts::{E, PI};
use gnuplot::{AxesCommon, Figure};

use crate::configuration::loader::{load, load_config, Config};
use crate::functions::divide::divide;
use crate::functions::minus::minus;
use crate::interpreting::interpreter::interpret;
use crate::parsing::ast::{Ast, Parameters, Parameters::*};
Expand Down Expand Up @@ -1569,6 +1570,7 @@ pub fn diff(
}

let first_param = p.first().unwrap();
let second_param = p.len() > 1;

let mut c: HashMap<String, Parameters> = HashMap::new();
for (key, ele) in ram.as_deref().unwrap().clone() {
Expand All @@ -1578,81 +1580,65 @@ pub fn diff(
for (key, ele) in function.as_deref().unwrap().clone() {
s.insert(key, ele);
}
let insert = if second_param {
p.get(1).unwrap().clone()
} else {
Var(Box::from(Int(1)), 1, "x".to_string())
};
match first_param {
Identifier(fun) => match fun.as_str() {
"cos" => Plus(
Box::from(Int(0)),
Box::from(mult(
Int(-1),
Call(
"sin".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
),
Some(&c),
)),
),
"sin" => Call(
"cos".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
),
"exp" => Call(
"exp".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
),
"ln" => Var(Box::from(Int(1)), -1, "x".to_string()),
"tan" => Div(
Box::from(Int(1)),
Box::from(Mul(
Box::from(Call(
"cos".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
)),
Box::from(Call(
"cos".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
)),
)),
),
"sinh" => Call(
"cosh".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
"cos" => mult(
Int(-1),
Call("sin".to_string(), Box::from(insert)),
Some(&c),
),
"cosh" => Call(
"sinh".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
"sin" => Call("cos".to_string(), Box::from(insert)),
"exp" => Call("exp".to_string(), Box::from(insert)),
"ln" => divide(Int(1), insert, Some(&c)),
"tan" => divide(
Int(1),
mult(
Call("cos".to_string(), Box::from(insert.clone())),
Call("cos".to_string(), Box::from(insert.clone())),
Some(&c),
),
Some(&c),
),
"acos" => Div(
Box::from(Int(-1)),
Box::from(Call(
"sinh" => Call("cosh".to_string(), Box::from(insert)),
"cosh" => Call("sinh".to_string(), Box::from(insert)),
"acos" => divide(
Int(-1),
Call(
"sqrt".to_string(),
Box::from(minus(
Int(1),
Var(Box::from(Int(1)), 2, "x".to_string()),
mult(insert.clone(), insert.clone(), Some(&c)),
Some(&c),
)),
)),
),
Some(&c),
),
"asin" => Div(
Box::from(Int(1)),
Box::from(Call(
"asin" => divide(
Int(1),
Call(
"sqrt".to_string(),
Box::from(minus(
Int(1),
Var(Box::from(Int(1)), 2, "x".to_string()),
mult(insert.clone(), insert.clone(), Some(&c)),
Some(&c),
)),
)),
),
Some(&c),
),
"x" => Identifier("1".to_string()),
"sqrt" => Div(
Box::from(Int(1)),
Box::from(Mul(
Box::from(Int(2)),
Box::from(Call(
"sqrt".to_string(),
Box::from(Var(Box::from(Int(1)), 1, "x".to_string())),
)),
)),
"sqrt" => divide(
Int(1),
mult(
Int(2),
Call("sqrt".to_string(), Box::from(insert)),
Some(&c),
),
Some(&c),
),
p => {
let param = exec(
Expand Down Expand Up @@ -1686,9 +1672,34 @@ pub fn diff(
),
Some(&c),
),
Div(x, y) => Div(
Box::from(other_add(
mult(
*x.clone(),
diff(&vec![*y.clone()], &Some(&mut c), Some(&mut s)),
Some(&c),
),
mult(
mult(Int(-1), *y.clone(), Some(&c)),
diff(&vec![*x.clone()], &Some(&mut c), Some(&mut s)),
Some(&c),
),
Some(&c),
)),
Box::from(mult(*y.clone(), *y.clone(), Some(&c))),
),
Call(name, pst) => {
let prefix = diff(&vec![*pst.clone()], &Some(&mut c), Some(&mut s));
let call = diff(
&vec![Identifier(name), *pst.clone()],
&Some(&mut c),
Some(&mut s),
);
mult(prefix, call, Some(&c))
}
_ => Int(0),
}
} //2*x = 2'*x + 2*x' = 0*x + 2
}
},
Var(x, y, z) => Var(
Box::from(mult(Parameters::Int(*y), *x.clone(), Some(&c))),
Expand All @@ -1713,6 +1724,32 @@ pub fn diff(
),
Some(&c),
),
Div(x, y) => Div(
Box::from(other_add(
mult(
*x.clone(),
diff(&vec![*y.clone()], &Some(&mut c), Some(&mut s)),
Some(&c),
),
mult(
Mul(Box::from(Int(-1)), y.clone()),
diff(&vec![*x.clone()], &Some(&mut c), Some(&mut s)),
Some(&c),
),
Some(&c),
)),
Box::from(mult(*y.clone(), *y.clone(), Some(&c))),
),

Call(name, pst) => {
let prefix = diff(&vec![*pst.clone()], &Some(&mut c), Some(&mut s));
let call = diff(
&vec![Identifier(name.to_string()), *pst.clone()],
&Some(&mut c),
Some(&mut s),
);
mult(prefix, call, Some(&c))
}
_ => Int(0),
}
}
Expand Down
Loading