Replaced for loops and explicit sums with a linear algebra expression

It might be less clear to read, but there are fewer local variables and loops in the code now and that is good. Plus, with scipy I think this code should also be a lot faster.