Archive for the 'Mathematics' Category

Mathematics for Computer Scientists

Mathematics is an integral part of computer science. Anyone serious about a degree in computer science will be looking at a good few mathematics courses in front of them. However, the mathematics required by computer scientists is somewhat different from the mathematics that is needed by other scientists. This difference stems from the basic difference between computer science and the other science.

Physics is probably the most mathematically heavy of all the sciences, in fact, large parts of modern mathematics were developed specifically to be used to describe physical concepts. Classical physics (and to some extent chemistry) deals with quantities that are for the most part, continuous in nature, i.e. things like velocities and energies. These quantities are not easily divisible into discrete units (unless you invoke quantum mechanics, but that is another matter altogether). Classical physics is intrinsically combined with a branch of mathematics that is known under the broad heading of calculus. Calculus encompasses a large number of mathematical techniques and theorems, but they all have one thing in common: they deal essentially with things that change gradually i.e. whatever system you are studying moves gradually from one state to another, and does not suddenly jump between states. Calculus is also an important part of economics and econometric analysis.

Computer science is different because the basic operations of computer circuits are most easily described in terms that involve specific, distinguishable chunks of information. At the basic electronic level, everything is in one of two distinct states: on or of, a 1 or a 0. Unlike Physics things are not continuous, they are discrete. Enter discrete mathematics. Discrete mathematics deals with concepts and information that are not continuous. The most common examples are integers and countable sets. Like calculus, discrete mathematics is also an umbrella topic covering many distinct topics and techniques. What computer scientists use most are boolean algebra (and related mathematical logic representations), combinatorics (how many ways there are to get a job done), computability theory and algorithmics (can a computer be used to solve a problem and what is the best way to go about it) and linear algebra.

So what’s the best way to get started? Firstly head off to Wikipedia and read through some articles to get a basic grasp of what’s involved. After that, it’s time to get a book. There are lots of good books and your local library should have a large store. If you’re studying for a specific course at school or looking toward a degree, it might be a good idea to see what the professors recommend. If you’re interested in computer science in general and not just discrete math, Concrete Mathematics would be a good idea as it incorporates important aspects of calculus as well. If you’re only interested in discrete math, a classic book would be Elements of Discrete Mathematics. Then again, what’s important is not a good book, but how much you really want to learn. And above all, have fun while you’re at it. 

 PS. You might be interested in this slightly old blog post about Math for Programmers.

The Ackermann Function in Java: Why Computers are Stupid

I’ve started teaching myself Java, right from the basics and as a guide I’m using the Java version of the How to Think Like a Computer Scientist book. One of the exercises at the end of the fifth chaper (called Fruitful Functions) is to implement the Ackermann Function as a recursive method. The Ackermann function is mathematically defined as:


Now, the Ackermann function is quite well suited to computerization, it takes little real intelligence to solve for any two numbers, and is mostly repetitive calculation (which computers are good at). It took me less than a minute to implement the function as a Java method as follows:

