tanh.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex tanh() 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 hyperbolic tangent of a complex number.
  12. *
  13. * @param Complex|mixed $complex Complex number or a numeric value.
  14. * @return Complex The hyperbolic 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 tanh($complex)
  19. {
  20. $complex = Complex::validateComplexArgument($complex);
  21. $real = $complex->getReal();
  22. $imaginary = $complex->getImaginary();
  23. $divisor = \cos($imaginary) * \cos($imaginary) + \sinh($real) * \sinh($real);
  24. if ($divisor == 0.0) {
  25. throw new \InvalidArgumentException('Division by zero');
  26. }
  27. return new Complex(
  28. \sinh($real) * \cosh($real) / $divisor,
  29. 0.5 * \sin(2 * $imaginary) / $divisor,
  30. $complex->getSuffix()
  31. );
  32. }