将矩阵定义为数组数组并在 C++ 中计算其逆矩阵;

发布于 2024-07-20 01:46:47 字数 520 浏览 7 评论 0原文

不幸的是,我在 C++ 方面没有太多经验,并且我正在努力在 C++ 方面取得进步。

首先,我定义了数组数组,以便形成一个 3x3 矩阵:

array< array< double >^ >^ input = gcnew array< array< double >^ >(3);

for (j=0;j<input->Length;j++){
    input[j]=gcnew array<double>(3);

然后我将矩阵元素分配给输入数组数组:

 int value=1;
for(y=0;y<(3);y++){
  for(x=0;x<(3);x++)
 {input[y][x]=value;
  value=value+1;
  }
  }

是否有一个 C++ 函数可以计算此输入数组数组的逆矩阵?

请问你能帮帮我吗?

此致...

Unfortunately I haven't much experience in C++ and I'm trying to progress myself in C++.

Firstly,I defined array of arrays so that I formed an 3x3 matrix:

array< array< double >^ >^ input = gcnew array< array< double >^ >(3);

for (j=0;j<input->Length;j++){
    input[j]=gcnew array<double>(3);

Then I assigned matrix elements to input array of arrays:

 int value=1;
for(y=0;y<(3);y++){
  for(x=0;x<(3);x++)
 {input[y][x]=value;
  value=value+1;
  }
  }

Is there a C++ function that compute inverse matrix of this input array of arrays?

Could you help me please?

Best Regards...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清旖 2024-07-27 01:46:48

使用 C++ 中的基本行变换实现矩阵逆的代码

#include<iostream>
#include<stdio.h>
#include<conio.h>
using
namespace std;

float a[4][4];float b[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
int no = 4;

int check(int k) {
    float cont = 0, cont2 = 0;
    for (int i = k; i < no; i++) {
        if (a[i][k] == 1) {
            for (int j = 0; j < no; j++) {
                cont = a[i][j];
                cont2 = b[i][j];
                a[i][j] = a[k][j];
                b[i][j] = b[k][j];
                a[k][j] = cont;
                b[k][j] = cont2;
            }
        } else if (a[i][k] == 0) {
            for (int j = 0; j < no; j++) {
                cont = a[i][j];
                cont2 = b[i][j];
                a[i][j] = a[no - 1][j];
                b[i][j] = b[no - 1][j];
                a[no - 1][j] = cont;
                b[no - 1][j] = cont2;
            }
        }
    }
    return 0;
}

int divi(int k) {
    float particular = a[k][k];
    for (int i = 0; i < no; i++) {
        a[k][i] = a[k][i] / particular;
        b[k][i] = b[k][i] / particular;
        if (a[k][i] == (-0)) {
            a[k][i] = 0;
        }
        if (b[k][i] == (-0)) {
            b[k][i] = 0;
        }
    }
    return 0;
}

int sub1(int k) {
    float particular;
    for (int j = k + 1; j < no; j++) {
        particular = a[j][k];
        for (int i = 0; i < no; i++) {
            a[j][i] = a[j][i] - (particular * a[k][i]);
            b[j][i] = b[j][i] - (particular * b[k][i]);
            if (a[j][i] == (-0)) {
                a[j][i] = 0;
            }
            if (b[j][i] == (-0)) {
                b[j][i] = 0;
            }
        }
    }
    return 0;
}

int sub2(int k) {
    float particular;
    for (int j = k - 1; j >= 0; j--) {
        particular = a[j][k];
        for (int i = no - 1; i >= 0; i--) {
            a[j][i] = a[j][i] - (particular * a[k][i]);
            b[j][i] = b[j][i] - (particular * b[k][i]);
            if (a[j][i] == (-0)) {
                a[j][i] = 0;
            }
            if (b[j][i] == (-0)) {
                b[j][i] = 0;
            }
        }
    }
    return 0;
}

int disp(){
 cout<<endl;
             for(int x=0;x<no;x++){
             for(int y=0;y<no;y++){
                   if(a[x][y]==(-0)){a[x][y]=0;}
                   if(b[x][y]==(-0)){b[x][y]=0;}
                   printf("%0.1f|||%0.1f     ",a[x][y],b[x][y]);
             }
             cout<<endl;}
}

int main()
{
    for(int i=0;i<no;i++){
          for(int j=0;j<no;j++){cout<<"Enter a["<<i<<"]["<<j<<"]";cin>>a[i}[j];}
          }
    for(int i=0;i<no;i++){
            for(int j=0;j<no;j++){cout<<a[i][j]<<" ";}
    cout<<endl;
          }

    for(int i=0;i<no;i++){
        check(i);
            disp();
        divi(i);
            disp();
        sub1(i);
            disp();
    }
    for(int i=no-1;i>=0;i--){
     sub2(i);
     disp();
     cout<<endl;
     }
     getch();
     getch();
     getch();
 return 0;}

 }

Code for inverse of matrix using Elementary row transformation in c++

#include<iostream>
#include<stdio.h>
#include<conio.h>
using
namespace std;

float a[4][4];float b[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
int no = 4;

int check(int k) {
    float cont = 0, cont2 = 0;
    for (int i = k; i < no; i++) {
        if (a[i][k] == 1) {
            for (int j = 0; j < no; j++) {
                cont = a[i][j];
                cont2 = b[i][j];
                a[i][j] = a[k][j];
                b[i][j] = b[k][j];
                a[k][j] = cont;
                b[k][j] = cont2;
            }
        } else if (a[i][k] == 0) {
            for (int j = 0; j < no; j++) {
                cont = a[i][j];
                cont2 = b[i][j];
                a[i][j] = a[no - 1][j];
                b[i][j] = b[no - 1][j];
                a[no - 1][j] = cont;
                b[no - 1][j] = cont2;
            }
        }
    }
    return 0;
}

int divi(int k) {
    float particular = a[k][k];
    for (int i = 0; i < no; i++) {
        a[k][i] = a[k][i] / particular;
        b[k][i] = b[k][i] / particular;
        if (a[k][i] == (-0)) {
            a[k][i] = 0;
        }
        if (b[k][i] == (-0)) {
            b[k][i] = 0;
        }
    }
    return 0;
}

int sub1(int k) {
    float particular;
    for (int j = k + 1; j < no; j++) {
        particular = a[j][k];
        for (int i = 0; i < no; i++) {
            a[j][i] = a[j][i] - (particular * a[k][i]);
            b[j][i] = b[j][i] - (particular * b[k][i]);
            if (a[j][i] == (-0)) {
                a[j][i] = 0;
            }
            if (b[j][i] == (-0)) {
                b[j][i] = 0;
            }
        }
    }
    return 0;
}

int sub2(int k) {
    float particular;
    for (int j = k - 1; j >= 0; j--) {
        particular = a[j][k];
        for (int i = no - 1; i >= 0; i--) {
            a[j][i] = a[j][i] - (particular * a[k][i]);
            b[j][i] = b[j][i] - (particular * b[k][i]);
            if (a[j][i] == (-0)) {
                a[j][i] = 0;
            }
            if (b[j][i] == (-0)) {
                b[j][i] = 0;
            }
        }
    }
    return 0;
}

int disp(){
 cout<<endl;
             for(int x=0;x<no;x++){
             for(int y=0;y<no;y++){
                   if(a[x][y]==(-0)){a[x][y]=0;}
                   if(b[x][y]==(-0)){b[x][y]=0;}
                   printf("%0.1f|||%0.1f     ",a[x][y],b[x][y]);
             }
             cout<<endl;}
}

int main()
{
    for(int i=0;i<no;i++){
          for(int j=0;j<no;j++){cout<<"Enter a["<<i<<"]["<<j<<"]";cin>>a[i}[j];}
          }
    for(int i=0;i<no;i++){
            for(int j=0;j<no;j++){cout<<a[i][j]<<" ";}
    cout<<endl;
          }

    for(int i=0;i<no;i++){
        check(i);
            disp();
        divi(i);
            disp();
        sub1(i);
            disp();
    }
    for(int i=no-1;i>=0;i--){
     sub2(i);
     disp();
     cout<<endl;
     }
     getch();
     getch();
     getch();
 return 0;}

 }
小耗子 2024-07-27 01:46:47

C++ 中没有函数可以进行矩阵运算,您需要找到一些库来执行此操作或实现您自己的库。

请注意,对于固定大小的数组,您可以使用常规 C/C++ 数组,如下所示:

double arr[3][3];

There are no functions in C++ to make matrix operations, you'll need to find some library to do it or implement your own.

Note that for fixed size arrays, you can use regular C/C++ arrays, like this one:

double arr[3][3];
很酷不放纵 2024-07-27 01:46:47

维基百科有一个各种编程语言的数值库列表。 如果您正在寻找的函数不可用,您还可以考虑围绕可用的 Fortran 或 C 或 C++ 实现编写包装函数。

Wikipedia has a list of numerical libraries in various programming languages. If the functions that you are looking for are not available you could also consider writing a wrapper function around an available Fortran or C or C++ implementation.

执笔绘流年 2024-07-27 01:46:47

没有内置的 C++ 函数来进行矩阵求逆(或任何其他矩阵计算)。

这里提到了很多矩阵求逆的方法

如果您想要高效且易于实施,那么高斯消除< /a> 是
可能是最好的。

There is not built in C++ function to do matrix inversion (or for any other matrix calculations).

There are lots of methods for matrix inversion mentioned HERE.

If you want efficiency and ease of implementation then Guassian Elimination is
probably the best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文