/**
  * Convert if possible a supplied argument to a rational
  *
  * @param int|float|string|NumericTypeInterface $value
  *
  * @return \Chippyash\Math\Matrix\RationalNumber
  *
  * @throws \Chippyash\Matrix\Exceptions\MatrixException
  * @throws \Exception
  */
 protected function convertNumberToRational($value)
 {
     if ($value instanceof NumericTypeInterface) {
         return $value->asRational();
     }
     switch (gettype($value)) {
         case 'integer':
             return RationalTypeFactory::create($value, 1);
         case 'double':
             return RationalTypeFactory::fromFloat($value);
         case 'string':
             try {
                 return RationalTypeFactory::fromString($value);
             } catch (\Exception $e) {
                 try {
                     return ComplexTypeFactory::fromString($value)->asRational();
                 } catch (\Exception $ex) {
                     throw new MatrixException('The string representation of the number is invalid for a rational');
                 }
             }
         case 'NULL':
             return RationalTypeFactory::create(0, 1);
         case 'boolean':
             return RationalTypeFactory::create($value ? 1 : 0, 1);
         default:
             throw new MatrixException('Rational expects int, float, string, Rational or NumericTypeInterface ');
     }
 }
 /**
  * @dataProvider polars
  */
 public function testCreateFromPolarReturnsComplexType($r, $t)
 {
     $radius = RationalTypeFactory::fromString($r);
     $theta = RationalTypeFactory::fromString($t);
     $p = ComplexTypeFactory::fromPolar($radius, $theta);
     $this->assertInstanceOf(self::CTYPE_NAME, $p);
 }
Пример #3
0
 /**
  * Create and return a rational number matrix
  * $data elements are either:
  *  - a RationalType
  *  - string representations of rational number
  *  - a PHP float
  *  - a 2 item array representing numerator & denominator e.g. [2,-4] = '-2/4'
  *
  * @param array $data
  *
  * @return \Chippyash\Math\Matrix\RationalMatrix
  *
  * @throws \Chippyash\Math\Matrix\Exceptions\MathMatrixException
  */
 public static function createRational(array $data)
 {
     foreach ($data as &$row) {
         foreach ($row as &$item) {
             if (!$item instanceof RationalType) {
                 if (is_array($item) && count($item) == 2) {
                     $item = RationalTypeFactory::create($item[0], $item[1]);
                 } elseif (is_string($item)) {
                     try {
                         $item = RationalTypeFactory::fromString($item);
                     } catch (\InvalidArgumentException $e) {
                         throw new MathMatrixException('Invalid item type for Rational Matrix');
                     }
                 } elseif (is_float($item)) {
                     $item = RationalTypeFactory::fromFloat($item);
                 } else {
                     throw new MathMatrixException('Invalid item type for Rational Matrix');
                 }
             }
         }
     }
     return new RationalMatrix($data);
 }
Пример #4
0
 /**
  * Convert to RationalType
  *
  * @param mixed $original
  *
  * @return \Chippyash\Type\Number\Rational\RationalType|\Chippyash\Type\Number\Rational\GMPRationalType
  *
  * @throws InvalidTypeException
  */
 protected static function convertType($original)
 {
     if ($original instanceof AbstractRationalType) {
         return RationalTypeFactory::create($original->numerator()->get(), $original->denominator()->get());
     }
     if (is_numeric($original)) {
         if (is_int($original)) {
             return RationalTypeFactory::create($original, 1);
         }
         //default - convert to float
         return RationalTypeFactory::fromFloat(floatval($original));
     }
     if ($original instanceof FloatType) {
         return RationalTypeFactory::fromFloat($original());
     }
     if ($original instanceof IntType) {
         return RationalTypeFactory::create($original, 1);
     }
     if (is_string($original)) {
         try {
             return RationalTypeFactory::fromString($original);
         } catch (\InvalidArgumentException $e) {
             throw new InvalidTypeException("{$original} for Complex type construction");
         }
     }
     $type = gettype($original);
     throw new InvalidTypeException("{$type} for Complex type construction");
 }
Пример #5
0
 public function testSingleMatrixReturnsValueOfItsSingleEntry()
 {
     $this->assertEquals(2, $this->object->determinant(new NumericMatrix([2]))->get());
     $this->assertEquals('2/5', (string) $this->object->determinant(new RationalMatrix([RationalTypeFactory::fromString('2/5')])));
     $this->assertEquals('1+3i', $this->object->determinant(new ComplexMatrix([ComplexTypeFactory::fromString('1+3i')]))->get());
 }