bridge/class-master/depend/pari/src/basemath/bibli2.c
/********************************************************************/
/**                  STIRLING NUMBERS                              **/
/********************************************************************/
/* Stirling number of the 2nd kind. The number of ways of partitioning
   a set of n elements into m non-empty subsets. */
STIRLING NUMBERS
Stirling number of the 2nd kind. The number of ways of partitioning
a set of n elements into m non-empty subsets.
STIRLING NUMBERS Stirling number of the 2nd kind . The number of ways of partitioning a set of n elements into m non - empty subsets .
GEN
stirling2(ulong n, ulong m)
{
  pari_sp av = avma;
  GEN s, bmk;
  ulong k;
  if (n==0) return (m == 0)? gen_1: gen_0;
  if (m > n || m == 0) return gen_0;
  if (m==n) return gen_1;
  bmk = gen_1; s  = powuu(m, n);
  for (k = 1; k <= ((m-1)>>1); ++k)
  { 
    GEN c, kn, mkn;
    bmk = diviuexact(mului(m-k+1, bmk), k);
    kn  = powuu(k, n); mkn = powuu(m-k, n);
    c = odd(m)? subii(mkn,kn): addii(mkn,kn);
    c = mulii(bmk, c);
    s = odd(k)? subii(s, c): addii(s, c);
    if (gc_needed(av,2))
    {
      if(DEBUGMEM>1) pari_warn(warnmem,"stirling2");
      gerepileall(av, 2, &s, &bmk);
    }
  }
  if (!odd(m))
  {
    GEN c;
    bmk = diviuexact(mului(k+1, bmk), k);
    c = mulii(bmk, powuu(k,n));
    s = odd(k)? subii(s, c): addii(s, c);
  }
  return gerepileuptoint(av, diviiexact(s, mpfact(m)));
}
GEN stirling2 ( ulong n , ulong m ) { pari_sp av = avma ; GEN s , bmk ; ulong k ; if ( n == 0 ) return ( m == 0 ) ? gen_1 : gen_0 ; if ( m > n || m == 0 ) return gen_0 ; if ( m == n ) return gen_1 ; bmk = gen_1 ; s = powuu ( m , n ) ; for ( k = 1 ; k <= ( ( m - 1 ) >> 1 ) ; ++ k ) { GEN c , kn , mkn ; bmk = diviuexact ( mului ( m - k + 1 , bmk ) , k ) ; kn = powuu ( k , n ) ; mkn = powuu ( m - k , n ) ; c = odd ( m ) ? subii ( mkn , kn ) : addii ( mkn , kn ) ; c = mulii ( bmk , c ) ; s = odd ( k ) ? subii ( s , c ) : addii ( s , c ) ; if ( gc_needed ( av , 2 ) ) { if ( DEBUGMEM > 1 ) pari_warn ( warnmem , " " ) ; gerepileall ( av , 2 , & s , & bmk ) ; } } if ( ! odd ( m ) ) { GEN c ; bmk = diviuexact ( mului ( k + 1 , bmk ) , k ) ; c = mulii ( bmk , powuu ( k , n ) ) ; s = odd ( k ) ? subii ( s , c ) : addii ( s , c ) ; } return gerepileuptoint ( av , diviiexact ( s , mpfact ( m ) ) ) ; }
STIRLING NUMBERS
Stirling number of the 2nd kind.
STIRLING NUMBERS Stirling number of the 2nd kind .
/* k = 0 */ /* bmk = binomial(m, k) */ /* k = m/2 */