Archive for the 'Computer Science' Category

A simple FFT in Python

Tuesday, October 9th, 2007

I wrote this to check my pen-and-paper FFT (Fast Finite Fourier) problems for my Algorithms and Data homework. It follows a pseudocode implementation given in my textbook, from which I have no idea where it was derived from.

#!/sw/bin/python
i = complex(0,1)
ni = complex(0,-1)

def fft(a,w):
	if (w==1):
		return a
	s =  fft([a[z] for z in range(0,len(a),2)], w**2)
	sp = fft([a[z] for z in range(1,len(a),2)], w**2)
	n = len(a)
	r=[]
	for j in range(0,(n/2)):
		r.insert(j, (s[j] + (w**j * sp[j])))
		r.insert(j+(n/2), s[j] - (w**j * sp[j]))
	return r

def mult(poly1, poly2):
	a= len(poly1)
	b= len(poly2)
	if a > b:
		for x in range(0, a-b):
			poly2.append(0)
	if b > a:
		for x in range(0, b-a):
			poly1.append(0)
	fftval1 = fft(poly1,i)
	fftval2 = fft(poly2,i)
	multip = [fftval1[x]*fftval2[x] for x in range(0,len(fftval1))]
	inverse = [ (1.0/(len(multip))*x).real for x in fft(multip,ni)]
	return inverse

#test
assert mult([1,1,2,0], [2,3,0,0]) == [2,5,7,6]

Apparently there is a fully featured FFT library in scipy, so really I am just happy to have something on my own that works for my needs.

Still happy with Apple.

Monday, September 24th, 2007

First, let me preface this that I’ve wanted a PowerMac (and then subsequently a Macbook Pro) ever since I arrived at college. It’s clear that they were powerhouse mobile computers, and they were sexy at that.

I finally got myself a MacBook about year ago. It has replaced my PC, which is now in pieces in the closet. It’s totally amazing, and for the few things that I’ve still needed Windows for, Parallels desktop has filled the void.

A year later, though, I started running into some issues. My Mighty Mouse fell off the coffee table (about 20″) and broke. That was a quick $70 down the drain. Then the LCD backlight started flickering randomly when on low brightness. And then the power supply refused to charge the battery. And then the top panel that rests over the keyboard started to warp, and it cracked on the edges.

I could not believe all this. So I took it in to the local Genius Bar, and had it serviced. I also brought in my broken Mighty Mouse.

A week later, for the total cost of $0, I have now:

- New keyboard installed, bonus
- New trackpad installed (1 year of finger oil stains gone), bonus
- New keyboard panel installed - replaced cracked edges
- New PMU installed (Power management unit, fixes battery charging issue)
- New MLB installed (Main Logic Board, fixes LCD backlight flicker)
- New Mighty Mouse

I’m so happy…my laptop now looks and feels new again for free (one week without the laptop was painful, though). If I choose to sell the laptop, I can now advertise it as being in excellent condition, rather than fair. I wouldn’t mind upgrading to a Macbook Pro as a graduation present to myself, perhaps.

Cheers, Apple. I’m still a happy customer now. My faith was shaken in the quality, but the service was exceptional. Thanks!

PayPal’s NVP API - sample Python wrapper

Friday, September 14th, 2007

I was unable to find any Python samples for using the PayPal NVP API, so I went to work writing a helper class for my needs. Hopefully this should be enough to help get others started at least.

This is not a complete reference! Be sure to understand what this class is doing before you try it on production servers! …use it at your own peril.

http://python.pastebin.com/f782d48d9

Also be sure to check the complete reference doc for any error codes returned, as the doc is very helpful.

Update April 3rd, 2008: I’m pleased to discover that this has made its way around the internet:
The Freesound Project based out of Belgium is reimplementing their site in Python and they are using my reference.
A Google Code project called PyPayPal is using it as a reference.
GoCept, a German based company, seems to have implemented it in their content management system.

MassWrestling.com gets about 90k requests to its RSS feeds every month, which eats up about 3GB of bandwidth transfers and puts extra load on the web server.

