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 12, 2024
1 parent 21f6897 commit 4856526
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1734,11 +1734,6 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
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 @@ -2216,11 +2211,6 @@ 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 @@ -4671,10 +4661,6 @@ 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 @@ -683,10 +683,6 @@ 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -826,22 +827,6 @@ 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 All @@ -856,6 +841,9 @@ public IASTMutable evalArgs(final IAST ast, final int attributes, boolean numeri
final int astSize = ast.size();

if (astSize > 1) {
if (!numericFunction) {
numericFunction = isNumericArg(ast);
}
boolean numericMode = fNumericMode;
boolean localNumericMode = fNumericMode;
final boolean isNumericFunction;
Expand Down Expand Up @@ -956,6 +944,20 @@ public IASTMutable evalArgs(final IAST ast, final int attributes, boolean numeri
return F.NIL;
}

/**
* Test if the arguments of this <code>ast</code> should be evaluated numerically in numeric mode.
*
* @param ast
* @return
*/
private boolean isNumericArg(final IAST ast) {
int id = ast.headID();
if (id >= 0) {
return Arrays.binarySearch(F.SORTED_NUMERIC_ARGS_IDS, id) >= 0;
}
return false;
}

/**
* Evaluate an AST with only one argument (i.e. <code>head[arg1]</code>). The evaluation steps are
* controlled by the header attributes.
Expand Down Expand Up @@ -1437,11 +1439,12 @@ private IExpr evalNoAttributes(IASTMutable mutableAST) {
}
final int astSize = mutableAST.size();
final boolean localNumericMode = fNumericMode;
final boolean argNumericMode = isNumericArg(mutableAST);
IASTMutable[] rlist = new IASTMutable[] {F.NIL};
mutableAST.forEach(1, astSize, (arg, i) -> {
if (!arg.isUnevaluated()) {
fNumericMode = localNumericMode;
evalArg(rlist, mutableAST, arg, i, false);
evalArg(rlist, mutableAST, arg, i, argNumericMode);
}
});
if (rlist[0].isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ public void createUserSymbol(ISymbol symbol) {}
public static final AbstractAST.NILPointer INVALID = AbstractAST.INVALID;
// public final static ISymbol usage = initFinalHiddenSymbol("usage");

/**
* Built-in function IDs those arguments should be evaluated numerically in numeric mode:
*/
public static final int[] SORTED_NUMERIC_ARGS_IDS = new int[] {//
ID.Equal, ID.Greater, ID.GreaterEqual, ID.Labeled, ID.Less, ID.LessEqual, ID.Unequal //
};

/**
* Used to represent a formal pattern <code>a_</code> that will be used only for predefined
* pattern-matching rules and can match one expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15984,6 +15984,10 @@ public void testN() {
"x==-1.5708");
check("ConditionalExpression(x==-157079632679/100000000000*C(1),C(1)∈Integers) // N", //
"ConditionalExpression(x==-1.5708*C(1),C(1)∈Integers)");
check("f(1/3)//N", //
"f(0.333333)");
check("f(1/2*x)//N", //
"f(0.5*x)");
check("N({Labeled(4/3,\"test\")})", //
"{Labeled(1.33333,test)}");
check("N(Labeled(4/3,\"test\"))", //
Expand Down

0 comments on commit 4856526

Please sign in to comment.