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
10
votes
1 answer

Scipy sparse matrices element wise multiplication

I am trying to do an element-wise multiplication for two large sparse matrices. Both are of size around (400K X 500K), with around 100M elements. However, they might not have non-zero elements in the same positions, and they might not have the same…
Avisek
  • 363
  • 1
  • 3
  • 16
10
votes
4 answers

matlab/octave - Generalized matrix multiplication

I would like to do a function to generalize matrix multiplication. Basically, it should be able to do the standard matrix multiplication, but it should allow to change the two binary operators product/sum by any other function. The goal is to be as…
gaborous
  • 15,832
  • 10
  • 83
  • 102
10
votes
5 answers

Binary matrix multiplication bit twiddling hack

Abstract Hi, suppose you have two different independent 64-bit binary matrices A and T (T is another matrix that is stored in transposed form, using the transposed version of matrix allows during multiplication to operate on T's rows rather than…
Lu4
  • 14,873
  • 15
  • 79
  • 132
10
votes
2 answers

Error using simple matrix multiplication

I stumbled upon an error during a simple multiplication that rather surprised me. What is happening here, I always assumed * was only for matrix multiplication. x = 2; y = zeros(1,4); y(1) = 1 *x; y(2) = x* 1; y(3) = (x *1); y(4) = x *1; y x…
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
10
votes
5 answers

Equivalent of cudaGetErrorString for cuBLAS?

CUDA runtime has a convenience function cudaGetErrorString(cudaError_t error) that translates an error enum into a readable string. cudaGetErrorString is used in the CUDA_SAFE_CALL(someCudaFunction()) macro that many people use for CUDA error…
solvingPuzzles
  • 8,541
  • 16
  • 69
  • 112
9
votes
2 answers

How to update opengl modelview matrix with my own 4x4 matrix?

I have 4x4 matrix for object's transformations. float mat44[16]; But i don't know how to update OpenGL ModelView matrix using my matrix. should i use glTranslatef()/glRotatef() with relavant values from my matrix or should i use…
shan
  • 1,164
  • 4
  • 14
  • 30
9
votes
1 answer

How do you combine X, Y, and Z rotations in a model's local space (not global)?

I am starting out on Molehill and am having a lot of problems grasping Matrix3D. My first problem is how to manage a Matrix3D that describes a single model's orientation. I use Matrix3D.appendRotation() to do this. But depending on the order I…
9
votes
1 answer

find the dot product of sub-arrays in numpy

In numpy, the numpy.dot() function can be used to calculate the matrix product of two 2D arrays. I have two 3D arrays X and Y (say), and I'd like to calculate the matrix Z where Z[i] == numpy.dot(X[i], Y[i]) for all i. Is this possible to do…
Ben Kirwin
  • 164
  • 2
  • 8
9
votes
7 answers

Solving floating-point rounding issues C++

I develop a scientific application (simulation of chromosomes moving in a cell nucleus). The chromosomes are divided in small fragments that rotate around a random axis using 4x4 rotation matrices. The problem is that the simulation performs…
9
votes
2 answers

smooth scroll/inertial scrolling/momentum scroll

I have an OpenGL ES View in Android thats controlled by a matrix for translation. Im trying to figure out a way to get a hint of momentum scrolling as seen in the google maps app or the iPhone. Thanks.
jfisk
  • 6,125
  • 20
  • 77
  • 113
9
votes
2 answers

ValueError: matrices are not aligned

I have the following code: dotp = np.dot(X[i], w) mult = -Y[i] * dotp lhs = Y[i] * X[i] rhs = logistic(mult) s += lhs * rhs And it throws me the following error (truncated for brevity): File "/Users/leonsas/Projects/temp/learners/learners.py",…
leonsas
  • 4,718
  • 6
  • 43
  • 70
9
votes
3 answers

Multithreaded sparse matrix multiplication in Matlab

I am performing several matrix multiplications of an NxN sparse (~1-2%) matrix, let's call it B, with an NxM dense matrix, let's call it A (where M < N). N is large, as is M; on the order of several thousands. I am running Matlab 2013a. Now,…
9
votes
4 answers

Where is strassen's matrix multiplication useful?

Strassen's algorithm for matrix multiplication just gives a marginal improvement over the conventional O(N^3) algorithm. It has higher constant factors and is much harder to implement. Given these shortcomings, is strassens algorithm actually useful…
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
9
votes
4 answers

How to optimize this product of three matrices in C++ for x86?

I have a key algorithm in which most of its runtime is spent on calculating a dense matrix product: A*A'*Y, where: A is an m-by-n matrix, A' is its conjugate transpose, Y is an m-by-k matrix Typical characteristics: …
Jason R
  • 11,159
  • 6
  • 50
  • 81
9
votes
2 answers

Scipy LinearOperator With Multiple Inputs

I need to invert a large, dense matrix which I hoped to use Scipy's gmres to do. Fortunately, the dense matrix A follows a pattern and I do not need to store the matrix in memory. The LinearOperator class allows us to construct an object which acts…
user35959
  • 359
  • 2
  • 13