Skip to content

Commit

Permalink
PolynomialQuotient avoid division by 0
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Mar 17, 2020
1 parent b923498 commit 03ad6bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ private static IExpr[] calculatePlusIntegerGCD(IASTAppendable numeratorPlus, IIn
times.set(1, ((IInteger) times.arg1()).div(gcd));
numeratorPlus.set(i, times);
} else {
error[0] = true;
error[0] = true;
}
}
});
if (error[0]) {
return null;
}
}
IExpr[] result = new IExpr[3];
result[0] = F.C1;
result[1] = numeratorPlus;
Expand Down Expand Up @@ -2183,7 +2183,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
IExpr result = F.REMEMBER_AST_CACHE.getIfPresent(ast);
if (result != null) {
return result;
}
}
try {
IExpr expr = F.evalExpandAll(ast.arg1(), engine);
// ASTRange r = new ASTRange(eVar.getVarList(), 1);
Expand Down Expand Up @@ -2233,7 +2233,7 @@ private static class FactorSquareFreeList extends Factor {
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {

VariablesSet eVar = new VariablesSet(ast.arg1());
VariablesSet eVar = new VariablesSet(ast.arg1());
try {
IExpr expr = F.evalExpandAll(ast.arg1(), engine);
// ASTRange r = new ASTRange(eVar.getVarList(), 1);
Expand Down Expand Up @@ -3223,34 +3223,38 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
IExpr arg1 = F.evalExpandAll(ast.arg1(), engine);
IExpr arg2 = F.evalExpandAll(ast.arg2(), engine);
if (arg1.isZero()||arg2.isZero()) {
if (arg1.isZero() || arg2.isZero()) {
return F.NIL;
}
if (!arg1.isPolynomialStruct()) {
// `1` is not a polynomial.
return IOFunctions.printMessage(ast.topHead(), "poly", F.List(arg1), engine);
}
if (!arg2.isPolynomialStruct()) {
// `1` is not a polynomial.
return IOFunctions.printMessage(ast.topHead(), "poly", F.List(arg2), engine);
}
if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] result = quotientRemainderModInteger(arg1, arg2, variable, option);
if (result == null) {
return F.NIL;
try {
if (!arg1.isPolynomialStruct()) {
// `1` is not a polynomial.
return IOFunctions.printMessage(ast.topHead(), "poly", F.List(arg1), engine);
}
if (!arg2.isPolynomialStruct()) {
// `1` is not a polynomial.
return IOFunctions.printMessage(ast.topHead(), "poly", F.List(arg2), engine);
}
if (ast.size() == 5) {
final OptionArgs options = new OptionArgs(ast.topHead(), ast, 4, engine);
IExpr option = options.getOption(F.Modulus);
if (option.isInteger() && !option.isZero()) {
IExpr[] result = quotientRemainderModInteger(arg1, arg2, variable, option);
if (result == null) {
return F.NIL;
}
return result[0];
}
return result[0];
return F.NIL;
}
return F.NIL;
}
IExpr[] result = quotientRemainder(arg1, arg2, variable);
if (result == null) {
return F.NIL;
IExpr[] result = quotientRemainder(arg1, arg2, variable);
if (result == null) {
return F.NIL;
}
return result[0];
} catch (ArithmeticException ae) {
// division by zero
}
return result[0];
}
return F.NIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15927,6 +15927,8 @@ public void testPolynomialQ() {
public void testPolynomialQuotient() {
check("PolynomialQuotient(0,2,x,Modulus->2)", //
"PolynomialQuotient(0,2,x,Modulus->2)");
check("PolynomialQuotient(x^2+4*x+1,-10,x,Modulus->2)", //
"PolynomialQuotient(1+4*x+x^2,-10,x,Modulus->2)");

check("PolynomialQuotient(x^2+4*x+1,Indeterminate,x,Modulus->3)", //
"PolynomialQuotient(1+4*x+x^2,Indeterminate,x,Modulus->3)");
Expand Down

0 comments on commit 03ad6bf

Please sign in to comment.