Added prod function and proper sum symbol

This commit is contained in:
bakk 2021-06-01 15:52:41 +02:00
parent 349aeb7f0b
commit e07c5e5339
5 changed files with 34 additions and 7 deletions

View File

@ -310,25 +310,42 @@ pub(crate) fn eval_fn_call_expr(
// Special functions
match identifier.full_name.as_ref() {
"sum" | "Σ" => {
"sum" | "Σ" | "" | "prod" | "" => {
// Make sure exactly 3 arguments were supplied.
if expressions.len() != 3 {
return Err(CalcError::IncorrectAmountOfArguments(
3,
"sum".into(),
"sum/prod".into(),
expressions.len(),
));
}
let start = eval_expr(context, &expressions[0], "")?.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 {
context.sum_n_value = Some(n);
let eval = eval_expr(context, &expressions[2], "")?;
sum.value += eval.value;
sum.imaginary_value += eval.imaginary_value;
if sum_else_prod {
sum.value += eval.value;
sum.imaginary_value += eval.imaginary_value;
} else {
sum.value *= eval.value;
sum.imaginary_value *= eval.imaginary_value;
}
}
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::Unknown, "", span),
};

View File

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

View File

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

View File

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