Skip to content

Commit 1b691d0

Browse files
committed
Add and update codes 2
- Better README.md as well
1 parent 9ce5fa3 commit 1b691d0

32 files changed

+1042
-674
lines changed

CGL/A2.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using namespace std;
77

88
/*
9-
* 1001 | 1000 | 1010
10-
* ------------------
11-
* 0001 | 0000 | 0010
12-
* ------------------
9+
* 1001 | 1000 | 1010
10+
* ------------------
11+
* 0001 | 0000 | 0010
12+
* ------------------
1313
* 0101 | 0100 | 0110
1414
*
1515
* Above : First bit
File renamed without changes.

CGL/C6-B.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ void keyboard(unsigned char key, int x, int y) {
9292
sz += 0.1;
9393
break;
9494
case 'x':
95-
sx = std::max(0.1f, sx - 0.1f);
96-
sy = std::max(0.1f, sy - 0.1f);
97-
sz = std::max(0.1f, sz - 0.1f);
95+
sx = max(0.1f, sx - 0.1f);
96+
sy = max(0.1f, sy - 0.1f);
97+
sz = max(0.1f, sz - 0.1f);
9898
break; // Scale down
9999
case 27: exit(0); break; // ESC key to exit
100100
}

DSL/A/A10.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,9 @@ def main_menu():
189189
else:
190190
print("\nInvalid choice.")
191191

192-
if __name__ == "__main__":
193-
main_menu()
192+
main_menu()
194193

195194
"""
196-
if __name__ == "__main__":
197195
M1 = [[1, 0], [0, 2]]
198196
M2 = [[3, 0, 5],[0, 7, 0]]
199197
sparse_M1 = convert(M1)

DSL/A/A6.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,4 @@ def main():
8282
else:
8383
print("Invalid choice. Please try again.")
8484

85-
if __name__ == "__main__":
86-
main()
85+
main()

DSL/A/A7Extra.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,4 @@ def main():
9595
else:
9696
print("Invalid choice. Try again.")
9797

98-
if __name__ == "__main__":
99-
main()
98+
main()

DSL/A/A8Pure.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ def findSaddlePoint(M):
2424
return True
2525
return False
2626

27-
if __name__ == '__main__':
28-
M = [
29-
[1, 2, 3],
30-
[4, 5, 6],
31-
[7, 8, 9] # Output: 7
32-
]
27+
M = [
28+
[1, 2, 3],
29+
[4, 5, 6],
30+
[7, 8, 9] # Output: 7
31+
]
3332

34-
if(findSaddlePoint(M) == False):
35-
print("No Saddle Point")
33+
if(findSaddlePoint(M) == False):
34+
print("No Saddle Point")

DSL/A/A9.py

Lines changed: 170 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,170 @@
1-
"""
2-
9. WAPP to compute following computation on matrix:
3-
4-
a) Addition of two matrices
5-
b) Subtraction of two matrices
6-
c) Multiplication of two matrices
7-
d) Transpose of a matrix
8-
"""
9-
10-
import numpy
11-
x = numpy.array([[1, 2], [4, 5]])
12-
y = numpy.array([[7, 8], [9, 10]])
13-
print("Addition of matrices is: ")
14-
print(numpy.add(x,y))
15-
16-
print("Subtraction of the two matrices: ")
17-
print(numpy.subtract(x,y))
18-
print("The product of the two matrices is: ")
19-
print(numpy.dot(x,y))
20-
print("The transpose of given matrix is: ")
21-
print(x.T)
1+
class Matrices:
2+
def __init__(self):
3+
self.rows = 0
4+
self.columns = 0
5+
self.rowsOther = 0
6+
self.columnsOther = 0
7+
self.matrix = []
8+
self.matrix2 = []
9+
self.matrix3 = []
10+
self.matrix4 = []
11+
self.matrix5 = []
12+
13+
def inputM(self):
14+
self.rows = int(input("Enter number of rows: "))
15+
self.columns = int(input("Enter number of columns: "))
16+
print(f"You will enter {self.rows*self.columns} elements individually in Row by Row fashion")
17+
for i in range(self.rows):
18+
row = []
19+
for j in range(self.columns):
20+
m = int(input(f"Enter element ({i}, {j}): "))
21+
row.append(m)
22+
self.matrix.append(row)
23+
24+
def displayM(self):
25+
print("==========")
26+
for i in range(self.rows):
27+
for j in range(self.columns):
28+
print(self.matrix[i][j], end=" ")
29+
print()
30+
31+
def transpose(self):
32+
print("==========")
33+
print("Tranpose: ")
34+
for i in range(self.columns):
35+
for j in range(self.rows):
36+
print(self.matrix[j][i], end=" ")
37+
print()
38+
print("==========")
39+
40+
def inputM2(self):
41+
self.rowsOther = int(input("Enter number of rows: "))
42+
self.columnsOther = int(input("Enter number of columns: "))
43+
print(f"You will enter {self.rowsOther*self.columnsOther} elements individually in Row by Row fashion")
44+
self.matrix2 = []
45+
for i in range(self.rowsOther):
46+
rowOther = []
47+
for j in range(self.columnsOther):
48+
m = int(input(f"Enter element ({i}, {j}): "))
49+
rowOther.append(m)
50+
self.matrix2.append(rowOther)
51+
52+
def displayM2(self):
53+
print("==========")
54+
print("Second Matrix:")
55+
for i in range(self.rowsOther):
56+
for j in range(self.columnsOther):
57+
print(self.matrix2[i][j], end=" ")
58+
print()
59+
60+
def addition(self):
61+
if self.rows != self.rowsOther or self.columns != self.columnsOther:
62+
print("Matrices cannot be added.")
63+
else:
64+
for i in range(self.rows):
65+
M3 = []
66+
for j in range(self.columns):
67+
c = self.matrix[i][j] + self.matrix2[i][j]
68+
M3.append(c)
69+
self.matrix3.append(M3)
70+
print("==========")
71+
print("Addition is: ")
72+
for i in range(self.rowsOther):
73+
for j in range(self.columnsOther):
74+
print(self.matrix3[i][j], end=" ")
75+
print()
76+
print("==========")
77+
78+
def subtraction(self):
79+
if self.rows != self.rowsOther or self.columns != self.columnsOther:
80+
print("Matrices cannot be subtracted.")
81+
else:
82+
for i in range(self.rows):
83+
M3 = []
84+
for j in range(self.columns):
85+
c = self.matrix[i][j] - self.matrix2[i][j]
86+
M3.append(c)
87+
self.matrix3.append(M3)
88+
print("==========")
89+
print("Subtraction is: ")
90+
for i in range(self.rowsOther):
91+
for j in range(self.columnsOther):
92+
print(self.matrix3[i][j], end=" ")
93+
print()
94+
print("==========")
95+
96+
def multiplication(self):
97+
if self.rowsOther != self.columns:
98+
print("Matrices cannot be multiplied.")
99+
return
100+
else:
101+
for i in range(self.rows):
102+
row = []
103+
for i in range(self.columnsOther):
104+
row.append(0)
105+
self.matrix5.append(row)
106+
# Or self.matrix5 = [[0] * self.columnsOther for _ in range(self.rows)]
107+
# self.matrix5 = [[0 for _ in range(self.columnsOther)] for _ in range(self.rows)]
108+
for i in range(self.rows):
109+
for j in range(self.columnsOther):
110+
for k in range(self.rowsOther):
111+
# Or can iterate over self.columns
112+
self.matrix5[i][j] += self.matrix[i][k]*self.matrix2[k][j]
113+
print("==========")
114+
print("Multiplication is:")
115+
for i in range(self.rows):
116+
for j in range(self.columnsOther):
117+
print(self.matrix5[i][j], end=" ")
118+
print()
119+
"""
120+
for row in self.matrix5:
121+
for value in row:
122+
print(value, end=" ")
123+
print()
124+
"""
125+
print("==========")
126+
127+
def main():
128+
while True:
129+
ops = Matrices()
130+
print("Make your choice!\n 1. Addition\n 2. Subtraction\n 3. Tranpose\n 4. Multiplication\n 5. Exit")
131+
try:
132+
choice = int(input("Enter your choice: "))
133+
except ValueError:
134+
print("Invalid input! Please enter a number")
135+
continue
136+
137+
if choice == 1:
138+
print("Enter first matrix")
139+
ops.inputM()
140+
ops.displayM()
141+
print("Now enter second matrix")
142+
ops.inputM2()
143+
ops.displayM2()
144+
ops.addition()
145+
elif choice == 2:
146+
print("Enter first matrix")
147+
ops.inputM()
148+
ops.displayM()
149+
print("Now enter second matrix")
150+
ops.inputM2()
151+
ops.displayM2()
152+
ops.subtraction()
153+
elif choice == 3:
154+
ops.inputM()
155+
ops.displayM()
156+
ops.transpose()
157+
elif choice == 4:
158+
ops.inputM()
159+
ops.displayM()
160+
ops.inputM2()
161+
ops.displayM2()
162+
ops.multiplication()
163+
elif choice == 5:
164+
print("The program ended!")
165+
break
166+
else:
167+
print("Invalid choice! Select a valid option.")
168+
169+
main()
170+

DSL/A/A9Compact.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ def main():
7070
else:
7171
print("Invalid choice! Select a valid option.")
7272

73-
if __name__ == "__main__":
74-
main()
73+
main()
7574

7675
"""
7776
Menu:

DSL/A/A9Numpy.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
9. WAPP to compute following computation on matrix:
3+
4+
a) Addition of two matrices
5+
b) Subtraction of two matrices
6+
c) Multiplication of two matrices
7+
d) Transpose of a matrix
8+
"""
9+
10+
import numpy
11+
x = numpy.array([[1, 2], [4, 5]])
12+
y = numpy.array([[7, 8], [9, 10]])
13+
print("Addition of matrices is: ")
14+
print(numpy.add(x,y))
15+
16+
print("Subtraction of the two matrices: ")
17+
print(numpy.subtract(x,y))
18+
print("The product of the two matrices is: ")
19+
print(numpy.dot(x,y))
20+
print("The transpose of given matrix is: ")
21+
print(x.T)

0 commit comments

Comments
 (0)