/**
  * 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 ');
     }
 }
Ejemplo n.º 2
0
 public function testIntPowReturnsRationalOrIntOrComplexTypes()
 {
     $base = new IntType(5);
     $exp = new IntType(3);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\IntType', $this->object->intPow($base, $exp));
     $this->assertEquals(125, $this->object->intPow($base, $exp)->get());
     $exp2 = new FloatType(3.5);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Rational\\RationalType', $this->object->intPow($base, $exp2));
     $this->assertEquals('332922571/1191100', (string) $this->object->intPow($base, $exp2));
     $exp3 = RationalTypeFactory::fromFloat(3.5);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Rational\\RationalType', $this->object->intPow($base, $exp3));
     $this->assertEquals('332922571/1191100', (string) $this->object->intPow($base, $exp3));
     $base2 = new IntType(4);
     $exp4 = ComplexTypeFactory::fromString('5+3i');
     $pow = $this->object->intPow($base2, $exp4);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', $pow);
     $this->assertEquals('-778299179/1445876', (string) $pow->r());
     $this->assertEquals('-861158767/988584', (string) $pow->i());
     $exp5 = ComplexTypeFactory::fromString('0+3i');
     $pow = $this->object->intPow($base2, $exp5);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', $pow);
     $this->assertEquals('-1722722/3277175', (string) $pow->r());
     $this->assertEquals('-20905959/24575389', (string) $pow->i());
     $exp6 = ComplexTypeFactory::fromString('5+0i');
     $pow = $this->object->intPow($base2, $exp6);
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Rational\\RationalType', $pow);
     $this->assertEquals(1024, $pow->get());
 }
Ejemplo n.º 3
0
 /**
  * Create and return a complex number matrix
  * $data elements are either:
  *  - a ComplexType
  *  - string representations of complex number
  *  - a 2 item array representing r & i e.g. [2,-4] = '2-4i'
  *
  * @param array $data
  *
  * @return \Chippyash\Math\Matrix\ComplexMatrix
  *
  * @throws \Chippyash\Math\Matrix\Exceptions\MathMatrixException
  */
 public static function createComplex(array $data)
 {
     foreach ($data as &$row) {
         foreach ($row as &$item) {
             if (!$item instanceof ComplexType) {
                 if (is_array($item) && count($item) == 2) {
                     $item = ComplexTypeFactory::create($item[0], $item[1]);
                 } elseif (is_string($item)) {
                     try {
                         $item = ComplexTypeFactory::fromString($item);
                     } catch (\InvalidArgumentException $e) {
                         throw new MathMatrixException('Invalid item type for Complex Matrix');
                     }
                 } else {
                     throw new MathMatrixException('Invalid item type for Complex Matrix');
                 }
             }
         }
     }
     return new ComplexMatrix($data);
 }
Ejemplo n.º 4
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());
 }
 public function testSqrtComplexTypeReturnsComplexType()
 {
     $res = $this->object->sqrt(ComplexTypeFactory::fromString('3+2i'));
     $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', $res);
     $this->assertEquals('32479891/17872077+17872077/32479891i', (string) $res);
 }
Ejemplo n.º 6
0
 public function testCreateFromComplexReturnsRationalMatrix()
 {
     $c = CF::fromString('2+4i');
     $mA = MatrixFactory::createFromComplex($c);
     $test = MatrixFactory::createRational([['2/1', '-4/1'], ['4/1', '2/1']]);
     $this->assertEquals($test, $mA);
 }
 public function testCanComputeRootsUsingPow()
 {
     $this->assertEquals(3, $this->object->pow(new IntType(27), RationalTypeFactory::create(1, 3))->get());
     $this->assertEquals('3/4', (string) $this->object->pow(RationalTypeFactory::create(27, 64), RationalTypeFactory::create(1, 3)));
     $this->assertEquals('32479891/17872077+17872077/32479891i', (string) $this->object->pow(ComplexTypeFactory::fromString('3+2i'), RationalTypeFactory::create(1, 2)));
 }