Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Multiply a vector by a matrix without NumPy

I'm fairly new to Python and trying to create a function to multiply a vector by a matrix (of any column size). e.g.:

multiply([1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]])

[1, 1]

Here is my code:

def multiply(v, G):
    result = []
    total = 0
    for i in range(len(G)):
        r = G[i]
        for j in range(len(v)):
            total += r[j] * v[j]
        result.append(total)
    return result  

The problem is that when I try to select the first row of each column in the matrix (r[j]) the error 'list index out of range' is shown. Is there any other way of completing the multiplication without using NumPy?

like image 510
JGraham353 Avatar asked Jan 31 '15 15:01

JGraham353


People also ask

How do you multiply a 3X3 matrix in Python?

Multiplication can be done using nested loops. Following program has two matrices x and y each with 3 rows and 3 columns. The resultant z matrix will also have 3X3 structure. Element of each row of first matrix is multiplied by corresponding element in column of second matrix.

How do you multiply an element by a matrix in Python?

Use NumPy.multiply() function on 2-D arrays. This multiplies every element of the first matrix by the equivalent element in the second matrix using element-wise multiplication, or Hadamard Product.

Is dot the same as Matmul?

matmul differs from dot in two important ways. Multiplication by scalars is not allowed. Stacks of matrices are broadcast together as if the matrices were elements.


2 Answers

# check matrices
A = [[1,2],[3,4]]
B = [[1,4],[5,6],[7,8],[9,6]]

def custom_mm(A,B):    
    if len(A[0]) == len(B): -- condition to check if matrix multiplication is valid or not. Making sure matrix is nXm and mXy
        result = [] -- final matrix
        for i in range(0,len(A)): -- loop through each row of first matrix
            temp = [] -- temporary list to hold output of each row of the output matrix where number of elements will be column of second matrix
            for j in range(0,len(B[0])): -- loop through each column of second matrix
                total = 0 
                l = 0 -- dummy index to switch row of second matrix 
                for k in range(0,len(A[0])):
                    total += A[i][k]*B[l][j]
                    l = l+1
                temp.append(total)
            result.append(temp)                
        return result
    else:
        return (print("not possible"))

print(custom_mm(A,B))


    
like image 163
Dhritiman Das Avatar answered Sep 16 '22 14:09

Dhritiman Das


i have attached a code for matrix multiplication do follow the example format for one dimensional multiplication (lists of list)

def MM(a,b):
c = []
for i in range(0,len(a)):
    temp=[]
    for j in range(0,len(b[0])):
        s = 0
        for k in range(0,len(a[0])):
            s += a[i][k]*b[k][j]
        temp.append(s)
    c.append(temp)

return c
a=[[1,2]]
b=[[1],[2]]
print(MM(a,b))

result is [[5]]

like image 40
Pradeep Padmanaban C Avatar answered Sep 19 '22 14:09

Pradeep Padmanaban C