MNUM – Gauss

Posted: April 4th, 2008

# this requires numpy get it from http://numpy.sf.net

from copy import deepcopy
from numpy import *

# this function, swapRows, was adapted from
# Numerical Methods Engineering with Python, Jean Kiusalaas
def swapRows(v,i,j):
    """Swaps rows i and j of vector or matrix [v]."""
    if len(v) == 1:
        v[i],v[j] = v[j],v[i]
    else:
        temp = v[i].copy()
        v[i] = v[j]
        v[j] = temp

def pivoting(a, b):
    """changes matrix A by pivoting"""

    n = len(b)

    for k in range(0, n-1):
        p = int(argmax(abs(a[k:n, k]))) + k
        if (p != k):
            swapRows(b, k, p)
            swapRows(a,k,p)

def gauss(a, b, t=1.0e-9, verbose=False):
    """ Solves [a|b] by gauss elimination"""

    n = len(b)

    # make copies of a and b so as not to change the values in the arguments
    tempa = deepcopy(a)
    tempb = deepcopy(b)

    # check if matrix is singular
    if abs(linalg.det(tempa)) < t:
        return -1

    pivoting(tempa, tempb)

    for k in range(0,n-1):    
        for i in range(k+1, n):
            if tempa[i,k] != 0.0:
                m = tempa[i,k]/tempa[k,k]
                if verbose:
                    print "m =", m
                tempa[i,k+1:n] = tempa[i,k+1:n] - m * tempa[k,k+1:n]
                tempb[i] = tempb[i] - m * tempb[k]

    # Back substitution
    for k in range(n-1,-1,-1):
        tempb[k] = (tempb[k] - dot(tempa[k,k+1:n], tempb[k+1:n]))/tempa[k,k]

    return tempb

def residue(a, b, c):
    """Calculates the residue of a system solved by gauss elimination"""
    n = len(b)

    t = a * c # t is the A with the values of x replaced (an [n x n] matrix)

    s = []
    for i in range(0, n):
        s.append(sum(t[i])) # s is the solution

    res = b - s # res is the residue

    return res

#a = array([[1.0, 2.0, 0.0],[-1.0, 2.0, 3.0],[1.0, 4.0, 1.0]])
#b = array([3.0, -1.0, 4.0])
#a = array([[-1.414214, 2, 0],[1, -1.414214, 1], [0, 2, -1.414214]])
#b = array([1.0,1.0,1.0])
#a = array([[2.0, 2.0, 2.0],[1.0, 1.0, 5.0], [2.0, 5.0, 1.0]])
#b = array([6.0, 7.0, 8.0])
a = array([[1.001, 2.001, 3.001],[0.999, 2.0, 2.999], [1.002, 1.999, 2.999]])
b = array([4.003, 4.001, 3.999])

x = gauss(a, b)
print "Solution = ", x

#sol = linalg.solve(a, b)
#print "linalg Solution = ", sol

y = residue(a, b, x)
print "Residue = ", y

u = gauss(a, y)
print "Residue destribution = ", u

z = gauss(a, b+y)
print "New Solution (with added residue) = ", z

y2 = residue(a, b+y, z)
print "Residule of new solution = ", y2

if linalg.norm(y2) < linalg.norm(y):
    print "New solution has a smaller residue."
else:
    print "Original solution has a smaller residue."

Google Web Services for Free

Posted: March 31st, 2008

Dave Winer recently broke the news that Google will soon introduce a “Google Web Services”,  a competitor to Amazon Web Services. This isn’t much of a shock to anyone. The extra bit that is somewhat of a surprise  is the price: free. He then explains that the reason it will be free is that they can pay for it in the reduced cost of integrating new acquisitions into their infrastructures which would become, effectively zero.

I don’t agree with that. At least I don’t think that is the main reason. I think it’s just the same old business model – basic service is free, premium is paid. The same model you see in Google Apps and a lot of other business on the web.

The basic service will be limited, in terms of traffic and/or storage (file and DB) etc. This will be the most widely used by startups. The premium service will just be the same pricing model amazon – pay as you go, the only different is that you don’t start paying at zero usage but rather at a higher threshold. When a startup becomes successful its needs will grow exponentially. They will be using more than the maximum allowed for free, and they’ll need to pay. But that’s alright because now that they are successful they can afford it. And the 1% or so that will be successful will have subsidized everyone that didn’t make it and still give Google a nice profit. Let’s not forget that Google’s infrastructure is already here and even tens of thousands of failed ventures won’t make a dent in it. Successful ventures will generate enough cash to upgrade that same infrastructure. Specially as the cost of hardware continues to drop.

