And the game continues: Royal Tiara (Altaj Almalaki)

1. Name who asked you to play this game:

Solom

2.Mention six secrets nobody can uncover about you when meeting you for the first time.

1. I get bored really easy.

On Human Stupidity!

Some miserable primate creates a facebook page with an attractive religious title. Contrary to what you’d expect, the page is actually full of age-old, oft-repeated accusations against Islam that have long been refuted.

What does the page get? 30,000 Muslim fans who didn’t really bother to look up anything on the page past its title :S

‘Two things are infinite, The Universe and Human Stupidity, and I am not sure of the former’
– Albert Einstein

TEDxCairo

After looking at the final list of sessions. All I could say is “Meh!”.

So much geek-factor is obviously missing.

No VT for You!

So here it goes. I got a new Toshiba laptop with an Intel T6500 chip coming with it. Now naturally, all new Intel chips come with virtualization extensions enabled. So I try this:
mohd@mohd-laptop:~$ sudo modprobe kvm_intel
and I get this
FATAL: Error inserting kvm_intel (/lib/modules/2.6.31-16-generic/kernel/arch/x86/kvm/kvm-intel.ko): Operation not supported

Then I figure I haven’t enabled VT from the BIOS since I got the laptop, so I go into the BIOS settings, and find out that the option for enabling VT is greyed out, giving me no way in hell to enable it. I look on the internet for some solution and I find this.

So now, the OEM vendors have the capability just to turn off VT *completely*, and not even giving you the choice to enable it, and worse yet, Intel goes out to announce the chip has no VT support after about 1 month from shipping it to customers. Seriously WTF!

No, I can’t hack KVM anymore. DAMN!!!

Git vs. Mercurial

We’re moving from Subversion to Mercurial as our version control system at work, and since I was addicted to the caveats of distributed version control, I was using git-svn to maintain my working copy on top of SVN, but this is no more needed after the move.

After some playing with mercurial I can only say this: Mercurial maybe a more intuitive and user friendly VCS, but I really miss the fine-grained control I used to get from Git, even if that means occasionally shooting yourself in the foot at times.

I know, I know, you probably think I am a sick demented psycho. But I have no problem with that.

My $0.02. *Sigh*,

UPDATE:
I agree

Quick Shots!

– First and foremost, happy eid everyone.

– To whoever has been repeatedly viewing my resume for the last few days, I’d really appreciate knowing what’s so interesting to you about it?

– You can obviously see the enoromous writer’s block problem I am having in the lines above. I have a lot to say, but obviously I am failing miserably at articulating it. If you have any suggestions about that, please comment!

My Newest Musical Obsession

It’s no secret to those who know me that I am an electronica junkie, especially towards IDM/Ambient/Experimental types of electronica. I’ve happened to be introduced to Rena Jones‘s music through SomaFM, and man did I become obsessed! I just love how she blends classical violin with synthetic electronic sounds and glitchy beats.
Here is her newest album “Indra’s Web”:

“Driftwood”

“Transmigration”

Google Chromium on Linux … First Impressions

Perhaps you’ll first say ‘You probably mean Chrome, don’t you?’. Well, not really. Chromium is the open source project behind Chrome (Much like OpenOffice.org to Sun StarOffice, or Fedora to RedHat Enterprise Linux). It seems good things are going to come out real soon, especially with the upcoming release of Google Chrome OS, it seems Google is paying more attention to get Chrome finally released on Linux. Here are my first impressions:

1 – Running it is so smooth, not a memory hog like Firefox.
2 – The V8 JavaScript engine seems to make browsing a lot faster, AJAX interfaces almost run as fast as a native desktop application
3 – On the downside, it doesn’t look anywhere near as cool as Firefox, and not even as cool as it looks on Windows.

To Understand Recursion…You Need To Understand Recursion (Take 2)

Look here first.

So after some insights from Nicolas, I’ve modified my implementations to overcome some glitches. Now this C++ implementation doesn’t wrap around with large values of n. The python implementation also now overcomes the non-deterministic call order it suffered from in the past (credit to the tip goes to my colleague Abdelrahman Hussein).

So here is the C++ implementation:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;

long double fib(unsigned int n) {
	static map<unsigned int, long double> memo;
	if (n < memo.size()) {
		return memo[n];
	} else if (n <= 1) {
		memo[0] = 1;
		memo[1] = 1;
		return memo[n];
	} else { 
		long double x = fib(n-1) + fib(n-2);
		memo[n] = x;
		return x;
	}
}

int main(int argc, char **argv) {
	unsigned int n = 0;
	long double x = 0.0;
	time_t t1, t2;
	if (argc != 2) {
		cout << "Usage: fib <n>, where <n> is the input in f(n) = f(n-1) + f(n-2)" << endl;
		return 1;
	}

	n = atoi(argv[1]);

	t1 = time(NULL);
	x = fib(n);
	t2 = time(NULL);

	cout << "Time taken: " << t2 - t1 << endl;
	cout << "fib(n) = " << fixed << x << endl;
	return 0;
}

and here is the python implementation:

def fib(n, memo=None):
	if memo == None:
		memo = dict()
	if n in memo:
		return memo[n]
	elif n == 0 or n == 1:
		memo[n] = 1
	else:
		memo[n] = fib(n-1,memo) + fib(n-2,memo)
	return memo[n]

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print "Usage: fib <n>, where <n> is the input to f(n) = f(n-1) + f(n-2)"

	n = int(sys.argv[1])
	t1 = time.time()
	x = fib(n)
	t2 = time.time()
	
	print "Time Taken: ", t2 - t1
	print "fib(n) =",x

