arb/acb/sqrt.c

136 lines
3.1 KiB
C
Raw Normal View History

2014-05-15 16:36:30 +02:00
/*=============================================================================
This file is part of ARB.
ARB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ARB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ARB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2013 Fredrik Johansson
******************************************************************************/
#include "acb.h"
void
acb_sqrt(acb_t y, const acb_t x, long prec)
{
arb_t r, t, u;
long wp;
#define a acb_realref(x)
#define b acb_imagref(x)
#define c acb_realref(y)
#define d acb_imagref(y)
if (arb_is_zero(b))
{
if (arb_is_nonnegative(a))
{
arb_sqrt(c, a, prec);
arb_zero(d);
return;
}
else if (arb_is_nonpositive(a))
{
arb_neg(d, a);
arb_sqrt(d, d, prec);
arb_zero(c);
return;
}
}
if (arb_is_zero(a))
{
if (arb_is_nonnegative(b))
{
arb_mul_2exp_si(c, b, -1);
arb_sqrt(c, c, prec);
arb_set(d, c);
return;
}
else if (arb_is_nonpositive(b))
{
arb_mul_2exp_si(c, b, -1);
arb_neg(c, c);
arb_sqrt(c, c, prec);
arb_neg(d, c);
return;
}
}
wp = prec + 4;
arb_init(r);
arb_init(t);
arb_init(u);
acb_abs(r, x, wp);
arb_add(t, r, a, wp);
2015-08-26 13:43:50 +02:00
if (arb_rel_accuracy_bits(t) > 8)
{
/* sqrt(a+bi) = sqrt((r+a)/2) + b/sqrt(2*(r+a))*i, r = |a+bi| */
arb_mul_2exp_si(u, t, 1);
arb_sqrt(u, u, wp);
arb_div(d, b, u, prec);
arb_set_round(c, u, prec);
arb_mul_2exp_si(c, c, -1);
}
else
{
/*
sqrt(a+bi) = sqrt((r+a)/2) + (b/|b|)*sqrt((r-a)/2)*i
(sign)
*/
arb_mul_2exp_si(t, t, -1);
arb_sub(u, r, a, wp);
arb_mul_2exp_si(u, u, -1);
arb_sqrtpos(c, t, prec);
2014-05-15 16:36:30 +02:00
2015-08-26 13:43:50 +02:00
if (arb_is_nonnegative(b))
{
arb_sqrtpos(d, u, prec);
}
else if (arb_is_nonpositive(b))
{
arb_sqrtpos(d, u, prec);
arb_neg(d, d);
}
else
{
arb_sqrtpos(t, u, wp);
arb_neg(u, t);
arb_union(d, t, u, prec);
}
}
2014-05-15 16:36:30 +02:00
arb_clear(r);
arb_clear(t);
arb_clear(u);
#undef a
#undef b
#undef c
#undef d
}