Question

In: Computer Science

This exercise allows a user to enter the values of two, 3x3 matrices and then select...

This exercise allows a user to enter the values of two, 3x3 matrices and then select from options including, addition, subtraction, matrix multiplication, and element by element multiplication. You should use numpy.matmul() for matrix multiplication (e.g. np.matmul(a, b) ). The program should computer the appropriate results and return the results, the transpose of the results, the mean of the rows for the results, and the mean of the columns for the results. When entering data you should check that each value is numeric for the matrices. The user interface should continue to run until the user indicates they are ready to exit.

If an inappropriate entry is detected, the program should prompt for a correct value and continue to do so until a correct value is entered. Hints: 1. Use numpy and associated functionality 2. Create and use functions as often as possible 3. Use comments to document your code 4. Both integers and float values are acceptable

Solutions

Expert Solution

import numpy as np


def checkEntry(inputValue):
    try:
        float(inputValue)
    except ValueError:
        return False
    return True


def getMatrix():
    mat = np.zeros((3, 3))
    for row in range(3):
        for column in range(3):
            check = False
            while not check:
                num = input('Enter element at position ({:d},{:d}): '.format(row + 1, column + 1))
                check = checkEntry(num)
                if not check:
                    print('Please enter a numeric value!')
            mat[row, column] = float(num)
        print()
    return mat


print('Enter first matrix: ')
mat1 = getMatrix()

print('Enter second matrix: ')
mat2 = getMatrix()

print('Enter option:\n1.Addition\n2.Subtraction\n3.Matrix multiplication\n4.Element by element multiplication\n5.Exit')
choice = int(input())

while choice != 5:
    if choice == 1:
        res = mat1 + mat2
    elif choice == 2:
        res = mat1 - mat2
    elif choice == 3:
        res = np.matmul(mat1, mat2)
    elif choice == 4:
        res = np.multiply(mat1, mat2)
    print('Result:')
    print(res)
    print('Transpose of result:')
    print(res.T)
    print('Mean of rows:')
    print(np.mean(res, 1))
    print('Mean of columns:')
    print(np.mean(res, 0))

    print('\nEnter option:\n1.Addition\n2.Subtraction\n3.Matrix multiplication\n4.Element by element multiplication\n5.Exit')
    choice = int(input())


Related Solutions

Using python, allows a user to enter the values of two, 3x3 matrices and then select...
Using python, allows a user to enter the values of two, 3x3 matrices and then select from options including, addition, subtraction, matrix multiplication, and element by element multiplication. You should use numpy.matmul()for matrix multiplication(e.g. np.matmul(a, b)).The program should computer the appropriate results and return the results, the transpose of the results, the mean of the rows for the results, and the mean of the columns for the results.When entering data you should check that each value is numeric for the...
Provide two text boxes for the user to enter in 2 values. Once the user clicks...
Provide two text boxes for the user to enter in 2 values. Once the user clicks off the second box, the sum of the two numbers displayed as a header level 2. here is what i have , and i cant find a way to display the sum of the numbers in each box in the header along with the other text. <h1>Problem 6</h1> <input type="text" id="txt1" onkeyup="sum();" /> <input type="text" id="txt2" onkeyup="sum();" /> <h2 id="txt3" result=""> The sum of...
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
Make a function in C that allows for the user to enter in 2 separate strings....
Make a function in C that allows for the user to enter in 2 separate strings. The first string that is entered is then made equal to the second string, but any duplicate characters (the same character appearing twice or more in a row) should only be copied once to the output string. Lastly the function displays how many duplicate characters were found.
Design the logic for a program that allows a user to enter 20 numbers, then displays...
Design the logic for a program that allows a user to enter 20 numbers, then displays them in the reverse order of entry. Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the numeric average of the numbers entered. The program is C++. I need a Pseudocode
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT