-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcomplex.py
71 lines (57 loc) · 1.88 KB
/
complex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''Complex Numbers from the book'''
from math import sqrt, degrees,atan2, sin, cos,radians
def cAdd(a,b):
return [a[0]+b[0],a[1]+b[1]]
def cMult(u,v):
'''Returns the product of two complex numbers'''
return [u[0]*v[0]-u[1]*v[1],u[1]*v[0]+u[0]*v[1]]
def theta(z):
'''Calculates the angle of rotation of a complex number'''
return degrees(atan2(z[1],z[0]))
def magnitude(z):
return sqrt(z[0]**2 + z[1]**2)
def power(z,n):
r = magnitude(z)
angle = (1/3)*radians(theta(z)+360)
return [r**n*cos(n*angle),r**n*sin(n*angle)]
def synthDiv(divisor,dividend):
'''divides a polynomial by a constant and returns a lower-degree polynomial. Enter divisor as a constant: (x - 3) is 3
Enter dividend as a list of coefficients:
x**2 – 5*x + 6 becomes [1,-5,6]'''
quotient = [] #empty list for coefficients of quotient
row2 = [0] #start the second row
for i in range(len(dividend)):
quotient.append(dividend[i]+row2[i]) #add the ith column
row2.append(divisor*quotient[i]) #put the new number in row 2
print(quotient)
def quad(a,b,c):
'''Returns the solutions of an equation
of the form a*x**2 + b*x + c = 0'''
x1 = (-b + sqrt(b**2 - 4*a*c))/(2*a)
x2 = (-b - sqrt(b**2 - 4*a*c))/(2*a)
return x1,x2
def f(x):
return x**3 - 15*x - 4
def average(a,b):
return (a + b) / 2
def guess():
lower = -3
upper = -4
for i in range(20):
midpt = average(lower,upper)
if f(midpt) == 0:
return midpt
elif f(midpt) < 0:
upper = midpt
else:
lower = midpt
return midpt
def arange(start,stop,step):
'''Returns a list of numbers from
start to stop by step'''
output = []
x = start
while x < stop:
output.append(x)
x += step
return output