示例#1
0
 public function testIsRealReturnsFalseForNotRealNumber()
 {
     $c = new ComplexType($this->createRationalType(1), $this->createRationalType(1));
     $this->assertFalse($c->isReal());
 }
示例#2
0
 /**
  * Create complex power from natural base and complex exponent
  *
  * @param int|float $base base
  * @param ComplexType $exp exponent
  *
  * @return NI|ComplexType
  */
 private function complexExponent($base, ComplexType $exp)
 {
     if ($exp->isReal()) {
         return $this->rationalPow(RationalTypeFactory::fromFloat($base), $exp->r());
     }
     //do the imaginary part
     //n^bi = cos(b.lg(n)) + i.sin(b.lg(n))
     $b = $exp->i()->get();
     $n = log($base);
     $r = cos($b * $n);
     $i = sin($b * $n);
     //no real part
     if ($exp->r()->get() == 0) {
         return new ComplexType(RationalTypeFactory::fromFloat($r), RationalTypeFactory::fromFloat($i));
     }
     //real and imaginary part
     //n^a+bi = n^a(cos(b.lg(n)) + i.sin(b.lg(n)))
     $na = pow($base, $exp->r()->get());
     $rr = $na * $r;
     $ii = $na * $i;
     return new ComplexType(RationalTypeFactory::fromFloat($rr), RationalTypeFactory::fromFloat($ii));
 }
示例#3
0
 /**
  * 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;
 }