Skip to content

Commit

Permalink
Refactor/Clean up
Browse files Browse the repository at this point in the history
- renamed Algebra.getNumeratorDenominator -> numeratorDenominator
  • Loading branch information
axkr committed Nov 2, 2023
1 parent af61595 commit befe819
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ private static IExpr factor(IAST ast, IExpr arg1, VariablesSet eVar, boolean squ
if (!arg1.isTimes() && !arg1.isPower()) {
expr = S.Together.of(engine, arg1);
if (expr.isAST()) {
IExpr[] parts = Algebra.getNumeratorDenominator((IAST) expr, engine, true);
IExpr[] parts = Algebra.numeratorDenominator((IAST) expr, true, engine);
if (!parts[1].isOne()) {
try {
IExpr numerator = factorExpr(F.Factor(parts[0]), parts[0], eVar, squareFree,
Expand Down Expand Up @@ -2296,7 +2296,7 @@ public static IExpr evaluateSolve(IExpr expr, SolveData solveData, EvalEngine en
IExpr variable = eVar.getArrayList().get(0);
expr = S.Together.of(engine, expr);
if (expr.isAST()) {
IExpr[] parts = Algebra.getNumeratorDenominator((IAST) expr, engine, true);
IExpr[] parts = Algebra.numeratorDenominator((IAST) expr, true, engine);
try {
IExpr numerator =
factorExprSolve(F.Factor(parts[0]), parts[0], variable, solveData, engine);
Expand Down Expand Up @@ -5309,41 +5309,80 @@ private static IRational factorTermsGCD(IAST plusAST, EvalEngine engine) {
}

/**
* Split the expression into numerator and denominator parts, by calling the <code>Numerator[]
* </code> and <code>Denominator[]</code> functions
* Split the {@link IAST} expression into numerator and denominator parts, by calling the
* <code>Numerator(ast)</code> and <code>Denominator(ast)</code> functions and return the result
* at index <code>0</code> (numerator) and index <code>1</code> (denominator).
*
* @param ast
* @param together TODO
* @return an array with the numerator, denominator and the evaluated <code>Together[expr]</code>.
* @param together if <code>true</code> the evaluated <code>Together(ast)</code> result, will be
* appended at index <code>2</code> in the result array
* @return an array with the numerator, denominator and the evaluated <code>Together(ast)</code>
* if requested.
*/
public static IExpr[] getNumeratorDenominator(IAST ast, EvalEngine engine, boolean together) {
public static IExpr[] numeratorDenominator(IAST ast, boolean together, EvalEngine engine) {
// if (together) {
// boolean noSimplifyMode = engine.isNoSimplifyMode();
// try {
// engine.setNoSimplifyMode(true);
// IExpr[] result = new IExpr[3];
// result[2] = together(ast, engine);
// // split expr into numerator and denominator
// result[1] = engine.evaluate(F.Denominator(result[2]));
// if (!result[1].isOne()) {
// // search roots for the numerator expression
// result[0] = engine.evaluate(F.Numerator(result[2]));
// } else {
// result[0] = ast; // result[2];
// }
// return result;
// } finally {
// engine.setNoSimplifyMode(noSimplifyMode);
// }
// }
//
// IExpr[] result = new IExpr[2];
// // split expr into numerator and denominator
// result[1] = engine.evaluate(F.Denominator(ast));
// if (!result[1].isOne()) {
// result[0] = engine.evaluate(F.Numerator(ast));
// } else {
// result[0] = ast;
// }
// return result;

if (together) {
boolean noSimplifyMode = engine.isNoSimplifyMode();
try {
engine.setNoSimplifyMode(true);
IExpr[] result = new IExpr[3];
result[2] = together(ast, engine);
// split expr into numerator and denominator
result[1] = engine.evaluate(F.Denominator(result[2]));
if (!result[1].isOne()) {
// search roots for the numerator expression
result[0] = engine.evaluate(F.Numerator(result[2]));
} else {
result[0] = ast; // result[2];
}
return result;
return splitNumeratorDenominator(ast, result[2], result, engine);
} finally {
engine.setNoSimplifyMode(noSimplifyMode);
}
}

IExpr[] result = new IExpr[2];
// split expr into numerator and denominator
result[1] = engine.evaluate(F.Denominator(ast));
return splitNumeratorDenominator(ast, ast, result, engine);
}

/**
* Split <code>rewrittenAST</code> into numerator and denominator.
*
* @param originalAST the original {@link IAST} expression
* @param rewrittenAST the rewritten AST (for example by {@link S#Together}
* @param result the allocated result array
* @param engine the evaluation engine
*
* @return the <code>result</code> array of expressions <code>[numerator, denominator]</code>.
*/
private static IExpr[] splitNumeratorDenominator(final IAST originalAST, final IExpr rewrittenAST,
IExpr[] result, EvalEngine engine) {
result[1] = engine.evaluate(F.Denominator(rewrittenAST));
if (!result[1].isOne()) {
result[0] = engine.evaluate(F.Numerator(ast));
result[0] = engine.evaluate(F.Numerator(rewrittenAST));
} else {
result[0] = ast;
result[0] = originalAST;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
return assoc;
} catch (ValidateException ve) {
Errors.printMessage(S.Association, ve, engine);
// print no message
}
return evaled ? assocList : F.NIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private IExpr tryTransformations(IExpr expr) {
if (fFullSimplify) {
if (together.isTimes()) {
IExpr[] parts =
Algebra.getNumeratorDenominator((IAST) together, EvalEngine.get(), true);
Algebra.numeratorDenominator((IAST) together, true, EvalEngine.get());
IExpr numerator = parts[0];
IExpr denominator = parts[1];
// common factors in numerator, denominator may be canceled here, so check if we have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public int simplifyAndAnalyze() {
}

public void splitNumeratorDenominator(IAST ast) {
IExpr[] result = Algebra.getNumeratorDenominator(ast, fEngine, true);
IExpr[] result = Algebra.numeratorDenominator(ast, true, fEngine);
this.fNumerator = result[0];
this.fDenominator = result[1];
this.fExpr = result[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private static IExpr extractVariableRecursive(IExpr exprWithVariable, IExpr expr
IAST timesWithoutVariable = timesFilter[0];
IAST timesWithVariable = timesFilter[1];
if (timesWithoutVariable.isAST0()) {
IExpr[] numerDenom = Algebra.getNumeratorDenominator(ast, EvalEngine.get(), true);
IExpr[] numerDenom = Algebra.numeratorDenominator(ast, true, EvalEngine.get());
if (!numerDenom[1].isOne()) {
IExpr[] numerLinear = numerDenom[0].linear(variable);
if (numerLinear != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24339,6 +24339,10 @@ public void testTimeValue() {
}

public void testTogether() {
check("Together((1/x-1/3)/(x-3)^2)", //
"1/(3*(3-x)*x)");
check("Together((1/x-1/3)^3/(x-3))", //
"-(3-x)^2/(27*x^3)");
check("Together((1/x-1/3)^3/(x-3)^2)", //
"(3-x)/(27*x^3)");
check("Together((1/x-1/3)/(x-3))", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ public void testSolveInequality() {
}

public void testSolve() {
check("Solve(3*(x+a)*(x-b)==0,x)", //
"{{x->-a},{x->b}}");

// github #261 - JUnit test for Apfloat switching to complex Power calculation
check("Solve(0.00004244131815783 == x^5 , x)", //
"{{x->-0.10802279680851234+I*0.07848315587546605},{x->-0.10802279680851212+I*(-0.07848315587546605)},"//
Expand Down

0 comments on commit befe819

Please sign in to comment.