[英]How do I multiply a matrix by a vector in python without any built in functions where both the vector and matrix are inputs from the user
My homework question is "Write a function which takes as input a vector and a matrix, and multiplies them. This means that it constructs a new vector, where the element at position i is the dot product of the input vector with column I from the matrix" 我的作业问题是“编写一个函数,它将矢量和矩阵作为输入,并将它们相乘。这意味着它构造了一个新的矢量,其中位置i的元素是输入矢量的点积,其中列I来自矩阵”
I have already tried to write out some code however it only works in some instances, in others it will say my j is out of range for the line 'b=v[j]' 我已经尝试写出一些代码,但它只适用于某些情况,在其他情况下它会说我的j超出了'b = v [j]'行的范围
rows = int(input("Enter how many rows you want in your matrix"))
matrix1 = []
for i in range(0, rows):
x = input("enter some numbers for your matrix").split(' ')
for j in range(len(x)):
x[j]=int(x[j])
matrix1.insert(i, x)
vector1 = []
y = input("enter some numbers for your vector").split(' ')
for i in range(0,len(y)):
a = y[i]=int(y[i])
vector1.insert(i,a)
def dotproduct(v,m):
dot1 = []
dotsum = 0
for i in range(0,len(v)):
for j in range (0, len(m)):
a=m[j][i]
b=v[j]
mult=a*b
if j==0:
dotsum=0
dotsum = dotsum+ mult
dot1.append(dotsum)
return(dot1)
print(dotproduct(vector1, matrix1))
You should try with b=v[i]
but I don't think this will give you the correct dot product computation. 您应该尝试使用b=v[i]
但我不认为这会给您正确的点积计算。
For validation, if I use the dot product from numpy
, I get : 为了验证,如果我使用numpy
的点积,我得到:
>>> import numpy as np
>>> a = [1, 2]
>>> b = [[4, 1], [2, 2]]
>>> np.dot(a, b)
array([8, 5])
A more general advice is to validate input dimensions. 更一般的建议是验证输入维度。
First, you should introduce some validation checks on the input. 首先,您应该对输入进行一些验证检查。
Try to think on how to add these validation checks in your code. 试着考虑如何在代码中添加这些验证检查。 Ideally you want to check that the matrix is a matrix after the user has provided all the rows, and that the dimensions allow a row column product after the user has provided the vector. 理想情况下,您希望在用户提供所有行后检查矩阵是否为矩阵,并且在用户提供向量后,维度允许行列产品。
An hypotethical validation for the matrix could be: 对矩阵的hypotethical验证可以是:
ncols = len(matrix1[0])
for row in matrix1[1:]:
if len(row) != ncols:
#stop the program and raise an error to the user.
I let to you the dimension validation (it is your homework, I don't want to do it all for you). 我告诉你维度验证(这是你的功课,我不想为你做这一切)。
Second, your dotproduct()
is not doing properly the row column product. 其次,你的dotproduct()
没有正确执行行列产品。 I'll give you a tip: 我会给你一个提示:
for i in range(0,len(v)):
i
will be index of vector elements. for i in range(0,len(v)):
i
将是向量元素的索引。
for j in range (0, len(m)):
j
will be index of matrix rows. for j in range (0, len(m)):
j
将是矩阵行的索引。
In a vector x matrix product, they are the same index: you are going to multiply the i-th
element of the row with the i-th
element of the vector. 在向量x矩阵乘积中,它们是相同的索引:您将要将行的第i-th
元素与向量i-th
i-th
元素相乘。 One of them should be instead the index of matrix columns. 其中一个应该是矩阵列的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.