2017 © Pedro PelĂĄez
 

library math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

image

mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  • Friday, March 9, 2018
  • by mossadal
  • Repository
  • 6 Watchers
  • 39 Stars
  • 9,646 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 5 Forks
  • 3 Open issues
  • 22 Versions
  • 59 % Grown

The README.md

math-parser

Latest Stable Version Total Downloads License Code Climate, (*1)

DESCRIPTION

PHP parser and evaluator library for mathematical expressions., (*2)

Intended use: safe and reasonably efficient evaluation of user submitted formulas. The library supports basic arithmetic and elementary functions, as well as variables and extra functions., (*3)

The lexer and parser produces an abstract syntax tree (AST) that can be traversed using a tree interpreter. The math-parser library ships with three interpreters:, (*4)

  • an evaluator computing the value of the given expression.
  • a differentiator transforming the AST into a (somewhat) simplied AST representing the derivative of the supplied expression.
  • a rudimentary LaTeX output generator, useful for pretty printing expressions using MathJax

EXAMPLES

It is possible to fine-tune the lexer and parser, but the library ships with a StdMathParser class, capable of tokenizing and parsing standard mathematical expressions, including arithmetical operations as well as elementary functions., (*5)

~~~{.php} use MathParser\StdMathParser; use MathParser\Interpreting\Evaluator;, (*6)

$parser = new StdMathParser();, (*7)

// Generate an abstract syntax tree $AST = $parser->parse('1+2');, (*8)

// Do something with the AST, e.g. evaluate the expression: $evaluator = new Evaluator();, (*9)

$value = $AST->accept($evaluator); echo $value;, (*10)


More interesting example, containing variables: ~~~{.php} $AST = $parser->parse('x+sqrt(y)'); $evaluator->setVariables([ 'x' => 2, 'y' => 3 ]); $value = $AST->accept($evaluator);

We can do other things with the AST. The library ships with a differentiator, computing the (symbolic) derivative with respect to a given variable., (*11)

~~~{.php} use MathParser\Interpreting\Differentiator;, (*12)

$differentiator = new Differentiator('x'); $f = $parser->parse('exp(2x)-xy'); $df = $f->accept($differentiator);, (*13)

// $df now contains the AST of '2*exp(x)-y' and can be evaluated further $evaluator->setVariables([ 'x' => 1, 'y' => 2 ]); $df->accept($evaluator); ~~~, (*14)

Implicit multiplication

Another helpful feature is that the parser understands implicit multiplication. An expression as 2x is parsed the same as 2*x and xsin(x)cos(x)^2 is parsed as x*sin(x)*cos(x)^2., (*15)

Note that implicit multiplication has the same precedence as explicit multiplication. In particular, xy^2z is parsed as x*y^2*z and not as x*y^(2*z)., (*16)

To make full use of implicit multiplication, the standard lexer only allows one-letter variables. (Otherwise, we wouldn't know if xy should be parsed as x*y or as the single variable xy)., (*17)

DOCUMENTATION

For complete documentation, see the github.io project page, (*18)

THANKS

The Lexer is based on the lexer described by Marc-Oliver Fiset in his blog., (*19)

The parser is a version of the "Shunting yard" algorithm, described for example by Theodore Norvell., (*20)

The Versions

09/03 2018

dev-master

9999999-dev https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

04/01 2018

v1.3.13

1.3.13.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

04/01 2018

v1.3.12

1.3.12.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

02/10 2017

v1.3.11

1.3.11.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

01/10 2017

v1.3.10

1.3.10.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

21/02 2017

v1.3.9

1.3.9.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

19/12 2016

v1.3.8

1.3.8.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

19/12 2016

v1.3.7

1.3.7.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

01/12 2016

v1.3.6

1.3.6.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

01/12 2016

v1.3.5

1.3.5.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

29/10 2016

v1.3.4

1.3.4.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

24/10 2016

v1.3.3

1.3.3.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

24/10 2016

v1.3.2

1.3.2.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

24/10 2016

dev-develop

dev-develop https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

20/10 2016

v1.3.1

1.3.1.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

19/10 2016

v1.3.0

1.3.0.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

22/03 2016

v1.2.3

1.2.3.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

21/03 2016

v1.2.2

1.2.2.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

20/02 2016

v1.2.1

1.2.1.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

07/02 2016

v1.2.0

1.2.0.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

08/01 2016

v1.1.0

1.1.0.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.4.0

 

The Development Requires

by Frank Wikström

parser mathematics

04/01 2016

v1.0

1.0.0.0 https://github.com/mossadal/math-parser

PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.

  Sources   Download

LGPL-3.0

The Development Requires

by Frank Wikström

parser mathematics