Skip to content

Bug: Number followed by j or J parses as a Python complex literal #321

In review
lambda-feedback/lambda-feedback-packages
#33

Description

@peterbjohnson

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 main at 6f29fd3
  • 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"]            # False
import 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_application inserts the *
  • strict syntax: the input is rejected as implicit multiplication, as 2i is

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 out

Prototype 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

  1. Strict syntax with complexNumbers: false: should 2j be rejected (consistent with 2i) or accepted as 2*j? The proposed fix rejects it.
  2. Physical quantities: app/context/physical_quantity/parsing.py calls the same parse_expression. Is 2J vs 2 J in scope? Untested.
  3. Other consumers of lambdafeedback.expression_parsing will change behaviour. Release after v0.3.0-rc.5, then bump the pin in compareExpressions.

Docs

  • app/docs/user.md, complexNumbers section: state that with complexNumbers: false, 2j is 2*j.
  • app/docs/user.md: add a note on entering vectors as 3i+2j+6k.
  • Package changelog.

Related

  • xi is read as ξ. Not in scope.
  • Alias substitution of input symbols operates on raw text (x2j becomes x2I with alias j for I). Not in scope.

Expression(s)/input that trigger it

See main text

Expected vs. actual result

See main text

Steps to reproduce / where it was used

See main text

Activity

  1. added
    bug
    on Nov 27, 2026
  2. peterbjohnson commented on Nov 27, 2026

    @peterbjohnson
    MemberAuthor

    Fix proposed in the underlying package:
    lambda-feedback/lambda-feedback-packages#33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions