Factorial and combinations functions

This commit is contained in:
Marshall Lochbaum 2022-11-22 20:51:11 -05:00
parent f6109b3985
commit 6eb21bd68c
4 changed files with 42 additions and 6 deletions

View File

@ -17,7 +17,7 @@
/*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") M(heapDump,"•internal.HeapDump") \
/*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") D(eequal,"•internal.EEqual") A(internalTemp,"•internal.Temp") \
/*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \
/* arithm.c*/M(sin,"•math.Sin") M(cos,"•math.Cos") M(tan,"•math.Tan") M(asin,"•math.Asin") M(acos,"•math.Acos") M(atan,"•math.Atan") D(atan2,"•math.Atan2") D(hypot,"•math.Hypot") M(sinh,"•math.Sinh") M(cosh,"•math.Cosh") M(tanh,"•math.Tanh") M(asinh,"•math.Asinh") M(acosh,"•math.Acosh") M(atanh,"•math.Atanh") M(cbrt,"•math.Cbrt") M(log2,"•math.Log2") M(log10,"•math.Log10") M(log1p,"•math.Log1p") M(expm1,"•math.Expm1") D(gcd,"•math.GCD") D(lcm,"•math.LCM")
/* arithm.c*/M(sin,"•math.Sin") M(cos,"•math.Cos") M(tan,"•math.Tan") M(asin,"•math.Asin") M(acos,"•math.Acos") M(atan,"•math.Atan") D(atan2,"•math.Atan2") D(hypot,"•math.Hypot") M(sinh,"•math.Sinh") M(cosh,"•math.Cosh") M(tanh,"•math.Tanh") M(asinh,"•math.Asinh") M(acosh,"•math.Acosh") M(atanh,"•math.Atanh") M(cbrt,"•math.Cbrt") M(log2,"•math.Log2") M(log10,"•math.Log10") M(log1p,"•math.Log1p") M(expm1,"•math.Expm1") M(fact,"•math.Fact") D(comb,"•math.Comb") D(gcd,"•math.GCD") D(lcm,"•math.LCM")
#define FOR_PM1(A,M,D) \
/*md1.c*/A(tbl,"") A(each,"¨") A(fold,"´") A(scan,"`") A(const,"˙") A(swap,"˜") A(cell,"˘") A(insert,"˝") \

View File

