c++lapackintel-mkllinear-equation

solving linear equationsystem in cpp using lapacke_dgesv


I am trying to solve a very simple equationsystem of the form M*Z=V, where M is a 2x2 Matrix, V and Z are Vectors with 2 entries. If i use Z = linsolve( M, V ); in Matlab the result is correct. I need to implement this in C++ somehow so I am trying to use the INTEL MKL Math library. Z should be calculated to be about [0.3091,0.6115]. But the LAPACKE_dgesv returns [1,2] for Z. What am I doing wrong?

#include <iostream>
#include <math.h>
#include <vector>
#include <numeric>
#include <mkl.h>
#include <stdlib.h>
#include <stdio.h>
#include "mkl_lapacke.h"
using namespace std;

 int main (){
    
    const int N = 2;
    const int NRHS = 1;
    const int LDA = N;
    const int LDB = NRHS;

    MKL_INT n = N, nrhs = NRHS, lda = LDA, ldb =LDB, info;
    MKL_INT z[2];
    
    double M[LDA * N] = {
        2, 1.5388, 
        1.5388,  2,
    };

    double V[LDB * N] = {
        1.5593,
        1.6987,
    };
    

    info = LAPACKE_dgesv(LAPACK_ROW_MAJOR, n, nrhs, M, lda, z, V, ldb);
    
    cout << "solution for z" "\n";
    for (int i = 0; i < 2; i++) {
        cout << z[i] << "     ";
    }


}

Solution

  • You should print V vector instead of z, z is just the pivot information.