I saw a tip on Coding Horror Blog that it is a great idea to rewrite your RSS feeds to be syndicated by Feedburner. Unfortunately, the example given doesn’t quite work for MassWrestling.com’s e107 feed URLs:

/cms/e107_plugins/rss_menu/rss.php?forumposts.2

So I spent some time relearning Apache’s mod_rewrite, and came up with the following lines to add to my .htaccess files:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !FeedBurner
RewriteCond %{QUERY_STRING} ^forumposts\.1$
RewriteRule ^rss\.php$ http://feeds.feedburner.com/masswrestling/posts_rss? [R=301]

This rewrite sends all requests that are not FeedBurner’s http agent to Feedburner’s copy of my feed. The problem was figuring out that the question mark after the rss.php? is actually not matchable in the RewriteRule condition. Instead, you have to match it in a RewriteCond against the %{QUERY_STRING} variable. The replacement pattern requires a question mark at the end of it to ensure that mod_rewrite does not pass on the extra paremeters to the new URL.

So now:

rss.php?forumposts.2

redirects (301, permanently moved) to:

http://feeds.feedburner.com/masswrestling/posts_rss2

Let’s hope it knocks out the extra RSS traffic I have.

How many licks does it take

Saturday, November 25th, 2006

It takes 1 click to download Firefox from GetFirefox, a website with big words and very little text, but it takes 4 or more clicks to get OpenOffice from OOo’s website after being presented with a barrage of information and donation requests.

This is definitely an indication of how to make open source software succeed en masse: Make it brain dead simple to acquire and use for the average user.

Firefox can’t DOEY!

Thursday, September 7th, 2006

If you are writing an XSL for some particular XML data you have to be outputted into HTML, you may run into a problem when you view your XML data with your XSL transformation applied to it in Firefox. The reason for this is because the Firefox developers have chosen not to implement part of the XSLT specification for disable-output-escape (DOEY).

To get around this, one must make quite a bit of changes to their XSL to accomodate Firefox. This smells alot like what alot of people have to do often for MSIE, except with Firefox being the ugly one this time.

Click to see my example XSL sheet below for a rough reference on how this can be achieved.

(more…)

Installing a local copy of a Drupal website

Wednesday, July 26th, 2006

Here are the steps you need to take to get a copy of a Drupal website onto your local computer for development purposes, using the XAMPP stack and a variety of tools. Many of the steps taken here have been made so that you can keep your PHP files outside the standard htdocs DocumentRoot directory of the Apache server while maintaining whatever else you have running on your XAMPP installation.

(more…)

I spent a good (read: probably too much) amount of time getting the spellchecker in TinyMCE to work on my Tomcat (JSP) application. The problem with this plugin is that it’s source code runs on PHP - which Tomcat can’t interpret on it’s own.

Instead, I happen to have another web server daemon (lighttpd/php) running on port 81 already to support a phpMyAdmin installation. Unfortunately, I had previously compiled PHP without XML support, so this involved some hacking.

(more…)

Pit and the Serveroom

Wednesday, May 31st, 2006

BEEP — still unceasingly — still inevitably BEEP! I gasped and struggled at each BEEP. I shrunk convulsively at its every BEEP. My ears followed its outward or upward whirls with the eagerness of the most unmeaning despair; they closed themselves spasmodically at the sound, although death would have been a relief, oh, how unspeakable! I still quivered in every nerve to think how slight a beeping of the machinery would precipitate that keen, glistening madness upon my bosom. It was hope that prompted the nerve to quiver — the frame to shrink. It was hope — the hope that triumphs on the server rack — that whispers to the death-condemned even in the dungeons of the Inquisition.

DailyWTF’ed

Monday, May 22nd, 2006


So I sent in a story to DailyWTF, and the editor (severely) dramaticized the story and even photoshopped the screenshot I sent in, but so what! The “WTF” part is true at least, even if the facts about it were warped. It’s probably better that the facts were warped anyways.

It’s a Little Funny