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

Converting matrix multiplication and sum function from Matlab to R

I'm converting a rather complicated set of code from Matlab to R. I have zero experience in Matlab and am a functioning novice in R. I have a segment of code which reads (in…
1
vote
2 answers

Multiplying matrices Java

I've been have some problems multiplying the matrices for this code, when I do it by hand and with a calculation tool I get something completely different than what my code is giving me. Code: public class mult1 { /** * @param args the command…
1
vote
1 answer

Matrix multiplication to mirror translation and rotation of only 1 axis?

I'm using OpenGL with some other library. This library will provide Projection Matrix and I cannot modify it. I have to provide only ModelViewMatrix. However, strange things happen. Only y-axis translation and rotation are inverted. For example if…
5argon
  • 3,683
  • 3
  • 31
  • 57
1
vote
1 answer

MatrixMatrixMultiply error invalid arguments

I am working on a procedure on Maple and it is generating a error. While i do the same steps outside the procedure and i don't get any error! (Error, (in LinearAlgebra:-Multiply) invalid arguments. This is my Maple code: Transform := proc (A,…
1
vote
1 answer

C++ Matrix product: increased speed with little changes

I'm writing a code to multiply a sparse matrix with a full matrix. I've created 2 class: SparseMatrix and Matrix, which store datas as a vector of shared pointers to vectors. In the SparseMatrix case i save item as an object, called SparseMatrixItem…
stefano1
  • 161
  • 10
1
vote
0 answers

intel mkl sparse blas (Matrix Multiplication)

I want using sparse matrix multiplication. I have a diagonal matrix "A" and I considered it as a sparse matrix and a dense matrix B. C = A * B ; now I'm using a intel mkl sparse BLAS. I read user guide mkl and I understand for multiplication a…
1
vote
1 answer

How to multiply matching columns between lists?

I have 2 lists. One is the stock weights and other the returns. Here is an example: a <- matrix(c(0.15, 0.20, 0.10, 0.30, 0.25), 1,5) colnames(a) <- c("AMBV4", "ARCZ6", "BBAS3", "BBDC4", "BRAP4") b <- matrix(c(0.20, 0.30, 0.40, 0.10),…
Jorge Dias
  • 49
  • 7
1
vote
0 answers

OpenCl program crashing video card driver

I have written a program for computing the matrix product on the GPU. My problem is that for large matrices the Catalyst-drivers crash. I am aware of the potential problem caused by timeout detection for long computations, but my computations are…
Bazooka
  • 51
  • 1
  • 5
1
vote
1 answer

Multiplication of Matrices composed of polynomials

Would it be possible to use numpy/scipy to multiply matrices composed of polynomials? Specifically I wish to multiply a 120 by 120 sparse matrix who's entries can look like a+7*b+c by itself. Honestly, I haven't tried very hard to do this. I see…
adam levin
  • 21
  • 3
1
vote
1 answer

How do you get the square root in Ada?

So I have been given an assignment to read in a file put the numbers into two matrices, multiply the matrices, and finally put the output into a .txt file. I have never used Ada before and I figured it would be a good challenge. I am stuck in trying…
Silence
  • 13
  • 1
  • 5
1
vote
1 answer

Multiplication of corresponding 2d slices of two arrays and inversion of array slices

I have two arrays A and B of the same dimension 1000 x 3 x 20 x 20. I want to generate a third array C of dimension 3 x 3 x 20 x 20 that would be an outcome of matrix multiplication of corresponding slices of A and B, i.e. C(:,:,i,j) =…
Laimond
  • 199
  • 1
  • 7
1
vote
5 answers

Matrix multiplication in java

I wanted to do matrix multiplication in Java, and the speed needs to be very good. I was thinking of calling R through java to achieve this. I had a couple of Qs though: Is calling R using Java a good idea? If yes, are there any code samples that…
Chapax
  • 21
  • 1
  • 2
1
vote
1 answer

famo.us - multiple rotation - switching axis

I started playing with famo.us and created cube which rotates left/right or up/down when pressing buttons. rotation left: y+=-Math.PI/2; trans=Transform.rotate(x,y,z); rotation top: x+=-Math.PI/2; trans=Transform.rotate(x,y,z); this works in one…
KChris
  • 31
  • 1
  • 4
1
vote
1 answer

Converting matrix operations in Matlab to R code

I'm trying to convert Matlab code to R. I'm not familiar with Matlab matrix operations, and it appears the results from my R code do not match the results from Matlab, so any help would be greatly appreciated. The Matlab code I'm trying to convert…
itpetersen
  • 1,475
  • 3
  • 13
  • 32
1
vote
2 answers

Vectorizing addition part of matrix multiplication using intrinsics?

I'm trying to vectorize matrix multiplication using blocking and vector intrinsics. It seems to me that the addition part in the vector multiplication cannot be vectorized. Could you please see if I can improve my code to vectorize further? …
the_naive
  • 2,936
  • 6
  • 39
  • 68