Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: math480-2016
Views: 2158

Is there a formula?

From last time...

Throw out a mathematical question: Is there a simple formula for 0<mnsin(m)\sum_{0< m\leq n} \sin(m) similar to the formula 0<m<nm=n(n1)2\sum_{0< m< n} m = \frac{n(n-1)}{2}?

YES!

k, n = var('k,n') show(sum(k^2, k, 1, n).factor())
16(2n+1)(n+1)n\displaystyle \frac{1}{6} \, {\left(2 \, n + 1\right)} {\left(n + 1\right)} n
k, n = var('k,n') f = sum(sin(k), k, 1, n-1) show(f)
cos(narctan(sin(1)cos(1)))sin(1)(cos(1)1)sin(narctan(sin(1)cos(1)))sin(1)2(cos(1)1)\displaystyle \frac{\cos\left(n \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right)\right) \sin\left(1\right) - {\left(\cos\left(1\right) - 1\right)} \sin\left(n \arctan\left(\frac{\sin\left(1\right)}{\cos\left(1\right)}\right)\right) - \sin\left(1\right)}{2 \, {\left(\cos\left(1\right) - 1\right)}}
%timeit float(f(n=10000))
625 loops, best of 3: 349 µs per loop
%timeit float(f(n=100000))
625 loops, best of 3: 315 µs per loop
g = fast_float(f, 'n') g
<sage.ext.interpreters.wrapper_rdf.Wrapper_rdf object at 0x7f7586a69980>
%timeit g(10000)
625 loops, best of 3: 827 ns per loop
g(10000)
1.9395054106807146
︠fbd4b34a-5f02-4ddf-9e7c-848f8e3baa15s︠ 315/.827
380.894800483676

500x speedup!

%time float(sum(sin(j) for j in [1..10000]))
1.6338910217924854 CPU time: 1.82 s, Wall time: 1.84 s
1.84 / (766*10^(-9))
2.40208877284595e6

So the net speedup from our one liner from day one is a factor of several million in speed.

Exercise now: Draw a plot of the function f(n)=0<mnsin(m)f(n) = \sum_{0< m\leq n} \sin(m) for various values of nn...

# one way stats.TimeSeries([sin(n) for n in range(10000)]).sums().plot()
# Another -- use the formula above...