• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • Abstract Data Types

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

Roman Maeder

Chapter 5

Abstract Data Types - all with Video Answers

Educators


Chapter Questions

04:17

Problem 1

A data type for rational numbers can be defined with the constructor makeRational $[n, d]$ and the selectors numerator $[r]$ and denominator $[r]$. The representation of a data element is simply rational $[n, d]$. It should represent the rational number $n / d$. In this exercise, you should not use the built-in rational numbers; use only integers.
1. Give the definitions of makeRational, numerator, and denominator. Complete the right side:
$$
\begin{aligned}
& \text { makeRational }\left[n_{-}, d_{-}\right]:=\ldots \\
& \text { numerator }\left[\text { rational }\left[n_{-}, d_{-}\right]\right]:=\ldots \\
& \text { denominator }\left[\text { rational }\left[n_{-}, d_{-}\right]\right]:=\ldots \\
&
\end{aligned}
$$
2. The representation rational $[n, d]$ for rational numbers is not yet unique. For example, the expressions rational $[-1,2]$, rational $[1,-2]$, and rational $[-2,4]$ describe the same number. The representation becomes unique if we demand that the denominator is always positive and the numerator and denominator are relatively prime; that is, they have no factors in common. Find two rules for rational $\left[n_{-}, d_{-}\right]$that ensure that data elements are transformed into a unique form.
3. Give definitions for addition and multiplication of rational numbers that work exclusively with constructors and selectors. Complete the right side:
rational/: a_rational + b_rational $:=\ldots$
rational/: a_rational $*$ b_rational $:=\ldots$

Eli Heath
Eli Heath
Numerade Educator
17:21

Problem 2

A data type for complex numbers can be defined with a constructor complexCartesian $[x, y]$ and the selectors re[c] and $\operatorname{im}[c]$. The representation is by a data element of the form cartesian $[x, y]$. It describes the number $x+i y$. These definitions implement the constructor and the selectors (The built-in complex numbers must not be used for this exercise).
$$
\begin{aligned}
& \text { complexCartesian }\left[x_{-}, y_{-}\right]:=\operatorname{cartesian}[x, y] \\
& \text { re[cartesian[x, } \left.\left.y_{-}\right]\right]:=x \\
& \text { im[cartesian[ } \left.\left[x_{-}, y_{-}\right]\right]:=y
\end{aligned}
$$
${ }^1$
1. Give definitions for addition, multiplication, and inverse of complex numbers that work exclusively with constructors and selectors. Complete the right side:
cartesian/: a_cartesian + b_cartesian := $\ldots$
cartesian/: a_cartesian *b_cartesian := $\ldots$
cartesian/: a_cartesian $\wedge-1:=\ldots$
Give similar definitions for the sum of a complex number and an integer or a rational number, as well as the product of a complex number with an integer or a rational number.
2. Are the rules from part 1 sufficient for all four arithmetic operations (addition, subtraction, multiplication, and division)?
$\bullet$ If yes, show how cartesian $[1,2]$ - cartesian $[0,1]$ and cartesian $[1,2] /$ cartesian $[0,1]$ are computed.
$\bullet$ If no, give more definitions sufficient for all four arithmetic operations. Show how the above two examples are now computed.
3. Complex numbers can also be given in polar coordinates as $r e^{i \varphi}=r \cos \varphi+i r \sin \varphi$. Here is the definition of a constructor complexPolar $[r, \varphi]$ that creates a data element polar $[r, \varphi]$ describing the complex number $r e^{i \varphi}$ :
$$
\text { complexPolar }\left[r_{-}, p_{-}\right]:=\operatorname{polar}[r, p] \text {. }
$$
Give definitions for the selectors $\mathrm{re}[]$ and $\mathrm{im}[]$ for data of type polar. With these definitions, re[polar[1, Pi/4]] evaluates to Sqrt[2]/2, because $e^{i \pi / 4}=\sqrt{2} / 2+$ $i \sqrt{2} / 2$.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator