コード例 #1
0
ファイル: Quantity.php プロジェクト: rawroland/Newton
 /**
  * Take an instance of any unit and attempt to divide by that unit after squaring it. This will pass
  * responsibility to the UnitComposition class so that it can attempt to figure out what the resulting
  * units should be.
  *
  * @param   Quantity $quantity
  * @param   int $precision
  * @return  Quantity
  * @throws  \Exception
  */
 public function divideBySquared(Quantity $quantity, $precision = 2)
 {
     return $this->unitCompClass->naiveMultiOpt([$this], [$quantity, $quantity], $precision);
 }
コード例 #2
0
ファイル: PhysicsProvider.php プロジェクト: rawroland/Newton
 /**
  * Universal Gravitation Equation:
  *
  * A = (G m1 m2)/ r^2
  *
  * @param   Mass[]|Acceleration[]|Length[]  ...$quantities
  * @return  Mass|Acceleration|Length
  * @throws  \Exception
  */
 public static function universalGravitation(...$quantities)
 {
     if (count($quantities) != 3) {
         throw new \Exception('The Universal Gravitation Equation requires at least three given units to calculate something.');
     }
     $vals = [];
     foreach ($quantities as $unit) {
         if ($unit instanceof Mass) {
             /** @var Mass */
             $vals['mass'][] = $unit;
         } elseif ($unit instanceof Length) {
             /** @var Length */
             $vals['length'] = $unit;
         } elseif ($unit instanceof Acceleration) {
             /** @var Acceleration */
             $vals['acceleration'] = $unit;
         } else {
             throw new \Exception('Only Mass, Acceleration and Length valid units for the Universal Gravitation Equation.');
         }
     }
     $gravitation = new Gravitation();
     $unitComposition = new UnitComposition();
     if (array_key_exists('mass', $vals) && count($vals['mass']) == 2) {
         if (array_key_exists('length', $vals)) {
             /** @return Acceleration */
             return $unitComposition->naiveMultiOpt([$gravitation, $vals['mass'][0], $vals['mass'][1]], [$vals['length'], $vals['length']]);
         } else {
             /** @return Length */
             return $gravitation->squareRoot([$vals['mass'][0], $vals['mass'][1]], [$vals['acceleration']]);
         }
     } else {
         /** @return Mass */
         return $unitComposition->naiveMultiOpt([$vals['acceleration'], $vals['length'], $vals['length']], [$gravitation, $vals['mass'][0]]);
     }
 }