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

Applying a "Spread" value to an XMFLOAT4X4

I'm attempting to add a small value to a World Matrix in order to replicate the accuracy of a fired weapon [pistol, assault rifle] Currently, my World Matrix resides at a Parent Objects' position, with the ability to rotate about the Y axis…
1
vote
2 answers

Python - Multiply matrices with different numbers of columns

I'm trying to multiple two matrices, A and B, where B has more columns than A using python and numpy preferably. Example: A = numpy.matrix([[2,3,15],[5,8,12],[1,13,4]], dtype=numpy.object) B =…
user2059300
  • 361
  • 1
  • 5
  • 17
1
vote
0 answers

Parallel matrix multiplication in C with MPI

I am new at parallel programming using MPI in C. So I am working on a matrix multiplication for example A*B. Here's my code in C but I have not yet any idea how I can implement the MPI in my code. #include #include #include…
JPerk
  • 325
  • 1
  • 2
  • 12
1
vote
1 answer

Spark code giving wrong matrix multiplication result

I have two matrices {1,2,3;4,5,6;7,8,9} and {1,4;2,5;3,6}. The following code is a matrix multiplication in apache spark. But it is giving me wrong output as {15.0,29.0;36.0,71.0;57.0,113.0}. I want to know where I am done mistake? …
Chandan
  • 764
  • 2
  • 8
  • 21
1
vote
2 answers

Accuracy issues with multiplication of matrices in Matlab R2012b

I have implemented a script that does constrained optimization for solving the optimal parameters of Support Vector Machines model. I noticed that my script for some reason gives inaccurate results (although very close to the real value). For…
1
vote
1 answer

Matrix multiplication using Pig Latin

I'm trying to perform a matrix multiplication on a simple 3 X 3 matrix using Pig. I'm neither unable to perform transpose nor group according to the row. Can someone help me on this please. Example Matrix A: 2 2 2 2 2 2 2 2 2 Matrix B: 1 1…
Anil
  • 420
  • 2
  • 16
1
vote
1 answer

Does numpy use spatial locality in memory while doing matrix multiplication?

While multiplying large matrices (say A and B, A.dot(B)), does numpy use spatial locality by computing the transpose of the B and using row wise multiplication, or does it access the elements of B in column-wise fashion which would lead to many…
Bharat
  • 2,139
  • 2
  • 16
  • 35
1
vote
1 answer

Solving Matrices in C using Lapack

So I have two matrices; One is 2 by 2 and the other is 2 by 1. I want to use the external library lapack to solve the linear system and it seems like I need to call the function dgesv_() which is Ax = B And I have to solve for x. So I am really…
Suliman Sharif
  • 607
  • 1
  • 9
  • 26
1
vote
3 answers

Need help debugging parallel matrix multiplication using MPI

I'm currently writing a program in C using MPI to perform matrix multiplication in parallel. I'm very new to C and MPI, so it's a pretty rough code. I can't seem to get my code to work, so could someone help me read through it and help me understand…
Sam Jiang
  • 23
  • 5
1
vote
1 answer

Large matrix operations: Multiplication in Scala/Apache Spark

I need to multiply two large matrices, X and Y. Typically X has ~500K rows and ~18K columns and Y has ~18K rows and ~18K columns. The matrix X is expected to be sparse and the matrix Y is expected to be sparse/dense. What is the ideal way of…
1
vote
1 answer

How to replace/modify something in a call to function 1 from within function 2 (both in their separate files)

The given task is to call a function from within another function, where both functions are handling matrices. Now lets call this function 1 which is in its own file: A = (1/dot(v,v))*(Ps'*Ps); Function 1 is called with the command: bpt =…
1
vote
1 answer

Optimize large matrices multiplication in Eigen

I'm doing some large stochastic matrices (at least 1000x1000) calculation in C++, using the Eigen Library, my code consists of the following functions : Eigen::VectorXd grid(...); initializes (element by element) a sorted vector of log-normally…
Naucle
  • 626
  • 12
  • 28
1
vote
0 answers

Sparse (COO format) matrix-vector multiplication using OpenMP

The following is the code for matrix-vector multiplication of a sparse matrix available in COO format for (int i=0; i
Prapanch Nair
  • 185
  • 1
  • 10
1
vote
1 answer

SUMMA (efficient distributed matrix multiplication) on Spark?

I'm trying to figure out if something like http://www.cs.utexas.edu/ftp/techreports/tr95-13.pdf is possible on Spark. Is it possible to access low level RDD functionality/distribution in the same kind of way as with MPI (Key concept for SUMMA is 2D…
1
vote
1 answer

Counting number of conflicting signs between two vectors

I have two vectors (actually 1xN matrices) that have numbers between [-1, 1]. I want to find the number of instances where the sign of two corresponding elements is not the same (sign(A[k]) ~= sign(B[k])). Is there a way to do this that's more…
Teknophilia
  • 758
  • 10
  • 23