Timing things

The Mathematica command is Timing.  Here are some times for Mathematica doing svd's.  Warning you can easily make a run take longre than you think!  The log log plot is because you expect the behavior to be a power function!  Small problems will be dominated by noise and if the problem is big enough memory effects will ruin the straight line.

Data=Table[
m=Floor[(2^p)];
A= RandomReal[{-1,1},{m,m}];
{m, Timing[SingularValueList[A];][[1]]},
{p,6,11,0.5}
]
ListLogLogPlot[Data,
Frame->True,
GridLines->Automatic,
FrameLabel->{"m","sec"}]
ListPlot[Log[Data]]

It looks like a straight line.  The line is roughly.

Fit[Log[Data],{1,x},x]

This means that as a function of m the time is roughly (E^(-16.9))(m^2.67)=4.5753387694458114`*^-8 (m^2.67) Seconds.  The power is most important.

Data=Table[
m=Floor[(2^p)];
A= RandomReal[{-1,1},{m,m}];
{m, ByteCount[A]},
{p,6, 11,0.2}] ;
ListLogLogPlot[Data]
Fit[Log[Data],{1,t},t]

This means that an mxm matrix occupies 8*(m^2) bytes. Again the power is most important but the 8 has meaning here.

Projections and Rotations

q=RandomReal[{-1,1},3];
q=q/Norm[q]
Proj[p_,q_]:= p - (p.q) q;

v0={0.7,0.2, 0.3};
Graphics3D[{
Point[{0,0,0}],
Polygon[{
{0,0,0},
Proj[{1,1,1},q],
Proj[{-1,-1,1},q]
}],
{Opacity[0.3],Sphere[{0,0,0},1]},
Arrow[{{0,0,0},q}]
},
BoxRatios->Automatic,
Axes->True,
SphericalRegion->True,
AxesLabel->{"(Subscript[x, 1])","(Subscript[x, 2])","(Subscript[x, 3])"}
]

Pass by Reference vs Value

x={1,2,3};
y=F[x];
{x,y}

I was asked how to do pass by reference in Mathematica!

Clear[F]
SetAttributes[F, HoldFirst];
F[x_]:=x[[1]]=x[[1]]+1
x={1,2,3};
y=F[x];
{x,y}

Efficiency Considerations

A typical piece of code looks something like this in Mathematica (and very similar in MatLab).  Lets try to speed it up a bit.

CGS[A_]:=Module[
{m,n,V,Q,R, i,j},
{m,n}=Dimensions[A];
Q=V=ConstantArray[0,{m,n}];
R=ConstantArray[0,{n,n}];
Do[
V[[All,j]]=A[[All,j]];
Do[
R[[i,j]]=Conjugate[Q[[All,i]]].A[[All,j]];
V[[All,j]]=V[[All,j]]-R[[i,j]]Q[[All,i]],
{i,1,j-1}
];
R[[j,j]]=Norm[V[[All,j]]];
Q[[All,j]]=V[[All,j]]/R[[j,j]],
{j,1,n}
];
{Q,R}
]

m=50;
n=m;
A=RandomReal[{-1,1},{m,n}];
Timing[
{Q,R}=CGS[A];
]
{Norm[Q.R -A],Norm[IdentityMatrix[n]-ConjugateTranspose[Q].Q],Norm[LowerTriangularize[R,-1]]}

Faster

XCGS[A_]:=Module[
{m,n,Q,R,v, i,j,qi,rij,rjj},
{m,n}=Dimensions[A];
Q=ConstantArray[0,{m,n}];
R=ConstantArray[0,{n,n}];
Do[
v=A[[All,j]];
Do[
qi=Q[[All,i]];
rij=Conjugate[qi].A[[All,j]];
R[[i,j]]=rij;
v=v-rij qi,
{i,1,j-1}
];
rjj=Norm[v];
R[[j,j]]=rjj;
Q[[All,j]]=v/rjj,
{j,1,n}
];
{Q,R}
]

m=100;
n=m;
m=50;
n=m;
A=RandomReal[{-1,1},{m,n}];
Timing[
{XQ,XR}=XCGS[A];
]
{Norm[XQ.XR -A],Norm[IdentityMatrix[n]-ConjugateTranspose[XQ].XQ],Norm[LowerTriangularize[XR,-1]]}

Householder Stability

m=5;
R= UpperTriangularize[RandomReal[{-1,1}, {m,m}]];
{Q, X}= QRDecomposition[ RandomReal[{-1,1},{m,m}]];
A= Transpose[Q].R;
{Q2, R2}= QRDecomposition[A];
Norm[Q2-Q]
((Norm[R-R2])/(Norm[R]))