filmtools/polynomial
Composer 安装命令:
composer require filmtools/polynomial
包简介
README 文档
README
Tools for working with simple polynomial models.
Installation
$ composer require filmtools/polynomial
Upgrading from v1
From v2 on, this package now uses SplFixedArrays wherever possible. This will likely affect all your CoefficientProviderInterface implementations, as they now are required to return \SplFixedArray.
Wherever you calculate with FromCoefficientsInterpolator or MultipleInterpolator, you get \SplFixedArray results. When you rely on array results, just call $result->toArray() as defined in SplFixedArray methods API.
What's in the package
Interfaces
CoefficientsProviderInterface: Returns the coefficients of the polynomial model.
public function getCoefficients(): \SplFixedArray;
InterpolatorInterface: Find an y value for a given x coordinate.
public function interpolate( float $x ): float;
XFinderInterface: Find an x for a given y, as opposite to InterpolatorInterface::interpolate
public function findX( float $y ) : float;
PolynomialModelInterface extends the above InterpolatorInterface, XFinderInterface, and CoefficientsProviderInterface
PolynomialModelProviderInterface returns a polynomial model interface instance.
public function getPolynomialModel(): PolynomialModelInterface;
FromCoefficientsInterpolator
This callable class interpolates a iterable with X values using the coefficients given on invokation:
<?php use FilmTools\PolynomialModel\FromCoefficientsInterpolator; // Use these X values every time: $x_iterable = array(1,2,3); $fci = new FromCoefficientsInterpolator( $x_iterable ); // Now find Y for each X $coefficients_iterable = [ 0 => 2, 1 => 3 ]; $interpolated = $fci( $coefficients_iterable ); // SplFixedArray [ 5, 8, 11 ]
You may also pass custom X values on invokation:
$fci = new FromCoefficientsInterpolator; $coefficients = array(2,3); $x_values = array(1,2,3); // Now find Y for each X $interpolated = $fci( $coefficients, $x_values); // SplFixedArray [ 5, 8, 11 ]
The interpolation method also accepts CoefficientsProviderInterface:
use FilmTools\PolynomialModel\CoefficientsProviderInterface; use FilmTools\PolynomialModel\FromCoefficientsInterpolator; class MyModel implements CoefficientsProviderInterface { public function getCoefficients(): \SplFixedArray { return \SplFixedArray::fromArray(array(2,3)); } } $x_values = array(1,2,3); $fci = new FromCoefficientsInterpolator( $x_values ); $interpolated = $fci( new MyModel ); // SplFixedArray [ 5, 8, 11 ]
MultipleInterpolator
Interpolates iterables of X values using the same default coefficients. The interpolation method returns a SplFixedArray. The Constructor accepts a numbers iterable as well as CoefficientsProviderInterface:
<?php use FilmTools\PolynomialModel\MultipleInterpolator; $coefficients = array(2,3); $mi = new MultipleInterpolator( $coefficients ); $x_iterable = array(1,2,3); $interpolated = $mi->interpolate( $x_iterable ); $interpolated = $mi( $x_values ); // SplFixedArray [ 5, 8, 11 ]
DerivativeCoefficientsProvider
Calculates the coefficients for a derivative polynomial on a given a set of coefficients (either array or Provider).
<?php use FilmTools\PolynomialModel\DerivativeCoefficientsProvider; use FilmTools\PolynomialModel\CoefficientsProviderInterface; class MyModel implements CoefficientsProviderInterface { public function getCoefficients(): \SplFixedArray { // Keys are exponents, values are factors! return \SplFixedArray::fromArray( [0=>16, 1=>30, 2=>5, 3=> 18 ]); } } $my_provider = new MyModel; $derivation_provider = new DerivativeCoefficientsProvider( $my_provider ); $coefficients = $derivation_provider->getCoefficients(); // SplFixedArray( 0 => 30, 1 => 10, 2 => 54)
The class itself implements CoefficientsProviderInterface, and thus works excellent in conjunction with MultipleInterpolator. Here an example using the above MyModel class:
use FilmTools\PolynomialModel\DerivativeCoefficientsProvider; use FilmTools\PolynomialModel\MultipleInterpolator; $derivated_coefficients = new DerivativeCoefficientsProvider( new MyModel ); $interpolator = new MultipleInterpolator( $derivated_coefficients ); $x_values = array(1,2,3); $slopes = $interpolator->interpolate( $x_values ); // SplFixedArray 94, 266, 546
Exceptions
The PolynomialModelException extends \Exception and implements PolynomialModelExceptionInterface.
The NotEnoughPointsException as well as XNotFoundException and YNotFoundException extend from PolynomialModelException. All these have PolynomialModelExceptionInterface in common. Use these whenever something fails.
filmtools/polynomial 适用场景与选型建议
filmtools/polynomial 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 filmtools/polynomial 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 filmtools/polynomial 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-02