简体   繁体   English

使用cblas_dgemv()的矩阵向量乘积的错误结果

[英]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. 我正在尝试使用cblas.h中声明的例程'cblas_dgemv()'计算矩阵向量乘积。 The code is given below: 代码如下:

#include <cblas.h>
#include <iostream>

using namespace std;

/* compile with: g++ test.cpp -o test -lblas */

int main (int argc, char** argv)
{
    /* We store the following 3x4 array in column major form:
        A = [1.83292  0.267964 0.382422 0.520162
             0.428562 0.720323 0.839606 1.30816
             0.388731 0.619452 1.01375  0.229333 ];
     */

     const double A[12] = {  1.83292,
                            0.428562,
                            0.388731,
                            0.267964,
                            0.720323,
                            0.619452,
                            0.382422,
                            0.839606,
                            1.01375,
                            0.520162,
                            1.30816,
                            0.229333};

    /* The vector X is:
        X = [ 0.570695, 0.670179, 0.927146, 0.297343 ]';
        where ' is transpose.
     */
    const double X[4] = { 0.570695, 0.670179, 0.927146, 0.297343};
    double Y[4] = {0, 0, 0, 0};

    /* Calculate Y = A*X (alpha = 1 and beta = 0)
     */
    cblas_dgemv(CblasColMajor, CblasNoTrans, 3, 4, 1, A, 3, X, 1, 0, Y, 1);

    /* If I was correct, I should have got:
            Y =[1.69366 1.20999 1.72082 1.38618] = A*X;
        but I get:
            Y = [1.73485 1.89473 1.64507 0] = A'*X;
     */
     cout<<"Y = [";
     for ( unsigned int i = 0; i < 4; i++ )
        cout<<Y[i]<<" ";
    cout<<"]";
}

However, instead of getting Y = A*X, I am consistently getting Y = A'*X, where ' represents transpose. 但是,我一直得到Y = A'* X,而不是得到Y = A * X,其中'表示转置。 I am not sure if I am doing some classical silly mistake somewhere, but after hours of trying, I could not figure out the problem. 我不确定我是否在某个地方犯了一些经典的愚蠢错误,但是经过数小时的尝试,我仍然无法找出问题所在。 Please Help!! 请帮忙!!

If it is required, I am using Linux version 3.2.0-4-amd64 ( Debian 4.6.3-14) ) #1 SMP Debian 3.2.57-3+deb7u2 and g++ (Debian 4.4.7-2) 4.4.7. 如果需要,我正在使用Linux版本3.2.0-4-amd64(Debian 4.6.3-14))#1 SMP Debian 3.2.57-3 + deb7u2和g ++(Debian 4.4.7-2)4.4.7 。 Thanks in advance. 提前致谢。

What's wrong is your math. 你的数学错了。 The product of a 3x4 matrix and a 4-component vector is a 3-component vector. 3 x 4矩阵和4分量向量的乘积是3分量向量。 In your case the product of A*X is [1.73485 1.89473 1.64507]. 在您的情况下,A * X的乘积为[1.73485 1.89473 1.64507]。

If you multiply the transpose of A (which is 4x3) you need a 3-component vector to multiply with and the product is a 4-component vector. 如果将A的转置相乘(4x3),则需要3分量向量与之相乘,乘积是4分量向量。 Let's call X' the first three components of X -> X' = [0.570695, 0.670179, 0.927146]. 我们将X'称为X'的前三个成分-> X'= [0.570695、0.670179、0.927146]。 Then A'*X' = [1.693662 1.209994 1.720827 1.386180]. 然后A'* X'= [1.693662 1.209994 1.720827 1.386180]。

#include <stdio.h>
void dgemv(const double *A, const double *u, double *v, const int n, const int m) {
    for(int i=0; i<n; i++) {
        double sum = 0;
        for(int j=0; j<m; j++) {
            sum += A[m*i+j]*u[j];
        }
        v[i] = sum;
    }
}

int main() {                          
    const double A[12] = {1.83292 , 0.267964, 0.382422, 0.520162,
                          0.428562, 0.720323, 0.839606, 1.30816 ,
                          0.388731, 0.619452, 1.01375 , 0.229333};            

    const double AT[12] = {1.83292 , 0.428562, 0.388731,
                           0.267964, 0.720323, 0.619452,
                           0.382422, 0.839606, 1.01375 ,
                           0.520162, 1.30816 , 0.229333};

    const double X[4]  = { 0.570695, 0.670179, 0.927146, 0.297343};
    const double X2[4] = { 0.570695, 0.670179, 0.927146};
    double Y[3], Y2[4];

    dgemv(A, X, Y,  3,4);
    for(int i=0; i<3; i++) printf("%f ", Y[i]); printf("\n");
    dgemv(AT, X2, Y2, 4,3);
    for(int i=0; i<4; i++) printf("%f ", Y2[i]); printf("\n");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM