Added prod function and proper sum symbol

This commit is contained in:
bakk 2021-06-01 15:52:41 +02:00
parent 8f3732b3c7
commit 1f6b59bdae
5 changed files with 34 additions and 7 deletions

View File

@ -310,25 +310,42 @@ pub(crate) fn eval_fn_call_expr(
// Special functions // Special functions
match identifier.full_name.as_ref() { match identifier.full_name.as_ref() {
"sum" | "Σ" => { "sum" | "Σ" | "" | "prod" | "" => {
// Make sure exactly 3 arguments were supplied. // Make sure exactly 3 arguments were supplied.
if expressions.len() != 3 { if expressions.len() != 3 {
return Err(CalcError::IncorrectAmountOfArguments( return Err(CalcError::IncorrectAmountOfArguments(
3, 3,
"sum".into(), "sum/prod".into(),
expressions.len(), expressions.len(),
)); ));
} }
let start = eval_expr(context, &expressions[0], "")?.to_f64() as i128; let start = eval_expr(context, &expressions[0], "")?.to_f64() as i128;
let end = eval_expr(context, &expressions[1], "")?.to_f64() as i128; let end = eval_expr(context, &expressions[1], "")?.to_f64() as i128;
let mut sum = KalkNum::default(); let sum_else_prod = match identifier.full_name.as_ref() {
"sum" => true,
"Σ" => true,
"" => true,
"prod" => false,
"" => false,
_ => unreachable!(),
};
let mut sum = if sum_else_prod {
KalkNum::default()
} else {
KalkNum::from(1f64)
};
for n in start..=end { for n in start..=end {
context.sum_n_value = Some(n); context.sum_n_value = Some(n);
let eval = eval_expr(context, &expressions[2], "")?; let eval = eval_expr(context, &expressions[2], "")?;
if sum_else_prod {
sum.value += eval.value; sum.value += eval.value;
sum.imaginary_value += eval.imaginary_value; sum.imaginary_value += eval.imaginary_value;
} else {
sum.value *= eval.value;
sum.imaginary_value *= eval.imaginary_value;
}
} }
context.sum_n_value = None; context.sum_n_value = None;

View File

@ -143,6 +143,7 @@ impl<'a> Lexer<'a> {
'τ' => build(TokenKind::Identifier, "τ", span), 'τ' => build(TokenKind::Identifier, "τ", span),
'ϕ' => build(TokenKind::Identifier, "ϕ", span), 'ϕ' => build(TokenKind::Identifier, "ϕ", span),
'Γ' => build(TokenKind::Identifier, "Γ", span), 'Γ' => build(TokenKind::Identifier, "Γ", span),
'∏' => build(TokenKind::Identifier, "Γ", span),
_ => build(TokenKind::Unknown, "", span), _ => build(TokenKind::Unknown, "", span),
}; };

View File

@ -161,6 +161,9 @@ impl BinaryFuncInfo {
pub fn is_prelude_func(identifier: &str) -> bool { pub fn is_prelude_func(identifier: &str) -> bool {
identifier == "sum" identifier == "sum"
|| identifier == "Σ" || identifier == "Σ"
|| identifier == ""
|| identifier == "prod"
|| identifier == ""
|| identifier == "integrate" || identifier == "integrate"
|| identifier == "integral" || identifier == "integral"
|| identifier == "" || identifier == ""

View File

@ -115,6 +115,7 @@ lazy_static! {
m.insert("floor", "⌊⌋"); m.insert("floor", "⌊⌋");
m.insert("gamma", "Γ"); m.insert("gamma", "Γ");
m.insert("sum", "Σ()"); m.insert("sum", "Σ()");
m.insert("prod", "∏()");
m.insert("integrate", "∫()"); m.insert("integrate", "∫()");
m.insert("integral", "∫()"); m.insert("integral", "∫()");
m.insert("phi", "ϕ"); m.insert("phi", "ϕ");
@ -152,6 +153,7 @@ impl Completer for RLHelper {
line.insert_str(line.pos(), elected); line.insert_str(line.pos(), elected);
line.move_forward(match elected { line.move_forward(match elected {
"Σ()" => 2, "Σ()" => 2,
"∏()" => 2,
"∫()" => 2, "∫()" => 2,
_ => 1, _ => 1,
}); });

View File

@ -33,7 +33,7 @@
"π", "π",
",", ",",
"%", "%",
"Σ", "",
"⌊", "⌊",
"⌈", "⌈",
"∫", "∫",
@ -272,7 +272,11 @@
break; break;
} }
case "sum": { case "sum": {
newSubstring = "Σ"; newSubstring = "∑";
break;
}
case "prod": {
newSubstring = "∏";
break; break;
} }
case "integrate": { case "integrate": {