Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matrix by vector
#1
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)
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Reply
#4
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)
Reply
#5
Paul is right, you need to unindent the append.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
with your suggestion I get another outcome:
[6, 21, 45]
Reply
#7
Well, you moved s = 0. It should be back where you had it before, between the for i loop and the for j loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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 ?
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 958 Dec-27-2022, 08:22 PM
Last Post: Cooketaker
  Vector field cross product Eduard 2 2,525 Aug-20-2018, 02:54 PM
Last Post: Eduard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020