Repository navigation
Description
Description of the bug
Summary
A numeric literal immediately followed by j or J (2j, 0.5j, 1e3j, 2J) is read by Python's tokenize as one imaginary-number token. SymPy's auto_number converts it to 2*I. With complexNumbers: false, I resolves to Symbol("I"); with complexNumbers: true, to the imaginary unit. Declaring j as an input symbol has no effect, because the token is formed before input symbols are applied.
Versions
- compareExpressions
mainat6f29fd3 - lambdafeedback-expression-parsing
v0.3.0-rc.5
Reproduce
from app.evaluation import evaluation_function
p = {"strict_syntax": False, "elementary_functions": True}
evaluation_function("3i+2j+6k", "3*i+2*j+6*k", p)["is_correct"] # False
evaluation_function("2j+1", "2*j+1", p)["is_correct"] # Falseimport tokenize, io
[t.string for t in tokenize.generate_tokens(io.StringIO("3i+2j+6k").readline) if t.string.strip()]
# ['3', 'i', '+', '2j', '+', '6', 'k']Observed (default params, complexNumbers: false)
| response | answer | is_correct |
|---|---|---|
2j, 2J, 0.5j, 1e3j |
2*j, 2*J, j/2, 1000*j |
false |
2jx |
2*j*x |
false |
2j^2 |
2*j^2 |
false |
x^2j |
x^(2*j) |
false |
3i+2j+6k |
3*i+2*j+6*k |
false |
3i+2j+6k (input symbols i, j, k declared) |
3*i+2*j+6*k |
false |
2 j, 2*j, j2, xj |
2*j, 2*j, 2*j, x*j |
true |
With physical_quantity: true, 2J against answer 2 J gives false. 2 J gives true.
Parse by mode
| input | complexNumbers false, loose | complexNumbers false, strict | complexNumbers true, loose | complexNumbers true, strict |
|---|---|---|---|---|
4j |
4*I (Symbol) |
4*I (Symbol) |
4*I (imaginary) |
4*I (imaginary) |
3i+2j+6k |
3*i + 6*k + 2*I (Symbol) |
syntax error | 3*i + 6*k + 2*I (imaginary) |
syntax error |
2J |
2*I (Symbol) |
2*I (Symbol) |
2*I (imaginary) |
2*I (imaginary) |
j |
j |
j |
j |
j |
2i |
2*i |
syntax error | 2*i |
syntax error |
With complexNumbers: true and input symbol I with aliases i, j, substitute_input_symbols("4j") returns "4I" before tokenizing. That path is not affected.
Proposed fix
Add a token transformation to SympyParsingConfig.transformations() (sympy_parsing.py), placed before auto_symbol and auto_number. When complexNumbers is false, it splits a NUMBER token ending in j or J into NUMBER + NAME. It inserts no *:
- loose syntax:
implicit_multiplication_applicationinserts the* - strict syntax: the input is rejected as implicit multiplication, as
2iis
When complexNumbers is true, behaviour is unchanged. No new parameter.
Prototype:
from tokenize import NUMBER, NAME
def split_imaginary_literals(tokens, local_dict, global_dict):
out = []
for tok in tokens:
if tok[0] == NUMBER and tok[1][-1] in "jJ":
out += [(NUMBER, tok[1][:-1]), (NAME, tok[1][-1])]
else:
out.append(tok)
return outPrototype results against plain SymPy (loose / strict): 3i+2j+6k gives 3*i + 2*j + 6*k / SyntaxError; 1e3j gives 1000.0*j / SyntaxError; sin(2j) gives sin(2*j) / SyntaxError. x2j and 0x1f are unchanged in both modes.
Expected after fix
| input | complexNumbers false, loose | complexNumbers false, strict | complexNumbers true |
|---|---|---|---|
4j |
4*j |
syntax error | unchanged (4*I, imaginary) |
3i+2j+6k |
3*i + 2*j + 6*k |
syntax error | unchanged |
2.5J |
2.5*J |
syntax error | unchanged |
j, x2j, 0x1f |
unchanged | unchanged | unchanged |
Open questions
- Strict syntax with
complexNumbers: false: should2jbe rejected (consistent with2i) or accepted as2*j? The proposed fix rejects it. - Physical quantities:
app/context/physical_quantity/parsing.pycalls the sameparse_expression. Is2Jvs2 Jin scope? Untested. - Other consumers of
lambdafeedback.expression_parsingwill change behaviour. Release afterv0.3.0-rc.5, then bump the pin in compareExpressions.
Docs
app/docs/user.md,complexNumberssection: state that withcomplexNumbers: false,2jis2*j.app/docs/user.md: add a note on entering vectors as3i+2j+6k.- Package changelog.
Related
xiis read asξ. Not in scope.- Alias substitution of input symbols operates on raw text (
x2jbecomesx2Iwith aliasjforI). Not in scope.
Expression(s)/input that trigger it
See main textExpected vs. actual result
See main text
Steps to reproduce / where it was used
See main text
Activity
peterbjohnson commentedon Nov 27, 2026
Fix proposed in the underlying package:
lambda-feedback/lambda-feedback-packages#33