division methods (work in progress)

This commit is contained in:
Fredrik Johansson 2014-05-10 03:27:12 +02:00
parent 2e256cf4ae
commit 1054e41f74
8 changed files with 845 additions and 1 deletions

8
arb.h
View file

@ -745,6 +745,14 @@ void arb_mul_si(arb_t z, const arb_t x, long y, long prec);
void arb_mul_ui(arb_t z, const arb_t x, ulong y, long prec);
void arb_mul_fmpz(arb_t z, const arb_t x, const fmpz_t y, long prec);
void arb_div(arb_t z, const arb_t x, const arb_t y, long prec);
void arb_div_arf(arb_t z, const arb_t x, const arf_t y, long prec);
void arb_div_si(arb_t z, const arb_t x, long y, long prec);
void arb_div_ui(arb_t z, const arb_t x, ulong y, long prec);
void arb_div_fmpz(arb_t z, const arb_t x, const fmpz_t y, long prec);
void arb_fmpz_div_fmpz(arb_t z, const fmpz_t x, const fmpz_t y, long prec);
void arb_ui_div(arb_t z, ulong x, const arb_t y, long prec);
#ifdef __cplusplus
}
#endif

186
arb/div.c Normal file
View file

@ -0,0 +1,186 @@
/*=============================================================================
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) 2014 Fredrik Johansson
******************************************************************************/
#include "arb.h"
void
arb_div_arf(arb_t z, const arb_t x, const arf_t y, long prec)
{
mag_t zr, ym;
int inexact;
if (arb_is_exact(x))
{
inexact = arf_div(arb_midref(z), arb_midref(x), y, prec, ARB_RND);
if (inexact)
arf_mag_set_ulp(arb_radref(z), arb_midref(z), prec);
else
mag_zero(arb_radref(z));
}
else
{
mag_init(ym);
mag_init(zr);
arf_get_mag_lower(ym, y);
mag_div(zr, arb_radref(x), ym);
inexact = arf_div(arb_midref(z), arb_midref(x), y, prec, ARB_RND);
if (inexact)
arf_mag_add_ulp(arb_radref(z), zr, arb_midref(z), prec);
else
mag_swap(arb_radref(z), zr);
mag_clear(ym);
mag_clear(zr);
}
}
void
mag_mul_lower(mag_t z, const mag_t x, const mag_t y)
{
if (mag_is_special(x) || mag_is_special(y))
{
if (mag_is_zero(x) || mag_is_zero(y))
mag_zero(z);
else
mag_inf(z);
}
else
{
long fix;
MAG_MAN(z) = MAG_FIXMUL(MAG_MAN(x), MAG_MAN(y));
fix = !(MAG_MAN(z) >> (MAG_BITS - 1));
MAG_MAN(z) <<= fix;
_fmpz_add2_fast(MAG_EXPREF(z), MAG_EXPREF(x), MAG_EXPREF(y), -fix);
}
}
void
arb_div(arb_t z, const arb_t x, const arb_t y, long prec)
{
mag_t zr, xm, ym, yl, yw;
int inexact;
if (arb_is_exact(y))
{
arb_div_arf(z, x, arb_midref(y), prec);
}
else
{
mag_init_set_arf(xm, arb_midref(x));
mag_init_set_arf(ym, arb_midref(y));
mag_init(zr);
mag_init(yl);
mag_init(yw);
/* (|x|*yrad + |y|*xrad)/(y*(|y|-yrad)) */
mag_mul(zr, xm, arb_radref(y));
mag_addmul(zr, ym, arb_radref(x));
arb_get_mag_lower(yw, y);
arf_get_mag_lower(yl, arb_midref(y));
mag_mul_lower(yl, yl, yw);
mag_div(zr, zr, yl);
inexact = arf_div(arb_midref(z), arb_midref(x), arb_midref(y), prec, ARB_RND);
if (inexact)
arf_mag_add_ulp(arb_radref(z), zr, arb_midref(z), prec);
else
mag_swap(arb_radref(z), zr);
mag_clear(xm);
mag_clear(ym);
mag_clear(zr);
mag_clear(yl);
mag_clear(yw);
}
}
void
arb_div_si(arb_t z, const arb_t x, long y, long prec)
{
arf_t t;
arf_init_set_si(t, y); /* no need to free */
arb_div_arf(z, x, t, prec);
}
void
arb_div_ui(arb_t z, const arb_t x, ulong y, long prec)
{
arf_t t;
arf_init_set_ui(t, y); /* no need to free */
arb_div_arf(z, x, t, prec);
}
void
arb_div_fmpz(arb_t z, const arb_t x, const fmpz_t y, long prec)
{
arf_t t;
if (!COEFF_IS_MPZ(*y))
{
arf_init_set_si(t, *y); /* no need to free */
arb_div_arf(z, x, t, prec);
}
else
{
arf_init(t);
arf_set_fmpz(t, y);
arb_div_arf(z, x, t, prec);
arf_clear(t);
}
}
void
arb_fmpz_div_fmpz(arb_t z, const fmpz_t x, const fmpz_t y, long prec)
{
int inexact;
inexact = arf_fmpz_div_fmpz(arb_midref(z), x, y, prec, ARB_RND);
if (inexact)
arf_mag_set_ulp(arb_radref(z), arb_midref(z), prec);
else
mag_zero(arb_radref(z));
}
void
arb_ui_div(arb_t z, ulong x, const arb_t y, long prec)
{
arb_t t;
arb_init(t);
arb_set_ui(t, x);
arb_div(z, t, y, prec);
arb_clear(t);
}

