irrevion/science 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

irrevion/science

Composer 安装命令:

composer require irrevion/science

包简介

PHP library for extended mathematical operations

README 文档

README

PHP library for extended mathematical operations. Its mostly focused on operations with Complex numbers, Quaternions and Linear Algebra.

Key features and advantages

  • Improved Math::pow($base, $exponent) function, able to raise any real or complex number to any real or complex exponent
  • Support for Complex numbers and Quaternions ( just like Python with NumPy + PyQuaternion + SymPy )
  • Physics units conversion (more units to be added...) using (new Quantity(0.7, IAU::parsec))->convert(SI::metre)

Installation

Library is available on Packagist and could be installed using CLI command:

composer require irrevion/science

Usage

If installed via composer include composer generated autoloader and add use irrevion\science\Math\Math statement for example.
If you are not using composer or using a custom folder you can modify and include custom autoloader.
Example:

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};

Examples

Scalar is a basic number class:

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

Basic arithmetic operations available:

$sum = $n->add(new Scalar(3));
$diff = $n->subtract(new Scalar(2));
$prod = $n->multiply(new Scalar(10));
$quotient = $n->divide(new Scalar(3));

As mentioned above, significant enhancement of pow() function has been made:

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)

It is also possible to get all roots of a number

$roots = (new Complex(-64))->roots(3);
print Utils::printR($roots)." \n";
// Output: [[2 + 3.4641016151378i], [-4 + 4.8985871965894E-16i], [2 + -3.4641016151378i]]

So, as you can see, the following operations are supported:

  • raise of real number to a real power
  • raise of real number to a fractional power (2/3 means cubic root squared)
  • raise of negative number to a rational or real power (1/n is n-th root of a number)
  • raise of an any number to negative power (rational or real)
  • raise of a number to imaginary or complex power
  • raise of an any available value (entity-type instance: mixed/numeric, Scalar, Fraction, Imaginary, Complex, ComplexPolar, QuaternionComponent, Quaternion, Vector, etc) at least to positive integer power

Versions history

~dev-main

  • Symbolic math implementation started
  • Physics: Added Rydberg energy, kilometre, attometre, angstrom and Bohr radius units

v0.0.5

  • Matrix inversion implemented (as well as methods for finding cofactor matrix, adjugate, minors, row echelon form, reduced REF, rank)
  • Complex power, exponentiation, natural logarythm implemented
  • Quaternions added
  • Physics: Added units Dalton, Apostilb, Planck energy

v0.0.4

  • Vector checking methods added (orthogonal, collinear, coplarar)
  • Angle between vectors calculation added
  • Physics: Angle units added

v0.0.3

  • Matrix determinant implemented
  • Physics: New units added (Ohm, Farad, Watt, etc)

v0.0.2

  • Added units in physics
  • Fixed fallback autoloader
  • Implemented search by unit entity in physical unit categories

v0.0.1

  • Basic entities implemented
  • Composer requirements are met

Documentation

irrevion/science 适用场景与选型建议

irrevion/science 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「math」 「science」 「vector」 「linear-algebra」 「matrix」 「physics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 irrevion/science 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 irrevion/science 我们能提供哪些服务?
定制开发 / 二次开发

基于 irrevion/science 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-24