Questions tagged [matrix-multiplication]

Questions related to matrix multiplication, especially implementation. Mathematical questions should consider the [linear-algebra] tag.

Matrix multiplication is usually the process of multiplying two (or more) matrices. This arises quite frequently in linear algebra contexts and is a particularly fundamental task in computing, especially in scientific computing.

To that end, a number of fundamental libraries, such as LAPACK, BLAS, ATLAS, and others have been developed. Because the growth of matrices affects the computational time, extensive effort has been made to optimize these packages for various computer architectures and various matrix sizes.

In scientific software for statistical computing and graphics, operator %*% performs matrix multiplication (see ?"%*%"), and interfaces BLAS routine dgemmm.


The product of the multiplication of two matrices a and b is the matrix c, where each element is the sum of the products of the i-th row of a and the j-th column of b.

c[i][j] += a[i][k] * b[k][j];

Example:

     (b) { 1,  2,  3,  4}
         { 5,  6,  7,  8}
(a)    ┌(c)──────────────
{1, 2} │ {11, 14, 17, 20}
{3, 4} │ {23, 30, 37, 44}
{5, 6} │ {35, 46, 57, 68}

Algorithm of the matrix multiplication:

// rows of 'a' matrix
int m = 3;
// columns of 'a' matrix
// and rows of 'b' matrix
int n = 2;
// columns of 'b' matrix
int p = 4;
// matrices 'a=m×n', 'b=n×p'
int[][] a = {{1, 2}, {3, 4}, {5, 6}},
        b = {{1, 2, 3, 4}, {5, 6, 7, 8}};
// resulting matrix 'c=m×p'
int[][] c = new int[m][p];
// iterate over the rows of the 'a' matrix
for (int i = 0; i < m; i++) {
    // iterate over the columns of the 'b' matrix
    for (int j = 0; j < p; j++) {
        // iterate over the columns of the 'a'
        // matrix, aka rows of the 'b' matrix
        for (int k = 0; k < n; k++) {
            // sum of the products of
            // the i-th row of 'a' and
            // the j-th column of 'b'
            c[i][j] += a[i][k] * b[k][j];
        }
    }
}
2901 questions
9
votes
3 answers

Matrix power sum

What is the best way to calculate sum of matrices such as A^i + A^(i+1) + A^i+2........A^n for very large n? I have thought of two possible ways: 1) Use logarithmic matrix exponentiation(LME) for A^i, then calculate the subsequent matrices by…
ishan3243
  • 1,870
  • 4
  • 30
  • 49
9
votes
1 answer

Matrix Multiplication of a Pandas DataFrame and Series

I want to do a matrix multiplcation of a pandas dataframe and a series df = pandas.DataFrame({'a':[4,1,3], 'b':[5,2,4]},index=[1,2,3]) ser = pandas.Series([0.6,0.4]) df is, a b 1 4 5 2 1 2 3 3 4 ser is, 0 0.6 1 0.4 My desired result…
nitin
  • 7,234
  • 11
  • 39
  • 53
9
votes
4 answers

Fast multiplication of k x k boolean matrices, where 8 <= k <= 16

I want to find an as fast as possible way of multiplying two small boolean matrices, where small means, 8x8, 9x9 ... 16x16. This routine will be used a lot, so it needs to be very efficient, so please don't suggest that the straightforward solution…
hakoja
  • 896
  • 7
  • 16
9
votes
1 answer

Large matrix multiplication on gpu

I need to implement a matrix multiplication on GPU with CUDA for large matrices. Size of each matrix alone is bigger than the GPU memory. So I think I need an algorithm to do that efficiently. I went around the internet but couldn't find any. Can…
Soroosh Khoram
  • 417
  • 1
  • 7
  • 11
9
votes
2 answers

Cache friendly method to multiply two matrices

I intend to multiply 2 matrices using the cache-friendly method ( that would lead to less number of misses) I found out that this can be done with a cache friendly transpose function. But I am not able to find this algorithm. Can I know how to…
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
9
votes
0 answers

Efficient way of computing matrix product AXA'?

I'm currently using BLAS function DSYMM to compute Y = AX and then DGEMM for YA', but I'm wondering is there some more efficient way of computing the matrix product AXAT, where A is an arbitrary n×n matrix and X is a symmetric n×n matrix?
Jouni Helske
  • 6,427
  • 29
  • 52
9
votes
2 answers

Matrix multiplication using 1d arrays

I'm trying to multiply two matrices stored inside 1d arrays. I'm using this function, but my program crashes, I assume due to an out of bounds error. However, I have no (easy) ability to debug, so I have to decide if my code is correct, and to me it…
Tom Teman
  • 1,975
  • 3
  • 28
  • 43
8
votes
4 answers

Generalized Matrix Product

I'm fairly new to MATLAB. Normal matrix multiplication of a M x K matrix by an K x N matrix -- C = A * B -- has c_ij = sum(a_ik * b_kj, k = 1:K). What if I want this to be instead c_ij = sum(op(a_ik, b_kj), k = 1:K) for some simple binary operation…
Nick
  • 2,821
  • 5
  • 30
  • 35
8
votes
5 answers

MATLAB: How to vector-multiply two arrays of matrices?

I have two 3-dimensional arrays, the first two dimensions of which represent matrices and the last one counts through a parameterspace, as a simple example take A = repmat([1,2; 3,4], [1 1 4]); (but assume A(:,:,j) is different for each j). How can…
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
8
votes
5 answers

How to optimize matrix multiplication operation

I need to perform a lot of matrix operations in my application. The most time consuming is matrix multiplication. I implemented it this way template Matrix Matrix::operator * (Matrix& matrix) { Matrix multipliedMatrix =…
george
  • 133
  • 1
  • 2
  • 4
8
votes
2 answers

Sparse matrix-vector multiplication in CUDA

I'm trying to implement a matrix-vector Multiplication on GPU (using CUDA). In my C++ code (CPU), I load the matrix as a dense matrix, and then I perform the matrix-vector multiplication using CUDA. I'm also using shared memory to improve the…
all_by_grace
  • 2,315
  • 6
  • 37
  • 52
8
votes
4 answers

Is there a numerically optimal order of matrix multiplication?

TL;DR: The question is about multiplication ACCURACY I have to multiply matrices A (100x8000), B (8000x27) and C (27x1). Since matrices B and C are constant and A is variable, I prefer to calculate it as: ABC = np.dot(A, np.dot(B, C)). However I…
8
votes
5 answers

Partitioned matrix multiplication in tensorflow or pytorch

Assume I have matrices P with the size [4, 4] which partitioned (block) into 4 smaller matrices [2,2]. How can I efficiently multiply this block-matrix into another matrix (not partitioned matrix but smaller)? Let's Assume our original matric is: P…
Amir
  • 16,067
  • 10
  • 80
  • 119
8
votes
1 answer

Batch-Matrix multiplication in Pytorch - Confused with the handling of the output's dimension

I got two arrays : A B Array A contains a batch of RGB images, with shape: [batch, Width, Height, 3] whereas Array B contains coefficients needed for a "transformation-like" operation on images, with shape: [batch, 4, 4, 3] To put it simply, the…
8
votes
3 answers

Can UIPinchGestureRecognizer and UIPanGestureRecognizer Be Merged?

I am struggling a bit trying to figure out if it is possible to create a single combined gesture recognizer that combines UIPinchGestureRecognizer with UIPanGestureRecognizer. I am using pan for view translation and pinch for view scaling. I am…
dugla
  • 12,774
  • 26
  • 88
  • 136