On this page:
9.4.1 Exercises 43
9.4.1.1 Question 164
9.4.1.2 Question 165
9.4.1.3 Question 166
9.4.1.4 Question 167

9.4 Identity Function

We have previously seen how to implement the summation function \(\sum_{i=a}^bf(i)\):

summation(f, a, b) =

  if a > b

    0

  else

    f(a)+summation(f, a+1, b)

  end

All summations can be calculated simply by specifying, on the one hand, the function \(f\) that provides each term of the summation and, on the other hand, the \(a\) and \(b\) limits of that summation. For example, the mathematical expression \(\sum_{i=1}^{10}\sqrt{i}\) is calculated by the corresponding Julia expression:

summation(sqrt, 1, 10)

In case the summation terms are calculated by an expression for which Julia does not have a predefined function, the more immediate solution will be to use a lambda expression. For example, to calculate \[\sum_{i=1}^{10}\frac{\sin i}{\sqrt{i}}\]

we can write:

summation(i -> sin(i)/sqrt(i), 1, 10)

In general, the specification of the function \(f\), which computes each term of the summation, does not raise any difficulties. However, there is a particular case that may cause some perplexity and therefore deserves to be discussed. Let us consider the \(\sum_{i=1}^{10}i\) summation. In this case, it is not entirely clear what is the function \(f\) that is at stake in the calculation of the summation. And yet, it is there. To make it clearer we have to remember that the summation function calculates \(\sum_{i=a}^bf(i)\), whereby, in this example, the parameters in question are \(a=1\), \(b=10\) and, finally, \(f(i)=i\). It is this last function that is rather strange: given an argument, it merely returns this argument, without performing any operation on it. Mathematically speaking, this function is called identity function and corresponds to the neutral element of the composition of functions. The identity function can be easily defined in Julia based on its mathematical definition:

identity(x) = x

Although it seems to be a useless function, the identity function is, in reality, very useful. First of all, because it allows us to calculate the \(\sum_{i=1}^{10}i\) summation just by writing:

> summation(identity, 1, 10)

55

Many other problems also benefit from the identity function. For example, if we define the function \(\prod\) that computes the product of a sequence:

\[\prod_{i=a}^{b}f(i)=f(a)\cdot f(a+1)\cdot\cdots\cdot f(b-1)\cdot f(b)\]

product(f, a, b) =

  if a > b

    1

  else

    f(a)*product(f, a+1, b)

  end

it allows us to define the factorial function: \[n!=\prod_{i=1}^{n}i\]

factorial(n) = product(identity, 1, n)

Further ahead we will find new uses for the identity function.

9.4.1 Exercises 43
9.4.1.1 Question 164

Both the summation and product functions can be seen as special cases of a more generic function, designated accumulation. In this more abstract function, the parameters are: the operation of combination of elements, the function to apply to each one, the initial value, the lower limit, the transition to the next element (designated as successor) and the upper limit. Define this function. Also, define the summation and product functions in terms of the accumulation function.

9.4.1.2 Question 165

It is known that the sum \(\frac{8}{1\cdot 3}+\frac{8}{5\cdot7}+\frac{8}{9\cdot11}+\cdots\) converges (very slowly) to \(\pi\). Using the accumulation function defined in the previous exercise, define the function that calculates an approximation of \(\pi\) to the \(n\)-th term of the sum. Then, determine an approximation of \(\pi\) to the term \(2000\).

9.4.1.3 Question 166

The enumerate function defined in section Enumerations is capable of generating sequences in arithmetic progression, i.e., sequences in which there is a constant difference between two consecutive elements. For example:

> enumerate(0, 20, 2)

11-element Vector{Int64}:

  0

  2

  4

  6

  8

 10

 12

 14

 16

 18

 20

It may be necessary, however, to produce sequences in which the elements evolve differently, for example, in geometric progression.

Define the higher-order function succession that receives the limits \(a\) and \(b\) of a range and also the function \(f\) and generates an array with all the elements \(x_i\) that do not exceed \(b\), such that \(x_0=a\) and \(x_{i+i}=f(x_i)\). For example:

> succession(2, 600, x -> x*2)

ERROR: UndefVarError: succession not defined

Stacktrace:

 [1] top-level scope

   @ none:1

9.4.1.4 Question 167

Redefine the enumerate function in terms of the succession function.