Question

In: Computer Science

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 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 isentered.

1.Use numpy and associated functionality

2.Create and use functions as often as possible

3.Use comments to document your code4.Both integers and float values are acceptable

Solutions

Expert Solution

import numpy as np
global a   #declare globally for use in whole program and its function
global b
a = np.zeros([3, 3])  #creation of 3x3  array for matrices
b = np.zeros([3, 3])

def create():   #create two numpy array

    def enter():      # if user enter value rather than integer and float then this function call to enter appropriate value for first array
        try:
            a[i][j] = input()
        except ValueError:
            print("Enter integer or float only")
            enter()    # recursion will occur until user enter integer or floating point value

    print("enter elements of first array")
    for i in range(3): #entering element by index in first matrix
        for j in range(3):
            try:
                a[i][j] = input()
            except ValueError:
                print("Enter integer or float only")
                enter()
    print("Your first matrix\n",a)


    def enter(): # if user enter value rather than integer and float then this function call to enter appropriate value for second  array
        try:
            b[i][j] = input()
        except ValueError:
            print("Enter integer or float only")
            enter()  # recursion will occur until user enter integer or floating point value

    print("enter elements of Second array")
    for i in range(3):   #entering element by index in second matrix
        for j in range(3):
            try:
                b[i][j] = input()
            except ValueError:
                print("Enter integer or float only")
                enter()
    print("your second matrix\n",b)
    appropriate()


global choice   #globally declared to use in whole program
def selection():
    if (choice == 1):
        c = a + b  #matrix addition
        print("resultant matix\n")
        print(c)
        print("transpose of resultant matrix")
        print(c.T)
        print("mean of coloumn")
        print(np.mean(c, axis=0))
        print("mean of rows")
        print(np.mean(c, axis=1))
        appropriate() # show available option
    elif(choice==2):
        c = a - b   #matrix subtraction
        print("resultant matix\n")
        print(c)
        print("transpose of resultant matrix")
        print(c.T)
        print("mean of coloumn")
        print(np.mean(c, axis=0))
        print("mean of rows")
        print(np.mean(c, axis=1))
        appropriate() # show available option
    elif(choice==3):
        c=np.matmul(a, b)  #matrix multiplication
        print("resultant matix\n")
        print(c)
        print("transpose of resultant matrix")
        print(c.T)
        print("mean of coloumn")
        print(np.mean(c, axis=0))
        print("mean of rows")
        print(np.mean(c, axis=1))
        appropriate() # show available option
    elif(choice==4):
        c = a*b #element by element multiplication
        print("resultant matix\n")
        print(c)
        print("transpose of resultant matrix")
        print(c.T)
        print("mean of coloumn")
        print(np.mean(c, axis=0))
        print("mean of rows")
        print(np.mean(c, axis=1))
        appropriate()  # show available option
    elif(choice==5):
        create() #re create two matrices
    elif(choice==6):
        pass  #no function call it is end of program



def appropriate():    #user must enter integer value for given choice
    try:
        print("\n enter choice:\n1.Matrix Addition\n2.Matrix Subtraction\n3.Matrix Multiplication \n4.multiplication element by element\n5.Re-enter all two matrices\n6.exit")
        global  choice
        choice=int(input())
        selection()
    except ValueError:
        print("enter appropriate choice")
        appropriate()    #function recursion until user enters appropriate value


create()    #program  exceution starting by creation of matrices by calling the function


Related Solutions

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...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a Python program that allows the user to enter the total rainfall for each of...
Write a Python program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September 3.7 inches October 5.1 inches November 7.2...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
Write a program that allows the user to enter two integers and a character If the...
Write a program that allows the user to enter two integers and a character If the character is A, add the two integers If it is S, subtract the second integer from the first else multiply the integers Display the results of the arithmetic
Using PHP, Make a form that allows the user to enter the weight of the item...
Using PHP, Make a form that allows the user to enter the weight of the item being shipped. This will be used to calculate the shipping cost.Create a form that uses the method POST The form should capture a customer's package weight with the one field for the package weight in pounds. All fields should have the REQUIRED attribute. The form should have a submit button and a reset button. The form should look nice. All the labels should line...
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT