• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • Functions

Computer science with Mathematica: theory and practice for science, mathematics, and engineering

Roman Maeder

Chapter 11

Functions - all with Video Answers

Educators


Chapter Questions

03:10

Problem 1

Give the result of evaluating the following expressions. If there are any nested functions, also give the most important intermediate steps. Assume that each example is evaluated in a fresh Mathematica session. Consecutive expressions in one example are evaluated one after another in the same session.
1. $g\left[x_{-}\right]:=1+1 / x$ Nest $[g, a, 2]$
2. $i\left[i_{-}\right]:=i_{\wedge} I$ $\operatorname{Nest}[i, I, 2]$

James Kiss
James Kiss
Numerade Educator

Problem 2

Realize some other implementation for the potentially infinite lists from Section 11.2.2. Some Ideas:
1. Binary trees (see Section 6.3). Note that old information has to be overwritten if an already existing key is inserted again.
2. A list of rules of the form $\left\{i_1 \rightarrow e_1, \ldots, i_n->e_n\right\}$. To store a new value at position $i$, you can simply prepend a new rule. For efficiency reasons it may be necessary to remove other rules for the same $i$ to prevent the rule list from growing too much.
3. Your own idea.
Compare efficiency (run time and memory needed) of various implementations.

Check back soon!
04:55

Problem 3

Use the package LSR.m to implement a random number generator, as indicated in Section 11.3.

Jessica Waggener
Jessica Waggener
Numerade Educator

Problem 4

The $n$th Bell polynomial $B_n(x ; g(t))$ is the coefficient of $t^n$ in the Taylor series of $e^{x g(t)}$ at $t=0$. We assume $g(0)=0$. The formula for the Taylor series gives:
$$
e^{x g(t)}=\sum_{k=0}^{\infty} B_n(x) \frac{t^n}{n !}
$$
$\square$ Example 1: $g(t)=t$ :
$$
\begin{aligned}
e^{x t} & =\sum_{k=0}^{\infty} x^n \frac{t^n}{n !} \\
B_n(x ; t) & =x^n
\end{aligned}
$$
$\square$ Example 2: $g(t)=\log (1+t)$ :
$$
\begin{aligned}
e^{x \log (1+t)} & =\sum_{k=0}^{\infty} x^{\underline{n}} \frac{t^n}{n !} \\
B_n(x ; \log (1+t)) & =x^{\underline{n}}
\end{aligned}
$$
The falling factorial $x^{\underline{n}}$ is defined as
$$
x^{\underline{n}}=\prod_{i=0}^{n-1}(x-i)=x(x-1) \ldots(x-n+1) .
$$
Write a package that implements the function $\operatorname{BellP}[n, x, g]$ to compute the Bell polynomial $B_n(x ; g)$.
The identity is a good way to test our function (Example 1).

Here are the falling factorials up to degree four. (Example 2).

Their construction becomes apparent in factored form.
$$
\begin{aligned}
& \operatorname{In}[1]:=\operatorname{BellP}[10, x, \text { Identity }] \\
& \text { Out }[1]=x^{10} \\
& \operatorname{In}[2]:=\operatorname{Table}[\operatorname{BellP}[i, x, \log [1+8] k],\{i, 0,4\}] \\
& \text { Out }[2]=\left\{1, x,-x+x^2, 2 x-3 x^2+x^3,\right. \\
&-6 x\left.+11 x^2-6 x^3+x^4\right\}
\end{aligned}
$$
$$
\begin{aligned}
& \operatorname{In}[3]:=\text { Factor }[\%] \\
& \text { Out }[3]=\{1, x,(-1+x) x,(-2+x)(-1+x) x, \\
& (-3+x)(-2+x)(-1+x) x\}
\end{aligned}
$$

Check back soon!