public function testAbsReturnsAbsoluteValue() { $c1 = new ComplexType($this->createRationalType(1), $this->createRationalType(2)); $c2 = new ComplexType($this->createRationalType(-1), $this->createRationalType(2)); $c3 = new ComplexType($this->createRationalType(1), $this->createRationalType(-2)); $c4 = new ComplexType($this->createRationalType(-1), $this->createRationalType(-2)); $this->assertEquals($c1->modulus(), $c1->abs()); $this->assertEquals($c1->modulus(), $c2->abs()); $this->assertEquals($c1->modulus(), $c3->abs()); $this->assertEquals($c1->modulus(), $c4->abs()); }
/** * @param ComplexType $a first operand * @param ComplexType $exp second operand * * @return array [real, imaginary] */ protected function getPowExponentPartsFromPolar(ComplexType $a, ComplexType $exp) { $eReal = $exp->r()->get(); $eImaginary = $exp->i()->get(); $logr = log($a->modulus()->get()); $theta = $a->theta()->get(); $rho = exp($logr * $eReal - $eImaginary * $theta); $beta = $theta * $eReal + $eImaginary * $logr; return [$rho * cos($beta), $rho * sin($beta)]; }
/** * Compare complex numbers. * If both operands are real then compare the real parts * else compare the modulii of the two numbers * * @param ComplexType $a * @param ComplexType $b * * @return boolean */ protected function complexCompare(ComplexType $a, ComplexType $b) { if ($a->isReal() && $b->isReal()) { return $this->rationalCompare($a->r(), $b->r()); } //hack to get around native php integer limitations //what we should be doing here is to return $this->rationalCompare($a->modulus(), $b->modulus()) //but this blows up because of the big rationals it produces $am = $a->modulus()->asFloatType()->get(); $bm = $b->modulus()->asFloatType()->get(); if ($am == $bm) { return 0; } if ($am < $bm) { return -1; } return 1; }