Python Forum
matrix by vector - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: matrix by vector (/thread-17930.html)



matrix by vector - mcgrim - Apr-29-2019

This code is supposed to multiply a matrix by a vector without the aid of a .dot function.
In order to make sure I get the right result, I use the @ command right after printing the function.
This code prints
[7, 15, 24]
when using the function
and
[ 6 15 24]
when using the dot product command.
Can anyone please tell me where this is wrong?
Why does the function print 7 instead of 6?

def matvec1(a,b):
    if a.shape[0] == b.shape[0]:
        for i in range(len(a)):
            newarray = []
            s = 0
            for j in range(len(b)):
                s += a[i][j] * b[i]
                newarray.append(s)
        return newarray
    else: 
        return "The matrix and vector aren't compatible."
        
        
    
A1=np.array([[1,2,3],[4,5,6],[7,8,9]    ])
B1=np.array([1,1,1])

print(matvec1(A1,B1))
print(A1@B1)



RE: matrix by vector - ichabod801 - Apr-29-2019

You are building newarray wrong. You are resetting it to empty each row, and appending to it after each addition. You should just set it to empty once (before the first for loop), and append to it after calculating each row sum (after the j for loop, it is currently indented under that loop).


RE: matrix by vector - mcgrim - May-01-2019

I have done what you said but now I am getting a even stranger result.
I set the empty list before the first loop and I left append after the j loop.
but now what I am getting is
[1, 3, 6, 10, 15, 21, 28, 36, 45].

How come?

def matvec1(a,b):
    newarray = []
    s = 0
    if a.shape[0] == b.shape[0]:
        for i in range(len(a)):
            for j in range(len(b)):
                s += a[i][j] * b[i]
                newarray.append(s)
        return newarray
    else: 
        return "The matrix and vector aren't compatible."
        
        
    
A1=np.array([[1,2,3],[4,5,6],[7,8,9]    ])
B1=np.array([1,1,1])

print(matvec1(A1,B1))
print(A1@B1)



RE: matrix by vector - paul18fr - May-01-2019

maybe have a look of the indentation of "newarray.append(s)" line (go back once on the left) and the result is identical to numpy.dot

I've a comment/advice: dynamic allocation memory is costly (using append) and you must initiate your matrix/array before any calculation (using numpy.zeros)


RE: matrix by vector - ichabod801 - May-01-2019

Paul is right, you need to unindent the append.


RE: matrix by vector - mcgrim - May-01-2019

with your suggestion I get another outcome:
[6, 21, 45]


RE: matrix by vector - ichabod801 - May-01-2019

Well, you moved s = 0. It should be back where you had it before, between the for i loop and the for j loop.


RE: matrix by vector - mcgrim - May-01-2019

thank you. Now it works, but I am not understanding how the location of s=0 makes a difference.
Would you perhaps be willing to explain, if not too time consuming ?


RE: matrix by vector - ichabod801 - May-02-2019

s is the sum of the products in each row in a. Since it is for each row, you need to reset it at the start of each row. The i loop goes through the rows, so it should be reset at the start of each i loop.