K E Y|
|Student ID: ||_______________________________|
In this part of the exam you will be designing and coding a Python program.
The sections of the exam correspond to the sections of a lab writeup.
Total points: 40
Design and write a library cplx that implements an abstract
data type for complex numbers, similar to what Python has built-in.
Your library should provide, at a minimum:
- A way for users to create a new complex number
object, as well as access its real and imaginary parts (set/get).
- Functions to add, subtract, and multiply complex numbers.
- A function intpow(c, n) that raises a complex
number c to a positive integer power n ≥ 0.
- A function rand()
that returns a complex number with randomly generated real and imaginary
parts between 0 and 1 (you may use Python's own random number generator).
Recall that complex numbers are a + bj, where a and
b are real numbers and j=sqrt(-1).
Complex numbers multiply like ordinary binomials, but j**2 == -1;
collect the real and imaginary terms together.
Docstrings with pre/post-conditions are required for your library and each
function. Comments are not required but may be helpful for partial credit if
your code isn't perfect.
Problem Suitability: 
Numerical calculations with complex numbers are doable by hand for short,
simple problems, but a computer is designed to do these mathematical
calculations much faster.
Problem Restatement: 
cplx(): two float numbers
real(): a cplx number
imag(): a cplx number
add(): two cplxs
sub(): two cplxs
mul(): two cplxs
intpow(): a cplx and a positive integer
rand(): no parameters
- To Do:
cplx(): make a new cplx number
real(): get the real part of a cplx number
imag(): get the imaginary part of a cplx number
add(): return the sum
sub(): return the difference
mul(): return the product
intpow(): raise the cplx to the given power
rand(): return a new random cplx number
- Desired Result:
cplx(): new cplx
real(): real part (float)
imag(): imaginary part (float)
add(): sum (cplx)
sub(): difference (cplx)
mul(): product (cplx)
intpow(): cplx raised to the given power (cplx)
rand(): random cplx with real and imaginary parts between 0 and 1
Just the random standard library.
Problem Refinement (natural language): 
cplx(): takes two parameters, real and imaginary parts,
and returns a new "cplx object" -- for our internal representation, we'll use a
tuple of two floats.
real(): takes one parameter which is a "cplx object"
(really, just a tuple of two floats), and returns the "real part" (really, just
the first float in the tuple).
imag(): takes one parameter which is a "cplx object"
(really, just a tuple of two floats), and returns the "imaginary part" (really,
just the second float in the tuple).
add(): takes two "cplx objects" as parameters, and
returns a new "cplx object" (a new tuple): add real part to real part, add
imaginary part to imaginary part.
sub(): Similar to add(), but subtract real part from
real part, subtract imaginary part from imaginary part.
mul(): Multiply two "cplx objects" according to
the rules for complex numbers, i2 = -1. Return the product as a new
intpow(): Raise a complex number to a positive integer
power; i.e., iterated multiplication.
rand(): Create and return a new complex number, where
the real part is a random number between 0 and 1, and the imaginary part is a
random number between 0 and 1.
Data Tables: 
No static variables internal to the library (e.g., seed in the
pseudorandom library), but our internal representation of a cplx number
is a tuple of two floats: (real, imag).
Sample I/O: 
For this section, write a short testbed program to demonstrate your library.
This need not have user input; it can just have a few test cases hard-coded.
The testbed program does not need pseudocode.
(See separate file: cplxtest.py)
User Manual: 
cplx(r, i): This is the function you'd use to create a new
instance of our ADT -- a new complex number -- when you know the real and
imaginary parts. It takes two parameters of type float.
real(): This is a "get" accessor function that returns
just the real part of a complex number.
imag(): This is a "get" accessor function that returns
just the imaginary part of a complex number.
add(): This operator takes two complex numbers (such
as are created by cplx()) and returns the sum of the two.
The sum is done component-wise.
sub(): This operator takes two complex numbers (such
as are created by cplx()) and returns the difference: c1 - c2.
mul(): This operator takes two complex numbers (such
as are created by cplx()) and returns the product, using the rules of
complex number multiplication.
intpow(): This operator takes a complex number c and a
positive integer n, and raises the complex number to the n-th power, returning
the result as a complex number.
rand(): This function takes no parameters, and every
time you call it, it returns a different complex number randomly chosen from
within the square in the complex plane bounded by 0+0j, 1+0j, 1+1j, 0+1j.
cplx(): Return a new tuple (r,i)
real(): Return the first entry in the tuple: c
imag(): Return the second entry in the tuple: c
add(): Return a new tuple where the first entry is the
sum of real parts, the second entry is the sum of imaginary parts:
sub(): Make a helper function neg() which
negates real and imaginary parts: then sub(c1,c2) is just add(c1, neg(c2)).
mul(): Use rules of complex multiplication:
(r1*r2 - i1*i2, r1*i2 + r2*i1).
result = 1
repeat n times:
result *= c (using our mul() function)
rand(): Return a new tuple where each entry
is a call to random.random().
(See separate file: cplx.py)