Homework

Numerical Methods

David Mayerich

Scalable Tissue Imaging and Modeling (STIM) Laboratory

Department of Electrical and Computer Engineering

Cullen College of Engineering

University of Houston

David Mayerich

STIM Laboratory, University of Houston

Homework 1

Real and Complex Sets

Hilbert Spaces

Linear Algebra

Taylor Series

David Mayerich

STIM Laboratory, University of Houston

Calculate \(y=z^2z^*\) where \(z = (2+3i)\)

David Mayerich

STIM Laboratory, University of Houston

y = (2+3i)^2(2+3i)^*
y = (2+3i)(2+3i)(2-3i)
y = (4 + 12i + 9i^2)(2-3i)
y = (-5 + 12i)(2-3i)
y = -10 + 15i + 24i - 36i^2
y = 26 + 39i

Sets and Hilbert Spaces

  • Describe the Hilbert space of \(\mathbf{y} = \mathbf{ABx}\) if \(\mathbf{A}\in \mathbb{R}^{5\times 4}\) and \(\mathbf{x}\in\mathbb{C}^{7}\)

David Mayerich

STIM Laboratory, University of Houston

\mathbf{y} = \mathbf{ABx}
\mathbf{y} = \mathbb{R}^{5\times 4} \mathbf{B} \mathbb{C}^7
\mathbb{M}^{4\times 7}
\mathbb{C}^{5}
\mathbb{M} = \mathbb{R} \quad \text{or} \quad \mathbb{M} = \mathbb{C}

Calculate \(\langle \mathbf{x}, \mathbf{y} \rangle\)

David Mayerich

STIM Laboratory, University of Houston

\mathbf{x} = \begin{bmatrix} 2i \\ -7 + 2i\\ 3 - i \end{bmatrix}
\mathbf{y} = \begin{bmatrix} -1 + 2i \\ 1 - 3i\\ i \end{bmatrix}
\bar{\mathbf{y}} = \begin{bmatrix} -2 - 2i \\ 1 + 3i\\ -i \end{bmatrix}
\mathbf{x}^T \bar{\mathbf{y}} = 2i(-2-2i) + (-7+2i)(1+3i)-i(3-i)
= (-4i-4i^2) + (-7-21i+2i+6i^2)+(-3i+i^2)
=4-4i -13-19i-1-3i
=-10 -26i

Gaussian Elimination

  • Solve the following linear system:

David Mayerich

STIM Laboratory, University of Houston

\begin{split} x-2y + 3z &= 7\\ 2x + y + z &= 4\\ -3x+2y-2z &= -10 \end{split}
\begin{bmatrix} 1 & -2 & 3\\ 2 & 1 & 1\\ -3 &2 &-2 \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} 7\\ 4\\ -10 \end{bmatrix}
\begin{bmatrix} 1 & -2 & 3\\ 0 & 5 & -5\\ 0 & -4 & 7 \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} 7\\ -10\\ 11 \end{bmatrix}
\begin{bmatrix} 1 & -2 & 3\\ 0 & 5 & -5\\ 0 & 0 & 3 \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} 7\\ -10\\ 3 \end{bmatrix}
3z=3\\ z=1
5y-5z=-10\\ 5y-5=-10\\ 5y=-5\\ y=-1
1x-2y+3z=7\\ 1x+2+3=7\\ 1x+5=7\\ x=2

Taylor Series

  • Calculate a 4th-order Maclaurin series to approximate \(f(x) = e^x \cos{x^2}\)

David Mayerich

STIM Laboratory, University of Houston

