Search code examples
cdoublematrix-multiplicationmex

C: matrix-vector product, multiplying two double numbers gives wrong sign


I'm trying to perform a simple matrix times vector multiplication and for some reason I am getting the wrong sign in my results in a couple of my multiplications. I have no idea why this is happening, any pointers would be greatly appreciated.

Here is my whole code, i.e. the matrix * vector function and a caller function (or whatever it's called) from mex - I'm running the code from Matlab via mex.

#include "mex.h"

void mxv(int m, int n, double *A, double *b, double *c) {
    double sum;
    int i, j;

    for (i = 0; i < m; i++) {
        sum = 0.0;
        for (j = 0; j < n; j++) {
            sum += A[i * n + j] * b[j];
        }
        c[i] = sum;
    }
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

    double *A, *b, *c;
    int i, j, Am, An;

    A = mxGetPr(prhs[0]);
    Am = (int)mxGetM(prhs[0]);   
    An = (int)mxGetN(prhs[0]); 

    c = malloc(Am * sizeof(double));
    b = mxGetPr(prhs[1]);

    mxv(Am, An, A, b, c); 

    for (i = 0; i < Am; i++)
        printf("c[%d] = %1.4f\n", i, c[i]);
 }

I call the mex function with the following inputs:

A = [-865.6634 0 0 0;
    0 -17002.6822 0 0;
    0 0 -1726.2421 2539.6267;
    0 0 -2539.6267 -1726.2421;]
b = [-0.00153521; -0.00011165; -0.00037659; 0.00044981]

The correct result should be:

1.3290
1.8983
1.7924
0.1799

But I get

c[0] = 1.3290
c[1] = 1.8983
c[2] = -0.4923
c[3] = -1.7329

So the first two are correct (c[0] and c[1]), but not the latter two.

I added a bunch of print statements into my code to try and figure out where the error occurs:

#include "mex.h"

void mxv(int m, int n, double *A, double *b, double *c) {
    double sum;
    int i, j;

    for (i = 0; i < m; i++) {
        sum = 0.0;
        printf("********\n");
        for (j = 0; j < n; j++) {
            printf("A[%d][%d] = %1.10f\nb[%d] = %1.10f\n", i , j, A[i + n * j], j, b[j]);
            printf("A[%d][%d]*b[%d] = %1.10f\n", i, j, j, A[i * n + j] * b[j]);
            sum += A[i * n + j] * b[j];
            printf("sum = %1.10f\n", sum);
        }
        c[i] = sum;
    }
} 

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]) {

    double *A, *b, *c;    
    int i, j, Am, An;

    A = mxGetPr(prhs[0]);
    Am = (int)mxGetM(prhs[0]);   
    An = (int)mxGetN(prhs[0]); 
    printf("size(A) = (%d,%d)\n", Am, An);
    for (i = 0; i < Am; i++) {
        for (j = 0; j < An; j++) {
            printf("A[%d][%d] = %1.4f\n", i, j, A[i + Am * j]);
        }
    }

    c = malloc(Am *sizeof(double));

    b = mxGetPr(prhs[1]);
    for (i = 0; i < Am; i++) {
        printf("b[%d] = %1.4f\n", i, b[i]);
    }
    mxv(Am, An, A, b, c);

    for (i = 0; i < Am; i++)
        printf("c[%d] = %1.4f\n", i, c[i]);    
}

I'm pretty confident that I'm getting the right inputs from Matlab:

size(A) = (4,4)
A[0][0] = -865.6634
A[0][1] = 0.0000
A[0][2] = 0.0000
A[0][3] = 0.0000
A[1][0] = 0.0000
A[1][1] = -17002.6822
A[1][2] = 0.0000
A[1][3] = 0.0000
A[2][0] = 0.0000
A[2][1] = 0.0000
A[2][2] = -1726.2421
A[2][3] = 2539.6267
A[3][0] = 0.0000
A[3][1] = 0.0000
A[3][2] = -2539.6267
A[3][3] = -1726.2421
b[0] = -0.0015
b[1] = -0.0001
b[2] = -0.0004
b[3] = 0.0004

But when I look into the matrix-vector multiplication I find that the multiplication resuts are for some reason having the wrong sign in some cases. This is what it prints out for i = 2:

********
A[2][0] = 0.0000000000
b[0] = -0.0015352100
A[2][0]*b[0] = -0.0000000000
sum = 0.0000000000
A[2][1] = 0.0000000000
b[1] = -0.0001116500
A[2][1]*b[1] = -0.0000000000
sum = 0.0000000000
A[2][2] = -1726.2421000000
b[2] = -0.0003765900
A[2][2]*b[2] = 0.6500855124
sum = 0.6500855124
A[2][3] = 2539.6267000000
b[3] = 0.0004498100
A[2][3]*b[3] = -1.1423494859 <- THIS SHOULD BE 1.142349... (no minus sign)
sum = -0.4922639735
********

Something similar happens for the i=3 case.

Thanks in advance!


Solution

  • In:

    printf("A[%d][%d] = %1.10f\nb[%d] = %1.10f\n", i , j, A[i + n * j], j, b[j]);
    printf("A[%d][%d]*b[%d] = %1.10f\n", i, j, j, A[i * n + j] * b[j]);
    

    The first printf uses A[i + n*j], while the second uses A[i*n + j]. These are transposed positions in the array.