Thursday, June 30, 2011

To the Web and Back

Before the Web

Before the initial releases of Netscape and Internet Explorer in 1994, most people did not have access to the web. They were satisfied with the desktop software available at the time which ran on DOS/Windows or well, Unix. Commonly used applications were word-processors, editors and some games for fun.

With the advent of Win32 API lot of software companies started investing heavily in developing desktop software applications and by the time Windows 98 was available on the market there were a host of applications which ran on these machines loaded with Windows 98. In fact the success of Windows operating system was possible only because of the availability of a large number of applications (and games) for Windows. Microsoft took great pains ("application compatibility" of Raymond Chen) to ensure that people could run their software from 1990s on the latest versions of Windows available at time.

People were getting so used to using these desktop applications (like MS-Office apps, graphics editors like Paintshop, Photoshop etc) that to a common man a computer was equivalent to a collection of these apps. This is also one of the reasons why the Unix systems did not make into mainstream for end-users. The Unix systems simply did not have the applications which ran on the Windows desktop.

Arrival of the Web

Sometime during the 1995-1998 the web took off and people started browsing websites using Internet Explorer and Netscape Navigator. Initially the websites were more about having information available to public at large. The web-pages were coded in HTML which was used to present information in the form of well-formatted pages containing graphics, fonts, tables etc (almost like a document written using state of the art word-processors).

Then another significant milestone in the history of web was the introduction of web based email. People could have an email address for free and could use it from anywhere using any system with an internet browser. People could communicate with each other and exchange personal stuff using email. This really led to a huge increase in internet usage by the common people who were more interested in sharing stuff with their friends on email rather than reading some information on a web-page.

However even with the introduction of web based email, the web-technology was very primitive and people used to do most of the work on computer using locally installed applications. With the AOL and Yahoo! Messenger coming to existence it became even easier to interact with friends without using the email.

The web-usage was increasing gradually but because of limited bandwidth the experience on the web was far inferior to that provided by a locally installed application. Using a desktop mail application like Outlook, Lotus Mail was far more satisfying that using web mail (only web mail was free!). The user-experience in terms of visual design and responsiveness of the web was simply not upto the mark as compared to those provided by these desktop apps.

Enter GMAIL and AJAX

Fast forwarding to April Fool's Day 2004 and something really changed on the web with Google's introduction of GMail. GMail made extensive use of Java-Script to enhance user-experience and since Java-Script runs locally in the browser the responsiveness of the mail application improved greatly. GMail relied on the now available faster internet speeds to load huge amount of Java-Script code which then executed in the browser to provide an experience similar to a locally installed email application.

This heavy usage of Java-Script was given a new name called AJAX (Asynchronous Java-Script and XML). Asynchronous mode of operation led to a better user-experience as the user didn't have to wait for an operation to complete. It would be taken care by running asynchronously in the background.

Google started to build many applications on the web using this technology (e.g. Google Documents where you could create an MS-Word document using the web) and soon the web was flooded with rich web based applications based on AJAX. You had graphics editors, messenger apps (like GMail chat) and even Google Voice and Video chat right inside GMail.

Clearly Google's wanted to run every application on the web and thus alleviate the need to have Windows desktop. All you needed to run these web apps was an internet browser running on any operating system. With Google Chrome browser web applications became even better.

Apple iPhone and Android

At the same time when Google was trying to boost experience on the web, Apple launched iPhone. This was a mobile device which focused on small simple apps which the end-user would enjoy using. iPhone's phenomenal success led to the creation of a large number of applications which could be used without any access to a desktop computer.

Now people could check their email and have a chat using their iPhone. Thus almost all the popular web-sites (like email and social networking sites like Facebook) started providing iPhone apps for their web based services. These application were quite simple to use and provided almost the same functionality provided by the corresponding website and it was possible to do so without having access to a computer with web browser.

With Google's introduction of Android platform the mobile devices with rich applications became cheaper and led to an even widespread use of these mobile applications. The number of people checking web mail using mobile devices is increasing at a rapid rate and will soon outgrow the number people who are accessing the same services using a web browser.

