Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Functions - Matrix - Transpose of a Matrix

# Functions and Matrix
# Transpose of a Matrix

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Transpose
def fn_transpose(MA, r, c):
    M = []
    for i in range(0,c,1):
        tmp = []
        for j in range(0,r,1):
            ele = MA[j][i]
            tmp.append(ele)
        M.append(tmp)
    return M

# Main Program

import sys

Mat_A = []
Mat_Transpose = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))

# Transpose
Mat_A = fn_read(r1, c1)
       
print("Given Matrix : ")   
fn_display(Mat_A)
   
Mat_Transpose = fn_transpose(Mat_A, r1, c1)
print("Transpose of given matrices : ")
fn_display(Mat_Transpose)   
   

Functions - Matrix - Sum of diagonal elements

# Functions and Matrix
# Matrix Sum of diagonal elements

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Sum of diagonal elements
def fn_add(MA, r):
    s = 0
    for i in range(0,r,1):
        s += MA[i][i]
    return s

# Main Program

import sys

Mat_A = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))

# Addition and Subtraction
if (r1 != c1):
    print("Not a square matrix.")
    print("Cannot Add the diagonal elements.")
else:
    Mat_A = fn_read(r1, c1)
       
    print("Given Matrix : ")   
    fn_display(Mat_A)
   
    dsum = fn_add(Mat_A, r1)
    print("Sum of diagonal elements of given matrices :", dsum)   
   

Functions - Matrix - Multiplication

# Functions and Matrix
# Matrix Multiplication

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Matrix Multiplication
def fn_mul(MA, MB, ra, ca, cb):
    M = []
    for i in range(0,ra,1):
        tmp = []
        for j in range(0,cb,1):
            ele = 0
            for k in range(0,ca,1):
                ele = ele + (Mat_A[i][k] * Mat_B[k][j])
            tmp.append(ele)
        M.append(tmp)
    return M


# Main Program

import sys

Mat_A = []
Mat_B = []
Mat_Mul = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Multiplication
if (c1 != r2):
    print("Rows and columns do not match.")
    print("Cannot multiply.")
else:
    Mat_A = fn_read(r1, c1)
    Mat_B = fn_read(r2, c2)
   
    print("Given matrix 1 : ")   
    fn_display(Mat_A)
    print("Given matrix 2 : ")   
    fn_display(Mat_B)
       
    Mat_Mul = fn_mul(Mat_A, Mat_B, ra = r1, ca = c1, cb = c2)
    print("Product of given matrices : ")
    fn_display(Mat_Mul)

Functions - Matrix - Subtraction

# Functions and Matrix
# Matrix Subtraction

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Matrix Subtraction
def fn_sub(MA, MB, r, c):
    M = []
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = MA[i][j] - MB[i][j]
            tmp.append(ele)
        M.append(tmp)
    return M
   
# Main Program

import sys

Mat_A = []
Mat_B = []
Mat_Sub = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Subtraction
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Subtract the matrices.")
else:
    Mat_A = fn_read(r1, c1)
    Mat_B = fn_read(r2, c2)
   
    print("Given matrix 1 : ")   
    fn_display(Mat_A)
    print("Given matrix 2 : ")   
    fn_display(Mat_B)
   
    Mat_Sub = fn_sub(Mat_A, Mat_B, r1, c1)
    print("Difference of given matrices : ")
    fn_display(Mat_Sub)
   

Functions - Matrix - Addition

# Functions and Matrix
# Matrix Addition

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Matrix Addition
def fn_add(MA, MB, r, c):
    M = []
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = MA[i][j] + MB[i][j]
            tmp.append(ele)
        M.append(tmp)
    return M

# Main Program

import sys

Mat_A = []
Mat_B = []
Mat_Add = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Addition
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Add the matrices.")
else:
    Mat_A = fn_read(r1, c1)
    Mat_B = fn_read(r2, c2)
   
    print("Given matrix 1 : ")   
    fn_display(Mat_A)
    print("Given matrix 2 : ")   
    fn_display(Mat_B)
   
    Mat_Add = fn_add(Mat_A, Mat_B, r1, c1)
    print("Sum of given matrices : ")   
    fn_display(Mat_Add)
   

List - Matrix Multiplication

# MatrixMultiplication

Mat_A = []
Mat_B = []
Mat_Mul = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Multiplication
if (c1 != r2):
    print("Rows and columns do not match.")
    print("Cannot multiply.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
       
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c2,1):
            ele = 0
            for k in range(0,c1,1):
                ele = ele + (Mat_A[i][k] * Mat_B[k][j])
            tmp.append(ele)
        Mat_Mul.append(tmp)
       
    print("Product of given matrices : ")
   
    for row in Mat_Mul:
        for ele in row:
            print(ele, end=" ")
        print()

List - Matrix Subtraction

# Matrix Subtraction

Mat_A = []
Mat_B = []

Mat_Sub = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))
 
# Subtraction
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Subtract the matrices.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)

    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = Mat_A[i][j] - Mat_B[i][j]
            tmp.append(ele)
        Mat_Sub.append(tmp)
   
    print("Difference of given matrices : ")
   
    for row in Mat_Sub:
        for ele in row:
            print(ele, end=" ")
        print()
  

List - Matrix Addition

# Matrix Addition

Mat_A = []
Mat_B = []
Mat_Add = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Addition
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Add the matrices.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
   
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = Mat_A[i][j] + Mat_B[i][j]
            tmp.append(ele)
        Mat_Add.append(tmp)

    print("Sum of given matrices : ")
   
    for row in Mat_Add:
        for ele in row:
            print(ele, end=" ")
        print()