Repository navigation
expression-parsing: read a number followed by j or J like 2i - #33
Open
peterbjohnson wants to merge 1 commit into
Open
peterbjohnson wants to merge 1 commit into
peterbjohnson wants to merge 1 commit into
Conversation
Python's tokenizer reads `2j` (also `2.5J`, `1e3j`) as one imaginary literal, so without complexNumbers `3i+2j+6k` parsed as `3*i + 2*I + 6*k`, whatever symbols the task defined. A new transformation, placed before auto_symbol and used only without complex_numbers, splits such a NUMBER token into the number and the name `j` or `J`. Implicit multiplication then reads it as `2*j`; with strict syntax it is a syntax error, like `2i`. With complex_numbers nothing changes. Names such as `x2j` are NAME tokens and unaffected. Fixes lambda-feedback/compareExpressions#321. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes lambda-feedback/compareExpressions#321.
Python's tokenizer reads
2j(also2.5J,1e3j) as one imaginary literal, andauto_numberturns it into2*I. So withoutcomplexNumbers,3i+2j+6kparsed as3*i + 2*I + 6*k(withIjust an ordinary symbol), whatever input symbols the task defined.Change
A new token transformation
_split_imaginary_literalsinSympyParsingConfig.transformations(). It runs first, beforeauto_symbol, and only whencomplex_numbersis off. It splits a NUMBER token ending inj/Jinto the number and the NAMEj/J, without inserting*, so the existing rules apply:complexNumbersoff, loose4j4*j4*I)4*I, unchanged3i+2j+6k3*i + 2*j + 6*k2.5J5*J/25*I/2)j,x2j,x_2j,0x1fIn strict syntax
2jis now rejected, the same as2i.1e3jstill runs into compareExpressions#322 (ewithelementary_functions), which is out of scope here.Physical quantities still need a space between value and unit:
2Jkeeps its "Jwas not interpreted as a unit" feedback.Checks
ruff check,ruff format --check,mypy: cleanscripts/test.sh: all pass. The 15 new tests are inTestNumberFollowedByJ, and 7 of them fail without the fix.tools/parity/check.py: 0 unexpected, 0 mismatchedmainwith this branch installed: 1281 passed, 2 skipped, 1 xfailedReleasing needs a version bump and tag (e.g.
v0.3.0-rc.6) after merge, so compareExpressions can pin it.🤖 Generated with Claude Code