Skip to content

Commit

Permalink
WIP #937 N( Equal-expression ) does not return numeric result
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Mar 11, 2024
1 parent 85954f2 commit 3f76bf8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,12 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (ast.exists(x -> x.equals(S.Undefined))) {
return S.Undefined;
}

IAST evalArgs = engine.evalArgsN(ast, ISymbol.NOATTRIBUTE);
if (evalArgs.isPresent()) {
return evalArgs;
}

if (ast.size() > 2) {
IExpr.COMPARE_TERNARY b = IExpr.COMPARE_TERNARY.UNDECIDABLE;
if (ast.isAST2()) {
Expand Down Expand Up @@ -2210,6 +2216,11 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
return S.True;
}

IASTMutable evalArgs = engine.evalArgsN(ast, ISymbol.NOATTRIBUTE);
if (evalArgs.isPresent()) {
return evalArgs;
}

IASTAppendable flattened;
if ((flattened = EvalAttributes.flattenDeep(ast)).isPresent()) {
ast = flattened;
Expand Down Expand Up @@ -4660,6 +4671,10 @@ private static final class Unequal extends Equal {

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
IASTMutable evalArgs = engine.evalArgsN(ast, ISymbol.NOATTRIBUTE);
if (evalArgs.isPresent()) {
return evalArgs;
}
if (ast.exists(x -> x.equals(S.Undefined))) {
return S.Undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.matheclipse.core.convert.RGBColor;
import org.matheclipse.core.eval.Errors;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.interfaces.AbstractCoreFunctionEvaluator;
import org.matheclipse.core.eval.interfaces.AbstractEvaluator;
import org.matheclipse.core.eval.interfaces.AbstractSymbolEvaluator;
import org.matheclipse.core.eval.util.OptionArgs;
Expand Down Expand Up @@ -660,6 +661,7 @@ private static void init() {
S.Cylinder.setEvaluator(new Cylinder());
S.Dodecahedron.setEvaluator(new Dodecahedron());
S.Icosahedron.setEvaluator(new Icosahedron());
S.Labeled.setEvaluator(new Labeled());
S.Line.setEvaluator(new Line());
S.Octahedron.setEvaluator(new Octahedron());
S.Point.setEvaluator(new Point());
Expand All @@ -677,6 +679,24 @@ private static void init() {
}
}

private static final class Labeled extends AbstractCoreFunctionEvaluator {

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
IASTMutable evalArgs = engine.evalArgsN(ast, ISymbol.NOATTRIBUTE);
if (evalArgs.isPresent()) {
return evalArgs;
}
return F.NIL;
}

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

}

private static class Line extends AbstractEvaluator implements IGraphics2D, IGraphics3D {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,22 @@ public void evalArg(final IASTMutable[] result0, final IAST ast, final IExpr arg

}

/**
* Evaluate the arguments of the given <code>ast</code> numerically, if {@link #isNumericMode()}
* is <code>true</code> taking the attributes
* <code>HoldFirst, NHoldFirst, HoldRest, NHoldRest, NumericFunction</code> into account.
*
* @param ast
* @param attributes
* @return <code>F.NIL</code> is no evaluation was possible
*/
public IASTMutable evalArgsN(final IAST ast, final int attributes) {
if (isNumericMode()) {
return evalArgs(ast, attributes, true);
}
return F.NIL;
}

/**
* Evaluate the arguments of the given ast, taking the attributes <code>
* HoldFirst, NHoldFirst, HoldRest, NHoldRest, NumericFunction</code> into account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4044,20 +4044,23 @@ public boolean isNumericFunction(boolean allowList) {
}
}
}
if (allowList) {
if (head().isSymbol() && ((ISymbol) head()).isNumericFunctionAttribute() || isList()) {
// check if all arguments are &quot;numeric&quot;
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
addEvalFlags(
forAll ? IAST.IS_NUMERIC_FUNCTION_OR_LIST : IAST.IS_NOT_NUMERIC_FUNCTION_OR_LIST);
return forAll;
}
} else {
if (head().isSymbol() && ((ISymbol) head()).isNumericFunctionAttribute()) {
// check if all arguments are &quot;numeric&quot;
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
addEvalFlags(forAll ? IAST.IS_NUMERIC_FUNCTION : IAST.IS_NOT_NUMERIC_FUNCTION);
return forAll;
if (head().isSymbol()) {
ISymbol header = (ISymbol) head();
if (allowList) {
if (header.isNumericFunctionAttribute() || isList()) {
// check if all arguments are &quot;numeric&quot;
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
addEvalFlags(
forAll ? IAST.IS_NUMERIC_FUNCTION_OR_LIST : IAST.IS_NOT_NUMERIC_FUNCTION_OR_LIST);
return forAll;
}
} else {
if (header.isNumericFunctionAttribute()) {
// check if all arguments are &quot;numeric&quot;
boolean forAll = forAll(x -> x.isNumericFunction(allowList), 1);
addEvalFlags(forAll ? IAST.IS_NUMERIC_FUNCTION : IAST.IS_NOT_NUMERIC_FUNCTION);
return forAll;
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11685,4 +11685,8 @@ public static boolean isDomain(ISymbol domain) {
return domain == Algebraics || domain == Booleans || domain == Complexes || domain == Integers
|| domain == Primes || domain == Rationals || domain == Reals;
}

static {
C.setAttributes(ISymbol.NHOLDALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15979,6 +15979,11 @@ public void testDeterminePrecision() {

@Test
public void testN() {
// issue #937
check("(x==-157079632679/100000000000) // N", //
"x==-1.5708");
check("ConditionalExpression(x==-157079632679/100000000000*C(1),C(1)∈Integers) // N", //
"ConditionalExpression(x==-1.5708*C(1),C(1)∈Integers)");
check("N({Labeled(4/3,\"test\")})", //
"{Labeled(1.33333,test)}");
check("N(Labeled(4/3,\"test\"))", //
Expand Down

0 comments on commit 3f76bf8

Please sign in to comment.