Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 16658

Is there a formula?

From last time...

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

YES!

Better yet, you can use Sage to discover it!

k, n = var('k,n') show(sum(k, k, 1, n-1).factor()) ## Warning: in this notation, the right endpoint is *included*, contrary to usual Python syntax
12(n1)n\displaystyle \frac{1}{2} \, {\left(n - 1\right)} n
k, n = var('k,n') show(sum(k^4, k, 1, n).factor())
130(3n2+3n1)(2n+1)(n+1)n\displaystyle \frac{1}{30} \, {\left(3 \, n^{2} + 3 \, n - 1\right)} {\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)}}

Aside: a couple of ways you could have discovered this formula yourself:

  • Use the identity sin(x)+sin(y)=\sin(x) + \sin(y) = \cdots

  • Write sin(x)\sin(x) in terms of eixe^{ix} and sum the resulting geometric series.

%timeit float(f(n=10000))
625 loops, best of 3: 663 µs per loop
%timeit float(f(n=100000))
625 loops, best of 3: 341 µs per loop
fast_float?
File: /projects/sage/sage-7.5/src/sage/ext/fast_eval.pyx Signature : fast_float(f, old=None, expect_one_var=False, *args) Docstring : Tries to create a function that evaluates f quickly using floating- point numbers, if possible. There are two implementations of fast_float in Sage; by default we use the newer, which is slightly faster on most tests. On failure, returns the input unchanged. INPUT: * "f" -- an expression * "vars" -- the names of the arguments * "old" -- use the original algorithm for fast_float * "expect_one_var" -- don't give deprecation warning if "vars" is omitted, as long as expression has only one var EXAMPLES: sage: from sage.ext.fast_eval import fast_float sage: x,y = var('x,y') sage: f = fast_float(sqrt(x^2+y^2), 'x', 'y') sage: f(3,4) 5.0 Specifying the argument names is essential, as fast_float objects only distinguish between arguments by order. sage: f = fast_float(x-y, 'x','y') sage: f(1,2) -1.0 sage: f = fast_float(x-y, 'y','x') sage: f(1,2) 1.0
g = fast_float(f, 'n') g
<sage.ext.interpreters.wrapper_rdf.Wrapper_rdf object at 0x7fa97c11a788>
%timeit g(10000)
625 loops, best of 3: 587 ns per loop
g(10000)
1.9395054106807146
N(f(10000))
1.93950541068071
N((663 * 10^(-6)) / (587 * 10^(-9)))
1129.47189097104

1000x speedup!

%time float(sum(sin(j) for j in [1..10000]))
1.6338910217924534 CPU time: 2.08 s, Wall time: 2.12 s
2.08 / (587*10^(-9))
3.54344122657581e6

So the net speedup from our first attempt is a factor of several million in speed.

Exercise now: Draw a plot of the function f(n)=0<m<nsin(m)f(n) = \sum_{0< m< 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...
show(points([(n, g(n)) for n in range(10000)]))