mossadal/math-parser
Composer 安装命令:
composer require mossadal/math-parser
包简介
PHP parser for mathematical expressions, including elementary functions, variables and implicit multiplication. Also supports symbolic differentiation.
关键字:
README 文档
README
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.
use MathParser\StdMathParser;
use MathParser\Interpreting\Evaluator;
$parser = new StdMathParser();
// Generate an abstract syntax tree
$AST = $parser->parse('1+2');
// Do something with the AST, e.g. evaluate the expression:
$evaluator = new Evaluator();
$value = $AST->accept($evaluator);
echo $value;
More interesting example, containing variables:
$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.
use MathParser\Interpreting\Differentiator;
$differentiator = new Differentiator('x');
$f = $parser->parse('exp(2*x)-x*y');
$df = $f->accept($differentiator);
// $df now contains the AST of '2*exp(x)-y' and can be evaluated further
$evaluator->setVariables([ 'x' => 1, 'y' => 2 ]);
$df->accept($evaluator);
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).
DOCUMENTATION
For complete documentation, see the github.io project page
THANKS
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.
mossadal/math-parser 适用场景与选型建议
mossadal/math-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.81M 次下载、GitHub Stars 达 179, 最近一次更新时间为 2016 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「mathematics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mossadal/math-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mossadal/math-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mossadal/math-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Mathematical functions with strict typing and predictable error handling
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Geo calculations library
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 1.81M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 179
- 点击次数: 26
- 依赖项目数: 16
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2016-01-04