atan.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex atan() function
  5. *
  6. * @copyright Copyright (c) 2013-2018 Mark Baker (https://github.com/MarkBaker/PHPComplex)
  7. * @license https://opensource.org/licenses/MIT MIT
  8. */
  9. namespace Complex;
  10. //include_once 'Math/Complex.php';
  11. //include_once 'Math/ComplexOp.php';
  12. /**
  13. * Returns the inverse tangent of a complex number.
  14. *
  15. * @param Complex|mixed $complex Complex number or a numeric value.
  16. * @return Complex The inverse tangent of the complex argument.
  17. * @throws Exception If argument isn't a valid real or complex number.
  18. * @throws \InvalidArgumentException If function would result in a division by zero
  19. */
  20. if (!function_exists(__NAMESPACE__ . '\\atan')) {
  21. function atan($complex): Complex
  22. {
  23. $complex = Complex::validateComplexArgument($complex);
  24. if ($complex->isReal()) {
  25. return new Complex(\atan($complex->getReal()));
  26. }
  27. $t1Value = new Complex(-1 * $complex->getImaginary(), $complex->getReal());
  28. $uValue = new Complex(1, 0);
  29. $d1Value = clone $uValue;
  30. $d1Value = subtract($d1Value, $t1Value);
  31. $d2Value = add($t1Value, $uValue);
  32. $uResult = $d1Value->divideBy($d2Value);
  33. $uResult = ln($uResult);
  34. return new Complex(
  35. (($uResult->getImaginary() == M_PI) ? -M_PI : $uResult->getImaginary()) * -0.5,
  36. $uResult->getReal() * 0.5,
  37. $complex->getSuffix()
  38. );
  39. }
  40. }