# Trefethen & Bau & MATLAB & Julia, Lecture 8: Gram-Schmidt

This lecture is about the modified Gram-Schmidt method and flop counting. The notebooks are here.

I’m lost.

Almost as an afterthought I decided to add a demonstration of the timing of Gram-Schmidt compared to the asymptotic flop count. Both MATLAB and Julia got very close to the trend as got into the hundreds, using vectorized code:

```
= collect(50:50:500);
n_ = zeros(size(n_));
time_ for k = 1:length(n_)
= n_[k];
n = rand(1200,n);
A = zeros(1200,n); R = zeros(600,600);
Q
tic();
1,1] = norm(A[:,1]);
R[:,1] = A[:,1]/R[1,1];
Q[for j = 2:n
1:j-1,j] = Q[:,1:j-1]'*A[:,j];
R[= A[:,j] - Q[:,1:j-1]*R[1:j-1,j];
v = norm(v);
R[j,j] :,j] = v/R[j,j];
Q[end
= toc();
time_[k] end
using PyPlot
loglog(n_,time_,"-o",n_,(n_/500).^2,"--")
xlabel("n"), ylabel("elapsed time")
```

I noticed that while the timings were similar, Julia lagged MATLAB just a bit. I decided this would be a great chance for me to see Julia’s prowess with speedy loops firsthand.

Compare the vectorized and unvectorized Julia versions here:

Look at the last line–it’s allocating 1.4GB of memory to make the nested loop version happen! I thought perhaps I should use `copy`

to create `v`

in each pass, but that change didn’t help. I even tried writing my own loop for computing the dot product, to no avail.

It did help a little to replace the line in which `v`

is updated with

`= broadcast!(-,v,Q[:,i]*R[i,j]) v `

The bang on the name of the function makes it operate in-place, overwriting the current storage. Apparently Julia will create some syntactic sugar for this maneuver in version 0.5. Here it reduced the memory usage to 1.1 GB.

Julia’s reputation is that it’s great with loops, especially compared to MATLAB and Python. As a Julia newbie I recognize that there may still be only a small change I need to make in order to see this for myself. But I feel as though having to use that `broadcast!`

, or even the more natural `.=`

that may be coming, is already too much to ask. I’m frustrated, confused, and disappointed.