CS 171 & 171L | Introduction to Computer Science I & Lab | Fall 2020 | |

## Lab 6: Complex NumbersIn this lab, you will create a class that allows one to use complex numbers. You should start off withs1 driving and s2 navigating.
## 1. IntroductionsIntroduce yourself to your lab partner. What are three of your favorite songs have you listened to most (or often) in the past 12 months? ## Complex NumbersIn this lab you will familiarize yourself with the java representation of objects by working with a class which supports complex numbers. For this project, you need to complete a class which performs simple operations on complex numbers. Complex numbers have many interesting properties that make them handy for many mathematical, physics, and computational tasks. The complex number system is closed under addition, subtraction, multiplication, and division (except by 0), and taking roots. This is just some of the elegance found in the complex number system.The Fundamental Theorem of Algebra states that every polynomial of degree \(n \ge 1\) with complex coefficients has at least one solution in the complex numbers. This means if we have such a polynomial, all solutions can be considered complex. Gauss finally proved this in 1849. He also introduced the term complex number in 1831. Recall that there is no real number \(x\) such that \(x^2 = -1\), because the square of any real number must be nonnegative. A complex number \(z\) can be expressed as \(a + bi\), where \(a\) and \(b\) are real numbers and \(i\) is the square root of \(-1\), thus \(i^2 = -1\). If \(b = 0\), then the complex number \(z\) is real, in the same way that a real number without any digits to the right of the decimal point is an integer. Thus, we need two real numbers, an ordered pair (re, im), to represent a complex number: a real part (re) and an imaginary part (im). Two complex numbers \(z_1 = a + b i\) and \(z_2 = c + d i \) are equal when \(a = c\) and \(b = d\), that is their real parts are equal and their imaginary parts are equal. Because we have an ordered pair of real numbers, we can think about this as a point in the complex plane with the horizontal axis being the real axis and the vertical being the imaginary axis. We can also think about such a point in terms of polar coordinates, thus the number \(z = a + bi\) can be expressed as \((r, \theta)\) where \[r = \sqrt{a^2 + b^2}.\] and \[\theta = \tan^{-1}(b/a).\] The term \(r\) is called themodulus of \(z\) and is often written as \(|z|\) or \(\text{mod}(z)\).
The term \(\theta\) is called the argument of \(z\) and is often written as \(\arg(z)\)
A handy operation on complex numbers is called conjugatation, which negates the imaginary component. The conjugate of \(z\) is denoted \(z^*\) or \(\overline{z}\) and computed as \[z^* = a - bi.\] Geometrically, this flips the point \(z\) about the horizontal axis. Note that \(\left(z^*\right)^* = z\). Another property of the conjugate is \[z\cdot z^* = a^2 + b^2 = |z^*|^2 = |z|^2.\]Multiplication of the complex number \(z = a + bi\) by a real number \(s\) is given by \(s z = s a + s bi\). Addition of real \(t\) and the complex number \(z = a + bi\) is given by \(t+ z = (t+a) + bi\) (note the imaginary part is not effected). For the two numbers to act together as one complex number, we need to define that functionality. Arithmetic operations can be performed on complex numbers using this formulation. Let \(z_1 = a + b i\) and \(z_2 = c + d i \) be complex numbers. Then \[z_1 + z_2 = \underbrace{(a + c)}_{\text{re part}} + \underbrace{(b + d)}_{\text{im part}}i.\] Subtraction is defined in a similar way. For multiplication, we have \[z_1 z_2 = (a + b i)(c + d i) = a c + a d i + b c i + b d i^2 = (ac + b d i^2) + (a d i + b c i) = \underbrace{(a c - b d)}_{\text{re part}} + \underbrace{(b c + a d)}_{\text{im part}}i. \] The division of the two complex numbers \(z_1\) and \(z_2 \) can be perfomed as follows. \[\frac{z_1}{z_2} = \frac{(a + b i)}{(c + d i) } = \frac{(a + b i) (c - d i)}{(c + d i) (c-di)} = \frac{z_1 z_2^*}{z_2 z_2^*} = \frac{(a c + b d) + (b c - a d)i}{c^2 + d^2} = \underbrace{\frac{(a c + b d)}{c^2 + d^2}}_{\text{re part}} + \underbrace{\frac{(b c - a d)}{c^2 + d^2}}_{\text{im part}}i. \]
One can also perform exponentiation. First, raising \(e\) to a complex power \(z\) is given by
\[ e^z = e^{(a+bi)} = (e^a)(e^{ib})
= \left(e^a\right)\left(\cos(b) + i \sin(b)\right)
= \underbrace{e^a\cos(b)}_{\text{re part}} + \underbrace{e^a\sin(b)}_{\text{im part}}i.
\]
Note you can use Any positive real number \(x\) can be written as \(e^{\ln x}\), thus \(x^z\) can be written as \[x^z = \left(e^{\ln x}\right)^z = e^{(z \ln x)},\] which reduces to earlier solved problems. One can compute the natural logarithm of a complex number \(z\) as \[ \ln z = \ln(|z|) + \arg(z)i = \ln\left(\sqrt{a^2 + b^2}\right) + i \tan^{-1}(b/a).\] This is the prinicpal value, because it turns out that any multiple of \(2 \pi\) added to the imaginary component will result in the same point. This is called a branch cut of the multivalued logarithm function (way beyond the scope of this course). General exponentiation is a bit messy. Let \(z = a + b i\) and \(w = c + d i \) be complex numbers, then \(z^w\) can be performed as \[ z^w = (a + bi)^{(c+di)} = (a^2 + b^2)^{((c+di)/2)}e^{(i(c+id)\arg(a+ib))}. \] The square root of a general real number\(x\) is given by \[ \sqrt{x} = \begin{cases} \sqrt{x} = \sqrt{x} + 0i& \text{if } x \ge 0\\ \sqrt{-x} i = 0 + \sqrt{-x} i & \text{if } x \lt 0.\\ \end{cases} \] This can also be solved by previously implemented functions using intermediate calculation. The square root of \(z = a + b i\) is given by \[ \sqrt{z} = z^{(1/2)} = z^{(1/2 + 0i)}. \] This can also be solved by previously implemented functions using intermediate calculation. ## Implementing Complex NumbersCopy from the file the Lab6.pde into a new Processing window and save this as Lab6. Now create a new tab and call it Complex then copy the code from the Complex.pde to that window. Complete the methods in Complex.pde, as shown below. Change the driver/navigator roles after each method.
When you are finished, email your Complex.pde file to your lab partner and instructor. Copyright © 2020, David A. Reimann. All rights reserved. |