Matrix multiplication

Learn it by examples

On this page, you will find some simple and practical examples to perform matrix multiplication.

Matrix direct multiplication

To multiply two matrices A and B, the number of columns in A is equal to the number of rows in B.

/**
 * Calculates the direct product of two matrix
 * @param the first matrix, the second matrix
 */
void direct_product (matrix expansion, matrix m) {
    int i,j,k;
    matrix tmp = (matrix)malloc(sizeof(struct matrix_)); /*Temporary matrix for outer product*/
    /*Space allocation for tmp matrix*/
    int rows = expansion->rows, cols = expansion->cols;
    allocate_matrix (tmp, rows, cols);
    /*End space allocation for tmp wire*/
    copy (tmp, expansion);

    printf("\nCurrent matrix rows: %d, cols: %d\n", tmp->rows, tmp->cols);
    display(tmp);
    printf("\nDirect product by matrix m rows: %d, cols: %d\n", m->rows, m->cols);
    display(m);

    if(tmp->cols != m->rows)
    {
        printf("Direct product not possible: row | col mismatch\n");
        exit(0);
    }

    for (i=0; i<tmp->rows; i++)
        for(j=0; j<m->cols; j++)
        {
            expansion->dat[i][j]=0;
            for(k=0; k<tmp->cols; k++)
                expansion->dat[i][j]+= tmp->dat[i][k]*m->dat[k][j];
        }

    expansion->rows = tmp->rows;  //Update new expansion matrix size
    expansion->cols = m->cols;    //Update new expansion matrix size

    printf("\nResulting direct matrix rows: %d, cols: %d\n",expansion->rows, expansion->cols);
    display(expansion);

    free_matrix (tmp, rows);
}

Matrix outer product

To multiply to matrices A and B, the number of columns in A is equal to the number of rows in B.

/**
 * Calculates the outer product of two matrix
 * @param the first matrix, the second matrix
 */
void outer_product (matrix expansion, matrix m) {
    int i,j,k,l;
    matrix tmp = (matrix)malloc(sizeof(struct matrix_)); /*Temporary matrix for outer product*/
    /*Space allocation for tmp matrix*/
    int rows = expansion->rows, cols = expansion->cols;
    allocate_matrix (tmp, rows, cols);
    /*End space allocation for tmp wire*/
    copy (tmp, expansion); /*Copy the current expansion matrix in a temporary matrix*/
    printf("\nCurrent matrix rows: %d, cols: %d\n", tmp->rows, tmp->cols);
    display(tmp);
    printf("\nOuter product by matrix m rows: %d, cols: %d\n", m->rows, m->cols);
    display(m);
    /*Outer product operation*/
    for (i=0; i<tmp->rows; i++) {
        for (j=0; j<tmp->cols; j++) {
            for (k=m->rows*i; k<(m->rows*i)+m->rows; k++) {
                for (l=m->cols*j; l<(m->cols*j)+m->cols; l++)
                {
                    expansion->dat[k][l] = m->dat[k-(m->rows*i)][l-(m->cols*j)] * tmp->dat[i][j];
                }
            }
        }
    }
    expansion->rows = tmp->rows * m->rows; //Update new expansion matrix size
    expansion->cols = tmp->cols * m->cols; //Update new expansion matrix size

    printf("\nResulting outer product matrix rows: %d, cols: %d\n",expansion->rows, expansion->cols);
    display(expansion);

    free_matrix (tmp, rows);
}
Page tags