Skip to content

Commit

Permalink
WIP #944 check if number of args in builtin-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Mar 15, 2024
1 parent 461ba62 commit 0bb06b4
Showing 1 changed file with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.exception.ValidateException;
import org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator;
import org.matheclipse.core.eval.interfaces.IFunctionEvaluator;
import org.matheclipse.core.eval.interfaces.IRewrite;
import org.matheclipse.core.eval.util.AbstractAssumptions;
import org.matheclipse.core.eval.util.SourceCodeProperties;
Expand Down Expand Up @@ -1961,7 +1962,7 @@ public IExpr.COMPARE_TERNARY equalTernary(IExpr that, EvalEngine engine) {
@Override
public final INumber evalNumber() {
if (isNumericFunction(true)) {
IExpr result = EvalEngine.get().evalN(this);
IExpr result = EvalEngine.get().evalNumericFunction(this);
if (result.isNumber()) {
return (INumber) result;
}
Expand All @@ -1973,7 +1974,7 @@ public final INumber evalNumber() {
@Override
public final IReal evalReal() {
if (isNumericFunction(true)) {
IExpr result = EvalEngine.get().evalN(this);
IExpr result = EvalEngine.get().evalNumericFunction(this);
if (result.isReal()) {
return (IReal) result;
}
Expand All @@ -1986,7 +1987,7 @@ public final IReal evalReal() {
} else if (isAST(S.Labeled, 3, 4)) {
IExpr arg1 = arg1();
if (arg1.isNumericFunction(true)) {
IExpr result = EvalEngine.get().evalN(arg1);
IExpr result = EvalEngine.get().evalNumericFunction(arg1);
if (result.isReal()) {
return (IReal) result;
}
Expand Down Expand Up @@ -3963,8 +3964,7 @@ public final boolean isModuleOrWithCondition() {
@Override
public boolean isNegative() {
if (isNumericFunction(true)) {

IExpr result = EvalEngine.get().evalN(this);
IExpr result = EvalEngine.get().evalNumericFunction(this);
if (result.isReal()) {
return result.isNegative();
}
Expand Down Expand Up @@ -4050,6 +4050,9 @@ public boolean isNumericFunction(boolean allowList) {
if (header.isNumericFunctionAttribute() || isList()) {
// check if all arguments are "numeric"
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
if (forAll && !isList()) {
forAll = hasExpectedArgSize(header);
}
addEvalFlags(
forAll ? IAST.IS_NUMERIC_FUNCTION_OR_LIST : IAST.IS_NOT_NUMERIC_FUNCTION_OR_LIST);
return forAll;
Expand All @@ -4058,6 +4061,9 @@ public boolean isNumericFunction(boolean allowList) {
if (header.isNumericFunctionAttribute()) {
// check if all arguments are "numeric"
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
if (forAll) {
forAll = hasExpectedArgSize(header);
}
addEvalFlags(forAll ? IAST.IS_NUMERIC_FUNCTION : IAST.IS_NOT_NUMERIC_FUNCTION);
return forAll;
}
Expand All @@ -4066,6 +4072,28 @@ public boolean isNumericFunction(boolean allowList) {
return false;
}

private boolean hasExpectedArgSize(ISymbol header) {
if (header.isBuiltInSymbol()) {
IEvaluator evaluator = ((IBuiltInSymbol) header).getEvaluator();
if (evaluator instanceof IFunctionEvaluator) {
int[] expected = ((IFunctionEvaluator) evaluator).expectedArgSize(this);
if (expected != null) {
int argSize = argSize();
if (argSize < expected[0] || argSize > expected[1]
|| (expected[1] == Integer.MAX_VALUE && expected.length == 2)) {
if (argSize < expected[0]) {
return false;
}
if (argSize > expected[1]) {
return false;
}
}
}
}
}
return true;
}

/** {@inheritDoc} */
@Override
public boolean isNumericFunction(VariablesSet varSet) {
Expand Down Expand Up @@ -4254,7 +4282,7 @@ public final boolean isPolynomialOfMaxDegree(ISymbol variable, long maxDegree) {
@Override
public boolean isPositive() {
if (isNumericFunction(true)) {
IExpr result = EvalEngine.get().evalN(this);
IExpr result = EvalEngine.get().evalNumericFunction(this);
if (result.isReal()) {
return ((IReal) result).isPositive();
}
Expand Down Expand Up @@ -4399,9 +4427,12 @@ public boolean isRealResult() {
}
}
}
IReal e = evalReal();
INumber e = evalNumber();
if (e != null) {
return true;
if (e.isReal()) {
return true;
}
return false;
}
if (isPlus() || isTimes()) {
// check if all arguments are &quot;real values&quot;
Expand Down

0 comments on commit 0bb06b4

Please sign in to comment.