Off course I’m sure people at Google also thought of the acquisition factor. Past acquisitions have taken a lot of time to be integrated. I think it took something like a year for writely to become “Google Docs“. That’s a very long time on the web.

Combine the free GWS with the powerful web development frameworks like Ruby on Rails that allow single individuals to create useful applications quickly and the new marketting oppurtunities that the web2.0 has created and the cost of trying won’t be measured in millions, thousands or even hundreds of dollars. It will be measured in terms of hours – the hours you “wasted” trying. And that, in many cases, won’t even be “time wasted” but rather “experience gained”.

It’s a brave new world indeed.


JabberLogBot

Posted: March 31st, 2008

JabberLogBot is a jabber bot that records messages sent to it in a database.

The idea came from Nuno Dantas who wanted a jabber bot to record quick notes. He talked about it at a Prt.Sc dinner last Wennesday.

There’s also a simple PHP file in there that displays the data. That file is currently just for show as it is rather “plain”.

JLB Web Interface

UPDATED: added a screenshot of the web viewer.


iPhone SDK now includes Interface Builder

Posted: March 28th, 2008

icon_interfacebuilder.jpg The second beta version of the iPhone SDK is now available and includes Interface Builder, a powerful tool that allows you to visually build your interface and makes creating a UI as simple as drag and drop.

Screenshots:

ib_cocoa_touch.png ib_iphone
Download at iPhone Dev Center


Evernote Invites

Posted: March 27th, 2008

enlogo-beta.gif

I have 6 evernote (previously mentioned here) invites up for grabs. If you’re interested, drop a comment with your email.


Numerical Methods and Python

Posted: March 27th, 2008

I finally decided to take the Numerical Methods (MNUM) course. It turns out it’s a lot more fun than I thought. There is programming involved but you can chose to use whatever language you want. This is yet another nice excuse for me to use Python instead of C++ or Java. Last semester I was able to use Python to implement the game logic for Software Application Laboratory (LAS), which is mostly an OpenGL course with IPC via sockets thrown into the mix, and to write an article on dynamic languages (focusing mostly on Python) for Software Engineering (ESOF).

But back to this semester, 3 classes into the semester and the teacher is already said something like “I’m going to learn python now. I didn’t believe when I heard someone saying it was the best language in the world, but now I see there might be some truth to that claim”. That and I suspect his next laptop might be a macbook but that’s another story.

There are a few things that make Python great for Numerical Methods. In my opinion, Python’s clear, easy to understand, syntax is the most important one.It makes algorithms easier to implement. The syntax ends up being very close to language neutral pseudocode available in numerical methods books. Also Python’s datatypes as well as those provided by other libraries can be very useful.

The following code implements the stuff in chapter 2 (determining zeros) of the course. The methods implemented are Bisection, Rope and Newton. The function returns both the solution and the number of iterations necessary to get to that solution.

UPDATE: forgot the book - Numerical Methods in Engineering with Python

Appendix A – mnum2.py

from math import log

def bisect(f, a, b, e):
	""" Determines zero between a and b using Bisection. """
	n = 0
	fa = f(a)
	if fa == 0.0: return (a, n)
	fb = f(b)
	if fb == 0.0: return (b, n)

	while (abs(a-b) > e):
		c = 0.5*(a+b)
		fc = f(c)

		if fc == 0.0: return (c, n)
		n = n + 1
		if fb*fc < 0.0:
			a = c
			fa = fc

		else:
			b = c
			fb = fc

	if fa < fb:
		return (a, n)
	else:
		return (b, n)

def rope(f, a, b, e):
	""" Determines zero between a and b using the Rope methode. """
	n = 0
	fa = f(a)
	if fa == 0.0: return (a, n)
	fb = f(b)
	if fb == 0.0: return (b, n)

	while (abs(a-b) > e):
		c = (a*fb - b*fa) / (fb - fa)
		fc = f(c)
		if fc == 0.0: return (c, n)
		n = n + 1
		if fb*fc < 0:
			a = c
			fa = fc

		else:
			b = c
			fb = fc

	if fa < fb:
		return (a, n)
	else:
		return (b, n)

