diff --git a/1st Year/07. Matrix Multiplication/matrix_multiplication.py b/1st Year/07. Matrix Multiplication/matrix_multiplication.py new file mode 100644 index 0000000..67780a2 --- /dev/null +++ b/1st Year/07. Matrix Multiplication/matrix_multiplication.py @@ -0,0 +1,75 @@ +# To compute product of two matrices by 2D Array manipulation + +# accept user input to create 2D matrix +def input_matrix(): + row, column = [int(x) for x in input("Enter rows and columns of Matrix: ").split()] + matrix = [] + print("Enter the elements of this matrix:") + for i in range(0, row): + temp = [] + for j in range(0, column): + temp.append(int(input())) + matrix.append(temp) + return matrix + + +# Print 2D matrix with correct format +def print_matrix(matrix): + for i in range(len(matrix)): + for j in range(len(matrix[0])): + print(matrix[i][j], end=" ") + print() + return + + +if __name__ == "__main__": + matrix1 = [] + matrix2 = [] + prduct_matrix = [] + # input two matrics + matrix1 = input_matrix() + print("Matrix 1 are: ") + print_matrix(matrix1) + + matrix2 = input_matrix() + print("Matrix 2 are: ") + print_matrix(matrix2) + + if len(matrix1[0]) != len(matrix2): + print("Matrices are incompatible!, try again.") + exit(0) + + # product matrix rows and cols + rows, cols = (len(matrix1), len(matrix2[0])) + + # init product matrix value + prduct_matrix = [[0 for x in range(cols)] for y in range(rows)] + + # iterate through rows of first matrix + for i in range(rows): + # iterate through columns of second matrix + for j in range(cols): + # iterate through rows of second matrix + for k in range(len(matrix2)): + prduct_matrix[i][j] += matrix1[i][k] * matrix2[k][j] + + print("Product of matrices are: ") + print_matrix(prduct_matrix) + + """ + Sample output + + Matrix 1 are: + 12 7 3 + 4 5 6 + 7 8 9 + Matrix 2 are: + 5 8 1 2 + 6 7 3 0 + 4 5 9 1 + + Product of matrices are: + 114 160 60 27 + 74 97 73 14 + 119 157 112 23 + """ \ No newline at end of file diff --git a/1st Year/08. Compute sin(x)/compute_sinx.py b/1st Year/08. Compute sin(x)/compute_sinx.py new file mode 100644 index 0000000..3d05427 --- /dev/null +++ b/1st Year/08. Compute sin(x)/compute_sinx.py @@ -0,0 +1,28 @@ +## To compute sin x using taylor series approximation +import math + +if __name__ == "__main__": + x = float(input("Enter the value in degrees: ")) + num = int(input("Enter the number of terms in Taylor series expansion: ")) + + # Convert to radians + x = (x * 3.151592) / 180 + term = sum = x + + for i in range(1, 10): + # Term closest to approximation. + term = (- term * x * x) / (2 * i * (2 * i + 1)) + sum += term + + print("Calculated =", sum) + print("Builtin function =", math.sin(x)) + +''' +sample output + +Enter the value in degrees: 90 +Enter the number of terms in Taylor series expansion: 2 +Calculated = 0.9999875016599555 +Builtin function = 0.9999875016599559 + +''' \ No newline at end of file