# 2.3. sum¶

The Eq Language provide a sum (and a product) function, which can be abused in some ways. It’s a good to know how to pervert it and how to get it straight otherwise.

## 2.3.1. Sum for formatting¶

When you use a sum (or a product) in formatting, there isn’t any rules. You must at least give it an expression as parameter. If you add more parameters, they will be used as bounds for the sum.

```\$ eq format 'sum(k)'

__
\  k
/_
```

The last parameter is allways the summed expression.

```\$ eq format 'sum(a, k)'

__
\  k
/_
a
```

Here whe use the product function. Both function work the same way

```\$ eq format 'sum(a, b, k)'
b
__
\  k
/_
a
```

If you use to many parameters, your sum (or product is invalid), and will not be formatted.

```\$ eq format 'sum(a, b, c, k)'
sum(a, b, c, k)
```

## 2.3.2. Evaluation of sums¶

For a successful evaluation of sum, certain rules must be followed :

1. All bounds must be defined.
2. A variable must be used in the lowerbound and given an equality.
3. Lower bound (given as equality) and upper bound must be integral.

The first rule is really simple to test :

```\$ eq eval 'sum(3)'

__
\  3
/_
```

As you see, no evaluation has happened. To understand the second and third rules, we ha ve to see a working example.

```\$ eq eval '1 + sum( n = 1, 4, x ^ n / n! )'
2      3      4
x      x      x
1 + x + ---- + ---- + ----
2      6      24
```

By using n = 1 we tell the evaluator to use <code>n</code> as the iteration variable. We also tell it to start at 1. The upper bound tell to stop after 4. As you can see the bounds are integers, there is no point in the number. As an exemple, by slightly modifying the previous example, we obtain the following result :

```\$ eq eval '1 + sum( n = 1, 4.0, x ^ n / n! )'
4.0
_____
\       n
\     x
1 +  /    ----
/____  n!
n = 1
```

The floating point version of 4 stop the evaluator to perform some more processing. As some may have noticed, the previous samples are the taylor expension of the exponential function in 0.

sum(initialisation, upperbound, formula)
Parameters: initialisation – Must be in form var = expr the expr part is evaluated upperbound – Must be an expression, is evaluated at runtime. formula – The formula to be summed

## 2.3.3. Product for evaluation¶

The same rules apply for the product, this section is just here to provide a working example

```\$ eq eval 'product( n = 1, 10, n )'
3628800
```
```\$ eq eval 'product( n = 1, 10, n ) = 10!'
true
```
product(initialisation, upperbound, formula)
Parameters: initialisation – Must be in form var = expr the expr part is evaluated upperbound – Must be an expression, is evaluated at runtime. formula – The formula to be multiplied