Created
February 7, 2020 09:43
-
-
Save ArtemioVegas/064d60a617ea5e303f40e03648d2a4be to your computer and use it in GitHub Desktop.
Complex Number Class
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 | |
declare(strict_types=1); | |
namespace common\modules\catalog\frontend\components; | |
/** | |
* Класс для работы с комплексными числами | |
* | |
* @author Котов Артём <kotov.as2@dns-shop.ru> | |
*/ | |
class ComplexNumber { | |
/** @var float Вещественная часть числа */ | |
private $real; | |
/** @var float Мнимая часть числа */ | |
private $imag; | |
public function __construct() { | |
} | |
public function __toString() { | |
// TODO: Implement __toString() method. | |
} | |
public function getReal(): float { | |
return $this->real; | |
} | |
public function getImag(): float { | |
return $this->imag; | |
} | |
/** | |
* @param ComplexNumber $right | |
* | |
* @return self | |
*/ | |
public function add(ComplexNumber $right): self { | |
$this->real += $right->getReal(); | |
$this->imag += $right->getImag(); | |
return $this; | |
} | |
/** | |
* @param ComplexNumber $right | |
* | |
* @return self | |
*/ | |
public function subtract(ComplexNumber $right) : self { | |
$this->real -= $right->getReal(); | |
$this->imag -= $right->getImag(); | |
return $this; | |
} | |
/** | |
* @param ComplexNumber $right | |
* | |
* @return $this | |
*/ | |
public function multiply(ComplexNumber $right) : self { | |
$this->real = ($this->real * $right->getReal() - $this->imag * $right->getImag()); | |
$this->imag = ($this->imag * $right->getReal() + $this->real * $right->getImag()); | |
return $this; | |
} | |
/** | |
* @param ComplexNumber $right | |
* | |
* @return self | |
*/ | |
public function divide(ComplexNumber $right) : self { | |
$this->real = ($this->real * $right->getReal() + $this->imag * $right->getImag()) / ($right->getReal() ** 2 + $right->getImag() ** 2); | |
$this->imag = ($this->imag * $right->getReal() + $this->real * $right->getImag()); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment