Skip to content

Commit

Permalink
Implemented RamseyNumber(r,s) function
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Nov 6, 2023
1 parent 8200e10 commit 77816fb
Show file tree
Hide file tree
Showing 9 changed files with 733 additions and 607 deletions.
1 change: 1 addition & 0 deletions symja_android_library/doc/99-function-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ Functions in alphabetical order:
* ✅ [Quotient](functions/Quotient.md)
* ✅ [QuotientRemainder](functions/QuotientRemainder.md)
* ✅ [Ramp](functions/Ramp.md)
* [RamseyNumber](functions/RamseyNumber.md)
* ✅ [RandomChoice](functions/RandomChoice.md)
* ✅ [RandomComplex](functions/RandomComplex.md)
* ✅ [RandomGraph](functions/RandomGraph.md)
Expand Down
24 changes: 24 additions & 0 deletions symja_android_library/doc/functions/RamseyNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## RamseyNumber

```
RamseyNumber(r, s)
```

> returns the Ramsey number `R(r,s)`. Currently not all values are known for `1 <= r <= 4`. The function returns unevaluated if the value is unknown.
See
* [Wikipedia - Ramsey's theorem](https://en.wikipedia.org/wiki/Ramsey's_theorem)
* [OEIS - A212954](https://oeis.org/A212954)

### Examples

```
>> Table(RamseyNumber(1,j), {j,1,20})
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
>> Table(RamseyNumber(2,j), {j,1,20})
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
>> Table(RamseyNumber(i,j), {i,1,5},{j,i,10})
{{1,1,1,1,1,1,1,1,1,1},{2,3,4,5,6,7,8,9,10},{6,9,14,18,23,28,36,RamseyNumber(3,10)},{18,25,RamseyNumber(4,6),RamseyNumber(4,7),RamseyNumber(4,8),RamseyNumber(4,9),RamseyNumber(4,10)}}
```
Original file line number Diff line number Diff line change
Expand Up @@ -4940,6 +4940,67 @@ public void setUp(final ISymbol newSymbol) {
newSymbol.setAttributes(ISymbol.LISTABLE);
}
}

private static class RamseyNumber extends AbstractFunctionEvaluator {

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
int r = ast.arg1().toIntDefault();
int s = ast.arg2().toIntDefault();
if (r <= 0 || s <= 0) {
return F.NIL;
}
if (r > s) {
// symmetry across the diagonal: R(r, s) = R(s, r)
return F.binary(S.RamseyNumber, ast.arg2(), ast.arg1());
}
// https://en.wikipedia.org/wiki/Ramsey%27s_theorem#Known_values)
switch (r) {
case 1:
return F.C1;
case 2:
return F.ZZ(s);
case 3:
switch (s) {
case 3:
return F.C6;
case 4:
return F.C9;
case 5:
return F.ZZ(14);
case 6:
return F.ZZ(18);
case 7:
return F.ZZ(23);
case 8:
return F.ZZ(28);
case 9:
return F.ZZ(36);
}
break;
case 4:
switch (s) {
case 4:
return F.ZZ(18);
case 5:
return F.ZZ(25);
}
break;
}
return F.NIL;
}

@Override
public int[] expectedArgSize(IAST ast) {
return ARGS_2_2;
}

@Override
public int status() {
return ImplementationStatus.PARTIAL_SUPPORT;
}
}

/**
*
*
Expand Down Expand Up @@ -5599,6 +5660,7 @@ private static void init() {
S.PrimitiveRoot.setEvaluator(new PrimitiveRoot());
S.PrimitiveRootList.setEvaluator(new PrimitiveRootList());
S.QuadraticIrrationalQ.setEvaluator(new QuadraticIrrationalQ());
S.RamseyNumber.setEvaluator(new RamseyNumber());
S.Rationalize.setEvaluator(new Rationalize());
S.RootReduce.setEvaluator(new RootReduce());
S.SquareFreeQ.setEvaluator(new SquareFreeQ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ public class AST2Expr {
"ProductLog", "Projection", "Protect", "PseudoInverse", "Put", "Pyramid", "QRDecomposition",
"QuadraticIrrationalQ", "QuarticSolve", "Quantile", "Quantity", "QuantityDistribution",
"QuantityMagnitude", "QuantityQ", "QuantityUnit", "Quartiles", "Quiet", "Quit", "Quotient",
"QuotientRemainder", "RadicalBox", "Ramp", "RandomChoice", "RandomComplex", "RandomInteger",
"RandomGraph", "RandomPermutation", "RandomPrime", "RandomReal", "RandomSample",
"RandomVariate", "Range", "RankedMax", "RankedMin", "Rational", "Rationalize", "RawBoxes",
"Re", "Read", "ReadList", "ReadString", "RealAbs", "RealDigits", "RealSign",
"QuotientRemainder", "RadicalBox", "Ramp", "RamseyNumber", "RandomChoice", "RandomComplex",
"RandomInteger", "RandomGraph", "RandomPermutation", "RandomPrime", "RandomReal",
"RandomSample", "RandomVariate", "Range", "RankedMax", "RankedMin", "Rational", "Rationalize",
"RawBoxes", "Re", "Read", "ReadList", "ReadString", "RealAbs", "RealDigits", "RealSign",
"RealValuedNumericQ", "RealValuedNumberQ", "Reap", "Rectangle", "Reduce", "Refine",
"RegularExpression", "ReIm", "ReleaseHold", "Remove", "RemoveDiacritics", "Repeated",
"RepeatedNull", "RepeatedTiming", "Replace", "ReplaceAll", "ReplaceList", "ReplacePart",
Expand Down
Loading

0 comments on commit 77816fb

Please sign in to comment.