# Vectorized calculations in Euler Math Toolbox (EMT)

I am impressed with how computers and software opens up the exploration of math to someone like me. I certainly don’t have the mental calculating ability of an Euler or Ramanujan. But at least modern technology levels the playing field a little in the sense that I can gain insights from numerical results in a method similar to what they probably used. Granted, computers can not help one to acquire the brilliant math talent that they had in abundance. But I can do some calculations with software and an average laptop PC that Euler or Ramanujan could only have dreamed of.

One thing I like about Matlab-type software such as EMT is that vectorized calculations speeds things up considerably. Vectorized means that, for example, the function cos(kx), where x is a vector and k is an integer, cos(kx) is treated as a vector and is calculated automatically and efficiently by EMT, so I don’t have to loop through the vector x components to calculate cos(kx). I ran a calculation of a fourier series for x from 0 to for 10 000 terms and 300 points along the interval [0, 2π], comparing the vectorized time with the non-vectorized time.

Here is the sample code:
// vectorized calculation
x = linspace(0,2*pi,299);
S = zeros(1,300);
Start = time();
for k = 1 to 10000
S = S + cos(k*x)/k^3;
end;

Time1 = time()-Start
// non-vectorized calculation
Start = time();
x = linspace(0,2*pi,299);
S = zeros(1,300);
for k = 1 to 10000
for n = 1 to 300
S[n] = S[n] + cos(k*x[n])/k^3;
end;
end;
Time2 = time()-Start

Comparing Time1 and Time2 for these two cases, Time1 was about 67 times quicker than Time2. So wherever possible take advantage of the vectorized methods that EMT provides. You might start running in to trouble with memory if you calculate with a matrix or vector of a million components or so, so there is a trade off in speed versus memory at some point. According to the EMT website there are languages that one can use to make EMT loops even faster, but what comes natively with EMT is fast enough for me so far.