Defining a matrix as an array of arrays and comput

2019-05-10 16:53发布

问题:

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...

回答1:

Look at Simple 3x3 matrix inverse code (C++).



回答2:

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];


回答3:

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.



回答4:

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.



回答5:

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;}

 }


标签: c++-cli