Friday, March 21, 2008

The elegance of Fortran

You know, the point of OOP is to SAVE work. 

Yes, the following is the most trivial and trite and pointless possible example of OOP.  I am not showing you code that is interesting. I am just comparing it with bolted-on OOP. Have a look at the Fortran equivalent. It's completely equivalent, note.

The "polymorphism" section form the Fortran is expressed in Python as an arbitrarily small amount of whitespace. And you thought whitespace was a drawback of Python!

def vectorsum(vec1,vec2):
...return map(sum,zip(vec1,vec2))

class Circle(object):
...def __init__(self,center,radius):
......self.center = center
......self.radius = radius

...def __str__(self):
......return "circle of radius %s at %s" % (self.radius, self.center)

...def move(self,translation):
......assert len(translation) == len(self.center)
......self.center = vectorsum(self.center, translation)

class Square(Circle):
...""" note: can inherit constructor for circle; treat radius as length"""
...def __str__(self):
......s = self.radius
......shifts = ((0,0),(0,s),(s,0),(s,s))
......corners = tuple([vectorsum(self.center,shift) for shift in shifts])
......return "square at points %s %s %s %s" % corners

4 comments:

David B. Benson said...

Fortran?

Maybe Python?

Anyway, why do not global circulation models use quadtrees or octtrees? Or do some of them?

Michael Tobis said...

aargh... it ate my indents.

No, go follow the link, see what the equivalent Fortran looks like.

I don't know what these trees you suggest are meant to do. Can you elaborate?

David B. Benson said...

That's Fortran over there? Certainly has deteriorated, hasn't it?

http://en.wikipedia.org/wiki/Quadtree

http://en.wikipedia.org/wiki/Binary_space_partitioning

would enable focusing computational power on regions with large magnitude derivatives while cutting down on the effort expendied in regions with little of interest happening.

Michael Tobis said...

re Fortran; yes F77 was a well-designed and useful platform and f90 and successors are disasters.

See http://www.fortranstatement.com

reading...