我正在使用RFP格式存储矩阵,并尝试求解方程组。但是,结果是错误的。

我懂了

b = { 5.5, 10, 8.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;
}

最佳答案

您确定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?

关于c++ - 解决系统(矩形完整包装(RFP)格式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15524336/

10-11 16:06