/**
  * @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);
 }
예제 #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());
 }
 /**
  * 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 ');
     }
 }
예제 #4
0
 /**
  * Construct a complete Matrix with all entries set to a complex number
  * Takes a source matrix or array (which can be incomplete and converts each
  * entry to complex number type, setting a default value if entry does not exist.
  *
  * If a Matrix is supplied as $source, the data is cloned into the ComplexMatrix
  * converting to complex number values, with no further checks, although you
  * may get exceptions thrown if conversion is not possible.
  *
  * If you don't supply a default value, then 0+0i will be used
  *
  * @param Matrix|array $source Array to initialise the matrix with
  * @param ComplexType $normalizeDefault Value to set missing vertices
  *
  */
 public function __construct($source, ComplexType $normalizeDefault = null)
 {
     if (is_null($normalizeDefault)) {
         $ri = RationalTypeFactory::create(0, 1);
         $normalizeDefault = ComplexTypeFactory::create($ri, clone $ri);
     }
     parent::__construct($source, $normalizeDefault);
 }
예제 #5
0
 public function testCreateComplexIdentityReturnsComplexMatrix()
 {
     $cA = IdentityMatrix::complexIdentity(TypeFactory::createInt(2));
     $this->assertInstanceOf('Chippyash\\Math\\Matrix\\ComplexMatrix', $cA);
     $one = ComplexTypeFactory::create(1, 0);
     $zero = ComplexTypeFactory::create(0, 0);
     $this->assertEquals([[$one, $zero], [$zero, $one]], $cA->toArray());
 }
 /**
  * Create a new scalar based on type of original matrix
  *
  * @param \Chippyash\Math\Matrix\NumericMatrix $originalMatrix
  * @param scalar $scalar
  * @return Chippyash\Type\Interfaces\NumericTypeInterface
  *
  */
 protected function createCorrectScalarType(NumericMatrix $originalMatrix, $scalar)
 {
     if ($scalar instanceof NumericTypeInterface) {
         if ($originalMatrix instanceof RationalMatrix) {
             return $scalar->asRational();
         }
         if ($originalMatrix instanceof ComplexMatrix) {
             return $scalar->asComplex();
         }
         return $scalar;
     }
     if ($originalMatrix instanceof ComplexMatrix) {
         if (is_numeric($scalar)) {
             return ComplexTypeFactory::create($scalar, 0);
         }
         if (is_string($scalar)) {
             try {
                 return RationalTypeFactory::create($scalar)->asComplex();
             } catch (\Exception $e) {
                 //do nothing
             }
         }
         if (is_bool($scalar)) {
             return ComplexTypeFactory::create($scalar ? 1 : 0, 0);
         }
         return ComplexTypeFactory::create($scalar);
     }
     if ($originalMatrix instanceof RationalMatrix) {
         if (is_bool($scalar)) {
             $scalar = $scalar ? 1 : 0;
         }
         return RationalTypeFactory::create($scalar);
     }
     //handling for NumericMatrix
     if (is_int($scalar)) {
         return TypeFactory::createInt($scalar);
     } elseif (is_float($scalar)) {
         return TypeFactory::createRational($scalar);
     } elseif (is_bool($scalar)) {
         return TypeFactory::createInt($scalar ? 1 : 0);
     } elseif (is_string($scalar)) {
         try {
             return TypeFactory::createRational($scalar);
         } catch (\InvalidArgumentException $e) {
             try {
                 return ComplexTypeFactory::create($scalar);
             } catch (\InvalidArgumentException $e) {
                 //do nothing
             }
         }
     }
     throw new ComputationException('Scalar parameter is not a supported type for numeric matrices: ' . getType($scalar));
 }
예제 #7
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);
 }
