Repository navigation
Factor: a cache hit returns what the first call returned - #1571
Merged
Merged
Conversation
AlgebraUtil.factor(IAST ast, ...) put its result into the engine cache under ast before Factor.evaluate applied factorSigns to it, so since 0d84662 a second Factor of the same argument returned the form before factorSigns: {Factor(6/(2-x)), Factor(6/(2-x))} gave {-6/(-2+x),6/(2-x)}. AlgebraUtil.factor(IExpr, EvalEngine), which TrigSimplifyFu.tr0 calls for Simplify and FullSimplify, and togetherPlus passed F.Factor(e) as that ast, and so wrote the same key with results computed with other settings: after Simplify(6/(2-x)+Cos(y)) the entry was F.NIL, and Factor returned its argument. Factor.evaluate now caches the result it returns, after factorSigns, and AlgebraUtil.factor writes nothing. Factor.evaluate is the only reader of that cache, so the other callers lose nothing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Two
Factorcalls with the same argument on oneEvalEnginegive different answers, and so does aFactorafter aSimplifyof the same expression:Factor(e)alone gives-6/(-2+x),-(a-b)^3,-Cos(y)^2/(-2+x)and(-6-2*Cos(y)+x*Cos(y))/(-2+x). The engine cache under the keyFactor(e)holds something elsethan what
Factor(e)returns:AlgebraUtil.factor(IAST ast, ...)puts its result into the cache beforeFactor.evaluateapplies
factorSignsto it (0d84662), so a cache hit returns the form beforefactorSigns.AlgebraUtil.factor(IExpr, EvalEngine)(withHomogenization=false, called byTrigSimplifyFu.tr0, whichSimplifyandFullSimplifyrun) andtogetherPlus(trig=true)pass
F.Factor(e)as thatast, and so write the same key with results computed with othersettings. After
Simplify(6/(2-x)+Cos(y))the entry isF.NIL, andFactorreturns itsargument.
More inputs and the history are in #1570.
The fix
Factor.evaluatecaches the result it returns, afterfactorSigns, andAlgebraUtil.factor(IAST ast, ...)writes nothing to the cache.Factor.evaluateis the onlyreader of that cache, so the other callers lose nothing. A first call returns what it returned
before; only cache hits change. With the
Factor.evaluatechange alone, aFactorafterTrigSimplifyFu,SimplifyorFullSimplifystill returns the result they cached.Tests
AlgebraTest#testFactorIssue1570checks the first two inputs above, the equationFactor((x+2)/(x-5)+3==6/(2-x))twice, andFactor(e)afterTrigSimplifyFu(e),Simplify(e)andFullSimplify(e)for four inputs.AlgebraTest#testFactorIssue1570expected: <{-6/(-2+x),-6/(-2+x)}> but was: <{-6/(-2+x),6/(2-x)}>at the first checkThe fast tier is
mvn test -pl matheclipse-core -am; compared test by test, none of the other 7769changed its result. Verified on 9a50f18, against a build with the fix and a build without it.
Fixes #1570
🤖 Generated with Claude Code