- #1
missfangula
- 32
- 0
Trouble with BLAS subroutine dgemv -->vector- matrix product
Hello all,
So, I am trying to write some code that will compute the vector-matrix product using the BLAS subroutine dgemv. I have written the code below, but I am getting really wacky numbers! You will see that it takes array "Array" as the known matrix , and vector 'vector' as known vector, and outputs a product vector.
Does anyone know what maybe askew?
Thanks a million,
miss fangula
#import <Foundation/Foundation.h>
#include <Accelerate/Accelerate.h>
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
//*******FINDS X IN MATRIX EQUATION AB = X, WHERE A AND B ARE KNOWN MATRICES*******//
//
// [a a a] [b b b] [x x x]
// [a a a]*[b b b] = [x x x] {x,x,x,x,x,x,x,x,x} = ?
// [a a a] [b b b] [x x x]
//
//*********************************************************************************//
void printMatrix(char* name, int n, double* b, int lda);
/*Define all subroutine parameters*************************/
#define M 4 //Define 'Array' matrix size and number of rows in 'vector'
#define N 4 //Define number of columns in 'Array' (if a vector, then probably 1)
#define ALPHA 4 //Scaling constant alpha
#define LDA 4 //Leading dimension of 'Array'
#define INCX 1 //Stride value for 'vector' and can have any value
#define BETA 0 //Scaling constant beta
#define INCY 1 //Stride for 'y'
/**********************************************************/
int main()
{
double *vector;
double *y;
int m = M;//Define local variable n to be N
int n = N;//
int alpha = ALPHA;
int lda = LDA; //Define local variable lda to be LDA
int incx = INCX;
int beta = BETA;
int incy = INCY;
//**********
double a = 0.0;
double b = 180.0;
double R = 1.54;
//**********
double Array[16] = //Declare a NxN array 'Array'
{
-cos(a), sin(a)*cos(b), sin(a)*sin(b), 0.0,
-sin(a), -cos(a)*cos(b), -cos(a)*sin(b), 0.0,
0.0, -sin(b), cos(b), 0.0,
-R*cos(a), R*sin(a)*cos(b), R*sin(a)*sin(b), 1.0
};
vector = calloc(4, sizeof (double)); //Declare a vector to as other unknown in equation (see algorithm above)
y = calloc(4, sizeof (double));
*vector = 0.0; //Define first element of vector b
*(vector + 1) = 0.0; //Define second element of vector b
*(vector + 2) = 0.0;
*(vector + 3) = 1.0;
cblas_dgemv(CblasRowMajor, CblasNoTrans, m, n, alpha, Array, lda,
vector, incx, beta, y, incy);
/*cblas_dgemv(CblasRowMajor, CblasNoTrans, 3, 3, 1.0, m, 3,
x, 1, 0.0, y, 1);*/
printMatrix("Answer is: ", n, y, lda); //Print the solution matrix 'x'
free(vector); //free the input vector
free(y); //Free the solution vector
return 666; //since int main function, return some integer
}
void printMatrix (char* name, int n, double* b, int lda) //Function to print the solution matrix
{
int loopNumber;
int numTimes = n;
printf("\n %s\n", name); //Print name of solution matrix 'x'
for (loopNumber = 0; loopNumber < numTimes; loopNumber++) //For each element of vector
{
printf("%6.2f", b[loopNumber]); //Print the respective element at loopNumber position
printf( "\n" ); //Space
}
}
Hello all,
So, I am trying to write some code that will compute the vector-matrix product using the BLAS subroutine dgemv. I have written the code below, but I am getting really wacky numbers! You will see that it takes array "Array" as the known matrix , and vector 'vector' as known vector, and outputs a product vector.
Does anyone know what maybe askew?
Thanks a million,
miss fangula
#import <Foundation/Foundation.h>
#include <Accelerate/Accelerate.h>
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
//*******FINDS X IN MATRIX EQUATION AB = X, WHERE A AND B ARE KNOWN MATRICES*******//
//
// [a a a] [b b b] [x x x]
// [a a a]*[b b b] = [x x x] {x,x,x,x,x,x,x,x,x} = ?
// [a a a] [b b b] [x x x]
//
//*********************************************************************************//
void printMatrix(char* name, int n, double* b, int lda);
/*Define all subroutine parameters*************************/
#define M 4 //Define 'Array' matrix size and number of rows in 'vector'
#define N 4 //Define number of columns in 'Array' (if a vector, then probably 1)
#define ALPHA 4 //Scaling constant alpha
#define LDA 4 //Leading dimension of 'Array'
#define INCX 1 //Stride value for 'vector' and can have any value
#define BETA 0 //Scaling constant beta
#define INCY 1 //Stride for 'y'
/**********************************************************/
int main()
{
double *vector;
double *y;
int m = M;//Define local variable n to be N
int n = N;//
int alpha = ALPHA;
int lda = LDA; //Define local variable lda to be LDA
int incx = INCX;
int beta = BETA;
int incy = INCY;
//**********
double a = 0.0;
double b = 180.0;
double R = 1.54;
//**********
double Array[16] = //Declare a NxN array 'Array'
{
-cos(a), sin(a)*cos(b), sin(a)*sin(b), 0.0,
-sin(a), -cos(a)*cos(b), -cos(a)*sin(b), 0.0,
0.0, -sin(b), cos(b), 0.0,
-R*cos(a), R*sin(a)*cos(b), R*sin(a)*sin(b), 1.0
};
vector = calloc(4, sizeof (double)); //Declare a vector to as other unknown in equation (see algorithm above)
y = calloc(4, sizeof (double));
*vector = 0.0; //Define first element of vector b
*(vector + 1) = 0.0; //Define second element of vector b
*(vector + 2) = 0.0;
*(vector + 3) = 1.0;
cblas_dgemv(CblasRowMajor, CblasNoTrans, m, n, alpha, Array, lda,
vector, incx, beta, y, incy);
/*cblas_dgemv(CblasRowMajor, CblasNoTrans, 3, 3, 1.0, m, 3,
x, 1, 0.0, y, 1);*/
printMatrix("Answer is: ", n, y, lda); //Print the solution matrix 'x'
free(vector); //free the input vector
free(y); //Free the solution vector
return 666; //since int main function, return some integer
}
void printMatrix (char* name, int n, double* b, int lda) //Function to print the solution matrix
{
int loopNumber;
int numTimes = n;
printf("\n %s\n", name); //Print name of solution matrix 'x'
for (loopNumber = 0; loopNumber < numTimes; loopNumber++) //For each element of vector
{
printf("%6.2f", b[loopNumber]); //Print the respective element at loopNumber position
printf( "\n" ); //Space
}
}