예제 #8
0
 /**
  * Construct a square NumericMatrix whose entries on the diagonal ==  1, 1/1 or 1+0i
  * All other entries == 0, 0/1 or 0+0i
  *
  * @param IntType $size Number of required rows and columns
  * @param IntType $identityType Type of identity entries: default == IDM_TYPE_INT
  *
  * @throws \InvalidArgumentException
  */
 public function __construct(IntType $size, IntType $identityType = null)
 {
     if (is_null($identityType)) {
         $idt = self::IDM_TYPE_INT;
     } else {
         $idt = $identityType();
     }
     if (!in_array($idt, $this->availableTypes)) {
         throw new \InvalidArgumentException('Identity type invalid');
     }
     if ($size() < 1) {
         throw new \InvalidArgumentException('size must be >= 1');
     }
     $f = function ($row, $col) use($idt) {
         if ($idt == self::IDM_TYPE_RATIONAL) {
             return RationalTypeFactory::create($row == $col ? 1 : 0, 1);
         } elseif ($idt == self::IDM_TYPE_COMPLEX) {
             return ComplexTypeFactory::create(RationalTypeFactory::create($row == $col ? 1 : 0, 1), RationalTypeFactory::create(0, 1));
         } else {
             return TypeFactory::createInt($row == $col ? 1 : 0);
         }
     };
     parent::__construct($f, $size, $size);
 }
 /**
  * @dataProvider nonComplexNumbers
  * @param numeric $nonComplex
  */
 public function testAddNonComplexNumbersWithComplexNumberThrowsException($nonComplex)
 {
     $this->assertInstanceOf('Chippyash\\Type\\Number\\Complex\\ComplexType', $this->object->add($nonComplex, ComplexTypeFactory::create(1, 5)));
 }
예제 #10
0
 /**
  * Complex number reciprocal: 1/a+bi
  *
  * @param ComplexType $a operand
  *
  * @return ComplexType
  *
  * @throws \BadMethodCallException
  */
 public function complexReciprocal(ComplexType $a)
 {
     if ($a->isZero()) {
         throw new \BadMethodCallException('Cannot compute reciprocal of zero complex number');
     }
     //div = ar^2 + ai^2
     $div = $this->rationalAdd($this->rationalMul($a->r(), $a->r()), $this->rationalMul($a->i(), $a->i()));
     $r = $this->rationalDiv($a->r(), $div);
     $i = $this->rationalDiv($a->i(), $div);
     return ComplexTypeFactory::create($r, $i);
 }
예제 #11
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 testCreationWillUseGmpAutomaticallyIfItExists()
 {
     RequiredType::getInstance()->set(RequiredType::TYPE_DEFAULT);
     $c = ComplexTypeFactory::create('2+3i');
     $this->assertInstanceOf(self::CTYPE_NAME, $c);
 }
예제 #13
0
 public function testYouCanGetTheSignOfAComplexNumber()
 {
     $c1 = ComplexTypeFactory::create(1, 1);
     //unreal positive
     $c2 = ComplexTypeFactory::create(-1, -3);
     //unreal negative
     $c3 = ComplexTypeFactory::create(1, 0);
     //real positive
     $c4 = ComplexTypeFactory::create(-1, 0);
     //real negative
     $c5 = ComplexTypeFactory::create(0, 0);
     //zero
     $this->assertEquals(1, $c1->sign());
     $this->assertEquals(1, $c2->sign());
     //unreal complex use modulus for sign - therefore always positive
     $this->assertEquals(1, $c3->sign());
     $this->assertEquals(-1, $c4->sign());
     $this->assertEquals(0, $c5->sign());
 }
예제 #14
0
 protected function toComplex(array $dA)
 {
     foreach ($dA as &$row) {
         foreach ($row as &$entry) {
             if ($entry instanceof NumericTypeInterface) {
                 $entry = $entry->asComplex();
             } elseif (is_numeric($entry)) {
                 $entry = ComplexTypeFactory::create($entry);
             } else {
                 $entry = $entry;
             }
         }
     }
     return new Matrix($dA);
 }
예제 #15
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)));
 }
 /**
  * @expectedException BadMethodCallException
  * @expectedExceptionMessage Cannot divide complex number by zero complex number
  */
 public function testDivComplexByZeroComplexThrowsException()
 {
     $this->object->div(ComplexTypeFactory::create(1, 5), ComplexTypeFactory::create(0, 0));
 }
 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);
 }
 /**
  * @expectedException BadMethodCallException
  * @expectedExceptionMessage Cannot compute reciprocal of zero complex number
  */
 public function testReciprocalOfZeroComplexThrowsException()
 {
     $this->object->reciprocal(ComplexTypeFactory::create(0, 0));
 }
예제 #20
0
 /**
  * Create a Complex number
  * If imaginary part is null, a complex equivalent real number is created r+0i
  *
  * @param int|float|string|NumericTypeInterface $realPart
  * @param int|float|string|NumericTypeInterface|null $imaginaryPart
  *
  * @return \Chippyash\Type\Number\Complex\ComplexType
  */
 public static function createComplex($realPart, $imaginaryPart = null)
 {
     return ComplexTypeFactory::create($realPart, $imaginaryPart);
 }