Friday, June 04, 2021

How to install Compiler for C and GNU Scientific Library on Windows?

I often find that students are struggling to install a compiler for C and GNU Scientific Library (GSL) on their MS Windows machines. Sometimes Windows can behave weirdly while you try to install these tools. This happens mostly when you have installed and uninstalled lots of programs and packages. 

Here is a step-by-step guide to install 1) a compiler for C and 2) GSL on your Windows machine.

Installation of MinGW on Windows 10

1. MinGW is a native Windows port of the GNU Compiler Collection (GCC). Download the MinGW installer from SourceForge:

    https://sourceforge.net/projects/mingw/

2. Run the installer and follow the instructions on the screen.

3. The installer will install the MinGW installation manager. This is used for installing MinGW and to uninstall MinGW.

4. Run the installation manager. To start with we will install the basic setup.

    Select basic setup on the left-hand pane. 

    On the righthand pane for the package list, select each package one by one and mark those for installation (Right-click > Mark for Installation).

    Once all the packages are marked, install those: Installation > Apply Changes

5. Installation may take some time. Note that apart from MinGW, this process will also install MSYS. MSYS is a bash shell that we will use while compiling and running C codes.

6. On successful installation you may have the following directory structure:

    C:\MinGW

    Within this folder, you will find the MSYS subfolder. Find the msys.bat file.     For example, this may be at: C:\MinGW\msys\1.0\msys.bat

    Run this batch file (double-click) to get the bash shell. You can create a shortcut for this batch file and keep it at a suitable location, like the desktop.

7. Check MinGW installation by running a simple C code. For example, here is a 

code named as test.c

        #include <stdio.h>

        int main() {

                       printf("Hello, world!\n");

                       return 0;

        }

8.  To compile the code, you have to be in the folder with the code. Use cd to reach the appropriate folder. Note that we are now working in Bash shell (not in the Windows default command prompt). Therefore, in the pathname, we have to use / in place of \

9.  Compile the code: 

        $ gcc test.c -o testrun

10.  Run the code

        $ ./testrun

        If everything works fine, the program will print:

          $  Hello, world!


Installation of GSL on Windows 10

1. You must install MinGW with MSYS before installing GSL.

2. Download GSL:

    https://www.gnu.org/software/gsl/

3. Place the downloaded compressed file (named like: gsl-latest.tar.gz) in the MinGW folder. 

4. Start MSYS. We will now use MSYS for all the subsequent work.

5. cd to the appropriate folder and extract the files:

    $ tar xzf gsl-latest.tar.gz

6. The files will be placed in a newly created folder, for example, gsl-2.6 

7. Move to that newly created folder:

    $ cd gsl-2.6

8.  Configure, make, and install GSL:

   $ ./configure

  $ make

  $ make install

Each of these steps takes some time. Wait for the completion of each step before executing the next. 

9.  Upon completion of installation you should find the GSL lib and include folders inside msys folder. For example:

    C:\MinGW\msys\1.0\local\include

    C:\MinGW\msys\1.0\local\lib

10.  Compile and run a C code that uses GSL. Again, we will use MSYS to compile and run the code. For example, here is the code for testgsl.c:

    #include <stdio.h>

    #include <gsl/gsl_sf_bessel.h>

    int

    main (void)

    {

        double x = 5.0;

        double y = gsl_sf_bessel_J0 (x);

        printf ("J0(%g) = %.18e\n", x, y);

        return 0;

    }

11.   To compile:

    $ gcc -I/usr/local/include -L/usr/local/lib   testgsl.c -lgsl -o testgslrun


(Note that depending upon how MinGW and GSL have been installed, we may not have to include -I/usr/local/include -L/usr/local/lib in the command for compilation.)

12.   Run the compiled code:

         $ ./testgslrun

This should print:

    $ J0(5) = -1.775967713143382900e-001 

Done!! Happy Computing. 


#C #programming #GSL #computing #compiler #gcc


Wednesday, November 06, 2019

What's In A Rule: Dissecting BODMAS



Ughh!! Not again! I muttered as I scroll through my Twitter feed. This post and similar virals keep popping in our Facebook and Twitter pages regularly. Every time, thousands reply. Many get it right, but most fail. I don’t curse. We do not do arithmetic in our day-to-day life, except for counting money or paying bills. Even then, we use calculators.  

By now, you must be saying- it’s elementary Watson. Don’t start a lecture on BODMAS.

No! not at all. Instead, we will explore the logic behind this rule and its origin.



BODMAS is an elementary rule that you learn in school. It is so common that it’s difficult to dig out the history of its origin or ask the logic behind this rule.

Some argue that this rule must have been formed as a ‘Convention’ to streamline arithmetic calculations, sometime in the 19th century, when we started producing mathematics textbooks. The need for such convention became extremely crucial when we began developing computers. However, there are documented evidences of the use of primitive forms of parenthesis, even in the seventeen century. Is there any chance that this rule may have originated in ancient times and has evolved further?

Let us investigate the rule itself to get some clues.

Parenthesis, in itself, represents priority. Therefore, it is common sense to perform the operations within parenthesis first. However, to understand the rest of the rule, let us focus on addition and multiplication.

