Defining a matrix as an array of arrays and calculating its inverse matrix in C ++

Unfortunately, I have little experience in C ++ and am trying to advance in C ++.

First, I defined an array of arrays to form a 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 an 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 calculates the inverse matrix of this input array of arrays?

could you help me?

Regards...

+3
source share
5 answers

In C ++ there are no functions for performing matrix operations, you need to find some library to do this or implement your own.

, C/++, :

double arr[3][3];
0

. , , , Fortran C ++.

0
0

++

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

 }
0

Source: https://habr.com/ru/post/1708416/


All Articles