[英]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, ie the matrix * vector function and a caller function (or whatever it's called) from mex - I'm running the code from Matlab via mex. 这是我的整个代码,即矩阵*向量函数和来自mex的调用函数(或其所谓的任何函数) - 我正在通过mex从Matlab运行代码。
#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: 我用以下输入调用mex函数:
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. 所以前两个是正确的(c [0]和c [1]),而不是后两个。
I added a bunch of print statements into my code to try and figure out where the error occurs: 我在我的代码中添加了一堆print语句,试图找出错误发生的位置:
#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: 我非常有信心从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:
这是它为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. 对于i = 3的情况,会发生类似的情况。
Thanks in advance! 提前致谢!
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]
. 第一个
printf
使用A[i + n*j]
,而第二个使用A[i*n + j]
。 These are transposed positions in the array. 这些是阵列中的转置位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.