Residual of planefitting using svd

Note to self: Find out about residuals of planefits using singular value decomposition. And while you're at it: find out, how least square fitting works with singular value decomposition anyhow. Right now - as with any transformation matrix bigger than 3x3 - i mostly think it's magic..

Here is my current planefitting code in python:

PYTHON:
  1. import numpy as np
  2. from Geometry import Point, Line, Plane
  3.  
  4. def fitplane(XYZ):
  5.     [rows,npts] = XYZ.shape
  6.    
  7.     if not rows == 3:
  8.         print XYZ.shape
  9.         raise ('data is not 3D')
  10.         return None
  11.  
  12.     if npts <3:
  13.         raise ('too few points to fit plane')
  14.         return None
  15.  
  16.     # Set up constraint equations of the form  AB = 0,
  17.     # where B is a column vector of the plane coefficients
  18.     # in the form   b(1)*X + b(2)*Y +b(3)*Z + b(4) = 0.
  19.     t = XYZ.T
  20.     p = (np.ones((npts,1)))
  21.     A = np.hstack([t,p])
  22.  
  23.     if npts == 3:                       # Pad A with zeros
  24.         A = [A, np.zeros(1,4)]
  25.  
  26.     [u, d, v] = np.linalg.svd(A)        # Singular value decomposition.
  27.     #print v[3,:]
  28.     B = v[3,:];                         # Solution is last column of v.
  29.     nn = np.linalg.norm(B[0:3])
  30.     B = B / nn
  31.     plane = Plane(Point(B[0],B[1],B[2]),D=B[3])
  32.     return plane

The geometry-module is the one i've posted on github. It enables me to do fast transformations and - the most important part - intersections between planes and lines. Go check that out. I would like to hear some feedback..

Back to the problem: I would like to also return a measure of error or residual. If anyone could help..

Update (25.10.2010): There was a small but still significant error in the algorithm. I assumed that B[0:2] returns the elements [B[0],B[1],B[2]], but it appears the slicing in python is in line with the syntax for a "for"-statement. So, the line with the norm had to be changed to

PYTHON:
  1. nn = np.linalg.norm(B[0:3])

On another note, i'd have to look up the np.linalg.solve()-function which should be able to solve my problem a little bit more efficiently.

Nice Fourier poem

A poem written in honor of the ever-enlightening Fourier-transform, which is often hated by students, mainly because they don't know what it means.. Here is to the Fourier-transform:

One day in a land far away
Some mathematicians at play
Found a transform of convenient form
The basis of physics today.

Convolving would wreck people's brains
Still the advent of Fourier domains
for convolving in one
means multiplication
in the corresponding domain.

Got trouble with an ODE?
Fourier transforms will set you free
When once you would cry,
You now multiply,
by a constant times the frequency.

Fourier transforms backwards and forth
I hope that you now see their worth
For in every domain
Advantages reign
Fourier was the salt of the earth.

by Luke Krieg
Posted to sci.physics, 11 Sep 2000

Thanks to Amara Graps (i was reading on wavelets..).

Wolfram Alpha goes public

Wolfram Alpha is finally the computer we were waiting for. Not only does it compute mathematical problems really fast, but it tries to give us everything about the answer, that we would like to find out. It's like a wikipedia - just with more accurate information. Also, WolframAlpha is able to interpret user input and find out what the user is trying to find out by searching for the terms of a query in a semantic database: Weather in Guatemala, President in Germany or C6H6O.

Below the answer set that WolframAlpha delivers, there is the option for an output as pdf-file and a rather general information of where the information came from.