View file

@ -82,7 +82,7 @@ _arb_get_mag_lower(mag_t z, const arf_t mid, const mag_t rad)
if (arf_sgn(t) <= 0)
mag_zero(z);
else
abort(); /* arf_get_mag_lower(z, t); */
arf_get_mag_lower(z, t);
arf_clear(t);
}
@ -91,6 +91,7 @@ _arb_get_mag_lower(mag_t z, const arf_t mid, const mag_t rad)
mp_limb_t m;
ARF_GET_TOP_LIMB(m, mid);
m = m >> (FLINT_BITS - MAG_BITS);
if (shift <= MAG_BITS)
m = m - (MAG_MAN(rad) >> shift) - 1;
@ -99,6 +100,7 @@ _arb_get_mag_lower(mag_t z, const arf_t mid, const mag_t rad)
fix = !(m >> (MAG_BITS - 1));
m <<= fix;
MAG_MAN(z) = m;
_fmpz_add_fast(MAG_EXPREF(z), MAG_EXPREF(mid), -fix);
}
}

174
arb/test/t-div.c Normal file
View file

@ -0,0 +1,174 @@
/*=============================================================================
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) 2012 Fredrik Johansson
******************************************************************************/
#include "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("div....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 100000; iter++)
{
arb_t a, b, c;
fmpq_t x, y, z;
arb_init(a);
arb_init(b);
arb_init(c);
fmpq_init(x);
fmpq_init(y);
fmpq_init(z);
do {
arb_randtest(a, state, 1 + n_randint(state, 200), 10);
arb_randtest(b, state, 1 + n_randint(state, 200), 10);
arb_randtest(c, state, 1 + n_randint(state, 200), 10);
arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200));
arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200));
} while (fmpq_is_zero(y));
arb_div(c, a, b, 2 + n_randint(state, 200));
fmpq_div(z, x, y);
if (!arb_contains_fmpq(c, z))
{
printf("FAIL: containment\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("x = "); fmpq_print(x); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("y = "); fmpq_print(y); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("z = "); fmpq_print(z); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
fmpq_clear(x);
fmpq_clear(y);
fmpq_clear(z);
}
/* aliasing of c and a */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b;
fmpq_t x, y, z;
arb_init(a);
arb_init(b);
fmpq_init(x);
fmpq_init(y);
fmpq_init(z);
do {
arb_randtest(a, state, 1 + n_randint(state, 200), 10);
arb_randtest(b, state, 1 + n_randint(state, 200), 10);
arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200));
arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200));
} while (fmpq_is_zero(y));
arb_div(a, a, b, 2 + n_randint(state, 200));
fmpq_div(z, x, y);
if (!arb_contains_fmpq(a, z))
{
printf("FAIL: aliasing (c, a)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("x = "); fmpq_print(x); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("y = "); fmpq_print(y); printf("\n\n");
printf("z = "); fmpq_print(z); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
fmpq_clear(x);
fmpq_clear(y);
fmpq_clear(z);
}
/* aliasing of c and b */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b;
fmpq_t x, y, z;
arb_init(a);
arb_init(b);
fmpq_init(x);
fmpq_init(y);
fmpq_init(z);
do {
arb_randtest(a, state, 1 + n_randint(state, 200), 10);
arb_randtest(b, state, 1 + n_randint(state, 200), 10);
arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200));
arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200));
} while (fmpq_is_zero(y));
arb_div(b, a, b, 2 + n_randint(state, 200));
fmpq_div(z, x, y);
if (!arb_contains_fmpq(b, z))
{
printf("FAIL: aliasing (c, b)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("x = "); fmpq_print(x); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("y = "); fmpq_print(y); printf("\n\n");
printf("z = "); fmpq_print(z); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
fmpq_clear(x);
fmpq_clear(y);
fmpq_clear(z);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

120
arb/test/t-div_arf.c Normal file
View file

@ -0,0 +1,120 @@
/*=============================================================================
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) 2012-2014 Fredrik Johansson
******************************************************************************/
#include "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("div_arf....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d;
arf_t x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
arf_init(x);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
arf_randtest_special(x, state, 1 + n_randint(state, 2000), 100);
prec = 2 + n_randint(state, 2000);
arb_set_arf(b, x);
arb_div_arf(c, a, x, prec);
arb_div(d, a, b, prec);
if (!arb_equal(c, d))
{
printf("FAIL\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("d = "); arb_print(d); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
arf_clear(x);
}
/* aliasing */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c;
arf_t x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arf_init(x);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
arf_randtest_special(x, state, 1 + n_randint(state, 2000), 100);
prec = 2 + n_randint(state, 2000);
arb_set_arf(b, x);
arb_div_arf(c, a, x, prec);
arb_div_arf(a, a, x, prec);
if (!arb_equal(a, c))
{
printf("FAIL (aliasing)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arf_clear(x);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

120
arb/test/t-div_fmpz.c Normal file
View file

@ -0,0 +1,120 @@
/*=============================================================================
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) 2012-2014 Fredrik Johansson
******************************************************************************/
#include "arb.h"
int main()
{
long iter;
flint_rand_t state;
printf("div_fmpz....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d;
fmpz_t x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
fmpz_init(x);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
fmpz_randtest(x, state, 1 + n_randint(state, 2000));
prec = 2 + n_randint(state, 2000);
arb_set_fmpz(b, x);
arb_div_fmpz(c, a, x, prec);
arb_div(d, a, b, prec);
if (!arb_equal(c, d))
{
printf("FAIL\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("d = "); arb_print(d); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
fmpz_clear(x);
}
/* aliasing */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c;
fmpz_t x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
fmpz_init(x);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
fmpz_randtest(x, state, 1 + n_randint(state, 2000));
prec = 2 + n_randint(state, 2000);
arb_set_fmpz(b, x);
arb_div_fmpz(c, a, x, prec);
arb_div_fmpz(a, a, x, prec);
if (!arb_equal(a, c))
{
printf("FAIL (aliasing)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
fmpz_clear(x);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

117
arb/test/t-div_si.c Normal file
View file

@ -0,0 +1,117 @@
/*=============================================================================
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) 2012-2014 Fredrik Johansson
******************************************************************************/
#include "arb.h"
#include "long_extras.h"
int main()
{
long iter;
flint_rand_t state;
printf("div_si....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d;
long x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
x = z_randtest(state);
prec = 2 + n_randint(state, 2000);
arb_set_si(b, x);
arb_div_si(c, a, x, prec);
arb_div(d, a, b, prec);
if (!arb_equal(c, d))
{
printf("FAIL\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("d = "); arb_print(d); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
}
/* aliasing */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c;
long x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
x = z_randtest(state);
prec = 2 + n_randint(state, 2000);
arb_set_si(b, x);
arb_div_si(c, a, x, prec);
arb_div_si(a, a, x, prec);
if (!arb_equal(a, c))
{
printf("FAIL (aliasing)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}

117
arb/test/t-div_ui.c Normal file
View file

@ -0,0 +1,117 @@
/*=============================================================================
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) 2012-2014 Fredrik Johansson
******************************************************************************/
#include "arb.h"
#include "ulong_extras.h"
int main()
{
long iter;
flint_rand_t state;
printf("div_ui....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c, d;
ulong x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_init(d);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
x = n_randtest(state);
prec = 2 + n_randint(state, 2000);
arb_set_ui(b, x);
arb_div_ui(c, a, x, prec);
arb_div(d, a, b, prec);
if (!arb_equal(c, d))
{
printf("FAIL\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
printf("d = "); arb_print(d); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
arb_clear(d);
}
/* aliasing */
for (iter = 0; iter < 10000; iter++)
{
arb_t a, b, c;
ulong x;
long prec;
arb_init(a);
arb_init(b);
arb_init(c);
arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100);
arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100);
x = n_randtest(state);
prec = 2 + n_randint(state, 2000);
arb_set_ui(b, x);
arb_div_ui(c, a, x, prec);
arb_div_ui(a, a, x, prec);
if (!arb_equal(a, c))
{
printf("FAIL (aliasing)\n\n");
printf("a = "); arb_print(a); printf("\n\n");
printf("b = "); arb_print(b); printf("\n\n");
printf("c = "); arb_print(c); printf("\n\n");
abort();
}
arb_clear(a);
arb_clear(b);
arb_clear(c);
}
flint_randclear(state);
flint_cleanup();
printf("PASS\n");
return EXIT_SUCCESS;
}