e^x \approx 1 + x + \frac{x^2}{2} + \frac{x^3}{6}+\frac{x^4}{24}
\cos{x} \approx 1 - \frac{x^2}{2} + \frac{x^4}{24}
f(x)\approx\left( 1 + x + \frac{x^2}{2} + \frac{x^3}{6}+\frac{x^4}{24} \right) \left( 1 - \frac{\left(x^2\right)^2}{2} + \frac{\left(x^2\right)^4}{24}\right)
f(x)\approx\left( 1 + x + \frac{x^2}{2} + \frac{x^3}{6}+\frac{x^4}{24} \right) \left( 1 - \frac{x^4}{2} + \frac{x^8}{24}\right)
f(x)\approx\left( 1 + x + \frac{x^2}{2} + \frac{x^3}{6}+\frac{x^4}{24} \right) - \left( \frac{x^4}{2} + \frac{x^5}{2} + \frac{x^4}{4} + \frac{x^7}{12}+\frac{x^8}{48} \right) + \left( \frac{x^8}{24} + \frac{x^9}{24} + \frac{x^{10}}{48} + \frac{x^{11}}{144}+\frac{x^{12}}{576} \right)
f(x)\approx1+x+\frac{x^2}{2} + \frac{x^3}{6} - \frac{11x^4}{24} \cdots

Homework 2

Data Types

Promotions

Heap Allocation

David Mayerich

STIM Laboratory, University of Houston

Promotions

  • What are the register contents after the following allocations?

David Mayerich

STIM Laboratory, University of Houston

//...
unsigned char a = 200;
unsigned char b = 2;
unsigned int c = 128;
unsigned int w = c % 7
unsigned int x = a + c;
unsigned int y = (unsigned char)(a + c);
unsigned int z = a * b;
//...
w = 2
x = 328
y = 328 % 256 = 72
z = 400     // why not 144?

Heap Allocation

  • Allocate space for the following arrays in the GEMM operation:

David Mayerich

STIM Laboratory, University of Houston

\mathbf{C} = \alpha \mathbf{AB} + \beta \mathbf{C} \quad \text{where} \quad \mathbf{A} \in \mathbb{R}^{m \times k} \quad \mathbf{B} \in \mathbb{R}^{k \times n}

given the function signature:

void sgemm(int m, int n, int k, float alpha, float* A, float* B, float beta, float* C)
float* A = (float*) malloc(m * k * sizeof(float));

float* B = (float*) malloc(k * n * sizeof(float));

float* C = (float*) malloc(m * n * sizeof(float));

Homework 3

Maclaurin Series

Error Terms

Approximating Complex Functions

David Mayerich

STIM Laboratory, University of Houston

Complexity Analysis

  • Calculate the computational complexity of the following algorithms:

David Mayerich

STIM Laboratory, University of Houston

\mathbf{Av}\quad \text{where} \quad \mathbf{A}\in\mathbb{R}^{N\times N}
\mathbf{T} \mathbf{v}_i \quad \text{where} \quad i \in [1, N] \quad \mathbf{v}_i \in \mathbb{R}^N \quad \mathbb{T}\in \mathbb{R}^{3 \times 3}
\mathbf{Y} = \mathbf{ABx} \quad \text{where} \quad \mathbf{A}\in\mathbb{R}^{M \times N}\quad \mathbf{B}\in \mathbb{R}^{M \times N}\quad \mathbf{x}\in \mathbb{R}^N
\mathbf{x} \cdot \mathbf{y} \quad \mathbf{x},\mathbf{y} \in \mathbb{R}^N
\mathbf{x} \otimes \mathbf{y} \quad \mathbf{x},\mathbf{y} \in \mathbb{R}^N
O \left( N^2 \right)
O \left( N \right)
O \left( NM + N^2 \right)
O \left( N \right)
O \left( N^2 \right)

Synthetic Division

  • Use Horner's algorithm to calculate \(p(x)\) and \(p'(x)\)

David Mayerich

STIM Laboratory, University of Houston

p(x) = 3x^4 - x^2 + 2x + 13 \quad \text{for} \quad x=5
3
r(x) = 3x^3 + 15x^2 + 74x + 372 + \frac{1860}{x-5}
15
15
75
74
370
372
1873
3
15
30
224
1120
1492
150
1860
p(5) = 1873
p'(5) = 1492
p'(x) = 12x^3 - 2x + 2
3 \quad\quad 0 \quad\quad -1 \quad\quad 2 \quad\quad 13
5
3 \quad\quad 15 \quad\quad 74 \quad\quad 372
5

Homework 4

Series Expansions

Truncation Error

Relative Error

David Mayerich

STIM Laboratory, University of Houston

Truncation Error in Series

  • Calculate the relative error in the first 3 terms of the following expansion for \(\pi\)

David Mayerich

STIM Laboratory, University of Houston

