Numerical Recipes in C

William H. Press

Mentioned 9

The product of a unique collaboration among four leading scientists in academic research and industry, Numerical Recipes is a complete text and reference book on scientific computing. In a self-contained manner it proceeds from mathematical and theoretical considerations to actual practical computer routines. With over 100 new routines bringing the total to well over 300, plus upgraded versions of the original routines, the new edition remains the most practical, comprehensive handbook of scientific computing available today.

More on Amazon.com

Mentioned in questions and answers.

I was looking for a FFT implementation in C. However, I am not looking for a huge library (like FFTW) but for a easy to use single C-file implementation. Unfortunately I haven't been able to find anything like this.

Can someone recommend a simple implementation?

You could start converting this java snippet to C the author states he has converted it from C based on the book numerical recipies which you find online! here

I need an efficent algorithm to do math::power function between two floats, do you have any idea how to do this, (i need the algorithm not to use the function itself)

The general algorithm tends to be computing the float power as the combination of the integer power and the remaining root. The integer power is fairly straightforward, the root can be computed using either Newton - Raphson method or Taylor series. IIRC numerical recipes in C has some text on this. There are other (potentially better) methods for doing this too, but this would make a reasonable starting point for what is a surprisingly complex problem to implement. Note also that some implementations use lookup tables and a number of tricks to reduce the computation required.

I've been working with a lot of FORTRAN 77 lately, and though I had reservations at first, I now see some great qualities of the language. It is:

  • Simple to learn (no object-oriented or functional-programming complications)
  • Easy to read
  • Blazing fast at number-crunching

Those qualities are naturally attractive to non-programmer engineers and scientists who just want a way to make a computer give answers quickly. But it has a lot of limitations based on its age and some of its basic assumptions. I'm skeptical about how easily non-programmers can understand some of the added capabilities of later versions of Fortran, as well.

Is there a modern language that is as straightforward as FORTRAN, just as fast for mathematical applications, and less limited? Or is the answer Fortran 90, 95, 2003 . . . ?

Simple to learn (no object-oriented or functional-programming complications)

Easy to read

Blazing fast at number-crunching

Sounds like C.

Buy Numerical Recipes in C. That pretty much covers all the bases in a clear, readable style.

http://www.amazon.com/Numerical-Recipes-Art-Scientific-Computing/dp/0521431085

Can anybody recommend a good introduction book on Monte Carlo algorithms in c++? Preferably with applications to physics, and even more preferably, the kind of physics being quantum mechanics.

Thanks!

Why not using C# instead ?

I would recommend you the book of George Levy http://www.amazon.fr/Computational-Finance-Using-C/dp/0750669195. This book deals with financial mathematics and so with Monte-Carlo methods..

If you don't mind a book with a finance slant to it, my initial assessment of Monte Carlo Frameworks: Building Customisable High-Performance C++ Applications is very positive.

What is the best way to create the best pseudo-random number generator? (any language works)

Yikes, that can get VEEEEEERY complicated! There seem to be a number of metrics for how to measure the "randomness" of a random number generator, so it's difficult to meaure which are "best". I would start with Numerical Recipes in C (or whatever langauge you can find one for) for a few examples. I coded up my first simple one from the examples given there.

EDIT: It's also important to start by determining how complex you need your random number generator to be. I remember a rude awakening I had in C years ago when I discovered that the default random number generator had a period somewhere around 32,767, meaning that it tended to repeat itself periodically after generating that many numbers! If you need a few dice rolls, that's fine. But not when you need to generate millions of "random" values for a simulation.

I was dusting off my copy of Numerical Recipes in C book and started wondering if a similar treatment exists for algorithms that can be palatalized using MapReduce and Haddop.

Thanks.

There is a nice compilation from atbrox for the different algorithms in different domains. For text processing here is nice one another. Also, search @ CiteSeerx.

I have implemented a rudimentary low-pass filter using a time based value. This is ok, but trying to find the correct time slice is guess work, and gives different results based on different input audio files. Here is what I have now:

