Simplify a product of numbers that cancels to one - #89
Merged
Conversation
When `simplify_integers_in_roots` merges the factors of a product, it accumulates the numeric factors into `numberFactor` and omits that factor from the result when it equals one. If the product consisted entirely of numbers, that left an empty multiplication `["*"]`, which later evaluates to `undefined`. That happens whenever a scalar is distributed over a vector, tuple, or matrix, since those products are formed after numbers are evaluated. An entry whose product with the scalar is one, such as the -1 entry of a matrix being subtracted, therefore disappeared from the sum. If the rest of that entry's sum was negative, the entry silently came out wrong; if it was positive, the missing operand sorted to the front of the sum and simplifying threw a TypeError. Return the number itself when there are no other factors, and bump the version so the fix can be released. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01563D3v3KXwuRLmni12AuvU
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.
Simplifying a numeric matrix subtraction such as
either produced a wrong entry or threw
TypeError: Cannot read properties of undefined (reading '0'), depending on the numbers involved.Cause
simplify_integers_in_rootsmerges the factors of a product, collecting numeric factors intonumberFactorand everything else intootherFactors. It then omitsnumberFactorfrom the result when it equals one:When the product consisted entirely of numbers,
otherFactorsis empty, so a product whose numbers cancel to one became the empty multiplication["*"], whichevaluate_numbers_subreduces toundefined.Ordinary numeric products never reach that state, because
simplifyevaluates numbers before this pass runs. The products created by distributing a scalar over a vector, tuple, or matrix do, sinceperform_vector_matrix_additions_scalar_multiplicationsbuilds them afterwards. An entry whose product with the scalar is one — most commonly an entry of-1in a matrix being subtracted — therefore vanished from the entry's sum. What the reader saw depended on howdefault_ordersorted the broken node among the remaining addends:undefinedsorted last, and theright === undefinedearly return in the"+"branch dropped it silently, giving a wrong entryundefinedsorted first, andleft[0]threw theTypeErrorBoth symptoms are reachable from user input in DoenetML, where
<math simplify>$A + $B - $C</math>over numeric matrices is common.Fix
Return the accumulated number when there are no other factors, so the product collapses to
1rather than to nothing.Also in this PR
2.x— two by assertion, one with theTypeError.2.0.0-alpha96.build/regenerated. It had not been rebuilt since Switch to Vite #68, so this also picks up thelib/mathjs.jschange from Publishnumericon the global object so browsers can use it #86.Full suite passes: 6194 tests across 29 files.
🤖 Generated with Claude Code