ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
multiply.php
Go to the documentation of this file.
1<?php
2
10namespace Complex;
11
18if (!function_exists(__NAMESPACE__ . '\\multiply')) {
19 function multiply(...$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
36 $real = ($result->getReal() * $complex->getReal()) -
37 ($result->getImaginary() * $complex->getImaginary());
38 $imaginary = ($result->getReal() * $complex->getImaginary()) +
39 ($result->getImaginary() * $complex->getReal());
40
41 $result = new Complex(
42 $real,
43 $imaginary,
44 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix())
45 );
46 }
47
48 return $result;
49 }
50}
$result
An exception for terminatinating execution or to throw for unit testing.
$base
Definition: index.php:4