循环 / 牛顿
一个提供物理单位库的库,这些单位相互完全了解,并且可以在它们上执行数学运算。
Requires
- php: >=5.6.0
- ircmaxell/random-lib: 1.1.*
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
本项目对5.6.X和7.X进行单元测试,除非测试通过,否则不接受合并。
安装
要安装,只需使用Composer要求该软件包。
composer require samsara/newton "1.*"
或者将其包含在您的composer.json
文件中
{ "require": { "samsara/newton": "1.*" } }
项目命名空间为Samsara\Newton\*
。您可以在Packagist上查看项目。
用法
使用此软件包有许多方法。首选的方法是实例化UnitComposition类,并将其用作工厂。
为了允许新的单位类扩展Quantity与UnitComposition类一起工作,有必要强制实例化它。这将在扩展部分进一步解释。
这也意味着如果您直接实例化一个单位,您必须注入一个UnitComposition实例。
$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]
您还可以添加不同类型的单位。
$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;
MathProvider有静态方法,允许您使用BC Math扩展执行数学运算。在项目中,我们可能会很容易超过PHP_INT_MAX限制,因此我们内部使用此功能。它还提供了一些随机函数,包括gaussianRandom()方法。
PhysicsProvider有静态方法,使用正确的单位类实现一些常见的物理方程。
此库的一个有趣且非物理用途是确定给定服务器每秒可以执行循环多少次。例如
$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();
扩展
添加新单位相对简单。您必须首先创建您的单位类,并且此类必须扩展Samsara\Newton\Core\Quantity
。此类必须在$units
属性中定义一组单位(在其中定义$rates
的索引),然后定义它们之间的相对转换率。
所有转换都必须用原生
单位表示,这定义在$native
属性中。
示例
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']); } }
然后,在调用上下文中,您必须使用自定义单位包含的类型定义准备UnitComposition类。这使得UnitComposition类能够在适当的时候自动使用您的自定义类进行乘法和除法操作。
$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);
只有通过addUnit()调用以这种方式准备的UnitComposition实例才会理解如何自动返回MyUnit()的实例。因此,建议将UnitComposition类视为服务,并在您的应用程序中使用其单个实例。
贡献
请确保拉取请求符合以下指南
- 在拉取请求中创建的新文件必须有一个相应的单元测试文件,或者必须被现有测试文件覆盖。
- 您的合并可能不会使项目的测试覆盖率低于85%。
- 您的合并可能不会使项目的测试覆盖率降低超过5%。
- 您的合并必须通过Travis-CI构建测试,对于PHP 5.6.X和PHP 7.X都有效。
有关更多信息,请参阅贡献部分。