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

Getting new coordinate position after AutoCad/WPF/SVG Transform

I'm trying to retrieve and save a (x,y) position for later reference in a 2D model in SVG. The original model is in Autocad and using a software tool to convert to WPF which gets converted again to SVG like this. var model = GetModel(); var…
Tim Davis
  • 524
  • 6
  • 17
1
vote
1 answer

Matrix Transformation Problem - Z Axis Rotation is Skewing

For a simple 2d game I'm making I'm trying to rotate sprites around the z axis using matrices. I'm clearly doing something wrong as when I attempt to rotate my sprite it looks like it's being rotated around the screen origin (bottom, left) and not…
Binary Bob
  • 11
  • 3
1
vote
1 answer

Multiply each element of a csr matrix column in Scipy

How can i multiply a particular column of a csr matrix with a fixed value (e.g. 5) My approach seems not to work. First i'm creating a update_vector of the same size as my matrix column filled with my default value. Then i utilize the…
J-H
  • 1,795
  • 6
  • 22
  • 41
1
vote
2 answers

CL_INVALID_KERNEL_ARGS in JOCL (a Java Binding to OpenCL).

Anybody encounter this kind of error on doing Matrix Multiplication in JOCL? Exception in thread "main" org.jocl.CLException: CL_INVALID_KERNEL_ARGS at org.jocl.CL.checkResult(CL.java:787) at org.jocl.CL.clEnqueueNDRangeKernel(CL.java:20802) at…
Renée
  • 31
  • 5
1
vote
1 answer

Complex Matrix Multiplication using gsl

I have a very simple question -- I just want to multiply two matrices with complex entries together in gsl in C. So for example I want to define a function gsl_matrix_complex *multiply( gsl_matrix_complex *A, gsl_matrix_complex *B ) { ??? } I've…
user2180027
1
vote
1 answer

Java program to mulitiply matrices by calling C code

So I'm interested in this concept. I have a lot of experience with programming in C and Fortran, but little to no Java experience. Is it feasible to be able to call C code (even C++) to multiply matrices within a Java code? My idea, in concept, is…
mjswartz
  • 715
  • 1
  • 6
  • 19
1
vote
1 answer

Working with Givens rotations

If we consider a matrix R of size pxp. If we want to multiply A'RA where A is equal to (I+Givens rotation). Here I is an identity matrix and ' denotes the transpose operator. We know that a Givens rotation is a sparse matrix written as: To perform…
1
vote
2 answers

FORTRAN is faster than C - for a matrix multiplication program running on a same processor, why?

I was running n*n matrix multiplication code using C and FORTRAN on a xeon processor system. I was surprised to see the real time difference between the two approaches. Why did the FORTRAN code gave me a faster execution time? I was using dgemm()…
beginner
  • 411
  • 4
  • 14
1
vote
1 answer

Broadcastable Numpy dot

I have an array H of dimensions (n0, n2) and an array W of dimensions (n0, n1, n2, n3) and I want to do the following operation: (H[:, None, :, None] * W).sum(axis=(0, 2)) As far as I know, the above line does not use BLAS libraries. Is there a way…
AdeB
  • 187
  • 1
  • 8
1
vote
1 answer

How do you store a discretized 3D domain (for solve PDE) in sparse format, when boundary conditions can change?

I am looking at solving a problem that is a PDE, and the 3D discretized domain can have a different boundary condition on each of the 6 boundaries (or all the same). What is the best way to put this sparse matrix into a compressed format? Is CSR…
Derek
  • 11,715
  • 32
  • 127
  • 228
1
vote
1 answer

Having quite different results in matrix multiplication with numpy and cudamat

I'm trying to do the same matrix multiplication using numpy and cudamat. The results are quite different. What am I doing wrong? I'm using: Ubuntu 14.04 cuda 7.0-28 nvidia 346.46 numpy 1.9.2 Python 2.7.10 Anaconda 2.2.0 (64-bit) cudamat (latest…
virilo
  • 139
  • 2
  • 11
1
vote
3 answers

How to multiply matrix having different size (without knowing exactly the size they will have)?

I have to multiply 2 matrices (scalar multiplication) that have different sizes. For instance, the 1st has size n-by-m and the 2nd n+1-by-m+1. The fact is that it is not always the case. I mean, sometimes the first has size n+1-by-m+1 and the 2nd…
1
vote
2 answers

Rotating matrix of points for a custom angle

I wanted to make matrix of points and rotate them for a custom angle. I get a bunch of 0's when I enter the desired angle, can't figure out why. Might be a pretty simple mistake, I'm kind of new to programming in C, trying to learn. Here's my…
pdjurisic
  • 25
  • 8
1
vote
2 answers

Is there a way to make array entries complex variables in NumPy?

I am working with numpy arrays in Python. Is there a way to keep the entries of an array as variables so that they follow proper matrix multiplication and other matrix functions (addition, determinant, etc.)? For example: import numpy as…
advocateofnone
  • 2,527
  • 3
  • 17
  • 39
1
vote
1 answer

Armadillo sparse real matrix multiplication with complex vector

I'm trying to multiply a sparse real matrix with a complex vector but the program does not compile. If I change the vector to real or the matrix to dense, then everything goes through. A sample code is: #define ARMA_64BIT_WORD #include…
MaviPranav
  • 335
  • 2
  • 14