What's the best way to compute the orthogonal projection of a vector
to the row space of a vector?

I have a matrix sp whose rows are vectors in a euclidean space, and a
vector ve.

   sp=: 4 _9 _4 ,: 4 _3 _2
   ve=: _1 _7 12

(Note.  For larger example inputs, try (sp=: 10-?4 10 $ 21 [ ve=: 10-?10 $ 21))

I'd like to compute the orthogonal projection of ve to the subspace
spanned by the rows of sp.  The subspace can be of any dimension, and
the rows of sp might not be independent.

One way to do this would be using the domino verb

   ve ((] +/ .* %.) |:) sp
_4 _3 8.88178e_16

but this won't work if the rows of the matrix are linearly dependent,
or are close to that:

   1 1 1 ((] +/ .* %.) |:) 0 0 1 ,: 0 0 2
|domain error
|   1 1 1    ((]+/ .*%.)|:)0 0 1,:0 0 2
   1 1 1 ((] +/ .* %.) |:) 0 0 1 ,: 0 1e_20 1
|domain error
|   1 1 1    ((]+/ .*%.)|:)0 0 1,:0 1e_20 1

(Would it perhaps make sense to add (]+/ .*%.) as a new dwim special code?)

The following has the same problem:

   (}:"1@:[ +/ .* }:@:({:"1)@:])&:>/ 128!:0 |:sp,ve
_4 _3 6.10623e_16

Another way would be this horrible mess which works even in the degenerate case:

   ve ([-[:{.[:((],~[-[:+/+/ .*~*(%+/&:*:"1)@]),:^:(2>#...@$))/,) sp
_4 _3 0

Still, I think there must be some nicer way to do an orthogonal
projection.  Please help me find a better expression.  Would eg.
pulling in one of the least square functions from lapack or gsl might
do this straightaway?

Ambrus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to