• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • Rule-Based Programming

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

Roman Maeder

Chapter 10

Rule-Based Programming - all with Video Answers

Educators


Chapter Questions

03:53

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. $0<=x<y<=7 / .\{x \rightarrow 5, y \rightarrow 7\}$
2. $\begin{aligned} & \operatorname{abs}\left[x_{-}\right]:=x / ; x>0 \\ & \operatorname{abs}\left[x_{-}\right]:=-x / ; x<0 \\ & \operatorname{Map}[\text { abs, }\{-1,1 / 3,0, I\}]\end{aligned}$
3. $\begin{aligned} & \operatorname{sign}\left[x_2\right]:=\operatorname{If}[x<0,-1,1] \\ & \{\operatorname{sign}[-2.33], \operatorname{sign}[c], \operatorname{sign}[\operatorname{sign}[-1]]\}\end{aligned}$

Gaurav Kalra
Gaurav Kalra
Numerade Educator
01:48

Problem 2

Describe the terms that match this pattern:
$$
g\left[x_{-}+n_{-} \text {Integer } y_{-} \cdot\right] .
$$
Which parts (if any) of the following expressions can be filled in for the pattern variables $\mathrm{x}_{-}$, $\mathrm{y}_{-}$, and $\mathrm{n}_{-}$?
1. $g[u+3 x+2]$
2. $g[2 u \wedge 2+v]$
3. $\mathrm{g}[6]$
4. $\mathrm{g}[\mathrm{au}+6]$
5. $\mathrm{g}[\mathrm{u} \wedge 3-\mathrm{v}]$
6. $g[u \wedge 3 / 2]$

AG
Ankit Gupta
Numerade Educator
01:38

Problem 3

Write a package that defines a function $\operatorname{diff}[\operatorname{expr}$, var $]$ to differentiate expressions symbolically.
The derivative of an arithmetic expression $U$ with respect to $x$, $\left(\frac{d U}{d x}\right)$, is defined recursively by differentiation rules that are applied to $U$. From a handbook, we take the following rules ( $U$ and $V$ are arbitrary arithmetic expressions, and $c$ is a constant):
$$
\begin{array}{ll}
\frac{d c}{d x} & \rightarrow 0 \\
\frac{d x}{d x} & \rightarrow 1 \\
\frac{d-U}{d x} & \rightarrow-\frac{d U}{d x} \\
\frac{d U+V}{d x} & \rightarrow \frac{d U}{d x}+\frac{d V}{d x} \\
\frac{d U V}{d x} & \rightarrow \frac{d U}{d x} V+U \frac{d V}{d x} \\
\frac{d U^c}{d x} & \rightarrow c U^{c-1} \frac{d U}{d x} \\
\frac{d \exp (U)}{d x} & \rightarrow \exp (U) \frac{d U}{d x} \\
\frac{d \ln (U)}{d x} & \rightarrow \frac{1}{U} \frac{d U}{d x} \\
\frac{d U^V}{d x} & \rightarrow U^V \frac{d V \ln (U)}{d x}
\end{array}
$$
1. Write these rules as definitions for $\operatorname{diff}[]$. Not all of these rules are strictly necessary. Which ones are sufficient to differentiate all the functions given?
2. These definitions can be augmented by rules for special functions, such as sine, cosine, and so on. The function diff [] becomes more powerful in this way. Implement a few of these rules.

Nick Johnson
Nick Johnson
Numerade Educator

Problem 4

An L-System consists of a word over some alphabet and a set of rewrite rules for replacing symbols or subwords by other words.
Words are associative, so we should define In[1]: = Sethttributes[rord, [Plat, OneIdentity\}] these attributes for our data type for words.
Here is a word over the alphabet consisting of the three symbols $\mathrm{m}, 1$, and $\mathrm{r}$.
Our set of rules consists of a single rule for replacing the symbol $\mathrm{m}$ by the given word.
$$
\begin{aligned}
& \text { In }[2]:=\operatorname{gen}=\operatorname{vord}[m, x, r, m, x, r, m] \\
& \text { Out }[2]=\operatorname{vord}[m, r, x, m, r, r, m] \\
& \text { In }[3]:=\operatorname{rules}=\{m \rightarrow \operatorname{gord}[m, 1, m, x, x, m, 1, m]\} \\
& \text { Out }[3]=\{m \rightarrow \operatorname{vord}[m, 1, m, x, r, m, 1, m]\}
\end{aligned}
$$
One application of the rules replaces every occurrence of m by a word; the result is $\operatorname{In}[4]:=\operatorname{gen} 1=\operatorname{gen} /$. rules flattened out, due to the attribute Flat.
$$
\begin{aligned}
& \text { Out }[4]=\operatorname{vord}[m, 1, m, r, r, m, 1, m, r, r, m, 1, m, r \text {, } \\
& \mathrm{r}, \mathrm{m}, 1, \mathrm{~m}, \mathrm{r}, \mathrm{r}, \mathrm{m}, 1, \mathrm{~m}, \mathrm{r}, \mathrm{r}, \mathrm{m}, \mathrm{l}, \mathrm{m}] \\
&
\end{aligned}
$$
We can visualize the resulting words using an idea from Turtle graphics, a simple graphic model used in the language Logo. The turtle sits originally at the origin and faces right. Each letter in our word is interpreted as a command for the turtle to change either its location by moving a certain distance in the current direction, or by turning a certain amount. As the turtle moves, it leaves a visible trail behind.
(Figure Cant Copy)
1. Define a suitable data type for representing the possible states of the turtle.
2. Design a way to specify an interpretation of a word by giving the effect each letter of the alphabet has on the state of the turtle.
3. Program a function for converting a word to a list of points, given an interpretation and the start state of the turtle.
4. Plot the line connecting the points to obtain pictures similar to the ones given here.
5. Optionally remove duplicate points from the line before plotting it. The words will often consist of long sequences of turns between the moves, leading to many occurrences of the same point in lines. For better rendering, only one of a sequence of identical points should be drawn.
Experiment with various generators, rule sets, and interpretations, such as the flowsnake. The alphabet consists of $\{\mathrm{m} 1, \mathrm{~m} 2, \mathrm{r}, 1\}$, the rules are
$$
\begin{aligned}
& \mathrm{m} 1 \rightarrow \text { word }[1, \mathrm{~m} 2, \mathrm{r}, \mathrm{m} 1, \mathrm{~m} 1, \mathrm{r}, \mathrm{r}, \mathrm{m} 1, \mathrm{r}, \mathrm{m} 2,1,1, \mathrm{~m} 2,1, \mathrm{~m} 1] \\
& \mathrm{m} 2 \rightarrow \operatorname{word}[\mathrm{m} 2, \mathrm{r}, \mathrm{m} 1, \mathrm{r}, \mathrm{r}, \mathrm{m} 1,1, \mathrm{~m} 2,1,1, \mathrm{~m} 2, \mathrm{~m} 2,1, \mathrm{~m} 1, \mathrm{r}]
\end{aligned}
$$
Both, $\mathrm{m} 1$ and $\mathrm{m} 2$ are interpreted as moves, and $\mathrm{r}$ and 1 as $60^{\circ}$ turns as before.

Check back soon!