- (void)processDataWithInBuffer:(const int16_t *)buffer outBuffer:(int16_t *)outBuffer sampleCount:(int)len {   
    BOOL positive;
    for(int i = 0; i < len; i++) {
        positive = (buffer[i] >= 0);
        currentFilteredValueOfSampleAmplitude = LOWPASSFILTERTIMESLICE * (float)abs(buffer[i]) + (1.0 - LOWPASSFILTERTIMESLICE) * previousFilteredValueOfSampleAmplitude;
        previousFilteredValueOfSampleAmplitude = currentFilteredValueOfSampleAmplitude; 
        outBuffer[i] = currentFilteredValueOfSampleAmplitude * (positive ? 1 : -1);
    }
}

What can I do to convert this code into code that will allow me to cut frequencies over a certain hz by a certain db level?

I strongly recommend numerical recipes in c. Outside of that, I'm not sure I can help you.

When you design a filter, you need to calculate the coefficients of that filter based on the frequency so you almost need a class to handle it, not just a function.

This is in C++ but it should get you started. Sorry I can't provide a concrete answer.

i am working with monte carlo simulations and i need to understand this function in C language. I hope someone knows how to explain the modus operandi. Thx. // function that gen random numbers

        #include <stdlib.h>
        #define MBIG 1000000000
        #define MSEED 161803398
        #define MZ 0
        #define FAC (1.0/MBIG)

        double ran3(long *idum)
        {
          static int inext,inextp;
          static long ma[56];
          static int iff=0;
          long mj,mk;
          int i,ii,k;

          if (*idum < 0 || iff == 0){
              iff=1;
              mj = labs(MSEED-labs(*idum));
              mj %= MBIG;
              ma[55]=mj;
              mk=1;
              for (i=1;i<=54;i++){
                  ii=(21*i) % 55;
                  ma[ii]=mk;
                  mk=mj-mk;
                  if (mk < MZ) mk += MBIG;
                  mj =ma[ii];
              }
              for (k=1;k<=4;k++)
                  for (i=1;i<=55;i++) {
                      ma[i] -= ma[1+(i+30) % 55];
                      if (ma[i] < MZ) ma[i] += MBIG;
                  }
              inext=0;
              inext=31;
              *idum=1;
          }
          if (++inext == 56) inext=1;
          if (++inextp == 56) inextp=1;
          mj =ma[inext]-ma[inextp];
          if (mj < MZ) mj += MBIG;
          ma[inext] =mj;
          return mj*FAC;
        }

That function is from Numerical Recipes in C, Chapter 7, Random Numbers. The best explanation you will find is in there.

So, I want to write a function in code using some sort of algorithm to calculate any number to any power, including decimals. I use JavaScript and it already has an inbuilt pow function:

Math.pow(2, 0.413) // 2^0.413 = 1.331451613236371, took under 1 second.

Now I want to write my own like this:

function pow(x, y) {
    // Algorithm
}

This is a function that calculates the square root of any number (x^0.5), and it's very accurate with only 10 loops:

function sqrt(x, p) { // p = precision (accuracy)
    var a = 1;
    var b = x;

    while (p--) {
        a = (a + b) / 2
        b = x / a
    }

    return a
}

Is there any simple formula to calculate any exponential?

If there isn't a simple one, is there a hard one?

If the solution is slow, how can JavaScript's pow estimate under a single second?

I checked this post, but it worked only for whole numbers (1,2,3... not 0.1, 0.3...)

Recursive power function: Why does this work if there's no initial return value?

Then,

I got this from here : Algorithm for pow(float, float)

function power(x,n) {
    if(n === 0) return 1;
    if(n === -1) return 1/x;
    if(n === 1) return x;
    return Math.exp(n*Math.log(x))
}

console.log(power(2,3.5));

I added some basic checks (n===0)... To fasten things up in case.

Flexo sums it up :

The general algorithm tends to be computing the float power as the combination of the integer power and the remaining root. The integer power is fairly straightforward, the root can be computed using either Newton - Raphson method or Taylor series. IIRC numerical recipes in C has some text on this. There are other (potentially better) methods for doing this too, but this would make a reasonable starting point for what is a surprisingly complex problem to implement. Note also that some implementations use lookup tables and a number of tricks to reduce the computation required.

http://mathworld.wolfram.com/NewtonsMethod.html

http://mathworld.wolfram.com/TaylorSeries.html

http://en.wikipedia.org/wiki/Logarithm#Power_series

https://amazon.com/dp/0521431085/?tag=devbookscom20-20