George
George

Reputation: 5681

c++ how to expess a mathematical term

i have the following : enter image description here

I only want (for now) to express the (s 1) , (s 2) term . For example ,(s 1)=s , (s 2)= s(s-1)/2! , (s 3)=s(s-1)(s-2)/3!.

I created a factorial function :

//compute factorial
int fact(int x){

if (x==0)
return 1;
else
    return fact(x-1)*x;

}

and i have problem in how to do right the above.

.....
double s=(z-x[1])/h;
double s_term=0;
    for (int p=1;p<=n;p++){
        if p==1 
            s_term=s;
            else
                    s_term=s*(s-p)/fact(p+1);

    }

Also, it is that : s=(x - x0)/h. I don't know if i have declared right the s above.(i use x1 in the declaration because this is my starting point)

Thank you!

Upvotes: 1

Views: 259

Answers (3)

Benjamin Bannier
Benjamin Bannier

Reputation: 58694

As others have pointed out implementing factorial and binomial coefficient functions is not easy (e.g. overflows lurk everywhere).

If you are interested in reasonable implementations as opposed to implementing all this yourself have a look at what is available in gsl which everybody dealing with numerical problems should know of.

#include <gsl/gsl_sf_gamma.h>

double factorial_10  = gsl_sf_fact(10);
double ten_over_four = gsl_sf_choose(10, 4);

Have also a look at the documentation. There are numerous functions returning the log instead of the value to avoid overflow problems.

Upvotes: 0

Thomas Russell
Thomas Russell

Reputation: 5980

You can calculate the Binomial Coefficient simply using this function (probably the best for performance and memory usage):

unsigned long long ComputeBinomialCoefficient( int n, int k )
{
        // Run-time assert to ensure correct behavior
        assert( n > k && n > 1 );

        // Exploit the symmetry in the line x = k/2:
        if( k > n - k )
                k = n - k;

        unsigned long long c(1);
        // Perform the product over the space i = [1...k]
        for( int i = 1; i < k+1; i++ )
        {
                c *= n - (k - i);
                c /= i;
        }

        return c;

}

You can then just call this when you see the brackets. (I'm assuming that is the Binomial Coefficient, rather than a 2D column vector?). This technique only uses 2 variables internally (taking up a grand total of 12 bytes), and uses no recursion.

Hope this helps! :)

EDIT: I'm curious how you're going to do the (I assume laplacian) operator? Are you intending to do the forward difference method for discrete values of x, and then calculate the 2nd derivative using the results from the first, then take the quotient?

Upvotes: 2

user837324
user837324

Reputation:

The factorial part will be much more efficient using a loop rather than recursion.

As for the binomial coefficients, the line:

s_term=s*(s-p)/fact(p+1);

isn't going to have the desired effect, as you're only setting the first and last terms correctly and missing out the (s-1), (s-2), ..., (s-p+1) terms. It's easier to just use:

s_term = fact(s) / (fact(p) * fact(s-p))

for s choose p.

Upvotes: 0

Related Questions