Expression adding

If your goal is to quickly add a known number values together and you want this to be performed as efficiently as possible, the add(...) function is the most appropriate. It is essentially a for loop that is implemented in the kernel and is very fast.

To add the integers $3 + 4 + 5 + 6 + 7$, we use:

[> add( 3..7 );

$25$

A step size that is either an integer, rational number or floating-point number can also be given:

[> add( 3..7, 5/6 );

$\frac{70}{3}$

You will note that $\frac{70}{3} = 3 + \frac{23}{6} + \frac{14}{3} + \frac{11}{2} + \frac{19}{3}$, and you will see that $\frac{19}{3} \le 7$, but adding $\frac{5}{6}$ to that last term results in $\frac{43}{6} > 7$.

[> add( 3..4, 0.1 );

$38.5$

You will note this equals $3 + 3.1 + 3.2 + 3.3 + 3.4 + 3.5 + 3.6 + 3.7 + 3.8 + 3.9 + 4.0$

The step size can also be negative:

[> add( 10..1, -2/3 );

$\frac{238}{3}$

You will note that this equals $10 + \frac{28}{3} + \cdots + 2 + \frac{4}{3}$.

If an expression is given first, then it is simply added as often as there are expressions that would have been added:

[> add( x, 3..4, 0.1 ); # 11 additions

$11x$

You also create an expression that depends on the value in the range by using the following formulation:

[> add( 3*k^2, k = 1..10 );

$1155$

You will note that this equals $3 + 12 + 27 + 48 + 75 + 108 + 147 + 192 + 243 + 300$.

[> add( sin(k*Pi/12.0), k = 0..6 );

$4.297877056$

You will note this equals $0. + 0.2588190451 + 0.5000000000 + 0.7071067812 + 0.8660254037 + 0.9659258263 + 1.$

[> add( sin(k*Pi), k = 0..1/2, 1/12 );

$\frac{3}{2} + \sin\left( \frac{\pi}{12} \right) + \frac{\sqrt{2}}{2}, \frac{\sqrt{3}}{2} + \sin\left( \frac{5\pi}{12} \right)$

[> add( t^k, k = 0..5 );

$t^5 + t^4 + t^3 + t^2 + t + 1$

The end points of the range must be given; for example, you cannot ask:

[> add( k, k = 0..n );
Error, range bounds in add must be numeric