csroot Subroutine

private pure subroutine csroot(xr, xi, yr, yi)

Compute the complex square root of a complex number.

(YR,YI) = complex sqrt(XR,XI)

REVISION HISTORY (YYMMDD)

  • 811101 DATE WRITTEN
  • 891214 Prologue converted to Version 4.0 format. (BAB)
  • 900402 Added TYPE section. (WRB)
  • Jacob Williams, 9/14/2022 : modernized this code

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: xr
real(kind=wp), intent(in) :: xi
real(kind=wp), intent(out) :: yr
real(kind=wp), intent(out) :: yi

Calls

proc~~csroot~~CallsGraph proc~csroot polyroots_module::csroot proc~pythag polyroots_module::pythag proc~csroot->proc~pythag

Called by

proc~~csroot~~CalledByGraph proc~csroot polyroots_module::csroot proc~comqr polyroots_module::comqr proc~comqr->proc~csroot proc~cpqr79 polyroots_module::cpqr79 proc~cpqr79->proc~comqr

Source Code

pure subroutine csroot(xr, xi, yr, yi)
    implicit none

    real(wp), intent(in) :: xr, xi
    real(wp), intent(out) :: yr, yi

    real(wp) :: s, tr, ti

    ! branch chosen so that yr >= 0.0 and sign(yi) == sign(xi)
    tr = xr
    ti = xi
    s = sqrt(0.5_wp*(pythag(tr, ti) + abs(tr)))
    if (tr >= 0.0_wp) yr = s
    if (ti < 0.0_wp) s = -s
    if (tr <= 0.0_wp) yi = s
    if (tr < 0.0_wp) yr = 0.5_wp*(ti/yi)
    if (tr > 0.0_wp) yi = 0.5_wp*(ti/yr)

end subroutine csroot