samsara/newton
Composer 安装命令:
composer require samsara/newton
包简介
A library providing physics units which are fully aware of each other, and which can have math operations performed on them.
关键字:
README 文档
README
This project is unit tested against 5.6.X and 7.X, and merges are not accepted unless the tests pass on both.
Installation
To install, simply require the package using composer:
composer require samsara/newton "1.*"
Or include it in your composer.json file:
{
"require": {
"samsara/newton": "1.*"
}
}
The project namespace is Samsara\Newton\*. You can view the project on Packagist.
Usage
There are many ways to use this package. The preferred way involves instantiating the UnitComposition class and using it as a factory.
In order to allow new unit classes which extend Quantity to work with the UnitComposition class, it was necessary to force instantiation of it. This is explained in further detail in the Extending section.
This also means that if you directly instantiate a unit, you must inject a UnitComposition instance.
$unitComposition = new Samsara\Newton\Core\UnitComposition(); $thrust = $unitComposition->getUnitClass(UnitComposition::FORCE, 1000); echo $thrust; // 1000 Newtons $mass = $unitComposition->getUnitClass(UnitComposition::MASS, 1000); echo $mass; // 1000 kg $acceleration = $thrust->divideBy($mass); echo $acceleration; // 1 m/s^2 $acceleration->addAlias('N/kg', 'm/s^2')->to('N/kg'); echo $acceleration; // 1 N/kg [Gravitational field strength]
You can also add unit of different types.
$unitComposition = new Samsara\Newton\Core\UnitComposition(); $thrust = $unitComposition->getUnitClass(UnitComposition::FORCE, 1000); echo $thrust; // 1000 Newtons $mass = $unitComposition->getUnitClass(UnitComposition::MASS, 500); echo $mass; // 500 kg $mass2 = new Mass(5, $unitComposition, 't'); // 't' = metric ton = 1000kg $mass->add($mass2); echo $mass; // 5500 kg;
The MathProvider has static methods which allow you to perform math operations using the BC Math extension. This is used internally in the project as we might very easily exceed the PHP_INT_MAX limit during unit conversions. It also provides several random functions, including a gaussianRandom() method.
The PhysicsProvider has static methods which implement some common physics equations using the correct unit classes.
An interesting, non-physics use for something like this library is to determine how many times a given server can execute a loop per second. For instance:
$unitComposition = new UnitComposition(); $start = microtime(true); for ($i = 0;$i < 10000;$i++) { // Loop to test } $end = microtime(true); $duration = $end - $start; $durationInMilliseconds = $duration * 1000; $time = new Time($durationInMilliseconds, $unitComposition, 'ms'); $cycles = new Cycles(10000, $unitComposition); $loopsPerSecond = $unitComposition->naiveDivide($cycles, $time); // The number of times, as measured, that the computer can execute the loop // in a single second. echo $loopsPerSecond->getValue();
Extending
Adding new units is relatively easy. You must first make your unit class, and this class must extend Samsara\Newton\Core\Quantity. This class must define a set of units in the $units property (where it defines the index for $rates), and then define the relative conversion rates between them.
All of the conversions must be in terms of the native unit, which is defined in the property $native.
Example
use Samsara\Newton\Core\Quantity; use Samsara\Newton\Core\UnitComposition; class MyUnit extends Quantity { const SOMEUNIT = 'g'; const BIGUNIT = 'bg'; protected $units = [ // It is the first index in the rates array self::SOMEUNIT => 1, self::BIGUNIT => 2 ]; protected $native = self::SOMEUNIT; public function __construct($value, UnitComposition $unitComposition, $unit = null) { $this->rates = [ // Almost always the 'native' unit is set equal to 1 $this->units[self::SOMEUNIT] => '1', $this->units[self::BIGUNIT] => '1000', ]; parent::__construct($value, $unitComposition, $unit) $this->setComposition($unitComposition->dynamicUnits['MyUnit']); } }
Then, in the calling context, you must prepare the UnitComposition class with the definitions of what types of units this custom unit contains. This allows the UnitComposition class to automatically use your custom class in multiply and divide operations when it is appropriate to do so.
$unitComposition = new UnitComposition(); // This will automatically instatiate the class Namespaced\MyUnit() // when 'time' has an exponent of 2, and 'mass' has an exponent of 1 // after multiply or divide operations using the naive*() methods. // // The last argument defines how you can refer to the unit in the // factory method: getUnitClass() $unitComposition->addUnit('Namespaced\\MyUnit', ['time' => 2, 'mass' => 1], 'MyUnit'); // Now we can instantiate two ways: // $myunit is now an object of type MyUnit, in its native units, with a value of zero $myunit = $unitComposition->getUnitClass('MyUnit'); // Object of MyUnit type in native units with value 1000 $myunit2 = $unitComposition->getUnitClass('MyUnit', 1000); // OR // MyUnit object in BIGUNIT with value 1 == 1000 in SOMEUNIT $myunit3 = new Namespaced\MyUnit(1, $unitComposition, 'bg'); // We can add them if we want // Automatically converts. $myunit3 now has value of 2 and units of BIGUNIT. $myunit3->add($myunit2)->add($myunit);
Only the instance of UnitComposition prepared in the way outlined above, with a call to addUnit(), will understand how to automatically return an instance of MyUnit(). Because of this, it is suggested that you treat the UnitComposition class as a service, and use a single instance of it within your application.
Contributing
Please ensure that pull requests meet the following guidelines:
- New files created in the pull request must have a corresponding unit test file, or must be covered within an existing test file.
- Your merge may not drop the project's test coverage below 85%.
- Your merge may not drop the project's test coverage by MORE than 5%.
- Your merge must pass Travis-CI build tests for BOTH PHP 5.6.X and PHP 7.X.
For more information, please see the section on Contributing
samsara/newton 适用场景与选型建议
samsara/newton 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 30, 最近一次更新时间为 2015 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「math」 「conversion」 「unit」 「units」 「physics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samsara/newton 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samsara/newton 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samsara/newton 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
LaTeX formula to PNG
Symfony bundle for unigen test generator
A cli tool which generates unit tests.
PHPUnit Testing extensions for HMTL and CSS insures valid HTML and CSS via v.Nu validator
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2015-08-25