Gram-Schmidt Process in Three Dimensions

Manipulate[
Module[{check, v1,v2,v3,q1,q2,q3,pr},
check=(Det[{u1,u2,u3}]==0);

If[check,{},
v1=u1;
v2=u2-proj[u2,v1];
v3=u3-proj[u3,v1]-proj[u3,v2];

q1=v1/Norm[v1];
q2=v2/Norm[v2];
q3=v3/Norm[v3]
];

pr=5;

Text@Grid[{{
Item[Style[Switch[n,
1,Row[{Row[{Subscript[sty@"u",1]," = ",u1,", "}],Row[{Subscript[sty@"u",2]," = ",u2,", "}],Row[{Subscript[sty@"u",3]," = ",u3}]}],
2,If[check,"",Row[{Row[{Subscript[sty@"v",1]," = ",Subscript[sty@"u",1]}],Row[{" = ",v1}]}]],
3,If[check,"",Row[{Row[{Subscript[sty@"v",2]," = ",Subscript[sty@"u",2]}],Row[{" - ",Subscript["proj",Subscript[sty@"v",1]],Subscript[sty@"u",2]}],Row[{" = ",v2}]}]],
4,If[check,"",Row[{Row[{Subscript[sty@"v",3]," = ",Subscript[sty@"u",3]}],Row[{" - ",Subscript["proj",Subscript[sty@"v",1]],Subscript[sty@"u",3]}],Row[{" - ",Subscript["proj",Subscript[sty@"v",2]],Subscript[sty@"u",3]}],
Row[{" = ",v3}]}]],
5,If[check,"",Row[{Subscript[sty@"q","i"]," = ",Subscript[sty@"v","i"]/Row[{"||",Subscript[sty@"v","i"],"||"}]}]],
6,If[check,"",Row[{Row[{Subscript[sty@"q",1]," = ",q1,", "}],Row[{Subscript[sty@"q",2]," = ",q2,", "}],Row[{Subscript["q",3]," = ",q3}]}]]
],16],ItemSize->{44,5}]
},

{Item[
If[Det[{u1,u2,u3}]==0,Grid[{{"Linearly Dependent Vectors"}},Alignment->Center],
Show[Graphics3D[
Switch[n,
1,(* just u vectors *){
{Arrowheads[0.02],Thick,
{Blue,
{Arrow[{{0,0,0},u1}]},{Text[Subscript[sty@"u",1],1.1u1]},
{Arrow[{{0,0,0},u2}]},{Text[Subscript[sty@"u",2],1.1u2]},
{Arrow[{{0,0,0},u3}]},{Text[Subscript[sty@"u",3],1.1u3]}
}
}
},
2, (* first v *){
{Arrowheads[0.02],Thick,
{Blue,
{Arrow[{{0,0,0},u1}]},
{Arrow[{{0,0,0},u2}]},{Text[Subscript[sty@"u",2],1.1u2]},
{Arrow[{{0,0,0},u3}]},{Text[Subscript[sty@"u",3],1.1u3]},
},

{Darker[Green,0.2],
{Arrow[{{0,0,0},v1}]},{Text[sty@Row[{"  ",Subscript[sty@"v",1],Style[" = ",Black],Style[Subscript[sty@"u",1],Blue]}],1.1v1]}
}
}
},
3, (* second v *)
{
{Arrowheads[0.02],Thick,
{Blue,
{Arrow[{{0,0,0},u1}]},
{Arrow[{{0,0,0},u2}]},{Text[Subscript[sty@"u",2],1.1u2]},
{Arrow[{{0,0,0},u3}]},{Text[Subscript[sty@"u",3],1.1u3]},
},
{Red,
{Dashing[Medium],Line[{{0,0,0},proj[u2,v1],u2,v2}]}
},
{Darker[Green,0.2],
{Arrow[{{0,0,0},v1}]},{Text[Row[{"  ",Subscript[sty@"v",1],Style[" = ",Black],Style[Subscript[sty@"u",1],Blue]}],1.1v1]},
{Arrow[{{0,0,0},v2}]},{Text[Subscript[sty@"v",2],1.1v2]}
}
}
},
4, (* third v *){
{Arrowheads[0.02],Thick,
{Blue,
{Arrow[{{0,0,0},u1}]},
{Arrow[{{0,0,0},u2}]},{Text[Subscript[sty@"u",2],1.1u2]},
{Arrow[{{0,0,0},u3}]},{Text[Subscript[sty@"u",3],1.1u3]},
},
{Red,
{Dashing[Medium],Line[{{0,0,0},proj[u3,v1]+proj[u3,v2],u3,v3}]}
},
{Darker[Green,0.2],
{Arrow[{{0,0,0},v1}]},{Text[Row[{"  ",Subscript[sty@"v",1],Style[" = ",Black],Style[Subscript[sty@"u",1],Blue]}],1.1v1]},
{Arrow[{{0,0,0},v2}]},{Text[Subscript[sty@"v",2],1.1v2]},
{Arrow[{{0,0,0},v3}]},{Text[Subscript[sty@"v",3],1.1v3]}
}
}
},
5,(* q vectors *){
{Arrowheads[0.02],Thick,
{Blue,
{Arrow[{{0,0,0},u1}]},
{Arrow[{{0,0,0},u2}]},{Text[Subscript[sty@"u",2],1.1u2]},
{Arrow[{{0,0,0},u3}]},{Text[sty@Subscript[sty@"u",3],1.1u3]}
},
{Darker[Green,0.2],
{Arrow[{{0,0,0},v1}]},{Text[Row[{"  ",Subscript[sty@"v",1],Style[" = ",Black],Style[Subscript[sty@"u",1],Blue]}],1.1v1]},
{Arrow[{{0,0,0},v2}]},{Text[Subscript[sty@"v",2],1.1v2]},
{Arrow[{{0,0,0},v3}]},{Text[Subscript[sty@"v",3],1.1v3]}
},
{Orange,
{Arrow[{{0,0,0},q1}]},{Text[Style[Subscript[sty@"q",1],Background->White],1.1q1]},
{Arrow[{{0,0,0},q2}]},{Text[Style[Subscript[sty@"q",2],Background->White],1.1q2]},
{Arrow[{{0,0,0},q3}]},{Text[Style[Subscript[sty@"q",3],Background->White],1.1q3]}
}
}
},
6,(* only q's *){
{Arrowheads[0.02],Thick,
{Orange,
{Arrow[{{0,0,0},q1}]},{Text[Style[Subscript[sty@"q",1],Background->White],1.1q1]},
{Arrow[{{0,0,0},q2}]},{Text[Style[Subscript[sty@"q",2],Background->White],1.1q2]},
{Arrow[{{0,0,0},q3}]},{Text[Style[Subscript[sty@"q",3],Background->White],1.1q3]}
}
}
}
],
PlotRange->pr,PlotRangePadding->1,Boxed->False,AxesOrigin->{0,0,0},Axes->showaxes,ImageSize->{550,350},RotationAction->"Clip",AxesLabel->{"x","y","z"},ViewAngle->0.272,ViewPoint->{2.639,1.389,1.597},ViewVertical->{0.125,0.062,0.990}
],

If[n==4,ParametricPlot3D[a*v1+b*v2,{a,-pr,pr},{b,-pr,pr},PlotStyle->{Red,Opacity[0.2],EdgeForm[None]},Mesh->None],{}]
]
],ItemSize->{40,35}]
}},Alignment->Top]]
,

Row[{Control@{{n,1,"step"},Range[6],Setter},Spacer[30],Control@{{showaxes,False,"show axes"},{True,False}}
}],

OpenerView[{"change the vectors",
Column[{
Control@{{u1,{2,2,0},Row[{Subscript[Style["u",Bold],1],Spacer[20]}]},{-5,-5,-5},{5,5,5},{1,1,1},ImageSize->Tiny,Appearance->"Labeled"},
Control@{{u2,{2,4,1},Row[{Subscript[Style["u",Bold],2],Spacer[20]}]},{-5,-5,-5},{5,5,5},{1,1,1},ImageSize->Tiny,Appearance->"Labeled"},
Control@{{u3,{4,-4,4},Row[{Subscript[Style["u",Bold],3],Spacer[20]}]},{-5,-5,-5},{5,5,5},{1,1,1},ImageSize->Tiny,Appearance->"Labeled"}
}]}],

ContentSize->{500,480},
Initialization:>{proj[u_,v_]:=(u.v/Norm[v]^2)v;sty[txt_]:=Style[txt,18,Bold];}
]

The Gram–Schmidt process converts a set of linearly independent vectors to a set of orthonormal vectors. This Demonstration illustrates the process in three dimensions with several steps. The original vectors (Subscript[u, i]), i=1,2,3 are converted to a set of orthogonal vectors (Subscript[v, i]) by subtracting appropriate projections. These vectors are then normalized to a set of orthonormal vectors (Subscript[q, i]).

Contributed by: Abby Brown