irrevion / science
PHP扩展数学库
0.0.5
2024-01-23 22:05 UTC
Requires
- php: >=8.1
README
PHP扩展数学库。它主要关注复数、四元数和线性代数的运算。
杀手特性及优势
- 改进的
Math::pow($base, $exponent)
函数,能够将任何实数或复数提升到任何实数或复数次方 - 支持 复数 和四元数(类似于Python的NumPy + PyQuaternion + SymPy)
- 使用
(new Quantity(0.7, IAU::parsec))->convert(SI::metre)
进行物理单位转换(将添加更多单位...)
安装
该库可在 Packagist 上找到,并可以使用CLI命令安装
composer require irrevion/science
使用
如果通过composer安装,请包含由composer生成的自动加载器,并添加use irrevion\science\Math\Math
语句,例如。
如果您不使用composer或使用自定义文件夹,则可以修改并包含自定义自动加载器。
示例
require_once("../autoloader.php"); use irrevion\science\Helpers\Utils; use irrevion\science\Math\Operations\Delegator; use irrevion\science\Math\Math; use irrevion\science\Math\Entities\{Scalar, Fraction, Imaginary, Complex};
示例
标量是一个基本数字类
use irrevion\science\Math\Entities\Scalar; $n = new Scalar(5); print("Scalar to string is {$n}\nType of n is ".($n::class)."\n"); // Outputs: // Scalar to string is 5 // Type of n is irrevion\science\Math\Entities\Scalar
支持基本算术运算
$sum = $n->add(new Scalar(3)); $diff = $n->subtract(new Scalar(2)); $prod = $n->multiply(new Scalar(10)); $quotient = $n->divide(new Scalar(3));
如上所述,pow()函数已得到显著增强
use irrevion\science\Helpers\Utils; use irrevion\science\Math\Math; use irrevion\science\Math\Operations\Delegator; use irrevion\science\Math\Entities\{Scalar, Fraction, Imaginary, Complex}; $x = 2; $y = 3; $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".Delegator::getType($z)." ) \n"; // Output: 2**3 is 8 ( integer ) $x = new Scalar(25); $y = new Scalar(2); $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n"; // Output: 25**2 is 625 ( irrevion\science\Math\Entities\Scalar ) $x = new Scalar(25); $y = new Scalar(-2); $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n"; // Output: 25**-2 is 0.0016 ( irrevion\science\Math\Entities\Scalar ) $x = new Scalar(-1.678903); $y = new Scalar(-2.5); $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n"; // Output: -1.678903**-2.5 is [8.3827564317097E-17 + -0.27380160345158i] ( irrevion\science\Math\Entities\Complex ) // Vanilla PHP will give you: NAN // Python will give you: (8.382756431709652e-17-0.2738016034515765j) $x = new Scalar(Math::E); $y = new Imaginary(Math::PI); $z = Math::pow($x, $y); print "e**πi is {$z} ( ".($z::class)." ) \n"; // Output: e**πi is [-1 + 1.2246467991474E-16i] ( irrevion\science\Math\Entities\Complex ) // Python gives: (-1+1.2246467991473532e-16j) // Expected: -1 by formulae e**πi+1=0 $x = new Scalar(125); $y = new Fraction('1/3'); $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n"; // Output: 125**1/3 is 5 ( irrevion\science\Math\Entities\Scalar ) $x = new Scalar(4); $y = new Complex(2.5, 2); $z = Math::pow($x, $y); print "{$x}**{$y} is {$z} ( ".($z::class)." ) \n"; // Output: 4**(2.5+2i) is [-29.845986458754 + 11.541970902054i] ( irrevion\science\Math\Entities\Complex ) // Python gives: (-29.845986458754275+11.541970902053793j)
还可以获取一个数的所有根
$roots = (new Complex(-64))->roots(3); print Utils::printR($roots)." \n"; // Output: [[2 + 3.4641016151378i], [-4 + 4.8985871965894E-16i], [2 + -3.4641016151378i]]
因此,您可以看到以下操作都是支持的
- 实数到实数次方的提升
- 实数到分数次方的提升(2/3表示立方根的平方)
- 负数到有理数或实数次方的提升(1/n是一个数的n次根)
- 任何数到负数次方的提升(有理数或实数)
- 任何数到虚数或复数次方的提升
- 任何可用的值(实体类型实例:混合/数字,标量,分数,虚数,复数,复数极坐标,四元数分量,四元数,向量等)至少到正整数次方
版本历史
~dev-main
- 符号数学实现开始
- 物理:添加了Rydberg能量单位
v0.0.5
- 矩阵求逆实现(以及求伴随矩阵、伴随矩阵、余子式、行阶梯形、简化行阶梯形、秩的方法)
- 复数幂、指数、自然对数实现
- 四元数添加
- 物理:添加了Dalton、Apostilb、Planck能量单位
v0.0.4
- 添加了向量检查方法(正交、共线、共面)
- 添加了向量间角度计算
- 物理:添加了角度单位
v0.0.3
- 矩阵行列式实现
- 物理:添加了新的单位(欧姆、法拉、瓦特等)