Matrix vector product with dgemm / dgemv

Using Lapack with C ++ gives me a little headache. I found the functions defined for fortran a bit eccentric, so I tried to make some functions in C ++ to make it easier for me to read what was going on.

Anyway, I am not getting a matrix vector product that works the way I want. Here is a small example program.

smallmatlib.cpp:

#include <cstdio>
#include <stdlib.h>


extern "C"{
    // product C= alphaA.B + betaC                                               
   void dgemm_(char* TRANSA, char* TRANSB, const int* M,
               const int* N, const int* K, double* alpha, double* A,
               const int* LDA, double* B, const int* LDB, double* beta,
               double* C, const int* LDC);
    // product Y= alphaA.X + betaY                                               
   void dgemv_(char* TRANS, const int* M, const int* N,
               double* alpha, double* A, const int* LDA, double* X,
               const int* INCX, double* beta, double* C, const int* INCY);
   } 

void initvec(double* v, int N){
    for(int i= 0; i<N; ++i){
        v[i]= 0.0;
        }
   }

void matvecprod(double* A, double* v, double* u, int N){ 
    double alpha= 1.0, beta= 0.0;
    char no= 'N', tr= 'T';
    int m= N, n= N, lda= N, incx= N, incy= N;
    double* tmp= new double[N];
    initvec(tmp, N);
    dgemv_(&no,&m,&n,&alpha,A,&lda,v,&incx,&beta,tmp,&incy);
    for(int i= 0; i<N; ++i){
        u[i]= tmp[i];
        }
    delete [] tmp;
    }

void vecmatprod(double* v, double* A, double* u, int N){
    double alpha= 1.0, beta= 0.0;
    char no= 'N', tr= 'T';
    int m= N, n= 1, k= N, lda= N, ldb= N, ldc= N;
    double* tmp= new double[N];
    initvec(tmp, N);
    dgemm_(&no,&no,&m,&n,&k,&alpha,A,&lda,v,&ldb,&beta,tmp,&ldc);
    for(int i= 0; i<N; ++i){ 
        u[i]= tmp[i];
        }
    delete [] tmp;
    }

smallmatlib.h:

#ifndef SMALLMATLIB_H
#define SMALLMATLIB_H

void initvec(double* v, int N);

void matvecprod(double* A, double* v, double* u, int N);

void vecmatprod(double* v, double* A, double* u, int N);

#endif

smallmatlab.cpp:

#include "smallmatlib.h"
#include <cstdio>
#include <stdlib.h>
#define SIZE 2

int main(){
  double A[SIZE*SIZE]=
    { 1,2,
      3,4 };
  double v[SIZE]= {2,5.2};
  double u[SIZE]= {0,0};
  matvecprod(A,v,u,SIZE);
  printf("%f %f\n",u[0],u[1]);
  vecmatprod(v,A,u,SIZE);
  printf("%f %f\n",u[0],u[1]);
  return 0;
  }

Compilation:
  g ++ -c smallmatlib.cpp
  g ++ smallmatlab.cpp smallmatlib.o -L / usr / local / lib -lclapack -lcblas

The matvecprod function is now a problem. With an approximate matrix A and a vector of the vector v, it should output output, for example

12.4..    26.8..

but instead displays

2.00..    0.00..

dgemm, dgemv, . , incx incy , , .

,   vecmatprod (V, A, V, ) - , u, vecmatprod (v, A, u, SIZE). .

++, /, .

+4
1

, incx - 1, .

INCX is INTEGER
  On entry, INCX specifies the increment for the elements of
  X. INCX must not be zero.

, x ( , , ).

vecmatprod(v,A,v,SIZE) v x y. , (, wikipedia). x . :

y = A * x 

y = [ y1 y2 ]
A = [ [a11 a12] [a21 a22] ]
x = [ x1 x2 ]

y

y1 = a11 * x1 + a12 * x2
y2 = a21 * x1 + a22 * x2

, , y2, x1 x2, x = A * x ( ), x1 y1, , .

+2

All Articles