예제 #1
0
long double compute_coefficient(int n , int k){
     long double res = 0 ;
    if (n == 0 ){
        res =  0 ;
    }else if (k == 0 ){
       res = 1 ;
    }else{    
        double gcd = getgcd(n,k);
        res = (n*gcd)*compute_coefficient(n-1,k-1) / (k*gcd);
    }
    
/*    printf("Binomial coefficient of (%d,%d) is %.0llf \n", n,k,res);*/
    return res ;
}
예제 #2
0
int main(int argc, char *argv[] ){

    if(argc != 4 || atoi(argv[1]) > atoi(argv[2]) ){
        printf("Usage  : prog k n sieve_size, with k <= n \n");
        return 1 ;
    }
    
    int k = atoi(argv[1]);
    int n = atoi(argv[2]);
    int s = atoi(argv[3]);
    
    printf("Given n:%d, k:%d and sieve_size:%d\n",n,k,s);

    long double res = compute_coefficient(n,k);
    
    printf("Binomial coefficient of (%d,%d) is %.0llf \n", n,k,res);
    /*printf("%.0llf",res);*/
    compute_prime_factors(res,s);

    return 0;
}
예제 #3
0
  Vector Bspline::basis(double x) const {
    if (basis_dimension_ == 0) {
      return Vector(0);
    }

    // Each basis function looks forward 'degree' knots to include x.
    Vector ans(basis_dimension(), 0.0);
    if (x < knots_[0] || x > knots_.back()) {
      return ans;
    }

    // To find the knot in the left endpoint of the knot span, we first find the
    Vector::const_iterator terminal_knot_position = std::upper_bound(
        knots_.begin(), knots_.end(), x);
    int terminal_knot = terminal_knot_position - knots_.begin();
    int knot_span_for_x = terminal_knot - 1;

    // Each row of the basis_function_table corresponds to one knot
    // span.  Row zero corresponds to [knots_[0], knots_[1]), row 1 to
    // [knots_[1], knots_[2]), etc.  The columns correspond to the
    // degree of the spline.  We first compute the zero-degree bases.
    // We can use the zero-degree bases to get the first degree bases,
    // etc.
    //
    // The zero-degree basis is 1 in exactly one position (the knot
    // span containing x).  The linear basis is nonzero in two
    // positions: knot span containing x and the the one prior to
    // that.  Each additional degree increases the number of nonzero
    // knot-spans by 1.
    //
    // Given the initial (zero-degree) basis, we can compute each
    // higher degree basis using the recurrence relation:
    //   basis[knot_span, degree](x) =
    //       L(x, knot, degree) * basis(knot, degree - 1)
    //      +R(x, knot, degree) * basis(knot + 1, degree - 1)
    // The left coefficient L is
    //
    //                        (x - knots_[knot])
    // L(x, knot, degree) =   ---------------------
    //                  knots_[knot + degree] - knots_[knot]
    //
    // if the denominator is zero (because of knot multiplicity) then
    // the value of L is arbitrary, so we set it to zero.
    //
    // and the right coefficient R is
    //     R(x, knot, degree) = 1 - L(x, knot + 1, degree).
    //
    // All this is explained in deBoor ("A Practical Guide to
    // Splines") chapter IX, and online at
    // www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/B-spline/bspline-basis.html

    int number_of_knot_spans = number_of_knots() - 1;

    // The entries of basis_function_table, column d, are the spline
    // basis functions of degree d.
    ArbitraryOffsetMatrix basis_function_table(
        -degree(), number_of_knot_spans + order_,
        0, order_,
        0.0);

    basis_function_table(knot_span_for_x, 0) = 1.0;
    for (int d = 1; d <= degree(); ++d) {
      // For a given knot_span containing x, each spline basis of
      // degree d is nonzero over its knot span, and the next d spans.
      // Thus to find the set of nonzero bases containing x, we must
      // look _back_ d spaces.
      for (int lag = 0; lag <= d; ++lag) {
        int span = knot_span_for_x - lag;
        double left_coefficient = compute_coefficient(x, span, d);
        double right_coefficient =
            1 - compute_coefficient(x, span + 1, d);
        double left_basis = basis_function_table(span, d - 1);
        double right_basis =
            span < number_of_knot_spans ?
                   basis_function_table(span + 1, d - 1) : 0.0;
        basis_function_table(span, d) = left_coefficient * left_basis
            + right_coefficient * right_basis;
      }
    }
    if (number_of_knots() > 1) {
      for (int i = -degree(); i < number_of_knot_spans; ++i) {
        ans[i + degree()] = basis_function_table(i, degree());
      }
    }
    return(ans);
  }