I feel that in some sense we are back to where we started. The trend is towards using locally installed applications (although not installed on a desktop, but rather mobile devices) which connect to the websites for data and provide it to the user on his device. In the beginning applications were fully local (user-interface, data, logic all on the local machine), then with AJAX you had everything in the browser. With the widespread availability of mobile devices the applications now have some part (mainly user-interface and some logic) installed locally whereas the web is being used as a back-end storage and database.

Thursday, December 02, 2010

Angle Sum Formula for Polygons

We are all familiar with the fact that the sum of all the angles of a triangle is 180 degrees. And for quadrilaterals the sum of its angles comes to be 360 degrees. This can be generalized to prove that the sum of angles of polygon of n sides is (n - 2) * 180 degrees.

A simple proof of this angle sum formula can be provided in two ways. First we can divide the polygon into (n - 2) triangles using (n - 3) diagonals and then the sum of the angles is clearly (n - 2) * 180 degrees.

Second approach is to take a point in the interior of the polygon and join this point with every vertex of the polygon. This gives us n triangles and so the sum of angles of all these triangles is n * 180 degrees. This also includes the angles made at the chosen point in the interior which add up to 360 degrees. Again this means that the sum of angles of a polygon is n * 180 - 360 = (n - 2) * 180 degrees.

Both the above approaches make use of the fact that the sum of angles of a triangle is 180 degrees. We will present here a third approach which does not use this fact but rather relies on the simpler fact that the angles in a linear pair add up to 180 degrees (this is more of an axiom, or the definition of degree measure).

The proof can be illustrated by the following diagram:

The idea here is to note that the sum of exterior angles add upto 360 degrees. This is very clear if we observe that if we rotate the ray AA' by the exterior angle at B we get a ray parallel to ray BB'. Similarly if BB' is rotated by the exterior angle at C then we get a ray parallel to ray CC'. Thus if the ray AA' is rotated by an angle which is sum of exterior angles at B and C then we reach parallel to ray CC'. Continuing this way we can see that if the ray AA' is rotated by an angle which is the sum of all exterior angles then we get back to the direction of ray AA' (full circle!). Thus the sum of exterior angles must be 360 degrees (this is the definition of degree measure, one complete revolution is 360 degrees).

Now if we add up the interior and exterior angles at a vertex we get a linear pair of angles. And so if we add all interior and exterior angles of a polygon we get n linear pairs of angles. So the total sum of the interior and exterior angles is n * 180 degrees. Subtracting the sum of exterior angles from it we see that the sum of interior angles of a polygon is n * 180 - 360 = (n - 2) * 180 degrees.

Note how smartly the proof avoids the use of the special case of sum of angles of a triangle. The idea of this proof came to me some months ago when I was looking at a problem in NCERT 8th standard text where one was supposed to measure all exterior angles of a quadrilateral and show that it is 360 degrees.

Monday, November 29, 2010

Another Proof of Pythagoras Theorem

Pythagoras Theorem is one of the most important theorems in elementary geometry. It states that in a right angled triangle the square of the largest side is the sum of the squares of the other two sides.

There are a large number of proofs available in textbooks and online articles. I will show here a very simple proof that uses only two concepts:

1) Area is additive.
2) Congruent figures have same area.

The proof falls in the category of "Proof Without Words" (may need a line or two of explanation):

As can be seen both squares are of side (a + b) and contain 4 right triangles of equal area (in fact they are congruent triangles) in the shaded portion. It follows that the remaining area (in white) in both the squares is same and therefore we have a^2 + b^2 = c^2.

This is something of a marvel as you don't even need to know the formula of the area of a triangle. If you know the formula for the area of the triangle then the figure on the left is enough to prove the Pythagoras theorem:

Area of bigger square = (a + b)^2 = Area of 4 triangles + Area of smaller square (in white)
and so (a + b)^2 = 4 * (ab / 2) + c^2 and thus a^2 + b^2 = c^2

The proof above using two figures is from 7th standard NCERT textbook. And it again shows that NCERT beats any other textbook in the school level curriculum. Next time I will show another proof about sum of angles of a polygon.

Friday, August 21, 2009

The Legacy of getch()

Many software engineers of my age (including me) started C programming using the Turbo C/C++ IDE. Most of us had no idea of computer usage in the school time and therefore no one really understood deeply what Turbo C/C++ (shortened as TC in what follows) was. It was just more or less equivalent to C programming. So we will go back in history and write our first C program in TC:

