Download the PHP package nadybot/math-parser without Composer
On this page you can find all versions of the php package nadybot/math-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nadybot/math-parser
More information about nadybot/math-parser
Files in nadybot/math-parser
Package math-parser
Short Description PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.
License LGPL-3.0
Homepage https://github.com/Nadybot/math-parser
Informations about the package math-parser
math-parser
This is a fork from the original math-parser that's adapted to fit the need of my own projects. Please don't use it without talking to me, because it might behave in unexpected ways.
The codebase was modified to work with PHP 8.1+, and adhere to the coding styles of my projects.
DESCRIPTION
PHP parser and evaluator library for mathematical expressions.
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.
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:
- 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.
More interesting example, containing variables:
We can do other things with the AST. The library ships with a differentiator, computing the (symbolic) derivative with respect to a given variable.
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
.
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)
.
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
).
THANKS
The original math-parser library was coded by Frank Wikström
The Lexer is based on the lexer described by Marc-Oliver Fiset in his blog.
The parser is a version of the "Shunting yard" algorithm, described for example by Theodore Norvell.
All versions of math-parser with dependencies
ext-ctype Version *