Remember, that we currently use the Indo-Arabic number system that is suggested to be invented by ancient Indian mathematicians. Suppose we want to write “Three Hundred Twenty-One.” In the Roman numeral system, this would be CCCXXI. However, in the Indo-Arabic system, we will write it as 321. How are we getting this number?

Following the rule of Indo-Arabic system: 321 = 3 Hundreds + 2 Tens + 1 Ones = 3 × 100 + 2 × 10 + 1 × 1. Though it’s elementary for us, the invention of this system of representing a number was revolutionary.

Note the sequence of operations in this example. We have multiplied first and then performed the additions. Isn’t that what BODMAS says? What if we do not follow this rule and perform the additions first? Let’s check: 3 × 100 + 2 × 10 + 1 × 1 = 3 × 102 × 11 × 1 = 3366. So, the rule of multiplication before the addition is at the heart of our numeral system. Without this rule, our number system will not work.

In a way, division and subtractions are similar to multiplication and addition, respectively. Let me elaborate: 8 ÷ 4 × 2 = 8 × (1/4) × 2 and 4 – 2 + 2 = 4 + (-2) + 2. So, the order that we have followed for multiplication and addition should also be followed for division and subtraction.

The example of 4 – 2 + 2 also explains why we must perform operations from left to right. Let’s do the operations in the opposite way, 4 – 2 + 2 = 4 – 4 = 0. But even without following the BODMAS we can say that this result is wrong as 4 – 2 + 2 = 4 + (-2) + 2 = 4. Therefore, we have to stick to the rule of doing operations from left to right.

What about the exponents then? If you pay attention, you can easily notice that exponentiated terms are actually multiple operations clubbed together, just like we do by parenthesis. For example, 23 = (2 × 2 × 2) and $2^{\frac{3}{2}}= \sqrt{2\times 2\times 2}$. So, it is wiser to keep such terms separate from other operations and perform them beforehand. Otherwise, the calculation would get complicated.

So, in brief, the rule for multiplication and addition is at the heart of BODMAS, and as I suggested, it is a fallout of our numeral system. This numeral system originated in ancient India. The way we use symbolic algebra now was not developed at that time. But they had already started using notations for mathematical operations. Can there be any clue in the works on ancient Indian mathematicians on the order of operation? My hunch is there must be. I haven’t been able to explore much of their works. If you have any clue, do share it with me. 

Monday, September 05, 2016

FlowPy: from code to software


Cheers!! FlowPy, the software that we developed, got  three citations in published papers (PMID: 26116575, PMID: 27253695 & 27084942). 

It started as a lazy B. Tech project. My Ph. D student was using flow cytometry. We had good data. We wanted to use some advanced statistical analysis of that. Unfortunately, those tools are only available in commercial flow cytometry software. And to my utter surprise, our flow cytometer does not allow us to export raw data as plain-text. So we were trapped. We can not use any advanced statistical tools on our data.

So Tejas was entrusted to develop a software that would extract data from flow Cytometery files and export that as plain-text for further analysis. It was his B. Tech project. He wrote it in Python. We named it FlowPy.



At that time, I had no idea about software development. Software development is quite different from writing some codes for personal academic use. I had no idea about web-hosting of software, github, version control, GNU licence. We had no funding for the work either.

We were just happy to write the code, use it and share with others. So I posted the stuff at WikiDot, a blogging site. It is still there: http://flowpy.wikidot.com/

Over the years, other B. Tech students joined the team. Two students, M. V. S. R. Sastry, and Revanth Sai Kumar made most contributions. From a mere Python script, FlowPy graduated to a GUI-based software. It is no more a mere data extraction tool. It can also perform various statistical analysis and visualizations. 



We never published any paper on FlowPy. Just let it sleep at its WikiDot site. The information sipped slowly through the Web. Several Web lists of Flow Cytometry software listed FlowPy. Occasionally, I got queries on bug in our code. 

Slowly we were losing interest in FlowPy. For last one year, we stopped further development of FlowPy. But then came the bravo moment!! I realized some of our colleagues across the globe are using it successfully and two papers cited it. Unfortunately, WikiDot does not provide file download data. But Google Analytics tells me that we do get regular traffic. That too from all over the globe. 



Publishing a software is quite different from publishing a paper. A software is not a mere collection of information. It is a product: a product that has to be stable, reliable and works as promised. It has to work every time a user uses it. And we don't know the users.

That's why we are thrilled to see that some of our fellow scientists are finding FlowPy useful for their work. FlowPy is still buggy. It still has ample rooms for improvements.

We have again started development of FlowPy. Hope to remove the bugs and increase number of  in-built tools. If you use Flow Cytometry, give FlowPy a try and let us know your experience and expectations.

Happy Flow!

(Updated on 30/9/2016)

Sites that list FlowPy:

1. https://pypi.python.org/pypi/FlowCytometryTools/0.4.5
2. https://www.cds.caltech.edu/~murray/wiki/index.php/Flow_cytometry_software
3. http://gorelab.bitbucket.org/flowcytometrytools/
4. http://flowpy.software.informer.com/
5. http://en.freedownloadmanager.org/Windows-PC/FlowPy-FREE.html