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

Matrix Computations Python

I have a function where I am calculating a value as: a = a + pow(b - np.dot(A[i,:],B[:,j]), 2) final_Result= a + C * ((np.square(A)).sum() +(np.square(B)).sum() ) print final_Result func.extend(final_Result) # C is a float value It shows…
user3407267
1
vote
1 answer

scipy/sklearn sparse matrix decomposition for document classification

I'm trying to do documentation classification on a large corpus (4 mil documents) and keep running into memory errors when using the standard scikit-learn methods. After cleaning/stemming my data, I have a very sparse matrix with about 1 mil words.…
1
vote
2 answers

How to display a result from a class?

I've been working on a program which multiplies matrices using threads. I written the program in a non-threaded fashion and it was working like a charm. However when I was writing it w/ threading function, it appears that I am not getting a result…
1
vote
1 answer

how can i convert my cpu code of dot product of two matrices to GPU in matlab

I want to take weighted sum of two matrices in GPUarray to be fast. for example my code on cpu is given below: mat1 = rand(19,19); mat2= rand(19,19); Receptive_fieldsize = [4,3]; overlap = 1; Output = GetweightedSum(mat1,mat2,…
khan
  • 531
  • 6
  • 29
1
vote
1 answer

Matrix and Tensor Multiplication

I have a matrix X of dimensions nx2. Using this matrix I want to construct a tensor Y of dimensions 2x2xn. So that Y(:, :, i) = X(i, :)'*X(i, :) Can this be done in Matlab without a loop using some linear algebra operation?
ssb
  • 7,422
  • 10
  • 36
  • 61
1
vote
1 answer

ejml library use mult() to multiply a matrix by a scalar

For example, I want to multiply the scalar, Gamma, by the NxN matrix, A, and return the result as the NxN matrix, B, i.e. B = Gamma * A. First, I create DenseMatrix64F A, DenseMatrix64F B and double Gamma. Then, I would like to use the…
dulang10
  • 11
  • 2
1
vote
2 answers

Matrix Multiplication in R without using %*% or crossprod

I am trying to multiply two matrix in R without using %*% or crossprod. what i have tried so far x <- matrix(1:4, ncol = 2) y <- matrix(5:8, ncol = 2) MatMul <- function(X,Y) { t(apply(x,1,crossprod,y)) } MatMul(x,y) I want to multiply…
1089
  • 135
  • 1
  • 1
  • 8
1
vote
1 answer

Fortran: efficient matrix-vector multiplication

I have a piece of code which is a significant bottleneck: do s = 1,ns msum = 0.d0 do k = 1,ns msum = msum + tm(k,s)*f(:,:,k) end do m(:,:,s) = msum end do This is a simple matrix-vector product…
nluigi
  • 1,263
  • 2
  • 15
  • 35
1
vote
3 answers

Does an algorithm exist that finds the product of a n*n matrix in less than n^3 iteration?

I read that there is an algorithm that can calculate the product of a matrix in n^(2.3) complexity, but was unable to find the algorithm.
Shankar
  • 417
  • 1
  • 6
  • 17
1
vote
1 answer

Which should I choose between vdsp_mmul or cblas_dgemm?

I'm using the Accelerate framework for the first time for a huge matrix multiplication, but I don't understand the difference between vDSP and CBLAS in this case. Are they different in performance?
1
vote
1 answer

Incorrect result for matrix-vector product using cblas_dgemv()

I am trying to use the routine 'cblas_dgemv()' declared in cblas.h to calculate matrix vector product. The code is given below: #include #include using namespace std; /* compile with: g++ test.cpp -o test -lblas */ int main…
Chatter
  • 193
  • 8
1
vote
1 answer

Optimizing numpy matrix operations (currently using a for loop)

I've written some code to compute n matrices based on n items in a list, and then multiply all matrices together at the end. The code is relatively slow and I'd like to learn more about python optimization. I've used the profiling tools and…
IanRoberts
  • 2,846
  • 5
  • 26
  • 33
1
vote
1 answer

Null Space Binary Matrix : Java

Here is my question: How to calcul the Kernel of a Binary Matrix ?? To calcul the Kernel (or Null Space if you prefer) in Java, it's pretty simple in the Real Space, there is already a lot of classes, so don't need to invent the wheel again, we just…
Valentin Montmirail
  • 2,594
  • 1
  • 25
  • 53
1
vote
0 answers

Two dimensional array with million rows and columns

I have a user-event data with user ratings for events user has attended.I am trying to use Jama library for similarity matrix which needs two dimensional array as input (users-event matrix with event ratings) the data i have as three columns : …
aman
  • 1,875
  • 4
  • 18
  • 27
1
vote
2 answers

How can I efficiently expand a factored tensor in numpy?

I have a 3D tensor factored as three 2D matrices, like equation 22 in this paper: http://www.iro.umontreal.ca/~memisevr/pubs/pami_relational.pdf My question is, if I want to calculate the tensor explicitly, is there a better way than this in…
capybaralet
  • 1,757
  • 3
  • 21
  • 31