sqrt.php 922 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. *
  4. * Function code for the complex sqrt() 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 square root of a complex number.
  12. *
  13. * @param Complex|mixed $complex Complex number or a numeric value.
  14. * @return Complex The Square root of the complex argument.
  15. * @throws Exception If argument isn't a valid real or complex number.
  16. */
  17. if (!function_exists(__NAMESPACE__ . '\\sqrt')) {
  18. function sqrt($complex): Complex
  19. {
  20. $complex = Complex::validateComplexArgument($complex);
  21. $theta = theta($complex);
  22. $delta1 = \cos($theta / 2);
  23. $delta2 = \sin($theta / 2);
  24. $rho = \sqrt(rho($complex));
  25. return new Complex($delta1 * $rho, $delta2 * $rho, $complex->getSuffix());
  26. }
  27. }