# Note: must verify that for the function f and guess c
#		the method will _converge_.
def newton(f, df, c, t):
	""" Determines zero between a and b using Newton """
	n = 0
	fc = f(c)
	if fc == 0.0: return (c, n)

	while (True):
		fc = f(c)
		dfc = df(c)
		if dfc == 0:
			print "dfc is 0"
			return (0, -1)

		dc = -fc/dfc

		c = c + dc
		n = n + 1
		if abs(dc) < t: return (c, n)

##Tests
#def f(x): return -log(x)+4.0
#def df(x): return -1.0/x
#x= bisect(f, 1, 70, 0.00000001)
#print x
#x = rope(f, 1, 70, 0.00000001)
#print x
#x = newton(f, df, 0.1, 0.0001)
#print x

Min and Max – An explanation

Posted: March 26th, 2008

Ok, from the comments on the previous post it seems people are NOT “getting it”. This is NOT a “problem”, this is funny.

So I guess people don’t know what the functions min() and max() do or are just confusing them

max([...]) - this function usually takes a list of numbers and returns the highest number in the list.

e.g.

if x belongs to [0,20] then y = max(x, 18) => y belongs to [18-20]

max(20,18) = 20
max(19, 18) = 19
max(10, 18) = 18

min([...]) - this function usually takes a list of numbers and returns the lowest number in the list.

e.g.

if x belongs to [0,20] then y = min(x, 18) => y belongs to [0-18]

min(20,18) = 18       [<- fixed thanks Mind Booster Noori]
min(19, 18) = 18
min(10, 18) = 10

This means that according to the function the function displayed in my previous post, according to that function, ALL students grades will be between 18 and 20. That means if you have a freaking 0 in the exam and a 0 in the Assignment you’ll get an 18/20 (which is an excellent grade). i.e. THE MINIMUM grade is 18. It’s a simple mistake in the equation. What the page should say to be correct is

Nota Final = min(Nota do exame final+Nota do trabalho, 18)<- this would be the correct equation

And hell this was posted under “Entertainment”.

So the previous post wasn’t supposed to be about something “unfair” it was supposed to be about making fun of a simple mistake that under the circumstances really is funny.


FEUP Maximizing Student Grades For A Change

Posted: March 25th, 2008

Or not. Someone got min() and max() confused:

Maxing Student Grades

Nota Final = max(Nota do exame final+Nota do trabalho, 18)

translation: Final Grade = max(Exam Grade + Assignment Grade, 18)
[Note: it’s 18/20 – 20 is the maximum grade possible)

Than again, maybe it’s not  a mistake. Perhaps the teacher really enjoys giving high grades in the 18-20 range. Now wouldn’t that be different…

 Link to the page

UPDATE: Read my next post for an explanation of why this is funny.


Out of WordPress.com and Into Evernote.com

Posted: March 24th, 2008

I mentioned in a previous post that I was using a private WordPress blog to keep my notes. Not anymore. I migrated to Evernote.

Thanks to Maria Joao Valente for sending me the invite to evernote.

Evernote is a note organizers, similar to Journler which I used a while back.

Check out the About Evernote and their screencast. My highlights:
* Web client
* Desktop client
* Works with Mobile Devices
* Painless, automatic synchronization (think gmail + IMAP but better)
* Notes can be found by searching and filtering for text within images
* Clip (via bookmarklet) or email entire webpages into your account
* Can import html files (you’ll see why this was important for me)

See also: Wired Review and TUAW Review.

Migrating between applications has never been an easy task. In this case I need to migrate from a WordPress blog to evernote. I could manually click “Clip to Evernote” for each post on that blog or I could’ve written a simple AppleScript to do it or I could probably have found a way to do it in Javascript or I could’ve taken advantage of the “clip” thing in another way. But off course I choose the hardest way possible – I wrote a python script to convert the WordPress XML Export File to multiple HTML notes and then dragged those files to evernote. At least it was fun if a colossal waste of time…

Anyway here’s the python script in case you ever want to convert a wordpress blog (or more accurately a WordPress XML Export File) to html files.

wpdepress.py


# Copyright (c) 2008 Luis Rei
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Notes:
# - currently does not handle images, attachments or comments
# - was only tested on MacOS X (10.5)
# - not "carefully" developed e.g. poor exception handling, little testing, ...
# - see also http://wordpress.com/blog/2006/06/12/xml-import-export/

import string, os, sys, getopt
from xml.dom import minidom

__author__ = 'Luis Rei (luis.rei@gmail.com)'
__homepage__ = 'http://luisrei.com'
__version__ = '1.0'
__date__ = '2008/03/23'

