Function Repository Resource:

ProjectionMatrix

Source Notebook

Compute the projection matrix for a given vector space

Contributed by: Rauan Kaldybayev

ResourceFunction["ProjectionMatrix"][A]

returns the projection matrix onto the column space (range) of an m×n matrix A, with mn.

ResourceFunction["ProjectionMatrix"][A,W]

returns the projection matrix onto the column space (range) of A with a specified metric W.

Details

The metric W should be a positive-definite matrix.
ResourceFunction["ProjectionMatrix"][A,W] returns the projection matrix for the case when the inner product of two vectors x and y equals xT.W.y for some positive-definite matrix W.

Examples

Basic Examples (3) 

Compute the projection matrix onto the column space of a matrix:

In[1]:=
ResourceFunction["ProjectionMatrix"][({
   {1, 4},
   {2, 5},
   {3, 6}
  })]
Out[1]=

Project a vector onto this vector space:

In[2]:=
ResourceFunction["ProjectionMatrix"][({
    {1, 4},
    {2, 5},
    {3, 6}
   })] . {3, -4, 5}
Out[2]=

Visualize:

In[3]:=
With[{A = ({
     {1, 4},
     {2, 5},
     {3, 6}
    }), x = {3, -4, 5}}, Graphics3D[{{InfinitePlane[Transpose@A] (* column space of A *)}, {Arrow[{{0, 0, 0}, x}]  (* the vector x *) }, {Arrow[{{0, 0, 0}, ResourceFunction["ProjectionMatrix"][A] . x}] (* projection of x *)}}]]
Out[3]=

Scope (3) 

Compute the projection matrix for the case when the dot product between a pair of vectors x and y is given by xT.W.y:

In[4]:=
ResourceFunction["ProjectionMatrix"][({
   {1, 4},
   {2, 5},
   {3, 6}
  }), ({
   {1, -1/2, 0},
   {-1/2, 1, 0},
   {0, 0, 1}
  })]
Out[4]=

Compute the projection matrix onto the column space of a singular square matrix:

In[5]:=
ResourceFunction["ProjectionMatrix"][({
   {1, -1},
   {-1, 1}
  })]
Out[5]=

Compute the projection matrix onto the column space of a symbolic real-valued matrix:

In[6]:=
Simplify[ResourceFunction["ProjectionMatrix"][({
    {a},
    {b},
    {c}
   })], {a, b, c} \[Element] Reals]
Out[6]=

Properties and Relations (2) 

ProjectionMatrix[Transpose@{v}].u is equivalent to Projection[u,v]:

In[7]:=
With[{u = {1, 2, 3}, v = {4, 5, 6}}, {ResourceFunction["ProjectionMatrix"][
    Transpose@{v}] . u, Projection[u, v]}]
Out[7]=

ProjectionMatrix[Transpose@{v},W].u is equivalent to :

In[8]:=
With[{u = {1, 2, 3}, v = {4, 5, 6}, W = ({
     {1, -1/2, 0},
     {-1/2, 1, 0},
     {0, 0, 1}
    })}, {ResourceFunction["ProjectionMatrix"][Transpose@{v}, W] . u, Projection[u, v, Function[{x, y}, x . W . y]]}]
Out[8]=

Possible Issues (1) 

ProjectionMatrix can fail with badly conditioned numerical matrices. Consider Läuchli’s example. The output of the function may differ depending on whether the input is given in exact numbers or in MachinePrecision numbers:

In[9]:=
N@With[{n = 3, \[CurlyEpsilon] = 2^-52}, ResourceFunction["ProjectionMatrix"][
   Prepend[\[CurlyEpsilon] IdentityMatrix[n], ConstantArray[1, n]]]]
Out[9]=
In[10]:=
With[{n = 3, \[CurlyEpsilon] = 2^-52}, ResourceFunction["ProjectionMatrix"][
  N@Prepend[\[CurlyEpsilon] IdentityMatrix[n], ConstantArray[1, n]]]]
Out[10]=

Publisher

Wolfram Summer Camp

Version History

  • 1.0.0 – 19 January 2022

License Information