tan.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex tan() 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 tangent of a complex number.
  12. *
  13. * @param Complex|mixed $complex Complex number or a numeric value.
  14. * @return Complex The tangent of the complex argument.
  15. * @throws Exception If argument isn't a valid real or complex number.
  16. * @throws \InvalidArgumentException If function would result in a division by zero
  17. */
  18. function tan($complex)
  19. {
  20. $complex = Complex::validateComplexArgument($complex);
  21. if ($complex->isReal()) {
  22. return new Complex(\tan($complex->getReal()));
  23. }
  24. $real = $complex->getReal();
  25. $imaginary = $complex->getImaginary();
  26. $divisor = 1 + \pow(\tan($real), 2) * \pow(\tanh($imaginary), 2);
  27. if ($divisor == 0.0) {
  28. throw new \InvalidArgumentException('Division by zero');
  29. }
  30. return new Complex(
  31. \pow(sech($imaginary)->getReal(), 2) * \tan($real) / $divisor,
  32. \pow(sec($real)->getReal(), 2) * \tanh($imaginary) / $divisor,
  33. $complex->getSuffix()
  34. );
  35. }