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) 2010 Fredrik Johansson
******************************************************************************/
#include "arith.h"
static __inline__ void
_fmpz_addmul_alt(fmpz_t s, fmpz_t t, fmpz_t u, int parity)
{
if (parity % 2)
fmpz_submul(s, t, u);
else
fmpz_addmul(s, t, u);
}
static void
_fmpz_stirling2_powsum(fmpz_t s, slong n, slong k)
{
fmpz_t t, u;
fmpz * bc;
slong j, m, max_bc;
fmpz_init(t);
fmpz_init(u);
max_bc = (k+1) / 2;
bc = _fmpz_vec_init(max_bc + 1);
fmpz_one(bc);
for (j = 1; j <= max_bc; j++)
{
fmpz_set(bc+j, bc+j-1);
fmpz_mul_ui(bc+j, bc+j, k+1-j);
fmpz_divexact_ui(bc+j, bc+j, j);
}
fmpz_zero(s);
for (j = 1; j <= k; j += 2)
{
fmpz_set_ui(u, j);
fmpz_pow_ui(u, u, n);
m = j;
/* Process each m = 2^p * j */
while (1)
{
if (m > max_bc)
_fmpz_addmul_alt(s, bc+k-m, u, k + m);
else
_fmpz_addmul_alt(s, bc+m, u, k + m);
m *= 2;
if (m > k)
break;
fmpz_mul_2exp(u, u, n);
}
}
_fmpz_vec_clear(bc, max_bc + 1);
fmpz_fac_ui(t, k);
fmpz_divexact(s, s, t);
fmpz_clear(t);
fmpz_clear(u);
}
void
arith_stirling_number_2(fmpz_t s, slong n, slong k)
{
if (n < 0 || k < 0 || k > n)
{
fmpz_zero(s);
return;
}
/* Topmost diagonals */
if (k >= n - 1)
{
if (k == n)
fmpz_one(s);
else /* k == n - 1 */
{
/* S(n,n-1) = binomial(n,2) */
fmpz_set_ui(s, n);
fmpz_mul_ui(s, s, n-1);
fmpz_divexact_ui(s, s, UWORD(2));
}
return;
}
/* Leftmost columns */
if (k <= 2)
{
if (k < 2)
fmpz_set_ui(s, k);
else
{
/* S(n,2) = 2^(n-1)-1 */
fmpz_one(s);
fmpz_mul_2exp(s, s, n-1);
fmpz_sub_ui(s, s, UWORD(1));
}
return;
}
_fmpz_stirling2_powsum(s, n, k);
}
void
arith_stirling_number_2_vec(fmpz * row, slong n, slong klen)
{
slong m;
for (m = 0; m <= n; m++)
arith_stirling_number_2_vec_next(row, row, m, klen);
}
|