Last active
August 11, 2021 09:17
-
-
Save JordanRL/98cceb392ba5ba943462fe574f18de51 to your computer and use it in GitHub Desktop.
Complex number multiply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function multiply($num) | |
{ | |
[ | |
$thatRealPart, | |
$thatImaginaryPart, | |
$thisRealPart, | |
$thisImaginaryPart, | |
$num | |
] = $this->translateToParts($this, $num, 1); | |
if ($num->isComplex()) { | |
$foiled = PolynomialFunction::createFromFoil([ | |
$thisRealPart, | |
$thisImaginaryPart | |
], [ | |
$thatRealPart, | |
$thatImaginaryPart | |
]); | |
$parts = $foiled->describeShape(); | |
$newRealPart = new ImmutableDecimal($parts[0]); | |
$newImaginaryPart = new ImmutableDecimal($parts[1]); | |
/** @var ImmutableDecimal $newRealPart */ | |
$newRealPart = $newRealPart->add($parts[2]); | |
} else { | |
$value1 = $thisRealPart->multiply($num); | |
$value2 = $thisImaginaryPart->multiply($num); | |
$newRealPart = $value1->isReal() ? $value1 : $value2; | |
$newImaginaryPart = $value1->isImaginary() ? $value1 : $value2; | |
} | |
if (!$newRealPart->isEqual(0) && !$newImaginaryPart->isEqual(0)) { | |
return $this->setValue($newRealPart, $newImaginaryPart); | |
} | |
$value = $newRealPart->isEqual(0) ? $newImaginaryPart : $newRealPart; | |
return new ImmutableDecimal($value->getValue()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment