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()); }
/** * 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); }
/** * 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)); }
/** * 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); }
/** * Convert if possible a supplied argument to a complex * * @param int|float|string|NumericTypeInterface $numerator * * @return Chippyash\Math\Matrix\RationalNumber * * @throws Chippyash\Matrix\Exceptions\MatrixException */ protected function convertNumberToComplex($value) { if ($value instanceof NumericTypeInterface) { return $value->asComplex(); } switch (gettype($value)) { case 'integer': case 'double': return ComplexTypeFactory::create($value, 0); case 'string': try { return ComplexTypeFactory::fromString($value); } catch (\Exception $e) { throw new MatrixException('The string representation of the number is invalid for a complex number'); } case 'NULL': return ComplexTypeFactory::create(0, 0); case 'boolean': return ComplexTypeFactory::create($value ? 1 : 0, 0); default: throw new MatrixException('Complex expects int, float, string, or NumericTypeInterface value'); } }
/** * 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); }
/** * @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)); }
/** * @dataProvider nonComplexNumbers * @param numeric $nonComplex */ public function testAddNonComplexNumbersWithComplexNumberThrowsException($nonComplex) { $this->assertInstanceOf('Chippyash\\Type\\Number\\Complex\\ComplexType', $this->object->add($nonComplex, ComplexTypeFactory::create(1, 5))); }
/** * 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); }
/** * @expectedException BadMethodCallException * @expectedExceptionMessage Cannot compute reciprocal of zero complex number */ public function testReciprocalOfZeroComplexThrowsException() { $this->object->reciprocal(ComplexTypeFactory::create(0, 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()); }
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); }
/** * @dataProvider correctParamCombinations */ public function testCreateWithCorrectParamTypesReturnsComplexType($r, $i) { $c = ComplexTypeFactory::create($r, $i); $this->assertInstanceOf(self::CTYPE_NAME, $c); }
public function testCreateComplexMatrixWithComplexTypeEntriesReturnsComplexMatrix() { $data = [[CF::create(1, -3), CF::create(-4, 6), CF::create(12, 3)]]; $this->assertInstanceOf('Chippyash\\Math\\Matrix\\ComplexMatrix', MatrixFactory::createComplex($data)); }
public function testCreationWillUseGmpAutomaticallyIfItExists() { RequiredType::getInstance()->set(RequiredType::TYPE_DEFAULT); $c = ComplexTypeFactory::create('2+3i'); $this->assertInstanceOf(self::CTYPE_NAME, $c); }
/** * 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); }