ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
divideby.php
Go to the documentation of this file.
1 <?php
2 
10 namespace Complex;
11 
18 if (!function_exists(__NAMESPACE__ . '\\divideby')) {
19  function divideby(...$complexValues): Complex
20  {
21  if (count($complexValues) < 2) {
22  throw new \Exception('This function requires at least 2 arguments');
23  }
24 
25  $base = array_shift($complexValues);
26  $result = clone Complex::validateComplexArgument($base);
27 
28  foreach ($complexValues as $complex) {
29  $complex = Complex::validateComplexArgument($complex);
30 
31  if ($result->isComplex() && $complex->isComplex() &&
32  $result->getSuffix() !== $complex->getSuffix()) {
33  throw new Exception('Suffix Mismatch');
34  }
35  if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
36  throw new \InvalidArgumentException('Division by zero');
37  }
38 
39  $delta1 = ($result->getReal() * $complex->getReal()) +
40  ($result->getImaginary() * $complex->getImaginary());
41  $delta2 = ($result->getImaginary() * $complex->getReal()) -
42  ($result->getReal() * $complex->getImaginary());
43  $delta3 = ($complex->getReal() * $complex->getReal()) +
44  ($complex->getImaginary() * $complex->getImaginary());
45 
46  $real = $delta1 / $delta3;
47  $imaginary = $delta2 / $delta3;
48 
49  $result = new Complex(
50  $real,
51  $imaginary,
52  ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix())
53  );
54  }
55 
56  return $result;
57  }
58 }
$result
$base
Definition: index.php:4