2012-09-02 06:09:40 +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
|
|
|
|
|
|
|
|
=============================================================================*/
|
|
|
|
/******************************************************************************
|
|
|
|
|
2013-03-26 11:41:12 +01:00
|
|
|
Copyright (C) 2012, 2013 Fredrik Johansson
|
2012-09-02 06:09:40 +02:00
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "fmprb.h"
|
|
|
|
|
2013-09-19 00:30:04 +01:00
|
|
|
#define BINEXP_LIMIT 64
|
|
|
|
|
2012-09-02 06:09:40 +02:00
|
|
|
void
|
2013-09-19 00:30:04 +01:00
|
|
|
_fmprb_pow_exp(fmprb_t z, const fmprb_t x, int negx, const fmprb_t y, long prec)
|
2012-09-02 06:09:40 +02:00
|
|
|
{
|
2012-09-03 16:26:41 +02:00
|
|
|
fmprb_t t;
|
|
|
|
fmprb_init(t);
|
2013-09-19 00:30:04 +01:00
|
|
|
|
|
|
|
if (negx)
|
|
|
|
{
|
|
|
|
fmprb_neg(t, x);
|
|
|
|
fmprb_log(t, t, prec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fmprb_log(t, x, prec);
|
|
|
|
|
2013-03-26 11:41:12 +01:00
|
|
|
fmprb_mul(t, t, y, prec);
|
|
|
|
fmprb_exp(z, t, prec);
|
2012-09-03 16:26:41 +02:00
|
|
|
fmprb_clear(t);
|
|
|
|
}
|
2012-09-02 06:09:40 +02:00
|
|
|
|
2012-09-03 16:26:41 +02:00
|
|
|
void
|
2013-03-26 11:41:12 +01:00
|
|
|
fmprb_pow(fmprb_t z, const fmprb_t x, const fmprb_t y, long prec)
|
2012-09-03 16:26:41 +02:00
|
|
|
{
|
2013-09-19 00:30:04 +01:00
|
|
|
const fmpr_struct * ymid = fmprb_midref(y);
|
|
|
|
const fmpr_struct * yrad = fmprb_radref(y);
|
|
|
|
|
|
|
|
if (fmprb_is_zero(y))
|
2012-09-17 00:06:03 +02:00
|
|
|
{
|
2013-09-19 00:30:04 +01:00
|
|
|
fmprb_one(z);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-26 11:41:12 +01:00
|
|
|
|
2013-09-19 00:30:04 +01:00
|
|
|
if (fmpr_is_zero(yrad) && !fmpr_is_special(fmprb_midref(x)))
|
|
|
|
{
|
|
|
|
/* small half-integer or integer */
|
|
|
|
if (fmpr_cmpabs_2exp_si(ymid, BINEXP_LIMIT) < 0 &&
|
|
|
|
fmpr_is_int_2exp_si(ymid, -1))
|
2013-03-26 11:41:12 +01:00
|
|
|
{
|
2013-09-19 00:30:04 +01:00
|
|
|
fmpz_t e;
|
|
|
|
fmpz_init(e);
|
2013-03-26 11:41:12 +01:00
|
|
|
|
2013-09-19 00:30:04 +01:00
|
|
|
if (fmpr_is_int(ymid))
|
2013-03-26 11:41:12 +01:00
|
|
|
{
|
2013-09-19 00:30:04 +01:00
|
|
|
fmpr_get_fmpz_fixed_si(e, ymid, 0);
|
|
|
|
fmprb_pow_fmpz_binexp(z, x, e, prec);
|
2013-03-26 11:41:12 +01:00
|
|
|
}
|
2013-09-19 00:30:04 +01:00
|
|
|
else
|
2013-03-26 11:41:12 +01:00
|
|
|
{
|
2013-09-19 00:30:04 +01:00
|
|
|
fmpr_get_fmpz_fixed_si(e, ymid, -1);
|
|
|
|
fmprb_sqrt(z, x, prec + fmpz_bits(e));
|
|
|
|
fmprb_pow_fmpz_binexp(z, z, e, prec);
|
2013-03-26 11:41:12 +01:00
|
|
|
}
|
2013-09-19 00:30:04 +01:00
|
|
|
|
|
|
|
fmpz_clear(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (fmpr_is_int(ymid) && fmpr_sgn(fmprb_midref(x)) < 0)
|
|
|
|
{
|
|
|
|
/* use (-x)^n = (-1)^n * x^n to avoid NaNs
|
|
|
|
at least at high enough precision */
|
|
|
|
int odd = !fmpr_is_int_2exp_si(ymid, 1);
|
|
|
|
_fmprb_pow_exp(z, x, 1, y, prec);
|
|
|
|
if (odd)
|
|
|
|
fmprb_neg(z, z);
|
|
|
|
return;
|
2013-03-26 11:41:12 +01:00
|
|
|
}
|
2012-09-17 00:06:03 +02:00
|
|
|
}
|
2012-09-12 12:16:29 +02:00
|
|
|
|
2013-09-19 00:30:04 +01:00
|
|
|
_fmprb_pow_exp(z, x, 0, y, prec);
|
2012-09-12 12:16:29 +02:00
|
|
|
}
|
2013-03-26 11:41:12 +01:00
|
|
|
|