@ -360,13 +360,47 @@ AR_F_SCALAR("|", stile, pfmod(x.f, w.f))
AR_F_SCALAR("⋆⁼",log , log(x.f)/log(w.f))
#undef AR_F_SCALAR
static f64 comb_nat(f64 k, f64 n, f64 j) {
if (j < k) k = j;
if (k > 514) return INFINITY;
f64 p = 1;
for (usz i=0; i<(usz)k; i++) {
p*= (n-i) / (k-i);
if (p == INFINITY) return p;
}
return round(p);
}
static f64 comb(f64 k, f64 n) { // n choose k
f64 j = n - k; // j+k == n
bool jint = j == round(j);
if (k == round(k)) {
if (jint) {
if (n >= 0) {
if (!(k>=0 && j>=0)) return 0; // Negative phrasing to catch NaN
return comb_nat(k, n, j);
} else {
if (k<0) {
if (j<0) return 0;
f64 t=k; k=j; j=t; // Swap so k is non-negative
}
f64 r = comb_nat(k, -1-j, -1-n);
return k<(1ull<<53) && ((i64)k&1)? -r : r;
}
}
if (k < 0) return 0;
} else if (jint) {
if (j < 0) return 0;
}
return exp(lgamma(n+1) - lgamma(k+1) - lgamma(j+1));
}
#define MATH(n,N) \
B n##_c2(B t, B w, B x) { \
if (isNum(w) && isNum(x)) return m_f64(n(x.f, w.f)); \
P2(n) \
thrM("•math." #N ": Unexpected argument types"); \
}
MATH(atan2,Atan2) MATH(hypot,Hypot)
MATH(atan2,Atan2) MATH(hypot,Hypot) MATH(comb,Comb)
#undef MATH
static u64 gcd_u64(u64 a, u64 b) {

View File

@ -88,12 +88,14 @@ GC1f( div, 1/xv, "÷: Getting reciprocal of non-number")
GC1f(root, sqrt(xv), "√: Getting square root of non-number")
#undef GC1f
f64 fact(f64 x) { return tgamma(x+1); }
#define P1(N) { if(isArr(x)) { SLOW1("arithm " #N, x); return arith_recm(N##_c1, x); } }
B pow_c1(B t, B x) { if (isF64(x)) return m_f64( exp(x.f)); P1( pow); thrM("⋆: Getting exp of non-number"); }
B log_c1(B t, B x) { if (isF64(x)) return m_f64( log(x.f)); P1( log); thrM("⋆⁼: Getting log of non-number"); }
#define MATH(n,N) \
B n##_c1(B t, B x) { if (isF64(x)) return m_f64(n(x.f)); P1(n); thrM("•math." #N ": Argument contained non-number"); }
MATH(cbrt,Cbrt) MATH(log2,Log2) MATH(log10,Log10) MATH(log1p,Log1p) MATH(expm1,Expm1)
MATH(cbrt,Cbrt) MATH(log2,Log2) MATH(log10,Log10) MATH(log1p,Log1p) MATH(expm1,Expm1) MATH(fact,Fact)
#define TRIG(n,N) MATH(n,N) MATH(a##n,A##n) MATH(n##h,N##h) MATH(a##n##h,A##n##h)
TRIG(sin,Sin) TRIG(cos,Cos) TRIG(tan,Tan)
#undef TRIG
@ -109,8 +111,8 @@ static B mathNS;
B getMathNS() {
if (mathNS.u == 0) {
#define F(X) inc(bi_##X),
Body* d = m_nnsDesc("sin","cos","tan","asin","acos","atan","atan2","sinh","cosh","tanh","asinh","acosh","atanh","cbrt","log2","log10","log1p","expm1","hypot","gcd","lcm");
mathNS = m_nns(d, F(sin)F(cos)F(tan)F(asin)F(acos)F(atan)F(atan2)F(sinh)F(cosh)F(tanh)F(asinh)F(acosh)F(atanh)F(cbrt)F(log2)F(log10)F(log1p)F(expm1)F(hypot)F(gcd)F(lcm));
Body* d = m_nnsDesc("sin","cos","tan","asin","acos","atan","atan2","sinh","cosh","tanh","asinh","acosh","atanh","cbrt","log2","log10","log1p","expm1","hypot","fact","comb","gcd","lcm");
mathNS = m_nns(d, F(sin)F(cos)F(tan)F(asin)F(acos)F(atan)F(atan2)F(sinh)F(cosh)F(tanh)F(asinh)F(acosh)F(atanh)F(cbrt)F(log2)F(log10)F(log1p)F(expm1)F(hypot)F(fact)F(comb)F(gcd)F(lcm));
#undef F
gc_add(mathNS);
}

View File

@ -1551,7 +1551,7 @@ u32* dsv_text[] = {
U"•file.MapBytes",U"•file.Modified",U"•file.Name",U"•file.Parent",U"•file.Remove",U"•file.Rename",U"•file.Size",U"•file.Type",
U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.HeapDump",U"•internal.Info",U"•internal.IsPure",U"•internal.ListVariations",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Variation",
U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Cos",U"•math.Cosh",U"•math.Expm1",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.Sin",U"•math.Sinh",U"•math.Tan",U"•math.Tanh",
U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.Sin",U"•math.Sinh",U"•math.Tan",U"•math.Tanh",
U"•rand.Deal",U"•rand.Range",U"•rand.Subset",
U"•term.CharB",U"•term.CharN",U"•term.ErrRaw",U"•term.Flush",U"•term.OutRaw",U"•term.RawMode",
NULL