#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}

When we run the program in TC the screen gets black (from the all blue TC background) just for a moment and we don't see the output of the program. The lab instructor / professor (or maybe Yashawant Kanetkar's Let Us C) tells us to modify the program as follows:

#include <stdio.h>
#include <conio.h>
int main()
{
printf("Hello World!\n");
getch();
return 0;
}

And Wow! This time the program does give an output "Hello World!" nicely written on a black background. Press Enter and we are back to the Blue Screen of TC (not Death).

And thus we meet the beast getch() and admire its purpose of holding the screen to show the program output. No further use is made of this function in our entire software career. From here on there are two paths people follow:

1. People learn UNIX/Linux.
They are badly hurt by the absence of getch() and finally realize that getch() is not really an essential part of C programming. In fact its not even part of the standard C library. A reasonable job could equally have been done by the the getchar() function provided by standard C library.

In fact UNIX users actually learn much more. They realize that a C program could be written using any text editor and it needs to be compiled using the "cc" command, following which an executable "a.out" is generated. This executable can then be run on the command prompt and then there is no need to hold the screen to see the output. The screen just displays the program output and prints the command prompt again.

Some of the UNIX users go back to TC world and repeat the same experiment now with the "tcc.exe" command (Turbo C command line Compiler) and understand that TC is actually an IDE which manages the job of creating/compiling/running programs in one place.

2. Or they don't.
These kind of people remember the getch() for their life and use it for any test program in their entire software career and we are then stuck with the legacy of getch().

Let us now ponder why this legacy is being carried to this day (when Microsoft Visual Studio , to be referred as VC in what follows, has completely wiped out the Borland TC). Also is it really worthwhile to carry on with getch()?

Why does getch() persist?
Software engineers who are exclusively trained on Windows machines almost never get the hang of command line. For them GUI is everything and they just cannot do without GUI. But strangely when they need to test any C feature (to clear some confusion) they will write a console based C program. Mind you they almost never write an MFC app to test a C feature (which could be done in a minute or two in VC). When you run a console based program in VC it opens a console window, prints the output on console and the console window is destroyed.

To hold the console window we again ask help from our old friend getch() and because of Microsoft's extreme obsession with backward compatibility VC retains the getch() function, thereby doing our job of seeing program output.

The most ironical thing about the whole episode is that most users are unaware that both TC and VC provide a keyboard shortcut to show the contents of the output window so that they see the program output or that a simple getchar() from C library could also perform the job. Why they are unable to comprehend this is completely beyond my comprehension. I believe they never really came out of the TC paradigm of programming.

This is not to belittle the quality of TC as a software. Before VC it was the only reliable software available on Windows/DOS systems to develop/debug C/C++ programs. It had a nice user interface (apart from that blue background which was not that eye-soothing to some of my friends). In fact TC also had a TurboVision library (TV.LIB) using which you could make a program with menus and buttons similar to TC itself.

But somehow TC sent a wrong message to most of the not so talented programmers that "TC == C programming" and not that "TC == C programming IDE." This is definitely the case in India as is evident from many C programming books written by Indian authors. In fact one of our colleagues used to keep a copy of TC only to clarify some C language confusion. TC was handy for him compared to VC.

Do we need to carry on the legacy?
No!! Absolutely not! Run away from those who are still stuck in the TC era. They will never have a clear understanding of what computer programming is all about. Many TC users don't even have the faintest idea of the steps involved in generating an executable from a C source code because TC makes it all transparent.

In contrast anyone who is working with UNIX systems is sure to face the all too well known problem related with <math.h>. If one writes a C program which uses any function from <math.h>, say cos(), and tries to compile the program using "cc f.c" command, he gets an "unresolved external" error. He is then forced to do some research and come up with "cc f.c -lm" which actually works. One then realizes concepts of compiling and linking and sometime later the "cc -v" command gives it all: preprocessing, compiling, assembling and linking.

For a beginner TC works greats, but to be a software developer it is very essential to come out of that transparent mode. One must know what goes behind the scenes and work with some command line compiler tools like "cc" (nowadays "gcc") (TC also provides "tcc.exe").

Tuesday, August 04, 2009

New Blog for Mathematical Topics

I have started a new blog on WordPress to discuss mathematical topics. Please check it out here.