Friday, June 04, 2021

How to install GNU Scientific Library on Ubuntu

GNU Scientific Library (GSL) is an essential library for scientific computing using C. You may have to use this library to generate random numbers or solve a system of ODEs. Installing GSL on a Linux machine is straightforward. Even then some students find it difficult. Here is a step-by-step guide to install GSL on your Ubuntu machine.

1. Download GSL:

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

2. It will be a compressed tar file, like gsl-x.x.tar.gz

3. Place this file in the home 

4. Open the terminal and unpack this downloaded file:

    $ tar -zxvf gsl-x.x.tar.gz

5. A new folder will be created. cd to that folder.

6. Now we will configure, make and install GSL:

    $ ./configure
   $ make
   $ sudo make install

Each of these steps may take some time. Wait patiently. 
7. Close the terminal and open .bashrc file in the home. It is a hidden file. If required use Ctrl+H to access it. 
Edit this file to add:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
8. Save the edited file and close it.
9. Now get back to the terminal and compile a C code that uses GSL. For example, use the test code 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;
    }

To compile:

    $ gcc -Wall testgsl.c -o run -lgsl -lgslcblas -lm
10. Now run the compiled code:

         $ ./testgslrun

This should print:

    $ J0(5) = -1.775967713143382900e-001 


Done!! Happy Computing. 


#C #GNU #GSL #computing #programming





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.