Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Project a vector onto a subspace
| ResourceFunction["ProjectOnSubspace"][u,v] finds the projection of the vector u onto the vector v. | |
| ResourceFunction["ProjectOnSubspace"][u,v,ip] finds the projection using the inner product ip. | |
| ResourceFunction["ProjectOnSubspace"][u,{v1,v2,…}] finds the projection of u onto the subspace spanned by {v1,v2,…}. | |
| ResourceFunction["ProjectOnSubspace"][u,{v1,v2,…},ip] finds the projection onto the subspace using the inner product ip. | 
The projection of one vector onto a 1D subspace:
| In[1]:= | ![ResourceFunction["ProjectOnSubspace"][{1, 1}, {{2, 1}}]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/00dedcdc6c53ed0e.png) | 
| Out[1]= |  | 
This is the same as Mathematica’s Projection command, which projects one vector onto another:
| In[2]:= | ![Projection[{1, 1}, {2, 1}]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/4b230abc07ddf45d.png) | 
| Out[2]= |  | 
The projection of a vector onto a subspace spanned by a list of vectors:
| In[3]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{-1, -1, 1, 1}, {{1, 0, 1, 1}, {0, 1, 1, 2}, {2, -2, 0, -2}}]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/4be1043b53fbfe04.png) | 
| Out[3]= |  | 
The projection using a weighted inner product:
| In[4]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{-1, -1, 1, 1}, {{1, 0, 1, 1}, {0, 1, 1, 2}, {2, -2, 0, -2}}, ({1, 2, 3, 4} #1) . #2 &]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/04e942869bf1e15b.png) | 
| Out[4]= |  | 
Vectors with symbolic entries are assumed to represent complex numbers:
| In[5]:= | ![ResourceFunction[
  "ProjectOnSubspace"][{a, b}, {{c, d}}] // FullSimplify](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/36612996a802f4bc.png) | 
| Out[5]= |  | 
By using Dot as the inner product, Mathematica will assume all symbolic entries are real:
| In[6]:= | ![ResourceFunction["ProjectOnSubspace"][{a, b}, {c, d}, Dot]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/719e76a53b179df7.png) | 
| Out[6]= |  | 
Project a vector in  onto a subspace of
 onto a subspace of  :
:
| In[7]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{I, 1 + I, 3 - 2 I, -2}, {{2 - 2 I, 2 - 2 I, -2, -2 + I}, {-I, -2 + I, -2 + 2 I, I}}]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/14546dbb6e036f0c.png) | 
| Out[7]= |  | 
Project a vector in  onto a subspace of
 onto a subspace of  having an orthogonal basis:
 having an orthogonal basis:
| In[8]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{1, 1 + I, 3 - 2 I}, {{-((1 - I)/Sqrt[11]), (2 + I)/Sqrt[
   11], -((2 I)/Sqrt[11])}, {(2 - I/3)/Sqrt[11], (1/3 - I)/Sqrt[11], (
   2 - (4 I)/3)/Sqrt[11]}}]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/5acad946da0d8a4e.png) | 
| Out[8]= |  | 
Verify that the basis is orthonormal:
| In[9]:= | ![Outer[Conjugate[#1] . #2 &, {{-((1 - I)/Sqrt[11]), (2 + I)/Sqrt[
   11], -((2 I)/Sqrt[11])}, {(2 - I/3)/Sqrt[11], (1/3 - I)/Sqrt[11], (
   2 - (4 I)/3)/Sqrt[11]}}, {{-((1 - I)/Sqrt[11]), (2 + I)/Sqrt[
   11], -((2 I)/Sqrt[11])}, {(2 - I/3)/Sqrt[11], (1/3 - I)/Sqrt[11], (
   2 - (4 I)/3)/Sqrt[11]}}, 1]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/27ae817544b743b5.png) | 
| Out[9]= |  | 
Since the basis is orthonormal, the projection is the sum of the projections of the vector onto the orthonormal basis vectors:
| In[10]:= | ![Total[Map[
  ResourceFunction[
    "ProjectOnSubspace"][{1, 1 + I, 3 - 2 I}, #] &, {{-((1 - I)/Sqrt[
     11]), (2 + I)/Sqrt[11], -((2 I)/Sqrt[11])}, {(2 - I/3)/Sqrt[
    11], (1/3 - I)/Sqrt[11], (2 - (4 I)/3)/Sqrt[11]}}]]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/66ab9f278045b430.png) | 
| Out[10]= |  | 
The projection of one matrix onto another using the standard matrix inner product:
| In[11]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{{1, 2}, {3, 4}}, {{0, 1}, {1, 0}}, Tr[Transpose[#1] . #2] &]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/32671fe788265f25.png) | 
| Out[11]= |  | 
The projection of a matrix onto a subspace of the space of 2×2 symmetric matrices using the standard matrix inner product:
| In[12]:= | ![ResourceFunction[
 "ProjectOnSubspace"][{{1, 1}, {2, 2}}, {{{1, 2}, {2, 0}}, {{3, 1}, {1, 5}}}, (Tr[Transpose[#1] . #2] &)]](https://www.wolframcloud.com/obj/resourcesystem/images/e57/e572b522-3c54-45be-8ae2-d8ce82a28243/240001c3f9893f37.png) | 
| Out[12]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License