acos.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex acos() 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. /**
  11. * Returns the inverse cosine of a complex number.
  12. *
  13. * @param Complex|mixed $complex Complex number or a numeric value.
  14. * @return Complex The inverse cosine of the complex argument.
  15. * @throws Exception If argument isn't a valid real or complex number.
  16. */
  17. if (!function_exists(__NAMESPACE__ . '\\acos')) {
  18. function acos($complex): Complex
  19. {
  20. $complex = Complex::validateComplexArgument($complex);
  21. $square = clone $complex;
  22. $square = multiply($square, $complex);
  23. $invsqrt = new Complex(1.0);
  24. $invsqrt = subtract($invsqrt, $square);
  25. $invsqrt = sqrt($invsqrt);
  26. $adjust = new Complex(
  27. $complex->getReal() - $invsqrt->getImaginary(),
  28. $complex->getImaginary() + $invsqrt->getReal()
  29. );
  30. $log = ln($adjust);
  31. return new Complex(
  32. $log->getImaginary(),
  33. -1 * $log->getReal()
  34. );
  35. }
  36. }