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
1
vote
1 answer

SQL Matrix Multiplication - Could you explain what's happening here?

SELECT A.row_number, B.column_number, SUM(A.value*B.value) FROM A, B WHERE A.column_number = B.row_number GROUP BY A.row_number, B.column_number This snippet outputs the multiplication of two matrices where each matrix is a…
1
vote
0 answers

What is the best way to multiply a large dense matrix with its transpose?

I have a large matrix of the order 1M x 300 (obtained after SVD decomposition of a large item matrix). So, the matrix is a dense one with float as data type. I would like to compute the similarity matrix by multiplying the dimensionally reduced…
sravan_kumar
  • 1,129
  • 1
  • 13
  • 25
1
vote
1 answer

OpenCL Matrix multiplication: inner product versus outer product

I'm hoping everyone is familiar with the standard "naive" method of multiplying two (n x n square for simplicity) matrices. In C this is: for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) for(int k = 0; k < n; ++k) C[i*n…
matmul
  • 589
  • 5
  • 16
1
vote
2 answers

OpenMP for matrix multiplication

I am new to OpenMP and am trying desperately to learn. I have tried to write an example code in C++ in visual studio 2012 to implement matrix multiplication. I was hoping someone with OpenMP experience could take a look at this code and help me to…
user2863626
  • 187
  • 3
  • 12
1
vote
1 answer

bsxfun implementation in solving a min. optimization task

I really need help with this one. I have to matrices L1 and L2, both are (500x3) of size. First of all, I compute the difference of every element of each column of L1 from L2 as follows: lib1 = bsxfun(@minus, L1(:,1)',L2(:,1)); lib1=lib1(:); lib2…
Sergio Haram
  • 437
  • 4
  • 17
1
vote
1 answer

Computing the trace of a hat matrix from and independent variable matrix with a large number of rows; how can I avoid memory errors?

I have a (edited, silly typo) independent variable matrix, X. I would like to either take the trace of the hat matrix computed from X, or find some computational shortcut for getting that trace without actually computing the hat matrix. The issue…
MNagy
  • 423
  • 7
  • 20
1
vote
3 answers

Vector times Matrix - Two cases

I have a vector row b = [b1 b2 b3 b4] and a Matrix M = [m1 m2 m3 m4] where the m1,m2,m3 and m4 are column vectors of size N. I want to do perform a multiplication so that I can have the following result in Matlab: Result = [b1*m1 b2*m2 b3*m3…
1
vote
3 answers

Comparision of Speed of matrix multiplication in matlab and C

We can do matrix multiplication in several ways. A and B are two matrix, of size 1000 * 1000. Use the matlab builtin matrix operator. A*B Write two nests explicitly. Write three nests explicitly. Dynamically link C code to do the three nests.…
Yan Song
  • 112
  • 2
  • 13
1
vote
1 answer

sse precision error with Matrix multiplication

My program does NxN matrices multiplication where elements of both the matrices are initialized to values (0, 1, 2, ... N) using a for loop. Both the matrix elements are of type float. There is no memory allocation problem. Matrix sizes are input as…
JagPK
  • 148
  • 1
  • 9
1
vote
1 answer

Processing: getting Yaw, Pitch and Roll angle from matrix4x4

So I am pretty new with Processing and I am trying to integrate the Oculus Rift in my current project. Since I am reading the Oculus sensor data with an application found on internet, I am only able to get the transformation matrix instead of the…
1
vote
2 answers

Matlab vectorizing equations and matrix multiplication

I have a program that currently uses a for loop to iterate through a set of functions. I've tried using parfor but that only works on the university's version of Matlab. I'd like to vectorize the handling of this so that a for loop isn't necessary.…
1
vote
1 answer

Intel MKL --- *** glibc detected *** free(): invalid next size (fast): 0x00000000006302c0 ***

I am new to Intel MKL the code and error details follows next. I am trying to multiply two sparse matrices using intel mkl mkl_scsrmultd() routine. I am using two functions gen_col() to generate column vector and gen_row() to generate row pointer as…
leonahi
  • 11
  • 2
1
vote
1 answer

confuse with row major and column major matrix multiplication in hlsl

i realize that in d3dx math library or xnamath library, there matrix is saved row major in memory. in effect framework, when we set a matrix parameter by effect interface setvarible, it will transpose the target matrix. if i am not use effect…
boo
  • 493
  • 6
  • 17
1
vote
1 answer

Array multiplication in Excel

In my excel document I have two sheets. The first is a data set and the second is a matrix of the relationship between two of the variables in my data set. Each possibility of the variable is a column in my matrix. I'm trying to get the sum of the…
Brent Ferrier
  • 312
  • 1
  • 4
  • 18
1
vote
1 answer

The result of projMat * viewMat * modelMat * vertPos should result in a screen-space position... right?

And given that, shouldn't all position values that end up being rendered be between the values -1 and 1? I tried passing said position value as the "color" value from my vertex shader to my fragment shader to see what would happen, and expected a…
Phildo
  • 986
  • 2
  • 20
  • 36
1 2 3
99
100