PHP code example of samsara / newton

1. Go to this page and download the library: Download samsara/newton library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samsara / newton example snippets


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

$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();

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 = 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);