gamajo / quadratic
处理二次方程求解。
1.0.0
2016-05-02 22:56 UTC
Requires
- php: >=7
Requires (Dev)
- phpunit/phpunit: ^5.2
This package is auto-updated.
Last update: 2024-08-23 18:15:27 UTC
README
PHP类,用于处理和求解二次方程。
什么是二次方程?
二次方程是一个一元二次多项式,其度为2,然后设置为等于零,以便可以确定未知数。
ax2 + bx + c = 0
方程通常可以通过因式分解或完成平方来求解,但还有一个二次公式。
二次方程接口构造函数接受、和c的值,求解器可以返回一个或两个根,包括虚根。
安装
composer require gamajo/quadratic
需要PHP 7。
用法
基本用法
创建方程对象,将其传递给求解器,求解并检索根
use Gamajo\Quadratic; // Represents x^2 + 5x + 6 = 0. $equation = new BasicQuadraticEquation(1, 5, 6); $solver = new Solver($equation); $solver->solve(); echo $solver->get(); // '2 and 3' echo $solver->get('root1'); // '2' echo $solver->get('root2'); // '3'
求解器对复数根没有问题
use Gamajo\Quadratic; // Represents 3x^2 + 4x + 5 = 0. $equation = new BasicQuadraticEquation(3, 4, 5); $solver = new Solver($equation); $solver->solve(); echo $solver->get(); // '-0.667 + 1.106i and -0.667 - 1.106i'
BasicQuadraticEquation
方法
BasicQuadraticEquation
类实现了 QuadraticEquation
接口,该接口又扩展了 Equation
接口。因此,它支持以下方法
use Gamajo\Quadratic; // Represents x^2 + 5x + 6 = 0. $equation = new BasicQuadraticEquation(1, 5, 6); echo $equation->getA(); // 1 echo $equation->getB(); // 5 echo $equation->getC(); // 6 print_r( $equation->getArgsAsArray() ); // [1, 5, 6]
还有一个 hasValidArguments()
方法,但由于这个库使用标量类型声明,手动检查它们是否都是整数是有些多余的。
Solver
方法
Solver
类可以设置根的最大小数位数精度
use Gamajo\Quadratic; // Represents 8x^2 + 5x - 2 = 0. $equation = new BasicQuadraticEquation(8, 5, -2); $solver = new Solver($equation); $solve->setPrecision(4); // Default precision is 3 echo $solve->getPrecision(); // 4 $solver->solve(); echo $solver->get(); // '-0.9021 and 0.2771' instead of '-0.902 and 0.277'
变更日志
请参阅变更日志。
许可证
MIT.
贡献
欢迎贡献 - 请在 develop
分支上fork,修复并发送pull请求。
致谢
由Gary Jones构建。
原始过程化代码版本,版权2004 Gary Jones。
此版本版权2016 Gamajo Tech