hoa-math-community / math
The Hoa\Math 库。
v2.0.0
2022-12-17 12:21 UTC
Requires
- php: ~8.0
- hoa-math-community/compiler: ~4.0
- hoa-math-community/consistency: ~3.0
- hoa-math-community/exception: ~3.0
- hoa-math-community/iterator: ~3.0
- hoa-math-community/protocol: ~2.0
- hoa-math-community/zformat: ~2.0
This package is auto-updated.
Last update: 2024-09-17 16:30:34 UTC
README
Hoa 是一套 模块化、可扩展 和 结构化 的 PHP 库。
此外,Hoa 旨在成为工业和研究领域之间的桥梁。
Hoa\Math
此库提供数学运算的工具。
了解更多.
安装
使用 Composer,要将此库添加到依赖项中,您需要要求 hoa/math
$ composer require hoa/math '~1.0'
有关更多安装程序,请参阅 源代码页面。
测试
在运行测试套件之前,必须安装开发依赖项
$ composer install
然后,要运行所有测试套件
$ vendor/bin/hoa test:run
有关更多信息,请参阅 贡献指南。
快速使用
我们提出快速了解一个功能:算术表达式的评估。
算术表达式的评估
hoa://Library/Math/Arithmetic.pp
描述了算术表达式的形式。因此,我们将使用处理语法的经典工作流程,涉及 Hoa\Compiler
库 和 Hoa\Math\Visitor\Arithmetic
类。
// 1. Load the compiler. $compiler = Hoa\Compiler\Llk::load( new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp') ); // 2. Load the visitor, aka the “evaluator”. $visitor = new Hoa\Math\Visitor\Arithmetic(); // 3. Declare the expression. $expression = '1 / 2 / 3 + 4 * (5 * 2 - 6) * PI / avg(7, 8, 9)'; // 4. Parse the expression. $ast = $compiler->parse($expression); // 5. Evaluate. var_dump( $visitor->visit($ast) ); /** * Will output: * float(6.4498519738463) */ // Bonus. Print the AST of the expression. $dump = new Hoa\Compiler\Visitor\Dump(); echo $dump->visit($ast); /** * Will output: * > #addition * > > #division * > > > token(number, 1) * > > > #division * > > > > token(number, 2) * > > > > token(number, 3) * > > #multiplication * > > > token(number, 4) * > > > #multiplication * > > > > #group * > > > > > #substraction * > > > > > > #multiplication * > > > > > > > token(number, 5) * > > > > > > > token(number, 2) * > > > > > > token(number, 6) * > > > > #division * > > > > > token(constant, PI) * > > > > > #function * > > > > > > token(id, avg) * > > > > > > token(number, 7) * > > > > > > token(number, 8) * > > > > > > token(number, 9) */
我们可以通过 addFunction
和 addConstant
方法在访问者中添加函数和常量。因此,我们将添加具有 2 个参数的 rand
函数和设置为 42 的 ANSWER
常量。
$visitor->addFunction('rand', function ($min, $max) { return mt_rand($min, $max); }); $visitor->addConstant('ANSWER', 42); $expression = 'rand(ANSWER / 2, ANSWER * 2)' var_dump( $visitor->visit($compiler->parse($expression)) ); /** * Could output: * int(53) */
文档
Hoa\Math 的黑客手册 包含有关如何使用此库以及它是如何工作的详细信息。
要本地生成文档,请执行以下命令
$ composer require --dev hoa/devtools $ vendor/bin/hoa devtools:documentation --open
有关更多文档,请访问项目的网站: hoa-project.net。
获取帮助
主要有两种方法可以获得帮助
- 在
#hoaproject
IRC 频道中, - 在 users.hoa-project.net 的论坛上。
贡献
您想贡献吗?谢谢!详细的 贡献指南 解释了您需要知道的一切。
许可
Hoa 采用新的 BSD 许可证 (BSD-3-Clause)。请参阅 LICENSE
了解详细信息。
相关项目
以下项目正在使用此库
- PSIH & PMSIpilot,PSIH 是法国医疗保健行业商业智能解决方案的领先集成商,
- PHP Telegram Bot,基于官方 Telegram Bot API 的 Telegram 机器人。