Skip to content

Commit 9eadc83

Browse files
committed
StirlingS2(n,k) for int values
1 parent 800e9af commit 9eadc83

File tree

6 files changed

+49
-21
lines changed

6 files changed

+49
-21
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
## StirlingS1
22

3-
```
3+
```
44
StirlingS1(n, k)
5-
```
5+
```
66

77
> returns the Stirling numbers of the first kind.
88
99
See:
1010
* [Wikipedia - Stirling numbers of the first kind](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind)
1111

1212
### Examples
13-
```
13+
14+
```
1415
>> StirlingS1(9, 6)
1516
-4536
1617
```
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
## StirlingS2
22

3-
```
3+
```
44
StirlingS2(n, k)
5-
```
5+
```
66

7-
> returns the Stirling numbers of the second kind.
7+
> returns the Stirling numbers of the second kind. `StirlingS2(n,k)` is the number of ways of partitioning an `n`-element set into `k` non-empty subsets.
88
99
See:
1010
* [Wikipedia - Stirling numbers of the second kind](http://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind)
1111

1212
### Examples
13-
```
13+
14+
```
1415
>> StirlingS2(10, 6)
1516
22827
1617
```

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static org.matheclipse.core.expression.F.C1;
66
import static org.matheclipse.core.expression.F.C2;
77
import static org.matheclipse.core.expression.F.CN1;
8-
import static org.matheclipse.core.expression.F.Cos;
98
import static org.matheclipse.core.expression.F.Factorial;
109
import static org.matheclipse.core.expression.F.Negate;
1110
import static org.matheclipse.core.expression.F.Plus;
@@ -23,6 +22,8 @@
2322
import java.util.concurrent.Callable;
2423
import java.util.concurrent.ExecutionException;
2524

25+
import org.hipparchus.exception.MathRuntimeException;
26+
import org.hipparchus.util.CombinatoricsUtils;
2627
import org.matheclipse.core.basic.Config;
2728
import org.matheclipse.core.convert.JASConvert;
2829
import org.matheclipse.core.convert.VariablesSet;
@@ -2927,16 +2928,38 @@ public void setUp(final ISymbol newSymbol) {
29272928
*/
29282929
private static class StirlingS2 extends AbstractFunctionEvaluator {
29292930

2930-
private static IExpr stirlingS2(IInteger nArg1, IInteger kArg2, int k) {
2931-
IAST temp = F.PlusAlloc(k >= 0 ? k : 0);
2932-
for (int i = 0; i < k; i++) {
2931+
/**
2932+
* Returns the Stirling number of the second kind, "{@code S(n,k)}", the number of ways of partitioning an
2933+
* {@code n}-element set into {@code k} non-empty subsets.
2934+
*
2935+
* @param n
2936+
* the size of the set
2937+
* @param k
2938+
* the number of non-empty subsets
2939+
* @param ki
2940+
* the number of non-empty subsets as int value
2941+
* @return {@code S(nArg1,kArg2)}
2942+
*/
2943+
private static IExpr stirlingS2(IInteger n, IInteger k, int ki) {
2944+
try {
2945+
int ni = n.toIntDefault(0);
2946+
if (ni != 0 && ni <= 25) {// S(26,9) = 11201516780955125625 is larger than Long.MAX_VALUE
2947+
return F.ZZ(CombinatoricsUtils.stirlingS2(ni, ki));
2948+
}
2949+
} catch (MathRuntimeException mre) {
2950+
if (Config.DEBUG) {
2951+
mre.printStackTrace();
2952+
}
2953+
}
2954+
IAST temp = F.PlusAlloc(ki >= 0 ? ki : 0);
2955+
for (int i = 0; i < ki; i++) {
29332956
if ((i & 1) == 1) { // isOdd(i) ?
2934-
temp.append(Times(Negate(Binomial(kArg2, integer(i))), Power(Plus(kArg2, integer(-i)), nArg1)));
2957+
temp.append(Times(Negate(Binomial(k, integer(i))), Power(Plus(k, integer(-i)), n)));
29352958
} else {
2936-
temp.append(Times(Times(Binomial(kArg2, integer(i))), Power(Plus(kArg2, integer(-i)), nArg1)));
2959+
temp.append(Times(Times(Binomial(k, integer(i))), Power(Plus(k, integer(-i)), n)));
29372960
}
29382961
}
2939-
return Times(Power(Factorial(kArg2), CN1), temp);
2962+
return Times(Power(Factorial(k), CN1), temp);
29402963
}
29412964

29422965
/** {@inheritDoc} */
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
## StirlingS1
22

3-
```
3+
```
44
StirlingS1(n, k)
5-
```
5+
```
66

77
> returns the Stirling numbers of the first kind.
88
99
See:
1010
* [Wikipedia - Stirling numbers of the first kind](https://en.wikipedia.org/wiki/Stirling_numbers_of_the_first_kind)
1111

1212
### Examples
13-
```
13+
14+
```
1415
>> StirlingS1(9, 6)
1516
-4536
1617
```
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
## StirlingS2
22

3-
```
3+
```
44
StirlingS2(n, k)
5-
```
5+
```
66

7-
> returns the Stirling numbers of the second kind.
7+
> returns the Stirling numbers of the second kind. `StirlingS2(n,k)` is the number of ways of partitioning an `n`-element set into `k` non-empty subsets.
88
99
See:
1010
* [Wikipedia - Stirling numbers of the second kind](http://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind)
1111

1212
### Examples
13-
```
13+
14+
```
1415
>> StirlingS2(10, 6)
1516
22827
1617
```

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6744,6 +6744,7 @@ public void testStirlingS1() {
67446744
}
67456745

67466746
public void testStirlingS2() {
6747+
check("StirlingS2(6,10)", "0");
67476748
check("StirlingS2(10,6)", "22827");
67486749
check("StirlingS2(0,0)", "1");
67496750
check("StirlingS2(1,1)", "1");

0 commit comments

Comments
 (0)