user1305541
user1305541

Reputation: 205

vector, matrix multiplication and sum

I'm going around in circles with all the options in numpy/scipy. Dot product, multiply, matmul, tensordot, einsum etc.

I want to multiply a 1d vector with a 2d matrix (which will be sparse csr) and sum the result so I have a 1d vector

eg

oneDarray = np.array([1, 2, 3])
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])

# multiple and sum the oneDarray against the rows of the matrix
#          eg 1*1 + 1*2 + 1*3 = 6, 2*4 + 2*5 + 2*6 = 30, 3*7 + 3*8 + 3*9 = 42
so output we be [6,30,53]


# multiple and sum the oneDarray against the columns of the matrix
#          eg 1*1 + 1*4 + 1*7 = 28, 2*2 + 2*5 + 2*8 = 30, 3*3 + 3*6 + 3*9 = 486
so output we be [28,30,486]

Any help would be greatly appreciated.

Upvotes: 1

Views: 972

Answers (2)

Gambit1614
Gambit1614

Reputation: 8811

Well you have some calculation error in your question.The first one will result in [6, 30, 72] and the second part will result in [12, 30, 54]. If I understand correctly, the first one can be solved with

np.sum(oneDarray * matrix.T, axis=0)

and the second part with

np.sum(np.multiply(matrix, oneDarray), axis=0)

Upvotes: 0

hpaulj
hpaulj

Reputation: 231595

# 1*1 + 1*2 + 1*3 = 6, 2*4 + 2*5 + 2*6 = 30, 3*7 + 3*8 + 3*9 = 42

1*1 + 1*2 + 1*3 = 6
2*4 + 2*5 + 2*6 = 30, 
3*7 + 3*8 + 3*9 = 42

1*(1 + 2 + 3) = 6, 
2*(4 + 5 + 6) = 30, 
3*(7 + 8 + 9) = 42

So this can calculated in several ways:

In [92]: oneDarray*(matrix.sum(axis=1))                                                                      
Out[92]: array([ 6, 30, 72])

In [93]: np.einsum('i,ij->i', oneDarray, matrix)                                                             
Out[93]: array([ 6, 30, 72])

In [94]: (oneDarray[:,None]*matrix).sum(axis=1)                                                              
Out[94]: array([ 6, 30, 72])

It doesn't fit the usual dot (matrix) product, which would have an einsum expression like ij,j->i (wrong index is summed).

The other expression is (your values are wrong, except the middle one):

In [95]: matrix.sum(axis=0)*oneDarray                                                                        
Out[95]: array([12, 30, 54])

If the matrix is sparse csr:

In [96]: M = sparse.csr_matrix(matrix)                                                                       
In [97]: M                                                                                                   
Out[97]: 
<3x3 sparse matrix of type '<class 'numpy.int64'>'
    with 9 stored elements in Compressed Sparse Row format>
In [98]: M.sum(axis=1)                                                                                       
Out[98]: 
matrix([[ 6],
        [15],
        [24]])
In [99]: M.sum(axis=1).A1*oneDarray                                                                          
Out[99]: array([ 6, 30, 72])

The sum is a (3,1) np.matrix. A1 flattens it into a 1d ndarray, making the element wise multiplication easier.

In [103]: M.sum(axis=0)                                                                                      
Out[103]: matrix([[12, 15, 18]], dtype=int64)
In [104]: M.sum(axis=0).A1*oneDarray                                                                         
Out[104]: array([12, 30, 54], dtype=int64)
In [116]: np.multiply(M.sum(0), oneDarray)                                                                   
Out[116]: matrix([[12, 30, 54]], dtype=int64)

Upvotes: 2

Related Questions