def convert(infile, outdir, authorDirs, categoryDirs):
    """Convert WordPress Export File to multiple html files.

    Keyword arguments:
    infile -- the location of the WordPress Export File
    outdir -- the directory where the files will be created
    authorDirs -- if true, create different directories for each author
    categoryDirs -- if true, create directories for each category

    """

    # First we parse the XML file into a list of posts.
    # Each post is a dictionary

    dom = minidom.parse(infile)

    blog = [] # list that will contain all posts

    for node in dom.getElementsByTagName('item'):
    	post = dict()

    	post["title"] = node.getElementsByTagName('title')[0].firstChild.data
    	post["date"] = node.getElementsByTagName('pubDate')[0].firstChild.data
    	post["author"] = node.getElementsByTagName(
    	                'dc:creator')[0].firstChild.data
    	post["id"] = node.getElementsByTagName('wp:post_id')[0].firstChild.data

    	if node.getElementsByTagName('content:encoded')[0].firstChild != None:
    	    post["text"] = node.getElementsByTagName(
    	                    'content:encoded')[0].firstChild.data
    	else:
    	    post["text"] = ""

    	# wp:attachment_url could be use to download attachments

    	# Get the categories
    	tempCategories = []
    	for subnode in node.getElementsByTagName('category'):
    		 tempCategories.append(subnode.getAttribute('nicename'))
    	categories = [x for x in tempCategories if x != '']
    	post["categories"] = categories

    	# Add post to the list of all posts
    	blog.append(post)

    # Then we create the directories and HTML files from the list of posts.

    # The "base" directory
    outdir += "/wordpress/"
    if os.path.exists(outdir) == False:
        os.makedirs(outdir)
    os.chdir(outdir)

    for post in blog:
        # The "category" directories
        path = ""
        if authorDirs == True:
            path += post["author"].encode('utf-8') + "/"

        # This creates a path for the file in the format
        # category1/category2/category3/file. Note that the category list was
        # sorted.

        if categoryDirs == True:
            if (post["categories"] != None):
                path += string.join(post["categories"],"/")

        if os.path.exists(path) == False and path != "":
            os.makedirs(path)

        # And finally the file itself
        path = outdir + path
        title = post["title"].encode('utf-8')
        filename = path + "/" + post["id"] + ' - ' + title \
                    + '.html'

        # Add a meta tag to specify charset (UTF-8) in the HTML file
        meta = """"""

        f = open(filename, 'w')
        f.write(meta+"\n")

        # Add "HTML header"
        start = "\n\n\n\n\n"
        f.write(start)

        # Convert the unicode object to a string that can be written to a file
        # with the proper encoding (UTF-8)
        text = post["text"].encode('utf-8')

        # Replace simple newlines with
 + newline so that the HTML file
        # represents the original post more accuratelly
        text = text.replace("\n", "
\n")

        f.write(text)

        # Finalize HTML
        end = "\n\n"
        f.write(end)

        f.close()

def usage(pname):
    """Displays usage information

    keyword arguments:
    pname -- program name (e.g. obtained as argv[0])

    """

    print """python %s [-hac] [-o outdir] infile
    Converts a WordPress Export File to multiple html files.

    Options:
        -h,--help\tDisplays this information.
        -a,--authors\tCreate different directories for each author.
        -c,--categories\tCreate directory structure from post categories.
        -o,--outdir\tSpecify a directory for the output.

    Example:
    python %s -c -o ~/TEMP ~/wordpress.2008-03-20.xml
        """ % (pname, pname)

def main(argv):
    outdir = ""
    authors = False
    categories = False

    try:
		opts, args = getopt.getopt(
		    argv[1:], "ha:o:c", ["help", "authors", "outdir", "categories"])
    except getopt.GetoptError, err:
		print str(err)
		usage(argv[0])
		sys.exit(2)

    for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage(argv[0])
			sys.exit()
		elif opt in ("-a", "--authors"):
			authors = True
		elif opt in ("-c", "--categories"):
		    categories = True
		elif opt in ("-o", "--outdir"):
		    outdir = arg

    infile = "".join(args)

    if infile == "":
	    print "Error: Missing Argument: missing wordpress export file."
	    usage(argv[0])
	    sys.exit(3)

    if outdir == "":
	    # Use the current directory
	    outdir = os.getcwd()

    convert(infile, outdir, authors, categories)

if __name__ == "__main__":
	main(sys.argv)

School Days

Posted: March 13th, 2008

School Days

From anime.mikomi.org via anidb:

A shy and unconfident teenager has become very accepting at only stealing glances at a cute girl as they ride the same train each morning on their way to their high school. At least until Saionji Sekai surprises Itou Makoto and chances to get a peek at a photo-snap of the beautiful Katsura on his cell-phone. After evilly teasing Mokoto about his forlorn crush, Sekai offers to support him in his romance. So after many Makoto-pep-talks and lots of scheming, the three become close friends. Close enough that Makoto braves asking Katsura out on date. And she agrees. Later that day, Makoto finds a quiet moment express his thanks for all support that Sekai has offered him, and makes clear his intent on finding a way to repay her kindness. But he is totally unprepared and very surprised at the reward that Sekai has chosen. Sekai steals a long kiss from his lips, before breaking away to run for the train. Sekai turns and wishes Makoto well on his date.

Not my usual type of show so I liked it partially for being different from what I usually watch. I gave it at rating of 8 at anidb.

The ending, “nice boat” (it’s a joke, you’ll get it if you watch it), was awesome!


Services Left Behind

Posted: March 11th, 2008

I’ve recently started using my macbook as my only personal computer.

One of the big side effects is that I no longer need to synchronize multiple computers running different Operating Systems. This had been one of my major headaches with computing. My solution was mostly rsync/ssh synchronization of certain directories with my home server and a switch to web applications.

So, now that the need is gone, I’m migrating back to conventional desktop applications.

Gmail
The first web app I started using. I tried to migrate to thunderbird but I simply don’t like thunderbird. So I’m now using Apple’ Mail App.
I still use the gmail service, I just don’t use the web interface regularly.

Google Calendar
The second web app I started using. I like it a lot but it did make 2-way synchronization with my cellphone and palm (up until Palm Desktop stopped working) painful. Yes there is software around to do it. BusySync looks awesome but I don’t think there’s any reason for me to pay money to use a service I don’t really need anymore. GCALDaemon is Free but I was unable to get it properly configured plus I just don’t trust software that’s hard to setup.
I now use just Apple’s iCal to manage my calendars and in case you’re curious I used the BusySync trial to migrate – it was painless.

Remember The Milk
Less known than the google services, Remember The Milk is an online task manager. I started using it less than a year ago – only after Palm Desktop stopped working. Before that, I used just my Palm TX and I had started using iSync to synchronize my Palm and my cellphone. The side effect was I couldn’t use 2-way sync with my cellphone which isn’t that much of a problem as it seems because I don’t really like using my cellphone for that. Still, there’s just no point in using RTM anymore, so I now use iCal/Mail App to handle my tasks. Since I only had a handful of tasks to complete, I migrated by hand.

What’s next?
I’ll consider switching feed readers from google reader to NetNewsWire. Migrating is painless since I can just download my OPML from google reader

I use a private blog as a notepad of sorts. I previously used just regular .txts in a specific directory and then switched to Journler. But Journler wasn’t available on Linux.
Maybe I’ll continue to use the blog for my longer notes but switch to Mail App for smaller ones. Maybe not. I’ll need to think about it and try it.
Plus there’s the issue of wether it’s gonna be easy to get the data into whatever application I choose. I migrated from Journler to a wordpress blog via an Automator script and MarsEdit.

The Elephant In The Blog
The iPhone. With the SDK out and App Store, I want one. So I now have to think what will work better with the iPhone. For instance, how good is the Newsgator iPhone web app compared to gReader? What’s the best way to synchronize my notes with the iPhone? Etc. I’ll want to speak with iPhone users about this.


Warning: Internet Forums Ahead

Posted: February 27th, 2008

Sort of the Greater Internet Fuckwad Theory applied to internet forums by CAD:

Intelligence is Optional


HD Wars Update – WB goes Blu-Ray

Posted: January 5th, 2008

Warner Brothers dropped HD-DVD and went Blue-Ray only.  This is how things look now:

HD Share Chart

(Source: Wikipedia)


Deconstructing Xbox 360 Security

Posted: January 1st, 2008

Almost one year ago I blogged Deconstructing Xbox Security, this year there’s Deconstructing Xbox 360 Security (24c3-2279-en-deconstructing_xbox_360_security.mkv) presented at 24C3. Link to 24C3 video recordings.


2007

Posted: December 31st, 2007

2007 – What we have to show for it (besides Halo 3).