Enhanced polynomial parser with extended mathematical expression support #356
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.
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
add
andsub
transformations using->
syntaxadd()
andsub()
methods inPolyTransformer
for handling+
and-
operationsa - b
,2*x - 3*y + z
2. Unary Negation Support
term
rule intoneg_term
andpos_term
for explicit handling of negative termsneg_term()
andpos_term()
transformer methods-x
,-2*y
,-(x+y)
3. Exponentiation (Powers) Support
^
operator support in the grammar with explicitexponent
rulefactor
rule to handlebase ("^" exponent)?
patternsfactor()
andexponent()
methods inPolyTransformer
x^2
,y^3
,(x+1)^2
4. Parentheses for Grouping
base
rule to include"(" expr ")"
for proper expression grouping2*(x+y)
,(a+b)^2
,x*(y+z)^3
5. Implicit Multiplication
("*"? factor)*
2x
,3xy
,2(x+y)
instead of requiring explicit*
6. Decimal Number Support
decimal
rule and importedDECIMAL
from Lark commonsdecimal()
transformer method usingFraction
for precise arithmetic1.5x
,0.25*y^2
,3.14159
Examples of New Supported Expressions
x + y
,a - b
,2*x - 3*y + z
-x
,-2*y
,-(x+y)
,-x^2 + y
x^2 + y^3
,2^3
,(x+1)^2
2*(x+y)
,(a+b)*(c+d)
,3*(x+y)^2
2x
,3xy
,2(x+1)
,πr^2
1.5x + 0.25y
,3.14159*r^2
2x^2 - 3(y+z)^3 + 1.5
,-0.5*π*(r+1)^2
Technical Details
->
transformations for clean parsingFraction
for decimal precision to avoid floating-point errorsThis 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