I have sparse COO 3D A matrix NxNxM (The third dimension is M) and dense 2D B matrix (NxN), which will be an optimization variable for my solver. I want to multiply A and B to obtain (NxNXM) matrix. Then I need to sum the elements of the resulting 3D matrix to create 2D matrix (NxN).
For clarification, If I wouldn’t use a sparse matrix, I could do it with the below code. It is a multiplication of 3D and 2D matrices.
np.sum(np.einsum('ijk,jk->ijk', A, B))
where A is 3D matrix and B is 2D matrix.
My objective function is to minimize the sum of the elements of the multiplication of sparse A with decision variable matrix B.
How can I do this?
I am using pydata sparse library to create sparse matrix.
pydata
provide? With thescipy.sparse
package you have to use its own multiplication methods.numpy
functions do it wrong. – hpauljscipy.sparse
but my A matrix is 3D and it is sparse. – Emin