1 <?php 2 3 namespace Complex; 4 5 use InvalidArgumentException; 6 7 class Operations 8 { 9 /** 10 * Adds two or more complex numbers 11 * 12 * @param array of string|integer|float|Complex $complexValues The numbers to add 13 * @return Complex 14 */ 15 public static function add(...$complexValues): Complex 16 { 17 if (count($complexValues) < 2) { 18 throw new \Exception('This function requires at least 2 arguments'); 19 } 20 21 $base = array_shift($complexValues); 22 $result = clone Complex::validateComplexArgument($base); 23 24 foreach ($complexValues as $complex) { 25 $complex = Complex::validateComplexArgument($complex); 26 27 if ($result->isComplex() && $complex->isComplex() && 28 $result->getSuffix() !== $complex->getSuffix()) { 29 throw new Exception('Suffix Mismatch'); 30 } 31 32 $real = $result->getReal() + $complex->getReal(); 33 $imaginary = $result->getImaginary() + $complex->getImaginary(); 34 35 $result = new Complex( 36 $real, 37 $imaginary, 38 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix()) 39 ); 40 } 41 42 return $result; 43 } 44 45 /** 46 * Divides two or more complex numbers 47 * 48 * @param array of string|integer|float|Complex $complexValues The numbers to divide 49 * @return Complex 50 */ 51 public static function divideby(...$complexValues): Complex 52 { 53 if (count($complexValues) < 2) { 54 throw new \Exception('This function requires at least 2 arguments'); 55 } 56 57 $base = array_shift($complexValues); 58 $result = clone Complex::validateComplexArgument($base); 59 60 foreach ($complexValues as $complex) { 61 $complex = Complex::validateComplexArgument($complex); 62 63 if ($result->isComplex() && $complex->isComplex() && 64 $result->getSuffix() !== $complex->getSuffix()) { 65 throw new Exception('Suffix Mismatch'); 66 } 67 if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) { 68 throw new InvalidArgumentException('Division by zero'); 69 } 70 71 $delta1 = ($result->getReal() * $complex->getReal()) + 72 ($result->getImaginary() * $complex->getImaginary()); 73 $delta2 = ($result->getImaginary() * $complex->getReal()) - 74 ($result->getReal() * $complex->getImaginary()); 75 $delta3 = ($complex->getReal() * $complex->getReal()) + 76 ($complex->getImaginary() * $complex->getImaginary()); 77 78 $real = $delta1 / $delta3; 79 $imaginary = $delta2 / $delta3; 80 81 $result = new Complex( 82 $real, 83 $imaginary, 84 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix()) 85 ); 86 } 87 88 return $result; 89 } 90 91 /** 92 * Divides two or more complex numbers 93 * 94 * @param array of string|integer|float|Complex $complexValues The numbers to divide 95 * @return Complex 96 */ 97 public static function divideinto(...$complexValues): Complex 98 { 99 if (count($complexValues) < 2) { 100 throw new \Exception('This function requires at least 2 arguments'); 101 } 102 103 $base = array_shift($complexValues); 104 $result = clone Complex::validateComplexArgument($base); 105 106 foreach ($complexValues as $complex) { 107 $complex = Complex::validateComplexArgument($complex); 108 109 if ($result->isComplex() && $complex->isComplex() && 110 $result->getSuffix() !== $complex->getSuffix()) { 111 throw new Exception('Suffix Mismatch'); 112 } 113 if ($result->getReal() == 0.0 && $result->getImaginary() == 0.0) { 114 throw new InvalidArgumentException('Division by zero'); 115 } 116 117 $delta1 = ($complex->getReal() * $result->getReal()) + 118 ($complex->getImaginary() * $result->getImaginary()); 119 $delta2 = ($complex->getImaginary() * $result->getReal()) - 120 ($complex->getReal() * $result->getImaginary()); 121 $delta3 = ($result->getReal() * $result->getReal()) + 122 ($result->getImaginary() * $result->getImaginary()); 123 124 $real = $delta1 / $delta3; 125 $imaginary = $delta2 / $delta3; 126 127 $result = new Complex( 128 $real, 129 $imaginary, 130 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix()) 131 ); 132 } 133 134 return $result; 135 } 136 137 /** 138 * Multiplies two or more complex numbers 139 * 140 * @param array of string|integer|float|Complex $complexValues The numbers to multiply 141 * @return Complex 142 */ 143 public static function multiply(...$complexValues): Complex 144 { 145 if (count($complexValues) < 2) { 146 throw new \Exception('This function requires at least 2 arguments'); 147 } 148 149 $base = array_shift($complexValues); 150 $result = clone Complex::validateComplexArgument($base); 151 152 foreach ($complexValues as $complex) { 153 $complex = Complex::validateComplexArgument($complex); 154 155 if ($result->isComplex() && $complex->isComplex() && 156 $result->getSuffix() !== $complex->getSuffix()) { 157 throw new Exception('Suffix Mismatch'); 158 } 159 160 $real = ($result->getReal() * $complex->getReal()) - 161 ($result->getImaginary() * $complex->getImaginary()); 162 $imaginary = ($result->getReal() * $complex->getImaginary()) + 163 ($result->getImaginary() * $complex->getReal()); 164 165 $result = new Complex( 166 $real, 167 $imaginary, 168 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix()) 169 ); 170 } 171 172 return $result; 173 } 174 175 /** 176 * Subtracts two or more complex numbers 177 * 178 * @param array of string|integer|float|Complex $complexValues The numbers to subtract 179 * @return Complex 180 */ 181 public static function subtract(...$complexValues): Complex 182 { 183 if (count($complexValues) < 2) { 184 throw new \Exception('This function requires at least 2 arguments'); 185 } 186 187 $base = array_shift($complexValues); 188 $result = clone Complex::validateComplexArgument($base); 189 190 foreach ($complexValues as $complex) { 191 $complex = Complex::validateComplexArgument($complex); 192 193 if ($result->isComplex() && $complex->isComplex() && 194 $result->getSuffix() !== $complex->getSuffix()) { 195 throw new Exception('Suffix Mismatch'); 196 } 197 198 $real = $result->getReal() - $complex->getReal(); 199 $imaginary = $result->getImaginary() - $complex->getImaginary(); 200 201 $result = new Complex( 202 $real, 203 $imaginary, 204 ($imaginary == 0.0) ? null : max($result->getSuffix(), $complex->getSuffix()) 205 ); 206 } 207 208 return $result; 209 } 210 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body