public static int ackerman(int ack1, int ack2){

if (ack1 == 0)

	return ack2+1;

else if (ack1 >0 && ack2 == 0)

	return ackerman(ack1-1, 1);
else if (ack1 >0 && ack2>0)

	return  ackerman(ack1-1, ackerman(ack1, ack2-1);
)

I passed it to the compiler and the compiler replied with a cheery: “missing return statement”. Since I already had three return statements, that meant that there was a possible path through the method where the method would end without a value being returned. The Ackermann function doesn’t work with negative numbers, so I had already implemented a check for negatives before the function call. I tried putting in another check for negatives in the function itself, but that didn’t work. By this point I was getting rather frustrated because the above code would catch all non-negative numbers and produce appropriate returns. I ran the code in my mind with a few small numbers and everything seemed to check out as it should, the values of ack1 and ack2 would keep on reducing until ack1 hit zero and the method would end with a proper return.

Finally on a hunch, I decided to remove the last if-else and make the last return statement free-standing. Semantically it was the same thing, because there was only one possible case if the first two conditions were not satisfied. And for some reason the compiler thought that this new version was perfectly passable. I haven’t entirely ruled out the possibility that there was really some path that would have resulted in no return. But I think that it is far more probable that the compiler for some reason couldn’t handle the multiple recursions and simply gave up. Of course, I’m not an expert in these things and if someone knows of a proper explanation please let me know. Until then I’ll stick to the knowledge that a computer is still quite some distance away from what would be common sense to a human (or at least the Java compiler is).

Mathematics: The Universal Language

Ok, I’m taking some time off from my technology chronicles and ramblings to perform a complete mind core dump regarding something else that I’m interested in: Mathematics and more particularly the way it is taught.First, some background: millions, probably billions of students around the world take high school math, many Indian students included. While many of us like maths, few of us actually love it, primarily because the way mathematics taught, in most places around the world and by most teachers, is, in my opinion, fundamentally flawed and leaves math students unable to grasp the true essence of mathematics. First off, mathematics is not so much a science or a subject, as it is a language. In the famous sci-fi movie Contact, Jodie Foster’s character, Dr. Ellie Arroway calls mathematics “the only universal language”. Know what? She’s right. Mathematics in its most basic form is simply a language (or more precisely, a notation) used to describe various logical constructs and processes. However, unlike most human languages, mathematics is inseparably and closely tied to whatever ideas and logic it conveys and has few ornamental components. In this way, mathematics is closer to computer languages than it is to human languages. Both strive to achieve maximum clarity and efficiency and both encourage the creation of new structures and algorithms to deal with new problems. Both evolve much faster than human languages and both are much richer than any human language in existence. However the most fundamental difference between these two and human languages, is that while all human languages allow for a certain amount of ambiguation, mathematics and computer languages reject ambiguity entirely, without losing richness i.e., There are multiple ways of expressing an idea or achieving a goal, but any statement can mean one and only one thing. Assuming that you can read the language properly.

Which brings me to my argument: mathematics is not taught like a language, at least, I have never been taught that way. The way mathematics is taught (in Indian schools certainly) is an inefficient and inconsistent jumble of ideas, formalisms, methods, and “problem solving tricks”. The result is that we learn some topics (set theory, Euclidean Geometry, Trigonometry, Calculus) , but we are simply unable to tie everything together into a consistent and usable conceptual/practical framework. We tend to see mathematics as a set of separate components which have only a few overlaps (like co-ordinate geometry) and consequently are utterly incapable of appreciating the true beauty of it all. In his book Hyperspace, Michio Kaku said that Mathematics is the set of all possible logical structures whereas physics is the set of those logical structures that actually exist. Unfortunately we see maths as just a set of problems to be solved. Mathematics, from the high school level onwards should be taught like computer programming: you are actively encouraged to use different branches of mathematics to solve problems and it is irrevelant what you use as long as you get the job done concisely and efficiently. The better mathematics teachers that I’ve seen actually do this: seamlessly melding together Calculus and Trigonometry or Geometry and Complex Numbers with all the dexterity of an eagle riding a rising thermal.

Of course, teaching or even using mathematics in this way would be completely different from what is the “standard” now. Many would say that maths like this too hard and not neccessary or is above the “standard”, once again completely missing the point. It is not that mathematics like this is harder than what is taught, it’s just different. Just as there’s no point in comparing a thoroughbred race-horse to an Olympic sailing boat, there’s no point in comparing the modern pattern with what I’m suggesting. We would require an army of excellent teachers who are not only good teachers, but regularly use mathematics like this themselves. They would have to be the Hackers of mathematics, who do things that only people like them have the capacity to truly understand, but yet hold us all spellbound by mere glimpses of their intellectual prowess. Unfortunately there are all too few of such people. At this moment I can think of only about 5 or 6.

How would one teach maths like this? Well, don’t ask me for details, but I do have an idea or two. The first step would be taking a leaf out of the book of one of mathematics most popular children: computer science. In my opinion, computer scientists got it right where mathematics teachers failed miserably: their whole world revolves around describing actions and data in machine-readable languages. They keep logic and representation essentially separate and so can easily use different representations in a concerted way. (This failure is unfortunate as most early computer scientists like Alan Turing and Jon von Neumann were originally mathemticians) So all math students should first be taught some amount of computer programming. Secondly Mathematics should be taught in breadth rather in depth. That is, students should be taught about the basics of a wide number of areas and then be allowed to choose the areas that they find most useful and interesting and be allowed to delve deeper into those areas as and when they need. Maths books and problems should also require the use of numerous different areas of maths to be solved properly. Importance should be given to visualizing the problem and the data given. An equation of a line in co-ordinate geometry should immediately cause students to have at least a rough idea of what it should look like and where on the plane it should be (though some students I know do this, their number is amazingly few). Students should then use visual intuition as much as mathematical analysis to solve the problem.

All that being said, I really don’t think that math education (especially at the high school level where it matters most) will change fundamentally, so all this is probably a waste of time and bandwidth and I might be very wrong as well. At least I have something to rant about.