dream2503
dream2503
Dream
1 post
vibing with fake scenarios
Don't wanna be here? Send us removal request.
dream2503 · 2 years ago
Text
class Matrix():
   
    def matrix():
         print("""All functions-
- Matrix.addition(m1, m2)
- Matrix.identity(m)
- Matrix.input()
- Matrix.isSkewSymmetric(m)
- Matrix.isSymmetric(m)
- Matrix.multiplication(m1, m2)
- Matrix.null(m, n)
- Matrix.order(m)
- Matrix.print(m)
- Matrix.scalarMultiplication(n, m)
- Matrix.subtraction(m1, m2)
- Matrix.transpose(m)""")
 
 
    def addition(m1, m2):
        """Add two matrix and returns the resultant matrix"""
        """REQUIRES - Matrix.null,  Matrix.order"""
        if type(m1)==list and type(m2)==list:
            order1=Matrix.order(m1)
            order2=Matrix.order(m2)
            if order1==order2:
                L=Matrix.null(order1[0], order1[1])
                for i in range(order1[0]):
                    for j in range(order2[1]):
                        L[i][j]+=m1[i][j]+m2[i][j]
            else:
                return 'Invalid order for addition'
        else:
            return 'Invalid matrix'
        return L
 
    def identity(m, n=1):
        """Creates a identity matrix of 1 (default) for given order"""
        """REQUIRES - Matrix.null"""
        if type(m)==int:    
            L=Matrix.null(m)
            for i in range(m):
                for j in range(m):
                    if i==j:
                        L[i][j]=n
        else:
            return 'Invalid input'
        return L
 
    def input(m, n=None):
        """Creates a matrix of given order and returns the matrix """
        if type(m)==int:
            if n==None:
                n=m
            elif type(n)!=int:
                return 'Invalid order'
            L=[]
            for i in range(m):
                row=eval(input(f'Enter {i+1}th row ([x, y, z]): '))
                if len(row)==n:
                    L.append(row)
                else:
                    return 'Invalid matrix elements'
        else:
            return 'Invalid order input'
        return L
 
    def isSkewSymmetric(m):
        """Checks if the given matrix is skew symmetric and returns a boolean value"""
        """REQUIRES - Matrix.scalarMultiplication, Matrix.transpose"""
        if type(m)==list and type(m[0])==list and len(m)==len(m[0]):
            if m==Matrix.scalarMultiplication(-1, Matrix.transpose(m)):
                return True
            else:
                return False
        else:
            return 'invalid square matrix'    
           
    def isSymmetric(m):
        """Checks if the given matrix is symmetric and returns a boolean value"""
        """REQUIRES - Matrix.transpose"""
        if type(m)==list and type(m[0])==list and len(m)==len(m[0]):
            if m==Matrix.transpose(m):
                return True
            else:
                return False
        else:
            return 'Invalid square matrix'
 
    def multiplication(m1, m2):
        """Multiplies two matrix and returns the resultant matrix"""
        """REQUIRES - Matrix.null, Matrix.order"""
        if type(m1)==list and type(m2)==list:
            order1=Matrix.order(m1)
            order2=Matrix.order(m2)
            if order1[1]==order2[0]:
                L=Matrix.null(order1[0], order2[1])
                for k in range(order1[0]):
                    for i in range(order2[1]):
                        for j in range(order1[0]):
                            L[k][i]+=m1[k][j]*m2[j][i]
            else:
                return 'Invalid matrix order to multiply'
        else:
            return 'Invalid matrix'
        return L
 
    def null(m, n=None):
        """Creates a null matrix for given order and returns the matrix"""
        if type(m)==int:
            if n==None:
                n=m
            elif type(n)!=int:
                return 'Invalid order'    
            L=[]
            for i in range(m):
                L.append([])
                for j in range(n):
                    L[i].append(0)
        else:
            return 'Invalid input'
        return L
 
    def order(m):
        """Returns the order of the given matrix"""
        if type(m)==list and type(m[0])==list:
            return [len(m), len(m[0])]
        else:
            return 'Invalid matrix'
 
    def print(m):
        """Prints the given matrix"""
        if type(m)==list and type(m[0])==list:
            for i in range(len(m)):
                print(m[i])
 
    def scalarMultiplication(n, m):
        """Multiplies a matrix with a scalar value and returns the resultant matrix"""
        """REQUIRES - Matrix.null, Matrix.order"""
        if type(n) in (int, float) and type(m)==list:
            order=Matrix.order(m)
            L=Matrix.null(order[0], order[1])
            for i in range(order[0]):
                for j in range(order[1]):
                    L[i][j]+=n*m[i][j]
        else:
            return 'Invalid matrix or scalar value'
        return L
 
    def subtraction(m1, m2):
        """Subtract two matrix and returns the resultant matrix"""
        """REQUIRES - Matrix.null, Matrix.order"""
        if type(m1)==list and type(m2)==list:
            order1=Matrix.order(m1)
            order2=Matrix.order(m2)
            if order1==order2:
                L=Matrix.null(order1[0], order1[1])
                for i in range(order1[0]):
                    for j in range(order2[1]):
                        L[i][j]+=m1[i][j]-m2[i][j]
            else:
                return 'Invalid order for substraction'
        else:
            return 'Invalid matrix'
        return L
 
    def transpose(m):
        """Returns the transpose of the given matrix"""
        """REQUIRES - Matrix.null, Matrix.order"""
        if type(m)==list and type(m[0])==list and len(m)==len(m[0]):
            order=Matrix.order(m)
            order=order[0]
            L=Matrix.null(order)
            for i in range(order):
                for j in range(order):
                    L[j][i]=m[i][j]
        else:
            return 'invalid square matrix'
        return L
1 note · View note