Added permutation functions (for nPr and nCr), closes #52

This commit is contained in:
PaddiM8 2022-01-01 01:09:30 +01:00
parent 7892eee23e
commit 474b4d9016
2 changed files with 17 additions and 2 deletions

View File

@ -107,6 +107,10 @@ lazy_static! {
m.insert("lcm", (BinaryFuncInfo(lcm, Other), ""));
m.insert("log", (BinaryFuncInfo(logx, Other), ""));
m.insert("root", (BinaryFuncInfo(nth_root, Other), ""));
m.insert("nCr", (BinaryFuncInfo(ncr, Other), ""));
m.insert("comb", (BinaryFuncInfo(ncr, Other), ""));
m.insert("nPr", (BinaryFuncInfo(npr, Other), ""));
m.insert("perm", (BinaryFuncInfo(npr, Other), ""));
m
};
}
@ -222,6 +226,7 @@ fn from_angle_unit(context: &mut interpreter::Context, x: KalkNum, angle_unit: &
pub mod funcs {
#[cfg(not(feature = "rug"))]
pub use super::regular::funcs::*;
use super::special_funcs::factorial;
#[cfg(feature = "rug")]
pub use super::with_rug::funcs::*;
use crate::kalk_num::KalkNum;
@ -715,6 +720,16 @@ pub mod funcs {
KalkNum::new_with_imaginary(x.value.trunc(), &x.unit, x.imaginary_value.trunc())
}
pub fn ncr(x: KalkNum, y: KalkNum) -> KalkNum {
factorial(x.clone()).div_without_unit(
factorial(y.clone()).mul_without_unit(factorial(x.sub_without_unit(y))),
)
}
pub fn npr(x: KalkNum, y: KalkNum) -> KalkNum {
factorial(x.clone()).div_without_unit(factorial(x.sub_without_unit(y)))
}
fn multiply_with_i(z: KalkNum) -> KalkNum {
// iz = i(a + bi) = -b + ai
KalkNum::new_with_imaginary(-z.imaginary_value, &z.unit, z.value)

View File

@ -50,8 +50,8 @@ pub(crate) mod funcs {
2f64.sqrt() * pi.sqrt() * t.powf(x - 0.5f64) * (-t).exp() * a
}
pub fn bitcmp(x: KalkNum, y: KalkNum) -> KalkNum {
KalkNum::from(!x.value.round() as i32)
pub fn bitcmp(x: KalkNum) -> KalkNum {
KalkNum::from(!(x.value.round() as i32))
}
pub fn bitand(x: KalkNum, y: KalkNum) -> KalkNum {