theta.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. function theta($complex)
  19. {
  20. $complex = Complex::validateComplexArgument($complex);
  21. if ($complex->getReal() == 0.0) {
  22. if ($complex->isReal()) {
  23. return 0.0;
  24. } elseif ($complex->getImaginary() < 0.0) {
  25. return M_PI / -2;
  26. }
  27. return M_PI / 2;
  28. } elseif ($complex->getReal() > 0.0) {
  29. return \atan($complex->getImaginary() / $complex->getReal());
  30. } elseif ($complex->getImaginary() < 0.0) {
  31. return -(M_PI - \atan(\abs($complex->getImaginary()) / \abs($complex->getReal())));
  32. }
  33. return M_PI - \atan($complex->getImaginary() / \abs($complex->getReal()));
  34. }