Skip to content

Conversation

RazinShaikh
Copy link
Contributor

This PR significantly expands the capabilities of the symbolic polynomial parser by adding support for four key mathematical expression features that were previously missing.

Changes Made

1. Subtraction Support

  • Enhanced grammar with proper add and sub transformations using -> syntax
  • Added add() and sub() methods in PolyTransformer for handling + and - operations
  • Now supports expressions like a - b, 2*x - 3*y + z

2. Unary Negation Support

  • Split term rule into neg_term and pos_term for explicit handling of negative terms
  • Added neg_term() and pos_term() transformer methods
  • Properly handles expressions like -x, -2*y, -(x+y)

3. Exponentiation (Powers) Support

  • Added ^ operator support in the grammar with explicit exponent rule
  • Updated factor rule to handle base ("^" exponent)? patterns
  • Implemented factor() and exponent() methods in PolyTransformer
  • Now supports expressions like x^2, y^3, (x+1)^2

4. Parentheses for Grouping

  • Enhanced base rule to include "(" expr ")" for proper expression grouping
  • Enables complex nested expressions like 2*(x+y), (a+b)^2, x*(y+z)^3

5. Implicit Multiplication

  • Modified grammar to support optional multiplication operator: ("*"? factor)*
  • Now accepts natural mathematical notation like 2x, 3xy, 2(x+y) instead of requiring explicit *

6. Decimal Number Support

  • Added decimal rule and imported DECIMAL from Lark commons
  • Implemented decimal() transformer method using Fraction for precise arithmetic
  • Supports expressions with decimal coefficients like 1.5x, 0.25*y^2, 3.14159

Examples of New Supported Expressions

  • Basic arithmetic: x + y, a - b, 2*x - 3*y + z
  • Negation: -x, -2*y, -(x+y), -x^2 + y
  • Powers: x^2 + y^3, 2^3, (x+1)^2
  • Grouping: 2*(x+y), (a+b)*(c+d), 3*(x+y)^2
  • Implicit multiplication: 2x, 3xy, 2(x+1), πr^2
  • Decimals: 1.5x + 0.25y, 3.14159*r^2
  • Complex combinations: 2x^2 - 3(y+z)^3 + 1.5, -0.5*π*(r+1)^2

Technical Details

  • Uses Lark's native operator precedence with -> transformations for clean parsing
  • Maintains backward compatibility with existing expressions
  • Leverages Fraction for decimal precision to avoid floating-point errors
  • Proper handling of operator precedence ensures mathematical correctness

This enhancement makes the polynomial parser much more user-friendly and capable of handling natural mathematical expressions while maintaining the robust symbolic computation capabilities of the existing system.

Closes zxcalc/zxlive#232 and closes zxcalc/zxlive#353

…ation in syntax. Motivation here is that the __str__ methods of poly use dot for multiplication so we should allow parsing it
@jvdwetering
Copy link
Collaborator

jvdwetering commented Oct 13, 2025

Nice! Perhaps add some tests to show things it can parse and also how it fails when it gets things it doesn't understand? (probably can be auto-generated by copilot)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minus symbol leads to invalid phase input Support basic expressions for parameterised spiders

2 participants