Thursday, February 22, 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 weblog entry from it instead. :-) However if this ends up being useful, I'll re-implement the numerically-stable variant.

No comments: