示例#1
0
 /**
  * Create a matrix representation of a complex number
  * For z = a+bi
  * Returns [[a, -b]
  *          [b, a]]
  * 
  * @param \Chippyash\Type\Number\Complex\ComplexType $c
  * @return \Chippyash\Math\Matrix\RationalMatrix
  */
 public static function createFromComplex(ComplexType $c)
 {
     $a = clone $c->r();
     $b = clone $c->i();
     $b2 = clone $c->i();
     $bi = new RationalType($b2->numerator()->negate(), $b->denominator());
     return self::createRational([[$a, $bi], [$b, $a]]);
 }
示例#2
0
 public function testPolarStringForNonZeroComplexReturnsNonZeroString()
 {
     $c = new ComplexType($this->createRationalType(1), $this->createRationalType(0));
     $this->assertEquals('cos 0 + i⋅sin 0', $c->polarString());
     $c = new ComplexType($this->createRationalType(5), $this->createRationalType(0));
     $this->assertEquals('5(cos 0 + i⋅sin 0)', $c->polarString());
     $c = new ComplexType($this->createRationalType(5), $this->createRationalType(2));
     $this->assertEquals('5.385165(cos 0.380506 + i⋅sin 0.380506)', $c->polarString());
 }
示例#3
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));
 }
示例#4
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;
 }