简体   繁体   English

如何在python中将矩阵乘以向量而不使用任何内置函数,其中向量和矩阵都是来自用户的输入

[英]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. 更一般的建议是验证输入维度。

Input validation 输入验证

First, you should introduce some validation checks on the input. 首先,您应该对输入进行一些验证检查。

  • Check that the matrix is a matrix, I mean, all rows have the same number of elements. 检查矩阵是否为矩阵,我的意思是,所有行都具有相同数量的元素。 AN x M matrix has N row, each one with M elements. AN x M矩阵具有N行,每行具有M个元素。
  • Matrix multiplication (or row column product) is possible if some constraints on matrix dimensions are satisfied. 如果满足矩阵维度的某些约束,则可以使用矩阵乘法 (或行列乘积)。 If matrix is N x M, the vector must have length M x 1 (multiplying matrix x vector) or 1 x N (multiplying vector x matrix). 如果矩阵是N×M,则向量必须具有长度M×1(乘以矩阵x向量)或1×N(乘以向量x矩阵)。 Consider the vector as a matrix with one row or one column. 将向量视为具有一行或一列的矩阵。
    To keep things simple, you may consider only one of the two cases, say vector x matrix only, as it seems your professor asked. 为了简单起见,你可能只考虑两种情况中的一种,比如矢量x矩阵,就像你的教授所说的那样。

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). 我告诉你维度验证(这是你的功课,我不想为你做这一切)。

row column product 行列产品

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.

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