简体   繁体   English

用户定义的Python矩阵计算程序输入

[英]User defined input for matrix calculation program in Python

I have written up this "hard-coded" version of my end goal. 我已经写下了最终目标的“硬编码”版本。 As you can see, I have a specific value for n as well as pre-defined matrices X and Y. The program currently performs the calculations just fine but what I'm having trouble with is modifying it to accept user defined input for n, X, and Y and accurately perform the calculations based on what the user enters. 如您所见,我有一个特定的n值以及预定义的矩阵X和Y。该程序目前可以正常执行计算,但是我遇到的麻烦是修改它以接受用户定义的n输入, X和Y,并根据用户输入的内容准确执行计算。 I'm still getting used to Python and user input so any help with coding this would be greatly appreciated! 我仍然习惯于Python和用户输入,因此在编码方面的任何帮助将不胜感激! I should also note that I'm trying to do this without NumPy for learning purposes. 我还应注意,出于学习目的,我正在尝试不使用NumPy进行此操作。 Thanks! 谢谢!

# Program to add two matrices
# using nested loop

n = 3

X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

Y = [[5,8,1],
    [6,7,3],
    [4,5,9]]

result = [[0,0,0],
         [0,0,0],
         [0,0,0]]

# adds the matrices
# 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]

for r in result:
   print(r)


# subtracts the matrices
for i in range(len(X)):
   for j in range(len(X[0])):
       result[i][j] = X[i][j] - Y[i][j]

for r in result:
   print(r)

# multiplies the matrices
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[i][j] = sum((X[i][v]*Y[v][j] for v in range(n)))

for r in result:
   print(r)

Maybe this will be helpful. 也许这会有所帮助。 First you get n from a user, and than read n number of rows for the X matrix. 首先,您从用户那里得到n,然后读取X矩阵的n行数。 values in each row are separated by comma. 每行中的值用逗号分隔。

n  = int(input("Provide n: "))

X = [];

for rowi in range(n):
    row_list = list(map(float, input("row {}: ".format(rowi+1)).split(',')))
    X.append(row_list)

print(X)

Please note that this was tested in python 3.4 and there is no any error checking here. 请注意,这已在python 3.4中进行了测试,此处没有任何错误检查。 So probably you would need to add some conditions to check if user inputs are numbers, not strings for example, and that each row has same number of entries etc. 因此,可能您需要添加一些条件来检查用户输入是否为数字,而不是字符串,例如,每行是否具有相同数量的条目等。

Since we know that matrx can be represented as nested Lists in Python,We can prompt for user input for each element.Elements in same row are first contained in a list called row[] which is then appended to the container list and hence we get lists inside container list and hence matrix. 因为我们知道matrx可以在Python中表示为嵌套列表,所以我们可以提示用户输入每个元素。同一行中的元素首先包含在名为row []的列表中,然后附加到容器列表中,因此我们得到容器列表内的列表,因此也包含矩阵。

#Prints the sum of matrix of orders mxn
first_row_count = int(input('Enter the number of rows of 1st matrix :'))
first_col_count = int(input('Enter the number of columns of 1st matrix :'))

second_row_count = int(input('Enter the number of rows of 1st matrix :'))
second_col_count = int(input('Enter the number of columns of 1st matrix :'))

first_Matrix =[]
second_matrix=[]

if(first_row_count == second_row_count) and (second_row_count == second_col_count):
    print('############Enter the elements of first Matrix#################')
    for i in range(0,first_row_count):
        row = []
        input_variable = None
        for j in range(0,first_col_count):
            input_variable = int(input('Enter the element at mat[{0}][{1}]'.format(i,j)))
            row.append(input_variable)
       first_Matrix.append(row) 

    print('############Enter the elements of second Matrix#################')    
    for i in range(0,second_row_count):
        row = []
        input_variable = None
        for j in range(0,second_col_count):
            input_variable = int(input('Enter the element at mat[{0}][{1}]'.format(i,j)))
            row.append(input_variable)
        second_matrix.append(row)          

    print('############Resultant Matrix#################')   
    result = [[first_Matrix[i][j] + second_matrix[i][j]  for j in range(len(first_Matrix[0]))] for i in range(len(first_Matrix))]
    for r in result:
           print(r)
else:
    print('------------The matrix of given orders are not compatible-----------------')    
print('########The Matrix Sum performed##################')    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM