File: rfac_ui.c

package info (click to toggle)
flint 2.4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,904 kB
  • ctags: 13,326
  • sloc: ansic: 208,848; cpp: 11,358; sh: 564; makefile: 250
file content (134 lines) | stat: -rw-r--r-- 3,267 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*=============================================================================

    This file is part of FLINT.

    FLINT 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.

    FLINT 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 FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2011 Fredrik Johansson

******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "ulong_extras.h"
#include "fmpz.h"

static __inline__ ulong rfac(ulong x, ulong b)
{
    ulong i, c = x;

    for (i = 1; i < b; i++)
        c *= x + i;

    return c;
}

/* Assumes x positive, b > a. b must also be small enough to
avoid integer overflow, which is no problem if the result
is to fit in memory. */
void
_fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong a, ulong b)
{
    if (b - a == 1)
    {
        fmpz_add_ui(r, x, a);
    }
    else if ((*x <= COEFF_MAX) && (b - a < 60))
    {
        ulong step, bits, factors_per_limb;
        ulong y = *x;

        /* Bound size of largest factor */
        bits = FLINT_BIT_COUNT(y + a + b - 1);

        /* The result fits in a single limb */
        if ((b - a) * bits < FLINT_BITS)
            step = factors_per_limb = b - a;
        else
        {
            factors_per_limb = FLINT_BITS / bits;
            step = FLINT_MIN(b - a, factors_per_limb);
        }

        fmpz_set_ui(r, rfac(y + a, step));
        a += step;

        while (a < b)
        {
            step = FLINT_MIN(b - a, factors_per_limb);
            fmpz_mul_ui(r, r, rfac(y + a, step));
            a += step;
        }
    }
    else
    {
        fmpz_t t, u;
        ulong m = (a + b) / 2;

        fmpz_init(t);
        fmpz_init(u);

        _fmpz_rfac_ui(t, x, a, m);
        _fmpz_rfac_ui(u, x, m, b);
        fmpz_mul(r, t, u);

        fmpz_clear(t);
        fmpz_clear(u);
    }
}

void
fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong n)
{
    if (n == 0)
    {
        fmpz_one(r);
    }
    else if (n == 1)
    {
        fmpz_set(r, x);
    }
    else if (fmpz_is_zero(x))
    {
        fmpz_zero(r);
    }
    else if (fmpz_sgn(x) < 0)
    {
        fmpz_t t;
        fmpz_init(t);
        fmpz_add_ui(t, x, n - 1);
        if (fmpz_sgn(t) >= 0)
        {
            fmpz_zero(r);
        }
        else
        {
            fmpz_neg(t, t);
            fmpz_rfac_ui(r, t, n);
            if (n % 2 == 1)
                fmpz_neg(r, r);
        }
        fmpz_clear(t);
    }
    else
    {
        _fmpz_rfac_ui(r, x, 0, n);
    }
}