Skip to content

Commit 36a6647

Browse files
committed
first set
1 parent baf3680 commit 36a6647

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

wee7marks.PNG

88.3 KB
Loading

week1.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import math
2+
def cos(n):
3+
return math.cos(n)
4+
def tan(n):
5+
return math.tan(n)
6+
7+
def cartesian_to_polar(x,y):
8+
# Calculating radius
9+
radius = math.sqrt( x * x + y * y )
10+
# Calculating angle (theta) in radian
11+
theta = math.atan(y/x)
12+
if x<0 and not y<0:
13+
theta=math.pi+theta
14+
return radius,theta
15+
16+
def polar_to_cartesian(radius,theta):
17+
x = radius * math.cos(theta)
18+
y = radius * math.sin(theta)
19+
return x,y
20+
21+
if __name__=="__main__":
22+
pi=math.pi
23+
#1st question
24+
a=pi/4
25+
print(f"1) {math.sin(a):.2f}")
26+
#2nd question
27+
a=pi/2
28+
print(f"2) {math.sin(a):.2f}")
29+
#3rd question
30+
a=pi/3
31+
print(f"3) {math.sin(a):.2f}")
32+
#4th question
33+
a=0
34+
print(f"4) {math.sin(a):.2f}")
35+
#fifth question
36+
b=0
37+
print(f"5) {math.cos(b):.2f}")
38+
#questions 6-10
39+
quantity={6:pi/3,7:3*pi/4,8:-pi/6,9:2*pi/3,10:5*pi/4}
40+
for i in range(6,11):
41+
if i in[6,7,8]:
42+
print(f"{i} {cos(quantity[i]): .2f}")
43+
else:
44+
print(f"{i} {tan(quantity[i]): .2f}")
45+
#questions 11-20
46+
quantity={11:[3,4],12:[math.sqrt(3),1],13:[-2,2],14:[0,1],15:[5,12],16:[1,0],17:[3,pi/4],18:[3,9*pi/4],
47+
19:[5,2*pi/3],20:[2,11*pi/6]}
48+
for i in range(11,21):
49+
50+
if i in (11,12,13,15):
51+
52+
print(f"{i} {cartesian_to_polar(*quantity[i])}")
53+
elif i==14:
54+
print(f"{i} {pi/2}")
55+
else:
56+
print(f"{i} {polar_to_cartesian(*quantity[i])}")
57+
58+
59+
60+
61+

week3.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
import cmath
3+
from week1 import cartesian_to_polar as cf
4+
5+
print(cf(-15,-8))
6+
print(cf(math.sqrt(1/3),math.sqrt(2/3)))
7+
8+
#multiplying complex numbers
9+
10+
def cmultiply(a,b):
11+
return a*b
12+
print(cmultiply(2-7j,3-2j))
13+
14+
#magnitude of complex numbers
15+
16+
print(abs(complex(5,-12)))
17+
18+
#converting complex numbers to polar form
19+
def cm2polar(a):
20+
"""
21+
converts the complex number into polar form,returns a tuple r,phi where r is
22+
modulus, phi is phase angle
23+
"""
24+
return cmath.polar(a)
25+
26+
def polar_display(a,b):
27+
"""
28+
displaying the polar form of complex number a+ib in conventional form
29+
"""
30+
q=math.pi/b
31+
return f"{a}e**iPI/{q}"
32+
33+
r,phi=cm2polar(complex(0,5))
34+
print(polar_display(r,phi))

week4.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
def vector_inner_product(a,b):
2+
if len(a)!=len(b):
3+
return "The two vectors should be of same dimensions"
4+
product=0
5+
for i in range(len(a)):
6+
product=product+a[i]*b[i]
7+
return product
8+
9+
def check_orthogonal(a,b):
10+
if vector_inner_product(a,b)==0:
11+
return True
12+
else:
13+
return False
14+
15+
def linear_combination_len2(r,a,b):
16+
"""
17+
returns a linear combinations of vectors of length 2,solving by elimination
18+
1st equation coefficients-
19+
rightside=r[0]
20+
x_coefficient=a[0]
21+
y_coefficient=b[0]
22+
2nd equation coefficients
23+
rightside=r[1]
24+
x_coefficient=a[1]
25+
y_coefficient=b[1]
26+
"""
27+
original=(r[0],a[0],b[0])
28+
if a[0]>=a[1]:
29+
multiplier=a[0]/a[1]
30+
r[1]=r[1]*multiplier
31+
a[1]=a[1]*multiplier
32+
b[1]=b[1]*multiplier
33+
else:
34+
multiplier=a[1]/a[0]
35+
r[0]=multiplier*r[0]
36+
a[0]=multiplier*a[0]
37+
b[0]=multiplier*b[0]
38+
39+
assert a[0]-a[1]==0
40+
y=(r[0]-r[1])/(b[0]-b[1])
41+
x=(original[0]-y*original[2])/original[1]
42+
return x,y
43+
44+
def transpose(p):
45+
"""returns the transpose t of a matrix p"""
46+
t=[]
47+
for i in range(len(p)):
48+
row=[]
49+
for j in range(len(p[i])):
50+
row.append(p[j][i])
51+
t.append(row)
52+
return t
53+
54+
def matrix_multiply(a,b):
55+
res=[]
56+
for row in a:
57+
r=[]
58+
for column in b:
59+
r.append(vector_inner_product(row,column))
60+
res.append(r)
61+
return res
62+
63+
64+
65+
66+
67+
if __name__ == "__main__":
68+
print("1) ",vector_inner_product([5,2],[6,8]))
69+
print("2) ",vector_inner_product([3,10],[1,7]))
70+
print("3) ",vector_inner_product([1,-2,4],[2,3,3]))
71+
print("4) ",check_orthogonal([1,5],[-5,1]))
72+
print("5) ",check_orthogonal([3,6,2],[1,4,3]))
73+
print("6) ", linear_combination_len2([5,5],[1,2],[3,1]))
74+
print("7) ", linear_combination_len2([-7,16],[1,2],[3,1]))
75+
print("8 ",matrix_multiply([[2,3],[1,4]],[[3,-2]]))
76+
print("9 ",matrix_multiply([[1,4],[0,5]],[[1,1]]))
77+
print("10 ",matrix_multiply([[-2,6],[9,4]],[[3,-5],[3,1]]))
78+
79+
80+

week7.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#My solutions to the homework assignments of week7
2+
"""
3+
1)option 2
4+
2)3
5+
3)1
6+
4)color
7+
5)seashells==3
8+
6)2
9+
7)2 4
10+
8)4
11+
9)1
12+
10)x*y
13+
11)0
14+
12)2
15+
13)year age
16+
14)3
17+
15)4
18+
16)2
19+
17)1
20+
"""

0 commit comments

Comments
 (0)