Question

In: Computer Science

Write the following Python script: Imagine you live in a world without modules in Python! No...

Write the following Python script:

Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it is actually tricky to make a useful list of zeros. For instance, if you need to start with a 5 row, 6 column double list of 0, you might be tempted to try:

'''

thing = [ [ 0 ] ∗ 6 ] ∗ 5

'''

and if you look at it in the console window, it would appear to be a 5 by 6 list of lists containing all zeros! However - try the following and see what happens:

'''

thing [ 2 ] [ 5 ]

thing

'''

Notice that the item in index 5 of every row has changed, not just the row with index 2! Given the difficulty, I will give you the line of code that will create a list of lists that is num_rows by num_cols:

'''

ans = [ [ 0 for col in range ( num_cols ) ] for row in range ( num_rows ) ]

'''

  • The autograder will not allow numpy (or anything else!) to be imported
  • Your solution will likely have a triple loop. Do some matrix multiplications by hand for small and then larger matrices and figure out what processes you are using. Code those.
  • Given that your solution will likely have a triple loop, be careful with your indentation!
  • As noted, trying to create an empty list of list of 0 can be problematic since the interior lists are actually pointing to the same information.

Solutions

Expert Solution

def multiplyMatrix(A,row1, col1, B, row2, col2): 
                          
    # Matrix to store the result  
    C = [[0 for i in range(MAX)] for j in range(MAX)] 
  
    # Checking if the multiplication is Possible or not 
    if (row2 != col1) : 
        print("Invalid Sizes") 
        return None
      
    # Multiplying  
    for i in range(row1) : 
        for j in range(col2) : 
            C[i][j] = 0
            for k in range(row2) : 
                C[i][j] += A[i][k] * B[k][j];  
  
    print("Resultant Matrix(AxB): ") 
    for i in range(row1) :  
        for j in range(col2) :  
            print(C[i][j], end = " ") 
  
        print() 
  
MAX = 1000  
A = [[0 for i in range(MAX)] for j in range(MAX)] 
B = [[0 for i in range(MAX)] for j in range(MAX)]

row1 = int(input("Enter the number of rows of First Matrix: ")) 
col1 = int(input("Enter the number of columns of First Matrix: ")) 
print("Enter the elements of First Matrix: ");  
for i in range(row1) :  
    for j in range(col1) :  
        A[i][j] = int(input()) 
  
row2 = int(input("Enter the number of rows of Second Matrix: "))  
col2 = int(input("Enter the number of columns of Second Matrix: "))  
  
print("Enter the elements of Second Matrix: ");  
for i in range(row2) :  
    for j in range(col2) : 
        B[i][j] = int(input()) 

print("A: ") 
for i in range(row1) :  
    for j in range(col1) :  
        print(A[i][j], end = " ") 
  
    print()
    
print("B: ") 
for i in range(row2) :  
    for j in range(col2) :  
        print(B[i][j], end = " ") 
  
    print()
multiplyMatrix(A, row1, col1, B, row2, col2) 


Related Solutions

Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing and solving linear algebra problems. The function will have up to three parameters: • A required 2D array representing the coefficient matrix of a linear algebra equation, • A required 1D or 2D array representing the right-side constants of a linear algebra equations, and • An optional parameter used to determine which condition number to use in determining the condition of the system. The...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
In this assignment, you will write a Python script to increase the population of each city...
In this assignment, you will write a Python script to increase the population of each city in the world_x database by 10% (rounded). First, add a new column to the "world_x" city table using the following MySQL command: ALTER TABLE `world_x`.`city` ADD COLUMN `Population` DOUBLE NULL DEFAULT NULL AFTER `Info`; The existing population data are stored as JSON datatype in the city table in a field named Info. You learned about JSON data types in Module 2. To obtain the...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT