Skip to content

Commit 2f7a2ac

Browse files
committed
Improve StirlingS1
1 parent 222f142 commit 2f7a2ac

File tree

1 file changed

+27
-19
lines changed
  • symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin

1 file changed

+27
-19
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/NumberTheory.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,12 +4617,6 @@ public static boolean isSquarefreeWithOption(
46174617
private static class StirlingS1 extends AbstractFunctionEvaluator {
46184618

46194619
private static IExpr stirlingS1(IInteger n, IInteger m) {
4620-
if (n.isZero() && m.isZero()) {
4621-
return F.C1;
4622-
}
4623-
if (n.isZero() && m.isPositive()) {
4624-
return C0;
4625-
}
46264620
IInteger nSubtract1 = n.subtract(F.C1);
46274621
if (n.isPositive() && m.isOne()) {
46284622
return Times(Power(F.CN1, nSubtract1), F.Factorial(nSubtract1));
@@ -4644,11 +4638,11 @@ private static IExpr stirlingS1(IInteger n, IInteger m) {
46444638
int counter = nSubtractm.toIntDefault(Integer.MIN_VALUE);
46454639
if (counter > Integer.MIN_VALUE) {
46464640
counter++;
4647-
IInteger k;
4641+
IInteger value;
46484642
IASTAppendable temp = F.PlusAlloc(counter >= 0 ? counter : 0);
46494643
long leafCount = 0;
46504644
for (int i = 0; i < counter; i++) {
4651-
k = F.ZZ(i);
4645+
value = F.ZZ(i);
46524646
if ((i & 1) == 1) { // isOdd(i) ?
46534647
factorPlusMinus1 = F.CN1;
46544648
} else {
@@ -4657,9 +4651,9 @@ private static IExpr stirlingS1(IInteger n, IInteger m) {
46574651
temp.append(
46584652
Times(
46594653
factorPlusMinus1,
4660-
F.Binomial(Plus(k, nSubtract1), Plus(k, nSubtractm)),
4661-
F.Binomial(nTimes2Subtractm, F.Subtract(nSubtractm, k)),
4662-
F.StirlingS2(Plus(k, nSubtractm), k)));
4654+
F.Binomial(Plus(value, nSubtract1), Plus(value, nSubtractm)),
4655+
F.Binomial(nTimes2Subtractm, F.Subtract(nSubtractm, value)),
4656+
F.StirlingS2(Plus(value, nSubtractm), value)));
46634657
leafCount += temp.leafCount();
46644658
if (leafCount > Config.MAX_AST_SIZE) {
46654659
ASTElementLimitExceeded.throwIt(leafCount);
@@ -4675,13 +4669,23 @@ private static IExpr stirlingS1(IInteger n, IInteger m) {
46754669
/** {@inheritDoc} */
46764670
@Override
46774671
public IExpr evaluate(final IAST ast, EvalEngine engine) {
4678-
IExpr nArg1 = ast.arg1();
4679-
IExpr mArg2 = ast.arg2();
4680-
if (nArg1.isNegative() || mArg2.isNegative()) {
4672+
IExpr n = ast.arg1();
4673+
IExpr m = ast.arg2();
4674+
if (n.isNegativeResult() || m.isNegativeResult()) {
46814675
return F.NIL;
46824676
}
4683-
if (nArg1.isInteger() && mArg2.isInteger()) {
4684-
return stirlingS1((IInteger) nArg1, (IInteger) mArg2);
4677+
if (n.isZero() && m.isZero()) {
4678+
return F.C1;
4679+
}
4680+
if (n.isZero() && m.isPositiveResult()) {
4681+
return C0;
4682+
}
4683+
4684+
if (n.equals(m)) {
4685+
return F.C1;
4686+
}
4687+
if (n.isInteger() && m.isInteger()) {
4688+
return stirlingS1((IInteger) n, (IInteger) m);
46854689
}
46864690

46874691
return F.NIL;
@@ -4733,14 +4737,13 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
47334737
try {
47344738
IExpr nArg1 = ast.arg1();
47354739
IExpr kArg2 = ast.arg2();
4736-
if (nArg1.isNegative() || kArg2.isNegative()) {
4740+
if (nArg1.isNegativeResult() || kArg2.isNegativeResult()) {
47374741
return F.NIL;
47384742
}
47394743
if (nArg1.isZero() && kArg2.isZero()) {
47404744
return F.C1;
47414745
}
47424746
if (nArg1.isInteger() && kArg2.isInteger()) {
4743-
int n = Validate.checkNonNegativeIntType(ast, 1);
47444747
IInteger ki = (IInteger) kArg2;
47454748
if (ki.greaterThan(nArg1).isTrue()) {
47464749
return C0;
@@ -4759,7 +4762,8 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
47594762
// {n,2}==2^(n-1)-1
47604763
return Subtract(Power(C2, Subtract(nArg1, C1)), C1);
47614764
}
4762-
4765+
4766+
int n = Validate.checkNonNegativeIntType(ast, 1);
47634767
int k = ki.toIntDefault(0);
47644768
if (k != 0) {
47654769
return stirlingS2(n, ki, k);
@@ -4989,6 +4993,10 @@ public static IInteger factorial(final IInteger x) {
49894993
return x.factorial();
49904994
}
49914995

4996+
public static boolean check(IExpr n, IExpr k, IExpr delta, EvalEngine engine) {
4997+
return engine.evalTrue(F.Equal(n, k.plus(delta)));
4998+
}
4999+
49925000
public static IInteger factorial(int ni) {
49935001
BigInteger result;
49945002
if (ni < 0) {

0 commit comments

Comments
 (0)