Skip to content

Commit

Permalink
Add and update codes 2
Browse files Browse the repository at this point in the history
- Better README.md as well
  • Loading branch information
Eshan05 committed Nov 30, 2024
1 parent 9ce5fa3 commit 1b691d0
Show file tree
Hide file tree
Showing 32 changed files with 1,042 additions and 674 deletions.
8 changes: 4 additions & 4 deletions CGL/A2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using namespace std;

/*
* 1001 | 1000 | 1010
* ------------------
* 0001 | 0000 | 0010
* ------------------
* 1001 | 1000 | 1010
* ------------------
* 0001 | 0000 | 0010
* ------------------
* 0101 | 0100 | 0110
*
* Above : First bit
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions CGL/C6-B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void keyboard(unsigned char key, int x, int y) {
sz += 0.1;
break;
case 'x':
sx = std::max(0.1f, sx - 0.1f);
sy = std::max(0.1f, sy - 0.1f);
sz = std::max(0.1f, sz - 0.1f);
sx = max(0.1f, sx - 0.1f);
sy = max(0.1f, sy - 0.1f);
sz = max(0.1f, sz - 0.1f);
break; // Scale down
case 27: exit(0); break; // ESC key to exit
}
Expand Down
4 changes: 1 addition & 3 deletions DSL/A/A10.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,9 @@ def main_menu():
else:
print("\nInvalid choice.")

if __name__ == "__main__":
main_menu()
main_menu()

"""
if __name__ == "__main__":
M1 = [[1, 0], [0, 2]]
M2 = [[3, 0, 5],[0, 7, 0]]
sparse_M1 = convert(M1)
Expand Down
3 changes: 1 addition & 2 deletions DSL/A/A6.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,4 @@ def main():
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()
main()
3 changes: 1 addition & 2 deletions DSL/A/A7Extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,4 @@ def main():
else:
print("Invalid choice. Try again.")

if __name__ == "__main__":
main()
main()
15 changes: 7 additions & 8 deletions DSL/A/A8Pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ def findSaddlePoint(M):
return True
return False

if __name__ == '__main__':
M = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9] # Output: 7
]
M = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9] # Output: 7
]

if(findSaddlePoint(M) == False):
print("No Saddle Point")
if(findSaddlePoint(M) == False):
print("No Saddle Point")
191 changes: 170 additions & 21 deletions DSL/A/A9.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,170 @@
"""
9. WAPP to compute following computation on matrix:
a) Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix
"""

import numpy
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])
print("Addition of matrices is: ")
print(numpy.add(x,y))

print("Subtraction of the two matrices: ")
print(numpy.subtract(x,y))
print("The product of the two matrices is: ")
print(numpy.dot(x,y))
print("The transpose of given matrix is: ")
print(x.T)
class Matrices:
def __init__(self):
self.rows = 0
self.columns = 0
self.rowsOther = 0
self.columnsOther = 0
self.matrix = []
self.matrix2 = []
self.matrix3 = []
self.matrix4 = []
self.matrix5 = []

def inputM(self):
self.rows = int(input("Enter number of rows: "))
self.columns = int(input("Enter number of columns: "))
print(f"You will enter {self.rows*self.columns} elements individually in Row by Row fashion")
for i in range(self.rows):
row = []
for j in range(self.columns):
m = int(input(f"Enter element ({i}, {j}): "))
row.append(m)
self.matrix.append(row)

def displayM(self):
print("==========")
for i in range(self.rows):
for j in range(self.columns):
print(self.matrix[i][j], end=" ")
print()

def transpose(self):
print("==========")
print("Tranpose: ")
for i in range(self.columns):
for j in range(self.rows):
print(self.matrix[j][i], end=" ")
print()
print("==========")

def inputM2(self):
self.rowsOther = int(input("Enter number of rows: "))
self.columnsOther = int(input("Enter number of columns: "))
print(f"You will enter {self.rowsOther*self.columnsOther} elements individually in Row by Row fashion")
self.matrix2 = []
for i in range(self.rowsOther):
rowOther = []
for j in range(self.columnsOther):
m = int(input(f"Enter element ({i}, {j}): "))
rowOther.append(m)
self.matrix2.append(rowOther)

def displayM2(self):
print("==========")
print("Second Matrix:")
for i in range(self.rowsOther):
for j in range(self.columnsOther):
print(self.matrix2[i][j], end=" ")
print()

def addition(self):
if self.rows != self.rowsOther or self.columns != self.columnsOther:
print("Matrices cannot be added.")
else:
for i in range(self.rows):
M3 = []
for j in range(self.columns):
c = self.matrix[i][j] + self.matrix2[i][j]
M3.append(c)
self.matrix3.append(M3)
print("==========")
print("Addition is: ")
for i in range(self.rowsOther):
for j in range(self.columnsOther):
print(self.matrix3[i][j], end=" ")
print()
print("==========")

def subtraction(self):
if self.rows != self.rowsOther or self.columns != self.columnsOther:
print("Matrices cannot be subtracted.")
else:
for i in range(self.rows):
M3 = []
for j in range(self.columns):
c = self.matrix[i][j] - self.matrix2[i][j]
M3.append(c)
self.matrix3.append(M3)
print("==========")
print("Subtraction is: ")
for i in range(self.rowsOther):
for j in range(self.columnsOther):
print(self.matrix3[i][j], end=" ")
print()
print("==========")

def multiplication(self):
if self.rowsOther != self.columns:
print("Matrices cannot be multiplied.")
return
else:
for i in range(self.rows):
row = []
for i in range(self.columnsOther):
row.append(0)
self.matrix5.append(row)
# Or self.matrix5 = [[0] * self.columnsOther for _ in range(self.rows)]
# self.matrix5 = [[0 for _ in range(self.columnsOther)] for _ in range(self.rows)]
for i in range(self.rows):
for j in range(self.columnsOther):
for k in range(self.rowsOther):
# Or can iterate over self.columns
self.matrix5[i][j] += self.matrix[i][k]*self.matrix2[k][j]
print("==========")
print("Multiplication is:")
for i in range(self.rows):
for j in range(self.columnsOther):
print(self.matrix5[i][j], end=" ")
print()
"""
for row in self.matrix5:
for value in row:
print(value, end=" ")
print()
"""
print("==========")

def main():
while True:
ops = Matrices()
print("Make your choice!\n 1. Addition\n 2. Subtraction\n 3. Tranpose\n 4. Multiplication\n 5. Exit")
try:
choice = int(input("Enter your choice: "))
except ValueError:
print("Invalid input! Please enter a number")
continue

if choice == 1:
print("Enter first matrix")
ops.inputM()
ops.displayM()
print("Now enter second matrix")
ops.inputM2()
ops.displayM2()
ops.addition()
elif choice == 2:
print("Enter first matrix")
ops.inputM()
ops.displayM()
print("Now enter second matrix")
ops.inputM2()
ops.displayM2()
ops.subtraction()
elif choice == 3:
ops.inputM()
ops.displayM()
ops.transpose()
elif choice == 4:
ops.inputM()
ops.displayM()
ops.inputM2()
ops.displayM2()
ops.multiplication()
elif choice == 5:
print("The program ended!")
break
else:
print("Invalid choice! Select a valid option.")

main()

3 changes: 1 addition & 2 deletions DSL/A/A9Compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def main():
else:
print("Invalid choice! Select a valid option.")

if __name__ == "__main__":
main()
main()

"""
Menu:
Expand Down
21 changes: 21 additions & 0 deletions DSL/A/A9Numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
9. WAPP to compute following computation on matrix:
a) Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix
"""

import numpy
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])
print("Addition of matrices is: ")
print(numpy.add(x,y))

print("Subtraction of the two matrices: ")
print(numpy.subtract(x,y))
print("The product of the two matrices is: ")
print(numpy.dot(x,y))
print("The transpose of given matrix is: ")
print(x.T)
Loading

0 comments on commit 1b691d0

Please sign in to comment.