Skip to content

Commit

Permalink
Ìmprove PolynomialQ
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Dec 24, 2024
1 parent 76dc812 commit b58f27f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -48,7 +49,6 @@
import org.matheclipse.core.eval.exception.JASConversionException;
import org.matheclipse.core.eval.exception.LimitException;
import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.interfaces.AbstractCoreFunctionEvaluator;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
import org.matheclipse.core.eval.util.OptionArgs;
import org.matheclipse.core.expression.ASTSeriesData;
Expand Down Expand Up @@ -3660,7 +3660,7 @@ public void setUp(final ISymbol newSymbol) {
*
* </blockquote>
*/
private static class PolynomialQ extends AbstractCoreFunctionEvaluator
private static class PolynomialQ extends AbstractFunctionEvaluator
implements BiPredicate<IExpr, IExpr> {

/**
Expand All @@ -3669,27 +3669,49 @@ private static class PolynomialQ extends AbstractCoreFunctionEvaluator
*/
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (ast.isAST2()) {
IExpr cached = engine.getCache(ast);
if (cached != null) {
return cached;
IAST variablesList = F.NIL;
IExpr arg1 = ast.arg1();
if (ast.isAST1()) {
// mimic S.Variables
IAST temp = VariablesSet.getAlgebraicVariables(arg1);
if (temp.isList()) {
variablesList = temp;
} else {
return F.NIL;
}
IExpr arg1 = engine.evaluate(ast.arg1());
IExpr arg2 = engine.evaluate(ast.arg2());

IAST variablesList = arg2.makeList();
IAST subst = substituteVariablesInPolynomial(arg1, variablesList, "§PolynomialQ", true);
if (subst.isPresent()) {
IExpr result = F.booleSymbol(subst.arg1().isPolynomial((IAST) subst.arg2()));
engine.putCache(ast, result);
return result;
if (variablesList.size() == 0) {
return S.True;
}
return F.NIL;
} else {
variablesList = ast.arg2().makeList();
Set<IExpr> fVariablesSet = new HashSet<IExpr>();
VariablesSet.addAlgebraicVariables(fVariablesSet, variablesList);
for (int i = 1; i < variablesList.size(); i++) {
IExpr variable = variablesList.get(i);
if (variable.isPlus() || variable.isTimes()) {
// `1` is not a valid variable.
Errors.printMessage(S.General, "ivar", F.List(variable));
return F.NIL;
}
}
// if (!fVariablesSet.contains(variable)) {
// // `1` is not a valid variable.
// Errors.printMessage(S.General, "ivar", F.List(variable));
// return F.NIL;
// }
}
if (ast.isAST1()) {
return S.True;
IExpr cached = engine.getCache(ast);
if (cached != null) {
return cached;
}
return S.False;
IAST subst = substituteVariablesInPolynomial(arg1, variablesList, "§PolynomialQ", false);
if (subst.isPresent()) {
IExpr result = F.booleSymbol(subst.arg1().isPolynomial((IAST) subst.arg2()));
engine.putCache(ast, result);
return result;
}
return F.NIL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,19 @@ public boolean isPolynomial(final IExpr expression, boolean coefficient)
return true;
} else if (ast.isPower()) {
IExpr base = ast.base();
IExpr exponent = ast.exponent();
for (int i = 1; i < vars.size(); i++) {
IExpr variable = vars.get(i);
if (variable.equals(base)) {
int exponent = ast.exponent().toIntDefault();
if (exponent < 0) {
return false;
if (exponent.isRational() && exponent.isNonNegativeResult()) {
return true;
}
return true;
return false;
// int exponent = ast.exponent().toIntDefault();
// if (exponent < 0) {
// return false;
// }
// return true;
} else if (variable.isPower() && variable.base().equals(ast.base())
&& variable.exponent().isRational()) {
IExpr expr = variable.exponent().reciprocal().times(ast.exponent());
Expand Down

0 comments on commit b58f27f

Please sign in to comment.