innmind/math
Composer 安装命令:
composer require innmind/math
包简介
Library holding a set of math functions
关键字:
README 文档
README
Expose some math concepts as objects.
Note: all classes are immutable.
Algebra
use Innmind\Math\Algebra\{ Value, Integer, }; $perimeter = Value::two->multiplyBy(Value::pi, $r = Integer::of(42)); // value still not calculated echo $perimeter->toString(); // 2 x π x 42 (value still not calculated) echo $perimeter->value(); // 263.89378290154
By doing math like this you calculate the data that's really needed, so if you pass around a variable but never check it's content then it will never be calculated. The other advantage is that by casting to a string an operation you can see what the operation steps are (might be helpful for debugging a function operation).
Note: by calling collapse on a Number it will try to optimize some calculations such as squareRoot(square(x)) will directly return x thus avoiding rounding errors.
Definition sets
use Innmind\Math\{ DefinitionSet\Range, Algebra\Integer, Algebra\Value, }; $set = Range::exlusive(Value::zero, Value::infinite); echo $set->toString(); // ]0;+∞[ $set->contains(Integer::of(42)); // true $set->contains(Integer::of(-42)); // false $set = $set->union( Range::exclusive(Value::negativeInfinite, Value::zero), ); echo $set; // ]-∞;0[∪]0;+∞[ $set->contains(Integer::of(-42)); // true $set->contains(Integer::of(0)); // false
Polynom
use Innmind\Math\Polynom\Polynom; $p = Polynom::interceptAt($intercept = Integer::of(1)) ->withDegree(Integer::of(1), new Number(0.5)) ->withDegree(Integer::of(2), new Number(0.1)); $p(Integer::of(4))->value(); // 4.6 echo $p->toString(); // 0.1x^2 + 0.5x + 1
You also can call the derived number for any point x (as well as the tangent). You can have access to the primitive and derivative of the polynom, the last one is notably used to calculate an Integral.
use Innmind\Math\Polynom\Integral; $integral = Integral::of($somePolynom); $area = $integral(Integer::of(0), new Integral(42)); // find the area beneath the curve between point 0 and 42 echo $integral->toString(); // ∫(-1x^2 + 4x)dx = [(-1 ÷ (2 + 1))x^3 + (4 ÷ (1 + 1))x^2] (if the polynom is -1x^2 + 4x)
Regression
Polynomial Regression
use Innmind\Math\{ Regression\PolynomialRegression, Regression\Dataset, Algebra\Integer, }; $regression = PolynomialRegression::of( Dataset::of([ [-8, 64], [-4, 16], [0, 0], [2, 4], [4, 16], [8, 64], ]), ); // so in essence it found x^2 $regression(Integer::of(9))->value(); // 81.0
Linear regression
use Innmind\Math\{ Regression\LinearRegression, Regression\Dataset, Algebra\Integer; }; $r = LinearRegression::of(Dataset::of([ [0, 0], [1, 1], [2, 0], [3, 2], ])); $r->intercept()->value(); // 0.0 $r->slope()->value(); // 0.5 $r(Integer::of(4))->value(); // 2.0
Probabilities
use Innmind\Math\{ Regression\Dataset, Probabilities\Expectation, Probabilities\StandardDeviation, Probabilities\Variance, }; $dataset = Dataset::of([ [-1, 4/6], // 4 6th of a chance to obtain a -1 [2, 1/6], [3, 1/6], ]); echo Expectation::of($dataset)()->value(); //0,1666666667 (1 6th) echo StandardDeviation::of($dataset)()->value(); //√(101/36) echo Variance::of($dataset)()->value(); //101/36
Quantile
use Innmind\Math\Quantile\Quantile; use Innmind\Immutable\Sequence; $q = Quantile::of(Sequence::of(...\range(1,12))); $q->min()->value(); // 1 $q->max()->value(); // 12 $q->mean(); // 6.5 $q->median()->value(); // 6.5 $q->quartile(1)->value(); // 3.5 because 25% of values from the set are lower than 3.5 $q->quartile(3)->value(); // 9.5 because 75% of values from the set are lower than 9.5
innmind/math 适用场景与选型建议
innmind/math 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.24k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「math」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 innmind/math 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 innmind/math 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 innmind/math 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LaTeX formula to PNG
Partition problem for balanced arrays splitting made easy.
Simple, chainable calculation library.
Easy to use arithmetic operations for decimals in PHP (using BCMath)
Core logic for elegant graphic visualization. Render to SVG, PNG, JPG and Base64.
Mandelbrot set simple PHP Generator.
统计信息
- 总下载量: 20.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-20