0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Matrix calculation with c++

Last updated at Posted at 2023-02-11

Matrix calculation c++ code

I'll post the whole code first and then explain.
Inverse Matrix
Transposed Matrix
Identity Matrix
Matrix Addition
3X3 Matrix Multiplication
3X3 Matrix 3X1 Matrix Multiplication
COUT 3X3 Matrix
COUT 3X1 Matrix

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

/*----------------------- Display on terminal -------------------------*/
void cout_33matrix(double A[3][3])
{
  int i, j; 
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      cout << scientific<< setprecision(2)<< A[i][j] << " ";
    }
    cout << endl;
  }
}

void cout_31matrix(double ACmul[3][1])
{
  int i, j;
 
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 1; j++) {
      cout << scientific<< setprecision(2)<< ACmul[i][j] << " ";
    }
    cout << endl;
  }
}

/*--------------------------- Calculation -----------------------------*/
void calculator(void)
{   //For example
    double A[3][3]={
                        { 1, 3, 2},
                        {-1, 0, 1},
                        { 2, 3, 0},
    };
    double B[3][3]={
                        { 7, 1, 5},
                        { 4, 3, 1},
                        { 0, 2, 1},
    };
    double C[3][1]={
                      {2},
                      {3},
                      {5},
    };
    cout << "A = "<<endl;
    cout_33matrix(A);
    cout << "B = "<<endl;
    cout_33matrix(B);
    cout << "C = "<<endl;
    cout_31matrix(C);

    int i,j,k;

/*Inverse matrix*/
    double Ainverse[3][3];
    double det = A[0][0] * A[1][1] * A[2][2]
               + A[0][1] * A[1][2] * A[2][0]
               + A[0][2] * A[1][0] * A[2][1]
               - A[0][2] * A[1][1] * A[2][0]
               - A[0][1] * A[1][0] * A[2][2]
               - A[0][0] * A[1][2] * A[2][1];
    Ainverse[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / det;
    Ainverse[0][1] = - (A[0][1] * A[2][2] - A[0][2] * A[2][1]) / det;
    Ainverse[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) / det;
    Ainverse[1][0] = - (A[1][0] * A[2][2] - A[1][2] * A[2][0]) / det;
    Ainverse[1][1] = (A[0][0] * A[2][2] - A[0][2] * A[2][0]) / det;
    Ainverse[1][2] = - (A[0][0] * A[1][2] - A[0][2] * A[1][0]) / det;
    Ainverse[2][0] = (A[1][0] * A[2][1] - A[1][1] * A[2][0]) / det;
    Ainverse[2][1] = - (A[0][0] * A[2][1] - A[0][1] * A[2][0]) / det;
    Ainverse[2][2] = (A[0][0] * A[1][1] - A[0][1] * A[1][0]) / det;
    cout << "Ainverse = "<<endl;
    cout_33matrix(Ainverse);

/*Transposed matrix*/
    double Atransposed[3][3];
    for (i = 0; i < 3; i++){
        for (j = 0; j < 3; j++){
            Atransposed[j][i]=A[i][j];
        }
    }
    cout << "Atransposed = "<<endl;
    cout_33matrix(Atransposed);

/*Identity matrix*/
    double Aidentity[3][3];
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        Aidentity[i][j]=0.0;
        for(k=0;k<3;k++){
          Aidentity[i][j]+=A[i][k]*Ainverse[k][j];
        }
      }
    }
    cout << "Aidentity = "<<endl;
    cout_33matrix(Aidentity);

/*A + B*/
    double ABsum[3][3];  
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABsum[i][j]=0.0;
        ABsum[i][j]=A[i][j]+B[i][j];
      }
    }
    cout << "ABsum = "<<endl;
    cout_33matrix(ABsum);

/*A X B*/
    double ABmul[3][3];  
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ABmul[i][j]+=A[i][k]*B[k][j];
        }
      }
    }
    cout << "ABmul = "<<endl;
    cout_33matrix(ABmul);

/*A X C*/
    double ACmul[3][1]; 
    for(i=0;i<3;i++){
      for(j=0;j<1;j++){
        ACmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ACmul[i][j]+=A[i][k]*C[k][j];
        }
      }
    }
    cout << "ACmul = "<<endl;
    cout_31matrix(ACmul);

}

int main(void)
{   
    calculator();
    return 0;
}

Inverse Matrix

    double det = A[0][0] * A[1][1] * A[2][2]
               + A[0][1] * A[1][2] * A[2][0]
               + A[0][2] * A[1][0] * A[2][1]
               - A[0][2] * A[1][1] * A[2][0]
               - A[0][1] * A[1][0] * A[2][2]
               - A[0][0] * A[1][2] * A[2][1];
    Ainverse[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / det;
    Ainverse[0][1] = - (A[0][1] * A[2][2] - A[0][2] * A[2][1]) / det;
    Ainverse[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) / det;
    Ainverse[1][0] = - (A[1][0] * A[2][2] - A[1][2] * A[2][0]) / det;
    Ainverse[1][1] = (A[0][0] * A[2][2] - A[0][2] * A[2][0]) / det;
    Ainverse[1][2] = - (A[0][0] * A[1][2] - A[0][2] * A[1][0]) / det;
    Ainverse[2][0] = (A[1][0] * A[2][1] - A[1][1] * A[2][0]) / det;
    Ainverse[2][1] = - (A[0][0] * A[2][1] - A[0][1] * A[2][0]) / det;
    Ainverse[2][2] = (A[0][0] * A[1][1] - A[0][1] * A[1][0]) / det;

Transposed Matrix

    for (i = 0; i < 3; i++){
        for (j = 0; j < 3; j++){
            Atransposed[j][i]=A[i][j];
        }
    }

Identity Matrix

    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        Aidentity[i][j]=0.0;
        for(k=0;k<3;k++){
          Aidentity[i][j]+=A[i][k]*Ainverse[k][j];
        }
      }
    }

3 X 3 Matrix Addition

    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABsum[i][j]=0.0;
        ABsum[i][j]=A[i][j]+B[i][j];
      }
    }

3 X 3 Matrix Multiplication

    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ABmul[i][j]+=A[i][k]*B[k][j];
        }
      }
    }

3 X 3 Matrix 3 X 1 Matrix Multiplication

    for(i=0;i<3;i++){
      for(j=0;j<1;j++){
        ACmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ACmul[i][j]+=A[i][k]*C[k][j];
        }
      }
    }

COUT 3 X 3 Matrix

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      cout << scientific<< setprecision(2)<< A[i][j] << " ";
    }
    cout << endl;
  }

COUT 3 X 1 Matrix

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 1; j++) {
      cout << scientific<< setprecision(2)<< ACmul[i][j] << " ";
    }
    cout << endl;
  }

QiitaLink
Designed by RENOX

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?