theta.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex theta() 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 theta of a complex number.
  12. * This is the angle in radians from the real axis to the representation of the number in polar coordinates.
  13. *
  14. * @param Complex|mixed $complex Complex number or a numeric value.
  15. * @return float The theta value of the complex argument.
  16. * @throws Exception If argument isn't a valid real or complex number.
  17. */
  18. if (!function_exists(__NAMESPACE__ . '\\theta')) {
  19. function theta($complex): float
  20. {
  21. $complex = Complex::validateComplexArgument($complex);
  22. if ($complex->getReal() == 0.0) {
  23. if ($complex->isReal()) {
  24. return 0.0;
  25. } elseif ($complex->getImaginary() < 0.0) {
  26. return M_PI / -2;
  27. }
  28. return M_PI / 2;
  29. } elseif ($complex->getReal() > 0.0) {
  30. return \atan($complex->getImaginary() / $complex->getReal());
  31. } elseif ($complex->getImaginary() < 0.0) {
  32. return -(M_PI - \atan(\abs($complex->getImaginary()) / \abs($complex->getReal())));
  33. }
  34. return M_PI - \atan($complex->getImaginary() / \abs($complex->getReal()));
  35. }
  36. }