c++ - 求解系统(矩形全包 (RFP) 格式)

标签 c++ system lapack

我正在使用 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/

相关文章:

c++ - 如何将C++结构暴露给QML?

c++ - 尝试打开 TCP 套接字时出错 - C++

iphone - 在哪里可以找到 iPhone 系统按钮和图标图形?

c - 为什么存在系统调用

installation - Alpine Linux 无法在 python :3. 5-alpine3.4 上安装 lapack-dev

c++ - struct pcap_pkthdr len 总是 == 零

c++ - valgrind memcheck 给出消息 "in a rw- anonymous segment"

c++ - 如何从 C++ 程序运行另一个程序

c++ - LAPACK/BLAS 与简单的 "for"循环

c++ - 用于 C/C++ 的 LAPACK 包装器