Running with the same scenarios, the python and the C++ implementations now probably perform as fast (although with this implementation I couldn’t get accurate numbers for the performance time of the C++ program), however I still have got some pet-peeves with Python:
1- The C++ reaches the maximum size of the table when n~=23600, while python segfaults at n=8585
2- I don’t like the fact that I have to explicitly set the maximum recursion depth in python, but rather it should -like Java for example- just throw an exception in the event of a stack overflow rather than at a pre-set depth.
3- I also don’t like that we had to pass the map as an argument to emulate the behaviour of static variables, I don’t understand the rationale behind the absence of such construct in python.

To Understand Recursion You Need To Understand Recursion…

After seeing this problem, and being a developer gradually falling in love with Python (or falling in love-hate with it to be precise). It occured to me “well, why not try to see what python could do?”

Of course, I didn’t want to use the straightforward recursive solution, since as my colleague, Ahmed Soliman, rightly concluded that recursion in Python is slow. (An interesting counter-argument can be found here as well).

To cut things short, I wrote a C++ and a Python implementation of the Fibonacci algorithm with memoization, and seemingly I had some interesting observations

Here is the C++ implementation:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;

unsigned long long fib(unsigned int n) {
	static map<unsigned long long, unsigned int> memo;
	if (n < memo.size()) {
		return memo[n];
	} else if (n <= 1) {
		memo[0] = 1;
		memo[1] = 1;
		return memo[n];
	} else { 
		unsigned long long x = fib(n-1) + fib(n-2);
		memo[n] = x;
		return x;
	}
}

int main(int argc, char **argv) {
	unsigned int n = 0;
	unsigned long long x = 0;
	time_t t1, t2;
	if (argc != 2) {
		cout << "Usage: fib , where  is the input in f(n) = f(n-1) + f(n-2)" << endl;
		return 1;
	}

	n = atoi(argv[1]);

	t1 = time(NULL);
	x = fib(n);
	t2 = time(NULL);

	cout << "Time taken: " << t2 - t1 << endl;
	cout << "fib(n)=" << x << endl;
	return 0;
}

Here are the inputs and outputs, note that some values might be incorrect due to overflows, but what matters here is the function call mechanism:

mohd@mohd-laptop:~$ ./fib_memo 40
Time taken: 0
fib(n)=165580141
mohd@mohd-laptop:~$ ./fib_memo 100
Time taken: 0
fib(n)=126979422405
mohd@mohd-laptop:~$ ./fib_memo 1000
Time taken: 0
fib(n)=2071492649197
mohd@mohd-laptop:~$ ./fib_memo 900
Time taken: 0
fib(n)=1862700660921

Now here is the python implementation:

memo = dict() # I don't like this, can't python just have static variables?

def fib(n):
	if memo.has_key(n):
		return memo[n]
	elif n == 0 or n == 1:
		memo[n] = 1
	else:
		memo[n] = fib(n-1) + fib(n-2)
	return memo[n]

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print "Usage: fib , where  is the input to f(n) = f(n-1) + f(n-2)"

	n = int(sys.argv[1])
	t1 = time.time()
	x = fib(n)
	t2 = time.time()
	
	print "Time Taken: ", t2 - t1
	print "fib(n)=", x

And here are my tests:

mohd@mohd-laptop:~$ python fib_memo.py 40
Time Taken:  0.000197887420654
fib(n)= 165580141
mohd@mohd-laptop:~$ python fib_memo.py 100
Time Taken:  0.00048303604126
fib(n)= 573147844013817084101
mohd@mohd-laptop:~$ python fib_memo.py 900
Time Taken:  0.00656604766846
fib(n)= 88793027306605937532517516910637647045239090036365766884466525589158360259770006891772711976920559280382807770394537471560061517120086971996377683290300054868066659454250625417891167369401
mohd@mohd-laptop:~$ python fib_memo.py 1000
File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
....
File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
    memo[n] = fib(n-1) + fib(n-2)
  File "fib_memo.py", line 11, in fib
RuntimeError: maximum recursion depth exceeded


So WTF? The recursion depth limit is somewhere near a 1000 function calls? To confirm my doubts I cracked open iPython with this scenario:

mohd@mohd-laptop:~$ ipython
/var/lib/python-support/python2.6/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated
  from sets import Set
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18) 
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from fib_memo import *

In [2]: fib(1000)

RuntimeError: maximum recursion depth exceeded

In [3]: fib(900)
Out[3]: 88793027306605937532517516910637647045239090036365766884466525589158360259770006891772711976920559280382807770394537471560061517120086971996377683290300054868066659454250625417891167369401L

In [4]: fib(1000)
Out[4]: 70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501L

Now python is happy because it has to do only 100 recursive calls to get fib(1000), which clearly shows that the recursion limit is much smaller than what’s offered by the C++ implementation, and even worse, it means that the function behaviour can change according to the order it receives arguments.

On the other hand, the C++ implementation suffers from the fact that lead to wrong return if they exceed the “long long” limit (how many bits is that on x86_64?), but at least the function behaviour is deterministic, and probably the only limiting factor to calling the function is when the size of the map grows too large to be held in the process’s address space hence causing a segfault.

Bottomline, yes, you can theoretically use recursion in Python, but neverthless you should worry A LOT about performance, and even if you find optimizations, be aware of how your functions can get called and that inputs don’t cause the function to exceed the recursion limits. In other words, just spare yourself the time and don’t use recursion in Python unless ABSOLUTELY necessary, othereise Python offers a lot of tricks to get your things done.

Design a site like this with WordPress.com
Get started