Skip to content

Commit

Permalink
Improve Cancel and Together finding common factors
Browse files Browse the repository at this point in the history
>> Together((1/x-1/3)^3/(x-3)^2)
(3-x)/(27*x^3)");

>> Together((1/x-1/3)/(x-3))
-1/(3*x)
  • Loading branch information
axkr committed Nov 1, 2023
1 parent 1966bb3 commit af61595
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 443 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4795,8 +4795,10 @@ private static IAST cancelCommonFactors(IExpr part0, IExpr part1) {
}
}
if (p0.isTimes() || p1.isTimes()) {
final IAST p0Times = p0.isTimes() ? (IAST) p0 : F.Times(p0);
final IAST p1Times = p1.isTimes() ? (IAST) p1 : F.Times(p1);
IAST p0Times = AbstractFunctionEvaluator.getNegativePlusInTimes(p0);
IAST p1Times = AbstractFunctionEvaluator.getNegativePlusInTimes(p1);
// IAST p0Times = p0.isTimes() ? (IAST) p0 : F.Times(p0);
// IAST p1Times = p1.isTimes() ? (IAST) p1 : F.Times(p0);
IASTAppendable t0 = p0Times.copyAppendable();
IASTAppendable t1 = p1Times.copyAppendable();
int i = 1;
Expand Down Expand Up @@ -4840,8 +4842,7 @@ private static IAST cancelCommonFactors(IExpr part0, IExpr part1) {
t1Base = t1Arg.base();
t1Exponent = t1Arg.exponent();
}

if (t0Base.equals(t1Base) && t0Exponent.isReal() && t1Exponent.isReal()) {
if (t0Exponent.isReal() && t1Exponent.isReal() && t0Base.equals(t1Base)) {
IReal exp0 = (IReal) t0Exponent;
IReal exp1 = (IReal) t1Exponent;
final IReal subtracted;
Expand All @@ -4862,6 +4863,7 @@ private static IAST cancelCommonFactors(IExpr part0, IExpr part1) {
subtracted = exp1.subtractFrom(exp0);
t0.remove(i);
t1.set(j, F.Power(t1Base, subtracted));
j++;
if (Config.TRACE_BASIC_ARITHMETIC) {
if (EvalEngine.get().isTraceMode()) {
if (commonFactors.isNIL()) {
Expand Down Expand Up @@ -4900,6 +4902,7 @@ private static IAST cancelCommonFactors(IExpr part0, IExpr part1) {
}
}
return F.NIL;

}

/**
Expand Down Expand Up @@ -5315,17 +5318,23 @@ private static IRational factorTermsGCD(IAST plusAST, EvalEngine engine) {
*/
public static IExpr[] getNumeratorDenominator(IAST ast, EvalEngine engine, boolean together) {
if (together) {
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];
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);
}
return result;
}

IExpr[] result = new IExpr[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,16 @@ public static void setReset(final EvalEngine engine) {
transient boolean fNumericMode;

/**
* if <code>true</code> the engine evaluates in &quot;F.Together(expr)&quot; in IExpr#times()
* method.
* if <code>true</code> the engine evaluates &quot;F.Together(expr)&quot; in IExpr#times() method.
*/
transient boolean fTogetherMode;

/**
* If <code>true</code> the engine does no simplification of &quot;negative {@link S#Plus}
* expressions&quot; inside {@link S#Times} expressions in common subexpression determining.
*/
transient boolean fNoSimplifyMode;

transient boolean fEvalLHSMode;

transient boolean fEvalRHSMode;
Expand Down Expand Up @@ -601,6 +606,7 @@ public synchronized EvalEngine copy() {
engine.fSessionID = fSessionID;
engine.fStopRequested = false;
engine.fTogetherMode = fTogetherMode;
engine.fNoSimplifyMode = fNoSimplifyMode;
engine.fTraceMode = fTraceMode;
engine.fTraceStack = fTraceStack;
engine.f$Input = f$Input;
Expand Down Expand Up @@ -2982,6 +2988,7 @@ public final void init() {
fRecursionCounter = 0;
fNumericMode = false;
fTogetherMode = false;
fNoSimplifyMode = false;
fEvalLHSMode = false;
fEvalRHSMode = false;
fDisabledTrigRules = false;
Expand Down Expand Up @@ -3146,6 +3153,15 @@ public final boolean isTogetherMode() {
return fTogetherMode;
}

/**
* If <code>true</code> the engine does no simplification of &quot;negative {@link S#Plus}
* expressions&quot; inside {@link S#Times} expressions in common subexpression determining.
*
*/
public final boolean isNoSimplifyMode() {
return fNoSimplifyMode;
}

/**
* If the trace mode is set the system writes an evaluation trace list or if additionally the
* <i>stop after evaluation mode</i> is set returns the first evaluated result.
Expand Down Expand Up @@ -3213,6 +3229,7 @@ private void reset() {
fDisabledTrigRules = false;
fRecursionCounter = 0;
fTogetherMode = false;
fNoSimplifyMode = false;
fTraceMode = false;
fTraceStack = null;
fStopRequested = false;
Expand Down Expand Up @@ -3495,12 +3512,22 @@ public void setStopRequested(final boolean stopRequested) {
* {@link S#Together} command during the evaluation of the multiplication if the parameter is set
* to <code>true</code>.
*
* @param fTogetherMode if <code>true</code> the evaluation will be wrapped by a
* {@link S#Together} function in basic multiplication
* @param togetherMode if <code>true</code> the evaluation will be wrapped by a {@link S#Together}
* function in basic multiplication
* @see #isTogetherMode()
*/
public void setTogetherMode(boolean fTogetherMode) {
this.fTogetherMode = fTogetherMode;
public void setTogetherMode(boolean togetherMode) {
this.fTogetherMode = togetherMode;
}

/**
* If set to <code>true</code> the engine does no simplification of &quot;negative {@link S#Plus}
* expressions&quot; inside {@link S#Times} expressions in common subexpression determining.
*
* @param noSimplifyMode
*/
public void setNoSimplifyMode(boolean noSimplifyMode) {
this.fNoSimplifyMode = noSimplifyMode;
}

/** @param b */
Expand Down
Loading

0 comments on commit af61595

Please sign in to comment.