[ answers in web view ]
 Name: _______________________________ 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

## Problem Statement:

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: 

• Given:
cplx(): two float numbers
real(): a cplx number
imag(): a cplx number
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
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)
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 (cplx)

## Libraries: 

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 "cplx object".

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: 

### Variables:

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.

## Pseudocode: 

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: (r1+r2, i1+i2)

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).

intpow():
```	result = 1
repeat n times:
result *= c (using our mul() function)
return result
```

rand(): Return a new tuple where each entry is a call to random.random().

## Code: 

(See separate file: cplx.py)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7869156,"math_prob":0.97277606,"size":5796,"snap":"2022-27-2022-33","text_gpt3_token_len":1454,"char_repetition_ratio":0.16868094,"word_repetition_ratio":0.108024694,"special_character_ratio":0.2653554,"punctuation_ratio":0.14776632,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99497104,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-12T21:14:51Z\",\"WARC-Record-ID\":\"<urn:uuid:515ca790-8579-4b0e-a802-9d6e17dded19>\",\"Content-Length\":\"9377\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3353592f-6c5e-402a-a784-4bb9a3518136>\",\"WARC-Concurrent-To\":\"<urn:uuid:991d4f93-9297-4b0a-86d8-53411abd56e0>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"https://twu.seanho.com/07fall/cmpt14x/exam2-2.html\",\"WARC-Payload-Digest\":\"sha1:5QEINGF37TC4GMNGVVO66CG6YHIJB5QG\",\"WARC-Block-Digest\":\"sha1:IGGZBKHCSRT5DNIWCVZXLQEVI6DACXIF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571758.42_warc_CC-MAIN-20220812200804-20220812230804-00748.warc.gz\"}"}