Showing posts with label Technical. Show all posts
Showing posts with label Technical. Show all posts

Sunday, September 6, 2009

An interesting read - Divide and Conquer

The slow spread of a fast algorithm

In 1963, during a meeting of President Kennedy's scientific advisors, John Tukey, a mathematician from Princeton, explained to IBM's Dick Garwin a fast method for computing Fourier transforms. Garwin listened carefully, because he was at the time working on ways to detect nuclear explosions from seismographic data, and Fourier transforms were the bottleneck of his method. When he went back to IBM, he asked John Cooley to implement Tukey's algorithm; they decided that a paper should be published so that the idea could not be patented.

Tukey was not very keen to write a paper on the subject, so Cooley took the initiative. And this is how one of the most famous and most cited scientific papers was published in 1965, co-authored by Cooley and Tukey. The reason Tukey was reluctant to publish the FFT was not secretiveness or pursuit of profit via patents. He just felt that this was a simple observation that was probably already known. This was typical of the period: back then (and for some time later) algorithms were considered second-class mathematical objects, devoid of depth and elegance, and unworthy of serious attention. But Tukey was right about one thing: it was later discovered that British engineers had used the FFT for hand calculations during the late 1930s. And—to end this chapter with the same great mathematician who started it—a paper by Gauss in the early 1800s on (what else?) interpolation contained essentially the same idea in it! Gauss's paper had remained a secret for so long because it was protected by an old-fashioned cryptographic technique: like most scientific papers of its era, it was written in Latin.

[ Courtesy : Algorithms by Dasgupta, Papadimitriou, Vazirani ]

Saturday, August 29, 2009

Header Files in C

This post might be helpful if you are reading a C header file or wish to write one and baffeled to see unknown keywords and begin to think of header files as alien objects in C.

Header files are just another C files which commonly contain function declarations, identifiers and variables.

A Programmer will prefer to write a header file than having these declarations in main stram code either to give the program more structured appearance by using standardized identifiers or there are more than one source files which aims to use these identifiers or functions, these files can rather include a single header file whenever the identifiers are required.

At the time of compilation, the task of including header files is taken care of by pre-processor. A pre-processor is a separate program which is invoked by the compiler prior to the compilation process. A C pre-processor will modify the source code before handling it to the compiler.

Pre-processsor deals with three things,
Directives: which instructs the pre-processor to do the needful; they always begin with ‘#’ (Read: Sharp). Examples of directives are #include, #define, #if etc.
Constants and Macros : they are defined using #define. A task of pre-processor is to take care of these  #defined macros and constants.

Moving back to header files... Whenever a  ‘#include’ statement is seen by the pre-processor, the content of the header file is directly placed into the current file. There are a few directives which shall catch your attention whenever you read a standard (well-written) header files, they are related to conditional compiling of the codes.
-#if -
#if
....
#else
....
#endif

They have the same meaning as normal if  else statement with the only BIG difference being that they are not conditionals for execution but conditionals for compilation. If the after #if has a zero value, compiler will skip compiling the code till #else, and if #else is not present then till #endif

Another Similar directive is -#ifndef-
When a header file is #include'd in multiple source files, the result is that, all the variables, identifiers and functions are defined multiple times. The pre-processor , with the help of #ifndef directive can ensure that each header file is included once and only once. Here's an example showing use of #ifndef [ Read:  if NOT defined ]


#ifndef _FILENAME_H
#define _FILENAME_H
....
#endif

This simply means : include the following content only if a particular expression is undefined; then, if it is undefined you can define the expression. This ensures that the code in the #ifndef is included only the first time the file is loaded and not as many times as the #include statement is compiled.. [Rather pre-processed ]

[ Readers: This is out of my understanding. Corrections are welcome, I might be wrong at a place or two… or even entirely :) :)  ]

- Happy Programming -