Friday, March 02, 2007

Gram-Schmidt in Factor

Here is the Gram-Schmidt algorithm in Factor:

: proj ( v u -- w )
[ [ v. ] keep norm-sq / ] keep n*v ;
: (gram-schmidt) ( v seq -- newseq )
dupd [ proj v- ] each-with ;
: gram-schmidt ( seq -- orthogonal )
V{ } clone [ over (gram-schmidt) over push ] reduce ;
: norm-gram-schmidt ( seq -- orthonormal )
gram-schmidt [ normalize ] map ;


I coded this because I thought I needed this for something I'm working on, and it turns out I did not, so I made a blog entry from it instead. Pretty freaky, huh? However if this ends up being useful, I'll re-implement the numerically-stable variant.

1 Comments:

At 1:18 PM, Blogger Michael said...

You are indeed a freak, B. You can take the boy out of Microsoft, but...

 

Post a Comment

<< Home