/** * 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)); }
/** * 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]]); }
/** * |c1 * c2| = |c1| * |c2| */ public function testCommutativeMultiplicationAttributeForModulus() { $c1 = new ComplexType($this->createRationalType(1), $this->createRationalType(2)); $c2 = new ComplexType($this->createRationalType(3), $this->createRationalType(4)); $c1R = $c1->r()->get(); $c1I = $c1->i()->get(); $c2R = $c2->r()->get(); $c2I = $c2->i()->get(); $nR = $c1R * $c2R - $c1I * $c2I; $nI = $c1I * $c2R + $c1R * $c2I; $c1mulc2 = new ComplexType(RationalTypeFactory::fromFloat($nR), RationalTypeFactory::fromFloat($nI)); $mod1 = $c1->modulus(); $mod2 = $c2->modulus(); $modc1mulc2 = $c1mulc2->modulus(); $this->assertEquals($modc1mulc2(), $mod1() * $mod2()); }