Question

In: Computer Science

In python and comments your code : Exercise 1 1.Write a function waiting for two matrices...

In python and comments your code :

Exercise 1

1.Write a function waiting for two matrices and returning their addition, −1 if it is not not possible.
2. Write a function associated with a matrix and a scalar and returning the produced matrix.
3. Write a function accompanying an n × m matrix and a vector n and returning the vector produced by each other, −1 if this is not possible.
4. Write a function accompanying two n × m and m × k matrices and returning the product matrix, −1 if this is not possible.
5.Write a function waiting for a vector (column or row) and returning its transpose.

Exercise 2

1.Write a function waiting for a 2 × 2 matrix and calculating its determinant.
2. Write a function waiting for a matrix A of size n × m and two integers i and j and rendering a matrix (n − 1) × (m − 1), copy the matrix A without row i and column j .
3. Write a determining function of a square matrix by expanding on a line.

Solutions

Expert Solution

1. Function for addition:

def addition(x,y):
    if len(x)!=len(y) or len(x[0])!=len(y[0]):
        return -1
    
    result = [[ 0 for i in range(len(x))] for j in range(len(x[0]))]

    #iterate through rows
    for i in range(len(x)):
        #iterate through columns
        for j in range(len(x[0]):
            result[i][j] = x[i][j]+y[i][j]

    return result

2. Function for Matrix multiplied with Scalar:

def scalarMultiply(x, scalar):
    #traversing through rows
    for i in range(len(x)):
        #traversing through columns
        for j in range(len(y)):
            #multiplying with scalar
            x[i][j]*=scalar

    return scalar

3. Function for multiplication of vector with matrix:

def matrixMultiplication(X,Y):
    result = [[0 for i in range(len(X))] for j in range(len(Y[0]))]
    # iterate through rows of X
    for i in range(len(X)):
        #iterate through columns of Y
        for j in range(len(Y[0])):
           #iterate through rows of Y
           for k in range(len(Y)):
               result[i][j] += X[i][k] * Y[k][j]


def vectorMatrix(x, vector):
    #checking if vector is a column vector
    if len(vector[0])==len(x):
        result = matrixMultiplication(vector,x)

    #checking if vector is a row vector
    elif len(x[0])==len(vector):
        result = matrixMultiplication(x,vector)

    #The matrix and the vector can't be multiplied
    else
        result = -1
    
    return result
        

4. Function for Matrix Multiplication:

def matrixMultiplication(X,Y):
    #Matrices cannot be multiplied
    if len(X[0])!=len(Y):
        return -1

    result = [[0 for i in range(len(X))] for j in range(len(Y[0]))]
    # iterate through rows of X
    for i in range(len(X)):
        #iterate through columns of Y
        for j in range(len(Y[0])):
           #iterate through rows of Y
           for k in range(len(Y)):
               result[i][j] += X[i][k] * Y[k][j]

    return result

5. Function for transpose of a vector:

def transposeVector(vector):
    result = [[0 for i in range(vector)] for j in range(vector[0])]
    #iterate through rows
    for i in range(len(vector)):
        #iterate through columns
        for j in range(len(vector[0])):
            result[j][i] = vector[i][j]

    return result

Related Solutions

Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a string to integer. The function will take the number as a string then will return it as integer. E.g. str2int(“123”) will return 123 str2int(“005”) will return 5 str2int(“102”) will return 102
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
In C++ write a function to find a product of two matrices using arrays. The function...
In C++ write a function to find a product of two matrices using arrays. The function should be general and should accept any size matrices.
In simple python code 1) Write a function to convert the image to grayscale. 2Write a...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a function to convert an image to black and white 3)Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows: newR = (R × 0.393 + G × 0.769 + B × 0.189) newG = (R × 0.349 + G × 0.686 + B × 0.168) newB = (R ×...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to...
how to write a code with python function trying to find minimum difference between any two...
how to write a code with python function trying to find minimum difference between any two elements in a list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT