LAPACK Solve系统(矩形全包装(RFP)格式)

时间:2013-03-20 13:03:21

标签: c++ system linear lapack

我正在使用RFP格式进行矩阵存储,并试图解决方程组。但结果是错误的。 =(请有人帮忙。

我明白了 b = {5.5,10,5.5}

但应该 b = {2.875,4.75,3.5}

我没有得到,我犯了错误。只需简单地使用标准函数:分解,然后求解分解矩阵。

谢谢。

#include "mkl.h"
#include "mkl_lapacke.h"
#include <math.h>
#include <iostream>
using namespace std;
#define NI 3
#define NJ 1

int main(int argc, char* argv[])
{

  double a[NI][NI];
  double b[NI][NJ];

  a[0][0] = 2;    a[0][1] = -1;    a[0][2] = 0;
  a[1][0] = 0;    a[1][1] = 2;    a[1][2] = -1;
  a[2][0] = 0;    a[2][1] = 0;    a[2][2] = 2;

  b[0][0] = 1;
  b[0][1] = 6;
  b[0][2] = 7;

  cout << "A1 = \n";
  for(int i = 0; i < NI; i++) {
     for(int j = 0; j < NI; j++) {
        cout << a[i][j] << "\t";
     }
     cout << "\n";
  }
  cout << "\n";



  cout << "B1 = \n";
  for(int i = 0; i < NI; i++) {
     for(int j = 0; j < NJ; j++) {
        cout << b[i][j] << "\t";
     }
     cout << "\n";
  }
  cout << "\n";

  char transr = 'N';
  char uplo = 'U';
  lapack_int n = NI;
  lapack_int lda = NI; //LDA is used to define the distance in memory between elements of two consecutive columns which have the same row index.
  double * arf = new double[ n * ( n + 1 ) / 2 ];
  lapack_int info = -1;

将一般矩阵转换为RFP格式

  info = LAPACKE_dtrttf(LAPACK_ROW_MAJOR, transr, uplo, n, *a, lda, arf);
  //lapack_int LAPACKE_<?>trttf( int matrix_order, char transr, char uplo, lapack_int n, const <datatype>* a, lapack_int lda, <datatype>* arf );
  cout << "LAPACKE_dtrttf = " << info << "\n";

  cout << "Rectangular full packed: \n";
  //cout.setf(std::ios::scientific);
  for(int i = 0; i < NI; i++) {
     for(int j = 0; j < (NI+1)/2; j++) {
        cout << arf[i * (NI+1)/2 + j] << "\t";
        //cout << arf[i] << "\t";
     }
     cout << "\n";
  }
  cout << "\n";

分解矩阵

  int matrix_order = LAPACK_ROW_MAJOR;
  transr = 'N';
  uplo = 'U';
  n = NI;

  info = LAPACKE_dpftrf( matrix_order, transr, uplo, n, arf );
  //lapack_int LAPACKE_<?>pftrf( int matrix_order, char transr, char uplo, lapack_int n, <datatype>* a );
  cout << "INFO LAPACKE_dpftrf = " << info << "\n";
  cout << "Factorized matrix: " << endl;

  for(int i = 0; i < NI; i++) {
     for(int j = 0; j < (NI+1)/2; j++) {
        cout << arf[i*(NI+1)/2+j] << "\t";
     }
     cout << "\n";
  }
  cout << "\n";

解决系统

  lapack_int nrhs = NJ;
  lapack_int ldb = NJ;
  info =  LAPACKE_dpftrs( matrix_order, transr, uplo, n, nrhs, arf, &b[0][0], ldb );
  //lapack_int LAPACKE_<?>pftrs( int matrix_order, char transr, char uplo, lapack_int n, lapack_int nrhs, const <datatype>* a, <datatype>* b, lapack_int ldb );
  cout << "INFO LAPACKE_dpftrs = " << info << "\n";

结果

cout << "Solved = \n";
  for(int i = 0; i < NI; i++) {
     for(int j = 0; j < NJ; j++) {
        cout << b[i][j] << "\t";
     }
     cout << "\n";
  }
  cout << "\n";
   delete [] arf;

   char ch;
   cin.get(ch);

   return 0;
}

1 个答案:

答案 0 :(得分:1)

您确定NJ的值对于以下内容是否正确:

b[NI][NJ];
b[0][0] = 1;
b[0][1] = 6;
b[0][2] = 7;

其中NI = 3且NJ = 1 ......

我认为你错误地将行的值放在了列上。如果是问题,那么你可能想知道为什么它没有给出任何错误,这又是一个已经存在的问题: Accessing an array out of bounds gives no error, why?