Reputation: 193
I am trying to use the routine 'cblas_dgemv()' declared in cblas.h to calculate matrix vector product. 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. 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. Thanks in advance.
Upvotes: 1
Views: 602
Reputation: 33659
What's wrong is your math. The product of a 3x4 matrix and a 4-component vector is a 3-component vector. In your case the product of A*X is [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. Let's call X' the first three components of X -> X' = [0.570695, 0.670179, 0.927146]. Then 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");
}
Upvotes: 1