Skip to content

Instantly share code, notes, and snippets.

@mrkodssldrf
Last active December 19, 2015 08:29
Show Gist options
  • Save mrkodssldrf/5926507 to your computer and use it in GitHub Desktop.
Save mrkodssldrf/5926507 to your computer and use it in GitHub Desktop.
Complex-Number calculation
<?php
class Complex {
/**
* Real
* @var double
*/
private $real;
/**
* Imag
* @var double
*/
private $imag;
/**
* Add Const
* @var static
*/
const ADD = 0;
/**
* Subtract Const
* @var static
*/
const SUBTRACT = 1;
/**
* Multiply Const
* @var static
*/
const MULTIPLY = 2;
/**
* Divide Const
* @var static
*/
const DIVIDE = 3;
/**
* Sets a complex number
*
* @param double $real
* @param double $imag
* @return Complex
*/
public function __construct($real = null, $imag = null) {
$this->real = $real;
$this->imag = $imag;
return $this;
}
/**
* Real getter
* @return double
*/
public function getReal() {
return $this->real;
}
/**
* Imag getter
* @return double
*/
public function getImag() {
return $this->imag;
}
/**
* Add method
* @param Complex $a
* @param Complex $b
* @return Complex
*/
private static function add(Complex $a, Complex $b) {
$real = $a->getReal() + $b->getReal();
$imag = $a->getImag() + $b->getImag();
return new Complex($real, $imag);
}
/**
* Subtract Method
* @param Complex $a
* @param Complex $b
* @return Complex
*/
private static function subtract(Complex $a, Complex $b) {
$real = $a->getReal() - $b->getReal();
$imag = $a->getImag() - $b->getImag();
return new Complex($real, $imag);
}
/**
* Multiply Method
* @param Complex $a
* @param Complex $b
* @return Complex
*/
private static function multiply(Complex $a, Complex $b) {
$real = $a->getReal() * $b->getReal() - $a->getImag() * $b->getImag();
$imag = $a->getReal() * $b->getImag() - $a->getImag() * $b->getReal();
return new Complex($real, $imag);
}
/**
* Divide Method
* @param Complex $a
* @param Complex $b
* @throws Exception
* @return Complex
*/
private static function divide(Complex $a, Complex $b) {
if ($b->getReal() != 0 && $b->getImag() != 0) {
$real = ($a->getReal() * $b->getReal() + $a->getImag() * $b->getImag()) / ($b->getReal() * $b->getReal() + $b->getImag() * $b->getImag());
$imag = ($a->getImag() * $b->getReal() - $a->getReal() * $b->getImag()) / ($b->getReal() * $b->getReal() + $b->getImag() * $b->getImag());
return new Complex($real, $imag);
}
else throw new Exception("Division durch Null nicht erlaubt");
}
/**
* Calculate Method
* @param Complex $a
* @param Complex $b
* @param const $operator
* @return Complex
*/
public static function calculate(Complex $a, Complex $b, $operator) {
switch ($operator) {
case static::ADD:
return static::add($a, $b);
break;
case static::SUBTRACT:
return static::subtract($a, $b);
break;
case static::MULTIPLY:
return static::multiply($a, $b);
break;
case static::DIVIDE:
return static::divide($a, $b);
break;
default:
return static::add($a, $b);
break;
}
}
public function __toString() {
return $this->getReal() . " + " . $this->getImag() . "i";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment