The main goals of this lab are to:
![]() |
A complex number z can be written in standard form
|
Figure 1: The Complex Plane |
Computing Complex Numbers by Hand: In class, we will go over the origin, definition, and operations of complex numbers. Once you are comfortable with the concepts, work through the following problems: Complex Number Problems (pdf).
These will not be collected but you will be expected to understand how to do them. The answers can be found here (pdf) but it is important that you try to do these without first looking at the solutions! In class, we will go over how these answers were computed. Below, you will also learn how to have Processing calculate the solutions for you.
In this part of the lab, you will have Processing do the calculations for you. To do this you must:
Create a new Processing program, and copy Program 1 into it.
Program 1: Complex Number Setup Program |
|
The above program shows how a complex number can be created and used. In class we will go over this notation in more detail. Before you can run this program, you must add the code which defines the complex numbers. We do this next.
Add a new tab to your sketch program and copy in the code below. To add a new tab, press the small arrow next to the name tag (see picture below) and select "New Tab" :
At the bottom where it says "Name for new file:", enter a name (e.g. give it the name "ComplexClass")
Next, paste the code below (Program 2) into this new tab. Note that you need to include this in any program where you use complex numbers. Look through this class and take note of all of the function names. These functions define the available Complex number operations, e.g. conjugate and cMult. You must examine the function declaration to see what parameters it take and what, if anything, it returns.
Program 2: Complex Number Class |
|
Once you have added this tab, you can run the program. Experiment creating different complex numbers and performing different operations on them. The goal is for you to become comfortable working with the class/object syntax.
Once you understand the syntax, modify the setup program so as to check your answers to written problems 2 through 5.
Recall that a complex number can be written in standard form
When you multiply two complex numbers in polar form, you get:
z1 z2 = r1 ei Θ1 r2 ei Θ2
= r1r2 ei (Θ1+Θ2)
= r ei Θ where r = r1r2 and angle Θ = (Θ1+Θ2)
In other words, multiplying two complex numbers results in a new complex number with modulus r equal to the product of the individual moduli, and angle Θ equal to the sum of the individual angles.
We can also think of multiplication as a transformation. Suppose we are given a complex number z = r ei Θ. We want to transform it by multiplying by another complex number w = s ei Φ. If w has a modulus of s=1, then we get a pure rotation by the angle Φ as shown here:
![]() |
z → w z = ei Φ r ei Θ = r ei (Φ + Θ) |
![]() |
z → w z = s ei 0 r ei Θ = s r ei (Θ) = s z |
In the image below, we have transformed the image on the left to obtain the image on the right. Each pixel in the image on the left can be thought of as being located in the complex plane at some position z. We can transform the pixel by applying some set of complex operations which take z to z'. We then replace the pixel color at location z with the pixel color at location z'. If z' is outside the bounds of the original image, then we set the pixel color at z to black.
![]() |
![]() |
To generate similar images of your own, create a new Processing Sketch and copy the code below (Program 3) into your sketch. As was done before, you also need to add a new tab containing the Complex Number class (Program 2). You do not need to understand everything in the code. What you do need to understand is that the code assumes the origin of the complex plane is at the center of the image, and the width of the image is 4, as shown in the image to the right.
The transformation we apply in the code below is z → z' = z^2 + 1. This is set in the function called transform which is highlighted with red text. Your task is to change the contents of this function so that you get the transformations listed below.
Program 3: Complex Image Tranformation | |
|
![]() |
Modify the transform function in the code to do the items (1-8) listed below. The images to the right show an example of an original and transformed image. You should use your own image. Use an image which makes the transformation easy to see.
You may use the same Processing sketch for all of the transformations. To do this, it
is recommended that you write
the transform function so that it contains each of the transformations listed below but only
returns the one you specifically want to draw. For example:
Complex transform(Complex c) {
// Compute translation:
Complex translation = ....
// Compute rotation = ...
Complex rotation = ...
etc ...
// return the name of the one you want to use:
return translation;
}
1. Translation:
z → z + w, where w=.2 + .4 i | ![]() | ![]() |
2. Rotation by Θ=45° :
z → w*z, where w = ei Θ (Hint: to create w, use the polar2Cart function) | ![]() | ![]() |
3. Pure scale:
z → .5*z, | ![]() | ![]() |
4. Power:
z → z5 | ![]() | ![]() |
5. Power and translation:
z → z3 - 2 | ![]() | ![]() |
6. Square root:
z → √z | ![]() | ![]() |
7. Inversion:
z → 1/z | ![]() | ![]() |
8. Be Creative!
Pick an interesting transformation of your own choosing. |
At the beginning of class on Friday, April 11:
Place 9 images (the original and the eight transformed) on gorr-classes, in the folder CS145/Lab7/FinalImages. It is important that you name the files by their transformation, e.g. original, scale, translation, etc.
Place the single Processing project containing code for each of the eight transformations on gorr-classes, in the folder CS145/Lab7/ProcessingProjects.
Be prepared to demonstrate your program in class.