Giter VIP home page Giter VIP logo

mathexpressioncompiler's Introduction

MathExpressionCompiler

How does it work?

  Before I tell you how compilers read and solve mathematical expressions, there are things that you must know.

  • Yours math expressions are called infix expressions.
    • mx+b
  • Compilers can only read postfix expressions.
    • mx*b+

  So, compilers do not read mathematical expressions like we do. They first change an infix to a postfix expression and then, evaluate it. Some compilers do this two steps at the same time. To return the expression answer, Queue and Stack are used during the process.
  The English Algorithm can be seen here.

How to use MathExpressionsCompiler functions

Import

from MathExpressionCompiler import to_postfix, evaluate
NOTE: My Queue and Stack AbstractDataTypes are needed at the same MathExpressionCompiler path

Infix to Postfix expression

infix_01 = 'a - b'
print(f'>> {to_postfix(infix_01)}')
# >> a b -

infix_02 = 'a - b * c'
print(f'>> {to_postfix(infix_02)}')
# a b c * -

infix_03 = '( a - b ) * c'
print(f'>> {to_postfix(infix_03)}')
# >> a b - c *

infix_04 = 'a + b * c ** d - e'
print(f'>> {to_postfix(infix_04)}')
# >> a b c d ** * + e -

infix_05 = 'a * ( b + c ) * ( d - g ) * h'
print(f'>> {to_postfix(infix_05)}')
# >> a b c + * d g - * h *

infix_06 = 'a * b - c * d ** e / f + g * h'
print(f'>> {to_postfix(infix_06)}')
# >> a b * c d e ** * f / - g h * +

Evaluate the postfix expression

print(f'>> {evaluate("8 7 2 * -")}')
# >> -6

Evaluate the postfix expression with variables

variables = {'a': 8, 'b': 7, 'c': 2}
print(f'>> {evaluate("a b c * -", variables)}')
# >> -6

Real/Usefull Exemple

Bhaskara Formula

expr_1 = '( 0 - b + ( ( b ** 2 - 4 * a * c ) ) ** sqrt ) / 2 * a'
expr_2 = '( 0 - b - ( ( b ** 2 - 4 * a * c ) ) ** sqrt ) / 2 * a'

print(f">> {to_postfix(expr_1)}")
# >> 0 b - b 2 ** 4 a * c * - sqrt ** + 2 / a *

print(f">> {to_postfix(expr_2)}")
# >> 0 b - b 2 ** 4 a * c * - sqrt ** - 2 / a *

variables = {'a': 1, 'b': -5, 'c': 6, 'sqrt': 0.5}
print(f">> {evaluate('0 b - b 2 ** 4 a * c * - sqrt ** + 2 / a *', variables)}")
# >> 3.0
print(f">> {evaluate('0 b - b 2 ** 4 a * c * - sqrt ** - 2 / a *', variables)}")
# >> 2.0

Tips

This algorithm doesn't support square root. But try:
sqrt(8) == pow(8, 0.5) == 8 ** 0.5
This algorithm doesn't support negative numbers in to_postfix() method. But try::
-8 == 0 - 8

mathexpressioncompiler's People

Contributors

senavs avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.