2021-05-17 16:51:16 +02:00
|
|
|
use crate::ast::Expr;
|
|
|
|
use crate::ast::Stmt;
|
|
|
|
use crate::interpreter;
|
|
|
|
use crate::kalk_num::KalkNum;
|
|
|
|
use crate::lexer::TokenKind;
|
|
|
|
use crate::parser::CalcError;
|
|
|
|
|
|
|
|
pub fn integrate(
|
|
|
|
context: &mut interpreter::Context,
|
|
|
|
expressions: &[Expr],
|
|
|
|
) -> Result<KalkNum, CalcError> {
|
|
|
|
let mut integration_variable: Option<&str> = None;
|
|
|
|
|
|
|
|
// integral(a, b, expr dx)
|
|
|
|
if let Expr::Binary(_, TokenKind::Star, right) = &expressions[2] {
|
|
|
|
if let Expr::Var(right_name) = &**right {
|
|
|
|
if right_name.starts_with("d") {
|
|
|
|
// Take the value, but remove the d, so that only eg. x is left from dx
|
|
|
|
integration_variable = Some(&right_name[1..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if integration_variable.is_none() {
|
|
|
|
return Err(CalcError::ExpectedDx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "dx" is still in the expression. Set dx = 1, so that it doesn't affect the expression value.
|
|
|
|
context.symbol_table.set(Stmt::VarDecl(
|
|
|
|
format!("d{}", integration_variable.unwrap()),
|
|
|
|
Box::new(Expr::Literal(1f64)),
|
|
|
|
));
|
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
simpsons_rule(
|
|
|
|
context,
|
|
|
|
&expressions[0],
|
|
|
|
&expressions[1],
|
|
|
|
&expressions[2],
|
|
|
|
integration_variable.unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Composite Simpson's 3/8 rule
|
|
|
|
fn simpsons_rule(
|
|
|
|
context: &mut interpreter::Context,
|
|
|
|
a_expr: &Expr,
|
|
|
|
b_expr: &Expr,
|
|
|
|
expr: &Expr,
|
|
|
|
integration_variable: &str,
|
|
|
|
) -> Result<KalkNum, CalcError> {
|
|
|
|
let mut result = KalkNum::default();
|
2021-05-17 16:51:16 +02:00
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
const N: i32 = 900;
|
|
|
|
let a = interpreter::eval_expr(context, a_expr, "")?.value.to_f64();
|
|
|
|
let b = interpreter::eval_expr(context, b_expr, "")?.value.to_f64();
|
|
|
|
let h = (b - a) / N as f64;
|
|
|
|
for i in 0..=N {
|
2021-05-17 16:51:16 +02:00
|
|
|
context.symbol_table.set(Stmt::VarDecl(
|
2021-05-17 18:05:22 +02:00
|
|
|
integration_variable.into(),
|
|
|
|
Box::new(Expr::Literal(a + i as f64 * h)),
|
2021-05-17 16:51:16 +02:00
|
|
|
));
|
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
let factor = if i == 0 || i == N {
|
|
|
|
1
|
|
|
|
} else if i % 3 == 0 {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
2021-05-17 16:51:16 +02:00
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
// factor * f(x_n)
|
|
|
|
result.value += factor * interpreter::eval_expr(context, expr, "")?.value;
|
|
|
|
}
|
2021-05-17 16:51:16 +02:00
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
result.value *= (3f64 * h) / 8f64;
|
2021-05-17 16:51:16 +02:00
|
|
|
|
2021-05-17 18:05:22 +02:00
|
|
|
Ok(result)
|
2021-05-17 16:51:16 +02:00
|
|
|
}
|