Diagonal Quadratic Form

Author

Eric W. Weisstein
April 14, 2004

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Algebra/DiagonalQuadraticForm.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/DiagonalQuadraticForm.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Additional Authors

Todd Rowland
Wolfram Research, Inc.
Dec 11, 2001

Todd's Form

DiagonalizerMatrix[a_]:=Module[{q,ctr,v1,t2},q[v_]:=v.a.v;
v1=Table[0,{Length[a]}];
v1[[1]]=1;
v1=N[v1];
t2=q[v1];
If[t2≠0,v1=(Abs[t2])^(-1/2) v1];
ctr={v1};
Do[{v1=NullSpace[N[ctr.a]][[1]];
t2=q[v1];
If[t2≠0,v1=(Abs[t2])^(-1/2) v1];
ctr=Join[ctr,{v1}];},{Length[a]-1}];
Transpose[Sort[ctr,q[#1]>q[#2]&]]]

Eric's Form

DiagonalizerMatrix[a_List?MatrixQ]:=Module[
{
q,ctr,t2,
v1=Prepend[Table[0,{Length[a]-1}],1]
},
q[v_]:=v.a.v;
If[(t2=q[v1])≠0,v1/=Sqrt[Abs[t2]]];
ctr={v1};
Do[
v1=NullSpace[ctr.a][[1]];
If[(t2=q[v1])≠0,v1/=Sqrt[Abs[t2]]];
AppendTo[ctr,v1],
{Length[a]-1}
];
Transpose[Sort[ctr,q[#1]>q[#2]&]]
]

Examples

Example 1

(({{1, 2}, {2, 3}}))

c=DiagonalizerMatrix[a={{1,2},{2,3}}]

Transpose[c].a. c//MatrixForm

Transpose[c].a. c//MatrixForm

Other Examples

r=(#+Transpose[#])/2&/@Table[Random[Integer,{1,9}],{10},{n=Random[Integer,{1,5}]},{n}]

Timing[d1=MatrixDiagonalizer/@r]

Timing[d2=DiagonalizerMatrix/@N[r]]

d3=DiagonalizerMatrix/@r

Chop[MatrixForm/@(d1-d2)]

MatrixForm/@{d1[[5]],d2[[5]],DiagonalizerMatrix[r[[5]]],N[DiagonalizerMatrix[r[[5]]]]}

Chop[Table[MatrixForm[Transpose[d1[[i]]].r[[i]].d1[[i]]],{i,Length[r]}]]

Chop[Table[MatrixForm[Transpose[d2[[i]]].r[[i]].d2[[i]]],{i,Length[r]}]]

Simplify[Table[MatrixForm[Transpose[d3[[i]]].r[[i]].d3[[i]]],{i,Length[r]}]]