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
2 answers

How do I multiply a numpy array by a numpy matrix?

I have a matrix T: [ 0.2 0.4 0.4] [ 0.8 0.2 0. ] [ 0.8 0. 0.2] T = numpy.mat("0.2 0.4 0.4;0.8 0.2 0.0;0.8 0.0 0.2") I have vector v, numpy.array(73543, -36772, 36772) v = numpy.array([ 73543, -36772, 36772]) How do I multiply the array v…
Chris Rigano
  • 687
  • 1
  • 11
  • 23
1
vote
0 answers

How to rotate 3D Points using Rotation Matrix

Below I have two different sets of code trying to do the same thing. I want to be able to take many poles[n, 3] and create a rotation matrix for each omega [n x 1]. I am having a difficult time trying to figure out how I am supposed to go about…
1
vote
1 answer

Funm @Sin VS Sin of Matrix Matlab

If I have a Matrix say A = [0.64 0.42; 0.42 0.97] in Matlab, why does sin(A) give me a different result than funm(A, @sin)? I would expect them to be the same?
Maggick
  • 763
  • 2
  • 10
  • 26
1
vote
2 answers

reading/writing a matrix with a stride much larger than its width causes a big loss in performance

I'm doing dense matrix multiplication on 1024x1024 matrices. I do this using loop blocking/tiling using 64x64 tiles. I have created a highly optimized 64x64 matrix multiplication function (see the end of my question for the code). gemm64(float *a,…
Z boson
  • 32,619
  • 11
  • 123
  • 226
1
vote
2 answers

Matrix multiply two lists with a formula

If I have two lists in Excel: X : x1 x2 x3 Y : y1 y2 y3 How can I make a 2-dimensional matrix like this?: x1*y1 x1*y2 x1*y3 x2*y1 x2*y2 x2*y3 x3*y1 x3*y2 x3*y3
1
vote
1 answer

Cuda matrix multiplication results differs from MATLAB

Its been two days and I am still cant figure it out why my implementation of CUDA matrix multiplication differs from the results produced in MATLAB. CUDA kernel: A(200x60000) = W(200x784) * Data(784x6000) __global__ void CalculateA(Matrix W, Matrix…
HadiRj
  • 1,015
  • 3
  • 21
  • 41
1
vote
0 answers

scipy sparse matrix multiplication overflow

I encountered the similar problem as in http://scipy-user.10969.n7.nabble.com/Help-Multiplying-two-sparse-matrices-td2472.html. So how can we use int64 to store the nonempty positions of sparse matrix to overcome this kind of overflow? PS, directly…
chentingpc
  • 1,283
  • 3
  • 18
  • 24
1
vote
1 answer

Matrix multiplication with LAPACK, BLAS, dgemm, intiger parameter type

I am testing different options for matrix multiplication with different parameter types for matrices. One of them is dgemm routine within BLAS. When I wanted to make a matrix defined as integer(kind=1) with a size of 1000x1000 (nxp) it crashed with…
1
vote
1 answer

Issues multiplying Mat matrices

I am trying to project an image to eigenface convariance matrix that EigenFacesRecognizer of opencv returns. I use the following code to load eigenfaces parameters loading an image and trying to project the sample image to pca subspace.…
Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
1
vote
1 answer

MATLAB: multiply each column 1x3xN matrix by corresponding 3x3xN rotation matrix

essentially I have a rotation matrix like so: % rot = [ cos(theta) sin(theta) 0; % -sin(theta) cos(theta) 0; % 0 0 1]; except that theta is 1xN, so I create the following monster: rot =…
Brian
  • 3,453
  • 2
  • 27
  • 39
1
vote
1 answer

My matrix multiplication is not working how i expect

This is what I want: What am I doing wrong with the following code. The output of the translation for the orbit rotation never occurs, it just ends up rotating all on the original axis. void Renderer::UpdateAsteroid( Placement^ asteroidRotation,…
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
1
vote
2 answers

R: Can I use some sort of matrix multiplication with exponentials?

I have data matrix (X) of the dimensions 5000x250 plus an extra parameter Y (Dim: 5000x1). The following loop gives me the desired results, but it takes forever to compute. for (i in 1:ncol(X)) for (j in 1:nrow(X)) { X[j,i]=Y[j,1]^X[j,i] …
aciM
  • 89
  • 1
  • 1
  • 8
1
vote
1 answer

OpenGL correct matrix transformation order to rotate object at fixed point

I am using OpenGL with Python, and have the following code: glTranslatef(0, 0, -3.0) glRotatef(rotation * 100.0, 0.0, 1.0, 0.0) square.render() - where rotation is a integer which increases by frame and square.render() simply renders a quad to the…
Fitzy
  • 1,871
  • 6
  • 23
  • 40
1
vote
1 answer

How much parallel speed up can I expect in matrix-vector multiplication?

I have written an MPI routine to parallelize matrix-vector multiplication. The speed up has been disappointing to non-existent. I have found a lot of routines on the net, and I am handling this about the same way that most of them do. What I…
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
1
vote
1 answer

math.net DenseVectors vs DenseMatrix 1xn | nx1

This is really simple stuff but, as I am a noob with math.net, I may need to be pointed in the right direction: let a = new DenseVector([| 5.0; 2.0; 3.0; |]) let m = new DenseMatrix(3, 3, 1.0) let r = a * m let r2 = m * a results in: > r;; val it…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
1 2 3
99
100