Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用cblas_dgemv()的矩阵向量积的结果不正确_C++_Matrix Multiplication_Blas - Fatal编程技术网

C++ 使用cblas_dgemv()的矩阵向量积的结果不正确

C++ 使用cblas_dgemv()的矩阵向量积的结果不正确,c++,matrix-multiplication,blas,C++,Matrix Multiplication,Blas,我试图使用cblas.h中声明的例程“cblas_dgemv()”来计算矩阵向量积。代码如下: #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 c

我试图使用cblas.h中声明的例程“cblas_dgemv()”来计算矩阵向量积。代码如下:

#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<<"]";
}
#包括
#包括
使用名称空间std;
/*使用:g++test.cpp-o test-lblas编译*/
int main(int argc,字符**argv)
{
/*我们以列主形式存储以下3x4数组:
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 ];
*/
常数双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};
/*向量X是:
X=[0.570695,0.670179,0.927146,0.297343];
其中'是转置。
*/
常数双X[4]={0.570695,0.670179,0.927146,0.297343};
双Y[4]={0,0,0,0};
/*计算Y=A*X(α=1,β=0)
*/
cblas_dgemv(CblasColMajor、CblasNoTrans、3、4、1、A、3、X、1、0、Y、1);
/*如果我是对的,我应该得到:
Y=[1.69366 1.20999 1.72082 1.38618]=A*X;
但我得到:
Y=[1.73485 1.89473 1.64507 0]=A'*X;
*/

cout错的是你的数学。3x4矩阵和4分量向量的乘积是3分量向量。在你的例子中,a*X的乘积是[1.73485 1.89473 1.64507]

如果将A(4x3)的转置相乘,则需要一个三分量向量相乘,乘积是一个四分量向量。我们将X的前三个分量称为X->X'=[0.570695,0.670179,0.927146],然后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");

}
#包括
无效dgemv(常数双*A、常数双*u、双*v、常数整数n、常数整数m){

对于(int i=0;iYes,谢谢!我很不好意思错过了这么简单的事情!@Chatter,好吧,不要觉得太尴尬,如果你仔细查看我的编辑,你会发现我自己花了几次努力才把它弄好。