\pi = 4 \sum_{k=0}^{\infty} \frac{(-1)^k}{2k+1}
\pi \approx 4
\pi \approx 4 - \frac{4}{3}
\pi \approx 4 - \frac{4}{3} + \frac{4}{5}
\rightarrow 4.000
\rightarrow 2.667
\rightarrow 3.467
\epsilon_r = \frac{|3.142 - 4.000|}{|3.142|} \approx 27 \%
\epsilon_r = \frac{|3.142 - 2.667|}{|3.142|} \approx 15 \%
\epsilon_r = \frac{|3.142 - 3.467|}{|3.142|} \approx 10 \%

Radiocarbon Dating

  • You are developing a new radiocarbon dating method. Your test sample is an Egyptian papyrus document that has been officially dated at \(2560\) BCE. Your method estimates the date as \(2830\) BCE. What is the relative error of your method (in percent), assuming that the official date is correct?

David Mayerich

STIM Laboratory, University of Houston

Relative error requires a ratio scale.

The time scale in this case is relative to the date that the papyrus was created.

The dating method is measuring how much time has passed since the manuscript was created.

t(x)=2560+x \quad \text{where} \quad x\ \text{is the current date}
c(x)=2830+x
\epsilon_r=\frac{|2830-2560|}{|2560+x|}
= \frac{|270|}{|4586|}\approx 5.9 \%

Homework 5

Signed and Unsigned Integers

Memory Addressing

Overflow

Floating Point

David Mayerich

STIM Laboratory, University of Houston

Signed Integers

  • What is the smallest value that can be represented with a 5-bit signed integer using two's complement?

  • Convert from positive to negative:

    1. perform a NOT operation on the positive number

    2. add 1

  • Reverse that to convert to positive:

David Mayerich

STIM Laboratory, University of Houston

0\ \ 1\ \ 1\ \ 1\ \ 1
1\ \ 0\ \ 0\ \ 0\ \ 0
\rightarrow 16
\rightarrow -16
1\ \ 0\ \ 0\ \ 0\ \ 0
-
1

Nintendo Memory

  • A Nintendo Entertainment System (NES) used a pixel processing unit (PPU) with 246 bytes of addressable sprite memory that could be used to process motion on the screen. How many bits were required to address this memory space?

David Mayerich

STIM Laboratory, University of Houston

\(3\) bits can address \(2^3 = 8\) locations

\(7\) bits can address \(2^7 = 128\) locations

\(8\) bits can address \(2^8 = 256\) locations

This is why machines like the NES are often called \(8\)-bit systems

NeoGeo Memory

  • The NeoGeo had \(64\) kilobytes (kB) of main VRAM. How many bits were required to address this memory space?

David Mayerich

STIM Laboratory, University of Houston

\(15\) bits can address \(2^{15}=32768\) locations

\(16\) bits can address \(2^{16}=65536\) locations

  • The SI standard defines a kilobyte (kB) as \(1000\) bytes

  • The JEDEC standard defined a kilobyte as \(2^{10} = 1024\) bytes

  • Since the power-of-two standard is still used in semiconductor manufacture, it is now generally referred to as a kibibyte (KiB)

Donkey Kong

  • The number of seconds given to complete each level \(L\) in the video game Donkey Kong is \(s=\text{min}(10L+40, 80)\). Why has nobody gotten past level 22?

David Mayerich

STIM Laboratory, University of Houston

Level 22 would allow the player \(s=\text{min}(260, 80) = 80\), right?

What if the calculation used \(8\)-bit integers?

s=\text{min}(260, 80) = 80
s=\text{min}(4, 80) = 4
260\ \text{mod}\ 256 = 4

Floating Point

  • Convert the following \(8\)-bit floating point values to decimal assuming the following format with a bias of \(7\):

David Mayerich

STIM Laboratory, University of Houston

0\ \ 0\ \ 0\ \ 1\ \ 0\ \ 1\ \ 0\ \ 1

sign

exponent

mantissa

+1.101_2\times 2^{0010_2 - 7}
+1.101_2\times 2^{2 - 7}
+1.625\times 2^{-5}
= 0.05078125
\frac{5}{8}

Homework

By STIM Laboratory